UserDAO.php 3.0 KB

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