/default_www/backend/modules/users/engine/model.php

https://github.com/zakgrant/forkcms · PHP · 394 lines · 162 code · 69 blank · 163 comment · 4 complexity · e3f56b91aa21df7af61c322c4d47d5c2 MD5 · raw file

  1. <?php
  2. /**
  3. * In this file we store all generic functions that we will be using in the users module.
  4. *
  5. * @package backend
  6. * @subpackage users
  7. *
  8. * @author Tijs Verkoyen <tijs@netlash.com>
  9. * @author Davy Hellemans <davy@netlash.com>
  10. * @since 2.0
  11. */
  12. class BackendUsersModel
  13. {
  14. /**
  15. * Overview of the active users
  16. *
  17. * @var string
  18. */
  19. const QRY_BROWSE = 'SELECT i.id
  20. FROM users AS i
  21. WHERE i.deleted = ?';
  22. /**
  23. * Mark the user as deleted and deactivate his account.
  24. *
  25. * @return void
  26. * @param int $id The userId to delete.
  27. */
  28. public static function delete($id)
  29. {
  30. BackendModel::getDB(true)->update('users', array('active' => 'N', 'deleted' => 'Y'), 'id = ?', array((int) $id));
  31. }
  32. /**
  33. * Deletes the reset_password_key and reset_password_timestamp for a given user ID
  34. *
  35. * @return void
  36. * @param int $id The userId wherfore the reset-stuff should be deleted.
  37. */
  38. public static function deleteResetPasswordSettings($id)
  39. {
  40. BackendModel::getDB(true)->delete('users_settings', '(name = \'reset_password_key\' OR name = \'reset_password_timestamp\') AND user_id = ?', array((int) $id));
  41. }
  42. /**
  43. * Was a user deleted before?
  44. *
  45. * @return bool
  46. * @param string $email The e-mail adress to check.
  47. */
  48. public static function emailDeletedBefore($email)
  49. {
  50. // redefine
  51. $email = (string) $email;
  52. // no user to ignore
  53. return (bool) BackendModel::getDB()->getVar('SELECT COUNT(i.id)
  54. FROM users AS i
  55. WHERE i.email = ? AND i.deleted = ?',
  56. array($email, 'Y'));
  57. }
  58. /**
  59. * Does the user exist.
  60. *
  61. * @return bool
  62. * @param int $id The userId to check for existance.
  63. * @param bool[optional] $active Should the user be active also?
  64. */
  65. public static function exists($id, $active = true)
  66. {
  67. // redefine
  68. $id = (int) $id;
  69. $active = (bool) $active;
  70. // get db
  71. $db = BackendModel::getDB();
  72. // if the user should also be active, there should be at least one row to return true
  73. if($active) return (bool) $db->getVar('SELECT COUNT(i.id)
  74. FROM users AS i
  75. WHERE i.id = ? AND i.deleted = ?',
  76. array($id, 'N'));
  77. // fallback, this doesn't take the active nor deleted status in account
  78. return (bool) $db->getVar('SELECT COUNT(i.id)
  79. FROM users AS i
  80. WHERE i.id = ?',
  81. array($id));
  82. }
  83. /**
  84. * Does a email already exist?
  85. * If you specify a userId, the email with the given id will be ignored.
  86. *
  87. * @return bool
  88. * @param string $email The email to check for.
  89. * @param int[optional] $id The userId to be ignored.
  90. */
  91. public static function existsEmail($email, $id = null)
  92. {
  93. // redefine
  94. $email = (string) $email;
  95. $id = ($id !== null) ? (int) $id : null;
  96. // get db
  97. $db = BackendModel::getDB();
  98. // userid specified?
  99. if($id !== null) return (bool) $db->getVar('SELECT COUNT(i.id)
  100. FROM users AS i
  101. WHERE i.id != ? AND i.email = ?',
  102. array($id, $email));
  103. // no user to ignore
  104. return (bool) $db->getVar('SELECT COUNT(i.id)
  105. FROM users AS i
  106. WHERE i.email = ?',
  107. array($email));
  108. }
  109. /**
  110. * Get all data for a given user
  111. *
  112. * @return array
  113. * @param int $id The userId to get the data for.
  114. */
  115. public static function get($id)
  116. {
  117. // redefine
  118. $id = (int) $id;
  119. // get db
  120. $db = BackendModel::getDB();
  121. // get general user data
  122. $user = (array) $db->getRecord('SELECT i.id, i.email, i.active
  123. FROM users AS i
  124. WHERE i.id = ?',
  125. array($id));
  126. // get user-settings
  127. $user['settings'] = (array) $db->getPairs('SELECT s.name, s.value
  128. FROM users_settings AS s
  129. WHERE s.user_id = ?',
  130. array($id));
  131. // loop settings and unserialize them
  132. foreach($user['settings'] as &$value) $value = unserialize($value);
  133. // return
  134. return $user;
  135. }
  136. /**
  137. * Fetch the list of date formats including examples of these formats.
  138. *
  139. * @return array
  140. */
  141. public static function getDateFormats()
  142. {
  143. // init var
  144. $possibleFormats = array();
  145. // loop available formats
  146. foreach((array) BackendModel::getModuleSetting('users', 'date_formats') as $format)
  147. {
  148. $possibleFormats[$format] = SpoonDate::getDate($format, null, BackendAuthentication::getUser()->getSetting('interface_language'));
  149. }
  150. // return
  151. return $possibleFormats;
  152. }
  153. /**
  154. * Get user groups
  155. *
  156. * @return array
  157. */
  158. public static function getGroups()
  159. {
  160. return (array) BackendModel::getDB()->getPairs('SELECT i.id, i.name
  161. FROM groups AS i');
  162. }
  163. /**
  164. * Get the user ID linked to a given email
  165. *
  166. * @return int
  167. * @param string $email The email for the user.
  168. */
  169. public static function getIdByEmail($email)
  170. {
  171. // get user-settings
  172. $userId = BackendModel::getDB()->getVar('SELECT i.id
  173. FROM users AS i
  174. WHERE i.email = ?',
  175. array((string) $email));
  176. // userId or false on error
  177. return ($userId == 0) ? false : (int) $userId;
  178. }
  179. /**
  180. * Fetch the list of number formats including examples of these formats.
  181. *
  182. * @return array
  183. */
  184. public static function getNumberFormats()
  185. {
  186. // init var
  187. $possibleFormats = array();
  188. // loop available formats
  189. foreach((array) BackendModel::getModuleSetting('core', 'number_formats') as $format => $example)
  190. {
  191. $possibleFormats[$format] = $example;
  192. }
  193. // return
  194. return $possibleFormats;
  195. }
  196. /**
  197. * Fetch the list of time formats including examples of these formats.
  198. *
  199. * @return array
  200. */
  201. public static function getTimeFormats()
  202. {
  203. // init var
  204. $possibleFormats = array();
  205. // loop available formats
  206. foreach(BackendModel::getModuleSetting('users', 'time_formats') as $format)
  207. {
  208. $possibleFormats[$format] = SpoonDate::getDate($format, null, BackendAuthentication::getUser()->getSetting('interface_language'));
  209. }
  210. // return
  211. return $possibleFormats;
  212. }
  213. /**
  214. * Get all users
  215. *
  216. * @return array
  217. */
  218. public static function getUsers()
  219. {
  220. // fetch users
  221. $users = (array) BackendModel::getDB()->getPairs('SELECT i.id, s.value
  222. FROM users AS i
  223. INNER JOIN users_settings AS s ON i.id = s.user_id AND s.name = ?
  224. WHERE i.active = ? AND i.deleted = ?',
  225. array('nickname', 'Y', 'N'), 'id');
  226. // loop users & unserialize
  227. foreach($users as &$value) $value = unserialize($value);
  228. // return
  229. return $users;
  230. }
  231. /**
  232. * Add a new user.
  233. *
  234. * @return int
  235. * @param array $user The userdata.
  236. * @param array $settings The settings for the new user.
  237. */
  238. public static function insert(array $user, array $settings)
  239. {
  240. // get db
  241. $db = BackendModel::getDB(true);
  242. // update user
  243. $userId = (int) $db->insert('users', $user);
  244. $userSettings = array();
  245. // loop settings
  246. foreach($settings as $key => $value) $userSettings[] = array('user_id' => $userId, 'name' => $key, 'value' => serialize($value));
  247. // insert all settings at once
  248. $db->insert('users_settings', $userSettings);
  249. // return the new users' id
  250. return $userId;
  251. }
  252. /**
  253. * Restores a user
  254. * @later this method should check if all needed data is present
  255. *
  256. * @return bool
  257. * @param string $email The e-mail adress of the user to restore.
  258. */
  259. public static function undoDelete($email)
  260. {
  261. // redefine
  262. $email = (string) $email;
  263. // get db
  264. $db = BackendModel::getDB(true);
  265. // get id
  266. $id = $db->getVar('SELECT id
  267. FROM users AS i
  268. INNER JOIN users_settings AS s ON i.id = s.user_id
  269. WHERE i.email = ? AND i.deleted = ?',
  270. array($email, 'Y'));
  271. // no valid users
  272. if($id === null) return false;
  273. else
  274. {
  275. // restore
  276. $db->update('users', array('active' => 'Y', 'deleted' => 'N'), 'id = ?', (int) $id);
  277. // return
  278. return true;
  279. }
  280. }
  281. /**
  282. * Save the changes for a given user
  283. * Remark: $user['id'] should be available
  284. *
  285. * @return void
  286. * @param array $user The userdata.
  287. * @param array $settings The settings for the user.
  288. */
  289. public static function update(array $user, array $settings)
  290. {
  291. // get db
  292. $db = BackendModel::getDB(true);
  293. // update user
  294. $updated = $db->update('users', $user, 'id = ?', array($user['id']));
  295. // loop settings
  296. foreach($settings as $key => $value)
  297. {
  298. // insert or update
  299. $db->execute('INSERT INTO users_settings(user_id, name, value)
  300. VALUES(?, ?, ?)
  301. ON DUPLICATE KEY UPDATE value = ?',
  302. array($user['id'], $key, serialize($value), serialize($value)));
  303. }
  304. // return updated
  305. return $updated;
  306. }
  307. /**
  308. * Update the user password
  309. *
  310. * @return void
  311. * @param BackendUser $user An instance of BackendUser.
  312. * @param string $password The new password for the user.
  313. */
  314. public static function updatePassword(BackendUser $user, $password)
  315. {
  316. // fetch user info
  317. $userId = $user->getUserId();
  318. $key = $user->getSetting('password_key');
  319. // update user
  320. BackendModel::getDB(true)->update('users', array('password' => BackendAuthentication::getEncryptedString((string) $password, $key)), 'id = ?', $userId);
  321. // remove the user settings linked to the resetting of passwords
  322. self::deleteResetPasswordSettings($userId);
  323. }
  324. }
  325. ?>