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