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 45 $UserName, $Password, $PasswordAgain, $EMail, $EMailAgain,
46 $IgnoreErrorCodes = NULL)
48 # check incoming values 50 $UserName, $Password, $PasswordAgain, $EMail, $EMailAgain);
52 # discard any errors we are supposed to ignore 53 if ($IgnoreErrorCodes)
55 $ErrorCodes = array_diff($ErrorCodes, $IgnoreErrorCodes);
58 # if error found in incoming values return error codes to caller 59 if (count($ErrorCodes)) {
return $ErrorCodes; }
61 # add user to database 63 $this->DB->Query(
"INSERT INTO APUsers" 64 .
" (UserName, CreationDate)" 65 .
" VALUES ('".addslashes($UserName).
"', NOW())");
67 # create new user object 68 $UserId = $this->DB->LastInsertId();
69 $User =
new User($this->DB, (
int)$UserId);
71 # if new user object creation failed return error code to caller 72 if ($User->Status() !=
U_OKAY) {
return array($User->Status()); }
74 # set password and e-mail address 75 $User->SetPassword($Password);
76 $User->Set(
"EMail", $EMail);
78 # return new user object to caller 92 $UserName, $Password, $PasswordAgain, $EMail, $EMailAgain)
94 $ErrorCodes = array();
105 $FoundOtherPasswordError = FALSE;
109 $FoundOtherPasswordError = TRUE;
114 $FoundOtherPasswordError = TRUE;
120 $FoundOtherPasswordError = TRUE;
125 $FoundOtherPasswordError = TRUE;
128 if ($FoundOtherPasswordError == FALSE)
137 $FoundOtherEMailError = FALSE;
141 $FoundOtherEMailError = TRUE;
146 $FoundOtherEMailError = TRUE;
152 $FoundOtherEMailError = TRUE;
157 $FoundOtherEMailError = TRUE;
160 if ($FoundOtherEMailError == FALSE)
180 return $this->DB->Query(
"SELECT COUNT(*) AS UserCount FROM APUsers" 181 .($Condition ?
" WHERE ".$Condition :
""),
"UserCount");
202 # query IDs of logged-in users from database 203 $LoggedInCutoffTime = date(
"Y-m-d H:i:s",
204 time() - ($InactivityTimeout * 60));
205 $this->DB->Query(
"SELECT UserId FROM APUsers" 206 .
" WHERE LastActiveDate > '".$LoggedInCutoffTime.
"'" 207 .
" AND LoggedIn != '0'");
208 $UserIds = $this->DB->FetchColumn(
"UserId");
210 # load array of logged in users 211 $ReturnValue = array();
212 foreach ($UserIds as $Id)
214 $ReturnValue[$Id] =
new User(intval($Id));
217 # return array of user data to caller 230 # get users recently logged in during the last 24 hours if no date given 233 $Date = date(
"Y-m-d H:i:s", time() - (24 * 60 * 60));
238 $Date = date(
"Y-m-d H:i:s", strtotime($Since));
241 # query for the users who were logged in since the given date 242 $this->DB->Query(
"SELECT UserId FROM APUsers" 243 .
" WHERE LastActiveDate > '".$Date.
"'" 244 .
" AND LoggedIn != '1'" 245 .
" ORDER BY LastActiveDate DESC" 246 .
" LIMIT ".intval($Limit));
247 $UserIds = $this->DB->FetchColumn(
"UserId");
249 $ReturnValue = array();
250 foreach ($UserIds as $Id)
252 $ReturnValue[$Id] =
new User(intval($Id));
255 # return array of user data to caller 267 # retrieve privileges 268 $Args = func_get_args();
269 if (is_array(reset($Args))) { $Args = reset($Args); }
271 foreach ($Args as $Arg)
275 $Privs = array_merge($Privs, $Args);
283 # start with query string that will return all users 284 $QueryString =
"SELECT DISTINCT APUsers.UserId, UserName FROM APUsers" 285 .(count($Privs) ?
", APUserPrivileges" :
"");
287 # for each specified privilege 288 foreach ($Privs as $Index => $Priv)
290 # add condition to query string 291 $QueryString .= ($Index == 0) ?
" WHERE (" :
" OR";
292 $QueryString .=
" APUserPrivileges.Privilege = ".$Priv;
295 # close privilege condition in query string and add user ID condition 296 $QueryString.= count($Privs)
297 ?
") AND APUsers.UserId = APUserPrivileges.UserId" :
"";
299 # add sort by user name to query string 300 $QueryString .=
" ORDER BY UserName ASC";
303 $this->DB->Query($QueryString);
305 # copy query result into user info array 306 $Users = $this->DB->FetchColumn(
"UserName",
"UserId");
308 # return array of users to caller 324 public function FindUsers($SearchString, $FieldName =
"UserName",
327 # retrieve matching user IDs 331 # create user objects 333 foreach ($UserNames as $UserId => $UserName)
335 $Users[$UserId] =
new User($this->DB, intval($UserId));
338 # return array of user objects to caller 358 $IdExclusions = array(), $ValueExclusions = array())
360 # Construct a database query: 361 $QueryString =
"SELECT UserId, UserName FROM APUsers WHERE";
363 # If the search string is a valid username which is shorter than the 364 # minimum word length indexed by the FTS, just do a normal 365 # equality test instead of using the index. 366 # Otherwise, FTS away. 367 $MinWordLen = $this->DB->Query(
368 "SHOW VARIABLES WHERE variable_name='ft_min_word_len'",
"Value");
370 strlen($SearchString) < $MinWordLen )
372 $QueryString .=
" UserName='".addslashes($SearchString).
"'";
376 # massage search string to use AND logic 377 $Words = preg_split(
"/[\s]+/", trim($SearchString));
378 $NewSearchString =
"";
379 $InQuotedString = FALSE;
380 foreach ($Words as $Word)
382 if ($InQuotedString == FALSE) { $NewSearchString .=
"+"; }
383 if (preg_match(
"/^\"/", $Word)) { $InQuotedString = TRUE; }
384 if (preg_match(
"/\"$/", $Word)) { $InQuotedString = FALSE; }
385 $NewSearchString .= $Word.
" ";
387 $QueryString .=
" MATCH (".$FieldName.
")" 388 .
" AGAINST ('".addslashes(trim($NewSearchString)).
"'" 389 .
" IN BOOLEAN MODE)";
392 # add each ID exclusion 393 foreach ($IdExclusions as $IdExclusion)
395 $QueryString .=
" AND ".$this->ItemIdFieldName.
" != '" 396 .addslashes($IdExclusion).
"' ";
399 # add each value exclusion 400 foreach ($ValueExclusions as $ValueExclusion)
402 $QueryString .=
" AND ".$this->ItemNameFieldName.
" != '" 403 .addslashes($ValueExclusion).
"' ";
406 $QueryString .=
" ORDER BY ".$SortFieldName
407 .
" LIMIT ".$Offset.
", ".$Count;
409 # retrieve matching user IDs 410 $this->DB->Query($QueryString);
411 $UserNames = $this->DB->FetchColumn(
"UserName",
"UserId");
413 # return names/IDs to caller 434 $ResultsStartAt = 0, $ReturnNumber = NULL)
436 # start with empty array (to prevent array errors) 437 $ReturnValue = array();
439 # if empty search string supplied, return nothing 440 $TrimmedSearchString = trim($SearchString);
441 if (empty($TrimmedSearchString))
446 # make sure ordering is done by user name if not specified 449 # begin constructing the query 450 $Query =
"SELECT * FROM APUsers";
451 $QueryOrderBy =
" ORDER BY $SortFieldName";
452 $QueryLimit = empty($ReturnNumber) ?
"" :
" LIMIT $ResultsStartAt, $ReturnNumber";
454 # the Criteria Query will be used to get the total number of results without the 456 $CriteriaQuery = $Query;
458 # if specific field comparison requested 459 if (!empty($FieldName))
461 # append queries with criteria 462 $Query .=
" WHERE ".$FieldName.
" REGEXP '".addslashes($SearchString).
"'";
463 $CriteriaQuery = $Query;
466 # optimize for returning all users 467 else if ($SearchString ==
".*.")
469 # set field name to username - this would be the first field 470 # returned by a field to field search using the above RegExp 471 $FieldName =
"UserName";
474 # add order by and limit to query for optimizing 475 $Query .= $QueryOrderBy.$QueryLimit;
478 $this->DB->Query($Query);
480 # ...and process query return 481 while ($Record = $this->DB->FetchRow())
483 # if specific field or all users requested 484 if (!empty($FieldName))
486 # add user to return array 487 $ReturnValue[$Record[
"UserId"]] = $Record;
489 # add matching search field to return array 490 $ReturnValue[$Record[
"UserId"]][
"APMatchingField"] = $FieldName;
495 # for each user data field 496 foreach ($Record as $FName => $FValue)
498 # if search string appears in data field 499 if (strpos($Record[$FName], $SearchString) !== FALSE)
501 # add user to return array 502 $ReturnValue[$Record[
"UserId"]] = $Record;
504 # add matching search field to return array 505 $ReturnValue[$Record[
"UserId"]][
"APMatchingField"] = $FName;
511 # add matching user count 512 $this->DB->Query($CriteriaQuery);
513 $this->MatchingUserCount = $this->DB->NumRowsSelected();
515 # return array of matching users to caller 526 # normalize user name 529 # check whether user name is already in use 530 $NameCount = $this->DB->Query(
531 "SELECT COUNT(*) AS NameCount FROM APUsers" 532 .
" WHERE UserName = '".addslashes($UserName).
"'",
535 # report to caller whether name exists 536 return ($NameCount > 0) ? TRUE : FALSE;
549 # check whether address is already in use 550 $AddressCount = $this->DB->Query(
551 "SELECT COUNT(*) AS AddressCount FROM APUsers" 552 .
" WHERE EMail = '".addslashes($Address).
"'",
555 # report to caller whether address is in use 556 return ($AddressCount > 0) ? TRUE : FALSE;
566 # assume no users will be found 569 # fetch the newest users 570 $this->DB->Query(
"SELECT *" 572 .
" ORDER BY CreationDate DESC" 573 .
" LIMIT ".intval($Limit));
574 $UserIds = $this->DB->FetchColumn(
"UserId");
576 # for each user id found 577 foreach ($UserIds as $UserId)
579 $Users[$UserId] =
new SPTUser($UserId);
582 # return the newest users 586 # ---- PRIVATE INTERFACE ------------------------------------------------- static NormalizeUserName($UserName)
static IsValidLookingEMailAddress($EMail)
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.
static NormalizePassword($Password)
UserNameExists($UserName)
Check whether user name currently exists.
const U_ILLEGALPASSWORDAGAIN
static IsValidPassword($Password)
GetRecentlyLoggedInUsers($Since=NULL, $Limit=10)
Get users recently logged in.
static IsValidUserName($UserName)
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).
static NormalizeEMailAddress($EMailAddress)
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