PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Ip/Config.php

https://gitlab.com/x33n/ImpressPages
PHP | 172 lines | 120 code | 28 blank | 24 comment | 31 complexity | 8bf94290091ef79e60da95cb7e2cb917 MD5 | raw file
  1. <?php
  2. /**
  3. * @package ImpressPages
  4. */
  5. namespace Ip;
  6. /*
  7. * Core configuration
  8. *
  9. */
  10. class Config
  11. {
  12. protected $config;
  13. protected $protocol;
  14. /**
  15. * @param $config
  16. * @param array|null $server $_SERVER
  17. */
  18. public function __construct($config, $server = null)
  19. {
  20. $this->config = $config;
  21. if (!isset($this->config['tablePrefix'])) {
  22. $this->config['tablePrefix'] = $this->config['db']['tablePrefix'];
  23. }
  24. if (!isset($this->config['database'])) {
  25. $this->config['database'] = $this->config['db']['database'];
  26. }
  27. if (!$server) {
  28. $server = $_SERVER;
  29. }
  30. if (empty($this->config['baseUrl'])) {
  31. $this->config['baseUrl'] = $server["HTTP_HOST"];
  32. $baseUrl = substr($server['SCRIPT_NAME'], 0, strrpos($server['SCRIPT_NAME'], '/') + 1);
  33. if (DIRECTORY_SEPARATOR == '/') { // unix system
  34. if (strpos($server['REQUEST_URI'], $baseUrl) !== 0) {
  35. // show instructions how to set baseUrl manually
  36. include __DIR__ . '/Internal/Config/view/couldNotDetectBaseUrl.php';
  37. exit();
  38. }
  39. } else { // windows system
  40. if (strpos(strtolower($server['REQUEST_URI']), strtolower($baseUrl)) !== 0) {
  41. // show instructions how to set baseUrl manually
  42. include __DIR__ . '/Internal/Config/view/couldNotDetectBaseUrl.php';
  43. exit();
  44. }
  45. }
  46. $this->config['baseUrl'] .= rtrim($baseUrl, '/') . '/';
  47. }
  48. if (empty($this->config['baseDir'])) {
  49. $this->config['baseDir'] = realpath(dirname($server['SCRIPT_FILENAME']));
  50. }
  51. if (empty($this->config['charset'])) {
  52. $this->config['charset'] = 'UTF-8';
  53. }
  54. if (empty($this->config['defaultDoctype'])) {
  55. $this->config['defaultDoctype'] = 'DOCTYPE_HTML5';
  56. }
  57. if ((isset($server['HTTPS']) && strtolower($server['HTTPS']) == "on") || strtolower(getenv('HTTP_X_FORWARDED_PROTO')) === 'https') {
  58. $this->protocol = 'https://';
  59. } else {
  60. $this->protocol = 'http://';
  61. }
  62. }
  63. public function database()
  64. {
  65. return $this->config['database'];
  66. }
  67. public function tablePrefix()
  68. {
  69. return $this->config['tablePrefix'];
  70. }
  71. /**
  72. * Returns absolute base url.
  73. * @param string $protocol 'http:// https:// or //. Current protocol will be used if null
  74. * @return string
  75. */
  76. public function baseUrl($protocol = null)
  77. {
  78. $prot = $this->protocol;
  79. if ($protocol !== null) {
  80. $prot = $protocol;
  81. }
  82. return $prot . $this->config['baseUrl'];
  83. }
  84. /**
  85. * @param string $name
  86. * @param mixed $default
  87. * @return mixed
  88. */
  89. public function get($name, $default = null)
  90. {
  91. if (array_key_exists($name, $this->config)) {
  92. return $this->config[$name];
  93. } elseif ($default instanceof \Closure) {
  94. /** @var $default \Closure */
  95. return $default();
  96. } else {
  97. return $default;
  98. }
  99. }
  100. public function set($name, $value)
  101. {
  102. if ($name == 'db' && $value) {
  103. $this->set('tablePrefix', $value['tablePrefix']);
  104. if (!empty($value['database'])) {
  105. $this->set('database', $value['database']);
  106. }
  107. }
  108. if ($value === null) {
  109. unset($this->config[$name]);
  110. } else {
  111. $this->config[$name] = $value;
  112. }
  113. }
  114. public function isDevelopmentEnvironment()
  115. {
  116. return !empty($this->config['developmentEnvironment']);
  117. }
  118. public function isDebugMode()
  119. {
  120. return !empty($this->config['debugMode']);
  121. }
  122. public function theme()
  123. {
  124. if (!empty($this->config['theme'])) {
  125. return $this->config['theme'];
  126. } else {
  127. return ipStorage()->get('Ip', 'theme');
  128. }
  129. }
  130. public function adminLocale()
  131. {
  132. if (!empty($_COOKIE["ipAdminLocale"])) {
  133. return $_COOKIE["ipAdminLocale"];
  134. }
  135. if (!empty($this->config['adminLocale'])) {
  136. return $this->config['adminLocale'];
  137. }
  138. return 'en';
  139. }
  140. public function showErrors()
  141. {
  142. return !empty($this->config['showErrors']) || !empty($this->config['errorsShow']);
  143. }
  144. }