pshb.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. FreshRSS_Context::initSystem();
  9. if (FreshRSS_Context::$system_conf == null) {
  10. header('HTTP/1.1 500 Internal Server Error');
  11. die('Invalid system init!');
  12. }
  13. FreshRSS_Context::$system_conf->auth_type = 'none'; // avoid necessity to be logged in (not saved!)
  14. //Minz_Log::debug(print_r(array('_SERVER' => $_SERVER, '_GET' => $_GET, '_POST' => $_POST, 'INPUT' => $ORIGINAL_INPUT), true), PSHB_LOG);
  15. $key = isset($_GET['k']) ? substr($_GET['k'], 0, 128) : '';
  16. if (!ctype_xdigit($key)) {
  17. header('HTTP/1.1 422 Unprocessable Entity');
  18. die('Invalid feed key format!');
  19. }
  20. chdir(PSHB_PATH);
  21. $canonical = @file_get_contents('keys/' . $key . '.txt');
  22. if ($canonical === false) {
  23. if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'unsubscribe') {
  24. Minz_Log::warning('Warning: Accept unknown unsubscribe', PSHB_LOG);
  25. header('Connection: close');
  26. exit(isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '');
  27. }
  28. // https://github.com/w3c/websub/issues/106 , https://w3c.github.io/websub/#content-distribution
  29. header('HTTP/1.1 410 Gone');
  30. Minz_Log::warning('Warning: Feed key not found!: ' . $key, PSHB_LOG);
  31. die('Feed key not found!');
  32. }
  33. $canonical = trim($canonical);
  34. $canonicalHash = sha1($canonical);
  35. $hubFile = @file_get_contents('feeds/' . $canonicalHash . '/!hub.json');
  36. if ($hubFile === false) {
  37. header('HTTP/1.1 410 Gone');
  38. unlink('keys/' . $key . '.txt');
  39. Minz_Log::error('Error: Feed info not found!: ' . $canonical, PSHB_LOG);
  40. die('Feed info not found!');
  41. }
  42. $hubJson = json_decode($hubFile, true);
  43. if (!is_array($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/' . $canonicalHash);
  49. $users = glob('*.txt', GLOB_NOSORT);
  50. if (empty($users)) {
  51. header('HTTP/1.1 410 Gone');
  52. Minz_Log::warning('Warning: Nobody subscribes to this feed anymore!: ' . $canonical, PSHB_LOG);
  53. unlink('../../keys/' . $key . '.txt');
  54. $feed = new FreshRSS_Feed($canonical);
  55. $feed->pubSubHubbubSubscribe(false);
  56. unlink('!hub.json');
  57. chdir('..');
  58. recursive_unlink('feeds/' . $canonicalHash);
  59. die('Nobody subscribes to this feed anymore!');
  60. }
  61. if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'subscribe') {
  62. $leaseSeconds = empty($_REQUEST['hub_lease_seconds']) ? 0 : intval($_REQUEST['hub_lease_seconds']);
  63. if ($leaseSeconds > 60) {
  64. $hubJson['lease_end'] = time() + $leaseSeconds;
  65. } else {
  66. unset($hubJson['lease_end']);
  67. }
  68. $hubJson['lease_start'] = time();
  69. if (!isset($hubJson['error'])) {
  70. $hubJson['error'] = true; //Do not assume that WebSub works until the first successful push
  71. }
  72. file_put_contents('./!hub.json', json_encode($hubJson));
  73. header('Connection: close');
  74. exit(isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '');
  75. }
  76. if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'unsubscribe') {
  77. if (empty($hubJson['lease_end']) || $hubJson['lease_end'] < time()) {
  78. header('Connection: close');
  79. exit(isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '');
  80. } else {
  81. header('HTTP/1.1 422 Unprocessable Entity');
  82. die('We did not ask to unsubscribe!');
  83. }
  84. }
  85. if ($ORIGINAL_INPUT == '') {
  86. header('HTTP/1.1 422 Unprocessable Entity');
  87. die('Missing XML payload!');
  88. }
  89. $simplePie = customSimplePie();
  90. $simplePie->set_raw_data($ORIGINAL_INPUT);
  91. $simplePie->init();
  92. unset($ORIGINAL_INPUT);
  93. $links = $simplePie->get_links('self');
  94. $self = isset($links[0]) ? $links[0] : null;
  95. if ($self !== $canonical) {
  96. //header('HTTP/1.1 422 Unprocessable Entity');
  97. Minz_Log::warning('Warning: Self URL [' . $self . '] does not match registered canonical URL!: ' . $canonical, PSHB_LOG);
  98. //die('Self URL does not match registered canonical URL!');
  99. $self = $canonical;
  100. }
  101. Minz_ExtensionManager::init();
  102. Minz_Translate::init();
  103. $nb = 0;
  104. foreach ($users as $userFilename) {
  105. $username = basename($userFilename, '.txt');
  106. if (!file_exists(USERS_PATH . '/' . $username . '/config.php')) {
  107. Minz_Log::warning('Warning: Removing broken user link: ' . $username . ' for ' . $self, PSHB_LOG);
  108. unlink($userFilename);
  109. continue;
  110. }
  111. try {
  112. FreshRSS_Context::initUser($username);
  113. if (FreshRSS_Context::$user_conf == null || !FreshRSS_Context::$user_conf->enabled) {
  114. Minz_Log::warning('FreshRSS skip disabled user ' . $username);
  115. continue;
  116. }
  117. Minz_ExtensionManager::enableByList(FreshRSS_Context::$user_conf->extensions_enabled, 'user');
  118. Minz_Translate::reset(FreshRSS_Context::$user_conf->language);
  119. list($updated_feeds, $feed, $nb_new_articles) = FreshRSS_feed_Controller::actualizeFeed(0, $self, false, $simplePie);
  120. if ($updated_feeds > 0 || $feed != false) {
  121. $nb++;
  122. } else {
  123. Minz_Log::warning('Warning: User ' . $username . ' does not subscribe anymore to ' . $self, PSHB_LOG);
  124. unlink($userFilename);
  125. }
  126. } catch (Exception $e) {
  127. Minz_Log::error('Error: ' . $e->getMessage() . ' for user ' . $username . ' and feed ' . $self, PSHB_LOG);
  128. }
  129. }
  130. $simplePie->__destruct(); //http://simplepie.org/wiki/faq/i_m_getting_memory_leaks
  131. unset($simplePie);
  132. if ($nb === 0) {
  133. header('HTTP/1.1 410 Gone');
  134. Minz_Log::warning('Warning: Nobody subscribes to this feed anymore after all!: ' . $self, PSHB_LOG);
  135. die('Nobody subscribes to this feed anymore after all!');
  136. } elseif (!empty($hubJson['error'])) {
  137. $hubJson['error'] = false;
  138. file_put_contents('./!hub.json', json_encode($hubJson));
  139. }
  140. Minz_Log::notice('WebSub ' . $self . ' done: ' . $nb, PSHB_LOG);
  141. exit('Done: ' . $nb . "\n");