[BUGFIX] stdWrap numRows fails due to wrong SELECT clause
[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 'INSTALL/wizardDone'
70 );
71
72 /**
73 * Return default configuration array t3lib/stddb/DefaultConfiguration.php
74 *
75 * @return array
76 */
77 public static function getDefaultConfiguration() {
78 return require(PATH_site . static::DEFAULT_CONFIGURATION_FILE);
79 }
80
81 /**
82 * Return local configuration array typo3conf/LocalConfiguration.php
83 *
84 * @return array Content array of local configuration file
85 */
86 public static function getLocalConfiguration() {
87 return require(PATH_site . static::LOCAL_CONFIGURATION_FILE);
88 }
89
90 /**
91 * Override local configuration with new values.
92 *
93 * @param array $configurationToMerge Override configuration array
94 * @return void
95 */
96 public static function updateLocalConfiguration(array $configurationToMerge) {
97 $newLocalConfiguration = t3lib_div::array_merge_recursive_overrule(
98 static::getLocalConfiguration(),
99 $configurationToMerge
100 );
101 static::writeLocalConfiguration($newLocalConfiguration);
102 }
103
104 /**
105 * Get a value at given path from default configuration
106 *
107 * @param string $path Path to search for
108 * @return mixed Value at path
109 */
110 public static function getDefaultConfigurationValueByPath($path) {
111 return t3lib_utility_Array::getValueByPath(
112 static::getDefaultConfiguration(),
113 $path
114 );
115 }
116
117 /**
118 * Get a value at given path from local configuration
119 *
120 * @param string $path Path to search for
121 * @return mixed Value at path
122 */
123 public static function getLocalConfigurationValueByPath($path) {
124 return t3lib_utility_Array::getValueByPath(
125 static::getLocalConfiguration(),
126 $path
127 );
128 }
129
130 /**
131 * Get a value from configuration, this is default configuration
132 * merged with local configuration
133 *
134 * @param string $path Path to search for
135 * @return mixed
136 */
137 public static function getConfigurationValueByPath($path) {
138 return t3lib_utility_Array::getValueByPath(
139 t3lib_div::array_merge_recursive_overrule(
140 static::getDefaultConfiguration(),
141 static::getLocalConfiguration()
142 ),
143 $path
144 );
145 }
146
147 /**
148 * Update a given path in local configuration to a new value.
149 *
150 * @param string $path Path to update
151 * @param mixed $value Value to set
152 * @return boolean TRUE on success
153 */
154 public static function setLocalConfigurationValueByPath($path, $value) {
155 $result = FALSE;
156 if (static::isValidLocalConfigurationPath($path)) {
157 $localConfiguration = static::getLocalConfiguration();
158 $localConfiguration = t3lib_utility_Array::setValueByPath(
159 $localConfiguration,
160 $path,
161 $value
162 );
163 $result = static::writeLocalConfiguration($localConfiguration);
164 }
165 return $result;
166 }
167
168 /**
169 * Update / set a list of path and value pairs in local configuration file
170 *
171 * @param array $pairs Key is path, value is value to set
172 * @return boolean TRUE on success
173 */
174 public static function setLocalConfigurationValuesByPathValuePairs(array $pairs) {
175 $localConfiguration = static::getLocalConfiguration();
176 foreach ($pairs as $path => $value) {
177 if (static::isValidLocalConfigurationPath($path)) {
178 $localConfiguration = t3lib_utility_Array::setValueByPath(
179 $localConfiguration,
180 $path,
181 $value
182 );
183 }
184 }
185 return static::writeLocalConfiguration($localConfiguration);
186 }
187
188 /**
189 * Write local configuration array to typo3conf/LocalConfiguration.php
190 *
191 * @param array $configuration The local configuration to be written
192 * @return boolean TRUE on success
193 */
194 protected static function writeLocalConfiguration(array $configuration) {
195 $configuration = t3lib_utility_Array::sortByKeyRecursive($configuration);
196 $result = t3lib_div::writeFile(
197 PATH_site . static::LOCAL_CONFIGURATION_FILE,
198 '<?php' . LF . 'return ' . t3lib_utility_Array::arrayExport($configuration) . ';' . LF . '?>'
199 );
200 return ($result === FALSE) ? FALSE : TRUE;
201 }
202
203 /**
204 * Check if access / write to given path in local configuration is allowed.
205 *
206 * @param string $path Path to search for
207 * @return boolean TRUE if access is allowed
208 */
209 protected static function isValidLocalConfigurationPath($path) {
210 // Early return for white listed paths
211 foreach (static::$whiteListedLocalConfigurationPaths as $whiteListedPath) {
212 if (t3lib_div::isFirstPartOfStr($path, $whiteListedPath)) {
213 return TRUE;
214 }
215 }
216 return t3lib_utility_Array::isValidPath(
217 static::getDefaultConfiguration(),
218 $path
219 );
220 }
221 }
222 ?>