Combu Server  3.1.1
PHP API Documentation
SessionToken.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
10 class SessionToken extends DataClass {
11 
12  const TABLE_NAME = "SessionToken";
13 
14  public $Token = "";
15  public $IdApp = 0;
16  public $IPAddress = "";
17  public $Created = "";
18  public $RSA_PrivateKey = "";
19  public $RSA_PublicKey = "";
20  public $AES_Key = "";
21  public $AES_IV = "";
22  public $IdAccount = 0;
23  public $ClientVersion = "";
24  public $Updated = "";
25 
29  public function __construct($src = null, $stripSlashes = false) {
30  global $Database;
31  if (!$src) {
32  return;
33  }
34  if (is_array($src)) {
35  // Load by array
36  $this->_loadByRow($src, $stripSlashes);
37  } else {
38  // Load by Id
39  $this->_loadFilter(self::GetTableName(__CLASS__), sprintf("Token = '%s'", $Database->Escape($src)));
40  }
41  }
42 
57  public static function Load ($idAccount = 0, $fromDate = NULL, $toDate = NULL, $idApp = 0, $ipAddress = NULL, $limit = NULL, $offset = NULL, &$count = NULL) {
58  global $Database;
59  $where = "";
60  if ($idAccount > 0) {
61  $where .= ($where ? " AND " : "") . sprintf("(IdAccount = %d)", $idAccount);
62  }
63  if ($fromDate) {
64  $where .= ($where ? " AND " : "") . sprintf("(Created >= %s)", $Database->EscapeDate($fromDate));
65  }
66  if ($toDate) {
67  $where .= ($where ? " AND " : "") . sprintf("(Created <= %s)", $Database->EscapeDate($toDate));
68  }
69  if ($idApp > 0) {
70  $where .= ($where ? " AND " : "") . sprintf("(IdApp = %d)", $idApp);
71  }
72  if (!empty($ipAddress)) {
73  $where .= ($where ? " AND " : "") . sprintf("(IPAddress = '%s')", $Database->Escape($ipAddress));
74  }
75  return self::_load(self::GetTableName(__CLASS__), __CLASS__, $where, "Created DESC", $limit, $offset, $count);
76  }
77 
84  public function Save() {
85  global $Database;
86  $now = Utils::GetCurrentDateTimeFormat();
87  if (!$this->Created) {
88  $this->Created = $now;
89  }
90  $this->Updated = $now;
91  $query = sprintf("REPLACE INTO %s (Token, IdApp, IPAddress, Created, RSA_PrivateKey, RSA_PublicKey, AES_Key, AES_IV, IdAccount, ClientVersion, Updated) VALUES ('%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', '%s')",
92  self::GetTableName(__CLASS__),
93  $Database->Escape($this->Token),
94  $this->IdApp,
95  $Database->Escape($this->IPAddress),
96  $this->Created,
97  $Database->Escape($this->RSA_PrivateKey),
98  $Database->Escape($this->RSA_PublicKey),
99  $Database->Escape($this->AES_Key),
100  $Database->Escape($this->AES_IV),
101  $this->IdAccount,
102  $Database->Escape($this->ClientVersion),
103  $this->Updated);
104  if ($Database->Query($query)) {
105  return TRUE;
106  }
107  return FALSE;
108  }
109 
116  function UpdateLastAction () {
117  global $Database;
118  $this->Updated = Utils::GetCurrentDateTimeFormat();
119  $query = sprintf("UPDATE %s SET Updated = '%s' WHERE Token = '%s'",
120  self::GetTableName(__CLASS__),
121  $this->Updated,
122  $Database->Escape($this->Token));
123  if ($Database->Query($query)) {
124  return TRUE;
125  }
126  return FALSE;
127  }
128 
134  public static function ClearSessions($idAccount, $deleteAll = FALSE) {
135  global $Database, $WS_TOKEN;
136  $where = sprintf("IdAccount = %d", $idAccount);
137  if ($WS_TOKEN) {
138  $where .= sprintf(" AND Token <> '%s'", $Database->Escape($WS_TOKEN));
139  }
140  if (!$deleteAll) {
141  // Get all sessions from 24 hours ago and earlier
142  $date = Utils::GetCurrentDateTime();
143  $date = $date->sub(new \DateInterval("P1D"));
144  $where .= sprintf(" AND LastActionDate <= '%s'", $date->format("Y-m-d H:i:s"));
145  }
146  $recs = self::_load(self::GetTableName(__CLASS__), __CLASS__, $where);
147  foreach ($recs as $rec) {
148  $rec->Delete();
149  }
150  }
151 
159  public static function GetLastSession($idAccount) {
160  global $AppId;
161  $where = sprintf("(IdAccount = %d)", $idAccount);
162  if ($AppId->IsValid()) {
163  $where .= sprintf(" AND (IdApp = %d)", $AppId->Id);
164  }
165  $records = self::_load(self::GetTableName(__CLASS__), __CLASS__, $where, "Updated DESC", 1);
166  if (count($records) > 0) {
167  return $records[0];
168  }
169  return NULL;
170  }
171 
180  public static function SetSession($idAccount, $token) {
181  global $Database;
182  $query = sprintf("UPDATE %s SET IdAccount = %d, Updated = '%s' WHERE Token = '%s'",
183  self::GetTableName(__CLASS__),
184  $idAccount,
185  Utils::GetCurrentDateTimeFormat(),
186  $Database->Escape($token));
187  if ($Database->Query($query)) {
188  return TRUE;
189  }
190  return FALSE;
191  }
192 
201  public static function UnsetSession($idAccount, $token) {
202  global $Database;
203  $query = sprintf("UPDATE %s SET IdAccount = 0, Updated = '%s' WHERE IdAccount = %d AND Token = '%s'",
204  self::GetTableName(__CLASS__),
205  Utils::GetCurrentDateTimeFormat(),
206  $idAccount,
207  $Database->Escape($token));
208  if ($Database->Query($query)) {
209  return TRUE;
210  }
211  return FALSE;
212  }
213 
219  public function Delete() {
220  global $Database;
221  if (!empty($this->Token)) {
222  return $this->_Delete(self::GetTableName(__CLASS__), sprintf("Token = '%s'", $Database->Escape($this->Token)));
223  }
224  return FALSE;
225  }
226 
230  public static function Prune() {
231  self::TruncateClass(__CLASS__);
232  }
233 }
static GetLastSession($idAccount)
__construct($src=null, $stripSlashes=false)
static UnsetSession($idAccount, $token)
Definition: Account.php:3
static SetSession($idAccount, $token)
static ClearSessions($idAccount, $deleteAll=FALSE)
static Load($idAccount=0, $fromDate=NULL, $toDate=NULL, $idApp=0, $ipAddress=NULL, $limit=NULL, $offset=NULL, &$count=NULL)