4 # Axis--UserFactory.php 5 # An Meta-Object for Handling User Information 7 # Copyright 2003-2012 Axis Data 8 # This code is free software that can be used or redistributed under the 9 # terms of Version 2 of the GNU General Public License, as published by the 10 # Free Software Foundation (http://www.fsf.org). 12 # Author: Edward Almasy (ealmasy@axisdata.com) 14 # Part of the AxisPHP library v1.2.4 15 # For more information see http://www.axisdata.com/AxisPHP/ 21 # ---- PUBLIC INTERFACE -------------------------------------------------- 28 # create database connection 31 # figure out user class name 32 $this->UserClassName = preg_replace(
33 '/Factory$/',
'', get_called_class());
49 $UserName, $Password, $PasswordAgain, $EMail, $EMailAgain,
50 $IgnoreErrorCodes = NULL)
52 # check incoming values 54 $UserName, $Password, $PasswordAgain, $EMail, $EMailAgain);
56 # discard any errors we are supposed to ignore 57 if ($IgnoreErrorCodes)
59 $ErrorCodes = array_diff($ErrorCodes, $IgnoreErrorCodes);
62 # if error found in incoming values return error codes to caller 63 if (count($ErrorCodes)) {
return $ErrorCodes; }
65 # add user to database 66 $UserClass = $this->UserClassName;
67 $UserName = $UserClass::NormalizeUserName($UserName);
68 $this->DB->Query(
"INSERT INTO APUsers" 69 .
" (UserName, CreationDate)" 70 .
" VALUES ('".addslashes($UserName).
"', NOW())");
72 # create new user object 73 $UserId = $this->DB->LastInsertId();
74 $User =
new User($this->DB, (
int)$UserId);
76 # if new user object creation failed return error code to caller 77 if ($User->Status() !=
U_OKAY) {
return array($User->Status()); }
79 # set password and e-mail address 80 $User->SetPassword($Password);
81 $User->Set(
"EMail", trim($EMail));
83 # return new user object to caller 97 $UserName, $Password, $PasswordAgain, $EMail, $EMailAgain)
99 $UserClass = $this->UserClassName;
101 # normalize incoming values 102 $UserName = $UserClass::NormalizeUserName($UserName);
103 $Password = $UserClass::NormalizePassword($Password);
104 $PasswordAgain = $UserClass::NormalizePassword($PasswordAgain);
105 $EMail = $UserClass::NormalizeEMailAddress($EMail);
106 $EMailAgain = $UserClass::NormalizeEMailAddress($EMailAgain);
108 # start off assuming success 109 $ErrorCodes = array();
111 # check that provided username is valid 112 if (strlen($UserName) == 0)
116 elseif (!$UserClass::IsValidUserName($UserName))
125 # check that email is not already in use 131 # check for password problems 132 $FoundOtherPasswordError = FALSE;
133 $PasswordErrors = $UserClass::CheckPasswordForErrors(
134 $Password, $UserName, $EMail);
136 # if there were problems, merge those in to our error list 137 if (count($PasswordErrors))
139 $ErrorCodes = array_merge($ErrorCodes, $PasswordErrors);
140 $FoundOtherPasswordError = TRUE;
143 # check that PasswordAgain was provided 144 if (strlen($PasswordAgain) == 0)
147 $FoundOtherPasswordError = TRUE;
149 # and that PasswordAgain matches Password 150 elseif ($Password != $PasswordAgain)
155 # check that provided email is valid 156 $FoundOtherEMailError = FALSE;
157 if (strlen($EMail) == 0)
160 $FoundOtherEMailError = TRUE;
162 elseif (!$UserClass::IsValidLookingEMailAddress($EMail))
165 $FoundOtherEMailError = TRUE;
168 if (strlen($EMailAgain) == 0)
171 $FoundOtherEMailError = TRUE;
173 elseif (!$UserClass::IsValidLookingEMailAddress($EMailAgain))
176 $FoundOtherEMailError = TRUE;
179 if ($FoundOtherEMailError == FALSE &&
180 $EMail != $EMailAgain)
196 return $this->DB->Query(
"SELECT COUNT(*) AS UserCount FROM APUsers" 197 .($Condition ?
" WHERE ".$Condition :
""),
"UserCount");
216 $this->DB->Query(
"SELECT UserId FROM APUsers");
217 return $this->DB->FetchColumn(
"UserId");
228 # query IDs of logged-in users from database 229 $LoggedInCutoffTime = date(
"Y-m-d H:i:s",
230 time() - ($InactivityTimeout * 60));
231 $this->DB->Query(
"SELECT UserId FROM APUsers" 232 .
" WHERE LastActiveDate > '".$LoggedInCutoffTime.
"'" 233 .
" AND LoggedIn != '0'");
234 $UserIds = $this->DB->FetchColumn(
"UserId");
236 # load array of logged in users 237 $ReturnValue = array();
238 foreach ($UserIds as $Id)
240 $ReturnValue[$Id] =
new User(intval($Id));
243 # return array of user data to caller 256 # get users recently logged in during the last 24 hours if no date given 259 $Date = date(
"Y-m-d H:i:s", time() - (24 * 60 * 60));
264 $Date = date(
"Y-m-d H:i:s", strtotime($Since));
267 # query for the users who were logged in since the given date 268 $this->DB->Query(
"SELECT UserId FROM APUsers" 269 .
" WHERE LastActiveDate > '".$Date.
"'" 270 .
" AND LoggedIn != '1'" 271 .
" ORDER BY LastActiveDate DESC" 272 .
" LIMIT ".intval($Limit));
273 $UserIds = $this->DB->FetchColumn(
"UserId");
275 $ReturnValue = array();
276 foreach ($UserIds as $Id)
278 $ReturnValue[$Id] =
new User(intval($Id));
281 # return array of user data to caller 293 # retrieve privileges 294 $Args = func_get_args();
295 if (is_array(reset($Args))) { $Args = reset($Args); }
297 foreach ($Args as $Arg)
301 $Privs = array_merge($Privs, $Args);
309 # start with query string that will return all users 310 $QueryString =
"SELECT DISTINCT APUsers.UserId, UserName FROM APUsers" 311 .(count($Privs) ?
", APUserPrivileges" :
"");
313 # for each specified privilege 314 foreach ($Privs as $Index => $Priv)
316 # add condition to query string 317 $QueryString .= ($Index == 0) ?
" WHERE (" :
" OR";
318 $QueryString .=
" APUserPrivileges.Privilege = ".$Priv;
321 # close privilege condition in query string and add user ID condition 322 $QueryString.= count($Privs)
323 ?
") AND APUsers.UserId = APUserPrivileges.UserId" :
"";
325 # add sort by user name to query string 326 $QueryString .=
" ORDER BY UserName ASC";
329 $this->DB->Query($QueryString);
331 # copy query result into user info array 332 $Users = $this->DB->FetchColumn(
"UserName",
"UserId");
334 # return array of users to caller 350 public function FindUsers($SearchString, $FieldName =
"UserName",
353 # retrieve matching user IDs 357 # create user objects 359 foreach ($UserNames as $UserId => $UserName)
361 $Users[$UserId] =
new User($this->DB, intval($UserId));
364 # return array of user objects to caller 384 $IdExclusions = array(), $ValueExclusions = array())
386 $UserClass = $this->UserClassName;
388 # make sure the provided field name is valid 389 if (!$this->DB->FieldExists(
"APUsers", $FieldName))
392 "There is no ".$FieldName.
" Field in the APUsers table");
395 # construct a database query 396 $QueryString =
"SELECT UserId, UserName FROM APUsers WHERE " 397 .$FieldName.
" = '".addslashes($SearchString).
"'";
399 # add each ID exclusion 400 foreach ($IdExclusions as $IdExclusion)
402 $QueryString .=
" AND ".$this->ItemIdFieldName.
" != '" 403 .addslashes($IdExclusion).
"' ";
406 # add each value exclusion 407 foreach ($ValueExclusions as $ValueExclusion)
409 $QueryString .=
" AND ".$this->ItemNameFieldName.
" != '" 410 .addslashes($ValueExclusion).
"' ";
413 $QueryString .=
" ORDER BY ".$SortFieldName
414 .
" LIMIT ".$Offset.
", ".$Count;
416 # retrieve matching user IDs 417 $this->DB->Query($QueryString);
418 $UserNames = $this->DB->FetchColumn(
"UserName",
"UserId");
420 # return names/IDs to caller 441 $ResultsStartAt = 0, $ReturnNumber = NULL)
443 # start with empty array (to prevent array errors) 444 $ReturnValue = array();
446 # if empty search string supplied, return nothing 447 $TrimmedSearchString = trim($SearchString);
448 if (empty($TrimmedSearchString))
453 # make sure ordering is done by user name if not specified 456 # begin constructing the query 457 $Query =
"SELECT * FROM APUsers";
458 $QueryOrderBy =
" ORDER BY $SortFieldName";
459 $QueryLimit = empty($ReturnNumber) ?
"" :
" LIMIT $ResultsStartAt, $ReturnNumber";
461 # the Criteria Query will be used to get the total number of results without the 463 $CriteriaQuery = $Query;
465 # if specific field comparison requested 466 if (!empty($FieldName))
468 # append queries with criteria 469 $Query .=
" WHERE ".$FieldName.
" REGEXP '".addslashes($SearchString).
"'";
470 $CriteriaQuery = $Query;
473 # optimize for returning all users 474 else if ($SearchString ==
".*.")
476 # set field name to username - this would be the first field 477 # returned by a field to field search using the above RegExp 478 $FieldName =
"UserName";
481 # add order by and limit to query for optimizing 482 $Query .= $QueryOrderBy.$QueryLimit;
485 $this->DB->Query($Query);
487 # ...and process query return 488 while ($Record = $this->DB->FetchRow())
490 # if specific field or all users requested 491 if (!empty($FieldName))
493 # add user to return array 494 $ReturnValue[$Record[
"UserId"]] = $Record;
496 # add matching search field to return array 497 $ReturnValue[$Record[
"UserId"]][
"APMatchingField"] = $FieldName;
502 # for each user data field 503 foreach ($Record as $FName => $FValue)
505 # if search string appears in data field 506 if (strpos($Record[$FName], $SearchString) !== FALSE)
508 # add user to return array 509 $ReturnValue[$Record[
"UserId"]] = $Record;
511 # add matching search field to return array 512 $ReturnValue[$Record[
"UserId"]][
"APMatchingField"] = $FName;
518 # add matching user count 519 $this->DB->Query($CriteriaQuery);
520 $this->MatchingUserCount = $this->DB->NumRowsSelected();
522 # return array of matching users to caller 533 if (!is_numeric($UserId))
537 $UserCount = $this->DB->Query(
"SELECT COUNT(*) AS UserCount" 538 .
" FROM APUsers WHERE UserId = ".intval($UserId),
"UserCount");
539 return ($UserCount > 0) ? TRUE : FALSE;
549 # normalize user name 550 $UserClass = $this->UserClassName;
551 $UserName = $UserClass::NormalizeUserName($UserName);
553 # check whether user name is already in use 554 $NameCount = $this->DB->Query(
555 "SELECT COUNT(*) AS NameCount FROM APUsers" 556 .
" WHERE UserName = '".addslashes($UserName).
"'",
559 # report to caller whether name exists 560 return ($NameCount > 0) ? TRUE : FALSE;
571 $UserClass = $this->UserClassName;
572 $UserName = $UserClass::NormalizeEMailAddress($Address);
574 # check whether address is already in use 575 $AddressCount = $this->DB->Query(
576 "SELECT COUNT(*) AS AddressCount FROM APUsers" 577 .
" WHERE EMail = '".addslashes($Address).
"'",
580 # report to caller whether address is in use 581 return ($AddressCount > 0) ? TRUE : FALSE;
591 $UserClass = $this->UserClassName;
593 # assume no users will be found 596 # fetch the newest users 597 $this->DB->Query(
"SELECT *" 599 .
" ORDER BY CreationDate DESC" 600 .
" LIMIT ".intval($Limit));
601 $UserIds = $this->DB->FetchColumn(
"UserId");
603 # for each user id found 604 foreach ($UserIds as $UserId)
606 $Users[$UserId] =
new $UserClass($UserId);
609 # return the newest users 613 # ---- PRIVATE INTERFACE ------------------------------------------------- 618 private $UserClassName;
const U_ILLEGALEMAILAGAIN
EMailAddressIsInUse($Address)
Check whether e-mail address currently has account associated with it.
SQL database abstraction object with smart query caching.
CreateNewUser($UserName, $Password, $PasswordAgain, $EMail, $EMailAgain, $IgnoreErrorCodes=NULL)
Create new user.
UserNameExists($UserName)
Check whether user name currently exists.
GetUserIds()
Get IDs for all users.
GetRecentlyLoggedInUsers($Since=NULL, $Limit=10)
Get users recently logged in.
const U_EMPTYPASSWORDAGAIN
GetUsersWithPrivileges()
Return array of user names who have the specified privileges.
GetNewestUsers($Limit=5)
Get the users sorted by when they signed up, starting with those who signed up most recently...
GetMatchingUsers($SearchString, $FieldName=NULL, $SortFieldName="UserName", $ResultsStartAt=0, $ReturnNumber=NULL)
Return array of users who have values matching search string (in specific field if requested)...
FindUsers($SearchString, $FieldName="UserName", $SortFieldName="UserName", $Offset=0, $Count=9999999)
Get users who have values matching specified string in specified field.
const U_DUPLICATEUSERNAME
TestNewUserValues($UserName, $Password, $PasswordAgain, $EMail, $EMailAgain)
Test new user values (usually used before creating new user).
UserExists($UserId)
Check whether user currently exists with specified ID.
GetUserCount($Condition=NULL)
Return number of users in the system.
GetLoggedInUsers($InactivityTimeout=60)
Get users who are currently logged in (i.e.
__construct()
Object constructor.
GetMatchingUserCount()
Get total number of user that matched last GetMatchingUsers() call.
FindUserNames($SearchString, $FieldName="UserName", $SortFieldName="UserName", $Offset=0, $Count=9999999, $IdExclusions=array(), $ValueExclusions=array())
Get users who have values matching specified string in specified field.
const U_PASSWORDSDONTMATCH