index.php 11 KB

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