pshb.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. require(__DIR__ . '/../../constants.php');
  3. require(LIB_PATH . '/lib_rss.php'); //Includes class autoloader
  4. const MAX_PAYLOAD = 3145728;
  5. header('Content-Type: text/plain; charset=UTF-8');
  6. header('X-Content-Type-Options: nosniff');
  7. $ORIGINAL_INPUT = file_get_contents('php://input', false, null, 0, MAX_PAYLOAD);
  8. Minz_Configuration::register('system', DATA_PATH . '/config.php', FRESHRSS_PATH . '/config.default.php');
  9. $system_conf = Minz_Configuration::get('system');
  10. $system_conf->auth_type = 'none'; // avoid necessity to be logged in (not saved!)
  11. //Minz_Log::debug(print_r(array('_SERVER' => $_SERVER, '_GET' => $_GET, '_POST' => $_POST, 'INPUT' => $ORIGINAL_INPUT), true), PSHB_LOG);
  12. $key = isset($_GET['k']) ? substr($_GET['k'], 0, 128) : '';
  13. if (!ctype_xdigit($key)) {
  14. header('HTTP/1.1 422 Unprocessable Entity');
  15. die('Invalid feed key format!');
  16. }
  17. chdir(PSHB_PATH);
  18. $canonical64 = @file_get_contents('keys/' . $key . '.txt');
  19. if ($canonical64 === false) {
  20. if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'unsubscribe') {
  21. Minz_Log::warning('Warning: Accept unknown unsubscribe', PSHB_LOG);
  22. header('Connection: close');
  23. exit(isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '');
  24. }
  25. header('HTTP/1.1 404 Not Found');
  26. Minz_Log::warning('Warning: Feed key not found!: ' . $key, PSHB_LOG);
  27. die('Feed key not found!');
  28. }
  29. $canonical64 = trim($canonical64);
  30. if (!preg_match('/^[A-Za-z0-9_-]+$/D', $canonical64)) {
  31. header('HTTP/1.1 500 Internal Server Error');
  32. Minz_Log::error('Error: Invalid key reference!: ' . $canonical64, PSHB_LOG);
  33. die('Invalid key reference!');
  34. }
  35. $hubFile = @file_get_contents('feeds/' . $canonical64 . '/!hub.json');
  36. if ($hubFile === false) {
  37. header('HTTP/1.1 404 Not Found');
  38. unlink('keys/' . $key . '.txt');
  39. Minz_Log::error('Error: Feed info not found!: ' . $canonical64, PSHB_LOG);
  40. die('Feed info not found!');
  41. }
  42. $hubJson = json_decode($hubFile, true);
  43. if (!$hubJson || empty($hubJson['key']) || $hubJson['key'] !== $key) {
  44. header('HTTP/1.1 500 Internal Server Error');
  45. Minz_Log::error('Error: Invalid key cross-check!: ' . $key, PSHB_LOG);
  46. die('Invalid key cross-check!');
  47. }
  48. chdir('feeds/' . $canonical64);
  49. $users = glob('*.txt', GLOB_NOSORT);
  50. if (empty($users)) {
  51. header('HTTP/1.1 410 Gone');
  52. $url = base64url_decode($canonical64);
  53. Minz_Log::warning('Warning: Nobody subscribes to this feed anymore!: ' . $url, PSHB_LOG);
  54. unlink('../../keys/' . $key . '.txt');
  55. Minz_Configuration::register('system',
  56. DATA_PATH . '/config.php',
  57. FRESHRSS_PATH . '/config.default.php');
  58. FreshRSS_Context::$system_conf = Minz_Configuration::get('system');
  59. $feed = new FreshRSS_Feed($url);
  60. $feed->pubSubHubbubSubscribe(false);
  61. unlink('!hub.json');
  62. chdir('..');
  63. recursive_unlink($canonical64);
  64. die('Nobody subscribes to this feed anymore!');
  65. }
  66. if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'subscribe') {
  67. $leaseSeconds = empty($_REQUEST['hub_lease_seconds']) ? 0 : intval($_REQUEST['hub_lease_seconds']);
  68. if ($leaseSeconds > 60) {
  69. $hubJson['lease_end'] = time() + $leaseSeconds;
  70. } else {
  71. unset($hubJson['lease_end']);
  72. }
  73. $hubJson['lease_start'] = time();
  74. if (!isset($hubJson['error'])) {
  75. $hubJson['error'] = true; //Do not assume that WebSub works until the first successul push
  76. }
  77. file_put_contents('./!hub.json', json_encode($hubJson));
  78. header('Connection: close');
  79. exit(isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '');
  80. }
  81. if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'unsubscribe') {
  82. if (empty($hubJson['lease_end']) || $hubJson['lease_end'] < time()) {
  83. header('Connection: close');
  84. exit(isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '');
  85. } else {
  86. header('HTTP/1.1 422 Unprocessable Entity');
  87. die('We did not ask to unsubscribe!');
  88. }
  89. }
  90. if ($ORIGINAL_INPUT == '') {
  91. header('HTTP/1.1 422 Unprocessable Entity');
  92. die('Missing XML payload!');
  93. }
  94. $simplePie = customSimplePie();
  95. $simplePie->set_raw_data($ORIGINAL_INPUT);
  96. $simplePie->init();
  97. unset($ORIGINAL_INPUT);
  98. $links = $simplePie->get_links('self');
  99. $self = isset($links[0]) ? $links[0] : null;
  100. if ($self !== base64url_decode($canonical64)) {
  101. //header('HTTP/1.1 422 Unprocessable Entity');
  102. Minz_Log::warning('Warning: Self URL [' . $self . '] does not match registered canonical URL!: ' . base64url_decode($canonical64), PSHB_LOG);
  103. //die('Self URL does not match registered canonical URL!');
  104. $self = base64url_decode($canonical64);
  105. }
  106. Minz_ExtensionManager::init();
  107. Minz_Translate::init();
  108. $nb = 0;
  109. foreach ($users as $userFilename) {
  110. $username = basename($userFilename, '.txt');
  111. if (!file_exists(USERS_PATH . '/' . $username . '/config.php')) {
  112. Minz_Log::warning('Warning: Removing broken user link: ' . $username . ' for ' . $self, PSHB_LOG);
  113. unlink($userFilename);
  114. continue;
  115. }
  116. try {
  117. Minz_Session::_param('currentUser', $username);
  118. Minz_Configuration::register('user',
  119. join_path(USERS_PATH, $username, 'config.php'),
  120. join_path(FRESHRSS_PATH, 'config-user.default.php'));
  121. new Minz_ModelPdo($username); //TODO: FIXME: Quick-fix while waiting for a better FreshRSS() constructor/init
  122. FreshRSS_Context::init();
  123. if (FreshRSS_Context::$user_conf != null) {
  124. Minz_ExtensionManager::enableByList(FreshRSS_Context::$user_conf->extensions_enabled);
  125. Minz_Translate::reset(FreshRSS_Context::$user_conf->language);
  126. }
  127. if (!FreshRSS_Context::$user_conf->enabled) {
  128. Minz_Log::warning('FreshRSS skip disabled user ' . $username);
  129. continue;
  130. }
  131. list($updated_feeds, $feed, $nb_new_articles) = FreshRSS_feed_Controller::actualizeFeed(0, $self, false, $simplePie);
  132. if ($updated_feeds > 0 || $feed != false) {
  133. $nb++;
  134. } else {
  135. Minz_Log::warning('Warning: User ' . $username . ' does not subscribe anymore to ' . $self, PSHB_LOG);
  136. unlink($userFilename);
  137. }
  138. } catch (Exception $e) {
  139. Minz_Log::error('Error: ' . $e->getMessage() . ' for user ' . $username . ' and feed ' . $self, PSHB_LOG);
  140. }
  141. }
  142. $simplePie->__destruct(); //http://simplepie.org/wiki/faq/i_m_getting_memory_leaks
  143. unset($simplePie);
  144. if ($nb === 0) {
  145. header('HTTP/1.1 410 Gone');
  146. Minz_Log::warning('Warning: Nobody subscribes to this feed anymore after all!: ' . $self, PSHB_LOG);
  147. die('Nobody subscribes to this feed anymore after all!');
  148. } elseif (!empty($hubJson['error'])) {
  149. $hubJson['error'] = false;
  150. file_put_contents('./!hub.json', json_encode($hubJson));
  151. }
  152. Minz_Log::notice('WebSub ' . $self . ' done: ' . $nb, PSHB_LOG);
  153. exit('Done: ' . $nb . "\n");