index.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/RSS-Bridge/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. try {
  79. Bridge::setDir(__DIR__ . '/bridges/');
  80. Format::setDir(__DIR__ . '/formats/');
  81. Cache::setDir(__DIR__ . '/caches/');
  82. $action = filter_input(INPUT_GET, 'action');
  83. $bridge = filter_input(INPUT_GET, 'bridge');
  84. if($action === 'display' && !empty($bridge)){
  85. // DEPRECATED: 'nameBridge' scheme is replaced by 'name' in bridge parameter values
  86. // this is to keep compatibility until futher complete removal
  87. if(($pos = strpos($bridge, 'Bridge')) === (strlen($bridge) - strlen('Bridge'))){
  88. $bridge = substr($bridge, 0, $pos);
  89. }
  90. $format = filter_input(INPUT_GET, 'format');
  91. // DEPRECATED: 'nameFormat' scheme is replaced by 'name' in format parameter values
  92. // this is to keep compatibility until futher complete removal
  93. if(($pos = strpos($format, 'Format')) === (strlen($format) - strlen('Format'))){
  94. $format = substr($format, 0, $pos);
  95. }
  96. // whitelist control
  97. if(!Bridge::isWhitelisted($whitelist_selection, $bridge)){
  98. throw new \HttpException('This bridge is not whitelisted', 401);
  99. die;
  100. }
  101. // Data retrieval
  102. $bridge = Bridge::create($bridge);
  103. $noproxy = filter_input(INPUT_GET, '_noproxy', FILTER_VALIDATE_BOOLEAN);
  104. if(defined('PROXY_URL') && PROXY_BYBRIDGE && $noproxy){
  105. define('NOPROXY',true);
  106. }
  107. $params = $_GET;
  108. unset($params['action']);
  109. unset($params['bridge']);
  110. unset($params['format']);
  111. unset($params['_noproxy']);
  112. // Initialize cache
  113. $cache = Cache::create('FileCache');
  114. $cache->setPath(__DIR__ . '/cache');
  115. $cache->purgeCache(86400); // 24 hours
  116. $cache->setParameters($params);
  117. // Load cache & data
  118. $bridge->setCache($cache);
  119. $bridge->setDatas($params);
  120. // Data transformation
  121. try {
  122. $format = Format::create($format);
  123. $format
  124. ->setItems($bridge->getItems())
  125. ->setExtraInfos(array(
  126. 'name' => $bridge->getName(),
  127. 'uri' => $bridge->getURI(),
  128. ))
  129. ->display();
  130. } catch(Exception $e){
  131. echo "The bridge has crashed. You should report this to the bridges maintainer";
  132. }
  133. die;
  134. }
  135. }
  136. catch(HttpException $e){
  137. header('HTTP/1.1 ' . $e->getCode() . ' ' . Http::getMessageForCode($e->getCode()));
  138. header('Content-Type: text/plain');
  139. die($e->getMessage());
  140. }
  141. catch(\Exception $e){
  142. die($e->getMessage());
  143. }
  144. $formats = Format::searchInformation();
  145. ?>
  146. <!DOCTYPE html>
  147. <html lang="en">
  148. <head>
  149. <meta charset="utf-8">
  150. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  151. <meta name="description" content="Rss-bridge" />
  152. <title>RSS-Bridge</title>
  153. <link href="css/style.css" rel="stylesheet">
  154. </head>
  155. <body>
  156. <?php
  157. $status = '';
  158. if(defined('DEBUG') && DEBUG === true){
  159. $status .= 'debug mode active';
  160. }
  161. echo <<<EOD
  162. <header>
  163. <h1>RSS-Bridge</h1>
  164. <h2>·Reconnecting the Web·</h2>
  165. <p class="status">{$status}</p>
  166. </header>
  167. EOD;
  168. $activeFoundBridgeCount = 0;
  169. $showInactive = filter_input(INPUT_GET, 'show_inactive', FILTER_VALIDATE_BOOLEAN);
  170. $inactiveBridges = '';
  171. $bridgeList = Bridge::listBridges();
  172. foreach($bridgeList as $bridgeName){
  173. if(Bridge::isWhitelisted($whitelist_selection, $bridgeName)){
  174. echo displayBridgeCard($bridgeName, $formats);
  175. $activeFoundBridgeCount++;
  176. } elseif($showInactive) {
  177. // inactive bridges
  178. $inactiveBridges .= displayBridgeCard($bridgeName, $formats, false) . PHP_EOL;
  179. }
  180. }
  181. echo $inactiveBridges;
  182. ?>
  183. <section>
  184. <a href="https://github.com/RSS-Bridge/rss-bridge">RSS-Bridge alpha 0.2 ~ Public Domain</a><br />
  185. <?= $activeFoundBridgeCount; ?>/<?= count($bridgeList) ?> active bridges. <br />
  186. <?php
  187. if($activeFoundBridgeCount !== count($bridgeList)){
  188. // FIXME: This should be done in pure CSS
  189. if(!$showInactive)
  190. echo '<a href="?show_inactive=1"><button class="small">Show inactive bridges</button></a><br />';
  191. else
  192. echo '<a href="?show_inactive=0"><button class="small">Hide inactive bridges</button></a><br />';
  193. }
  194. ?>
  195. </section>
  196. </body>
  197. </html>