aliroUser.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /*******************************************************************************
00004  * Aliro - the modern, accessible content management system
00005  *
00006  * Aliro is open source software, free to use, and licensed under GPL.
00007  * You can find the full licence at http://www.gnu.org/copyleft/gpl.html GNU/GPL
00008  *
00009  * The author freely draws attention to the fact that Aliro derives from Mambo,
00010  * software that is controlled by the Mambo Foundation.  However, this section
00011  * of code is totally new.  If it should contain any fragments that are similar
00012  * to Mambo, please bear in mind (1) there are only so many ways to do things
00013  * and (2) the author of Aliro is also the author and copyright owner for large
00014  * parts of Mambo 4.6.
00015  *
00016  * Tribute should be paid to all the developers who took Mambo to the stage
00017  * it had reached at the time Aliro was created.  It is a feature rich system
00018  * that contains a good deal of innovation.
00019  *
00020  * Your attention is also drawn to the fact that Aliro relies on other items of
00021  * open source software, which is very much in the spirit of open source.  Aliro
00022  * wishes to give credit to those items of code.  Please refer to
00023  * http://aliro.org/credits for details.  The credits are not included within
00024  * the Aliro package simply to avoid providing a marker that allows hackers to
00025  * identify the system.
00026  *
00027  * Copyright in this code is strictly reserved by its author, Martin Brampton.
00028  * If it seems appropriate, the copyright will be vested in the Aliro Organisation
00029  * at a suitable time.
00030  *
00031  * Copyright (c) 2007 Martin Brampton
00032  *
00033  * http://aliro.org
00034  *
00035  * counterpoint@aliro.org
00036  *
00037  * mosUser has kept its old name because of the extent that it has become embedded
00038  * in the system, including the new Role Based Access Control mechanism, and also
00039  * because aliroUser exists with a different purpose (see below).  It is
00040  * the user object that knows the basics about an Aliro user.  It could well be
00041  * extended to have more functionality and to know more.  Right now it is rather
00042  * feeble.
00043  *
00044  * aliroUser is a singleton class that embeds the mosUser object referring to
00045  * the currently active user - that is to say the person on the browser whose
00046  * request we are handling.  Any class wanting to obtain access to the current
00047  * user can get the instance of aliroUser.
00048  *
00049  */
00050 
00051 
00058 class mosUser extends aliroDatabaseRow {
00059     protected $DBclass = 'aliroDatabase';
00060     protected $tableName = '#__users';
00061     protected $rowKey = 'id';
00062 
00066     protected function getSessionData() {
00067         // Avoid using aliroRequest here - this will run before it is available
00068         $prefix = criticalInfo::getInstance()->isAdmin ? 'admin' : 'user';
00069         // Get session to ensure initialisation - don't actually need it - but do need session started
00070         // This shouldn't be necessary, but left in just to be sure
00071         aliroSessionFactory::getSession();
00072         $this->id = isset($_SESSION["aliro_{$prefix}id"]) ? (int) $_SESSION["aliro_{$prefix}id"] : 0;
00073         $this->name = isset($_SESSION["aliro_{$prefix}name"]) ? $_SESSION["aliro_{$prefix}name"] : '';
00074         $this->username = isset($_SESSION["aliro_{$prefix}username"]) ? $_SESSION["aliro_{$prefix}username"] : '';
00075         $this->email = isset($_SESSION["aliro_{$prefix}email"]) ? $_SESSION["aliro_{$prefix}email"] : '';
00076         $this->sendEmail = isset($_SESSION["aliro_{$prefix}sendEmail"]) ? $_SESSION["aliro_{$prefix}sendEmail"] : '';
00077         $this->usertype = isset($_SESSION["aliro_{$prefix}type"]) ? $_SESSION["aliro_{$prefix}type"] : '';
00078         $this->gid = isset($_SESSION["aliro_{$prefix}gid"]) ? (int) $_SESSION["aliro_{$prefix}gid"] : 0;
00079     }
00080 
00081     // Parameter will be ignored - required for consistency with parent class
00082     public function userStore($password='', $activation='') {
00083         $salt = aliroAdminAuthenticator::getInstance()->makeSalt();
00084         if ($this->id) {
00085             $ret = $this->update();
00086             if ($password) {
00087                 $database = aliroCoreDatabase::getInstance();
00088                 $database->doSQL("UPDATE #__core_users SET salt = IF(salt='', '$salt', salt), password = MD5(CONCAT(salt, '$password')) WHERE id = $this->id");
00089             }
00090         }
00091         else {
00092             $database = aliroCoreDatabase::getInstance();
00093             $database->doSQL("INSERT INTO #__core_users (password, salt, activation) VALUES (MD5(CONCAT('$salt', '$password')), '$salt', '$activation')");
00094             $this->id = $database->insertid();
00095             $ret = $this->insert();
00096         }
00097         if ($ret) return true;
00098         $this->_error = T_('mosUser::store failed');
00099         return false;
00100     }
00101 
00102     public function delete($oid=null) {
00103         if ($oid) $this->id = intval( $oid );
00104         aliroCoreDatabase::getInstance()->doSQL("DELETE FROM `#__core_users` WHERE `id` = '$this->id'");
00105         $database = aliroDatabase::getInstance();
00106         $database->doSQL("DELETE FROM `#__users` WHERE `id` = '$this->id'");
00107         // cleanup related data from private messaging
00108         $database->setQuery( "DELETE FROM `#__messages_cfg` WHERE `user_id`='$this->id'" );
00109         $database->query();
00110         $database->setQuery( "DELETE FROM `#__messages` WHERE `user_id_to`='{$this->id}'" );
00111         $database->query();
00112         return true;
00113     }
00114 
00115     public function check() {
00116         if ($this->name == '') $error = T_('Please enter your name');
00117         elseif ($this->username == '') $error = T_('Please enter a user name');
00118         elseif (strlen($this->username) < 3 OR preg_match("/[\\<\\>\\\"\\'\\%\\;\\(\\)\\&\\+\\-]/", $this->username)) $error = sprintf(T_('Please enter a valid %s.  No spaces, more than %d characters and containing only the characters 0-9,a-z, or A-Z'), T_('Username'), 2 );
00119         elseif (($this->email == '') OR preg_match("/[\w\.\-]+@\w+[\w\.\-]*?\.\w{1,4}/", $this->email ) == 0) $error = T_('Please enter a valid e-mail address');
00120         else {
00121             // check for existing username
00122             $database = aliroDatabase::getInstance();
00123             $database->setQuery( "SELECT COUNT(id) FROM #__users WHERE LOWER(username)=LOWER('$this->username') AND id!='$this->id'");
00124             if ($database->loadResult()) $error = T_('This username/password is already in use. Please try another.');
00125             elseif (aliroCore::get('mosConfig_uniquemail')) {
00126                 // check for existing email
00127                 $database->setQuery( "SELECT COUNT(id) FROM #__users WHERE email='$this->email' AND id!='$this->id'");
00128                 if ($database->loadResult()) $error = T_('This e-mail is already registered. If you forgot the password click on "Password Reminder" and new password will be sent to you.');
00129             }
00130         }
00131         if (isset($error)) {
00132             aliroRequest::getInstance()->setErrorMessage($error, _ALIRO_ERROR_FATAL);
00133             return false;
00134         }
00135         return true;
00136     }
00137 
00138 }
00139 
00140 class aliroUser extends mosUser {
00141     private static $instance = __CLASS__;
00142 
00143     private function __construct () {
00144         $this->getSessionData();
00145     }
00146 
00147     private function __clone () {
00148         // Enforce singleton
00149     }
00150 
00151     public static function getInstance () {
00152         return is_object(self::$instance) ? self::$instance : (self::$instance = new self::$instance());
00153     }
00154 
00155     public function reset () {
00156         $this->getSessionData();
00157     }
00158 }

Generated on Wed May 14 13:01:56 2008 for ALIRO by  doxygen 1.5.5