index.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. require_once __DIR__ . '/lib/RssBridge.php';
  3. define('PHP_VERSION_REQUIRED', '5.6.0');
  4. // Specify directory for cached files (using FileCache)
  5. define('CACHE_DIR', __DIR__ . '/cache');
  6. // Specify path for whitelist file
  7. define('WHITELIST_FILE', __DIR__ . '/whitelist.txt');
  8. Configuration::verifyInstallation();
  9. Configuration::loadConfiguration();
  10. Authentication::showPromptIfNeeded();
  11. date_default_timezone_set('UTC');
  12. error_reporting(0);
  13. /*
  14. Move the CLI arguments to the $_GET array, in order to be able to use
  15. rss-bridge from the command line
  16. */
  17. parse_str(implode('&', array_slice($argv, 1)), $cliArgs);
  18. $params = array_merge($_GET, $cliArgs);
  19. /*
  20. Create a file named 'DEBUG' for enabling debug mode.
  21. For further security, you may put whitelisted IP addresses in the file,
  22. one IP per line. Empty file allows anyone(!).
  23. Debugging allows displaying PHP error messages and bypasses the cache: this
  24. can allow a malicious client to retrieve data about your server and hammer
  25. a provider throught your rss-bridge instance.
  26. */
  27. if(file_exists('DEBUG')) {
  28. $debug_whitelist = trim(file_get_contents('DEBUG'));
  29. $debug_enabled = empty($debug_whitelist)
  30. || in_array($_SERVER['REMOTE_ADDR'], explode("\n", $debug_whitelist));
  31. if($debug_enabled) {
  32. ini_set('display_errors', '1');
  33. error_reporting(E_ALL);
  34. define('DEBUG', true);
  35. }
  36. }
  37. // FIXME : beta test UA spoofing, please report any blacklisting by PHP-fopen-unfriendly websites
  38. $userAgent = 'Mozilla/5.0(X11; Linux x86_64; rv:30.0)';
  39. $userAgent .= ' Gecko/20121202 Firefox/30.0(rss-bridge/0.1;';
  40. $userAgent .= '+https://github.com/RSS-Bridge/rss-bridge)';
  41. ini_set('user_agent', $userAgent);
  42. // default whitelist
  43. $whitelist_default = array(
  44. 'BandcampBridge',
  45. 'CryptomeBridge',
  46. 'DansTonChatBridge',
  47. 'DuckDuckGoBridge',
  48. 'FacebookBridge',
  49. 'FlickrExploreBridge',
  50. 'GooglePlusPostBridge',
  51. 'GoogleSearchBridge',
  52. 'IdenticaBridge',
  53. 'InstagramBridge',
  54. 'OpenClassroomsBridge',
  55. 'PinterestBridge',
  56. 'ScmbBridge',
  57. 'TwitterBridge',
  58. 'WikipediaBridge',
  59. 'YoutubeBridge');
  60. try {
  61. Bridge::setDir(__DIR__ . '/bridges/');
  62. Format::setDir(__DIR__ . '/formats/');
  63. Cache::setDir(__DIR__ . '/caches/');
  64. if(!file_exists(WHITELIST_FILE)) {
  65. $whitelist_selection = $whitelist_default;
  66. $whitelist_write = implode("\n", $whitelist_default);
  67. file_put_contents(WHITELIST_FILE, $whitelist_write);
  68. } else {
  69. $whitelist_file_content = file_get_contents(WHITELIST_FILE);
  70. if($whitelist_file_content != "*\n") {
  71. $whitelist_selection = explode("\n", $whitelist_file_content);
  72. } else {
  73. $whitelist_selection = Bridge::listBridges();
  74. }
  75. // Prepare for case-insensitive match
  76. $whitelist_selection = array_map('strtolower', $whitelist_selection);
  77. }
  78. $showInactive = filter_input(INPUT_GET, 'show_inactive', FILTER_VALIDATE_BOOLEAN);
  79. $action = array_key_exists('action', $params) ? $params['action'] : null;
  80. $bridge = array_key_exists('bridge', $params) ? $params['bridge'] : null;
  81. if($action === 'display' && !empty($bridge)) {
  82. // DEPRECATED: 'nameBridge' scheme is replaced by 'name' in bridge parameter values
  83. // this is to keep compatibility until futher complete removal
  84. if(($pos = strpos($bridge, 'Bridge')) === (strlen($bridge) - strlen('Bridge'))) {
  85. $bridge = substr($bridge, 0, $pos);
  86. }
  87. $format = $params['format']
  88. or returnClientError('You must specify a format!');
  89. // DEPRECATED: 'nameFormat' scheme is replaced by 'name' in format parameter values
  90. // this is to keep compatibility until futher complete removal
  91. if(($pos = strpos($format, 'Format')) === (strlen($format) - strlen('Format'))) {
  92. $format = substr($format, 0, $pos);
  93. }
  94. // whitelist control
  95. if(!Bridge::isWhitelisted($whitelist_selection, strtolower($bridge))) {
  96. throw new \HttpException('This bridge is not whitelisted', 401);
  97. die;
  98. }
  99. // Data retrieval
  100. $bridge = Bridge::create($bridge);
  101. $noproxy = array_key_exists('_noproxy', $params) && filter_var($params['_noproxy'], FILTER_VALIDATE_BOOLEAN);
  102. if(defined('PROXY_URL') && PROXY_BYBRIDGE && $noproxy) {
  103. define('NOPROXY', true);
  104. }
  105. // Custom cache timeout
  106. $cache_timeout = -1;
  107. if(array_key_exists('_cache_timeout', $params)) {
  108. if(!CUSTOM_CACHE_TIMEOUT) {
  109. throw new \HttpException('This server doesn\'t support "_cache_timeout"!');
  110. }
  111. $cache_timeout = filter_var($params['_cache_timeout'], FILTER_VALIDATE_INT);
  112. }
  113. // Initialize cache
  114. $cache = Cache::create('FileCache');
  115. $cache->setPath(CACHE_DIR);
  116. $cache->purgeCache(86400); // 24 hours
  117. $cache->setParameters($params);
  118. unset($params['action']);
  119. unset($params['bridge']);
  120. unset($params['format']);
  121. unset($params['_noproxy']);
  122. unset($params['_cache_timeout']);
  123. // Load cache & data
  124. try {
  125. $bridge->setCache($cache);
  126. $bridge->setCacheTimeout($cache_timeout);
  127. $bridge->setDatas($params);
  128. } catch(Error $e) {
  129. http_response_code($e->getCode());
  130. header('Content-Type: text/html');
  131. die(buildBridgeException($e, $bridge));
  132. } catch(Exception $e) {
  133. http_response_code($e->getCode());
  134. header('Content-Type: text/html');
  135. die(buildBridgeException($e, $bridge));
  136. }
  137. // Data transformation
  138. try {
  139. $format = Format::create($format);
  140. $format->setItems($bridge->getItems());
  141. $format->setExtraInfos($bridge->getExtraInfos());
  142. $format->display();
  143. } catch(Error $e) {
  144. http_response_code($e->getCode());
  145. header('Content-Type: text/html');
  146. die(buildTransformException($e, $bridge));
  147. } catch(Exception $e) {
  148. http_response_code($e->getCode());
  149. header('Content-Type: text/html');
  150. die(buildBridgeException($e, $bridge));
  151. }
  152. } else {
  153. echo BridgeList::create($whitelist_selection, $showInactive);
  154. }
  155. } catch(HttpException $e) {
  156. http_response_code($e->getCode());
  157. header('Content-Type: text/plain');
  158. die($e->getMessage());
  159. } catch(\Exception $e) {
  160. die($e->getMessage());
  161. }