1
0

index.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. //define('PROXY_URL', 'tcp://192.168.0.0:28');
  13. date_default_timezone_set('UTC');
  14. error_reporting(0);
  15. /*
  16. Create a file named 'DEBUG' for enabling debug mode.
  17. For further security, you may put whitelisted IP addresses
  18. in the 'DEBUG' file, one IP per line. Empty file allows anyone (!).
  19. Debugging allows displaying PHP error messages and bypasses the cache: this can allow a malicious
  20. client to retrieve data about your server and hammer a provider throught your rss-bridge instance.
  21. */
  22. if (file_exists('DEBUG')) {
  23. $debug_enabled = true;
  24. $debug_whitelist = trim(file_get_contents('DEBUG'));
  25. if (strlen($debug_whitelist) > 0) {
  26. $debug_enabled = false;
  27. foreach (explode("\n", $debug_whitelist) as $allowed_ip) {
  28. if (trim($allowed_ip) === $_SERVER['REMOTE_ADDR']) {
  29. $debug_enabled = true;
  30. break;
  31. }
  32. }
  33. }
  34. if ($debug_enabled) {
  35. ini_set('display_errors', '1');
  36. error_reporting(E_ALL);
  37. define('DEBUG', 'true');
  38. }
  39. }
  40. require_once __DIR__ . '/lib/RssBridge.php';
  41. // extensions check
  42. if (!extension_loaded('openssl'))
  43. die('"openssl" extension not loaded. Please check "php.ini"');
  44. // FIXME : beta test UA spoofing, please report any blacklisting by PHP-fopen-unfriendly websites
  45. ini_set('user_agent', 'Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20121202 Firefox/30.0 (rss-bridge/0.1; +https://github.com/sebsauvage/rss-bridge)');
  46. // default whitelist
  47. $whitelist_file = './whitelist.txt';
  48. $whitelist_default = array(
  49. "BandcampBridge",
  50. "CryptomeBridge",
  51. "DansTonChatBridge",
  52. "DuckDuckGoBridge",
  53. "FacebookBridge",
  54. "FlickrExploreBridge",
  55. "GooglePlusPostBridge",
  56. "GoogleSearchBridge",
  57. "IdenticaBridge",
  58. "InstagramBridge",
  59. "OpenClassroomsBridge",
  60. "PinterestBridge",
  61. "ScmbBridge",
  62. "TwitterBridge",
  63. "WikipediaBridge",
  64. "YoutubeBridge");
  65. if (!file_exists($whitelist_file)) {
  66. $whitelist_selection = $whitelist_default;
  67. $whitelist_write = implode("\n", $whitelist_default);
  68. file_put_contents($whitelist_file, $whitelist_write);
  69. }
  70. else {
  71. $whitelist_selection = explode("\n", file_get_contents($whitelist_file));
  72. }
  73. Cache::purge();
  74. try{
  75. Bridge::setDir(__DIR__ . '/bridges/');
  76. Format::setDir(__DIR__ . '/formats/');
  77. Cache::setDir(__DIR__ . '/caches/');
  78. if( isset($_REQUEST) && isset($_REQUEST['action']) ){
  79. switch($_REQUEST['action']){
  80. case 'display':
  81. if( isset($_REQUEST['bridge']) ){
  82. unset($_REQUEST['action']);
  83. $bridge = $_REQUEST['bridge'];
  84. unset($_REQUEST['bridge']);
  85. $format = $_REQUEST['format'];
  86. unset($_REQUEST['format']);
  87. // whitelist control
  88. if(!Bridge::isWhitelisted($whitelist_selection, $bridge)) {
  89. throw new \HttpException('This bridge is not whitelisted', 401);
  90. die;
  91. }
  92. $cache = Cache::create('FileCache');
  93. // Data retrieval
  94. $bridge = Bridge::create($bridge);
  95. if(defined("DEBUG")) {
  96. } else {
  97. $bridge->setCache($cache); // just add disable cache to your query to disable caching
  98. }
  99. $bridge->loadMetadatas();
  100. $bridge->setDatas($_REQUEST);
  101. // Data transformation
  102. try {
  103. $format = Format::create($format);
  104. $format
  105. ->setDatas($bridge->getDatas())
  106. ->setExtraInfos(array(
  107. 'name' => $bridge->getName(),
  108. 'uri' => $bridge->getURI(),
  109. ))
  110. ->display();
  111. } catch(Exception $e) {
  112. echo "The brige has crashed. You should report this to the bridges maintainer";
  113. }
  114. die;
  115. }
  116. break;
  117. }
  118. }
  119. }
  120. catch(HttpException $e){
  121. header('HTTP/1.1 ' . $e->getCode() . ' ' . Http::getMessageForCode($e->getCode()));
  122. header('Content-Type: text/plain');
  123. die($e->getMessage());
  124. }
  125. catch(\Exception $e){
  126. die($e->getMessage());
  127. }
  128. $formats = Format::searchInformation();
  129. ?>
  130. <!DOCTYPE html>
  131. <html lang="en">
  132. <head>
  133. <meta charset="utf-8">
  134. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  135. <meta name="description" content="Rss-bridge" />
  136. <title>RSS-Bridge</title>
  137. <link href="css/style.css" rel="stylesheet">
  138. </head>
  139. <body>
  140. <header>
  141. <h1>RSS-Bridge</h1>
  142. <h2>·Reconnecting the Web·</h2>
  143. </header>
  144. <?php
  145. $activeFoundBridgeCount = 0;
  146. $showInactive = isset($_REQUEST['show_inactive']) && $_REQUEST['show_inactive'] == 1;
  147. $inactiveBridges = '';
  148. $bridgeList = Bridge::listBridges();
  149. foreach($bridgeList as $bridgeName)
  150. {
  151. if(Bridge::isWhitelisted($whitelist_selection, $bridgeName))
  152. {
  153. echo HTMLUtils::displayBridgeCard($bridgeName, $formats);
  154. $activeFoundBridgeCount++;
  155. }
  156. elseif ($showInactive)
  157. {
  158. // inactive bridges
  159. $inactiveBridges .= HTMLUtils::displayBridgeCard($bridgeName, $formats, false) . PHP_EOL;
  160. }
  161. }
  162. echo $inactiveBridges;
  163. ?>
  164. <section>
  165. <a href="https://github.com/sebsauvage/rss-bridge">RSS-Bridge alpha 0.2 ~ Public Domain</a><br />
  166. <?= $activeFoundBridgeCount; ?>/<?= count($bridgeList) ?> active bridges. <br />
  167. <?php
  168. if($activeFoundBridgeCount !== count($bridgeList)){
  169. // FIXME: This should be done in pure CSS
  170. if(!$showInactive)
  171. echo '<a href="?show_inactive=1"><button class="small">Show inactive bridges</button></a><br />';
  172. else
  173. echo '<a href="?show_inactive=0"><button class="small">Hide inactive bridges</button></a><br />';
  174. }
  175. ?>
  176. </section>
  177. </body>
  178. </html>