UserDAO.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. class FreshRSS_UserDAO extends Minz_ModelPdo {
  3. public function createUser($username) {
  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. $ok = false;
  8. $bd_prefix_user = $db['prefix'] . $username . '_';
  9. if (defined('SQL_CREATE_TABLES')) { //E.g. MySQL
  10. $sql = sprintf(SQL_CREATE_TABLES, $bd_prefix_user, _t('gen.short.default_category'));
  11. $stm = $userPDO->bd->prepare($sql);
  12. $ok = $stm && $stm->execute();
  13. } else { //E.g. SQLite
  14. global $SQL_CREATE_TABLES;
  15. if (is_array($SQL_CREATE_TABLES)) {
  16. $ok = true;
  17. foreach ($SQL_CREATE_TABLES as $instruction) {
  18. $sql = sprintf($instruction, $bd_prefix_user, _t('gen.short.default_category'));
  19. $stm = $userPDO->bd->prepare($sql);
  20. $ok &= ($stm && $stm->execute());
  21. }
  22. }
  23. }
  24. if ($ok) {
  25. return true;
  26. } else {
  27. $info = empty($stm) ? array(2 => 'syntax error') : $stm->errorInfo();
  28. Minz_Log::error('SQL error : ' . $info[2]);
  29. return false;
  30. }
  31. }
  32. public function deleteUser($username) {
  33. $db = FreshRSS_Context::$system_conf->db;
  34. require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
  35. if ($db['type'] === 'sqlite') {
  36. return unlink(join_path(DATA_PATH, 'users', $username, 'db.sqlite'));
  37. } else {
  38. $userPDO = new Minz_ModelPdo($username);
  39. $sql = sprintf(SQL_DROP_TABLES, $db['prefix'] . $username . '_');
  40. $stm = $userPDO->bd->prepare($sql);
  41. if ($stm && $stm->execute()) {
  42. return true;
  43. } else {
  44. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  45. Minz_Log::error('SQL error : ' . $info[2]);
  46. return false;
  47. }
  48. }
  49. }
  50. public static function exist($username) {
  51. return is_dir(join_path(DATA_PATH , 'users', $username));
  52. }
  53. public static function touch($username) {
  54. return touch(join_path(DATA_PATH , 'users', $username, 'config.php'));
  55. }
  56. public static function mtime($username) {
  57. return @filemtime(join_path(DATA_PATH , 'users', $username, 'config.php'));
  58. }
  59. }