pshb.php 5.5 KB

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