index.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. if(file_exists("DEBUG")) {
  16. ini_set('display_errors','1'); error_reporting(E_ALL); //Report all errors
  17. define("DEBUG", "true");
  18. }
  19. require_once __DIR__ . '/lib/RssBridge.php';
  20. // extensions check
  21. if (!extension_loaded('openssl'))
  22. die('"openssl" extension not loaded. Please check "php.ini"');
  23. // FIXME : beta test UA spoofing, please report any blacklisting by PHP-fopen-unfriendly websites
  24. 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)');
  25. // default whitelist
  26. $whitelist_file = './whitelist.txt';
  27. $whitelist_default = array(
  28. "BandcampBridge",
  29. "CryptomeBridge",
  30. "DansTonChatBridge",
  31. "DuckDuckGoBridge",
  32. "FacebookBridge",
  33. "FlickrExploreBridge",
  34. "GooglePlusPostBridge",
  35. "GoogleSearchBridge",
  36. "IdenticaBridge",
  37. "InstagramBridge",
  38. "OpenClassroomsBridge",
  39. "PinterestBridge",
  40. "ScmbBridge",
  41. "TwitterBridge",
  42. "WikipediaENBridge",
  43. "WikipediaEOBridge",
  44. "WikipediaFRBridge",
  45. "YoutubeBridge");
  46. if (!file_exists($whitelist_file)) {
  47. $whitelist_selection = $whitelist_default;
  48. $whitelist_write = implode("\n", $whitelist_default);
  49. file_put_contents($whitelist_file, $whitelist_write);
  50. }
  51. else {
  52. $whitelist_selection = explode("\n", file_get_contents($whitelist_file));
  53. }
  54. Cache::purge();
  55. try{
  56. Bridge::setDir(__DIR__ . '/bridges/');
  57. Format::setDir(__DIR__ . '/formats/');
  58. Cache::setDir(__DIR__ . '/caches/');
  59. if( isset($_REQUEST) && isset($_REQUEST['action']) ){
  60. switch($_REQUEST['action']){
  61. case 'display':
  62. if( isset($_REQUEST['bridge']) ){
  63. unset($_REQUEST['action']);
  64. $bridge = $_REQUEST['bridge'];
  65. unset($_REQUEST['bridge']);
  66. $format = $_REQUEST['format'];
  67. unset($_REQUEST['format']);
  68. // whitelist control
  69. if(!Bridge::isWhitelisted($whitelist_selection, $bridge)) {
  70. throw new \HttpException('This bridge is not whitelisted', 401);
  71. die;
  72. }
  73. $cache = Cache::create('FileCache');
  74. // Data retrieval
  75. $bridge = Bridge::create($bridge);
  76. if(defined("DEBUG")) {
  77. } else {
  78. $bridge->setCache($cache); // just add disable cache to your query to disable caching
  79. }
  80. $bridge->setDatas($_REQUEST);
  81. $bridge->loadMetadatas();
  82. // Data transformation
  83. try {
  84. $format = Format::create($format);
  85. $format
  86. ->setDatas($bridge->getDatas())
  87. ->setExtraInfos(array(
  88. 'name' => $bridge->getName(),
  89. 'uri' => $bridge->getURI(),
  90. ))
  91. ->display();
  92. } catch(Exception $e) {
  93. echo "The brige has crashed. You should report this to the bridges maintainer";
  94. }
  95. die;
  96. }
  97. break;
  98. }
  99. }
  100. }
  101. catch(HttpException $e){
  102. header('HTTP/1.1 ' . $e->getCode() . ' ' . Http::getMessageForCode($e->getCode()));
  103. header('Content-Type: text/plain');
  104. die($e->getMessage());
  105. }
  106. catch(\Exception $e){
  107. die($e->getMessage());
  108. }
  109. $formats = Format::searchInformation();
  110. ?>
  111. <!DOCTYPE html>
  112. <html lang="en">
  113. <head>
  114. <meta charset="utf-8">
  115. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  116. <meta name="description" content="Rss-bridge" />
  117. <title>RSS-Bridge</title>
  118. <link href="css/style.css" rel="stylesheet">
  119. </head>
  120. <body>
  121. <header>
  122. <h1>RSS-Bridge</h1>
  123. <h2>·Reconnecting the Web·</h2>
  124. </header>
  125. <?php
  126. $activeFoundBridgeCount = 0;
  127. $showInactive = isset($_REQUEST['show_inactive']) && $_REQUEST['show_inactive'] == 1;
  128. $inactiveBridges = '';
  129. $bridgeList = Bridge::listBridges();
  130. foreach($bridgeList as $bridgeName)
  131. {
  132. if(Bridge::isWhitelisted($whitelist_selection, $bridgeName))
  133. {
  134. echo HTMLUtils::displayBridgeCard($bridgeName, $formats);
  135. $activeFoundBridgeCount++;
  136. }
  137. elseif ($showInactive)
  138. {
  139. // inactive bridges
  140. $inactiveBridges .= HTMLUtils::displayBridgeCard($bridgeName, $formats, false) . PHP_EOL;
  141. }
  142. }
  143. echo '<hr />' . $inactiveBridges;
  144. ?>
  145. <footer>
  146. <?= $activeFoundBridgeCount; ?>/<?= count($bridgeList) ?> active bridges (<a href="?show_inactive=1">Show inactive</a>)<br />
  147. <a href="https://github.com/sebsauvage/rss-bridge">RSS-Bridge alpha 0.2 ~ Public Domain</a>
  148. </footer>
  149. </body>
  150. </html>