index.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. ini_set('user_agent', 'Mozilla/5.0(X11; Linux x86_64; rv:30.0)
  62. Gecko/20121202 Firefox/30.0(rss-bridge/0.1;
  63. +https://github.com/RSS-Bridge/rss-bridge)');
  64. // default whitelist
  65. $whitelist_file = './whitelist.txt';
  66. $whitelist_default = array(
  67. "BandcampBridge",
  68. "CryptomeBridge",
  69. "DansTonChatBridge",
  70. "DuckDuckGoBridge",
  71. "FacebookBridge",
  72. "FlickrExploreBridge",
  73. "GooglePlusPostBridge",
  74. "GoogleSearchBridge",
  75. "IdenticaBridge",
  76. "InstagramBridge",
  77. "OpenClassroomsBridge",
  78. "PinterestBridge",
  79. "ScmbBridge",
  80. "TwitterBridge",
  81. "WikipediaBridge",
  82. "YoutubeBridge");
  83. if(!file_exists($whitelist_file)){
  84. $whitelist_selection = $whitelist_default;
  85. $whitelist_write = implode("\n", $whitelist_default);
  86. file_put_contents($whitelist_file, $whitelist_write);
  87. } else {
  88. $whitelist_selection = explode("\n", file_get_contents($whitelist_file));
  89. }
  90. try {
  91. Bridge::setDir(__DIR__ . '/bridges/');
  92. Format::setDir(__DIR__ . '/formats/');
  93. Cache::setDir(__DIR__ . '/caches/');
  94. $action = filter_input(INPUT_GET, 'action');
  95. $bridge = filter_input(INPUT_GET, 'bridge');
  96. if($action === 'display' && !empty($bridge)){
  97. // DEPRECATED: 'nameBridge' scheme is replaced by 'name' in bridge parameter values
  98. // this is to keep compatibility until futher complete removal
  99. if(($pos = strpos($bridge, 'Bridge')) === (strlen($bridge) - strlen('Bridge'))){
  100. $bridge = substr($bridge, 0, $pos);
  101. }
  102. $format = filter_input(INPUT_GET, 'format');
  103. // DEPRECATED: 'nameFormat' scheme is replaced by 'name' in format parameter values
  104. // this is to keep compatibility until futher complete removal
  105. if(($pos = strpos($format, 'Format')) === (strlen($format) - strlen('Format'))){
  106. $format = substr($format, 0, $pos);
  107. }
  108. // whitelist control
  109. if(!Bridge::isWhitelisted($whitelist_selection, $bridge)){
  110. throw new \HttpException('This bridge is not whitelisted', 401);
  111. die;
  112. }
  113. // Data retrieval
  114. $bridge = Bridge::create($bridge);
  115. $noproxy = filter_input(INPUT_GET, '_noproxy', FILTER_VALIDATE_BOOLEAN);
  116. if(defined('PROXY_URL') && PROXY_BYBRIDGE && $noproxy){
  117. define('NOPROXY', true);
  118. }
  119. $params = $_GET;
  120. // Initialize cache
  121. $cache = Cache::create('FileCache');
  122. $cache->setPath(CACHE_DIR);
  123. $cache->purgeCache(86400); // 24 hours
  124. $cache->setParameters($params);
  125. unset($params['action']);
  126. unset($params['bridge']);
  127. unset($params['format']);
  128. unset($params['_noproxy']);
  129. // Load cache & data
  130. $bridge->setCache($cache);
  131. $bridge->setDatas($params);
  132. // Data transformation
  133. try {
  134. $format = Format::create($format);
  135. $format->setItems($bridge->getItems());
  136. $format->setExtraInfos($bridge->getExtraInfos());
  137. $format->display();
  138. } catch(Exception $e){
  139. echo "The bridge has crashed. You should report this to the bridges maintainer";
  140. }
  141. die;
  142. }
  143. }
  144. catch(HttpException $e){
  145. header('HTTP/1.1 ' . $e->getCode() . ' ' . Http::getMessageForCode($e->getCode()));
  146. header('Content-Type: text/plain');
  147. die($e->getMessage());
  148. }
  149. catch(\Exception $e){
  150. die($e->getMessage());
  151. }
  152. $formats = Format::searchInformation();
  153. ?>
  154. <!DOCTYPE html>
  155. <html lang="en">
  156. <head>
  157. <meta charset="utf-8">
  158. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  159. <meta name="description" content="Rss-bridge" />
  160. <title>RSS-Bridge</title>
  161. <link href="css/style.css" rel="stylesheet">
  162. </head>
  163. <body>
  164. <?php
  165. $status = '';
  166. if(defined('DEBUG') && DEBUG === true){
  167. $status .= 'debug mode active';
  168. }
  169. echo <<<EOD
  170. <header>
  171. <h1>RSS-Bridge</h1>
  172. <h2>·Reconnecting the Web·</h2>
  173. <p class="status">{$status}</p>
  174. </header>
  175. EOD;
  176. $activeFoundBridgeCount = 0;
  177. $showInactive = filter_input(INPUT_GET, 'show_inactive', FILTER_VALIDATE_BOOLEAN);
  178. $inactiveBridges = '';
  179. $bridgeList = Bridge::listBridges();
  180. foreach($bridgeList as $bridgeName){
  181. if(Bridge::isWhitelisted($whitelist_selection, $bridgeName)){
  182. echo displayBridgeCard($bridgeName, $formats);
  183. $activeFoundBridgeCount++;
  184. } elseif($showInactive) {
  185. // inactive bridges
  186. $inactiveBridges .= displayBridgeCard($bridgeName, $formats, false) . PHP_EOL;
  187. }
  188. }
  189. echo $inactiveBridges;
  190. ?>
  191. <section>
  192. <a href="https://github.com/RSS-Bridge/rss-bridge">RSS-Bridge alpha 0.2 ~ Public Domain</a><br />
  193. <?= $activeFoundBridgeCount; ?>/<?= count($bridgeList) ?> active bridges. <br />
  194. <?php
  195. if($activeFoundBridgeCount !== count($bridgeList)){
  196. // FIXME: This should be done in pure CSS
  197. if(!$showInactive)
  198. echo '<a href="?show_inactive=1"><button class="small">Show inactive bridges</button></a><br />';
  199. else
  200. echo '<a href="?show_inactive=0"><button class="small">Hide inactive bridges</button></a><br />';
  201. }
  202. ?>
  203. </section>
  204. </body>
  205. </html>