pshb.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. require('../../constants.php');
  3. require(LIB_PATH . '/lib_rss.php'); //Includes class autoloader
  4. define('MAX_PAYLOAD', 3145728);
  5. header('Content-Type: text/plain; charset=UTF-8');
  6. header('X-Content-Type-Options: nosniff');
  7. function logMe($text) {
  8. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
  9. }
  10. $ORIGINAL_INPUT = file_get_contents('php://input', false, null, -1, MAX_PAYLOAD);
  11. //logMe(print_r(array('_SERVER' => $_SERVER, '_GET' => $_GET, '_POST' => $_POST, 'INPUT' => $ORIGINAL_INPUT), true));
  12. $key = isset($_GET['k']) ? substr($_GET['k'], 0, 128) : '';
  13. if (!ctype_xdigit($key)) {
  14. header('HTTP/1.1 422 Unprocessable Entity');
  15. die('Invalid feed key format!');
  16. }
  17. chdir(PSHB_PATH);
  18. $canonical64 = @file_get_contents('keys/' . $key . '.txt');
  19. if ($canonical64 === false) {
  20. if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'unsubscribe') {
  21. logMe('Warning: Accept unknown unsubscribe');
  22. header('Connection: close');
  23. exit(isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '');
  24. }
  25. header('HTTP/1.1 404 Not Found');
  26. logMe('Warning: Feed key not found!: ' . $key);
  27. die('Feed key not found!');
  28. }
  29. $canonical64 = trim($canonical64);
  30. if (!preg_match('/^[A-Za-z0-9_-]+$/D', $canonical64)) {
  31. header('HTTP/1.1 500 Internal Server Error');
  32. logMe('Error: Invalid key reference!: ' . $canonical64);
  33. die('Invalid key reference!');
  34. }
  35. $hubFile = @file_get_contents('feeds/' . $canonical64 . '/!hub.json');
  36. if ($hubFile === false) {
  37. header('HTTP/1.1 404 Not Found');
  38. unlink('keys/' . $key . '.txt');
  39. logMe('Error: Feed info not found!: ' . $canonical64);
  40. die('Feed info not found!');
  41. }
  42. $hubJson = json_decode($hubFile, true);
  43. if (!$hubJson || empty($hubJson['key']) || $hubJson['key'] !== $key) {
  44. header('HTTP/1.1 500 Internal Server Error');
  45. logMe('Error: Invalid key cross-check!: ' . $key);
  46. die('Invalid key cross-check!');
  47. }
  48. chdir('feeds/' . $canonical64);
  49. $users = glob('*.txt', GLOB_NOSORT);
  50. if (empty($users)) {
  51. header('HTTP/1.1 410 Gone');
  52. $url = base64url_decode($canonical64);
  53. logMe('Warning: Nobody subscribes to this feed anymore!: ' . $url);
  54. unlink('../../keys/' . $key . '.txt');
  55. Minz_Configuration::register('system',
  56. DATA_PATH . '/config.php',
  57. DATA_PATH . '/config.default.php');
  58. FreshRSS_Context::$system_conf = Minz_Configuration::get('system');
  59. $feed = new FreshRSS_Feed($url);
  60. $feed->pubSubHubbubSubscribe(false);
  61. unlink('!hub.json');
  62. chdir('..');
  63. recursive_unlink($canonical64);
  64. die('Nobody subscribes to this feed anymore!');
  65. }
  66. if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'subscribe') {
  67. $leaseSeconds = empty($_REQUEST['hub_lease_seconds']) ? 0 : intval($_REQUEST['hub_lease_seconds']);
  68. if ($leaseSeconds > 60) {
  69. $hubJson['lease_end'] = time() + $leaseSeconds;
  70. } else {
  71. unset($hubJson['lease_end']);
  72. }
  73. $hubJson['lease_start'] = time();
  74. if (!isset($hubJson['error'])) {
  75. $hubJson['error'] = true; //Do not assume that PubSubHubbub works until the first successul push
  76. }
  77. file_put_contents('./!hub.json', json_encode($hubJson));
  78. header('Connection: close');
  79. exit(isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '');
  80. }
  81. if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'unsubscribe') {
  82. if (empty($hubJson['lease_end']) || $hubJson['lease_end'] < time()) {
  83. header('Connection: close');
  84. exit(isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '');
  85. } else {
  86. header('HTTP/1.1 422 Unprocessable Entity');
  87. die('We did not ask to unsubscribe!');
  88. }
  89. }
  90. if ($ORIGINAL_INPUT == '') {
  91. header('HTTP/1.1 422 Unprocessable Entity');
  92. die('Missing XML payload!');
  93. }
  94. Minz_Configuration::register('system', DATA_PATH . '/config.php', DATA_PATH . '/config.default.php');
  95. $system_conf = Minz_Configuration::get('system');
  96. $system_conf->auth_type = 'none'; // avoid necessity to be logged in (not saved!)
  97. $simplePie = customSimplePie();
  98. $simplePie->set_raw_data($ORIGINAL_INPUT);
  99. $simplePie->init();
  100. unset($ORIGINAL_INPUT);
  101. $links = $simplePie->get_links('self');
  102. $self = isset($links[0]) ? $links[0] : null;
  103. if ($self !== base64url_decode($canonical64)) {
  104. //header('HTTP/1.1 422 Unprocessable Entity');
  105. logMe('Warning: Self URL [' . $self . '] does not match registered canonical URL!: ' . base64url_decode($canonical64));
  106. //die('Self URL does not match registered canonical URL!');
  107. $self = base64url_decode($canonical64);
  108. }
  109. $nb = 0;
  110. foreach ($users as $userFilename) {
  111. $username = basename($userFilename, '.txt');
  112. if (!file_exists(USERS_PATH . '/' . $username . '/config.php')) {
  113. logMe('Warning: Removing broken user link: ' . $username . ' for ' . $self);
  114. unlink($userFilename);
  115. continue;
  116. }
  117. try {
  118. Minz_Session::_param('currentUser', $username);
  119. Minz_Configuration::register('user',
  120. join_path(USERS_PATH, $username, 'config.php'),
  121. join_path(USERS_PATH, '_', 'config.default.php'));
  122. new Minz_ModelPdo($username); //TODO: FIXME: Quick-fix while waiting for a better FreshRSS() constructor/init
  123. FreshRSS_Context::init();
  124. list($updated_feeds, $feed) = FreshRSS_feed_Controller::actualizeFeed(0, $self, false, $simplePie);
  125. if ($updated_feeds > 0 || $feed != false) {
  126. $nb++;
  127. } else {
  128. logMe('Warning: User ' . $username . ' does not subscribe anymore to ' . $self);
  129. unlink($userFilename);
  130. }
  131. } catch (Exception $e) {
  132. logMe('Error: ' . $e->getMessage() . ' for user ' . $username . ' and feed ' . $self);
  133. }
  134. }
  135. $simplePie->__destruct();
  136. unset($simplePie);
  137. if ($nb === 0) {
  138. header('HTTP/1.1 410 Gone');
  139. logMe('Warning: Nobody subscribes to this feed anymore after all!: ' . $self);
  140. die('Nobody subscribes to this feed anymore after all!');
  141. } elseif (!empty($hubJson['error'])) {
  142. $hubJson['error'] = false;
  143. file_put_contents('./!hub.json', json_encode($hubJson));
  144. }
  145. logMe('PubSubHubbub ' . $self . ' done: ' . $nb);
  146. exit('Done: ' . $nb . "\n");