UserDAO.php 3.1 KB

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