pshb.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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] ?? null;
  97. if ($self !== $canonical) {
  98. //header('HTTP/1.1 422 Unprocessable Entity');
  99. Minz_Log::warning('Warning: Self URL [' . $self . '] does not match registered canonical URL!: ' . $canonical, PSHB_LOG);
  100. //die('Self URL does not match registered canonical URL!');
  101. $self = $canonical;
  102. }
  103. Minz_ExtensionManager::init();
  104. Minz_Translate::init();
  105. $nb = 0;
  106. foreach ($users as $userFilename) {
  107. $username = basename($userFilename, '.txt');
  108. if (!file_exists(USERS_PATH . '/' . $username . '/config.php')) {
  109. Minz_Log::warning('Warning: Removing broken user link: ' . $username . ' for ' . $self, PSHB_LOG);
  110. unlink($userFilename);
  111. continue;
  112. }
  113. try {
  114. FreshRSS_Context::initUser($username);
  115. if (!FreshRSS_Context::hasUserConf() || !FreshRSS_Context::userConf()->enabled) {
  116. Minz_Log::warning('FreshRSS skip disabled user ' . $username);
  117. continue;
  118. }
  119. Minz_ExtensionManager::enableByList(FreshRSS_Context::userConf()->extensions_enabled, 'user');
  120. Minz_Translate::reset(FreshRSS_Context::userConf()->language);
  121. [$nbUpdatedFeeds, ] = FreshRSS_feed_Controller::actualizeFeedsAndCommit(null, $self, null, $simplePie);
  122. if ($nbUpdatedFeeds > 0) {
  123. $nb++;
  124. } else {
  125. Minz_Log::warning('Warning: User ' . $username . ' does not subscribe anymore to ' . $self, PSHB_LOG);
  126. unlink($userFilename);
  127. }
  128. } catch (Exception $e) {
  129. Minz_Log::error('Error: ' . $e->getMessage() . ' for user ' . $username . ' and feed ' . $self, PSHB_LOG);
  130. }
  131. }
  132. $simplePie->__destruct(); //http://simplepie.org/wiki/faq/i_m_getting_memory_leaks
  133. unset($simplePie);
  134. if ($nb === 0) {
  135. header('HTTP/1.1 410 Gone');
  136. Minz_Log::warning('Warning: Nobody subscribes to this feed anymore after all!: ' . $self, PSHB_LOG);
  137. die('Nobody subscribes to this feed anymore after all!');
  138. } elseif (!empty($hubJson['error'])) {
  139. $hubJson['error'] = false;
  140. file_put_contents('./!hub.json', json_encode($hubJson));
  141. }
  142. Minz_Log::notice('WebSub ' . $self . ' done: ' . $nb, PSHB_LOG);
  143. exit('Done: ' . $nb . "\n");