index.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /*
  3. TODO :
  4. - factorize the annotation system
  5. - factorize to adapter : Format, Bridge, Cache (actually code is almost the same)
  6. - implement annotation cache for entrance page
  7. - Cache : I think logic must be change as least to avoid to reconvert object from json in FileCache case.
  8. - add namespace to avoid futur problem ?
  9. - see FIXME mentions in the code
  10. - implement header('X-Cached-Version: '.date(DATE_ATOM, filemtime($cachefile)));
  11. */
  12. //define('PROXY_URL', 'tcp://192.168.0.0:28');
  13. date_default_timezone_set('UTC');
  14. error_reporting(0);
  15. //ini_set('display_errors','1'); error_reporting(E_ALL); // For debugging only.
  16. require_once __DIR__ . '/lib/RssBridge.php';
  17. // extensions check
  18. if (!extension_loaded('openssl'))
  19. die('"openssl" extension not loaded. Please check "php.ini"');
  20. // FIXME : beta test UA spoofing, please report any blacklisting by PHP-fopen-unfriendly websites
  21. 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)');
  22. <<<<<<< HEAD
  23. // -------
  24. =======
  25. >>>>>>> a40bbbd2deb5f59d88d61fe2253a16c28deb900b
  26. // cache file purge - delete cache files older than 24 hours
  27. $cacheTimeLimit = time() - 60*60*24 ;
  28. $cachePath = 'cache';
  29. if(file_exists($cachePath)) {
  30. $cacheIterator = new RecursiveIteratorIterator(
  31. new RecursiveDirectoryIterator($cachePath),
  32. RecursiveIteratorIterator::CHILD_FIRST
  33. );
  34. foreach ($cacheIterator as $cacheFile) {
  35. if (in_array($cacheFile->getBasename(), array('.', '..')))
  36. continue;
  37. elseif ($cacheFile->isFile()) {
  38. if( filemtime($cacheFile->getPathname()) < $cacheTimeLimit )
  39. unlink( $cacheFile->getPathname() );
  40. }
  41. }
  42. }
  43. // default whitelist
  44. $whitelist_file = './whitelist.txt';
  45. $whitelist_default = array(
  46. "BandcampBridge",
  47. "CryptomeBridge",
  48. "DansTonChatBridge",
  49. "DuckDuckGoBridge",
  50. "FacebookBridge",
  51. "FlickrExploreBridge",
  52. "GooglePlusPostBridge",
  53. "GoogleSearchBridge",
  54. "IdenticaBridge",
  55. "InstagramBridge",
  56. "OpenClassroomsBridge",
  57. "PinterestBridge",
  58. "ScmbBridge",
  59. "TwitterBridge",
  60. "WikipediaENBridge",
  61. "WikipediaEOBridge",
  62. "WikipediaFRBridge",
  63. "YoutubeBridge");
  64. if (!file_exists($whitelist_file)) {
  65. $whitelist_selection = $whitelist_default;
  66. $whitelist_write = implode("\n", $whitelist_default);
  67. file_put_contents($whitelist_file, $whitelist_write);
  68. }
  69. else {
  70. $whitelist_selection = explode("\n", file_get_contents($whitelist_file));
  71. //Remove the last empty line.
  72. array_pop($whitelist_selection);
  73. }
  74. <<<<<<< HEAD
  75. =======
  76. Cache::purge();
  77. >>>>>>> a40bbbd2deb5f59d88d61fe2253a16c28deb900b
  78. try{
  79. Bridge::setDir(__DIR__ . '/bridges/');
  80. Format::setDir(__DIR__ . '/formats/');
  81. Cache::setDir(__DIR__ . '/caches/');
  82. if( isset($_REQUEST) && isset($_REQUEST['action']) ){
  83. switch($_REQUEST['action']){
  84. case 'display':
  85. if( isset($_REQUEST['bridge']) ){
  86. unset($_REQUEST['action']);
  87. $bridge = $_REQUEST['bridge'];
  88. unset($_REQUEST['bridge']);
  89. $format = $_REQUEST['format'];
  90. unset($_REQUEST['format']);
  91. // whitelist control
  92. if(!Bridge::isWhitelisted($whitelist_selection, $bridge)) {
  93. throw new \HttpException('This bridge is not whitelisted', 401);
  94. die;
  95. }
  96. $cache = Cache::create('FileCache');
  97. // Data retrieval
  98. $bridge = Bridge::create($bridge);
  99. if(isset($_REQUEST["disable_cache"])) {
  100. } else {
  101. $bridge->setCache($cache); // just add disable cache to your query to disable caching
  102. }
  103. $bridge->setDatas($_REQUEST);
  104. $bridge->loadMetadatas();
  105. // Data transformation
  106. try {
  107. $format = Format::create($format);
  108. $format
  109. ->setDatas($bridge->getDatas())
  110. ->setExtraInfos(array(
  111. 'name' => $bridge->getName(),
  112. 'uri' => $bridge->getURI(),
  113. ))
  114. ->display();
  115. } catch(Exception $e) {
  116. echo "The brige has crashed. You should report this to the bridges maintainer";
  117. }
  118. die;
  119. }
  120. break;
  121. }
  122. }
  123. }
  124. catch(HttpException $e){
  125. header('HTTP/1.1 ' . $e->getCode() . ' ' . Http::getMessageForCode($e->getCode()));
  126. header('Content-Type: text/plain');
  127. die($e->getMessage());
  128. }
  129. catch(\Exception $e){
  130. die($e->getMessage());
  131. }
  132. $formats = Format::searchInformation();
  133. ?>
  134. <!DOCTYPE html>
  135. <html lang="en">
  136. <head>
  137. <meta charset="utf-8">
  138. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  139. <meta name="description" content="Rss-bridge" />
  140. <title>RSS-Bridge</title>
  141. <link href="css/style.css" rel="stylesheet">
  142. </head>
  143. <body>
  144. <header>
  145. <h1>RSS-Bridge</h1>
  146. <h2>·Reconnecting the Web·</h2>
  147. </header>
  148. <?php
  149. $activeFoundBridgeCount = 0;
  150. $showInactive = isset($_REQUEST['show_inactive']) && $_REQUEST['show_inactive'] == 1;
  151. $inactiveBridges = '';
  152. $bridgeList = Bridge::listBridges();
  153. foreach($bridgeList as $bridgeName)
  154. {
  155. if(Bridge::isWhitelisted($whitelist_selection, $bridgeName))
  156. {
  157. echo HTMLUtils::displayBridgeCard($bridgeName, $formats);
  158. $activeFoundBridgeCount++;
  159. }
  160. elseif ($showInactive)
  161. {
  162. // inactive bridges
  163. $inactiveBridges .= HTMLUtils::displayBridgeCard($bridgeName, $formats, false) . PHP_EOL;
  164. }
  165. }
  166. echo '<hr />' . $inactiveBridges;
  167. ?>
  168. <footer>
  169. <?= $activeFoundBridgeCount; ?>/<?= count($bridgeList) ?> active bridges (<a href="?show_inactive=1">Show inactive</a>)<br />
  170. <a href="https://github.com/sebsauvage/rss-bridge">RSS-Bridge alpha 0.2 ~ Public Domain</a>
  171. </footer>
  172. </body>
  173. </html>