Bitrix-D7 22.6
 
Загрузка...
Поиск...
Не найдено
site.php
1<?php
2
10namespace Bitrix\Main;
11
14
32{
33 protected const CACHE_TTL = 86400;
34
35 protected static $documentRootCache = [];
36
37 public static function getDocumentRoot($siteId = null)
38 {
39 if ($siteId === null)
40 {
41 $siteId = Application::getInstance()->getContext()->getSite();
42 }
43
44 if (!isset(self::$documentRootCache[$siteId]))
45 {
46 $site = static::getRow([
47 "filter" => ["=LID" => $siteId],
48 "cache" => ["ttl" => static::CACHE_TTL],
49 ]);
50
51 if ($site && ($docRoot = $site["DOC_ROOT"]) && ($docRoot <> ''))
52 {
53 if (!IO\Path::isAbsolute($docRoot))
54 {
55 $docRoot = IO\Path::combine(Application::getDocumentRoot(), $docRoot);
56 }
57 self::$documentRootCache[$siteId] = $docRoot;
58 }
59 else
60 {
61 self::$documentRootCache[$siteId] = Application::getDocumentRoot();
62 }
63 }
64
65 return self::$documentRootCache[$siteId];
66 }
67
68 public static function getTableName()
69 {
70 return 'b_lang';
71 }
72
73 public static function getMap()
74 {
75 return array(
76 'LID' => array(
77 'data_type' => 'string',
78 'primary' => true
79 ),
80 'ID' => array(
81 'data_type' => 'string',
82 'expression' => array('%s', 'LID'),
83 ),
84 'SORT' => array(
85 'data_type' => 'integer',
86 ),
87 'DEF' => array(
88 'data_type' => 'boolean',
89 'values' => array('N', 'Y'),
90 ),
91 'ACTIVE' => array(
92 'data_type' => 'boolean',
93 'values' => array('N', 'Y'),
94 ),
95 'NAME' => array(
96 'data_type' => 'string'
97 ),
98 'DIR' => array(
99 'data_type' => 'string'
100 ),
101 'LANGUAGE_ID' => array(
102 'data_type' => 'string',
103 ),
104 'DOC_ROOT' => array(
105 'data_type' => 'string',
106 ),
107 'DOMAIN_LIMITED' => array(
108 'data_type' => 'boolean',
109 'values' => array('N', 'Y'),
110 ),
111 'SERVER_NAME' => array(
112 'data_type' => 'string'
113 ),
114 'SITE_NAME' => array(
115 'data_type' => 'string'
116 ),
117 'EMAIL' => array(
118 'data_type' => 'string'
119 ),
120 'CULTURE_ID' => array(
121 'data_type' => 'integer',
122 ),
123 'CULTURE' => array(
124 'data_type' => 'Bitrix\Main\Localization\Culture',
125 'reference' => array('=this.CULTURE_ID' => 'ref.ID'),
126 'join_type' => 'INNER',
127 ),
128 'LANGUAGE' => array(
129 'data_type' => 'Bitrix\Main\Localization\Language',
130 'reference' => array('=this.LANGUAGE_ID' => 'ref.ID'),
131 'join_type' => 'INNER',
132 ),
133 (new Fields\ExpressionField('DIR_LENGTH', 'LENGTH(%s)', 'DIR')),
134 (new Fields\ExpressionField('DOC_ROOT_LENGTH', 'IFNULL(LENGTH(%s), 0)', 'DOC_ROOT')),
135 );
136 }
137
138 public static function getByDomain(string $host, string $directory)
139 {
140 $site = null;
141
142 $sites = static::getList([
143 'select' => ['*'],
144 'filter' => ['=ACTIVE' => 'Y'],
145 'order' => [
146 'DIR_LENGTH' => 'DESC',
147 'DOMAIN_LIMITED' => 'DESC',
148 'SORT' => 'ASC',
149 ],
150 'cache' => ['ttl' => static::CACHE_TTL],
151 ])->fetchAll();
152
153 $result = SiteDomainTable::getList([
154 'select' => ['LD_LID' => 'LID', 'LD_DOMAIN' => 'DOMAIN'],
155 'order' => ['DOMAIN_LENGTH' => 'DESC'],
156 'cache' => ['ttl' => static::CACHE_TTL],
157 ]);
158
159 $domains = [];
160 while ($row = $result->fetch())
161 {
162 $domains[$row['LD_LID']][] = $row;
163 }
164
165 $join = [];
166 foreach ($sites as $row)
167 {
168 //LEFT JOIN
169 $left = true;
170 //LEFT JOIN b_lang_domain LD ON L.LID=LD.LID
171 if (array_key_exists($row['LID'], $domains))
172 {
173 foreach ($domains[$row['LID']] as $dom)
174 {
175 //AND '".$DB->ForSql($CURR_DOMAIN, 255)."' LIKE CONCAT('%', LD.DOMAIN)
176 if (strcasecmp(mb_substr(".".$host, -mb_strlen("." . $dom['LD_DOMAIN'])), "." . $dom['LD_DOMAIN']) == 0)
177 {
178 $join[] = $row + $dom;
179 $left = false;
180 }
181 }
182 }
183 if ($left)
184 {
185 $join[] = $row + ['LD_LID' => '', 'LD_DOMAIN' => ''];
186 }
187 }
188
189 $rows = [];
190 foreach ($join as $row)
191 {
192 //WHERE ('".$DB->ForSql($cur_dir)."' LIKE CONCAT(L.DIR, '%') OR LD.LID IS NOT NULL)
193 if ($row['LD_LID'] != '' || strcasecmp(mb_substr($directory, 0, mb_strlen($row['DIR'])), $row['DIR']) == 0)
194 {
195 $rows[] = $row;
196 }
197 }
198
199 foreach ($rows as $row)
200 {
201 if (
202 (strcasecmp(mb_substr($directory, 0, mb_strlen($row['DIR'])), $row['DIR']) == 0)
203 && (($row['DOMAIN_LIMITED'] == 'Y' && $row['LD_LID'] != '') || $row['DOMAIN_LIMITED'] != 'Y')
204 )
205 {
206 $site = $row;
207 break;
208 }
209 }
210
211 if ($site === null)
212 {
213 foreach ($rows as $row)
214 {
215 if (strncasecmp($directory, $row['DIR'], mb_strlen($directory)) == 0)
216 {
217 $site = $row;
218 break;
219 }
220 }
221 }
222
223 if($site === null)
224 {
225 foreach ($rows as $row)
226 {
227 if (($row['DOMAIN_LIMITED'] == 'Y' && $row['LD_LID'] != '') || $row['DOMAIN_LIMITED'] != 'Y')
228 {
229 $site = $row;
230 break;
231 }
232 }
233 }
234
235 if ($site === null && !empty($rows))
236 {
237 $site = $rows[0];
238 }
239
240 if ($site === null)
241 {
242 $site = static::getList([
243 'select' => ['*'],
244 'filter' => ['=ACTIVE' => 'Y'],
245 'order' => [
246 'DEF' => 'DESC',
247 'SORT' => 'ASC',
248 ],
249 'cache' => ['ttl' => static::CACHE_TTL],
250 ])->fetch();
251 }
252
253 if ($site)
254 {
255 // unset fields added from left join
256 unset($site['LD_LID'], $site['LD_DOMAIN']);
257 }
258
259 return $site;
260 }
261
262 public static function cleanCache(): void
263 {
264 parent::cleanCache();
265 self::$documentRootCache = [];
266 }
267}
static getMap()
Definition: site.php:73
static cleanCache()
Definition: site.php:262
static getByDomain(string $host, string $directory)
Definition: site.php:138
static getDocumentRoot($siteId=null)
Definition: site.php:37
static $documentRootCache
Definition: site.php:35
static getTableName()
Definition: site.php:68