4
0

UserDAO.php 2.7 KB

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