Refonte du code

This commit is contained in:
Yves ASTIER 2013-08-11 13:30:41 +02:00
parent 3c929e0aa6
commit 927b04dfef
20 changed files with 1137 additions and 475 deletions

272
index.php
View file

@ -1,96 +1,78 @@
<?php
require_once('rss-bridge-lib.php');
define('PATH_BRIDGES_RELATIVE', 'bridges/');
define('PATH_BRIDGES', __DIR__ . DIRECTORY_SEPARATOR . 'bridges' . DIRECTORY_SEPARATOR);
/*
TODO :
- gérer la détection du SSL
- faire la création de l'objet en dehors du bridge
- manage SSL detection because if library isn't load, some bridge crash !
- factorize the annotation system
- factorize to adapter : Format, Bridge, Cache (actually code is almost the same)
- implement annotation cache for entrance page
- Cache : I think logic must be change as least to avoid to reconvert object from json in FileCache case.
- add namespace to avoid futur problem ?
- see FIXME mentions in the code
- implement header('X-Cached-Version: '.date(DATE_ATOM, filemtime($cachefile)));
*/
/**
* Read bridge dir and catch informations about each bridge
* @param string @pathDirBridge Dir to the bridge path
* @return array Informations about each bridge
*/
function searchBridgeInformation($pathDirBridge){
$searchCommonPattern = array('description', 'name');
$listBridge = array();
if($handle = opendir($pathDirBridge)) {
while(false !== ($entry = readdir($handle))) {
if( preg_match('@([^.]+)\.php@U', $entry, $out) ){ // Is PHP file ?
$infos = array(); // Information about the bridge
$resParse = token_get_all(file_get_contents($pathDirBridge . $entry)); // Parse PHP file
foreach($resParse as $v){
if( is_array($v) && $v[0] == T_DOC_COMMENT ){ // Lexer node is COMMENT ?
$commentary = $v[1];
foreach( $searchCommonPattern as $name){ // Catch information with common pattern
preg_match('#@' . preg_quote($name, '#') . '\s+(.+)#', $commentary, $outComment);
if( isset($outComment[1]) ){
$infos[$name] = $outComment[1];
}
}
date_default_timezone_set('UTC');
error_reporting(0);
ini_set('display_errors','1'); error_reporting(E_ALL); // For debugging only.
preg_match_all('#@use(?<num>[1-9][0-9]*)\s?\((?<args>.+)\)(?:\r|\n)#', $commentary, $outComment); // Catch specific information about "use".
if( isset($outComment['args']) && is_array($outComment['args']) ){
$infos['use'] = array();
foreach($outComment['args'] as $num => $args){ // Each use
preg_match_all('#(?<name>[a-z]+)="(?<value>.*)"(?:,|$)#U', $args, $outArg); // Catch arguments for current use
if( isset($outArg['name']) ){
$usePos = $outComment['num'][$num]; // Current use name
if( !isset($infos['use'][$usePos]) ){ // Not information actually for this "use" ?
$infos['use'][$usePos] = array();
}
try{
require_once __DIR__ . '/lib/RssBridge.php';
foreach($outArg['name'] as $numArg => $name){ // Each arguments
$infos['use'][$usePos][$name] = $outArg['value'][$numArg];
}
}
}
}
}
Bridge::setDir(__DIR__ . '/bridges/');
Format::setDir(__DIR__ . '/formats/');
Cache::setDir(__DIR__ . '/caches/');
if( isset($_REQUEST) && isset($_REQUEST['action']) ){
switch($_REQUEST['action']){
case 'display':
if( isset($_REQUEST['bridge']) ){
unset($_REQUEST['action']);
$bridge = $_REQUEST['bridge'];
unset($_REQUEST['bridge']);
$format = $_REQUEST['format'];
unset($_REQUEST['format']);
// FIXME : necessary ?
// ini_set('user_agent', 'Mozilla/5.0 (X11; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0');
$cache = Cache::create('FileCache');
// Data retrieval
$bridge = Bridge::create($bridge);
$bridge
->setCache($cache) // Comment this lign for avoid cache use
->setDatas($_REQUEST);
// Data transformation
$format = Format::create($format);
$format
->setDatas($bridge->getDatas())
->setExtraInfos(array(
'name' => $bridge->getName(),
'uri' => $bridge->getURI(),
))
->display();
die;
}
if( isset($infos['name']) ){ // If informations containt at least a name
// $listBridge
$listBridge[$out[1]] = $infos;
}
}
break;
}
closedir($handle);
}
return $listBridge;
}
function createNetworkLink($bridgeName, $arguments){
}
if( isset($_REQUEST) && isset($_REQUEST['action']) ){
switch($_REQUEST['action']){
case 'create':
if( isset($_REQUEST['bridge']) ){
unset($_REQUEST['action']);
$bridge = $_REQUEST['bridge'];
unset($_REQUEST['bridge']);
// var_dump($_REQUEST);die;
$pathBridge = PATH_BRIDGES_RELATIVE . $bridge . '.php';
if( file_exists($pathBridge) ){
require $pathBridge;
exit();
}
}
break;
}
}
catch(HttpException $e){
header('HTTP/1.1 ' . $e->getCode() . ' ' . Http::getMessageForCode($e->getCode()));
header('Content-Type: text/plain');
die($e->getMessage());
}
catch(\Exception $e){
die($e->getMessage());
}
$listBridge = searchBridgeInformation(PATH_BRIDGES);
// echo '<pre>';
// var_dump($listBridge);
// echo '</pre>';
function getHelperButtonFormat($value, $name){
return '<button type="submit" name="format" value="' . $value . '">' . $name . '</button>';
}
$bridges = Bridge::searchInformation();
$formats = Format::searchInformation();
?>
<!DOCTYPE html>
<html lang="en">
@ -99,50 +81,92 @@ $listBridge = searchBridgeInformation(PATH_BRIDGES);
<title>Rss-bridge - Create your own network !</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Rss-bridge" />
<style type="text/css">
*{margin:0;padding:0}
fieldset,img{border:0}
ul,ol{list-style-type:none}
body{background:#fff;color:#000;}
h1{font-size:2rem;margin-bottom:1rem;text-shadow:0 3px 3px #aaa;}
button{cursor:pointer;border:1px solid #959595;border-radius:4px;
background-image: linear-gradient(top, rgb(255,255,255) 0%, rgb(237,237,237) 100%);
background-image: -o-linear-gradient(top, rgb(255,255,255) 0%, rgb(237,237,237) 100%);
background-image: -moz-linear-gradient(top, rgb(255,255,255) 0%, rgb(237,237,237) 100%);
background-image: -webkit-linear-gradient(top, rgb(255,255,255) 0%, rgb(237,237,237) 100%);
background-image: -ms-linear-gradient(top, rgb(255,255,255) 0%, rgb(237,237,237) 100%);
}
button:hover{
background-image: linear-gradient(top, rgb(237,237,237) 0%, rgb(255,255,255) 100%);
background-image: -o-linear-gradient(top, rgb(237,237,237) 0%, rgb(255,255,255) 100%);
background-image: -moz-linear-gradient(top, rgb(237,237,237) 0%, rgb(255,255,255) 100%);
background-image: -webkit-linear-gradient(top, rgb(237,237,237) 0%, rgb(255,255,255) 100%);
background-image: -ms-linear-gradient(top, rgb(237,237,237) 0%, rgb(255,255,255) 100%);
}
input[type="text"]{width:14rem;padding:.1rem;}
.main{width:98%;margin:0 auto;font-size:1rem;}
.list-bridge > li:first-child{margin-top:0;}
.list-bridge > li{background:#f5f5f5;padding:.5rem 1rem;margin-top:2rem;border-radius:4px;
-webkit-box-shadow: 0px 0px 6px 2px #cfcfcf;
box-shadow: 0px 0px 6px 2px #cfcfcf;
}
.list-bridge > li .name{font-size:1.4rem;}
.list-bridge > li .description{font-size:.9rem;color:#717171;margin-bottom:.5rem;}
.list-bridge > li label{display:none;}
.list-bridge > li .list-use > li:first-child{margin-top:0;}
.list-bridge > li .list-use > li{margin-top:.5rem;}
#origin{text-align:center;margin-top:2rem;}
</style>
</head>
<body>
<ul class="list-bridge">
<?php foreach($listBridge as $bridgeReference => $bridgeInformations): ?>
<li id="bridge-<?php echo $bridgeReference ?>" data-ref="<?php echo $bridgeReference ?>">
<div class="name"><?php echo $bridgeInformations['name'] ?></div>
<div class="informations">
<p class="description">
<?php echo isset($bridgeInformations['description']) ? $bridgeInformations['description'] : 'No description provide' ?>
</p>
<?php if( isset($bridgeInformations['use']) && count($bridgeInformations['use']) > 0 ): ?>
<ol class="list-use">
<?php foreach($bridgeInformations['use'] as $anUseNum => $anUse): ?>
<li data-use="<?php echo $anUseNum ?>">
<form method="POST" action="?">
<input type="hidden" name="action" value="create" />
<input type="hidden" name="bridge" value="<?php echo $bridgeReference ?>" />
<?php foreach($anUse as $argName => $argDescription): ?>
<?php
$idArg = 'arg-' . $bridgeReference . '-' . $anUseNum . '-' . $argName;
?>
<label for="<?php echo $idArg ?>"><?php echo $argDescription ?></label><input id="<?php echo $idArg ?>" type="text" value="" name="<?php echo $argName ?>" /><br />
<?php endforeach; ?>
<button type="submit" name="format" value="json">Json</button>
<button type="submit" name="format" value="plaintext">Text</button>
<button type="submit" name="format" value="html">HTML</button>
<button type="submit" name="format" value="atom">ATOM</button>
</form>
</li>
<?php endforeach; ?>
</ol>
<?php else: ?>
<form method="POST" action="?">
<input type="hidden" name="action" value="create" />
<input type="hidden" name="bridge" value="<?php echo $bridgeReference ?>" />
<button type="submit" name="format" value="json">Json</button>
<button type="submit" name="format" value="plaintext">Text</button>
<button type="submit" name="format" value="html">HTML</button>
<button type="submit" name="format" value="atom">ATOM</button>
</form>
<?php endif; ?>
</div>
</li>
<?php endforeach; ?>
</ul>
<div class="main">
<h1>RSS-Bridge</h1>
<ul class="list-bridge">
<?php foreach($bridges as $bridgeReference => $bridgeInformations): ?>
<li id="bridge-<?php echo $bridgeReference ?>" data-ref="<?php echo $bridgeReference ?>">
<div class="name"><?php echo $bridgeInformations['name'] ?></div>
<div class="informations">
<p class="description">
<?php echo isset($bridgeInformations['description']) ? $bridgeInformations['description'] : 'No description provide' ?>
</p>
<?php if( isset($bridgeInformations['use']) && count($bridgeInformations['use']) > 0 ): ?>
<ol class="list-use">
<?php foreach($bridgeInformations['use'] as $anUseNum => $anUse): ?>
<li data-use="<?php echo $anUseNum ?>">
<form method="GET" action="?">
<input type="hidden" name="action" value="display" />
<input type="hidden" name="bridge" value="<?php echo $bridgeReference ?>" />
<?php foreach($anUse as $argName => $argDescription): ?>
<?php
$idArg = 'arg-' . $bridgeReference . '-' . $anUseNum . '-' . $argName;
?>
<label for="<?php echo $idArg ?>"><?php echo $argDescription ?></label><input id="<?php echo $idArg ?>" type="text" value="" name="<?php echo $argName ?>" placeholder="<?php echo $argDescription ?>" />
<?php endforeach; ?>
<?php foreach( $formats as $name => $infos ): ?>
<?php if( isset($infos['name']) ){ echo getHelperButtonFormat($name, $infos['name']); } ?>
<?php endforeach; ?>
</form>
</li>
<?php endforeach; ?>
</ol>
<?php else: ?>
<form method="GET" action="?">
<input type="hidden" name="action" value="display" />
<input type="hidden" name="bridge" value="<?php echo $bridgeReference ?>" />
<?php foreach( $formats as $name => $infos ): ?>
<?php if( isset($infos['name']) ){ echo getHelperButtonFormat($name, $infos['name']); } ?>
<?php endforeach; ?>
</form>
<?php endif; ?>
</div>
</li>
<?php endforeach; ?>
</ul>
<p id="origin">
<a href="">RSS-Bridge</a>
</p>
</div>
</body>
</html>