index.php 9.7 KB

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