Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
cookie.php
1<?php
2
4
6
7class Cookie extends Http\Cookie
8{
9 public const SPREAD_SITES = 1;
10 public const SPREAD_DOMAIN = 2;
11
12 protected $spread;
13 protected $originalName;
14
22 public function __construct($name, $value, $expires = null, $addPrefix = true)
23 {
24 $this->name = ($addPrefix ? static::generateCookieName($name) : $name);
25 $this->originalName = $name;
26 $this->value = $value;
27 $this->expires = ($expires === null ? time() + 31104000 : $expires); //60*60*24*30*12
28 $this->spread = static::SPREAD_DOMAIN | static::SPREAD_SITES;
29 $this->setDefaultsFromConfig();
30 }
31
32 protected static function generateCookieName($name)
33 {
34 static $cookiePrefix = null;
35
36 if ($cookiePrefix === null)
37 {
38 $cookiePrefix = Config\Option::get("main", "cookie_name", "BITRIX_SM")."_";
39 }
40 if (strpos($name, $cookiePrefix) !== 0)
41 {
42 $name = $cookiePrefix . $name;
43 }
44 return $name;
45 }
46
47 protected function setDefaultsFromConfig()
48 {
49 $cookiesSettings = Config\Configuration::getValue('cookies');
50
51 $this->secure = ($cookiesSettings['secure'] ?? false);
52 $this->httpOnly = ($cookiesSettings['http_only'] ?? true);
53 if (isset($cookiesSettings['samesite']))
54 {
55 $this->sameSite = $cookiesSettings['samesite'];
56 }
57 }
58
59 public function getDomain(): ?string
60 {
61 if ($this->domain === null)
62 {
63 $this->domain = static::getCookieDomain();
64 }
65
66 return $this->domain;
67 }
68
69 public function getOriginalName(): string
70 {
72 }
73
74 public function setSpread($spread)
75 {
76 $this->spread = $spread;
77 return $this;
78 }
79
80 public function getSpread()
81 {
82 return $this->spread;
83 }
84
92 public static function getCookieDomain()
93 {
94 static $domain = null;
95
96 if ($domain !== null)
97 {
98 return $domain;
99 }
100
101 $request = \Bitrix\Main\Context::getCurrent()->getRequest();
102
103 $httpHost = $request->getHttpHost();
104
105 $cacheFlags = Config\Configuration::getValue('cache_flags');
106 $cacheTtl = ($cacheFlags['site_domain'] ?? 0);
107
108 if ($cacheTtl === false)
109 {
110 $connection = \Bitrix\Main\Application::getConnection();
111 $sqlHelper = $connection->getSqlHelper();
112
113 $sql = "SELECT DOMAIN ".
114 "FROM b_lang_domain ".
115 "WHERE '".$sqlHelper->forSql('.'.$httpHost)."' like ".$sqlHelper->getConcatFunction("'%.'", "DOMAIN")." ".
116 "ORDER BY ".$sqlHelper->getLengthFunction("DOMAIN")." ";
117 $recordset = $connection->query($sql);
118 if ($record = $recordset->fetch())
119 {
120 $domain = $record['DOMAIN'];
121 }
122 }
123 else
124 {
125 $managedCache = \Bitrix\Main\Application::getInstance()->getManagedCache();
126
127 if ($managedCache->read($cacheTtl, 'b_lang_domain', 'b_lang_domain'))
128 {
129 $arLangDomain = $managedCache->get('b_lang_domain');
130 }
131 else
132 {
133 $arLangDomain = ['DOMAIN' => [], 'LID' => []];
134
135 $connection = \Bitrix\Main\Application::getConnection();
136 $sqlHelper = $connection->getSqlHelper();
137
138 $recordset = $connection->query(
139 "SELECT * ".
140 "FROM b_lang_domain ".
141 "ORDER BY ".$sqlHelper->getLengthFunction("DOMAIN")
142 );
143 while ($record = $recordset->fetch())
144 {
145 //it's a bit tricky, the cache is used somewhere else, that's why we have the LID key here.
146 $arLangDomain['DOMAIN'][] = $record;
147 $arLangDomain['LID'][$record['LID']][] = $record;
148 }
149 $managedCache->set('b_lang_domain', $arLangDomain);
150 }
151
152 foreach ($arLangDomain['DOMAIN'] as $record)
153 {
154 if (strcasecmp(mb_substr('.'.$httpHost, -(mb_strlen($record['DOMAIN']) + 1)), ".".$record['DOMAIN']) == 0)
155 {
156 $domain = $record['DOMAIN'];
157 break;
158 }
159 }
160 }
161
162 if ($domain === null)
163 {
164 $domain = '';
165 }
166
167 return $domain;
168 }
169}