-
Notifications
You must be signed in to change notification settings - Fork 2
/
Varien.php.fixed
441 lines (395 loc) · 12 KB
/
Varien.php.fixed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Core
* @copyright Copyright (c) 2014 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class Mage_Core_Model_Session_Abstract_Varien extends Varien_Object
{
const VALIDATOR_KEY = '_session_validator_data';
const VALIDATOR_HTTP_USER_AGENT_KEY = 'http_user_agent';
const VALIDATOR_HTTP_X_FORVARDED_FOR_KEY = 'http_x_forwarded_for';
const VALIDATOR_HTTP_VIA_KEY = 'http_via';
const VALIDATOR_REMOTE_ADDR_KEY = 'remote_addr';
/**
* Configure and start session
*
* @param string $sessionName
* @return Mage_Core_Model_Session_Abstract_Varien
*/
public function start($sessionName=null)
{
if (isset($_SESSION) && !$this->getSkipEmptySessionCheck()) {
return $this;
}
// getSessionSaveMethod has to return correct version of handler in any case
$moduleName = $this->getSessionSaveMethod();
switch ($moduleName) {
/**
* backward compatibility with db argument (option is @deprecated after 1.12.0.2)
*/
case 'db':
$moduleName = 'user';
/* @var $sessionResource Mage_Core_Model_Resource_Session */
$sessionResource = Mage::getResourceSingleton('core/session');
$sessionResource->setSaveHandler();
break;
case 'user':
// getSessionSavePath represents static function for custom session handler setup
call_user_func($this->getSessionSavePath());
break;
case 'files':
//don't change path if it's not writable
if (!is_writable($this->getSessionSavePath())) {
break;
}
default:
session_save_path($this->getSessionSavePath());
break;
}
session_module_name($moduleName);
$cookie = $this->getCookie();
if (Mage::app()->getStore()->isAdmin()) {
$sessionMaxLifetime = Mage_Core_Model_Resource_Session::SEESION_MAX_COOKIE_LIFETIME;
$adminSessionLifetime = (int)Mage::getStoreConfig('admin/security/session_cookie_lifetime');
if ($adminSessionLifetime > $sessionMaxLifetime) {
$adminSessionLifetime = $sessionMaxLifetime;
}
if ($adminSessionLifetime > 60) {
$cookie->setLifetime($adminSessionLifetime);
}
}
// session cookie params
$cookieParams = array(
'lifetime' => $cookie->getLifetime(),
'path' => $cookie->getPath()
// 'domain' => $cookie->getConfigDomain(),
// 'secure' => $cookie->isSecure(),
// 'httponly' => $cookie->getHttponly()
);
if (!$cookieParams['httponly']) {
unset($cookieParams['httponly']);
if (!$cookieParams['secure']) {
unset($cookieParams['secure']);
if (!$cookieParams['domain']) {
unset($cookieParams['domain']);
}
}
}
if (isset($cookieParams['domain'])) {
$cookieParams['domain'] = $cookie->getDomain();
}
call_user_func_array('session_set_cookie_params', $cookieParams);
if (!empty($sessionName)) {
$this->setSessionName($sessionName);
}
// potential custom logic for session id (ex. switching between hosts)
$this->setSessionId();
Varien_Profiler::start(__METHOD__.'/start');
$sessionCacheLimiter = Mage::getConfig()->getNode('global/session_cache_limiter');
if ($sessionCacheLimiter) {
session_cache_limiter((string)$sessionCacheLimiter);
}
session_start();
/**
* Renew cookie expiration time if session id did not change
*/
if ($cookie->get(session_name()) == $this->getSessionId()) {
$cookie->renew(session_name());
}
Varien_Profiler::stop(__METHOD__.'/start');
return $this;
}
/**
* Retrieve cookie object
*
* @return Mage_Core_Model_Cookie
*/
public function getCookie()
{
return Mage::getSingleton('core/cookie');
}
/**
* Revalidate cookie
* @deprecated after 1.4 cookie renew moved to session start method
* @return Mage_Core_Model_Session_Abstract_Varien
*/
public function revalidateCookie()
{
return $this;
}
/**
* Init session with namespace
*
* @param string $namespace
* @param string $sessionName
* @return Mage_Core_Model_Session_Abstract_Varien
*/
public function init($namespace, $sessionName=null)
{
if (!isset($_SESSION)) {
$this->start($sessionName);
}
if (!isset($_SESSION[$namespace])) {
$_SESSION[$namespace] = array();
}
$this->_data = &$_SESSION[$namespace];
$this->validate();
$this->revalidateCookie();
return $this;
}
/**
* Additional get data with clear mode
*
* @param string $key
* @param bool $clear
* @return mixed
*/
public function getData($key='', $clear = false)
{
$data = parent::getData($key);
if ($clear && isset($this->_data[$key])) {
unset($this->_data[$key]);
}
return $data;
}
/**
* Retrieve session Id
*
* @return string
*/
public function getSessionId()
{
return session_id();
}
/**
* Set custom session id
*
* @param string $id
* @return Mage_Core_Model_Session_Abstract_Varien
*/
public function setSessionId($id=null)
{
if (!is_null($id) && preg_match('#^[0-9a-zA-Z,-]+$#', $id)) {
session_id($id);
}
return $this;
}
/**
* Retrieve session name
*
* @return string
*/
public function getSessionName()
{
return session_name();
}
/**
* Set session name
*
* @param string $name
* @return Mage_Core_Model_Session_Abstract_Varien
*/
public function setSessionName($name)
{
session_name($name);
return $this;
}
/**
* Unset all data
*
* @return Mage_Core_Model_Session_Abstract_Varien
*/
public function unsetAll()
{
$this->unsetData();
return $this;
}
/**
* Alias for unsetAll
*
* @return Mage_Core_Model_Session_Abstract_Varien
*/
public function clear()
{
return $this->unsetAll();
}
/**
* Retrieve session save method
* Default files
*
* @return string
*/
public function getSessionSaveMethod()
{
return 'files';
}
/**
* Get sesssion save path
*
* @return string
*/
public function getSessionSavePath()
{
return Mage::getBaseDir('session');
}
/**
* Use REMOTE_ADDR in validator key
*
* @return bool
*/
public function useValidateRemoteAddr()
{
return true;
}
/**
* Use HTTP_VIA in validator key
*
* @return bool
*/
public function useValidateHttpVia()
{
return true;
}
/**
* Use HTTP_X_FORWARDED_FOR in validator key
*
* @return bool
*/
public function useValidateHttpXForwardedFor()
{
return true;
}
/**
* Use HTTP_USER_AGENT in validator key
*
* @return bool
*/
public function useValidateHttpUserAgent()
{
return true;
}
/**
* Retrieve skip User Agent validation strings (Flash etc)
*
* @return array
*/
public function getValidateHttpUserAgentSkip()
{
return array();
}
/**
* Validate session
*
* @param string $namespace
* @return Mage_Core_Model_Session_Abstract_Varien
*/
public function validate()
{
if (!isset($this->_data[self::VALIDATOR_KEY])) {
$this->_data[self::VALIDATOR_KEY] = $this->getValidatorData();
}
else {
if (!$this->_validate()) {
$this->getCookie()->delete(session_name());
// throw core session exception
throw new Mage_Core_Model_Session_Exception('');
}
}
return $this;
}
/**
* Validate data
*
* @return bool
*/
protected function _validate()
{
$sessionData = $this->_data[self::VALIDATOR_KEY];
$validatorData = $this->getValidatorData();
if ($this->useValidateRemoteAddr()
&& $sessionData[self::VALIDATOR_REMOTE_ADDR_KEY] != $validatorData[self::VALIDATOR_REMOTE_ADDR_KEY]) {
return false;
}
if ($this->useValidateHttpVia()
&& $sessionData[self::VALIDATOR_HTTP_VIA_KEY] != $validatorData[self::VALIDATOR_HTTP_VIA_KEY]) {
return false;
}
$sessionValidateHttpXForwardedForKey = $sessionData[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY];
$validatorValidateHttpXForwardedForKey = $validatorData[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY];
if ($this->useValidateHttpXForwardedFor()
&& $sessionValidateHttpXForwardedForKey != $validatorValidateHttpXForwardedForKey ) {
return false;
}
if ($this->useValidateHttpUserAgent()
&& $sessionData[self::VALIDATOR_HTTP_USER_AGENT_KEY] != $validatorData[self::VALIDATOR_HTTP_USER_AGENT_KEY]
) {
$userAgentValidated = $this->getValidateHttpUserAgentSkip();
foreach ($userAgentValidated as $agent) {
if (preg_match('/' . $agent . '/iu', $validatorData[self::VALIDATOR_HTTP_USER_AGENT_KEY])) {
return true;
}
}
return false;
}
return true;
}
/**
* Retrieve unique user data for validator
*
* @return array
*/
public function getValidatorData()
{
$parts = array(
self::VALIDATOR_REMOTE_ADDR_KEY => '',
self::VALIDATOR_HTTP_VIA_KEY => '',
self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY => '',
self::VALIDATOR_HTTP_USER_AGENT_KEY => ''
);
// collect ip data
if (Mage::helper('core/http')->getRemoteAddr()) {
$parts[self::VALIDATOR_REMOTE_ADDR_KEY] = Mage::helper('core/http')->getRemoteAddr();
}
if (isset($_ENV['HTTP_VIA'])) {
$parts[self::VALIDATOR_HTTP_VIA_KEY] = (string)$_ENV['HTTP_VIA'];
}
if (isset($_ENV['HTTP_X_FORWARDED_FOR'])) {
$parts[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY] = (string)$_ENV['HTTP_X_FORWARDED_FOR'];
}
// collect user agent data
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$parts[self::VALIDATOR_HTTP_USER_AGENT_KEY] = (string)$_SERVER['HTTP_USER_AGENT'];
}
return $parts;
}
/**
* Regenerate session Id
*
* @return Mage_Core_Model_Session_Abstract_Varien
*/
public function regenerateSessionId()
{
session_regenerate_id(true);
return $this;
}
}