Combu Server  3.1.1
PHP API Documentation
Account.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
10 class Account extends DataClass {
11 
12  const TABLE_NAME = "Account";
13 
14  public $Id = 0;
15  public $Username = "";
16  public $Password = "";
17  public $GUID = "";
18  public $LastLoginDate = "";
19  public $LastLoginIp = "";
20  public $Email = "";
21  public $ActivationCode = "";
22  public $ChangePwdCode = "";
23  public $Enabled = 0;
24 
25  private $Session = NULL;
26 
30  public function __construct($src = null, $stripSlashes = false) {
31  global $Database;
32  if (!empty($src)) {
33  if (is_array($src)) {
34  // Load by array
35  $this->_loadByRow($src, $stripSlashes);
36  } else if (is_numeric($src)) {
37  // Load by Id
38  $this->_loadFilter(self::GetTableName(__CLASS__), sprintf("Id = %d", $src));
39  } else {
40  // Load by Username
41  $this->_loadFilter(self::GetTableName(__CLASS__), "Username = '" . $Database->Escape($src) . "'");
42  if ($this->Id < 1) {
43  // Load by Email
44  $this->_loadFilter(self::GetTableName(__CLASS__), "Email = '" . $Database->Escape($src) . "'");
45  }
46  }
47  }
48  }
49 
55  public function ExistsUsername() {
56  global $Database;
57  $sql = "SELECT Id FROM " . self::GetTableName(__CLASS__) . " WHERE Username = '" . $Database->Escape($this->Username) . "'";
58  if ($this->Id > 0)
59  $sql .= " AND Id <> " . $this->Id;
60  $res = $Database->Query($sql);
61  if ($res) {
62  $row = $Database->FetchAssoc($res);
63  if ($row)
64  return TRUE;
65  }
66  return FALSE;
67  }
68 
74  public function ExistsEmail() {
75  global $Database;
76  $sql = "SELECT Id FROM " . self::GetTableName(__CLASS__) . " WHERE Email = '" . $Database->Escape($this->Email) . "'";
77  if ($this->Id > 0)
78  $sql .= " AND Id <> " . $this->Id;
79  $res = $Database->Query($sql);
80  if ($res) {
81  $row = $Database->FetchAssoc($res);
82  if ($row)
83  return TRUE;
84  }
85  return FALSE;
86  }
87 
93  public function IsLogged() {
94  global $LoggedAccount;
95  return ($this->Id > 0 && $this->Id == $LoggedAccount->Id && !$this->ActivationCode);
96  }
97 
98  public function IsOnline() {
99  $lastAction = $this->GetLastActionDate();
100  if ($lastAction) {
101  if (time() - Utils::GetTimestamp($lastAction) <= ONLINE_SECONDS) {
102  return TRUE;
103  }
104  }
105  return FALSE;
106  }
107 
108  public function GetLastActionDate() {
109  if ($this->Id > 0) {
110  $session = $this->GetSession();
111  if ($session) {
112  return $session->Updated;
113  }
114  }
115  return NULL;
116  }
117 
122  public function GetSession() {
123  if ($this->Id > 0 && !$this->Session) {
124  $this->LoadSession();
125  }
126  return $this->Session;
127  }
128 
144  public static function Load($username = "", $email = "", $customData = NULL, $appCustomData = NULL, $isOnline = FALSE, $limit = NULL, $offset = NULL, &$count = NULL, $returnArray = FALSE) {
145  global $AppId;
146  return self::LoadAny($AppId->Id, $username, $email, $customData, $appCustomData = NULL, $isOnline, $limit, $offset, $count, $returnArray);
147  }
148 
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);
167  }
168 
186  public static function LoadAnyByIp($ip, $idApp = 0, $username = "", $email = "", $customData = NULL, $appCustomData = NULL, $isOnline = FALSE, $limit = NULL, $offset = NULL, &$count = NULL, $returnArray = FALSE) {
187  global $Database;
188  $select = "a.*";
189  $from = self::GetTableName(__CLASS__) . " a";
190  $where = "";
191  if (!empty($ip)) {
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) . "%");
195  }
196  if ($idApp > 0) {
197  $from .= sprintf(" INNER JOIN %s aapp ON (a.Id = aapp.IdAccount AND aapp.IdApp = %d)",
198  self::GetTableName(Account_App::class),
199  $idApp);
200  }
201  if ($isOnline) {
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) : "");
207  }
208  if ($username != "") {
209  $where .= ($where == "" ? "" : " AND ") . sprintf("(Username LIKE '%s')", $Database->Escape($username . "%"));
210  }
211  if ($email != "") {
212  $where .= ($where == "" ? "" : " AND ") . sprintf("(Email LIKE '%s')", $Database->Escape("%" . $email . "%"));
213  }
214  if ($customData && is_array($customData) && count($customData) > 0) {
215  foreach ($customData as $search_row) {
216  if (!is_array($search_row)) {
217  continue;
218  }
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) . "'";
224  }
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);
227  }
228  }
229  if ($appCustomData && is_array($appCustomData) && count($appCustomData) > 0) {
230  foreach ($appCustomData as $search_row) {
231  if (!is_array($search_row)) {
232  continue;
233  }
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) . "'";
239  }
240  $whereCustom = sprintf("(DataKey = '%s' AND DataValue %s %s)", $Database->Escape($key), $op, $value);
241  if ($idApp > 0) {
242  $whereCustom .= sprintf(" AND (IdApp = %d)", $idApp);
243  }
244  $where .= ($where == "" ? "" : " AND ") . sprintf("(ID IN (SELECT DISTINCT IdAccount FROM %s WHERE %s GROUP BY IdAccount))", self::GetTableName(AppCustomData::class), $whereCustom);
245  }
246  }
247  $where .= ($where ? "" : "(1 = 1)") . " GROUP BY a.Id";
248  return self::_loadEx($select, $from, ($returnArray ? "" : __CLASS__), $where, "Username", $limit, $offset, $count);
249  }
250 
257  public static function LoadIds($ids, $returnArray = FALSE) {
258  if (!$ids || !is_array($ids)) {
259  return array();
260  }
261  $where = sprintf("(Id IN (%s))", implode(",", $ids));
262  return self::_load(self::GetTableName(__CLASS__), ($returnArray ? "" : __CLASS__), $where, "Username");
263  }
264 
272  public static function LoadUsernames($usernames, $returnArray = FALSE) {
273  global $Database;
274  if (!$usernames || !is_array($usernames)) {
275  return array();
276  }
277  $array_usernames = array();
278  foreach ($usernames as $username) {
279  $array_usernames[] = "'" . $Database->Escape($username) . "'";
280  }
281  $where = sprintf("(Username IN (%s))", implode(",", $array_usernames));
282  return self::_load(self::GetTableName(__CLASS__), ($returnArray ? "" : __CLASS__), $where, "Username");
283  }
284 
290  private function LoadSession() {
291  if ($this->Id > 0) {
292  if ($this->GUID) {
293  // Load the session
294  $session = new SessionToken($this->GUID);
295  if ($session->IdAccount == $this->Id) {
296  $this->Session = $session;
297  }
298  } else {
299  $this->Session = SessionToken::GetLastSession($this->Id);
300  }
301  }
302  }
303 
312  public static function CheckLogin($username, $password, &$account = null) {
313  global $Database;
314  $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);
317  if ($res) {
318  $row = $Database->FetchAssoc($res);
319  if ($row) {
320  $account = new self($row);
321  return TRUE;
322  }
323  }
324  return FALSE;
325  }
326 
332  public function UpdateLastLogin() {
333  global $Database;
334  if ($this->Id > 0) {
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),
341  $this->Id);
342  return $Database->Query($query);
343  }
344  return FALSE;
345  }
346 
352  public static function SetSession(&$account) {
353  global $LoggedAccount, $WS_TOKEN, $AppId;
354  if ($account && is_a($account, __CLASS__) && $account->Id > 0) {
355  // Delete older sessions or every other session
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);
363  }
364  }
365  }
366 
372  public static function UnsetSession() {
373  global $LoggedAccount;
374  if ($LoggedAccount != NULL && $LoggedAccount->IsLogged()) {
375  SessionToken::UnsetSession($LoggedAccount->Id, $LoggedAccount->GUID);
376  $LoggedAccount->GUID = "";
377  }
378  $LoggedAccount = new self();
379  }
380 
385  public static function Logout() {
386  self::UnsetSession();
387  }
388 
394  public function Save() {
395  global $Database;
396  if ($this->Id < 1) {
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),
403  $this->Enabled);
404  } else {
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),
411  $this->Enabled,
412  $this->Id);
413  }
414  $saved = $Database->Query($query);
415  if ($saved) {
416  if ($this->Id <= 0) {
417  $this->Id = $Database->InsertedId();
418  }
419  return TRUE;
420  }
421  return FALSE;
422  }
423 
429  public function Delete() {
430  if ($this->Id > 0 && $this->_Delete(self::GetTableName(__CLASS__), "Id = " . $this->Id)) {
431  // Delete all the associated resources
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));
440  return TRUE;
441  }
442  return FALSE;
443  }
444 
448  public static function Prune() {
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);
457  CustomData::Prune();
458  UserFile::Prune();
459  Inventory::Prune();
460  SessionToken::Prune();
461  GameMail::Prune();
462  LeaderBoard_User::Prune();
463  Tournament::Prune();
464  }
465 
473  public function ChangePassword($newPassword) {
474  global $Database;
475  if ($this->Id > 0) {
476  $query = sprintf("UPDATE %s SET Password = '%s' WHERE Id = %d",
477  self::GetTableName(__CLASS__),
478  $Database->Escape($newPassword),
479  $this->Id);
480  if ($Database->Query($query)) {
481  $this->Password = $newPassword;
482  return TRUE;
483  }
484  }
485  return FALSE;
486  }
487 
498  public static function LoadRandom($excludeIds = array(), $customData = NULL, $limit = null, $offset = null, &$count = null, $returnArray = false) {
499  global $Database;
500  $where = "";
501  if (!is_array($excludeIds))
502  $excludeIds = array($excludeIds);
503  if (count($excludeIds) > 0) {
504  $ids = array();
505  foreach ($excludeIds as $id) {
506  if ($id > 0)
507  $ids[] = intval($id);
508  }
509  $where .= ($where == "" ? "" : " AND ") . "Id NOT IN (" . implode(",", $excludeIds) . ")";
510  }
511  if ($customData && is_array($customData) && count($customData) > 0) {
512  foreach ($customData as $search_row) {
513  if (!is_array($search_row))
514  continue;
515  $key = $search_row["key"];
516  $op = $search_row["op"];
517  $value = $search_row["value"];
518 
519  if (!is_numeric($value))
520  $value = "'" . $Database->Escape($value) . "'";
521 
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);
524  }
525  }
526  return self::_load(self::GetTableName(__CLASS__), ($returnArray ? "" : __CLASS__), $where, "RAND()", $limit, $offset, $count);
527  }
528 
540  public static function LoadRandomFriends($idUser, $excludeIds = array(), $customData = NULL, $limit = null, $offset = null, &$count = null, $returnArray = false) {
541  global $Database;
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) {
546  $ids = array();
547  foreach ($excludeIds as $id) {
548  if ($id > 0)
549  $ids[] = intval($id);
550  }
551  if (count($ids) > 0)
552  $where .= ($where == "" ? "" : " AND ") . "(Id NOT IN (" . implode(",", $ids) . "))";
553  }
554  if ($customData && is_array($customData) && count($customData) > 0) {
555  foreach ($customData as $search_row) {
556  if (!is_array($search_row))
557  continue;
558  $key = $search_row["key"];
559  $op = $search_row["op"];
560  $value = $search_row["value"];
561 
562  if (!is_numeric($value))
563  $value = "'" . $Database->Escape($value) . "'";
564 
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);
567  }
568  }
569  return self::_load(self::GetTableName(__CLASS__), ($returnArray ? "" : __CLASS__), $where, "RAND()", $limit, $offset, $count);
570  }
571 
577  public static function CreateRandom($prefix, &$account = NULL) {
578  $account = NULL;
579  $new = new self();
580  $new->Username = "__TEMP__" . session_id() . "_" . time();
581  if ($new->Save()) {
582  $new->Username = $prefix . $new->Id;
583  $new->Save();
584  $account = $new;
585  return TRUE;
586  }
587  return FALSE;
588  }
589 
595  public function ToJson () {
596  $array = $this->ToArray();
597  return json_encode($array);
598  }
599 
606  public function ToArray () {
607  global $LoggedAccount, $AppId;
608  $array = array();
609  if ($this->Id > 0 || $this->Username) {
610  $array = Utils::ObjectToArray($this);
611  // Remove Password from any response
612  unset($array["Password"]);
613  // Remove GUID token and other personal info from response if it's not me
614  if ($this->Id > 0 && $this->Id == $LoggedAccount->Id) {
615  $array["GUID"] = $LoggedAccount->GUID;
616  } else {
617  unset($array["GUID"]);
618  unset($array["Email"]);
619  }
620  // Rename signature timestamp in something more user-friendly :P
621  $lastAction = $this->GetLastActionDate();
622  if ($lastAction) {
623  $array["LastSeen"] = $lastAction;
624  }
625  // Add Custom data
626  $array["CustomData"] = array();
627  $customData = CustomData::Load($this->Id);
628  foreach ($customData as $data) {
629  $array["CustomData"][$data->DataKey] = $data->DataValue;
630  }
631  // Add Custom data for current App when running as web service
632  $array["AppCustomData"] = array();
633  $appCustomData = AppCustomData::Load($AppId->Id, $this->Id);
634  foreach ($appCustomData as $data) {
635  $array["AppCustomData"][$data->DataKey] = $data->DataValue;
636  }
637  // Add Platforms
638  $array["Platforms"] = array();
639  $platforms = Account_Platform::Load($this->Id);
640  foreach ($platforms as $platform) {
641  $array["Platforms"][] = $platform->ToArray();
642  }
643  }
644  return $array;
645  }
646 
652  public function ToArrayFiltered() {
653  global $AppId;
654  $array = $this->ToArray();
655  unset($array["LastLoginDate"]);
656  unset($array["LastLoginIp"]);
657  unset($array["ActivationCode"]);
658  unset($array["ChangePwdCode"]);
659  unset($array["Enabled"]);
660  // Add Custom Data
661  $customData = $array["CustomData"];
662  AddonModule::ProcessOutputUserCustomData($this, $customData);
663  if (!$customData) {
664  $customData = array();
665  } else if (!is_array($customData)) {
666  $customData = array($customData);
667  }
668  $array["CustomData"] = $customData;
669  // Add Custom Data for App
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);
676  }
677  $array["AppCustomData"] = $appCustomData;
678  AddonModule::ProcessOutputUser($array);
679  return $array;
680  }
681 
686  public function ToJsonFiltered() {
687  $array = $this->ToArrayFiltered();
688  return json_encode($array);
689  }
690 }
static CheckLogin($username, $password, &$account=null)
Definition: Account.php:312
static LoadRandomFriends($idUser, $excludeIds=array(), $customData=NULL, $limit=null, $offset=null, &$count=null, $returnArray=false)
Definition: Account.php:540
ChangePassword($newPassword)
Definition: Account.php:473
static LoadAnyByIp($ip, $idApp=0, $username="", $email="", $customData=NULL, $appCustomData=NULL, $isOnline=FALSE, $limit=NULL, $offset=NULL, &$count=NULL, $returnArray=FALSE)
Definition: Account.php:186
static CreateRandom($prefix, &$account=NULL)
Definition: Account.php:577
static Load($username="", $email="", $customData=NULL, $appCustomData=NULL, $isOnline=FALSE, $limit=NULL, $offset=NULL, &$count=NULL, $returnArray=FALSE)
Definition: Account.php:144
GetLastActionDate()
Definition: Account.php:108
static Prune()
Definition: Account.php:448
static LoadIds($ids, $returnArray=FALSE)
Definition: Account.php:257
static Logout()
Definition: Account.php:385
__construct($src=null, $stripSlashes=false)
Definition: Account.php:30
static SetSession(&$account)
Definition: Account.php:352
Definition: Account.php:3
ExistsUsername()
Definition: Account.php:55
static LoadUsernames($usernames, $returnArray=FALSE)
Definition: Account.php:272
static LoadAny($idApp=0, $username="", $email="", $customData=NULL, $appCustomData=NULL, $isOnline=FALSE, $limit=NULL, $offset=NULL, &$count=NULL, $returnArray=FALSE)
Definition: Account.php:165
static UnsetSession()
Definition: Account.php:372
static LoadRandom($excludeIds=array(), $customData=NULL, $limit=null, $offset=null, &$count=null, $returnArray=false)
Definition: Account.php:498