12 const TABLE_NAME =
"SessionToken";
16 public $IPAddress =
"";
18 public $RSA_PrivateKey =
"";
19 public $RSA_PublicKey =
"";
22 public $IdAccount = 0;
23 public $ClientVersion =
"";
29 public function __construct($src = null, $stripSlashes =
false) {
36 $this->_loadByRow($src, $stripSlashes);
39 $this->_loadFilter(self::GetTableName(__CLASS__), sprintf(
"Token = '%s'", $Database->Escape($src)));
57 public static function Load ($idAccount = 0, $fromDate = NULL, $toDate = NULL, $idApp = 0, $ipAddress = NULL, $limit = NULL, $offset = NULL, &$count = NULL) {
61 $where .= ($where ?
" AND " :
"") . sprintf(
"(IdAccount = %d)", $idAccount);
64 $where .= ($where ?
" AND " :
"") . sprintf(
"(Created >= %s)", $Database->EscapeDate($fromDate));
67 $where .= ($where ?
" AND " :
"") . sprintf(
"(Created <= %s)", $Database->EscapeDate($toDate));
70 $where .= ($where ?
" AND " :
"") . sprintf(
"(IdApp = %d)", $idApp);
72 if (!empty($ipAddress)) {
73 $where .= ($where ?
" AND " :
"") . sprintf(
"(IPAddress = '%s')", $Database->Escape($ipAddress));
75 return self::_load(self::GetTableName(__CLASS__), __CLASS__, $where,
"Created DESC", $limit, $offset, $count);
86 $now = Utils::GetCurrentDateTimeFormat();
87 if (!$this->Created) {
88 $this->Created = $now;
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),
95 $Database->Escape($this->IPAddress),
97 $Database->Escape($this->RSA_PrivateKey),
98 $Database->Escape($this->RSA_PublicKey),
99 $Database->Escape($this->AES_Key),
100 $Database->Escape($this->AES_IV),
102 $Database->Escape($this->ClientVersion),
104 if ($Database->Query($query)) {
118 $this->Updated = Utils::GetCurrentDateTimeFormat();
119 $query = sprintf(
"UPDATE %s SET Updated = '%s' WHERE Token = '%s'",
120 self::GetTableName(__CLASS__),
122 $Database->Escape($this->Token));
123 if ($Database->Query($query)) {
135 global $Database, $WS_TOKEN;
136 $where = sprintf(
"IdAccount = %d", $idAccount);
138 $where .= sprintf(
" AND Token <> '%s'", $Database->Escape($WS_TOKEN));
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"));
146 $recs = self::_load(self::GetTableName(__CLASS__), __CLASS__, $where);
147 foreach ($recs as $rec) {
161 $where = sprintf(
"(IdAccount = %d)", $idAccount);
162 if ($AppId->IsValid()) {
163 $where .= sprintf(
" AND (IdApp = %d)", $AppId->Id);
165 $records = self::_load(self::GetTableName(__CLASS__), __CLASS__, $where,
"Updated DESC", 1);
166 if (count($records) > 0) {
182 $query = sprintf(
"UPDATE %s SET IdAccount = %d, Updated = '%s' WHERE Token = '%s'",
183 self::GetTableName(__CLASS__),
185 Utils::GetCurrentDateTimeFormat(),
186 $Database->Escape($token));
187 if ($Database->Query($query)) {
203 $query = sprintf(
"UPDATE %s SET IdAccount = 0, Updated = '%s' WHERE IdAccount = %d AND Token = '%s'",
204 self::GetTableName(__CLASS__),
205 Utils::GetCurrentDateTimeFormat(),
207 $Database->Escape($token));
208 if ($Database->Query($query)) {
221 if (!empty($this->Token)) {
222 return $this->_Delete(self::GetTableName(__CLASS__), sprintf(
"Token = '%s'", $Database->Escape($this->Token)));
231 self::TruncateClass(__CLASS__);
static GetLastSession($idAccount)
__construct($src=null, $stripSlashes=false)
static UnsetSession($idAccount, $token)
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)