index.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. /*
  16. Create a file named 'DEBUG' for enabling debug mode.
  17. For further security, you may put whitelisted IP addresses
  18. in the 'DEBUG' file, one IP per line. Empty file allows anyone (!).
  19. Debugging allows displaying PHP error messages and bypasses the cache: this can allow a malicious
  20. client to retrieve data about your server and hammer a provider throught your rss-bridge instance.
  21. */
  22. if (file_exists('DEBUG')) {
  23. $debug_enabled = true;
  24. $debug_whitelist = trim(file_get_contents('DEBUG'));
  25. if (strlen($debug_whitelist) > 0) {
  26. $debug_enabled = false;
  27. foreach (explode("\n", $debug_whitelist) as $allowed_ip) {
  28. if (trim($allowed_ip) === $_SERVER['REMOTE_ADDR']) {
  29. $debug_enabled = true;
  30. break;
  31. }
  32. }
  33. }
  34. if ($debug_enabled) {
  35. ini_set('display_errors', '1');
  36. error_reporting(E_ALL);
  37. define('DEBUG', 'true');
  38. }
  39. }
  40. require_once __DIR__ . '/lib/RssBridge.php';
  41. // extensions check
  42. if (!extension_loaded('openssl'))
  43. die('"openssl" extension not loaded. Please check "php.ini"');
  44. // FIXME : beta test UA spoofing, please report any blacklisting by PHP-fopen-unfriendly websites
  45. 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)');
  46. // default whitelist
  47. $whitelist_file = './whitelist.txt';
  48. $whitelist_default = array(
  49. "BandcampBridge",
  50. "CryptomeBridge",
  51. "DansTonChatBridge",
  52. "DuckDuckGoBridge",
  53. "FacebookBridge",
  54. "FlickrExploreBridge",
  55. "GooglePlusPostBridge",
  56. "GoogleSearchBridge",
  57. "IdenticaBridge",
  58. "InstagramBridge",
  59. "OpenClassroomsBridge",
  60. "PinterestBridge",
  61. "ScmbBridge",
  62. "TwitterBridge",
  63. "WikipediaENBridge",
  64. "WikipediaEOBridge",
  65. "WikipediaFRBridge",
  66. "YoutubeBridge");
  67. if (!file_exists($whitelist_file)) {
  68. $whitelist_selection = $whitelist_default;
  69. $whitelist_write = implode("\n", $whitelist_default);
  70. file_put_contents($whitelist_file, $whitelist_write);
  71. }
  72. else {
  73. $whitelist_selection = explode("\n", file_get_contents($whitelist_file));
  74. }
  75. Cache::purge();
  76. try{
  77. Bridge::setDir(__DIR__ . '/bridges/');
  78. Format::setDir(__DIR__ . '/formats/');
  79. Cache::setDir(__DIR__ . '/caches/');
  80. if( isset($_REQUEST) && isset($_REQUEST['action']) ){
  81. switch($_REQUEST['action']){
  82. case 'display':
  83. if( isset($_REQUEST['bridge']) ){
  84. unset($_REQUEST['action']);
  85. $bridge = $_REQUEST['bridge'];
  86. unset($_REQUEST['bridge']);
  87. $format = $_REQUEST['format'];
  88. unset($_REQUEST['format']);
  89. // whitelist control
  90. if(!Bridge::isWhitelisted($whitelist_selection, $bridge)) {
  91. throw new \HttpException('This bridge is not whitelisted', 401);
  92. die;
  93. }
  94. $cache = Cache::create('FileCache');
  95. // Data retrieval
  96. $bridge = Bridge::create($bridge);
  97. if(defined("DEBUG")) {
  98. } else {
  99. $bridge->setCache($cache); // just add disable cache to your query to disable caching
  100. }
  101. $bridge->setDatas($_REQUEST);
  102. $bridge->loadMetadatas();
  103. // Data transformation
  104. try {
  105. $format = Format::create($format);
  106. $format
  107. ->setDatas($bridge->getDatas())
  108. ->setExtraInfos(array(
  109. 'name' => $bridge->getName(),
  110. 'uri' => $bridge->getURI(),
  111. ))
  112. ->display();
  113. } catch(Exception $e) {
  114. echo "The brige has crashed. You should report this to the bridges maintainer";
  115. }
  116. die;
  117. }
  118. break;
  119. }
  120. }
  121. }
  122. catch(HttpException $e){
  123. header('HTTP/1.1 ' . $e->getCode() . ' ' . Http::getMessageForCode($e->getCode()));
  124. header('Content-Type: text/plain');
  125. die($e->getMessage());
  126. }
  127. catch(\Exception $e){
  128. die($e->getMessage());
  129. }
  130. $formats = Format::searchInformation();
  131. ?>
  132. <!DOCTYPE html>
  133. <html lang="en">
  134. <head>
  135. <meta charset="utf-8">
  136. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  137. <meta name="description" content="Rss-bridge" />
  138. <title>RSS-Bridge</title>
  139. <link href="css/style.css" rel="stylesheet">
  140. </head>
  141. <body>
  142. <header>
  143. <h1>RSS-Bridge</h1>
  144. <h2>·Reconnecting the Web·</h2>
  145. </header>
  146. <?php
  147. $activeFoundBridgeCount = 0;
  148. $showInactive = isset($_REQUEST['show_inactive']) && $_REQUEST['show_inactive'] == 1;
  149. $inactiveBridges = '';
  150. $bridgeList = Bridge::listBridges();
  151. foreach($bridgeList as $bridgeName)
  152. {
  153. if(Bridge::isWhitelisted($whitelist_selection, $bridgeName))
  154. {
  155. echo HTMLUtils::displayBridgeCard($bridgeName, $formats);
  156. $activeFoundBridgeCount++;
  157. }
  158. elseif ($showInactive)
  159. {
  160. // inactive bridges
  161. $inactiveBridges .= HTMLUtils::displayBridgeCard($bridgeName, $formats, false) . PHP_EOL;
  162. }
  163. }
  164. echo $inactiveBridges;
  165. ?>
  166. <section>
  167. <a href="https://github.com/sebsauvage/rss-bridge">RSS-Bridge alpha 0.2 ~ Public Domain</a><br />
  168. <?= $activeFoundBridgeCount; ?>/<?= count($bridgeList) ?> active bridges. <br />
  169. <a href="?show_inactive=1"><button class="small">Show inactive bridges</button></a><br />
  170. </section>
  171. </body>
  172. </html>