pshb.php 5.6 KB

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