Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
kernelsession.php
1<?php
2
3namespace Bitrix\Main\Session;
4
8
9class KernelSession implements SessionInterface, \ArrayAccess
10{
12
13 private const COOKIE_NAME = 'kernel';
14
16 protected $started;
18 protected $sessionHandler;
20 private $lifetime = 0;
22 private $id;
24 private $hash;
25
26 public function __construct(int $lifetime = 0)
27 {
28 $this->lifetime = $lifetime;
29 }
30
31 public function isActive(): bool
32 {
33 return $this->isStarted();
34 }
35
36 public function isAccessible(): bool
37 {
38 return true;
39 }
40
41 public function getId(): string
42 {
43 return $this->id;
44 }
45
46 public function setId($id)
47 {
48 throw new NotSupportedException();
49 }
50
51 public function getName(): string
52 {
53 throw new NotSupportedException();
54 }
55
56 public function setName($name)
57 {
58 throw new NotSupportedException();
59 }
60
64 public function getSessionHandler(): \SessionHandlerInterface
65 {
67 }
68
69 final protected function hashData(string $data): string
70 {
71 return hash('sha512', $data);
72 }
73
74 public function start(): bool
75 {
76 if ($this->isStarted())
77 {
78 return true;
79 }
80
81 $this->started = true;
82 $this->sessionHandler = new CookieSessionHandler($this->lifetime);
83 $data = $this->getSessionHandler()->read(self::COOKIE_NAME);
84 $this->hash = $this->hashData($data);
85 $this->sessionData = unserialize($data, ['allowed_classes' => false]) ?: [];
86 if (!isset($this->sessionData['_id']))
87 {
88 $this->sessionData['_id'] = Random::getString(32, true);
89 }
90 $this->id = $this->sessionData['_id'];
91
92 return true;
93 }
94
95 public function regenerateId(): bool
96 {
97 return true;
98 }
99
100 public function destroy()
101 {
102 if ($this->isActive())
103 {
104 $this->clear();
105 }
106 }
107
108 public function save()
109 {
111 $data = serialize($this->sessionData);
112
113 if ($this->hashData($data) !== $this->hash)
114 {
115 $this->getSessionHandler()->write(self::COOKIE_NAME, $data);
116 }
117 $this->started = false;
118 }
119
120 public function clear()
121 {
122 $this->sessionData = [];
123 $this->nullPointers = [];
124 }
125
126 public function isStarted()
127 {
128 return (bool)$this->started;
129 }
130}