index.php 7.9 KB

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