pshb.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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('_GET' => $_GET, '_POST' => $_POST, 'INPUT' => $ORIGINAL_INPUT), true));
  11. $secret = isset($_GET['s']) ? substr($_GET['s'], 0, 128) : '';
  12. if (!ctype_xdigit($secret)) {
  13. header('HTTP/1.1 422 Unprocessable Entity');
  14. die('Invalid feed secret format!');
  15. }
  16. chdir(PSHB_PATH);
  17. $canonical64 = @file_get_contents('secrets/' . $secret . '.txt');
  18. if ($canonical64 === false) {
  19. header('HTTP/1.1 404 Not Found');
  20. logMe('Feed secret not found!: ' . $secret);
  21. die('Feed secret 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('Invalid secret reference!: ' . $canonical64);
  27. die('Invalid secret reference!');
  28. }
  29. $secret2 = @file_get_contents('feeds/' . $canonical64 . '/secret.txt');
  30. if ($secret2 === false) {
  31. header('HTTP/1.1 404 Not Found');
  32. //@unlink('secrets/' . $secret . '.txt');
  33. logMe('Feed reverse secret not found!: ' . $canonical64);
  34. die('Feed reverse secret not found!');
  35. }
  36. if ($secret !== $secret2) {
  37. header('HTTP/1.1 500 Internal Server Error');
  38. logMe('Invalid secret cross-check!: ' . $secret);
  39. die('Invalid secret cross-check!');
  40. }
  41. chdir('feeds/' . $canonical64);
  42. $users = glob('*/*.txt', GLOB_NOSORT);
  43. if (empty($users)) {
  44. header('HTTP/1.1 410 Gone');
  45. logMe('Nobody is subscribed to this feed anymore!: ' . $canonical64);
  46. die('Nobody is subscribed to this feed anymore!');
  47. }
  48. if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'subscribe') {
  49. //TODO: hub_lease_seconds
  50. exit(isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '');
  51. }
  52. Minz_Configuration::register('system', DATA_PATH . '/config.php', DATA_PATH . '/config.default.php');
  53. $system_conf = Minz_Configuration::get('system');
  54. $system_conf->auth_type = 'none'; // avoid necessity to be logged in (not saved!)
  55. Minz_Translate::init('en');
  56. Minz_Request::_param('ajax', true);
  57. $feedController = new FreshRSS_feed_Controller();
  58. $simplePie = customSimplePie();
  59. $simplePie->set_raw_data($ORIGINAL_INPUT);
  60. $simplePie->init();
  61. unset($ORIGINAL_INPUT);
  62. $links = $simplePie->get_links('self');
  63. $self = isset($links[0]) ? $links[0] : null;
  64. if ($self !== base64url_decode($canonical64)) {
  65. header('HTTP/1.1 422 Unprocessable Entity');
  66. logMe('Self URL does not match registered canonical URL!: ' . $self);
  67. die('Self URL does not match registered canonical URL!');
  68. }
  69. Minz_Request::_param('url', $self);
  70. $nb = 0;
  71. foreach ($users as $userLine) {
  72. $userLine = strtr($userLine, '\\', '/');
  73. $userInfos = explode('/', $userLine);
  74. $feedUrl = isset($userInfos[0]) ? base64url_decode($userInfos[0]) : '';
  75. $username = isset($userInfos[1]) ? basename($userInfos[1], '.txt') : '';
  76. if (!file_exists(USERS_PATH . '/' . $username . '/config.php')) {
  77. break;
  78. }
  79. try {
  80. Minz_Session::_param('currentUser', $username);
  81. Minz_Configuration::register('user',
  82. join_path(USERS_PATH, $username, 'config.php'),
  83. join_path(USERS_PATH, '_', 'config.default.php'));
  84. FreshRSS_Context::init();
  85. if ($feedController->actualizeAction($simplePie) > 0) {
  86. $nb++;
  87. }
  88. } catch (Exception $e) {
  89. logMe($e->getMessage());
  90. }
  91. }
  92. $simplePie->__destruct();
  93. unset($simplePie);
  94. if ($nb === 0) {
  95. header('HTTP/1.1 410 Gone');
  96. logMe('Nobody is subscribed to this feed anymore after all!: ' . $self);
  97. die('Nobody is subscribed to this feed anymore after all!');
  98. }
  99. logMe($self . ' done: ' . $nb);
  100. exit('Done: ' . $nb . "\n");