index.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. // Return list of bridges as JSON formatted text
  82. if($action === 'list') {
  83. $list = new StdClass();
  84. $list->bridges = array();
  85. $list->total = 0;
  86. foreach(Bridge::listBridges() as $bridgeName) {
  87. $bridge = Bridge::create($bridgeName);
  88. if($bridge === false) { // Broken bridge, show as inactive
  89. $list->bridges[$bridgeName] = array(
  90. 'status' => 'inactive'
  91. );
  92. continue;
  93. }
  94. $status = Bridge::isWhitelisted($whitelist_selection, strtolower($bridgeName)) ? 'active' : 'inactive';
  95. $list->bridges[$bridgeName] = array(
  96. 'status' => $status,
  97. 'uri' => $bridge->getURI(),
  98. 'name' => $bridge->getName(),
  99. 'parameters' => $bridge->getParameters(),
  100. 'maintainer' => $bridge->getMaintainer(),
  101. 'description' => $bridge->getDescription()
  102. );
  103. }
  104. $list->total = count($list->bridges);
  105. header('Content-Type: application/json');
  106. echo json_encode($list, JSON_PRETTY_PRINT);
  107. } elseif($action === 'display' && !empty($bridge)) {
  108. // DEPRECATED: 'nameBridge' scheme is replaced by 'name' in bridge parameter values
  109. // this is to keep compatibility until futher complete removal
  110. if(($pos = strpos($bridge, 'Bridge')) === (strlen($bridge) - strlen('Bridge'))) {
  111. $bridge = substr($bridge, 0, $pos);
  112. }
  113. $format = $params['format']
  114. or returnClientError('You must specify a format!');
  115. // DEPRECATED: 'nameFormat' scheme is replaced by 'name' in format parameter values
  116. // this is to keep compatibility until futher complete removal
  117. if(($pos = strpos($format, 'Format')) === (strlen($format) - strlen('Format'))) {
  118. $format = substr($format, 0, $pos);
  119. }
  120. // whitelist control
  121. if(!Bridge::isWhitelisted($whitelist_selection, strtolower($bridge))) {
  122. throw new \HttpException('This bridge is not whitelisted', 401);
  123. die;
  124. }
  125. // Data retrieval
  126. $bridge = Bridge::create($bridge);
  127. $noproxy = array_key_exists('_noproxy', $params) && filter_var($params['_noproxy'], FILTER_VALIDATE_BOOLEAN);
  128. if(defined('PROXY_URL') && PROXY_BYBRIDGE && $noproxy) {
  129. define('NOPROXY', true);
  130. }
  131. // Custom cache timeout
  132. $cache_timeout = -1;
  133. if(array_key_exists('_cache_timeout', $params)) {
  134. if(!CUSTOM_CACHE_TIMEOUT) {
  135. throw new \HttpException('This server doesn\'t support "_cache_timeout"!');
  136. }
  137. $cache_timeout = filter_var($params['_cache_timeout'], FILTER_VALIDATE_INT);
  138. }
  139. // Initialize cache
  140. $cache = Cache::create('FileCache');
  141. $cache->setPath(CACHE_DIR);
  142. $cache->purgeCache(86400); // 24 hours
  143. $cache->setParameters($params);
  144. unset($params['action']);
  145. unset($params['bridge']);
  146. unset($params['format']);
  147. unset($params['_noproxy']);
  148. unset($params['_cache_timeout']);
  149. // Load cache & data
  150. try {
  151. $bridge->setCache($cache);
  152. $bridge->setCacheTimeout($cache_timeout);
  153. $bridge->setDatas($params);
  154. } catch(Error $e) {
  155. http_response_code($e->getCode());
  156. header('Content-Type: text/html');
  157. die(buildBridgeException($e, $bridge));
  158. } catch(Exception $e) {
  159. http_response_code($e->getCode());
  160. header('Content-Type: text/html');
  161. die(buildBridgeException($e, $bridge));
  162. }
  163. // Data transformation
  164. try {
  165. $format = Format::create($format);
  166. $format->setItems($bridge->getItems());
  167. $format->setExtraInfos($bridge->getExtraInfos());
  168. $format->display();
  169. } catch(Error $e) {
  170. http_response_code($e->getCode());
  171. header('Content-Type: text/html');
  172. die(buildTransformException($e, $bridge));
  173. } catch(Exception $e) {
  174. http_response_code($e->getCode());
  175. header('Content-Type: text/html');
  176. die(buildBridgeException($e, $bridge));
  177. }
  178. } else {
  179. echo BridgeList::create($whitelist_selection, $showInactive);
  180. }
  181. } catch(HttpException $e) {
  182. http_response_code($e->getCode());
  183. header('Content-Type: text/plain');
  184. die($e->getMessage());
  185. } catch(\Exception $e) {
  186. die($e->getMessage());
  187. }