UserDAO.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. public static function touch(string $username = ''): bool {
  40. if (!FreshRSS_user_Controller::checkUsername($username)) {
  41. $username = Minz_User::name() ?? Minz_User::INTERNAL_USER;
  42. }
  43. return touch(USERS_PATH . '/' . $username . '/config.php');
  44. }
  45. public static function mtime(string $username): int {
  46. return @(int)filemtime(USERS_PATH . '/' . $username . '/config.php');
  47. }
  48. }