UserDAO.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. return true;
  29. } else {
  30. $info = $this->pdo->errorInfo();
  31. Minz_Log::error(__METHOD__ . ' error: ' . json_encode($info));
  32. return false;
  33. }
  34. }
  35. public static function exists(string $username): bool {
  36. return is_dir(USERS_PATH . '/' . $username);
  37. }
  38. public static function touch(string $username = ''): bool {
  39. if (!FreshRSS_user_Controller::checkUsername($username)) {
  40. $username = Minz_User::name() ?? Minz_User::INTERNAL_USER;
  41. }
  42. return touch(USERS_PATH . '/' . $username . '/config.php');
  43. }
  44. public static function mtime(string $username): int {
  45. return @(int)filemtime(USERS_PATH . '/' . $username . '/config.php');
  46. }
  47. }