Bitrix-D7  20.5.0
main/lib/config/configuration.php
См. документацию.
1 <?php
3 
4 use Bitrix\Main;
5 
6 final class Configuration
7  implements \ArrayAccess, \Iterator, \Countable
8 {
9  /**
10  * @var Configuration[]
11  */
12  private static $instances;
13 
14  private $moduleId = null;
15  private $storedData = null;
16  private $data = array();
17  private $isLoaded = false;
18 
19  const CONFIGURATION_FILE_PATH = "/bitrix/.settings.php";
20  const CONFIGURATION_FILE_PATH_EXTRA = "/bitrix/.settings_extra.php";
21 
22  public static function getValue($name)
23  {
24  $configuration = Configuration::getInstance();
25  return $configuration->get($name);
26  }
27 
28  public static function setValue($name, $value)
29  {
30  $configuration = Configuration::getInstance();
31  $configuration->add($name, $value);
32  $configuration->saveConfiguration();
33  }
34 
35  private function __construct($moduleId = null)
36  {
37  if($moduleId !== null)
38  {
39  $this->moduleId = preg_replace("/[^a-zA-Z0-9_.]+/i", "", trim($moduleId));
40  }
41  }
42 
43  /**
44  * @static
45  *
46  * @param string|null $moduleId
47  * @return Configuration
48  */
49  public static function getInstance($moduleId = null)
50  {
51  if (!isset(self::$instances[$moduleId]))
52  {
53  self::$instances[$moduleId] = new static($moduleId);
54  }
55 
56  return self::$instances[$moduleId];
57  }
58 
59  private static function getPath($path)
60  {
61  $path = Main\Loader::getDocumentRoot().$path;
62  return preg_replace("'[\\\\/]+'", "/", $path);
63  }
64 
65  private static function getPathConfigForModule($moduleId)
66  {
67  if (!$moduleId || !Main\ModuleManager::isModuleInstalled($moduleId))
68  {
69  return false;
70  }
71 
72  $moduleConfigPath = getLocalPath("modules/{$moduleId}/.settings.php");
73  if ($moduleConfigPath === false)
74  {
75  return false;
76  }
77 
78  return static::getPath($moduleConfigPath);
79  }
80 
81  private function loadConfiguration()
82  {
83  $this->isLoaded = false;
84 
85  if ($this->moduleId)
86  {
87  $path = static::getPathConfigForModule($this->moduleId);
88  if (file_exists($path))
89  {
90  $dataTmp = include($path);
91  if(is_array($dataTmp))
92  {
93  $this->data = $dataTmp;
94  }
95  }
96  }
97  else
98  {
99  $path = static::getPath(self::CONFIGURATION_FILE_PATH);
100  if (file_exists($path))
101  {
102  $dataTmp = include($path);
103  if(is_array($dataTmp))
104  {
105  $this->data = $dataTmp;
106  }
107  }
108 
109  $pathExtra = static::getPath(self::CONFIGURATION_FILE_PATH_EXTRA);
110  if (file_exists($pathExtra))
111  {
112  $dataTmp = include($pathExtra);
113  if (is_array($dataTmp) && !empty($dataTmp))
114  {
115  $this->storedData = $this->data;
116  foreach ($dataTmp as $k => $v)
117  {
118  $this->data[$k] = $v;
119  }
120  }
121  }
122  }
123 
124  $this->isLoaded = true;
125  }
126 
127  public function saveConfiguration()
128  {
129  if (!$this->isLoaded)
130  $this->loadConfiguration();
131 
132  if($this->moduleId)
133  {
134  throw new Main\InvalidOperationException('There is no support to rewrite .settings.php in module');
135  }
136  else
137  {
138  $path = static::getPath(self::CONFIGURATION_FILE_PATH);
139  }
140 
141  $data = ($this->storedData !== null) ? $this->storedData : $this->data;
142  $data = var_export($data, true);
143 
144  if (!is_writable($path))
145  @chmod($path, 0644);
146  file_put_contents($path, "<"."?php\nreturn ".$data.";\n");
147  }
148 
149  public function add($name, $value)
150  {
151  if (!$this->isLoaded)
152  $this->loadConfiguration();
153 
154  if (!isset($this->data[$name]) || !$this->data[$name]["readonly"])
155  $this->data[$name] = array("value" => $value, "readonly" => false);
156  if (($this->storedData !== null) && (!isset($this->storedData[$name]) || !$this->storedData[$name]["readonly"]))
157  $this->storedData[$name] = array("value" => $value, "readonly" => false);
158  }
159 
160  /**
161  * Changes readonly params.
162  * Warning! Developer must use this method very carfully!.
163  * You must use this method only if you know what you do!
164  * @param string $name
165  * @param array $value
166  * @return void
167  */
168  public function addReadonly($name, $value)
169  {
170  if (!$this->isLoaded)
171  $this->loadConfiguration();
172 
173  $this->data[$name] = array("value" => $value, "readonly" => true);
174  if ($this->storedData !== null)
175  $this->storedData[$name] = array("value" => $value, "readonly" => true);
176  }
177 
178  public function delete($name)
179  {
180  if (!$this->isLoaded)
181  $this->loadConfiguration();
182 
183  if (isset($this->data[$name]) && !$this->data[$name]["readonly"])
184  unset($this->data[$name]);
185  if (($this->storedData !== null) && isset($this->storedData[$name]) && !$this->storedData[$name]["readonly"])
186  unset($this->storedData[$name]);
187  }
188 
189  public function get($name)
190  {
191  if (!$this->isLoaded)
192  $this->loadConfiguration();
193 
194  if (isset($this->data[$name]))
195  return $this->data[$name]["value"];
196 
197  return null;
198  }
199 
200  public function offsetExists($name)
201  {
202  if (!$this->isLoaded)
203  $this->loadConfiguration();
204 
205  return isset($this->data[$name]);
206  }
207 
208  public function offsetGet($name)
209  {
210  return $this->get($name);
211  }
212 
213  public function offsetSet($name, $value)
214  {
215  $this->add($name, $value);
216  }
217 
218  public function offsetUnset($name)
219  {
220  $this->delete($name);
221  }
222 
223  public function current()
224  {
225  if (!$this->isLoaded)
226  $this->loadConfiguration();
227 
228  $c = current($this->data);
229 
230  return $c === false ? false : $c["value"];
231  }
232 
233  public function next()
234  {
235  if (!$this->isLoaded)
236  $this->loadConfiguration();
237 
238  $c = next($this->data);
239 
240  return $c === false ? false : $c["value"];
241  }
242 
243  public function key()
244  {
245  if (!$this->isLoaded)
246  $this->loadConfiguration();
247 
248  return key($this->data);
249  }
250 
251  public function valid()
252  {
253  if (!$this->isLoaded)
254  $this->loadConfiguration();
255 
256  $key = $this->key();
257  return isset($this->data[$key]);
258  }
259 
260  public function rewind()
261  {
262  if (!$this->isLoaded)
263  $this->loadConfiguration();
264 
265  return reset($this->data);
266  }
267 
268  public function count()
269  {
270  if (!$this->isLoaded)
271  $this->loadConfiguration();
272 
273  return count($this->data);
274  }
275 
276  public static function wnc()
277  {
278  $configuration = Configuration::getInstance();
279  $configuration->loadConfiguration();
280 
281  $ar = array(
282  "utf_mode" => array("value" => defined('BX_UTF'), "readonly" => true),
283  "default_charset" => array("value" => defined('BX_DEFAULT_CHARSET'), "readonly" => false),
284  "no_accelerator_reset" => array("value" => defined('BX_NO_ACCELERATOR_RESET'), "readonly" => false),
285  "http_status" => array("value" => (defined('BX_HTTP_STATUS') && BX_HTTP_STATUS) ? true : false, "readonly" => false),
286  );
287 
288  $cache = array();
289  if (defined('BX_CACHE_SID'))
290  $cache["sid"] = BX_CACHE_SID;
291  if (file_exists($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/cluster/memcache.php"))
292  {
293  $arList = null;
294  include($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/cluster/memcache.php");
295  if (defined("BX_MEMCACHE_CLUSTER") && is_array($arList))
296  {
297  foreach ($arList as $listKey => $listVal)
298  {
299  $bOtherGroup = defined("BX_CLUSTER_GROUP") && ($listVal["GROUP_ID"] !== BX_CLUSTER_GROUP);
300 
301  if (($listVal["STATUS"] !== "ONLINE") || $bOtherGroup)
302  unset($arList[$listKey]);
303  }
304 
305  if (count($arList) > 0)
306  {
307  $cache["type"] = array(
308  "extension" => "memcache",
309  "required_file" => "modules/cluster/classes/general/memcache_cache.php",
310  "class_name" => "CPHPCacheMemcacheCluster",
311  );
312  }
313  }
314  }
315  if (!isset($cache["type"]))
316  {
317  if (defined('BX_CACHE_TYPE'))
318  {
319  $cache["type"] = BX_CACHE_TYPE;
320 
321  switch ($cache["type"])
322  {
323  case "memcache":
324  case "CPHPCacheMemcache":
325  $cache["type"] = "memcache";
326  break;
327  case "eaccelerator":
328  case "CPHPCacheEAccelerator":
329  $cache["type"] = "eaccelerator";
330  break;
331  case "apc":
332  case "CPHPCacheAPC":
333  $cache["type"] = "apc";
334  break;
335  case "xcache":
336  case "CPHPCacheXCache":
337  $cache["type"] = array(
338  "extension" => "xcache",
339  "required_file" => "modules/main/classes/general/cache_xcache.php",
340  "class_name" => "CPHPCacheXCache",
341  );
342  break;
343  default:
344  if (defined("BX_CACHE_CLASS_FILE") && file_exists(BX_CACHE_CLASS_FILE))
345  {
346  $cache["type"] = array(
347  "required_remote_file" => BX_CACHE_CLASS_FILE,
348  "class_name" => BX_CACHE_TYPE
349  );
350  }
351  else
352  {
353  $cache["type"] = "files";
354  }
355  break;
356  }
357  }
358  else
359  {
360  $cache["type"] = "files";
361  }
362  }
363  if (defined("BX_MEMCACHE_PORT"))
364  $cache["memcache"]["port"] = intval(BX_MEMCACHE_PORT);
365  if (defined("BX_MEMCACHE_HOST"))
366  $cache["memcache"]["host"] = BX_MEMCACHE_HOST;
367  $ar["cache"] = array("value" => $cache, "readonly" => false);
368 
369  $cacheFlags = array();
370  $arCacheConsts = array("CACHED_b_option" => "config_options", "CACHED_b_lang_domain" => "site_domain");
371  foreach ($arCacheConsts as $const => $name)
372  $cacheFlags[$name] = defined($const) ? constant($const) : 0;
373  $ar["cache_flags"] = array("value" => $cacheFlags, "readonly" => false);
374 
375  $ar["cookies"] = array("value" => array("secure" => false, "http_only" => true), "readonly" => false);
376 
377  $ar["exception_handling"] = array(
378  "value" => array(
379  "debug" => true,
380  "handled_errors_types" => E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE,
381  "exception_errors_types" => E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_WARNING & ~E_COMPILE_WARNING & ~E_DEPRECATED,
382  "ignore_silence" => false,
383  "assertion_throws_exception" => true,
384  "assertion_error_type" => E_USER_ERROR,
385  "log" => array(
386  /*"class_name" => "...",
387  "extension" => "...",
388  "required_file" => "...",*/
389  "settings" => array(
390  "file" => "bitrix/modules/error.log",
391  "log_size" => 1000000
392  )
393  ),
394  ),
395  "readonly" => false
396  );
397 
398  global $DBHost, $DBName, $DBLogin, $DBPassword;
399 
400  $dbClassName = defined('BX_USE_MYSQLI') && BX_USE_MYSQLI === true ? "\\Bitrix\\Main\\DB\\MysqliConnection" : "\\Bitrix\\Main\\DB\\MysqlConnection";
401 
402  $ar['connections']['value']['default'] = array(
403  'className' => $dbClassName,
404  'host' => $DBHost,
405  'database' => $DBName,
406  'login' => $DBLogin,
407  'password' => $DBPassword,
408  'options' => ((!defined("DBPersistent") || DBPersistent) ? 1 : 0) | ((defined("DELAY_DB_CONNECT") && DELAY_DB_CONNECT === true) ? 2 : 0)
409  );
410  $ar['connections']['readonly'] = true;
411 
412  foreach ($ar as $k => $v)
413  {
414  if ($configuration->get($k) === null)
415  {
416  if ($v["readonly"])
417  $configuration->addReadonly($k, $v["value"]);
418  else
419  $configuration->add($k, $v["value"]);
420  }
421  }
422 
423  $configuration->saveConfiguration();
424 
425  $filename1 = $_SERVER["DOCUMENT_ROOT"]."/bitrix/php_interface/after_connect.php";
426  $filename2 = $_SERVER["DOCUMENT_ROOT"]."/bitrix/php_interface/after_connect_d7.php";
427  if (file_exists($filename1) && !file_exists($filename2))
428  {
429  $source = file_get_contents($filename1);
430  $source = trim($source);
431  $pos = 2;
432  if (mb_strtolower(mb_substr($source, 0, 5)) == '<?php')
433  $pos = 5;
434  $source = mb_substr($source, 0, $pos)."\n".'$connection = \Bitrix\Main\Application::getConnection();'.mb_substr($source, $pos);
435  $source = preg_replace("#\\\$DB->Query\‍(#i", "\$connection->queryExecute(", $source);
436  file_put_contents($filename2, $source);
437  }
438  }
439 }
static getDocumentRoot()
Returns document root.
static isModuleInstalled($moduleName)
__construct(Base $connector)
Constructor.
Definition: resultview.php:40