UserDAO.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. declare(strict_types=1);
  3. class FreshRSS_UserDAO extends Minz_ModelPdo {
  4. public function createUser(): bool {
  5. require(APP_PATH . '/SQL/install.sql.' . $this->pdo->dbType() . '.php');
  6. try {
  7. $sql = $GLOBALS['SQL_CREATE_TABLES'];
  8. $ok = $this->pdo->exec($sql) !== false; //Note: Only exec() can take multiple statements safely.
  9. } catch (Exception $e) {
  10. $ok = false;
  11. Minz_Log::error('Error while creating database for user ' . $this->current_user . ': ' . $e->getMessage());
  12. }
  13. if ($ok) {
  14. return true;
  15. } else {
  16. $info = $this->pdo->errorInfo();
  17. Minz_Log::error(__METHOD__ . ' error: ' . json_encode($info));
  18. return false;
  19. }
  20. }
  21. public function deleteUser(): bool {
  22. if (defined('STDERR')) {
  23. fwrite(STDERR, 'Deleting SQL data for user “' . $this->current_user . "”…\n");
  24. }
  25. require(APP_PATH . '/SQL/install.sql.' . $this->pdo->dbType() . '.php');
  26. $ok = $this->pdo->exec($GLOBALS['SQL_DROP_TABLES']) !== false;
  27. if ($ok) {
  28. $this->close();
  29. return true;
  30. } else {
  31. $info = $this->pdo->errorInfo();
  32. Minz_Log::error(__METHOD__ . ' error: ' . json_encode($info));
  33. return false;
  34. }
  35. }
  36. public static function exists(string $username): bool {
  37. return is_dir(USERS_PATH . '/' . $username);
  38. }
  39. /** Update time of the last modification action by the user (e.g., mark an article as read) */
  40. public static function touch(string $username = ''): bool {
  41. if ($username === '') {
  42. $username = Minz_User::name() ?? Minz_User::INTERNAL_USER;
  43. } elseif (!FreshRSS_user_Controller::checkUsername($username)) {
  44. return false;
  45. }
  46. return touch(USERS_PATH . '/' . $username . '/config.php');
  47. }
  48. /** Time of the last modification action by the user (e.g., mark an article as read) */
  49. public static function mtime(string $username): int {
  50. return @filemtime(USERS_PATH . '/' . $username . '/config.php') ?: 0;
  51. }
  52. /** Update time of the last new content automatically received by the user (e.g., cron job, WebSub) */
  53. public static function ctouch(string $username = ''): bool {
  54. if ($username === '') {
  55. $username = Minz_User::name() ?? Minz_User::INTERNAL_USER;
  56. } elseif (!FreshRSS_user_Controller::checkUsername($username)) {
  57. return false;
  58. }
  59. return touch(USERS_PATH . '/' . $username . '/' . LOG_FILENAME);
  60. }
  61. /** Time of the last new content automatically received by the user (e.g., cron job, WebSub) */
  62. public static function ctime(string $username): int {
  63. return @filemtime(USERS_PATH . '/' . $username . '/' . LOG_FILENAME) ?: 0;
  64. }
  65. }