pshb.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. function logMe($text) {
  7. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
  8. }
  9. $ORIGINAL_INPUT = file_get_contents('php://input', false, null, -1, MAX_PAYLOAD);
  10. //logMe(print_r(array('_SERVER' => $_SERVER, '_GET' => $_GET, '_POST' => $_POST, 'INPUT' => $ORIGINAL_INPUT), true));
  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. header('HTTP/1.1 404 Not Found');
  20. logMe('Error: Feed key not found!: ' . $key);
  21. die('Feed key not found!');
  22. }
  23. $canonical64 = trim($canonical64);
  24. if (!preg_match('/^[A-Za-z0-9_-]+$/D', $canonical64)) {
  25. header('HTTP/1.1 500 Internal Server Error');
  26. logMe('Error: Invalid key reference!: ' . $canonical64);
  27. die('Invalid key reference!');
  28. }
  29. $hubFile = @file_get_contents('feeds/' . $canonical64 . '/!hub.json');
  30. if ($hubFile === false) {
  31. header('HTTP/1.1 404 Not Found');
  32. //@unlink('keys/' . $key . '.txt');
  33. logMe('Error: Feed info not found!: ' . $canonical64);
  34. die('Feed info not found!');
  35. }
  36. $hubJson = json_decode($hubFile, true);
  37. if (!$hubJson || empty($hubJson['key']) || $hubJson['key'] !== $key) {
  38. header('HTTP/1.1 500 Internal Server Error');
  39. logMe('Error: Invalid key cross-check!: ' . $key);
  40. die('Invalid key cross-check!');
  41. }
  42. chdir('feeds/' . $canonical64);
  43. $users = glob('*.txt', GLOB_NOSORT);
  44. if (empty($users)) {
  45. header('HTTP/1.1 410 Gone');
  46. logMe('Error: Nobody is subscribed to this feed anymore!: ' . $canonical64);
  47. die('Nobody is subscribed to this feed anymore!');
  48. }
  49. if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'subscribe') {
  50. $leaseSeconds = empty($_REQUEST['hub_lease_seconds']) ? 0 : intval($_REQUEST['hub_lease_seconds']);
  51. if ($leaseSeconds > 60) {
  52. $hubJson['lease_end'] = time() + $leaseSeconds;
  53. } else {
  54. unset($hubJson['lease_end']);
  55. }
  56. $hubJson['lease_start'] = time();
  57. if (!isset($hubJson['error'])) {
  58. $hubJson['error'] = true; //Do not assume that PubSubHubbub works until the first successul push
  59. }
  60. file_put_contents('./!hub.json', json_encode($hubJson));
  61. exit(isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '');
  62. }
  63. if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'unsubscribe') {
  64. if (empty($hubJson['lease_end']) || $hubJson['lease_end'] < time()) {
  65. exit(isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '');
  66. } else {
  67. header('HTTP/1.1 422 Unprocessable Entity');
  68. die('We did not ask to unsubscribe!');
  69. }
  70. }
  71. if ($ORIGINAL_INPUT == '') {
  72. header('HTTP/1.1 422 Unprocessable Entity');
  73. die('Missing XML payload!');
  74. }
  75. Minz_Configuration::register('system', DATA_PATH . '/config.php', DATA_PATH . '/config.default.php');
  76. $system_conf = Minz_Configuration::get('system');
  77. $system_conf->auth_type = 'none'; // avoid necessity to be logged in (not saved!)
  78. Minz_Translate::init('en');
  79. Minz_Request::_param('ajax', true);
  80. $feedController = new FreshRSS_feed_Controller();
  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. Minz_Request::_param('url', $self);
  94. $nb = 0;
  95. foreach ($users as $userFilename) {
  96. $username = basename($userFilename, '.txt');
  97. if (!file_exists(USERS_PATH . '/' . $username . '/config.php')) {
  98. break;
  99. }
  100. try {
  101. Minz_Session::_param('currentUser', $username);
  102. Minz_Configuration::register('user',
  103. join_path(USERS_PATH, $username, 'config.php'),
  104. join_path(USERS_PATH, '_', 'config.default.php'));
  105. FreshRSS_Context::init();
  106. if ($feedController->actualizeAction($simplePie) > 0) {
  107. $nb++;
  108. }
  109. } catch (Exception $e) {
  110. logMe('Error: ' . $e->getMessage());
  111. }
  112. }
  113. $simplePie->__destruct();
  114. unset($simplePie);
  115. if ($nb === 0) {
  116. header('HTTP/1.1 410 Gone');
  117. logMe('Error: Nobody is subscribed to this feed anymore after all!: ' . $self);
  118. die('Nobody is subscribed to this feed anymore after all!');
  119. } elseif (!empty($hubJson['error'])) {
  120. $hubJson['error'] = false;
  121. file_put_contents('./!hub.json', json_encode($hubJson));
  122. }
  123. logMe('PubSubHubbub ' . $self . ' done: ' . $nb);
  124. exit('Done: ' . $nb . "\n");