pshb.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. $canonical64 = @file_get_contents('keys/' . $key . '.txt');
  18. if ($canonical64 === 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 404 Not Found');
  25. Minz_Log::warning('Warning: Feed key not found!: ' . $key, PSHB_LOG);
  26. die('Feed key not found!');
  27. }
  28. $canonical64 = trim($canonical64);
  29. if (!preg_match('/^[A-Za-z0-9_-]+$/D', $canonical64)) {
  30. header('HTTP/1.1 500 Internal Server Error');
  31. Minz_Log::error('Error: Invalid key reference!: ' . $canonical64, PSHB_LOG);
  32. die('Invalid key reference!');
  33. }
  34. $hubFile = @file_get_contents('feeds/' . $canonical64 . '/!hub.json');
  35. if ($hubFile === false) {
  36. header('HTTP/1.1 404 Not Found');
  37. unlink('keys/' . $key . '.txt');
  38. Minz_Log::error('Error: Feed info not found!: ' . $canonical64, PSHB_LOG);
  39. die('Feed info not found!');
  40. }
  41. $hubJson = json_decode($hubFile, true);
  42. if (!$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/' . $canonical64);
  48. $users = glob('*.txt', GLOB_NOSORT);
  49. if (empty($users)) {
  50. header('HTTP/1.1 410 Gone');
  51. $url = base64url_decode($canonical64);
  52. Minz_Log::warning('Warning: Nobody subscribes to this feed anymore!: ' . $url, PSHB_LOG);
  53. unlink('../../keys/' . $key . '.txt');
  54. $feed = new FreshRSS_Feed($url);
  55. $feed->pubSubHubbubSubscribe(false);
  56. unlink('!hub.json');
  57. chdir('..');
  58. recursive_unlink($canonical64);
  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 !== base64url_decode($canonical64)) {
  96. //header('HTTP/1.1 422 Unprocessable Entity');
  97. Minz_Log::warning('Warning: Self URL [' . $self . '] does not match registered canonical URL!: ' . base64url_decode($canonical64), PSHB_LOG);
  98. //die('Self URL does not match registered canonical URL!');
  99. $self = base64url_decode($canonical64);
  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) {
  114. Minz_ExtensionManager::enableByList(FreshRSS_Context::$user_conf->extensions_enabled);
  115. Minz_Translate::reset(FreshRSS_Context::$user_conf->language);
  116. }
  117. if (!FreshRSS_Context::$user_conf->enabled) {
  118. Minz_Log::warning('FreshRSS skip disabled user ' . $username);
  119. continue;
  120. }
  121. list($updated_feeds, $feed, $nb_new_articles) = FreshRSS_feed_Controller::actualizeFeed(0, $self, false, $simplePie);
  122. if ($updated_feeds > 0 || $feed != false) {
  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");