UserDAO.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. if (defined('SQL_INSERT_FEEDS')) { //E.g. MySQL
  30. $sql = sprintf(SQL_INSERT_FEEDS, $bd_prefix_user);
  31. $stm = $userPDO->bd->prepare($sql);
  32. $ok &= $stm && $stm->execute();
  33. } else { //E.g. SQLite
  34. global $SQL_INSERT_FEEDS;
  35. if (is_array($SQL_INSERT_FEEDS)) {
  36. foreach ($SQL_INSERT_FEEDS as $instruction) {
  37. $sql = sprintf($instruction, $bd_prefix_user);
  38. $stm = $userPDO->bd->prepare($sql);
  39. $ok &= ($stm && $stm->execute());
  40. }
  41. }
  42. }
  43. }
  44. } catch (Exception $e) {
  45. Minz_Log::error('Error while creating user: ' . $e->getMessage());
  46. }
  47. Minz_Translate::reset($currentLanguage);
  48. if ($ok) {
  49. return true;
  50. } else {
  51. $info = empty($stm) ? array(2 => 'syntax error') : $stm->errorInfo();
  52. Minz_Log::error('SQL error: ' . $info[2]);
  53. return false;
  54. }
  55. }
  56. public function deleteUser($username) {
  57. $db = FreshRSS_Context::$system_conf->db;
  58. require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
  59. if ($db['type'] === 'sqlite') {
  60. return unlink(USERS_PATH . '/' . $username . '/db.sqlite');
  61. } else {
  62. $userPDO = new Minz_ModelPdo($username);
  63. $sql = sprintf(SQL_DROP_TABLES, $db['prefix'] . $username . '_');
  64. $stm = $userPDO->bd->prepare($sql);
  65. if ($stm && $stm->execute()) {
  66. return true;
  67. } else {
  68. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  69. Minz_Log::error('SQL error : ' . $info[2]);
  70. return false;
  71. }
  72. }
  73. }
  74. public static function exists($username) {
  75. return is_dir(USERS_PATH . '/' . $username);
  76. }
  77. public static function touch($username = '') {
  78. if (!FreshRSS_user_Controller::checkUsername($username)) {
  79. $username = Minz_Session::param('currentUser', '_');
  80. }
  81. return touch(USERS_PATH . '/' . $username . '/config.php');
  82. }
  83. public static function mtime($username) {
  84. return @filemtime(USERS_PATH . '/' . $username . '/config.php');
  85. }
  86. }