UserDAO.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. class FreshRSS_UserDAO extends Minz_ModelPdo {
  3. public function createUser($new_user_language = null, $insertDefaultFeeds = false) {
  4. require_once(APP_PATH . '/SQL/install.sql.' . $this->bd->dbType() . '.php');
  5. $currentLanguage = Minz_Translate::language();
  6. try {
  7. if ($new_user_language != null) {
  8. Minz_Translate::reset($new_user_language);
  9. }
  10. $ok = false;
  11. if (defined('SQL_CREATE_TABLES')) { //E.g. MySQL
  12. $sql = sprintf(SQL_CREATE_TABLES . SQL_CREATE_TABLE_ENTRYTMP . SQL_CREATE_TABLE_TAGS, $this->prefix, _t('gen.short.default_category'));
  13. $stm = $this->bd->prepare($sql);
  14. $ok = $stm && $stm->execute();
  15. } else { //E.g. SQLite
  16. global $SQL_CREATE_TABLES, $SQL_CREATE_TABLE_ENTRYTMP, $SQL_CREATE_TABLE_TAGS;
  17. if (is_array($SQL_CREATE_TABLES)) {
  18. $instructions = array_merge($SQL_CREATE_TABLES, $SQL_CREATE_TABLE_ENTRYTMP, $SQL_CREATE_TABLE_TAGS);
  19. $ok = !empty($instructions);
  20. foreach ($instructions as $instruction) {
  21. $sql = sprintf($instruction, $this->prefix, _t('gen.short.default_category'));
  22. $stm = $this->bd->prepare($sql);
  23. $ok &= ($stm && $stm->execute());
  24. }
  25. }
  26. }
  27. if ($ok && $insertDefaultFeeds) {
  28. $default_feeds = FreshRSS_Context::$system_conf->default_feeds;
  29. foreach ($default_feeds as $feed) {
  30. $sql = sprintf(SQL_INSERT_FEED, $this->prefix);
  31. $stm = $this->bd->prepare($sql);
  32. $parameters = array(
  33. ':url' => $feed['url'],
  34. ':name' => $feed['name'],
  35. ':website' => $feed['website'],
  36. ':description' => $feed['description'],
  37. );
  38. $ok &= ($stm && $stm->execute($parameters));
  39. }
  40. }
  41. } catch (Exception $e) {
  42. Minz_Log::error('Error while creating database for user: ' . $e->getMessage());
  43. }
  44. Minz_Translate::reset($currentLanguage);
  45. if ($ok) {
  46. return true;
  47. } else {
  48. $info = empty($stm) ? array(2 => 'syntax error') : $stm->errorInfo();
  49. Minz_Log::error(__METHOD__ . ' error: ' . $info[2]);
  50. return false;
  51. }
  52. }
  53. public function deleteUser() {
  54. if (defined('STDERR')) {
  55. fwrite(STDERR, 'Deleting SQL data for user “' . $this->current_user . "”…\n");
  56. }
  57. require_once(APP_PATH . '/SQL/install.sql.' . $this->bd->dbType() . '.php');
  58. $ok = false;
  59. if (defined('SQL_DROP_TABLES')) { //E.g. MySQL
  60. $sql = sprintf(SQL_DROP_TABLES, $this->prefix);
  61. $stm = $this->bd->prepare($sql);
  62. $ok = $stm && $stm->execute();
  63. } else { //E.g. SQLite
  64. global $SQL_DROP_TABLES;
  65. if (is_array($SQL_DROP_TABLES)) {
  66. $instructions = $SQL_DROP_TABLES;
  67. $ok = !empty($instructions);
  68. foreach ($instructions as $instruction) {
  69. $sql = sprintf($instruction, $this->prefix);
  70. $stm = $this->bd->prepare($sql);
  71. $ok &= ($stm && $stm->execute());
  72. }
  73. }
  74. }
  75. if ($ok) {
  76. return true;
  77. } else {
  78. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  79. Minz_Log::error(__METHOD__ . ' error: ' . $info[2]);
  80. return false;
  81. }
  82. }
  83. public static function exists($username) {
  84. return is_dir(USERS_PATH . '/' . $username);
  85. }
  86. public static function touch($username = '') {
  87. if (!FreshRSS_user_Controller::checkUsername($username)) {
  88. $username = Minz_Session::param('currentUser', '_');
  89. }
  90. return touch(USERS_PATH . '/' . $username . '/config.php');
  91. }
  92. public static function mtime($username) {
  93. return @filemtime(USERS_PATH . '/' . $username . '/config.php');
  94. }
  95. }