pshb.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. declare(strict_types=1);
  3. require(__DIR__ . '/../../constants.php');
  4. require(LIB_PATH . '/lib_rss.php'); //Includes class autoloader
  5. const MAX_PAYLOAD = 3_145_728;
  6. header('Content-Type: text/plain; charset=UTF-8');
  7. header('X-Content-Type-Options: nosniff');
  8. $ORIGINAL_INPUT = file_get_contents('php://input', false, null, 0, MAX_PAYLOAD) ?: '';
  9. FreshRSS_Context::initSystem();
  10. if (!FreshRSS_Context::hasSystemConf()) {
  11. header('HTTP/1.1 500 Internal Server Error');
  12. die('Invalid system init!');
  13. }
  14. FreshRSS_Context::systemConf()->auth_type = 'none'; // avoid necessity to be logged in (not saved!)
  15. // Minz_Log::debug(print_r(['_SERVER' => $_SERVER, '_GET' => $_GET, '_POST' => $_POST, 'INPUT' => $ORIGINAL_INPUT], true), PSHB_LOG);
  16. $key = isset($_GET['k']) && is_string($_GET['k']) ? substr($_GET['k'], 0, 128) : '';
  17. if (!ctype_xdigit($key)) {
  18. header('HTTP/1.1 422 Unprocessable Entity');
  19. die('Invalid feed key format!');
  20. }
  21. chdir(PSHB_PATH);
  22. $canonical = @file_get_contents('keys/' . $key . '.txt');
  23. if ($canonical === false) {
  24. if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'unsubscribe') {
  25. Minz_Log::warning('Warning: Accept unknown unsubscribe', PSHB_LOG);
  26. header('Connection: close');
  27. exit($_REQUEST['hub_challenge'] ?? '');
  28. }
  29. // https://github.com/w3c/websub/issues/106 , https://w3c.github.io/websub/#content-distribution
  30. header('HTTP/1.1 410 Gone');
  31. Minz_Log::warning('Warning: Feed key not found!: ' . $key, PSHB_LOG);
  32. die('Feed key not found!');
  33. }
  34. $canonical = trim($canonical);
  35. $canonicalHash = sha1($canonical);
  36. $hubFile = @file_get_contents('feeds/' . $canonicalHash . '/!hub.json');
  37. if ($hubFile === false) {
  38. header('HTTP/1.1 410 Gone');
  39. unlink('keys/' . $key . '.txt');
  40. Minz_Log::error('Error: Feed info not found!: ' . $canonical, PSHB_LOG);
  41. die('Feed info not found!');
  42. }
  43. $hubJson = json_decode($hubFile, true);
  44. if (!is_array($hubJson) || empty($hubJson['key']) || $hubJson['key'] !== $key) {
  45. header('HTTP/1.1 500 Internal Server Error');
  46. Minz_Log::error('Error: Invalid key cross-check!: ' . $key, PSHB_LOG);
  47. die('Invalid key cross-check!');
  48. }
  49. chdir('feeds/' . $canonicalHash);
  50. $users = glob('*.txt', GLOB_NOSORT);
  51. if (empty($users)) {
  52. header('HTTP/1.1 410 Gone');
  53. Minz_Log::warning('Warning: Nobody subscribes to this feed anymore!: ' . $canonical, PSHB_LOG);
  54. unlink('../../keys/' . $key . '.txt');
  55. $feed = new FreshRSS_Feed($canonical);
  56. $feed->pubSubHubbubSubscribe(false);
  57. unlink('!hub.json');
  58. chdir('..');
  59. recursive_unlink('feeds/' . $canonicalHash);
  60. die('Nobody subscribes to this feed anymore!');
  61. }
  62. if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'subscribe') {
  63. $leaseSeconds = empty($_REQUEST['hub_lease_seconds']) || !is_numeric($_REQUEST['hub_lease_seconds']) ? 0 : (int)$_REQUEST['hub_lease_seconds'];
  64. if ($leaseSeconds > 60) {
  65. $hubJson['lease_end'] = time() + $leaseSeconds;
  66. } else {
  67. unset($hubJson['lease_end']);
  68. }
  69. $hubJson['lease_start'] = time();
  70. if (!isset($hubJson['error'])) {
  71. $hubJson['error'] = true; //Do not assume that WebSub works until the first successful push
  72. }
  73. file_put_contents('./!hub.json', json_encode($hubJson));
  74. header('Connection: close');
  75. exit($_REQUEST['hub_challenge'] ?? '');
  76. }
  77. if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'unsubscribe') {
  78. if (empty($hubJson['lease_end']) || $hubJson['lease_end'] < time()) {
  79. header('Connection: close');
  80. exit($_REQUEST['hub_challenge'] ?? '');
  81. } else {
  82. header('HTTP/1.1 422 Unprocessable Entity');
  83. die('We did not ask to unsubscribe!');
  84. }
  85. }
  86. if ($ORIGINAL_INPUT == '') {
  87. header('HTTP/1.1 422 Unprocessable Entity');
  88. die('Missing XML payload!');
  89. }
  90. $simplePie = customSimplePie();
  91. $simplePie->enable_cache(false);
  92. $simplePie->set_raw_data($ORIGINAL_INPUT);
  93. $simplePie->init();
  94. unset($ORIGINAL_INPUT);
  95. $links = $simplePie->get_links('self');
  96. $self = $links[0] ?? '';
  97. // Support HTTP header `Link: <http://example.net/hub.php>; rel="hub", <http://example.net/feed.php>; rel="self"`
  98. $httpLink = is_string($_SERVER['HTTP_LINK'] ?? null) ? $_SERVER['HTTP_LINK'] : '';
  99. if ($httpLink !== '' && preg_match_all('/<([^>]+)>;\\s*rel="([^"]+)"/', $httpLink, $matches, PREG_SET_ORDER)) {
  100. $links = [];
  101. foreach ($matches as $match) {
  102. if (!empty($match[1]) && !empty($match[2])) {
  103. $links[$match[2]] = $match[1];
  104. }
  105. }
  106. // if (!empty($links['hub'])) {
  107. // // TODO: Support WebSub hub redirection
  108. // }
  109. if (!empty($links['self'])) {
  110. $httpSelf = checkUrl($links['self']) ?: '';
  111. if ($self !== '' && $self !== $httpSelf) {
  112. Minz_Log::warning('Warning: Self URL mismatch between XML [' . $self . '] and HTTP!: ' . $httpSelf, PSHB_LOG);
  113. }
  114. $self = $httpSelf;
  115. }
  116. }
  117. if ($self !== $canonical) {
  118. //header('HTTP/1.1 422 Unprocessable Entity');
  119. Minz_Log::warning('Warning: Self URL [' . $self . '] does not match registered canonical URL!: ' . $canonical, PSHB_LOG);
  120. //die('Self URL does not match registered canonical URL!');
  121. }
  122. Minz_ExtensionManager::init();
  123. Minz_Translate::init();
  124. $nb = 0;
  125. foreach ($users as $userFilename) {
  126. $username = basename($userFilename, '.txt');
  127. if (!file_exists(USERS_PATH . '/' . $username . '/config.php')) {
  128. Minz_Log::warning('Warning: Removing broken user link: ' . $username . ' for ' . $canonical, PSHB_LOG);
  129. unlink($userFilename);
  130. continue;
  131. }
  132. try {
  133. FreshRSS_Context::initUser($username);
  134. if (!FreshRSS_Context::hasUserConf() || !FreshRSS_Context::userConf()->enabled) {
  135. Minz_Log::warning('FreshRSS skip disabled user ' . $username);
  136. continue;
  137. }
  138. Minz_ExtensionManager::enableByList(FreshRSS_Context::userConf()->extensions_enabled, 'user');
  139. Minz_Translate::reset(FreshRSS_Context::userConf()->language);
  140. [$nbUpdatedFeeds, ] = FreshRSS_feed_Controller::actualizeFeedsAndCommit(feed_url: $canonical, simplePiePush: $simplePie, selfUrl: $self);
  141. if ($nbUpdatedFeeds > 0) {
  142. $nb++;
  143. } else {
  144. Minz_Log::warning('Warning: User ' . $username . ' does not subscribe anymore to ' . $canonical, PSHB_LOG);
  145. unlink($userFilename);
  146. }
  147. } catch (Exception $e) {
  148. Minz_Log::error('Error: ' . $e->getMessage() . ' for user ' . $username . ' and feed ' . $canonical, PSHB_LOG);
  149. }
  150. }
  151. $simplePie->__destruct(); //http://simplepie.org/wiki/faq/i_m_getting_memory_leaks
  152. unset($simplePie);
  153. if ($nb === 0) {
  154. header('HTTP/1.1 410 Gone');
  155. Minz_Log::warning('Warning: Nobody subscribes to this feed anymore after all!: ' . $canonical, PSHB_LOG);
  156. die('Nobody subscribes to this feed anymore after all!');
  157. } elseif (!empty($hubJson['error'])) {
  158. $hubJson['error'] = false;
  159. file_put_contents('./!hub.json', json_encode($hubJson));
  160. }
  161. Minz_Log::notice('WebSub ' . $canonical . ' done: ' . $nb, PSHB_LOG);
  162. exit('Done: ' . $nb . "\n");