1
0

index.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /*
  3. TODO :
  4. - manage SSL detection because if library isn't loaded, some bridge crash !
  5. - factorize the annotation system
  6. - factorize to adapter : Format, Bridge, Cache (actually code is almost the same)
  7. - implement annotation cache for entrance page
  8. - Cache : I think logic must be change as least to avoid to reconvert object from json in FileCache case.
  9. - add namespace to avoid futur problem ?
  10. - see FIXME mentions in the code
  11. - implement header('X-Cached-Version: '.date(DATE_ATOM, filemtime($cachefile)));
  12. */
  13. date_default_timezone_set('UTC');
  14. error_reporting(0);
  15. //ini_set('display_errors','1'); error_reporting(E_ALL); // For debugging only.
  16. // extensions check
  17. if (!extension_loaded('openssl'))
  18. die('"openssl" extension not loaded. Please check "php.ini"');
  19. // FIXME : beta test UA spoofing, please report any blacklisting by PHP-fopen-unfriendly websites
  20. ini_set('user_agent', 'Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20121202 Firefox/30.0 (rss-bridge/0.1; +https://github.com/sebsauvage/rss-bridge)');
  21. // -------
  22. // default whitelist
  23. $whitelist_file = './whitelist.txt';
  24. $whitelist_default = array(
  25. "BandcampBridge",
  26. "CryptomeBridge",
  27. "DansTonChatBridge",
  28. "DuckDuckGoBridge",
  29. "FlickrExploreBridge",
  30. "GooglePlusPostBridge",
  31. "GoogleSearchBridge",
  32. "IdenticaBridge",
  33. "InstagramBridge",
  34. "OpenClassroomsBridge",
  35. "PinterestBridge",
  36. "ScmbBridge",
  37. "TwitterBridge",
  38. "WikipediaENBridge",
  39. "WikipediaEOBridge",
  40. "WikipediaFRBridge",
  41. "YoutubeBridge");
  42. if (!file_exists($whitelist_file)) {
  43. $whitelist_selection = $whitelist_default;
  44. $whitelist_write = implode("\n", $whitelist_default);
  45. file_put_contents($whitelist_file, $whitelist_write);
  46. }
  47. else {
  48. $whitelist_selection = explode("\n", file_get_contents($whitelist_file));
  49. }
  50. // whitelist control function
  51. function BridgeWhitelist( $whitelist, $name ) {
  52. if(in_array("$name", $whitelist) or in_array("$name.php", $whitelist))
  53. return TRUE;
  54. else
  55. return FALSE;
  56. }
  57. try{
  58. require_once __DIR__ . '/lib/RssBridge.php';
  59. Bridge::setDir(__DIR__ . '/bridges/');
  60. Format::setDir(__DIR__ . '/formats/');
  61. Cache::setDir(__DIR__ . '/caches/');
  62. if( isset($_REQUEST) && isset($_REQUEST['action']) ){
  63. switch($_REQUEST['action']){
  64. case 'display':
  65. if( isset($_REQUEST['bridge']) ){
  66. unset($_REQUEST['action']);
  67. $bridge = $_REQUEST['bridge'];
  68. unset($_REQUEST['bridge']);
  69. $format = $_REQUEST['format'];
  70. unset($_REQUEST['format']);
  71. // whitelist control
  72. if(!BridgeWhitelist($whitelist_selection, $bridge)) {
  73. throw new \HttpException('This bridge is not whitelisted', 401);
  74. die;
  75. }
  76. $cache = Cache::create('FileCache');
  77. // Data retrieval
  78. $bridge = Bridge::create($bridge);
  79. if(isset($_REQUEST["disable_cache"])) {
  80. } else {
  81. $bridge->setCache($cache); // just add disable cache to your query to disable caching
  82. }
  83. $bridge->setDatas($_REQUEST);
  84. // Data transformation
  85. $format = Format::create($format);
  86. $format
  87. ->setDatas($bridge->getDatas())
  88. ->setExtraInfos(array(
  89. 'name' => $bridge->getName(),
  90. 'uri' => $bridge->getURI(),
  91. ))
  92. ->display();
  93. die;
  94. }
  95. break;
  96. }
  97. }
  98. }
  99. catch(HttpException $e){
  100. header('HTTP/1.1 ' . $e->getCode() . ' ' . Http::getMessageForCode($e->getCode()));
  101. header('Content-Type: text/plain');
  102. die($e->getMessage());
  103. }
  104. catch(\Exception $e){
  105. die($e->getMessage());
  106. }
  107. function getHelperButtonFormat($value, $name){
  108. return '<button type="submit" name="format" value="' . $value . '">' . $name . '</button>';
  109. }
  110. function getHelperButtonsFormat($formats){
  111. $buttons = '';
  112. foreach( $formats as $name => $infos )
  113. {
  114. if ( isset($infos['name']) )
  115. {
  116. $buttons .= getHelperButtonFormat($name, $infos['name']) . PHP_EOL;
  117. }
  118. }
  119. return $buttons;
  120. }
  121. function displayBridgeCard($bridgeReference, $bridgeInformations, $formats, $isActive = true)
  122. {
  123. $name = isset($bridgeInformations['homepage']) ? '<a href="'.$bridgeInformations['homepage'].'">'.$bridgeInformations['name'].'</a>' : $bridgeInformations['name'];
  124. $description = isset($bridgeInformations['description']) ? $bridgeInformations['description'] : 'No description provided';
  125. $card = <<<CARD
  126. <section id="bridge-{$bridgeReference}" data-ref="{$bridgeReference}">
  127. <h2>{$name}</h2>
  128. <p class="description">
  129. {$description}
  130. </p>
  131. CARD;
  132. if( isset($bridgeInformations['use']) && count($bridgeInformations['use']) > 0 )
  133. {
  134. $card .= '<ol class="list-use">' . PHP_EOL;
  135. foreach($bridgeInformations['use'] as $anUseNum => $anUse)
  136. {
  137. $card .= '<li data-use="' . $anUseNum . '">' . PHP_EOL;
  138. $card .= '<form method="GET" action="?">
  139. <input type="hidden" name="action" value="display" />
  140. <input type="hidden" name="bridge" value="' . $bridgeReference . '" />' . PHP_EOL;
  141. foreach($anUse as $argName => $argDescription)
  142. {
  143. $idArg = 'arg-' . $bridgeReference . '-' . $anUseNum . '-' . $argName;
  144. $card .= '<input id="' . $idArg . '" type="text" value="" placeholder="' . $argDescription . '" name="' . $argName . '" />' . PHP_EOL;
  145. }
  146. $card .= '<br />';
  147. if ($isActive)
  148. {
  149. $card .= getHelperButtonsFormat($formats);
  150. }
  151. else
  152. {
  153. $card .= '<span style="font-weight: bold;">Inactive</span>';
  154. }
  155. $card .= '</form></li>' . PHP_EOL;
  156. }
  157. $card .= '</ol>' . PHP_EOL;
  158. }
  159. else
  160. {
  161. $card .= '<form method="GET" action="?">
  162. <input type="hidden" name="action" value="display" />
  163. <input type="hidden" name="bridge" value="' . $bridgeReference . '" />' . PHP_EOL;
  164. if ($isActive)
  165. {
  166. $card .= getHelperButtonsFormat($formats);
  167. }
  168. else
  169. {
  170. $card .= '<span style="font-weight: bold;">Inactive</span>';
  171. }
  172. $card .= '</form>' . PHP_EOL;
  173. }
  174. $card .= isset($bridgeInformations['maintainer']) ? '<span class="maintainer">'.$bridgeInformations['maintainer'].'</span>' : '';
  175. $card .= '</section>';
  176. return $card;
  177. }
  178. $bridges = Bridge::searchInformation();
  179. $formats = Format::searchInformation();
  180. ?>
  181. <!DOCTYPE html>
  182. <html lang="en">
  183. <head>
  184. <meta charset="utf-8">
  185. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  186. <meta name="description" content="Rss-bridge" />
  187. <title>RSS-Bridge</title>
  188. <link href="css/style.css" rel="stylesheet">
  189. <!--[if IE]>
  190. <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  191. <![endif]-->
  192. </head>
  193. <body>
  194. <header>
  195. <h1>RSS-Bridge</h1>
  196. <h2>·Reconnecting the Web·</h2>
  197. </header>
  198. <?php
  199. $activeFoundBridgeCount = 0;
  200. $showInactive = isset($_REQUEST['show_inactive']) && $_REQUEST['show_inactive'] == 1;
  201. $inactiveBridges = '';
  202. foreach($bridges as $bridgeReference => $bridgeInformations)
  203. {
  204. if(BridgeWhitelist($whitelist_selection, $bridgeReference))
  205. {
  206. echo displayBridgeCard($bridgeReference, $bridgeInformations, $formats);
  207. $activeFoundBridgeCount++;
  208. }
  209. elseif ($showInactive)
  210. {
  211. // inactive bridges
  212. $inactiveBridges .= displayBridgeCard($bridgeReference, $bridgeInformations, $formats, false) . PHP_EOL;
  213. }
  214. }
  215. echo '<hr />' . $inactiveBridges;
  216. ?>
  217. <footer>
  218. <?= $activeFoundBridgeCount; ?>/<?= count($bridges) ?> active bridges (<a href="?show_inactive=1">Show inactive</a>)<br />
  219. <a href="https://github.com/sebsauvage/rss-bridge">RSS-Bridge alpha 0.1 ~ Public Domain</a>
  220. </footer>
  221. </body>
  222. </html>