PageRenderTime 99ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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