index.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. require_once __DIR__ . '/lib/RssBridge.php';
  3. define('PHP_VERSION_REQUIRED', '5.6.0');
  4. // Specify directory for cached files (using FileCache)
  5. define('CACHE_DIR', __DIR__ . '/cache');
  6. // Specify path for whitelist file
  7. define('WHITELIST_FILE', __DIR__ . '/whitelist.txt');
  8. Configuration::verifyInstallation();
  9. Configuration::loadConfiguration();
  10. Authentication::showPromptIfNeeded();
  11. date_default_timezone_set('UTC');
  12. error_reporting(0);
  13. /*
  14. Move the CLI arguments to the $_GET array, in order to be able to use
  15. rss-bridge from the command line
  16. */
  17. parse_str(implode('&', array_slice($argv, 1)), $cliArgs);
  18. $params = array_merge($_GET, $cliArgs);
  19. /*
  20. Create a file named 'DEBUG' for enabling debug mode.
  21. For further security, you may put whitelisted IP addresses in the file,
  22. one IP per line. Empty file allows anyone(!).
  23. Debugging allows displaying PHP error messages and bypasses the cache: this
  24. can allow a malicious client to retrieve data about your server and hammer
  25. a provider throught your rss-bridge instance.
  26. */
  27. if(file_exists('DEBUG')) {
  28. $debug_whitelist = trim(file_get_contents('DEBUG'));
  29. $debug_enabled = empty($debug_whitelist)
  30. || in_array($_SERVER['REMOTE_ADDR'], explode("\n", $debug_whitelist));
  31. if($debug_enabled) {
  32. ini_set('display_errors', '1');
  33. error_reporting(E_ALL);
  34. define('DEBUG', true);
  35. }
  36. }
  37. // FIXME : beta test UA spoofing, please report any blacklisting by PHP-fopen-unfriendly websites
  38. $userAgent = 'Mozilla/5.0(X11; Linux x86_64; rv:30.0)';
  39. $userAgent .= ' Gecko/20121202 Firefox/30.0(rss-bridge/0.1;';
  40. $userAgent .= '+https://github.com/RSS-Bridge/rss-bridge)';
  41. ini_set('user_agent', $userAgent);
  42. // default whitelist
  43. $whitelist_default = array(
  44. 'BandcampBridge',
  45. 'CryptomeBridge',
  46. 'DansTonChatBridge',
  47. 'DuckDuckGoBridge',
  48. 'FacebookBridge',
  49. 'FlickrExploreBridge',
  50. 'GooglePlusPostBridge',
  51. 'GoogleSearchBridge',
  52. 'IdenticaBridge',
  53. 'InstagramBridge',
  54. 'OpenClassroomsBridge',
  55. 'PinterestBridge',
  56. 'ScmbBridge',
  57. 'TwitterBridge',
  58. 'WikipediaBridge',
  59. 'YoutubeBridge');
  60. try {
  61. Bridge::setDir(__DIR__ . '/bridges/');
  62. Format::setDir(__DIR__ . '/formats/');
  63. Cache::setDir(__DIR__ . '/caches/');
  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. } else {
  69. $whitelist_file_content = file_get_contents(WHITELIST_FILE);
  70. if($whitelist_file_content != "*\n") {
  71. $whitelist_selection = explode("\n", $whitelist_file_content);
  72. } else {
  73. $whitelist_selection = Bridge::listBridges();
  74. }
  75. // Prepare for case-insensitive match
  76. $whitelist_selection = array_map('strtolower', $whitelist_selection);
  77. }
  78. $action = array_key_exists('action', $params) ? $params['action'] : null;
  79. $bridge = array_key_exists('bridge', $params) ? $params['bridge'] : null;
  80. if($action === 'display' && !empty($bridge)) {
  81. // DEPRECATED: 'nameBridge' scheme is replaced by 'name' in bridge parameter values
  82. // this is to keep compatibility until futher complete removal
  83. if(($pos = strpos($bridge, 'Bridge')) === (strlen($bridge) - strlen('Bridge'))) {
  84. $bridge = substr($bridge, 0, $pos);
  85. }
  86. $format = $params['format']
  87. or returnClientError('You must specify a format!');
  88. // DEPRECATED: 'nameFormat' scheme is replaced by 'name' in format parameter values
  89. // this is to keep compatibility until futher complete removal
  90. if(($pos = strpos($format, 'Format')) === (strlen($format) - strlen('Format'))) {
  91. $format = substr($format, 0, $pos);
  92. }
  93. // whitelist control
  94. if(!Bridge::isWhitelisted($whitelist_selection, strtolower($bridge))) {
  95. throw new \HttpException('This bridge is not whitelisted', 401);
  96. die;
  97. }
  98. // Data retrieval
  99. $bridge = Bridge::create($bridge);
  100. $noproxy = array_key_exists('_noproxy', $params) && filter_var($params['_noproxy'], FILTER_VALIDATE_BOOLEAN);
  101. if(defined('PROXY_URL') && PROXY_BYBRIDGE && $noproxy) {
  102. define('NOPROXY', true);
  103. }
  104. // Custom cache timeout
  105. $cache_timeout = -1;
  106. if(array_key_exists('_cache_timeout', $params)) {
  107. if(!CUSTOM_CACHE_TIMEOUT) {
  108. throw new \HttpException('This server doesn\'t support "_cache_timeout"!');
  109. }
  110. $cache_timeout = filter_var($params['_cache_timeout'], FILTER_VALIDATE_INT);
  111. }
  112. // Initialize cache
  113. $cache = Cache::create('FileCache');
  114. $cache->setPath(CACHE_DIR);
  115. $cache->purgeCache(86400); // 24 hours
  116. $cache->setParameters($params);
  117. unset($params['action']);
  118. unset($params['bridge']);
  119. unset($params['format']);
  120. unset($params['_noproxy']);
  121. unset($params['_cache_timeout']);
  122. // Load cache & data
  123. try {
  124. $bridge->setCache($cache);
  125. $bridge->setCacheTimeout($cache_timeout);
  126. $bridge->setDatas($params);
  127. } catch(Error $e) {
  128. http_response_code($e->getCode());
  129. header('Content-Type: text/html');
  130. die(buildBridgeException($e, $bridge));
  131. } catch(Exception $e) {
  132. http_response_code($e->getCode());
  133. header('Content-Type: text/html');
  134. die(buildBridgeException($e, $bridge));
  135. }
  136. // Data transformation
  137. try {
  138. $format = Format::create($format);
  139. $format->setItems($bridge->getItems());
  140. $format->setExtraInfos($bridge->getExtraInfos());
  141. $format->display();
  142. } catch(Error $e) {
  143. http_response_code($e->getCode());
  144. header('Content-Type: text/html');
  145. die(buildTransformException($e, $bridge));
  146. } catch(Exception $e) {
  147. http_response_code($e->getCode());
  148. header('Content-Type: text/html');
  149. die(buildBridgeException($e, $bridge));
  150. }
  151. die;
  152. }
  153. } catch(HttpException $e) {
  154. http_response_code($e->getCode());
  155. header('Content-Type: text/plain');
  156. die($e->getMessage());
  157. } catch(\Exception $e) {
  158. die($e->getMessage());
  159. }
  160. $formats = Format::searchInformation();
  161. ?>
  162. <!DOCTYPE html>
  163. <html lang="en">
  164. <head>
  165. <meta charset="utf-8">
  166. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  167. <meta name="description" content="Rss-bridge" />
  168. <title>RSS-Bridge</title>
  169. <link href="static/style.css" rel="stylesheet">
  170. <script src="static/search.js"></script>
  171. <script src="static/select.js"></script>
  172. <noscript>
  173. <style>
  174. .searchbar {
  175. display: none;
  176. }
  177. </style>
  178. </noscript>
  179. </head>
  180. <body onload="search()">
  181. <?php
  182. $status = '';
  183. if(defined('DEBUG') && DEBUG === true) {
  184. $status .= 'debug mode active';
  185. }
  186. $query = filter_input(INPUT_GET, 'q');
  187. echo <<<EOD
  188. <header>
  189. <h1>RSS-Bridge</h1>
  190. <h2>·Reconnecting the Web·</h2>
  191. <p class="status">{$status}</p>
  192. </header>
  193. <section class="searchbar">
  194. <h3>Search</h3>
  195. <input type="text" name="searchfield"
  196. id="searchfield" placeholder="Enter the bridge you want to search for"
  197. onchange="search()" onkeyup="search()" value="{$query}">
  198. </section>
  199. EOD;
  200. $activeFoundBridgeCount = 0;
  201. $showInactive = filter_input(INPUT_GET, 'show_inactive', FILTER_VALIDATE_BOOLEAN);
  202. $inactiveBridges = '';
  203. $bridgeList = Bridge::listBridges();
  204. foreach($bridgeList as $bridgeName) {
  205. if(Bridge::isWhitelisted($whitelist_selection, strtolower($bridgeName))) {
  206. echo displayBridgeCard($bridgeName, $formats);
  207. $activeFoundBridgeCount++;
  208. } elseif($showInactive) {
  209. // inactive bridges
  210. $inactiveBridges .= displayBridgeCard($bridgeName, $formats, false) . PHP_EOL;
  211. }
  212. }
  213. echo $inactiveBridges;
  214. ?>
  215. <section class="footer">
  216. <a href="https://github.com/RSS-Bridge/rss-bridge">RSS-Bridge 2018-06-10 ~ Public Domain</a><br />
  217. <?= $activeFoundBridgeCount; ?>/<?= count($bridgeList) ?> active bridges. <br />
  218. <?php
  219. if($activeFoundBridgeCount !== count($bridgeList)) {
  220. // FIXME: This should be done in pure CSS
  221. if(!$showInactive)
  222. echo '<a href="?show_inactive=1"><button class="small">Show inactive bridges</button></a><br />';
  223. else
  224. echo '<a href="?show_inactive=0"><button class="small">Hide inactive bridges</button></a><br />';
  225. }
  226. ?>
  227. </section>
  228. </body>
  229. </html>