PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/database/table/user.php

https://github.com/rietn/minima
PHP | 414 lines | 248 code | 57 blank | 109 comment | 57 complexity | 139337c298d5691d60ebf07d464bb1da MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: user.php 20228 2011-01-10 00:52:54Z eddieajau $
  4. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. */
  7. defined('JPATH_BASE') or die;
  8. /**
  9. * Users table
  10. *
  11. * @package Joomla.Framework
  12. * @subpackage Table
  13. * @since 1.0
  14. */
  15. class JTableUser extends JTable
  16. {
  17. /**
  18. * Associative array of user names => group ids
  19. *
  20. * @access public
  21. * @since 1.6
  22. * @var array
  23. */
  24. var $groups;
  25. /**
  26. * @param database A database connector object
  27. */
  28. function __construct(&$db)
  29. {
  30. parent::__construct('#__users', 'id', $db);
  31. // Initialise.
  32. $this->id = 0;
  33. $this->sendEmail = 0;
  34. }
  35. /**
  36. * Method to load a user, user groups, and any other necessary data
  37. * from the database so that it can be bound to the user object.
  38. *
  39. * @access public
  40. * @param integer $userId An optional user id.
  41. * @return boolean True on success, false on failure.
  42. * @since 1.0
  43. */
  44. function load($userId = null, $reset = true)
  45. {
  46. // Get the id to load.
  47. if ($userId !== null) {
  48. $this->id = $userId;
  49. } else {
  50. $userId = $this->id;
  51. }
  52. // Check for a valid id to load.
  53. if ($userId === null) {
  54. return false;
  55. }
  56. // Reset the table.
  57. $this->reset();
  58. // Load the user data.
  59. $this->_db->setQuery(
  60. 'SELECT *' .
  61. ' FROM #__users' .
  62. ' WHERE id = '.(int) $userId
  63. );
  64. $data = (array) $this->_db->loadAssoc();
  65. // Check for an error message.
  66. if ($this->_db->getErrorNum()) {
  67. $this->setError($this->_db->getErrorMsg());
  68. return false;
  69. }
  70. if(!count($data))
  71. {
  72. return false;
  73. }
  74. // Bind the data to the table.
  75. $return = $this->bind($data);
  76. if ($return !== false)
  77. {
  78. // Load the user groups.
  79. $this->_db->setQuery(
  80. 'SELECT g.id, g.title' .
  81. ' FROM #__usergroups AS g' .
  82. ' JOIN #__user_usergroup_map AS m ON m.group_id = g.id' .
  83. ' WHERE m.user_id = '.(int) $userId
  84. );
  85. // Add the groups to the user data.
  86. $this->groups = $this->_db->loadAssocList('title','id');
  87. // Check for an error message.
  88. if ($this->_db->getErrorNum()) {
  89. $this->setError($this->_db->getErrorMsg());
  90. return false;
  91. }
  92. }
  93. return $return;
  94. }
  95. /**
  96. * Method to bind the user, user groups, and any other necessary data.
  97. *
  98. * @access public
  99. * @param array $array The data to bind.
  100. * @param mixed $ignore An array or space separated list of fields to ignore.
  101. * @return boolean True on success, false on failure.
  102. * @since 1.0
  103. */
  104. function bind($array, $ignore = '')
  105. {
  106. if (key_exists('params', $array) && is_array($array['params'])) {
  107. $registry = new JRegistry();
  108. $registry->loadArray($array['params']);
  109. $array['params'] = (string)$registry;
  110. }
  111. // Attempt to bind the data.
  112. $return = parent::bind($array, $ignore);
  113. // Load the real group data based on the bound ids.
  114. if ($return && !empty($this->groups))
  115. {
  116. // Set the group ids.
  117. JArrayHelper::toInteger($this->groups);
  118. // Get the titles for the user groups.
  119. $this->_db->setQuery(
  120. 'SELECT `id`, `title`' .
  121. ' FROM `#__usergroups`' .
  122. ' WHERE `id` = '.implode(' OR `id` = ', $this->groups)
  123. );
  124. // Set the titles for the user groups.
  125. $this->groups = $this->_db->loadAssocList('title','id');
  126. // Check for a database error.
  127. if ($this->_db->getErrorNum()) {
  128. $this->setError($this->_db->getErrorMsg());
  129. return false;
  130. }
  131. }
  132. return $return;
  133. }
  134. /**
  135. * Validation and filtering
  136. *
  137. * @return boolean True is satisfactory
  138. */
  139. function check()
  140. {
  141. jimport('joomla.mail.helper');
  142. // Validate user information
  143. if (trim($this->name) == '') {
  144. $this->setError(JText::_('JLIB_DATABASE_ERROR_PLEASE_ENTER_YOUR_NAME'));
  145. return false;
  146. }
  147. if (trim($this->username) == '') {
  148. $this->setError(JText::_('JLIB_DATABASE_ERROR_PLEASE_ENTER_A_USER_NAME'));
  149. return false;
  150. }
  151. if (preg_match( "#[<>\"'%;()&]#i", $this->username) || strlen(utf8_decode($this->username )) < 2) {
  152. $this->setError( JText::sprintf( 'JLIB_DATABASE_ERROR_VALID_AZ09', 2 ));
  153. return false;
  154. }
  155. if ((trim($this->email) == "") || ! JMailHelper::isEmailAddress($this->email)) {
  156. $this->setError(JText::_('JLIB_DATABASE_ERROR_VALID_MAIL'));
  157. return false;
  158. }
  159. // Set the registration timestamp
  160. if ($this->registerDate == null || $this->registerDate == '0000-00-00 00:00:00' ) {
  161. $this->registerDate = JFactory::getDate()->toMySQL();
  162. }
  163. // check for existing username
  164. $query = 'SELECT id'
  165. . ' FROM #__users '
  166. . ' WHERE username = ' . $this->_db->Quote($this->username)
  167. . ' AND id != '. (int) $this->id;
  168. ;
  169. $this->_db->setQuery($query);
  170. $xid = intval($this->_db->loadResult());
  171. if ($xid && $xid != intval($this->id)) {
  172. $this->setError( JText::_('JLIB_DATABASE_ERROR_USERNAME_INUSE'));
  173. return false;
  174. }
  175. // check for existing email
  176. $query = 'SELECT id'
  177. . ' FROM #__users '
  178. . ' WHERE email = '. $this->_db->Quote($this->email)
  179. . ' AND id != '. (int) $this->id
  180. ;
  181. $this->_db->setQuery($query);
  182. $xid = intval($this->_db->loadResult());
  183. if ($xid && $xid != intval($this->id)) {
  184. $this->setError(JText::_('JLIB_DATABASE_ERROR_EMAIL_INUSE'));
  185. return false;
  186. }
  187. // check for root_user != username
  188. $config = JFactory::getConfig();
  189. $rootUser = $config->get('root_user');
  190. if (!is_numeric($rootUser))
  191. {
  192. $query = $this->_db->getQuery(true);
  193. $query->select('id');
  194. $query->from('#__users');
  195. $query->where('username = '.$this->_db->quote($rootUser));
  196. $this->_db->setQuery($query);
  197. $xid = intval($this->_db->loadResult());
  198. if ($rootUser==$this->username && (!$xid || $xid && $xid != intval($this->id)) || $xid && $xid == intval($this->id) && $rootUser!=$this->username) {
  199. $this->setError( JText::_('JLIB_DATABASE_ERROR_USERNAME_CANNOT_CHANGE'));
  200. return false;
  201. }
  202. }
  203. return true;
  204. }
  205. function store($updateNulls = false)
  206. {
  207. // Get the table key and key value.
  208. $k = $this->_tbl_key;
  209. $key = $this->$k;
  210. // TODO: This is a dumb way to handle the groups.
  211. // Store groups locally so as to not update directly.
  212. $groups = $this->groups;
  213. unset($this->groups);
  214. // Insert or update the object based on presence of a key value.
  215. if ($key) {
  216. // Already have a table key, update the row.
  217. $return = $this->_db->updateObject($this->_tbl, $this, $this->_tbl_key, $updateNulls);
  218. }
  219. else {
  220. // Don't have a table key, insert the row.
  221. $return = $this->_db->insertObject($this->_tbl, $this, $this->_tbl_key);
  222. }
  223. // Handle error if it exists.
  224. if (!$return)
  225. {
  226. $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_STORE_FAILED', strtolower(get_class($this)), $this->_db->getErrorMsg()));
  227. return false;
  228. }
  229. // Reset groups to the local object.
  230. $this->groups = $groups;
  231. unset($groups);
  232. // Store the group data if the user data was saved.
  233. if ($return && is_array($this->groups) && count($this->groups))
  234. {
  235. // Delete the old user group maps.
  236. $this->_db->setQuery(
  237. 'DELETE FROM `#__user_usergroup_map`' .
  238. ' WHERE `user_id` = '.(int) $this->id
  239. );
  240. $this->_db->query();
  241. // Check for a database error.
  242. if ($this->_db->getErrorNum()) {
  243. $this->setError($this->_db->getErrorMsg());
  244. return false;
  245. }
  246. // Set the new user group maps.
  247. $this->_db->setQuery(
  248. 'INSERT INTO `#__user_usergroup_map` (`user_id`, `group_id`)' .
  249. ' VALUES ('.$this->id.', '.implode('), ('.$this->id.', ', $this->groups).')'
  250. );
  251. $this->_db->query();
  252. // Check for a database error.
  253. if ($this->_db->getErrorNum()) {
  254. $this->setError($this->_db->getErrorMsg());
  255. return false;
  256. }
  257. }
  258. return true;
  259. }
  260. /**
  261. * Method to delete a user, user groups, and any other necessary
  262. * data from the database.
  263. *
  264. * @access public
  265. * @param integer $userId An optional user id.
  266. * @return boolean True on success, false on failure.
  267. * @since 1.0
  268. */
  269. function delete($userId = null)
  270. {
  271. // Set the primary key to delete.
  272. $k = $this->_tbl_key;
  273. if ($userId) {
  274. $this->$k = intval($userId);
  275. }
  276. // Delete the user.
  277. $this->_db->setQuery(
  278. 'DELETE FROM `'.$this->_tbl.'`' .
  279. ' WHERE `'.$this->_tbl_key.'` = '.(int) $this->$k
  280. );
  281. $this->_db->query();
  282. // Check for a database error.
  283. if ($this->_db->getErrorNum()) {
  284. $this->setError($this->_db->getErrorMsg());
  285. return false;
  286. }
  287. // Delete the user group maps.
  288. $this->_db->setQuery(
  289. 'DELETE FROM `#__user_usergroup_map`' .
  290. ' WHERE `user_id` = '.(int) $this->$k
  291. );
  292. $this->_db->query();
  293. // Check for a database error.
  294. if ($this->_db->getErrorNum()) {
  295. $this->setError($this->_db->getErrorMsg());
  296. return false;
  297. }
  298. /*
  299. * Clean Up Related Data.
  300. */
  301. $this->_db->setQuery(
  302. 'DELETE FROM `#__messages_cfg`' .
  303. ' WHERE `user_id` = '.(int) $this->$k
  304. );
  305. $this->_db->query();
  306. // Check for a database error.
  307. if ($this->_db->getErrorNum()) {
  308. $this->setError($this->_db->getErrorMsg());
  309. return false;
  310. }
  311. $this->_db->setQuery(
  312. 'DELETE FROM `#__messages`' .
  313. ' WHERE `user_id_to` = '.(int) $this->$k
  314. );
  315. $this->_db->query();
  316. // Check for a database error.
  317. if ($this->_db->getErrorNum()) {
  318. $this->setError($this->_db->getErrorMsg());
  319. return false;
  320. }
  321. return true;
  322. }
  323. /**
  324. * Updates last visit time of user
  325. *
  326. * @param int The timestamp, defaults to 'now'
  327. * @return boolean False if an error occurs
  328. */
  329. function setLastVisit($timeStamp = null, $userId = null)
  330. {
  331. // Check for User ID
  332. if (is_null($userId))
  333. {
  334. if (isset($this)) {
  335. $userId = $this->id;
  336. } else {
  337. // do not translate
  338. jexit(JText::_('JLIB_DATABASE_ERROR_SETLASTVISIT'));
  339. }
  340. }
  341. // If no timestamp value is passed to functon, than current time is used.
  342. $date = JFactory::getDate($timeStamp);
  343. // Update the database row for the user.
  344. $this->_db->setQuery(
  345. 'UPDATE `'.$this->_tbl.'`' .
  346. ' SET `lastvisitDate` = '.$this->_db->Quote($date->toMySQL()) .
  347. ' WHERE `id` = '.(int) $userId
  348. );
  349. $this->_db->query();
  350. // Check for a database error.
  351. if ($this->_db->getErrorNum()) {
  352. $this->setError($this->_db->getErrorMsg());
  353. return false;
  354. }
  355. return true;
  356. }
  357. }