12 const TABLE_NAME =
"Account";
15 public $Username =
"";
16 public $Password =
"";
18 public $LastLoginDate =
"";
19 public $LastLoginIp =
"";
21 public $ActivationCode =
"";
22 public $ChangePwdCode =
"";
25 private $Session = NULL;
30 public function __construct($src = null, $stripSlashes =
false) {
35 $this->_loadByRow($src, $stripSlashes);
36 }
else if (is_numeric($src)) {
38 $this->_loadFilter(self::GetTableName(__CLASS__), sprintf(
"Id = %d", $src));
41 $this->_loadFilter(self::GetTableName(__CLASS__),
"Username = '" . $Database->Escape($src) .
"'");
44 $this->_loadFilter(self::GetTableName(__CLASS__),
"Email = '" . $Database->Escape($src) .
"'");
57 $sql =
"SELECT Id FROM " . self::GetTableName(__CLASS__) .
" WHERE Username = '" . $Database->Escape($this->Username) .
"'";
59 $sql .=
" AND Id <> " . $this->Id;
60 $res = $Database->Query($sql);
62 $row = $Database->FetchAssoc($res);
76 $sql =
"SELECT Id FROM " . self::GetTableName(__CLASS__) .
" WHERE Email = '" . $Database->Escape($this->Email) .
"'";
78 $sql .=
" AND Id <> " . $this->Id;
79 $res = $Database->Query($sql);
81 $row = $Database->FetchAssoc($res);
94 global $LoggedAccount;
95 return ($this->Id > 0 && $this->Id == $LoggedAccount->Id && !$this->ActivationCode);
99 $lastAction = $this->GetLastActionDate();
101 if (time() - Utils::GetTimestamp($lastAction) <= ONLINE_SECONDS) {
110 $session = $this->GetSession();
112 return $session->Updated;
123 if ($this->Id > 0 && !$this->Session) {
124 $this->LoadSession();
126 return $this->Session;
144 public static function Load($username =
"", $email =
"", $customData = NULL, $appCustomData = NULL, $isOnline = FALSE, $limit = NULL, $offset = NULL, &$count = NULL, $returnArray = FALSE) {
146 return self::LoadAny($AppId->Id, $username, $email, $customData, $appCustomData = NULL, $isOnline, $limit, $offset, $count, $returnArray);
165 public static function LoadAny($idApp = 0, $username =
"", $email =
"", $customData = NULL, $appCustomData = NULL, $isOnline = FALSE, $limit = NULL, $offset = NULL, &$count = NULL, $returnArray = FALSE) {
166 return self::LoadAnyByIp(NULL, $idApp, $username, $email, $customData, $appCustomData, $isOnline, $limit, $offset, $count, $returnArray);
186 public static function LoadAnyByIp($ip, $idApp = 0, $username =
"", $email =
"", $customData = NULL, $appCustomData = NULL, $isOnline = FALSE, $limit = NULL, $offset = NULL, &$count = NULL, $returnArray = FALSE) {
189 $from = self::GetTableName(__CLASS__) .
" a";
192 $from .= sprintf(
" INNER JOIN %s session ON (a.Id = session.IdAccount AND IPAddress LIKE '%s')",
193 self::GetTableName(SessionToken::class),
194 $Database->Escape($ip) .
"%");
197 $from .= sprintf(
" INNER JOIN %s aapp ON (a.Id = aapp.IdAccount AND aapp.IdApp = %d)",
198 self::GetTableName(Account_App::class),
202 $usersOnlineTicks = time() - ONLINE_SECONDS;
203 $where .= ($where ==
"" ?
"" :
" AND ") . sprintf(
"(Id IN (SELECT DISTINCT IdAccount FROM %s WHERE Updated >= '%s' %s GROUP BY IdAccount))",
204 self::GetTableName(SessionToken::class),
205 date(
"Y-m-d H:i:s", $usersOnlineTicks),
206 $idApp > 0 ?
" AND IdApp = " . intval($idApp) :
"");
208 if ($username !=
"") {
209 $where .= ($where ==
"" ?
"" :
" AND ") . sprintf(
"(Username LIKE '%s')", $Database->Escape($username .
"%"));
212 $where .= ($where ==
"" ?
"" :
" AND ") . sprintf(
"(Email LIKE '%s')", $Database->Escape(
"%" . $email .
"%"));
214 if ($customData && is_array($customData) && count($customData) > 0) {
215 foreach ($customData as $search_row) {
216 if (!is_array($search_row)) {
219 $key = $search_row[
"key"];
220 $op = $search_row[
"op"];
221 $value = $search_row[
"value"];
222 if (!is_numeric($value)) {
223 $value =
"'" . $Database->Escape($value) .
"'";
225 $whereCustom = sprintf(
"(DataKey = '%s' AND DataValue %s %s)", $Database->Escape($key), $op, $value);
226 $where .= ($where ==
"" ?
"" :
" AND ") . sprintf(
"(ID IN (SELECT DISTINCT IdAccount FROM %s WHERE %s GROUP BY IdAccount))", self::GetTableName(CustomData::class), $whereCustom);
229 if ($appCustomData && is_array($appCustomData) && count($appCustomData) > 0) {
230 foreach ($appCustomData as $search_row) {
231 if (!is_array($search_row)) {
234 $key = $search_row[
"key"];
235 $op = $search_row[
"op"];
236 $value = $search_row[
"value"];
237 if (!is_numeric($value)) {
238 $value =
"'" . $Database->Escape($value) .
"'";
240 $whereCustom = sprintf(
"(DataKey = '%s' AND DataValue %s %s)", $Database->Escape($key), $op, $value);
242 $whereCustom .= sprintf(
" AND (IdApp = %d)", $idApp);
244 $where .= ($where ==
"" ?
"" :
" AND ") . sprintf(
"(ID IN (SELECT DISTINCT IdAccount FROM %s WHERE %s GROUP BY IdAccount))", self::GetTableName(AppCustomData::class), $whereCustom);
247 $where .= ($where ?
"" :
"(1 = 1)") .
" GROUP BY a.Id";
248 return self::_loadEx($select, $from, ($returnArray ?
"" : __CLASS__), $where,
"Username", $limit, $offset, $count);
257 public static function LoadIds($ids, $returnArray = FALSE) {
258 if (!$ids || !is_array($ids)) {
261 $where = sprintf(
"(Id IN (%s))", implode(
",", $ids));
262 return self::_load(self::GetTableName(__CLASS__), ($returnArray ?
"" : __CLASS__), $where,
"Username");
274 if (!$usernames || !is_array($usernames)) {
277 $array_usernames = array();
278 foreach ($usernames as $username) {
279 $array_usernames[] =
"'" . $Database->Escape($username) .
"'";
281 $where = sprintf(
"(Username IN (%s))", implode(
",", $array_usernames));
282 return self::_load(self::GetTableName(__CLASS__), ($returnArray ?
"" : __CLASS__), $where,
"Username");
290 private function LoadSession() {
295 if ($session->IdAccount == $this->Id) {
296 $this->Session = $session;
299 $this->Session = SessionToken::GetLastSession($this->Id);
312 public static function CheckLogin($username, $password, &$account = null) {
315 $query = sprintf(
"SELECT * FROM " . self::GetTableName(__CLASS__) .
" WHERE (Username = '%s' OR Email = '%s') AND Password = '%s'", $Database->Escape($username), $Database->Escape($username), $Database->Escape($password));
316 $res = $Database->Query($query);
318 $row = $Database->FetchAssoc($res);
320 $account =
new self($row);
335 $this->LastLoginDate = Utils::GetCurrentDateTimeFormat();
336 $this->LastLoginIp = Utils::GetClientIP();
337 $query = sprintf(
"UPDATE %s SET LastLoginDate = %s, LastLoginIp = '%s' WHERE Id = %d",
338 self::GetTableName(__CLASS__),
339 $Database->EscapeDate($this->LastLoginDate),
340 $Database->Escape($this->LastLoginIp),
342 return $Database->Query($query);
353 global $LoggedAccount, $WS_TOKEN, $AppId;
354 if ($account && is_a($account, __CLASS__) && $account->Id > 0) {
356 SessionToken::ClearSessions($account->Id, defined(
"CLEAR_PLAYER_SESSIONS") && CLEAR_PLAYER_SESSIONS === TRUE);
357 $account->GUID = $WS_TOKEN;
358 $account->UpdateLastLogin();
359 SessionToken::SetSession($account->Id, $account->GUID);
360 $LoggedAccount = $account;
361 if ($AppId->IsValid()) {
362 Account_App::InsertOrUpdate($LoggedAccount->Id, $AppId->Id);
373 global $LoggedAccount;
374 if ($LoggedAccount != NULL && $LoggedAccount->IsLogged()) {
375 SessionToken::UnsetSession($LoggedAccount->Id, $LoggedAccount->GUID);
376 $LoggedAccount->GUID =
"";
378 $LoggedAccount =
new self();
386 self::UnsetSession();
397 $query = sprintf(
"INSERT INTO %s (Username, Password, Email, ActivationCode, Enabled) VALUES ('%s', '%s', '%s', '%s', %d)",
398 self::GetTableName(__CLASS__),
399 $Database->Escape($this->Username),
400 $Database->Escape($this->Password),
401 $Database->Escape($this->Email),
402 $Database->Escape($this->ActivationCode),
405 $query = sprintf(
"UPDATE %s SET Username = '%s', Email = '%s', ActivationCode = '%s', ChangePwdCode = '%s', Enabled = %d WHERE Id = %d",
406 self::GetTableName(__CLASS__),
407 $Database->Escape($this->Username),
408 $Database->Escape($this->Email),
409 $Database->Escape($this->ActivationCode),
410 $Database->Escape($this->ChangePwdCode),
414 $saved = $Database->Query($query);
416 if ($this->Id <= 0) {
417 $this->Id = $Database->InsertedId();
430 if ($this->Id > 0 && $this->_Delete(self::GetTableName(__CLASS__),
"Id = " . $this->Id)) {
432 $this->_Delete(self::GetTableName(Account_Platform::class), sprintf(
"IdAccount = %d", $this->Id));
433 $this->_Delete(self::GetTableName(Friend::class), sprintf(
"IdAccount = %d OR IdFriend = %d", $this->Id, $this->Id));
434 $this->_Delete(self::GetTableName(CustomData::class),
"IdAccount = " . $this->Id);
435 $this->_Delete(self::GetTableName(Inventory::class),
"IdAccount = " . $this->Id);
436 $this->_Delete(self::GetTableName(UserFile::class),
"IdAccount = " . $this->Id);
437 $this->_Delete(self::GetTableName(LeaderBoard_User::class),
"IdAccount = " . $this->Id);
438 $this->_Delete(self::GetTableName(Achievement_User::class),
"IdAccount = " . $this->Id);
439 $this->_Delete(self::GetTableName(GameMail::class), sprintf(
"IdAccount = %d OR IdSender = %d", $this->Id, $this->Id));
449 self::TruncateClass(__CLASS__);
450 self::TruncateClass(Account_App::class);
451 self::TruncateClass(Account_Platform::class);
452 self::TruncateClass(Friend::class);
453 self::TruncateClass(Achievement_User::class);
454 self::TruncateClass(UserGroup::class);
455 self::TruncateClass(UserGroupAccount::class);
456 self::TruncateClass(NewsletterLog::class);
460 SessionToken::Prune();
462 LeaderBoard_User::Prune();
476 $query = sprintf(
"UPDATE %s SET Password = '%s' WHERE Id = %d",
477 self::GetTableName(__CLASS__),
478 $Database->Escape($newPassword),
480 if ($Database->Query($query)) {
481 $this->Password = $newPassword;
498 public static function LoadRandom($excludeIds = array(), $customData = NULL, $limit = null, $offset = null, &$count = null, $returnArray =
false) {
501 if (!is_array($excludeIds))
502 $excludeIds = array($excludeIds);
503 if (count($excludeIds) > 0) {
505 foreach ($excludeIds as $id) {
507 $ids[] = intval($id);
509 $where .= ($where ==
"" ?
"" :
" AND ") .
"Id NOT IN (" . implode(
",", $excludeIds) .
")";
511 if ($customData && is_array($customData) && count($customData) > 0) {
512 foreach ($customData as $search_row) {
513 if (!is_array($search_row))
515 $key = $search_row[
"key"];
516 $op = $search_row[
"op"];
517 $value = $search_row[
"value"];
519 if (!is_numeric($value))
520 $value =
"'" . $Database->Escape($value) .
"'";
522 $whereCustom = sprintf(
"(DataKey = '%s' AND DataValue %s %s)", $Database->Escape($key), $op, $value);
523 $where .= ($where ==
"" ?
"" :
" AND ") . sprintf(
"(ID IN (SELECT IdAccount FROM %s WHERE %s))", self::GetTableName(CustomData::class), $whereCustom);
526 return self::_load(self::GetTableName(__CLASS__), ($returnArray ?
"" : __CLASS__), $where,
"RAND()", $limit, $offset, $count);
540 public static function LoadRandomFriends($idUser, $excludeIds = array(), $customData = NULL, $limit = null, $offset = null, &$count = null, $returnArray =
false) {
542 $where = sprintf(
"(Id IN (SELECT IdFriend FROM %s WHERE IdAccount = %d AND State = %d))", self::GetTableName(Friend::class), $idUser, FRIEND_STATE_ACCEPTED);
543 if (!is_array($excludeIds))
544 $excludeIds = array($excludeIds);
545 if (count($excludeIds) > 0) {
547 foreach ($excludeIds as $id) {
549 $ids[] = intval($id);
552 $where .= ($where ==
"" ?
"" :
" AND ") .
"(Id NOT IN (" . implode(
",", $ids) .
"))";
554 if ($customData && is_array($customData) && count($customData) > 0) {
555 foreach ($customData as $search_row) {
556 if (!is_array($search_row))
558 $key = $search_row[
"key"];
559 $op = $search_row[
"op"];
560 $value = $search_row[
"value"];
562 if (!is_numeric($value))
563 $value =
"'" . $Database->Escape($value) .
"'";
565 $whereCustom = sprintf(
"(DataKey = '%s' AND DataValue %s %s)", $Database->Escape($key), $op, $value);
566 $where .= ($where ==
"" ?
"" :
" AND ") . sprintf(
"(ID IN (SELECT IdAccount FROM %s WHERE %s))", self::GetTableName(CustomData::class), $whereCustom);
569 return self::_load(self::GetTableName(__CLASS__), ($returnArray ?
"" : __CLASS__), $where,
"RAND()", $limit, $offset, $count);
580 $new->Username =
"__TEMP__" . session_id() .
"_" . time();
582 $new->Username = $prefix . $new->Id;
596 $array = $this->ToArray();
597 return json_encode($array);
607 global $LoggedAccount, $AppId;
609 if ($this->Id > 0 || $this->Username) {
610 $array = Utils::ObjectToArray($this);
612 unset($array[
"Password"]);
614 if ($this->Id > 0 && $this->Id == $LoggedAccount->Id) {
615 $array[
"GUID"] = $LoggedAccount->GUID;
617 unset($array[
"GUID"]);
618 unset($array[
"Email"]);
621 $lastAction = $this->GetLastActionDate();
623 $array[
"LastSeen"] = $lastAction;
626 $array[
"CustomData"] = array();
627 $customData = CustomData::Load($this->Id);
628 foreach ($customData as $data) {
629 $array[
"CustomData"][$data->DataKey] = $data->DataValue;
632 $array[
"AppCustomData"] = array();
633 $appCustomData = AppCustomData::Load($AppId->Id, $this->Id);
634 foreach ($appCustomData as $data) {
635 $array[
"AppCustomData"][$data->DataKey] = $data->DataValue;
638 $array[
"Platforms"] = array();
639 $platforms = Account_Platform::Load($this->Id);
640 foreach ($platforms as $platform) {
641 $array[
"Platforms"][] = $platform->ToArray();
654 $array = $this->ToArray();
655 unset($array[
"LastLoginDate"]);
656 unset($array[
"LastLoginIp"]);
657 unset($array[
"ActivationCode"]);
658 unset($array[
"ChangePwdCode"]);
659 unset($array[
"Enabled"]);
661 $customData = $array[
"CustomData"];
662 AddonModule::ProcessOutputUserCustomData($this, $customData);
664 $customData = array();
665 }
else if (!is_array($customData)) {
666 $customData = array($customData);
668 $array[
"CustomData"] = $customData;
670 $appCustomData = $array[
"AppCustomData"];
671 AddonModule::ProcessOutputUserCustomData($this, $appCustomData);
672 if (!$appCustomData) {
673 $appCustomData = array();
674 }
else if (!is_array($appCustomData)) {
675 $appCustomData = array($appCustomData);
677 $array[
"AppCustomData"] = $appCustomData;
678 AddonModule::ProcessOutputUser($array);
687 $array = $this->ToArrayFiltered();
688 return json_encode($array);
static CheckLogin($username, $password, &$account=null)
static LoadRandomFriends($idUser, $excludeIds=array(), $customData=NULL, $limit=null, $offset=null, &$count=null, $returnArray=false)
ChangePassword($newPassword)
static LoadAnyByIp($ip, $idApp=0, $username="", $email="", $customData=NULL, $appCustomData=NULL, $isOnline=FALSE, $limit=NULL, $offset=NULL, &$count=NULL, $returnArray=FALSE)
static CreateRandom($prefix, &$account=NULL)
static Load($username="", $email="", $customData=NULL, $appCustomData=NULL, $isOnline=FALSE, $limit=NULL, $offset=NULL, &$count=NULL, $returnArray=FALSE)
static LoadIds($ids, $returnArray=FALSE)
__construct($src=null, $stripSlashes=false)
static SetSession(&$account)
static LoadUsernames($usernames, $returnArray=FALSE)
static LoadAny($idApp=0, $username="", $email="", $customData=NULL, $appCustomData=NULL, $isOnline=FALSE, $limit=NULL, $offset=NULL, &$count=NULL, $returnArray=FALSE)
static LoadRandom($excludeIds=array(), $customData=NULL, $limit=null, $offset=null, &$count=null, $returnArray=false)