index.php 8.3 KB

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