pshb.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. file_put_contents('./!hub.json', json_encode($hubJson));
  57. exit(isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '');
  58. }
  59. if ($ORIGINAL_INPUT == '') {
  60. header('HTTP/1.1 422 Unprocessable Entity');
  61. die('Missing XML payload!');
  62. }
  63. Minz_Configuration::register('system', DATA_PATH . '/config.php', DATA_PATH . '/config.default.php');
  64. $system_conf = Minz_Configuration::get('system');
  65. $system_conf->auth_type = 'none'; // avoid necessity to be logged in (not saved!)
  66. Minz_Translate::init('en');
  67. Minz_Request::_param('ajax', true);
  68. $feedController = new FreshRSS_feed_Controller();
  69. $simplePie = customSimplePie();
  70. $simplePie->set_raw_data($ORIGINAL_INPUT);
  71. $simplePie->init();
  72. unset($ORIGINAL_INPUT);
  73. $links = $simplePie->get_links('self');
  74. $self = isset($links[0]) ? $links[0] : null;
  75. if ($self !== base64url_decode($canonical64)) {
  76. //header('HTTP/1.1 422 Unprocessable Entity');
  77. logMe('Warning: Self URL [' . $self . '] does not match registered canonical URL!: ' . base64url_decode($canonical64));
  78. //die('Self URL does not match registered canonical URL!');
  79. $self = base64url_decode($canonical64);
  80. }
  81. Minz_Request::_param('url', $self);
  82. $nb = 0;
  83. foreach ($users as $userFilename) {
  84. $username = basename($userFilename, '.txt');
  85. if (!file_exists(USERS_PATH . '/' . $username . '/config.php')) {
  86. break;
  87. }
  88. try {
  89. Minz_Session::_param('currentUser', $username);
  90. Minz_Configuration::register('user',
  91. join_path(USERS_PATH, $username, 'config.php'),
  92. join_path(USERS_PATH, '_', 'config.default.php'));
  93. FreshRSS_Context::init();
  94. if ($feedController->actualizeAction($simplePie) > 0) {
  95. $nb++;
  96. }
  97. } catch (Exception $e) {
  98. logMe('Error: ' . $e->getMessage());
  99. }
  100. }
  101. $simplePie->__destruct();
  102. unset($simplePie);
  103. if ($nb === 0) {
  104. header('HTTP/1.1 410 Gone');
  105. logMe('Error: Nobody is subscribed to this feed anymore after all!: ' . $self);
  106. die('Nobody is subscribed to this feed anymore after all!');
  107. }
  108. logMe('PubSubHubbub ' . $self . ' done: ' . $nb);
  109. exit('Done: ' . $nb . "\n");