1
0

index.php 8.0 KB

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