UserDAO.php 3.1 KB

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