index.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // default whitelist
  23. $whitelist_file = './whitelist.txt';
  24. $whitelist_default = array(
  25. "BandcampBridge",
  26. "CryptomeBridge",
  27. "DansTonChatBridge",
  28. "DuckDuckGoBridge",
  29. "FacebookBridge",
  30. "FlickrExploreBridge",
  31. "GooglePlusPostBridge",
  32. "GoogleSearchBridge",
  33. "IdenticaBridge",
  34. "InstagramBridge",
  35. "OpenClassroomsBridge",
  36. "PinterestBridge",
  37. "ScmbBridge",
  38. "TwitterBridge",
  39. "WikipediaENBridge",
  40. "WikipediaEOBridge",
  41. "WikipediaFRBridge",
  42. "YoutubeBridge");
  43. if (!file_exists($whitelist_file)) {
  44. $whitelist_selection = $whitelist_default;
  45. $whitelist_write = implode("\n", $whitelist_default);
  46. file_put_contents($whitelist_file, $whitelist_write);
  47. }
  48. else {
  49. $whitelist_selection = explode("\n", file_get_contents($whitelist_file));
  50. //Remove the last empty line.
  51. array_pop($whitelist_selection);
  52. }
  53. Cache::purge();
  54. try{
  55. Bridge::setDir(__DIR__ . '/bridges/');
  56. Format::setDir(__DIR__ . '/formats/');
  57. Cache::setDir(__DIR__ . '/caches/');
  58. if( isset($_REQUEST) && isset($_REQUEST['action']) ){
  59. switch($_REQUEST['action']){
  60. case 'display':
  61. if( isset($_REQUEST['bridge']) ){
  62. unset($_REQUEST['action']);
  63. $bridge = $_REQUEST['bridge'];
  64. unset($_REQUEST['bridge']);
  65. $format = $_REQUEST['format'];
  66. unset($_REQUEST['format']);
  67. // whitelist control
  68. if(!Bridge::isWhitelisted($whitelist_selection, $bridge)) {
  69. throw new \HttpException('This bridge is not whitelisted', 401);
  70. die;
  71. }
  72. $cache = Cache::create('FileCache');
  73. // Data retrieval
  74. $bridge = Bridge::create($bridge);
  75. if(isset($_REQUEST["disable_cache"])) {
  76. } else {
  77. $bridge->setCache($cache); // just add disable cache to your query to disable caching
  78. }
  79. $bridge->setDatas($_REQUEST);
  80. $bridge->loadMetadatas();
  81. // Data transformation
  82. try {
  83. $format = Format::create($format);
  84. $format
  85. ->setDatas($bridge->getDatas())
  86. ->setExtraInfos(array(
  87. 'name' => $bridge->getName(),
  88. 'uri' => $bridge->getURI(),
  89. ))
  90. ->display();
  91. } catch(Exception $e) {
  92. echo "The brige has crashed. You should report this to the bridges maintainer";
  93. }
  94. die;
  95. }
  96. break;
  97. }
  98. }
  99. }
  100. catch(HttpException $e){
  101. header('HTTP/1.1 ' . $e->getCode() . ' ' . Http::getMessageForCode($e->getCode()));
  102. header('Content-Type: text/plain');
  103. die($e->getMessage());
  104. }
  105. catch(\Exception $e){
  106. die($e->getMessage());
  107. }
  108. $formats = Format::searchInformation();
  109. ?>
  110. <!DOCTYPE html>
  111. <html lang="en">
  112. <head>
  113. <meta charset="utf-8">
  114. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  115. <meta name="description" content="Rss-bridge" />
  116. <title>RSS-Bridge</title>
  117. <link href="css/style.css" rel="stylesheet">
  118. </head>
  119. <body>
  120. <header>
  121. <h1>RSS-Bridge</h1>
  122. <h2>·Reconnecting the Web·</h2>
  123. </header>
  124. <?php
  125. $activeFoundBridgeCount = 0;
  126. $showInactive = isset($_REQUEST['show_inactive']) && $_REQUEST['show_inactive'] == 1;
  127. $inactiveBridges = '';
  128. $bridgeList = Bridge::listBridges();
  129. foreach($bridgeList as $bridgeName)
  130. {
  131. if(Bridge::isWhitelisted($whitelist_selection, $bridgeName))
  132. {
  133. echo HTMLUtils::displayBridgeCard($bridgeName, $formats);
  134. $activeFoundBridgeCount++;
  135. }
  136. elseif ($showInactive)
  137. {
  138. // inactive bridges
  139. $inactiveBridges .= HTMLUtils::displayBridgeCard($bridgeName, $formats, false) . PHP_EOL;
  140. }
  141. }
  142. echo '<hr />' . $inactiveBridges;
  143. ?>
  144. <footer>
  145. <?= $activeFoundBridgeCount; ?>/<?= count($bridgeList) ?> active bridges (<a href="?show_inactive=1">Show inactive</a>)<br />
  146. <a href="https://github.com/sebsauvage/rss-bridge">RSS-Bridge alpha 0.2 ~ Public Domain</a>
  147. </footer>
  148. </body>
  149. </html>