3.3.1 release
[phpmyadmin/dkf.git] / setup / lib / ConfigFile.class.php
blobc0ff63daf0b3adc84f6dc8460fabe1a3a5c15d2c
1 <?php
2 /**
3 * Config file management and generation
5 * @author Piotr Przybylski <piotrprz@gmail.com>
6 * @license http://www.gnu.org/licenses/gpl.html GNU GPL 2.0
7 * @version $Id$
8 * @package phpMyAdmin-setup
9 */
11 /**
12 * Config file management and generation class
14 * @package phpMyAdmin-setup
16 class ConfigFile
18 /**
19 * Stores default PMA config from config.default.php
20 * @var array
22 private $cfg;
24 /**
25 * Stores allowed values for non-standard fields
26 * @var array
28 private $cfgDb;
30 /**
31 * Keys which will be always written to config file
32 * @var array
34 private $persistKeys;
36 /**
37 * ConfigFile instance
38 * @var ConfigFile
40 private static $_instance;
42 /**
43 * Private constructor, use {@link getInstance()}
45 private function __construct()
47 // load default config values
48 $cfg = &$this->cfg;
49 require './libraries/config.default.php';
51 // load additionsl config information
52 $cfg_db = &$this->cfgDb;
53 $persist_keys = array();
54 require './setup/lib/config_info.inc.php';
56 // apply default values overrides
57 if (count($cfg_db['_overrides'])) {
58 foreach ($cfg_db['_overrides'] as $path => $value) {
59 array_write($path, $cfg, $value);
63 // checking key presence is much faster than searching so move values to keys
64 $this->persistKeys = array_flip($persist_keys);
67 /**
68 * Returns class instance
70 * @return ConfigFile
72 public static function getInstance()
74 if (is_null(self::$_instance)) {
75 self::$_instance = new ConfigFile();
77 return self::$_instance;
80 /**
81 * Sets config value
83 * @param string $path
84 * @param mixed $value
85 * @param string $canonical_path
87 public function set($path, $value, $canonical_path = null)
89 if ($canonical_path === null) {
90 $canonical_path = $this->getCanonicalPath($path);
92 // remove if the path isn't protected and it's empty or has a default value
93 $default_value = $this->getDefault($canonical_path);
94 if (!isset($this->persistKeys[$canonical_path])
95 && (($value == $default_value) || (empty($value) && empty($default_value)))) {
96 array_remove($path, $_SESSION['ConfigFile']);
97 } else {
98 array_write($path, $_SESSION['ConfigFile'], $value);
103 * Returns config value or $default if it's not set
105 * @param string $path
106 * @param mixed $default
107 * @return mixed
109 public function get($path, $default = null)
111 return array_read($path, $_SESSION['ConfigFile'], $default);
115 * Returns default config value or $default it it's not set ie. it doesn't
116 * exist in config.default.php ($cfg) and config_info.inc.php
117 * ($_cfg_db['_overrides'])
119 * @param string $canonical_path
120 * @param mixed $default
121 * @return mixed
123 public function getDefault($canonical_path, $default = null)
125 return array_read($canonical_path, $this->cfg, $default);
129 * Returns config value, if it's not set uses the default one; returns
130 * $default if the path isn't set and doesn't contain a default value
132 * @param string $path
133 * @param mixed $default
134 * @return mixed
136 public function getValue($path, $default = null)
138 $v = array_read($path, $_SESSION['ConfigFile'], null);
139 if ($v !== null) {
140 return $v;
142 $path = $this->getCanonicalPath($path);
143 return $this->getDefault($path, $default);
147 * Returns canonical path
149 * @param string $path
150 * @return string
152 public function getCanonicalPath($path) {
153 return preg_replace('#^Servers/([\d]+)/#', 'Servers/1/', $path);
157 * Returns config database entry for $path ($cfg_db in config_info.php)
159 * @param string $path
160 * @param mixed $default
161 * @return mixed
163 public function getDbEntry($path, $default = null)
165 return array_read($path, $this->cfgDb, $default);
169 * Returns server count
171 * @return int
173 public function getServerCount()
175 return isset($_SESSION['ConfigFile']['Servers'])
176 ? count($_SESSION['ConfigFile']['Servers'])
177 : 0;
181 * Returns DSN of given server
183 * @param integer $server
184 * @return string
186 function getServerDSN($server)
188 if (!isset($_SESSION['ConfigFile']['Servers'][$server])) {
189 return '';
192 $path = 'Servers/' . $server;
193 $dsn = $this->getValue("$path/extension") . '://';
194 if ($this->getValue("$path/auth_type") == 'config') {
195 $dsn .= $this->getValue("$path/user");
196 if (!$this->getValue("$path/nopassword")) {
197 $dsn .= ':***';
199 $dsn .= '@';
201 if ($this->getValue("$path/connect_type") == 'tcp') {
202 $dsn .= $this->getValue("$path/host");
203 $port = $this->getValue("$path/port");
204 if ($port) {
205 $dsn .= ':' . $port;
207 } else {
208 $dsn .= $this->getValue("$path/socket");
210 return $dsn;
214 * Returns server name
216 * @param int $id
217 * @return string
219 public function getServerName($id)
221 if (!isset($_SESSION['ConfigFile']['Servers'][$id])) {
222 return '';
224 $verbose = $this->get("Servers/$id/verbose");
225 if (!empty($verbose)) {
226 return $verbose;
228 $host = $this->get("Servers/$id/host");
229 return empty($host) ? 'localhost' : $host;
233 * Removes server
235 * @param int $server
237 public function removeServer($server)
239 if (!isset($_SESSION['ConfigFile']['Servers'][$server])) {
240 return;
242 $last_server = $this->getServerCount();
244 for ($i = $server; $i < $last_server; $i++) {
245 $_SESSION['ConfigFile']['Servers'][$i] = $_SESSION['ConfigFile']['Servers'][$i+1];
247 unset($_SESSION['ConfigFile']['Servers'][$last_server]);
249 if (isset($_SESSION['ConfigFile']['ServerDefault'])
250 && $_SESSION['ConfigFile']['ServerDefault'] >= 0) {
251 unset($_SESSION['ConfigFile']['ServerDefault']);
256 * Returns config file path
258 * @return unknown
260 public function getFilePath()
262 return $this->getDbEntry('_config_file_path');
266 * Creates config file
268 * @return string
270 public function getConfigFile()
272 $crlf = (isset($_SESSION['eol']) && $_SESSION['eol'] == 'win') ? "\r\n" : "\n";
273 $c = $_SESSION['ConfigFile'];
275 // header
276 $ret = '<?php' . $crlf
277 . '/*' . $crlf
278 . ' * Generated configuration file' . $crlf
279 . ' * Generated by: phpMyAdmin '
280 . $_SESSION['PMA_Config']->get('PMA_VERSION')
281 . ' setup script by Piotr Przybylski <piotrprz@gmail.com>' . $crlf
282 . ' * Date: ' . date(DATE_RFC1123) . $crlf
283 . ' */' . $crlf . $crlf;
285 // servers
286 if ($this->getServerCount() > 0) {
287 $ret .= "/* Servers configuration */$crlf\$i = 0;" . $crlf . $crlf;
288 foreach ($c['Servers'] as $id => $server) {
289 $ret .= '/* Server: ' . strtr($this->getServerName($id), '*/', '-') . " [$id] */" . $crlf
290 . '$i++;' . $crlf;
291 foreach ($server as $k => $v) {
292 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
293 $ret .= "\$cfg['Servers'][\$i]['$k'] = "
294 . var_export($v, true) . ';' . $crlf;
296 $ret .= $crlf;
298 $ret .= '/* End of servers configuration */' . $crlf . $crlf;
300 unset($c['Servers']);
302 // other settings
303 $persistKeys = $this->persistKeys;
304 foreach ($c as $k => $v) {
305 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
306 $ret .= "\$cfg['$k'] = " . var_export($v, true) . ';' . $crlf;
307 if (isset($persistKeys[$k])) {
308 unset($persistKeys[$k]);
311 // keep 1d array keys which are present in $persist_keys (config_info.inc.php)
312 foreach (array_keys($persistKeys) as $k) {
313 if (strpos($k, '/') === false) {
314 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
315 $ret .= "\$cfg['$k'] = " . var_export($this->getDefault($k), true) . ';' . $crlf;
318 $ret .= '?>';
320 return $ret;