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

https://github.com/DoFken/forkcms · PHP · 415 lines · 199 code · 54 blank · 162 comment · 4 complexity · cbcb3a1402b104cff639229e29215d29 MD5 · raw file

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