pshb.php 5.7 KB

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