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/
20 # ---- PUBLIC INTERFACE --------------------------------------------------
27 # create database connection
44 $UserName, $Password, $PasswordAgain, $EMail, $EMailAgain,
45 $IgnoreErrorCodes = NULL)
47 # check incoming values
49 $UserName, $Password, $PasswordAgain, $EMail, $EMailAgain);
51 # discard any errors we are supposed to ignore
52 if ($IgnoreErrorCodes)
54 $ErrorCodes = array_diff($ErrorCodes, $IgnoreErrorCodes);
57 # if error found in incoming values return error codes to caller
58 if (count($ErrorCodes)) {
return $ErrorCodes; }
60 # add user to database
62 $this->DB->Query(
"INSERT INTO APUsers"
63 .
" (UserName, CreationDate)"
64 .
" VALUES ('".addslashes($UserName).
"', NOW())");
66 # create new user object
67 $UserId = $this->DB->LastInsertId(
"APUsers");
68 $User =
new User($this->DB, (
int)$UserId);
70 # if new user object creation failed return error code to caller
71 if ($User->Status() !=
U_OKAY) {
return array($User->Status()); }
73 # set password and e-mail address
74 $User->SetPassword($Password);
75 $User->Set(
"EMail", $EMail);
77 # return new user object to caller
81 # test new user creation values (returns array of error codes)
83 $UserName, $Password, $PasswordAgain, $EMail, $EMailAgain)
85 $ErrorCodes = array();
96 $FoundOtherPasswordError = FALSE;
100 $FoundOtherPasswordError = TRUE;
105 $FoundOtherPasswordError = TRUE;
111 $FoundOtherPasswordError = TRUE;
116 $FoundOtherPasswordError = TRUE;
119 if ($FoundOtherPasswordError == FALSE)
128 $FoundOtherEMailError = FALSE;
132 $FoundOtherEMailError = TRUE;
137 $FoundOtherEMailError = TRUE;
143 $FoundOtherEMailError = TRUE;
148 $FoundOtherEMailError = TRUE;
151 if ($FoundOtherEMailError == FALSE)
170 return $this->DB->Query(
"SELECT COUNT(*) AS UserCount FROM APUsers"
171 .($Condition ?
" WHERE ".$Condition :
""),
"UserCount");
174 # return total number of user that matched last GetMatchingUsers call
175 # before the return size was limited
189 # query IDs of logged-in users from database
190 $LoggedInCutoffTime = date(
"Y-m-d H:i:s",
191 time() - ($InactivityTimeout * 60));
192 $this->DB->Query(
"SELECT UserId FROM APUsers"
193 .
" WHERE LastActiveDate > '".$LoggedInCutoffTime.
"'"
194 .
" AND LoggedIn != '0'");
195 $UserIds = $this->DB->FetchColumn(
"UserId");
197 # load array of logged in users
198 $ReturnValue = array();
199 foreach ($UserIds as $Id)
201 $ReturnValue[$Id] =
new User(intval($Id));
204 # return array of user data to caller
208 # return array of users recently logged in. returns 10 users by default
211 # get users recently logged in during the last 24 hours if no date given
214 $Date = date(
"Y-m-d H:i:s", time() - (24 * 60 * 60));
219 $Date = date(
"Y-m-d H:i:s", strtotime($Since));
222 # query for the users who were logged in since the given date
223 $this->DB->Query(
"SELECT UserId FROM APUsers"
224 .
" WHERE LastActiveDate > '".$Date.
"'"
225 .
" AND LoggedIn != '1'"
226 .
" ORDER BY LastActiveDate DESC"
227 .
" LIMIT ".intval($Limit));
228 $UserIds = $this->DB->FetchColumn(
"UserId");
230 $ReturnValue = array();
231 foreach ($UserIds as $Id)
233 $ReturnValue[$Id] =
new User(intval($Id));
236 # return array of user data to caller
240 # return array of user names who have the specified privileges
241 # (array index is user IDs)
244 # start with query string that will return all users
245 $Args = func_get_args();
246 $QueryString =
"SELECT DISTINCT APUsers.UserId, UserName FROM APUsers"
247 .(count($Args) ?
", APUserPrivileges" :
"");
249 # for each specified privilege
250 if (is_array(reset($Args))) { $Args = reset($Args); }
251 foreach ($Args as $Index => $Arg)
253 # add condition to query string
254 $QueryString .= ($Index == 0) ?
" WHERE (" :
" OR";
255 $QueryString .=
" APUserPrivileges.Privilege = ".$Arg;
258 # close privilege condition in query string and add user ID condition
259 $QueryString.= count($Args)
260 ?
") AND APUsers.UserId = APUserPrivileges.UserId" :
"";
262 # add sort by user name to query string
263 $QueryString .=
" ORDER BY UserName ASC";
266 $this->DB->Query($QueryString);
268 # copy query result into user info array
269 $Users = $this->DB->FetchColumn(
"UserName",
"UserId");
271 # return array of users to caller
275 # return array of user objects who have values matching search string
276 # (array indexes are user IDs)
277 function FindUsers($SearchString, $FieldName =
"UserName",
280 # retrieve matching user IDs
284 # create user objects
286 foreach ($UserNames as $UserId => $UserName)
288 $Users[$UserId] =
new User($this->DB, intval($UserId));
291 # return array of user objects to caller
295 # return array of user names/IDs who have values matching search string
296 # (array indexes are user IDs, array values are user names)
299 $IdExclusions = array(), $ValueExclusions = array())
301 # Construct a database query:
302 $QueryString =
"SELECT UserId, UserName FROM APUsers WHERE";
304 # If the search string is a valid username which is shorter than the
305 # minimum word length indexed by the FTS, just do a normal
306 # equality test instead of using the index.
307 # Otherwise, FTS away.
308 $MinWordLen = $this->DB->Query(
309 "SHOW VARIABLES WHERE variable_name='ft_min_word_len'",
"Value");
311 strlen($SearchString) < $MinWordLen )
313 $QueryString .=
" UserName='".addslashes($SearchString).
"'";
317 # massage search string to use AND logic
318 $Words = preg_split(
"/[\s]+/", trim($SearchString));
319 $NewSearchString =
"";
320 $InQuotedString = FALSE;
321 foreach ($Words as $Word)
323 if ($InQuotedString == FALSE) { $NewSearchString .=
"+"; }
324 if (preg_match(
"/^\"/", $Word)) { $InQuotedString = TRUE; }
325 if (preg_match(
"/\"$/", $Word)) { $InQuotedString = FALSE; }
326 $NewSearchString .= $Word.
" ";
328 $QueryString .=
" MATCH (".$FieldName.
")"
329 .
" AGAINST ('".addslashes(trim($NewSearchString)).
"'"
330 .
" IN BOOLEAN MODE)";
333 # add each ID exclusion
334 foreach ($IdExclusions as $IdExclusion)
336 $QueryString .=
" AND ".$this->ItemIdFieldName.
" != '"
337 .addslashes($IdExclusion).
"' ";
340 # add each value exclusion
341 foreach ($ValueExclusions as $ValueExclusion)
343 $QueryString .=
" AND ".$this->ItemNameFieldName.
" != '"
344 .addslashes($ValueExclusion).
"' ";
347 $QueryString .=
" ORDER BY ".$SortFieldName
348 .
" LIMIT ".$Offset.
", ".$Count;
350 # retrieve matching user IDs
351 $this->DB->Query($QueryString);
352 $UserNames = $this->DB->FetchColumn(
"UserName",
"UserId");
354 # return names/IDs to caller
358 # return array of users who have values matching search string (in specific field if requested)
359 # (search string respects POSIX-compatible regular expressions)
360 # optimization: $SearchString = ".*." and $FieldName = NULL will return all
361 # users ordered by $SortFieldName
364 $ResultsStartAt = 0, $ReturnNumber = NULL)
366 # start with empty array (to prevent array errors)
367 $ReturnValue = array();
369 # if empty search string supplied, return nothing
370 $TrimmedSearchString = trim($SearchString);
371 if (empty($TrimmedSearchString))
376 # make sure ordering is done by user name if not specified
379 # begin constructing the query
380 $Query =
"SELECT * FROM APUsers";
381 $QueryOrderBy =
" ORDER BY $SortFieldName";
382 $QueryLimit = empty($ReturnNumber) ?
"" :
" LIMIT $ResultsStartAt, $ReturnNumber";
384 # the Criteria Query will be used to get the total number of results without the
386 $CriteriaQuery = $Query;
388 # if specific field comparison requested
389 if (!empty($FieldName))
391 # append queries with criteria
392 $Query .=
" WHERE ".$FieldName.
" REGEXP '".addslashes($SearchString).
"'";
393 $CriteriaQuery = $Query;
396 # optimize for returning all users
397 else if ($SearchString ==
".*.")
399 # set field name to username - this would be the first field
400 # returned by a field to field search using the above RegExp
401 $FieldName =
"UserName";
404 # add order by and limit to query for optimizing
405 $Query .= $QueryOrderBy.$QueryLimit;
408 $this->DB->Query($Query);
410 # ...and process query return
411 while ($Record = $this->DB->FetchRow())
413 # if specific field or all users requested
414 if (!empty($FieldName))
416 # add user to return array
417 $ReturnValue[$Record[
"UserId"]] = $Record;
419 # add matching search field to return array
420 $ReturnValue[$Record[
"UserId"]][
"APMatchingField"] = $FieldName;
425 # for each user data field
426 foreach ($Record as $FName => $FValue)
428 # if search string appears in data field
429 if (strpos($Record[$FName], $SearchString) !== FALSE)
431 # add user to return array
432 $ReturnValue[$Record[
"UserId"]] = $Record;
434 # add matching search field to return array
435 $ReturnValue[$Record[
"UserId"]][
"APMatchingField"] = $FName;
441 # add matching user count
442 $this->DB->Query($CriteriaQuery);
443 $this->MatchingUserCount = $this->DB->NumRowsSelected();
445 # return array of matching users to caller
449 # check whether user name currently exists
452 # normalize user name
455 # check whether user name is already in use
456 $NameCount = $this->DB->Query(
457 "SELECT COUNT(*) AS NameCount FROM APUsers"
458 .
" WHERE UserName = '".addslashes($UserName).
"'",
461 # report to caller whether name exists
462 return ($NameCount > 0);
465 # check whether e-mail address currently has account associated with it
471 # check whether address is already in use
472 $AddressCount = $this->DB->Query(
473 "SELECT COUNT(*) AS AddressCount FROM APUsers"
474 .
" WHERE EMail = '".addslashes($Address).
"'",
477 # report to caller whether address is in use
478 return ($AddressCount > 0);
488 # assume no users will be found
491 # fetch the newest users
492 $this->DB->Query(
"SELECT *"
494 .
" ORDER BY CreationDate DESC"
495 .
" LIMIT ".intval($Limit));
496 $UserIds = $this->DB->FetchColumn(
"UserId");
498 # for each user id found
499 foreach ($UserIds as $UserId)
501 $Users[$UserId] =
new SPTUser($UserId);
504 # return the newest users
508 # ---- PRIVATE INTERFACE -------------------------------------------------
514 # callback function for sorting users
517 return strcasecmp($UserA[$this->SortFieldName], $UserB[$this->SortFieldName]);
static NormalizeUserName($UserName)
static IsValidLookingEMailAddress($EMail)
EMailAddressIsInUse($Address)
SQL database abstraction object with smart query caching.
CreateNewUser($UserName, $Password, $PasswordAgain, $EMail, $EMailAgain, $IgnoreErrorCodes=NULL)
Create new user.
static NormalizePassword($Password)
UserNameExists($UserName)
static IsValidPassword($Password)
GetRecentlyLoggedInUsers($Since=NULL, $Limit=10)
UserFactory()
Object constructor.
static IsValidUserName($UserName)
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)
const U_DUPLICATEUSERNAME
FindUsers($SearchString, $FieldName="UserName", $SortFieldName="UserName", $Offset=0, $Count=9999999)
const U_PASSWORDSDONTMATCH
TestNewUserValues($UserName, $Password, $PasswordAgain, $EMail, $EMailAgain)
static NormalizeEMailAddress($EMailAddress)
const U_EMPTYPASSWORDAGAIN
const U_ILLEGALEMAILAGAIN
GetUserCount($Condition=NULL)
Return number of users in the system.
GetLoggedInUsers($InactivityTimeout=60)
Get users who are currently logged in (i.e.
CompareUsersForSort($UserA, $UserB)
FindUserNames($SearchString, $FieldName="UserName", $SortFieldName="UserName", $Offset=0, $Count=9999999, $IdExclusions=array(), $ValueExclusions=array())
const U_ILLEGALPASSWORDAGAIN