index.php 11 KB

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