1
|
|
|
<?php |
2
|
|
|
namespace Divergence\Models\Auth; |
3
|
|
|
|
4
|
|
|
use Divergence\Models\Model; |
5
|
|
|
use Divergence\Models\Relations; |
6
|
|
|
|
7
|
|
|
class Session extends Model |
8
|
|
|
{ |
9
|
|
|
use Relations; |
10
|
|
|
|
11
|
|
|
// Session configurables |
12
|
|
|
public static $cookieName = 's'; |
13
|
|
|
public static $cookieDomain = null; |
14
|
|
|
public static $cookiePath = '/'; |
15
|
|
|
public static $cookieSecure = false; |
16
|
|
|
public static $cookieExpires = false; |
17
|
|
|
public static $timeout = 31536000; //3600; |
18
|
|
|
|
19
|
|
|
// support subclassing |
20
|
|
|
public static $rootClass = __CLASS__; |
21
|
|
|
public static $defaultClass = __CLASS__; |
22
|
|
|
public static $subClasses = [__CLASS__]; |
23
|
|
|
|
24
|
|
|
// ActiveRecord configuration |
25
|
|
|
public static $tableName = 'sessions'; |
26
|
|
|
public static $singularNoun = 'session'; |
27
|
|
|
public static $pluralNoun = 'sessions'; |
28
|
|
|
|
29
|
|
|
public static $fields = [ |
30
|
|
|
'ContextClass' => null, |
31
|
|
|
'ContextID' => null, |
32
|
|
|
'Handle' => [ |
33
|
|
|
'unique' => true, |
34
|
|
|
], |
35
|
|
|
'LastRequest' => [ |
36
|
|
|
'type' => 'timestamp', |
37
|
|
|
'notnull' => false, |
38
|
|
|
], |
39
|
|
|
'LastIP' => [ |
40
|
|
|
'type' => 'binary' |
41
|
|
|
], |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
// Session |
46
|
|
|
public static function __classLoaded() |
47
|
|
|
{ |
48
|
|
|
parent::__classLoaded(); |
|
|
|
|
49
|
|
|
|
50
|
|
|
// auto-detect cookie domain |
51
|
|
|
if (empty(static::$cookieDomain)) { |
52
|
|
|
static::$cookieDomain = preg_replace('/^www\.([^.]+\.[^.]+)$/i', '$1', $_SERVER['HTTP_HOST']); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
public static function getFromRequest($create = true) |
58
|
|
|
{ |
59
|
|
|
$sessionData = [ |
60
|
|
|
'LastIP' => inet_pton($_SERVER['REMOTE_ADDR']), |
61
|
|
|
'LastRequest' => time(), |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
// try to load from cookie |
65
|
|
|
if (!empty($_COOKIE[static::$cookieName])) { |
66
|
|
|
if ($Session = static::getByHandle($_COOKIE[static::$cookieName])) { |
67
|
|
|
// update session & check expiration |
68
|
|
|
$Session = static::updateSession($Session, $sessionData); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// try to load from any request method |
73
|
|
|
if (empty($Session) && !empty($_REQUEST[static::$cookieName])) { |
74
|
|
|
if ($Session = static::getByHandle($_REQUEST[static::$cookieName])) { |
75
|
|
|
// update session & check expiration |
76
|
|
|
$Session = static::updateSession($Session, $sessionData); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!empty($Session)) { |
81
|
|
|
// session found |
82
|
|
|
return $Session; |
83
|
|
|
} elseif ($create) { |
84
|
|
|
// create session |
85
|
|
|
return static::create($sessionData, true); |
86
|
|
|
} else { |
87
|
|
|
// no session available |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public static function updateSession(Session $Session, $sessionData) |
93
|
|
|
{ |
94
|
|
|
|
95
|
|
|
// check timestamp |
96
|
|
|
if ($Session->LastRequest < (time() - static::$timeout)) { |
|
|
|
|
97
|
|
|
$Session->terminate(); |
98
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} else { |
101
|
|
|
// update session |
102
|
|
|
$Session->setFields($sessionData); |
103
|
|
|
if (function_exists('fastcgi_finish_request')) { |
104
|
|
|
register_shutdown_function(function (&$Session) { |
105
|
|
|
$Session->save(); |
106
|
|
|
}, $Session); |
107
|
|
|
} else { |
108
|
|
|
$Session->save(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $Session; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public static function getByHandle($handle) |
116
|
|
|
{ |
117
|
|
|
return static::getByField('Handle', $handle, true); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getData() |
121
|
|
|
{ |
122
|
|
|
// embed related object(s) |
123
|
|
|
return array_merge(parent::getData(), [ |
124
|
|
|
'Person' => $this->Person ? $this->Person->getData() : null, |
|
|
|
|
125
|
|
|
]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function save($deep = true) |
129
|
|
|
{ |
130
|
|
|
// set handle |
131
|
|
|
if (!$this->Handle) { |
132
|
|
|
$this->Handle = static::generateUniqueHandle(); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// call parent |
136
|
|
|
parent::save($deep); |
137
|
|
|
|
138
|
|
|
// set cookie |
139
|
|
|
setcookie( |
140
|
|
|
static::$cookieName, |
141
|
|
|
$this->Handle, |
142
|
|
|
static::$cookieExpires ? (time() + static::$cookieExpires) : 0, |
143
|
|
|
static::$cookiePath, |
144
|
|
|
static::$cookieDomain, |
145
|
|
|
static::$cookieSecure |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function terminate() |
150
|
|
|
{ |
151
|
|
|
setcookie(static::$cookieName, '', time() - 3600); |
152
|
|
|
unset($_COOKIE[static::$cookieName]); |
153
|
|
|
|
154
|
|
|
$this->destroy(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
|
159
|
|
|
public static function generateUniqueHandle() |
160
|
|
|
{ |
161
|
|
|
do { |
162
|
|
|
$handle = md5(mt_rand(0, mt_getrandmax())); |
163
|
|
|
} while (static::getByHandle($handle)); |
164
|
|
|
|
165
|
|
|
return $handle; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|