index.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. // configuration checks
  64. if(ini_get('allow_url_fopen') !== "1")
  65. die('"allow_url_fopen" is not set to "1". Please check "php.ini');
  66. // Check cache folder permissions (write permissions required)
  67. if(!is_writable(CACHE_DIR))
  68. die('RSS-Bridge does not have write permissions for ' . CACHE_DIR . '!');
  69. // Check whitelist file permissions (only in DEBUG mode)
  70. if(!file_exists(WHITELIST_FILE) && !is_writable(dirname(WHITELIST_FILE)))
  71. die('RSS-Bridge does not have write permissions for ' . WHITELIST_FILE . '!');
  72. // FIXME : beta test UA spoofing, please report any blacklisting by PHP-fopen-unfriendly websites
  73. $userAgent = 'Mozilla/5.0(X11; Linux x86_64; rv:30.0)';
  74. $userAgent .= ' Gecko/20121202 Firefox/30.0(rss-bridge/0.1;';
  75. $userAgent .= '+https://github.com/RSS-Bridge/rss-bridge)';
  76. ini_set('user_agent', $userAgent);
  77. // default whitelist
  78. $whitelist_default = array(
  79. 'BandcampBridge',
  80. 'CryptomeBridge',
  81. 'DansTonChatBridge',
  82. 'DuckDuckGoBridge',
  83. 'FacebookBridge',
  84. 'FlickrExploreBridge',
  85. 'GooglePlusPostBridge',
  86. 'GoogleSearchBridge',
  87. 'IdenticaBridge',
  88. 'InstagramBridge',
  89. 'OpenClassroomsBridge',
  90. 'PinterestBridge',
  91. 'ScmbBridge',
  92. 'TwitterBridge',
  93. 'WikipediaBridge',
  94. 'YoutubeBridge');
  95. try {
  96. Bridge::setDir(__DIR__ . '/bridges/');
  97. Format::setDir(__DIR__ . '/formats/');
  98. Cache::setDir(__DIR__ . '/caches/');
  99. if(!file_exists(WHITELIST_FILE)) {
  100. $whitelist_selection = $whitelist_default;
  101. $whitelist_write = implode("\n", $whitelist_default);
  102. file_put_contents(WHITELIST_FILE, $whitelist_write);
  103. } else {
  104. $whitelist_file_content = file_get_contents(WHITELIST_FILE);
  105. if($whitelist_file_content != "*\n") {
  106. $whitelist_selection = explode("\n", $whitelist_file_content);
  107. } else {
  108. $whitelist_selection = Bridge::listBridges();
  109. }
  110. // Prepare for case-insensitive match
  111. $whitelist_selection = array_map('strtolower', $whitelist_selection);
  112. }
  113. $action = array_key_exists('action', $params) ? $params['action'] : null;
  114. $bridge = array_key_exists('bridge', $params) ? $params['bridge'] : null;
  115. if($action === 'display' && !empty($bridge)) {
  116. // DEPRECATED: 'nameBridge' scheme is replaced by 'name' in bridge parameter values
  117. // this is to keep compatibility until futher complete removal
  118. if(($pos = strpos($bridge, 'Bridge')) === (strlen($bridge) - strlen('Bridge'))) {
  119. $bridge = substr($bridge, 0, $pos);
  120. }
  121. $format = $params['format']
  122. or returnClientError('You must specify a format!');
  123. // DEPRECATED: 'nameFormat' scheme is replaced by 'name' in format parameter values
  124. // this is to keep compatibility until futher complete removal
  125. if(($pos = strpos($format, 'Format')) === (strlen($format) - strlen('Format'))) {
  126. $format = substr($format, 0, $pos);
  127. }
  128. // whitelist control
  129. if(!Bridge::isWhitelisted($whitelist_selection, strtolower($bridge))) {
  130. throw new \HttpException('This bridge is not whitelisted', 401);
  131. die;
  132. }
  133. // Data retrieval
  134. $bridge = Bridge::create($bridge);
  135. $noproxy = array_key_exists('_noproxy', $params) && filter_var($params['_noproxy'], FILTER_VALIDATE_BOOLEAN);
  136. if(defined('PROXY_URL') && PROXY_BYBRIDGE && $noproxy) {
  137. define('NOPROXY', true);
  138. }
  139. // Custom cache timeout
  140. $cache_timeout = -1;
  141. if(array_key_exists('_cache_timeout', $params)) {
  142. if(!CUSTOM_CACHE_TIMEOUT) {
  143. throw new \HttpException('This server doesn\'t support "_cache_timeout"!');
  144. }
  145. $cache_timeout = filter_var($params['_cache_timeout'], FILTER_VALIDATE_INT);
  146. }
  147. // Initialize cache
  148. $cache = Cache::create('FileCache');
  149. $cache->setPath(CACHE_DIR);
  150. $cache->purgeCache(86400); // 24 hours
  151. $cache->setParameters($params);
  152. unset($params['action']);
  153. unset($params['bridge']);
  154. unset($params['format']);
  155. unset($params['_noproxy']);
  156. unset($params['_cache_timeout']);
  157. // Load cache & data
  158. try {
  159. $bridge->setCache($cache);
  160. $bridge->setCacheTimeout($cache_timeout);
  161. $bridge->setDatas($params);
  162. } catch(Exception $e) {
  163. http_response_code($e->getCode());
  164. header('Content-Type: text/html');
  165. die(buildBridgeException($e, $bridge));
  166. }
  167. // Data transformation
  168. try {
  169. $format = Format::create($format);
  170. $format->setItems($bridge->getItems());
  171. $format->setExtraInfos($bridge->getExtraInfos());
  172. $format->display();
  173. } catch(Exception $e) {
  174. http_response_code($e->getCode());
  175. header('Content-Type: text/html');
  176. die(buildTransformException($e, $bridge));
  177. }
  178. die;
  179. }
  180. } catch(HttpException $e) {
  181. http_response_code($e->getCode());
  182. header('Content-Type: text/plain');
  183. die($e->getMessage());
  184. } catch(\Exception $e) {
  185. die($e->getMessage());
  186. }
  187. $formats = Format::searchInformation();
  188. ?>
  189. <!DOCTYPE html>
  190. <html lang="en">
  191. <head>
  192. <meta charset="utf-8">
  193. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  194. <meta name="description" content="Rss-bridge" />
  195. <title>RSS-Bridge</title>
  196. <link href="static/style.css" rel="stylesheet">
  197. <script src="static/search.js"></script>
  198. <script src="static/select.js"></script>
  199. <noscript>
  200. <style>
  201. .searchbar {
  202. display: none;
  203. }
  204. </style>
  205. </noscript>
  206. </head>
  207. <body onload="search()">
  208. <?php
  209. $status = '';
  210. if(defined('DEBUG') && DEBUG === true) {
  211. $status .= 'debug mode active';
  212. }
  213. $query = filter_input(INPUT_GET, 'q');
  214. echo <<<EOD
  215. <header>
  216. <h1>RSS-Bridge</h1>
  217. <h2>·Reconnecting the Web·</h2>
  218. <p class="status">{$status}</p>
  219. </header>
  220. <section class="searchbar">
  221. <h3>Search</h3>
  222. <input type="text" name="searchfield"
  223. id="searchfield" placeholder="Enter the bridge you want to search for"
  224. onchange="search()" onkeyup="search()" value="{$query}">
  225. </section>
  226. EOD;
  227. $activeFoundBridgeCount = 0;
  228. $showInactive = filter_input(INPUT_GET, 'show_inactive', FILTER_VALIDATE_BOOLEAN);
  229. $inactiveBridges = '';
  230. $bridgeList = Bridge::listBridges();
  231. foreach($bridgeList as $bridgeName) {
  232. if(Bridge::isWhitelisted($whitelist_selection, strtolower($bridgeName))) {
  233. echo displayBridgeCard($bridgeName, $formats);
  234. $activeFoundBridgeCount++;
  235. } elseif($showInactive) {
  236. // inactive bridges
  237. $inactiveBridges .= displayBridgeCard($bridgeName, $formats, false) . PHP_EOL;
  238. }
  239. }
  240. echo $inactiveBridges;
  241. ?>
  242. <section class="footer">
  243. <a href="https://github.com/RSS-Bridge/rss-bridge">RSS-Bridge 2017-08-19 ~ Public Domain</a><br />
  244. <?= $activeFoundBridgeCount; ?>/<?= count($bridgeList) ?> active bridges. <br />
  245. <?php
  246. if($activeFoundBridgeCount !== count($bridgeList)) {
  247. // FIXME: This should be done in pure CSS
  248. if(!$showInactive)
  249. echo '<a href="?show_inactive=1"><button class="small">Show inactive bridges</button></a><br />';
  250. else
  251. echo '<a href="?show_inactive=0"><button class="small">Hide inactive bridges</button></a><br />';
  252. }
  253. ?>
  254. </section>
  255. </body>
  256. </html>