4
0

javascriptController.php 3.9 KB

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