PageRenderTime 21ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/Config.php

https://github.com/srhinow/myContaoSearchPortal
PHP | 315 lines | 157 code | 55 blank | 103 comment | 34 complexity | 23606eeae1be5dfdbcaf97fe07de7571 MD5 | raw file
  1. <?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');
  2. /**
  3. * Contao Open Source CMS
  4. * Copyright (C) 2005-2011 Leo Feyer
  5. *
  6. * Formerly known as TYPOlight Open Source CMS.
  7. *
  8. * This program is free software: you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation, either
  11. * version 3 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this program. If not, please visit the Free
  20. * Software Foundation website at <http://www.gnu.org/licenses/>.
  21. *
  22. * PHP version 5
  23. * @copyright Leo Feyer 2005-2011
  24. * @author Leo Feyer <http://www.contao.org>
  25. * @package System
  26. * @license LGPL
  27. * @filesource
  28. */
  29. /**
  30. * Class Config
  31. *
  32. * Provide methods to manage configuration files.
  33. * @copyright Leo Feyer 2005-2011
  34. * @author Leo Feyer <http://www.contao.org>
  35. * @package Library
  36. */
  37. class Config
  38. {
  39. /**
  40. * Current object instance (Singleton)
  41. * @var object
  42. */
  43. protected static $objInstance;
  44. /**
  45. * Files object
  46. * @var object
  47. */
  48. protected $Files;
  49. /**
  50. * Top content
  51. * @var string
  52. */
  53. protected $strTop = '';
  54. /**
  55. * Bottom content
  56. * @var string
  57. */
  58. protected $strBottom = '';
  59. /**
  60. * Modified
  61. * @var boolean
  62. */
  63. protected $blnIsModified = false;
  64. /**
  65. * Data array
  66. * @var array
  67. */
  68. protected $arrData = array();
  69. /**
  70. * Cache array
  71. * @var array
  72. */
  73. protected $arrCache = array();
  74. /**
  75. * Load all configuration files
  76. */
  77. protected function __construct()
  78. {
  79. include(TL_ROOT . '/system/config/config.php');
  80. include(TL_ROOT . '/system/config/localconfig.php');
  81. // Get module configuration files
  82. foreach ($this->getActiveModules() as $strModule)
  83. {
  84. $strFile = sprintf('%s/system/modules/%s/config/config.php', TL_ROOT, $strModule);
  85. @include($strFile);
  86. }
  87. include(TL_ROOT . '/system/config/localconfig.php');
  88. // Read the local configuration file
  89. $strMode = 'top';
  90. $resFile = fopen(TL_ROOT . '/system/config/localconfig.php', 'rb');
  91. while (!feof($resFile))
  92. {
  93. $strLine = fgets($resFile);
  94. $strTrim = trim($strLine);
  95. if ($strTrim == '?>')
  96. {
  97. continue;
  98. }
  99. if ($strTrim == '### INSTALL SCRIPT START ###')
  100. {
  101. $strMode = 'data';
  102. continue;
  103. }
  104. if ($strTrim == '### INSTALL SCRIPT STOP ###')
  105. {
  106. $strMode = 'bottom';
  107. continue;
  108. }
  109. if ($strMode == 'top')
  110. {
  111. $this->strTop .= $strLine;
  112. }
  113. elseif ($strMode == 'bottom')
  114. {
  115. $this->strBottom .= $strLine;
  116. }
  117. elseif ($strTrim != '')
  118. {
  119. $arrChunks = array_map('trim', explode('=', $strLine, 2));
  120. $this->arrData[$arrChunks[0]] = $arrChunks[1];
  121. }
  122. }
  123. fclose($resFile);
  124. }
  125. /**
  126. * Save the local configuration
  127. */
  128. public function __destruct()
  129. {
  130. if (!$this->blnIsModified)
  131. {
  132. return;
  133. }
  134. $strFile = trim($this->strTop) . "\n\n";
  135. $strFile .= "### INSTALL SCRIPT START ###\n";
  136. foreach ($this->arrData as $k=>$v)
  137. {
  138. $strFile .= "$k = $v\n";
  139. }
  140. $strFile .= "### INSTALL SCRIPT STOP ###\n\n";
  141. $this->strBottom = trim($this->strBottom);
  142. if ($this->strBottom != '')
  143. {
  144. $strFile .= $this->strBottom . "\n\n";
  145. }
  146. $strFile .= '?>';
  147. $objFile = new File('system/config/localconfig.php');
  148. $objFile->write($strFile);
  149. $objFile->close();
  150. }
  151. /**
  152. * Prevent cloning of the object (Singleton)
  153. */
  154. final private function __clone() {}
  155. /**
  156. * Return the current object instance (Singleton)
  157. * @return object
  158. */
  159. public static function getInstance()
  160. {
  161. if (!is_object(self::$objInstance))
  162. {
  163. self::$objInstance = new Config();
  164. }
  165. return self::$objInstance;
  166. }
  167. /**
  168. * Return all active modules (starting with "backend" and "frontend") as array
  169. * @param boolean
  170. * @return array
  171. */
  172. public function getActiveModules($blnNoCache=false)
  173. {
  174. if (!$blnNoCache && isset($this->arrCache['activeModules']))
  175. {
  176. return (array) $this->arrCache['activeModules']; // (array) = PHP 5.1.2 fix
  177. }
  178. $arrActiveModules = array('backend', 'frontend');
  179. $arrAllModules = scan(TL_ROOT . '/system/modules');
  180. $arrInactiveModules = deserialize($GLOBALS['TL_CONFIG']['inactiveModules']);
  181. $blnCheckInactiveModules = is_array($arrInactiveModules);
  182. foreach ($arrAllModules as $strModule)
  183. {
  184. if (substr($strModule, 0, 1) == '.')
  185. {
  186. continue;
  187. }
  188. if ($strModule == 'backend' || $strModule == 'frontend' || !is_dir(TL_ROOT . '/system/modules/' . $strModule))
  189. {
  190. continue;
  191. }
  192. if ($blnCheckInactiveModules && in_array($strModule, $arrInactiveModules))
  193. {
  194. continue;
  195. }
  196. $arrActiveModules[] = $strModule;
  197. }
  198. $this->arrCache['activeModules'] = $arrActiveModules;
  199. return $this->arrCache['activeModules'];
  200. }
  201. /**
  202. * Add a configuration variable to the local configuration file
  203. * @param string
  204. * @param mixed
  205. */
  206. public function add($strKey, $varValue)
  207. {
  208. $this->blnIsModified = true;
  209. $this->arrData[$strKey] = $this->escape($varValue) . ';';
  210. }
  211. /**
  212. * Alias for Config::add()
  213. * @param string
  214. * @param mixed
  215. */
  216. public function update($strKey, $varValue)
  217. {
  218. $this->add($strKey, $varValue);
  219. }
  220. /**
  221. * Delete a configuration variable from the local configuration file
  222. * @param string
  223. * @param mixed
  224. */
  225. public function delete($strKey)
  226. {
  227. $this->blnIsModified = true;
  228. unset($this->arrData[$strKey]);
  229. }
  230. /**
  231. * Escape a parameter depending on its type and return it
  232. * @param mixed
  233. * @return mixed
  234. */
  235. protected function escape($varValue)
  236. {
  237. if (is_numeric($varValue) && !preg_match('/e|^00+/', $varValue))
  238. {
  239. return $varValue;
  240. }
  241. if (is_bool($varValue))
  242. {
  243. return $varValue ? 'true' : 'false';
  244. }
  245. if ($varValue == 'true')
  246. {
  247. return 'true';
  248. }
  249. if ($varValue == 'false')
  250. {
  251. return 'false';
  252. }
  253. $varValue = preg_replace('/[\n\r\t]+/i', ' ', str_replace("'", "\\'", $varValue));
  254. $varValue = "'" . preg_replace('/ {2,}/i', ' ', $varValue) . "'";
  255. return $varValue;
  256. }
  257. }
  258. ?>