index.php 8.6 KB

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