userController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Controller to handle user actions.
  5. */
  6. class FreshRSS_user_Controller extends FreshRSS_ActionController {
  7. /**
  8. * The username is also used as folder name, file name, and part of SQL table name.
  9. * '_' is a reserved internal username.
  10. */
  11. public const USERNAME_PATTERN = '([0-9a-zA-Z_][0-9a-zA-Z_.@\-]{1,38}|[0-9a-zA-Z])';
  12. public static function checkUsername(string $username): bool {
  13. return preg_match('/^' . self::USERNAME_PATTERN . '$/', $username) === 1;
  14. }
  15. public static function userExists(string $username): bool {
  16. return @file_exists(USERS_PATH . '/' . $username . '/config.php');
  17. }
  18. /** @param array<string,mixed> $userConfigUpdated */
  19. public static function updateUser(string $user, ?string $email, string $passwordPlain, array $userConfigUpdated = []): bool {
  20. $userConfig = get_user_configuration($user);
  21. if ($userConfig === null) {
  22. return false;
  23. }
  24. if ($email !== null && $userConfig->mail_login !== $email) {
  25. $userConfig->mail_login = $email;
  26. if (FreshRSS_Context::systemConf()->force_email_validation) {
  27. $salt = FreshRSS_Context::systemConf()->salt;
  28. $userConfig->email_validation_token = sha1($salt . uniqid('' . mt_rand(), true));
  29. $mailer = new FreshRSS_User_Mailer();
  30. $mailer->send_email_need_validation($user, $userConfig);
  31. }
  32. }
  33. if ($passwordPlain != '') {
  34. $passwordHash = FreshRSS_password_Util::hash($passwordPlain);
  35. $userConfig->passwordHash = $passwordHash;
  36. }
  37. foreach ($userConfigUpdated as $configName => $configValue) {
  38. if ($configValue !== null) {
  39. $userConfig->_param($configName, $configValue);
  40. }
  41. }
  42. $ok = $userConfig->save();
  43. return $ok;
  44. }
  45. public function updateAction(): void {
  46. if (!FreshRSS_Auth::hasAccess('admin')) {
  47. Minz_Error::error(403);
  48. }
  49. if (Minz_Request::isPost()) {
  50. $passwordPlain = Minz_Request::paramString('newPasswordPlain', true);
  51. Minz_Request::_param('newPasswordPlain'); //Discard plain-text password ASAP
  52. $_POST['newPasswordPlain'] = '';
  53. $username = Minz_Request::paramString('username');
  54. $ok = self::updateUser($username, null, $passwordPlain, [
  55. 'token' => Minz_Request::paramString('token') ?: null,
  56. ]);
  57. if ($ok) {
  58. $isSelfUpdate = Minz_User::name() === $username;
  59. if ($passwordPlain == '' || !$isSelfUpdate) {
  60. Minz_Request::good(_t('feedback.user.updated', $username), ['c' => 'user', 'a' => 'manage']);
  61. } else {
  62. Minz_Request::good(_t('feedback.profile.updated'), ['c' => 'index', 'a' => 'index']);
  63. }
  64. } else {
  65. Minz_Request::bad(_t('feedback.user.updated.error', $username), ['c' => 'user', 'a' => 'manage']);
  66. }
  67. }
  68. }
  69. /**
  70. * This action displays the user profile page.
  71. */
  72. public function profileAction(): void {
  73. if (!FreshRSS_Auth::hasAccess()) {
  74. Minz_Error::error(403);
  75. }
  76. $email_not_verified = FreshRSS_Context::userConf()->email_validation_token != '';
  77. $this->view->disable_aside = false;
  78. if ($email_not_verified) {
  79. $this->view->_layout('simple');
  80. $this->view->disable_aside = true;
  81. }
  82. FreshRSS_View::prependTitle(_t('conf.profile.title') . ' · ');
  83. FreshRSS_View::appendScript(Minz_Url::display('/scripts/bcrypt.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/bcrypt.min.js')));
  84. if (Minz_Request::isPost() && Minz_User::name() != null) {
  85. $old_email = FreshRSS_Context::userConf()->mail_login;
  86. $email = Minz_Request::paramString('email');
  87. $passwordPlain = Minz_Request::paramString('newPasswordPlain', true);
  88. Minz_Request::_param('newPasswordPlain'); //Discard plain-text password ASAP
  89. $_POST['newPasswordPlain'] = '';
  90. if (FreshRSS_Context::systemConf()->force_email_validation && empty($email)) {
  91. Minz_Request::bad(
  92. _t('user.email.feedback.required'),
  93. ['c' => 'user', 'a' => 'profile']
  94. );
  95. }
  96. if (!empty($email) && !validateEmailAddress($email)) {
  97. Minz_Request::bad(
  98. _t('user.email.feedback.invalid'),
  99. ['c' => 'user', 'a' => 'profile']
  100. );
  101. }
  102. $ok = self::updateUser(
  103. Minz_User::name(),
  104. $email,
  105. $passwordPlain,
  106. [
  107. 'token' => Minz_Request::paramString('token'),
  108. ]
  109. );
  110. Minz_Session::_param('passwordHash', FreshRSS_Context::userConf()->passwordHash);
  111. if ($ok) {
  112. if (FreshRSS_Context::systemConf()->force_email_validation && $email !== $old_email) {
  113. Minz_Request::good(_t('feedback.profile.updated'), ['c' => 'user', 'a' => 'validateEmail']);
  114. } elseif ($passwordPlain == '') {
  115. Minz_Request::good(_t('feedback.profile.updated'), ['c' => 'user', 'a' => 'profile']);
  116. } else {
  117. Minz_Request::good(_t('feedback.profile.updated'), ['c' => 'index', 'a' => 'index']);
  118. }
  119. } else {
  120. Minz_Request::bad(_t('feedback.profile.error'), ['c' => 'user', 'a' => 'profile']);
  121. }
  122. }
  123. }
  124. public function purgeAction(): void {
  125. if (!FreshRSS_Auth::hasAccess('admin')) {
  126. Minz_Error::error(403);
  127. }
  128. if (Minz_Request::isPost()) {
  129. $username = Minz_Request::paramString('username');
  130. if (!FreshRSS_UserDAO::exists($username)) {
  131. Minz_Error::error(404);
  132. }
  133. $feedDAO = FreshRSS_Factory::createFeedDao($username);
  134. $feedDAO->purge();
  135. }
  136. }
  137. /**
  138. * This action displays the user management page.
  139. */
  140. public function manageAction(): void {
  141. if (!FreshRSS_Auth::hasAccess('admin')) {
  142. Minz_Error::error(403);
  143. }
  144. FreshRSS_View::prependTitle(_t('admin.user.title') . ' · ');
  145. if (Minz_Request::isPost()) {
  146. $action = Minz_Request::paramString('action');
  147. switch ($action) {
  148. case 'delete':
  149. $this->deleteAction();
  150. break;
  151. case 'update':
  152. $this->updateAction();
  153. break;
  154. case 'purge':
  155. $this->purgeAction();
  156. break;
  157. case 'promote':
  158. $this->promoteAction();
  159. break;
  160. case 'demote':
  161. $this->demoteAction();
  162. break;
  163. case 'enable':
  164. $this->enableAction();
  165. break;
  166. case 'disable':
  167. $this->disableAction();
  168. break;
  169. }
  170. }
  171. $this->view->show_email_field = FreshRSS_Context::systemConf()->force_email_validation;
  172. $this->view->current_user = Minz_Request::paramString('u');
  173. foreach (listUsers() as $user) {
  174. $this->view->users[$user] = $this->retrieveUserDetails($user);
  175. }
  176. }
  177. /**
  178. * @param array<string,mixed> $userConfigOverride
  179. * @throws Minz_ConfigurationNamespaceException
  180. * @throws Minz_PDOConnectionException
  181. */
  182. public static function createUser(string $new_user_name, ?string $email, string $passwordPlain,
  183. array $userConfigOverride = [], bool $insertDefaultFeeds = true): bool {
  184. $userConfig = [];
  185. $customUserConfigPath = join_path(DATA_PATH, 'config-user.custom.php');
  186. if (file_exists($customUserConfigPath)) {
  187. $customUserConfig = include($customUserConfigPath);
  188. if (is_array($customUserConfig)) {
  189. $userConfig = $customUserConfig;
  190. }
  191. }
  192. $userConfig = array_merge($userConfig, $userConfigOverride);
  193. $ok = self::checkUsername($new_user_name);
  194. $homeDir = join_path(DATA_PATH, 'users', $new_user_name);
  195. // create basepath if missing
  196. if (!is_dir(join_path(DATA_PATH, 'users'))) {
  197. $ok &= mkdir(join_path(DATA_PATH, 'users'), 0770, true);
  198. }
  199. $configPath = '';
  200. if ($ok) {
  201. $languages = Minz_Translate::availableLanguages();
  202. if (empty($userConfig['language']) || !in_array($userConfig['language'], $languages, true)) {
  203. $userConfig['language'] = 'en';
  204. }
  205. $ok &= !in_array(strtoupper($new_user_name), array_map('strtoupper', listUsers()), true); //Not an existing user, case-insensitive
  206. $configPath = join_path($homeDir, 'config.php');
  207. $ok &= !file_exists($configPath);
  208. }
  209. if ($ok) {
  210. // $homeDir must not exist beforehand,
  211. // otherwise it might be multiple remote parties racing to register one username
  212. $ok = mkdir($homeDir, 0770, true);
  213. if ($ok) {
  214. $ok &= (file_put_contents($configPath, "<?php\n return " . var_export($userConfig, true) . ';') !== false);
  215. }
  216. }
  217. if ($ok) {
  218. $newUserDAO = FreshRSS_Factory::createUserDao($new_user_name);
  219. $ok &= $newUserDAO->createUser();
  220. if ($ok && $insertDefaultFeeds) {
  221. $opmlPath = DATA_PATH . '/opml.xml';
  222. if (!file_exists($opmlPath)) {
  223. $opmlPath = FRESHRSS_PATH . '/opml.default.xml';
  224. }
  225. $importController = new FreshRSS_importExport_Controller();
  226. try {
  227. $importController->importFile($opmlPath, $opmlPath, $new_user_name);
  228. } catch (Exception $e) {
  229. Minz_Log::error('Error while importing default OPML for user ' . $new_user_name . ': ' . $e->getMessage());
  230. }
  231. }
  232. $ok &= self::updateUser($new_user_name, $email, $passwordPlain);
  233. }
  234. return (bool)$ok;
  235. }
  236. /**
  237. * This action creates a new user.
  238. *
  239. * Request parameters are:
  240. * - new_user_language
  241. * - new_user_name
  242. * - new_user_email
  243. * - new_user_passwordPlain
  244. * - r (i.e. a redirection url, optional)
  245. *
  246. * @todo clean up this method. Idea: write a method to init a user with basic information.
  247. */
  248. public function createAction(): void {
  249. if (!FreshRSS_Auth::hasAccess('admin') && max_registrations_reached()) {
  250. Minz_Error::error(403);
  251. }
  252. if (Minz_Request::isPost()) {
  253. $new_user_name = Minz_Request::paramString('new_user_name');
  254. $email = Minz_Request::paramString('new_user_email');
  255. $passwordPlain = Minz_Request::paramString('new_user_passwordPlain', true);
  256. $badRedirectUrl = [
  257. 'c' => Minz_Request::paramString('originController') ?: 'auth',
  258. 'a' => Minz_Request::paramString('originAction') ?: 'register',
  259. ];
  260. if (!self::checkUsername($new_user_name)) {
  261. Minz_Request::bad(
  262. _t('user.username.invalid'),
  263. $badRedirectUrl
  264. );
  265. }
  266. if (FreshRSS_UserDAO::exists($new_user_name)) {
  267. Minz_Request::bad(
  268. _t('user.username.taken', $new_user_name),
  269. $badRedirectUrl
  270. );
  271. }
  272. if (!FreshRSS_password_Util::check($passwordPlain)) {
  273. Minz_Request::bad(
  274. _t('user.password.invalid'),
  275. $badRedirectUrl
  276. );
  277. }
  278. if (!FreshRSS_Auth::hasAccess('admin')) {
  279. // TODO: We may want to ask the user to accept TOS before first login
  280. $tos_enabled = file_exists(TOS_FILENAME);
  281. $accept_tos = Minz_Request::paramBoolean('accept_tos');
  282. if ($tos_enabled && !$accept_tos) {
  283. Minz_Request::bad(_t('user.tos.feedback.invalid'), $badRedirectUrl);
  284. }
  285. }
  286. if (FreshRSS_Context::systemConf()->force_email_validation && empty($email)) {
  287. Minz_Request::bad(
  288. _t('user.email.feedback.required'),
  289. $badRedirectUrl
  290. );
  291. }
  292. if (!empty($email) && !validateEmailAddress($email)) {
  293. Minz_Request::bad(
  294. _t('user.email.feedback.invalid'),
  295. $badRedirectUrl
  296. );
  297. }
  298. $ok = self::createUser($new_user_name, $email, $passwordPlain, [
  299. 'language' => Minz_Request::paramString('new_user_language') ?: FreshRSS_Context::userConf()->language,
  300. 'timezone' => Minz_Request::paramString('new_user_timezone'),
  301. 'is_admin' => Minz_Request::paramBoolean('new_user_is_admin'),
  302. 'enabled' => true,
  303. ]);
  304. Minz_Request::_param('new_user_passwordPlain'); //Discard plain-text password ASAP
  305. $_POST['new_user_passwordPlain'] = '';
  306. invalidateHttpCache();
  307. // If the user has admin access, it means he’s already logged in
  308. // and we don’t want to login with the new account. Otherwise, the
  309. // user just created its account himself so he probably wants to
  310. // get started immediately.
  311. if ($ok && !FreshRSS_Auth::hasAccess('admin')) {
  312. $user_conf = get_user_configuration($new_user_name);
  313. if ($user_conf !== null) {
  314. Minz_Session::_params([
  315. Minz_User::CURRENT_USER => $new_user_name,
  316. 'passwordHash' => $user_conf->passwordHash,
  317. 'csrf' => false,
  318. ]);
  319. FreshRSS_Auth::giveAccess();
  320. } else {
  321. $ok = false;
  322. }
  323. }
  324. if ($ok) {
  325. Minz_Request::setGoodNotification(_t('feedback.user.created', $new_user_name));
  326. } else {
  327. Minz_Request::setBadNotification(_t('feedback.user.created.error', $new_user_name));
  328. }
  329. }
  330. if (FreshRSS_Auth::hasAccess('admin')) {
  331. $redirect_url = ['c' => 'user', 'a' => 'manage'];
  332. } else {
  333. $redirect_url = ['c' => 'index', 'a' => 'index'];
  334. }
  335. Minz_Request::forward($redirect_url, true);
  336. }
  337. public static function deleteUser(string $username): bool {
  338. $ok = self::checkUsername($username);
  339. if ($ok) {
  340. $default_user = FreshRSS_Context::systemConf()->default_user;
  341. $ok &= (strcasecmp($username, $default_user) !== 0); //It is forbidden to delete the default user
  342. }
  343. $user_data = join_path(DATA_PATH, 'users', $username);
  344. $ok &= is_dir($user_data);
  345. if ($ok) {
  346. FreshRSS_fever_Util::deleteKey($username);
  347. Minz_ModelPdo::$usesSharedPdo = false;
  348. $oldUserDAO = FreshRSS_Factory::createUserDao($username);
  349. $ok &= $oldUserDAO->deleteUser();
  350. Minz_ModelPdo::$usesSharedPdo = true;
  351. $ok &= recursive_unlink($user_data);
  352. $filenames = glob(PSHB_PATH . '/feeds/*/' . $username . '.txt');
  353. if (!empty($filenames)) {
  354. array_map('unlink', $filenames);
  355. }
  356. }
  357. return (bool)$ok;
  358. }
  359. /**
  360. * This action validates an email address, based on the token sent by email.
  361. * It also serves the main page when user is blocked.
  362. *
  363. * Request parameters are:
  364. * - username
  365. * - token
  366. *
  367. * This route works with GET requests since the URL is provided by email.
  368. * The security risks (e.g. forged URL by an attacker) are not very high so
  369. * it’s ok.
  370. *
  371. * It returns 404 error if `force_email_validation` is disabled or if the
  372. * user doesn’t exist.
  373. *
  374. * It returns 403 if user isn’t logged in and `username` param isn’t passed.
  375. */
  376. public function validateEmailAction(): void {
  377. if (!FreshRSS_Context::systemConf()->force_email_validation) {
  378. Minz_Error::error(404);
  379. }
  380. FreshRSS_View::prependTitle(_t('user.email.validation.title') . ' · ');
  381. $this->view->_layout('simple');
  382. $username = Minz_Request::paramString('username');
  383. $token = Minz_Request::paramString('token');
  384. if ($username !== '') {
  385. $user_config = get_user_configuration($username);
  386. } elseif (FreshRSS_Auth::hasAccess()) {
  387. $user_config = FreshRSS_Context::userConf();
  388. } else {
  389. Minz_Error::error(403);
  390. return;
  391. }
  392. if (!FreshRSS_UserDAO::exists($username) || $user_config === null) {
  393. Minz_Error::error(404);
  394. return;
  395. }
  396. if ($user_config->email_validation_token === '') {
  397. Minz_Request::good(
  398. _t('user.email.validation.feedback.unnecessary'),
  399. ['c' => 'index', 'a' => 'index']
  400. );
  401. }
  402. if ($token != '') {
  403. if ($user_config->email_validation_token !== $token) {
  404. Minz_Request::bad(
  405. _t('user.email.validation.feedback.wrong_token'),
  406. ['c' => 'user', 'a' => 'validateEmail']
  407. );
  408. }
  409. $user_config->email_validation_token = '';
  410. if ($user_config->save()) {
  411. Minz_Request::good(
  412. _t('user.email.validation.feedback.ok'),
  413. ['c' => 'index', 'a' => 'index']
  414. );
  415. } else {
  416. Minz_Request::bad(
  417. _t('user.email.validation.feedback.error'),
  418. ['c' => 'user', 'a' => 'validateEmail']
  419. );
  420. }
  421. }
  422. }
  423. /**
  424. * This action resends a validation email to the current user.
  425. *
  426. * It only acts on POST requests but doesn’t require any param (except the
  427. * CSRF token).
  428. *
  429. * It returns 403 error if the user is not logged in or 404 if request is
  430. * not POST. Else it redirects silently to the index if user has already
  431. * validated its email, or to the user#validateEmail route.
  432. */
  433. public function sendValidationEmailAction(): void {
  434. if (!FreshRSS_Auth::hasAccess()) {
  435. Minz_Error::error(403);
  436. }
  437. if (!Minz_Request::isPost()) {
  438. Minz_Error::error(404);
  439. }
  440. $username = Minz_User::name();
  441. if (FreshRSS_Context::userConf()->email_validation_token === '') {
  442. Minz_Request::forward([
  443. 'c' => 'index',
  444. 'a' => 'index',
  445. ], true);
  446. }
  447. $mailer = new FreshRSS_User_Mailer();
  448. $ok = $username != null && $mailer->send_email_need_validation($username, FreshRSS_Context::userConf());
  449. $redirect_url = ['c' => 'user', 'a' => 'validateEmail'];
  450. if ($ok) {
  451. Minz_Request::good(
  452. _t('user.email.validation.feedback.email_sent'),
  453. $redirect_url
  454. );
  455. } else {
  456. Minz_Request::bad(
  457. _t('user.email.validation.feedback.email_failed'),
  458. $redirect_url
  459. );
  460. }
  461. }
  462. /**
  463. * This action delete an existing user.
  464. *
  465. * Request parameter is:
  466. * - username
  467. *
  468. * @todo clean up this method. Idea: create a User->clean() method.
  469. */
  470. public function deleteAction(): void {
  471. $username = Minz_Request::paramString('username');
  472. $self_deletion = Minz_User::name() === $username;
  473. if (!FreshRSS_Auth::hasAccess('admin') && !$self_deletion) {
  474. Minz_Error::error(403);
  475. }
  476. $redirect_url = ['c' => 'user', 'a' => 'manage'];
  477. if (Minz_Request::isPost()) {
  478. $ok = true;
  479. if ($self_deletion) {
  480. // We check the password if it’s a self-destruction
  481. $nonce = Minz_Session::paramString('nonce');
  482. $challenge = Minz_Request::paramString('challenge');
  483. $ok &= FreshRSS_FormAuth::checkCredentials(
  484. $username, FreshRSS_Context::userConf()->passwordHash,
  485. $nonce, $challenge
  486. );
  487. }
  488. if ($ok) {
  489. $ok &= self::deleteUser($username);
  490. }
  491. if ($ok && $self_deletion) {
  492. FreshRSS_Auth::removeAccess();
  493. $redirect_url = ['c' => 'index', 'a' => 'index'];
  494. }
  495. invalidateHttpCache();
  496. if ($ok) {
  497. Minz_Request::setGoodNotification(_t('feedback.user.deleted', $username));
  498. } else {
  499. Minz_Request::setBadNotification(_t('feedback.user.deleted.error', $username));
  500. }
  501. }
  502. Minz_Request::forward($redirect_url, true);
  503. }
  504. public function promoteAction(): void {
  505. $this->toggleAction('is_admin', true);
  506. }
  507. public function demoteAction(): void {
  508. $this->toggleAction('is_admin', false);
  509. }
  510. public function enableAction(): void {
  511. $this->toggleAction('enabled', true);
  512. }
  513. public function disableAction(): void {
  514. $this->toggleAction('enabled', false);
  515. }
  516. private function toggleAction(string $field, bool $value): void {
  517. if (!FreshRSS_Auth::hasAccess('admin')) {
  518. Minz_Error::error(403);
  519. }
  520. if (!Minz_Request::isPost()) {
  521. Minz_Error::error(403);
  522. }
  523. $username = Minz_Request::paramString('username');
  524. if (!FreshRSS_UserDAO::exists($username)) {
  525. Minz_Error::error(404);
  526. }
  527. if (null === $userConfig = get_user_configuration($username)) {
  528. Minz_Error::error(500);
  529. return;
  530. }
  531. $userConfig->_param($field, $value);
  532. $ok = $userConfig->save();
  533. FreshRSS_UserDAO::touch($username);
  534. if ($ok) {
  535. Minz_Request::good(_t('feedback.user.updated', $username), ['c' => 'user', 'a' => 'manage']);
  536. } else {
  537. Minz_Request::bad(
  538. _t('feedback.user.updated.error', $username),
  539. ['c' => 'user', 'a' => 'manage']
  540. );
  541. }
  542. }
  543. public function detailsAction(): void {
  544. if (!FreshRSS_Auth::hasAccess('admin')) {
  545. Minz_Error::error(403);
  546. }
  547. $username = Minz_Request::paramString('username');
  548. if (!FreshRSS_UserDAO::exists($username)) {
  549. Minz_Error::error(404);
  550. }
  551. if (Minz_Request::paramBoolean('ajax')) {
  552. $this->view->_layout(null);
  553. }
  554. $this->view->username = $username;
  555. $this->view->details = $this->retrieveUserDetails($username);
  556. FreshRSS_View::prependTitle($username . ' · ' . _t('gen.menu.user_management') . ' · ');
  557. }
  558. /** @return array{'feed_count':int,'article_count':int,'database_size':int,'language':string,'mail_login':string,'enabled':bool,'is_admin':bool,'last_user_activity':string,'is_default':bool} */
  559. private function retrieveUserDetails(string $username): array {
  560. $feedDAO = FreshRSS_Factory::createFeedDao($username);
  561. $entryDAO = FreshRSS_Factory::createEntryDao($username);
  562. $databaseDAO = FreshRSS_Factory::createDatabaseDAO($username);
  563. $userConfiguration = get_user_configuration($username);
  564. if ($userConfiguration === null) {
  565. throw new Exception('Error loading user configuration!');
  566. }
  567. return [
  568. 'feed_count' => $feedDAO->count(),
  569. 'article_count' => $entryDAO->count(),
  570. 'database_size' => $databaseDAO->size(),
  571. 'language' => $userConfiguration->language,
  572. 'mail_login' => $userConfiguration->mail_login,
  573. 'enabled' => $userConfiguration->enabled,
  574. 'is_admin' => $userConfiguration->is_admin,
  575. 'last_user_activity' => date('c', FreshRSS_UserDAO::mtime($username)) ?: '',
  576. 'is_default' => FreshRSS_Context::systemConf()->default_user === $username,
  577. ];
  578. }
  579. }