pshb.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. Minz_Translate::init('en');
  82. Minz_Request::_param('ajax', true);
  83. $feedController = new FreshRSS_feed_Controller();
  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 !== base64url_decode($canonical64)) {
  91. //header('HTTP/1.1 422 Unprocessable Entity');
  92. logMe('Warning: Self URL [' . $self . '] does not match registered canonical URL!: ' . base64url_decode($canonical64));
  93. //die('Self URL does not match registered canonical URL!');
  94. $self = base64url_decode($canonical64);
  95. }
  96. Minz_Request::_param('url', $self);
  97. $nb = 0;
  98. foreach ($users as $userFilename) {
  99. $username = basename($userFilename, '.txt');
  100. if (!file_exists(USERS_PATH . '/' . $username . '/config.php')) {
  101. break;
  102. }
  103. try {
  104. Minz_Session::_param('currentUser', $username);
  105. Minz_Configuration::register('user',
  106. join_path(USERS_PATH, $username, 'config.php'),
  107. join_path(USERS_PATH, '_', 'config.default.php'));
  108. FreshRSS_Context::init();
  109. if ($feedController->actualizeAction($simplePie) > 0) {
  110. $nb++;
  111. }
  112. } catch (Exception $e) {
  113. logMe('Error: ' . $e->getMessage());
  114. }
  115. }
  116. $simplePie->__destruct();
  117. unset($simplePie);
  118. if ($nb === 0) {
  119. header('HTTP/1.1 410 Gone');
  120. logMe('Error: Nobody is subscribed to this feed anymore after all!: ' . $self);
  121. die('Nobody is subscribed to this feed anymore after all!');
  122. } elseif (!empty($hubJson['error'])) {
  123. $hubJson['error'] = false;
  124. file_put_contents('./!hub.json', json_encode($hubJson));
  125. }
  126. logMe('PubSubHubbub ' . $self . ' done: ' . $nb);
  127. exit('Done: ' . $nb . "\n");