UserDAO.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. if (!FreshRSS_user_Controller::checkUsername($username)) {
  45. return false;
  46. }
  47. return is_dir(USERS_PATH . '/' . $username);
  48. }
  49. /** Update time of the last modification action by the user (e.g., mark an article as read) */
  50. public static function touch(string $username = ''): bool {
  51. if ($username === '') {
  52. $username = Minz_User::name() ?? Minz_User::INTERNAL_USER;
  53. } elseif (!FreshRSS_user_Controller::checkUsername($username)) {
  54. return false;
  55. }
  56. return touch(USERS_PATH . '/' . $username . '/config.php');
  57. }
  58. /** Time of the last modification action by the user (e.g., mark an article as read) */
  59. public static function mtime(string $username): int {
  60. if (!FreshRSS_user_Controller::checkUsername($username)) {
  61. return 0;
  62. }
  63. return @filemtime(USERS_PATH . '/' . $username . '/config.php') ?: 0;
  64. }
  65. /** Update time of the last new content automatically received by the user (e.g., cron job, WebSub) */
  66. public static function ctouch(string $username = ''): bool {
  67. if ($username === '') {
  68. $username = Minz_User::name() ?? Minz_User::INTERNAL_USER;
  69. } elseif (!FreshRSS_user_Controller::checkUsername($username)) {
  70. return false;
  71. }
  72. return touch(USERS_PATH . '/' . $username . '/' . LOG_FILENAME);
  73. }
  74. /** Time of the last new content automatically received by the user (e.g., cron job, WebSub) */
  75. public static function ctime(string $username): int {
  76. if (!FreshRSS_user_Controller::checkUsername($username)) {
  77. return 0;
  78. }
  79. return @filemtime(USERS_PATH . '/' . $username . '/' . LOG_FILENAME) ?: 0;
  80. }
  81. }