[FEATURE] Add wincache as a cache backend
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_configuration.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2012 Helge Funk <helge.funk@e-net.info>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Handle loading and writing of global and local (instance specific)
27 * configuration.
28 *
29 * This class handles the access to the files
30 * - t3lib/stddb/DefaultConfiguration.php (default TYPO3_CONF_VARS)
31 * - typo3conf/LocalConfiguration.php (overrides of TYPO3_CONF_VARS)
32 * - typo3conf/AdditionalConfiguration.php (optional additional local code blocks)
33 * - typo3conf/localconf.php (legacy configuration file)
34 *
35 * @package TYPO3
36 * @subpackage t3lib
37 * @author Helge Funk <helge.funk@e-net.info>
38 */
39 class t3lib_Configuration {
40
41 /**
42 * Path to default TYPO3_CONF_VARS file, relative to PATH_site
43 */
44 const DEFAULT_CONFIGURATION_FILE = 't3lib/stddb/DefaultConfiguration.php';
45
46 /**
47 * Path to local overload TYPO3_CONF_VARS file, relative to PATH_site
48 */
49 const LOCAL_CONFIGURATION_FILE = 'typo3conf/LocalConfiguration.php';
50
51 /**
52 * Path to additional local file, relative to PATH_site
53 */
54 const ADDITIONAL_CONFIGURATION_FILE = 'typo3conf/AdditionalConfiguration.php';
55
56 /**
57 * Path to legacy localconf.php file, relative to PATH_site
58 */
59 const LOCALCONF_FILE = 'typo3conf/localconf.php';
60
61 /**
62 * Writing to these configuration pathes is always allowed,
63 * even if the requested sub path does not exist yet.
64 *
65 * @var array
66 */
67 protected static $whiteListedLocalConfigurationPaths = array(
68 'EXT/extConf',
69 'EXTCONF',
70 'INSTALL/wizardDone',
71 'DB',
72 );
73
74 /**
75 * Return default configuration array t3lib/stddb/DefaultConfiguration.php
76 *
77 * @return array
78 */
79 public static function getDefaultConfiguration() {
80 return require(PATH_site . static::DEFAULT_CONFIGURATION_FILE);
81 }
82
83 /**
84 * Return local configuration array typo3conf/LocalConfiguration.php
85 *
86 * @return array Content array of local configuration file
87 */
88 public static function getLocalConfiguration() {
89 return require(PATH_site . static::LOCAL_CONFIGURATION_FILE);
90 }
91
92 /**
93 * Override local configuration with new values.
94 *
95 * @param array $configurationToMerge Override configuration array
96 * @return void
97 */
98 public static function updateLocalConfiguration(array $configurationToMerge) {
99 $newLocalConfiguration = t3lib_div::array_merge_recursive_overrule(
100 static::getLocalConfiguration(),
101 $configurationToMerge
102 );
103 static::writeLocalConfiguration($newLocalConfiguration);
104 }
105
106 /**
107 * Get a value at given path from default configuration
108 *
109 * @param string $path Path to search for
110 * @return mixed Value at path
111 */
112 public static function getDefaultConfigurationValueByPath($path) {
113 return t3lib_utility_Array::getValueByPath(
114 static::getDefaultConfiguration(),
115 $path
116 );
117 }
118
119 /**
120 * Get a value at given path from local configuration
121 *
122 * @param string $path Path to search for
123 * @return mixed Value at path
124 */
125 public static function getLocalConfigurationValueByPath($path) {
126 return t3lib_utility_Array::getValueByPath(
127 static::getLocalConfiguration(),
128 $path
129 );
130 }
131
132 /**
133 * Get a value from configuration, this is default configuration
134 * merged with local configuration
135 *
136 * @param string $path Path to search for
137 * @return mixed
138 */
139 public static function getConfigurationValueByPath($path) {
140 return t3lib_utility_Array::getValueByPath(
141 t3lib_div::array_merge_recursive_overrule(
142 static::getDefaultConfiguration(),
143 static::getLocalConfiguration()
144 ),
145 $path
146 );
147 }
148
149 /**
150 * Update a given path in local configuration to a new value.
151 *
152 * @param string $path Path to update
153 * @param mixed $value Value to set
154 * @return boolean TRUE on success
155 */
156 public static function setLocalConfigurationValueByPath($path, $value) {
157 $result = FALSE;
158 if (static::isValidLocalConfigurationPath($path)) {
159 $localConfiguration = static::getLocalConfiguration();
160 $localConfiguration = t3lib_utility_Array::setValueByPath(
161 $localConfiguration,
162 $path,
163 $value
164 );
165 $result = static::writeLocalConfiguration($localConfiguration);
166 }
167 return $result;
168 }
169
170 /**
171 * Update / set a list of path and value pairs in local configuration file
172 *
173 * @param array $pairs Key is path, value is value to set
174 * @return boolean TRUE on success
175 */
176 public static function setLocalConfigurationValuesByPathValuePairs(array $pairs) {
177 $localConfiguration = static::getLocalConfiguration();
178 foreach ($pairs as $path => $value) {
179 if (static::isValidLocalConfigurationPath($path)) {
180 $localConfiguration = t3lib_utility_Array::setValueByPath(
181 $localConfiguration,
182 $path,
183 $value
184 );
185 }
186 }
187 return static::writeLocalConfiguration($localConfiguration);
188 }
189
190 /**
191 * Write local configuration array to typo3conf/LocalConfiguration.php
192 *
193 * @param array $configuration The local configuration to be written
194 * @return boolean TRUE on success
195 */
196 protected static function writeLocalConfiguration(array $configuration) {
197 $configuration = t3lib_utility_Array::sortByKeyRecursive($configuration);
198 $result = t3lib_div::writeFile(
199 PATH_site . static::LOCAL_CONFIGURATION_FILE,
200 '<?php' . LF . 'return ' . t3lib_utility_Array::arrayExport($configuration) . ';' . LF . '?>'
201 );
202 return ($result === FALSE) ? FALSE : TRUE;
203 }
204
205 /**
206 * Check if access / write to given path in local configuration is allowed.
207 *
208 * @param string $path Path to search for
209 * @return boolean TRUE if access is allowed
210 */
211 protected static function isValidLocalConfigurationPath($path) {
212 // Early return for white listed paths
213 foreach (static::$whiteListedLocalConfigurationPaths as $whiteListedPath) {
214 if (t3lib_div::isFirstPartOfStr($path, $whiteListedPath)) {
215 return TRUE;
216 }
217 }
218 return t3lib_utility_Array::isValidPath(
219 static::getDefaultConfiguration(),
220 $path
221 );
222 }
223 }
224 ?>