index.php 6.3 KB

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