[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/joomla/database/table/ -> user.php (source)

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


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1