javascriptController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. declare(strict_types=1);
  3. class FreshRSS_javascript_Controller extends FreshRSS_ActionController {
  4. /**
  5. * @var FreshRSS_ViewJavascript
  6. * @phpstan-ignore property.phpDocType
  7. */
  8. protected $view;
  9. public function __construct() {
  10. parent::__construct(FreshRSS_ViewJavascript::class);
  11. }
  12. #[\Override]
  13. public function firstAction(): void {
  14. $this->view->_layout(null);
  15. }
  16. public function actualizeAction(): void {
  17. if (!FreshRSS_Auth::hasAccess() && !(
  18. FreshRSS_Context::systemConf()->allow_anonymous
  19. && FreshRSS_Context::systemConf()->allow_anonymous_refresh
  20. )) {
  21. Minz_Error::error(403);
  22. return;
  23. }
  24. header('Content-Type: application/json; charset=UTF-8');
  25. Minz_Session::_param('actualize_feeds', false);
  26. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  27. $databaseDAO->minorDbMaintenance();
  28. Minz_ExtensionManager::callHookVoid(Minz_HookType::FreshrssUserMaintenance);
  29. $catDAO = FreshRSS_Factory::createCategoryDao();
  30. $this->view->categories = $catDAO->listCategoriesOrderUpdate(FreshRSS_Context::userConf()->dynamic_opml_ttl_default);
  31. $feedDAO = FreshRSS_Factory::createFeedDao();
  32. $this->view->feeds = $feedDAO->listFeedsOrderUpdate(FreshRSS_Context::userConf()->ttl_default);
  33. // When the refresh button is used from a feed or category view, limit the
  34. // batch to the feeds visible in that view.
  35. $get = Minz_Request::paramString('get');
  36. if (preg_match('/^c_(\d+)$/', $get, $matches)) {
  37. $category = $this->view->categories[(int)$matches[1]] ?? null;
  38. if ($category !== null) {
  39. $this->view->categories = [$category->id() => $category];
  40. // Filter feeds to keep only those from the selected category, preserving the order
  41. $this->view->feeds = array_filter($this->view->feeds, static fn(FreshRSS_Feed $feed) => $feed->category() === $category->id());
  42. }
  43. } elseif (preg_match('/^f_(\d+)$/', $get, $matches)) {
  44. $feed = $feedDAO->searchById((int)$matches[1]);
  45. $this->view->categories = [];
  46. $this->view->feeds = $feed === null ? [] : [$feed->id() => $feed];
  47. }
  48. }
  49. public function nbUnreadsPerFeedAction(): void {
  50. if (!FreshRSS_Auth::hasAccess() && !FreshRSS_Context::systemConf()->allow_anonymous) {
  51. Minz_Error::error(403);
  52. return;
  53. }
  54. header('Content-Type: application/json; charset=UTF-8');
  55. $catDAO = FreshRSS_Factory::createCategoryDao();
  56. $this->view->categories = $catDAO->listCategories(prePopulateFeeds: true, details: false);
  57. $tagDAO = FreshRSS_Factory::createTagDao();
  58. $this->view->tags = $tagDAO->listTags(precounts: true);
  59. }
  60. //For Web-form login
  61. /**
  62. * @throws Exception
  63. */
  64. public function nonceAction(): void {
  65. header('Content-Type: application/json; charset=UTF-8');
  66. header('Last-Modified: ' . gmdate('D, d M Y H:i:s \\G\\M\\T'));
  67. header('Expires: 0');
  68. header('Cache-Control: private, no-cache, no-store, must-revalidate');
  69. header('Pragma: no-cache');
  70. $user = Minz_Request::paramString('user');
  71. if ($user === '') {
  72. Minz_Error::error(400);
  73. return;
  74. }
  75. $user_conf = FreshRSS_UserConfiguration::getForUser($user);
  76. if ($user_conf !== null) {
  77. try {
  78. $s = $user_conf->passwordHash;
  79. if (strlen($s) >= 60) {
  80. //CRYPT_BLOWFISH Salt: "$2a$", a two digit cost parameter, "$", and 22 characters from the alphabet "./0-9A-Za-z".
  81. $this->view->salt1 = substr($s, 0, 29);
  82. $this->view->nonce = hash('sha256', FreshRSS_Context::systemConf()->salt . $user . random_bytes(32));
  83. Minz_Session::_param('nonce', $this->view->nonce);
  84. return; //Success
  85. }
  86. } catch (Minz_Exception $me) {
  87. Minz_Log::warning('Nonce failure: ' . $me->getMessage());
  88. }
  89. } else {
  90. Minz_Log::notice('Nonce failure due to invalid username! ' . $user);
  91. }
  92. //Failure: Return random data.
  93. $this->view->salt1 = sprintf('$2a$%02d$', FreshRSS_password_Util::BCRYPT_COST);
  94. $alphabet = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  95. for ($i = 22; $i > 0; $i--) {
  96. $this->view->salt1 .= $alphabet[random_int(0, 63)];
  97. }
  98. $this->view->nonce = hash('sha256', 'failure' . rand());
  99. Minz_Session::_param('nonce', $this->view->nonce);
  100. }
  101. }