pshb.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. header('HTTP/1.1 404 Not Found');
  21. logMe('Error: Feed key not found!: ' . $key);
  22. die('Feed key not found!');
  23. }
  24. $canonical64 = trim($canonical64);
  25. if (!preg_match('/^[A-Za-z0-9_-]+$/D', $canonical64)) {
  26. header('HTTP/1.1 500 Internal Server Error');
  27. logMe('Error: Invalid key reference!: ' . $canonical64);
  28. die('Invalid key reference!');
  29. }
  30. $hubFile = @file_get_contents('feeds/' . $canonical64 . '/!hub.json');
  31. if ($hubFile === false) {
  32. header('HTTP/1.1 404 Not Found');
  33. //@unlink('keys/' . $key . '.txt');
  34. logMe('Error: Feed info not found!: ' . $canonical64);
  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. logMe('Error: Invalid key cross-check!: ' . $key);
  41. die('Invalid key cross-check!');
  42. }
  43. chdir('feeds/' . $canonical64);
  44. $users = glob('*.txt', GLOB_NOSORT);
  45. if (empty($users)) {
  46. header('HTTP/1.1 410 Gone');
  47. logMe('Error: Nobody is subscribed to this feed anymore!: ' . $canonical64);
  48. die('Nobody is subscribed to this feed anymore!');
  49. }
  50. if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'subscribe') {
  51. $leaseSeconds = empty($_REQUEST['hub_lease_seconds']) ? 0 : intval($_REQUEST['hub_lease_seconds']);
  52. if ($leaseSeconds > 60) {
  53. $hubJson['lease_end'] = time() + $leaseSeconds;
  54. } else {
  55. unset($hubJson['lease_end']);
  56. }
  57. $hubJson['lease_start'] = time();
  58. if (!isset($hubJson['error'])) {
  59. $hubJson['error'] = true; //Do not assume that PubSubHubbub works until the first successul push
  60. }
  61. file_put_contents('./!hub.json', json_encode($hubJson));
  62. header('Connection: close');
  63. exit(isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '');
  64. }
  65. if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'unsubscribe') {
  66. if (empty($hubJson['lease_end']) || $hubJson['lease_end'] < time()) {
  67. header('Connection: close');
  68. exit(isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '');
  69. } else {
  70. header('HTTP/1.1 422 Unprocessable Entity');
  71. die('We did not ask to unsubscribe!');
  72. }
  73. }
  74. if ($ORIGINAL_INPUT == '') {
  75. header('HTTP/1.1 422 Unprocessable Entity');
  76. die('Missing XML payload!');
  77. }
  78. Minz_Configuration::register('system', DATA_PATH . '/config.php', DATA_PATH . '/config.default.php');
  79. $system_conf = Minz_Configuration::get('system');
  80. $system_conf->auth_type = 'none'; // avoid necessity to be logged in (not saved!)
  81. $simplePie = customSimplePie();
  82. $simplePie->set_raw_data($ORIGINAL_INPUT);
  83. $simplePie->init();
  84. unset($ORIGINAL_INPUT);
  85. $links = $simplePie->get_links('self');
  86. $self = isset($links[0]) ? $links[0] : null;
  87. if ($self !== base64url_decode($canonical64)) {
  88. //header('HTTP/1.1 422 Unprocessable Entity');
  89. logMe('Warning: Self URL [' . $self . '] does not match registered canonical URL!: ' . base64url_decode($canonical64));
  90. //die('Self URL does not match registered canonical URL!');
  91. $self = base64url_decode($canonical64);
  92. }
  93. $nb = 0;
  94. foreach ($users as $userFilename) {
  95. $username = basename($userFilename, '.txt');
  96. if (!file_exists(USERS_PATH . '/' . $username . '/config.php')) {
  97. break;
  98. }
  99. try {
  100. Minz_Session::_param('currentUser', $username);
  101. Minz_Configuration::register('user',
  102. join_path(USERS_PATH, $username, 'config.php'),
  103. join_path(USERS_PATH, '_', 'config.default.php'));
  104. new Minz_ModelPdo($username); //TODO: FIXME: Quick-fix while waiting for a better FreshRSS() constructor/init
  105. FreshRSS_Context::init();
  106. list($updated_feeds, $feed) = FreshRSS_feed_Controller::actualizeFeed(0, $self, false, $simplePie);
  107. if ($updated_feeds > 0) {
  108. $nb++;
  109. }
  110. } catch (Exception $e) {
  111. logMe('Error: ' . $e->getMessage());
  112. }
  113. }
  114. $simplePie->__destruct();
  115. unset($simplePie);
  116. if ($nb === 0) {
  117. header('HTTP/1.1 410 Gone');
  118. logMe('Error: Nobody is subscribed to this feed anymore after all!: ' . $self);
  119. die('Nobody is subscribed to this feed anymore after all!');
  120. } elseif (!empty($hubJson['error'])) {
  121. $hubJson['error'] = false;
  122. file_put_contents('./!hub.json', json_encode($hubJson));
  123. }
  124. logMe('PubSubHubbub ' . $self . ' done: ' . $nb);
  125. exit('Done: ' . $nb . "\n");