Merge branch 'QA_3_3'
[phpmyadmin/dkf.git] / setup / lib / ConfigFile.class.php
blobe7e7b288f866c6a978daa02b08914acd6b27422e
1 <?php
2 /**
3 * Config file management and generation
5 * @license http://www.gnu.org/licenses/gpl.html GNU GPL 2.0
6 * @version $Id$
7 * @package phpMyAdmin-setup
8 */
10 /**
11 * Config file management and generation class
13 * @package phpMyAdmin-setup
15 class ConfigFile
17 /**
18 * Stores default PMA config from config.default.php
19 * @var array
21 private $cfg;
23 /**
24 * Stores allowed values for non-standard fields
25 * @var array
27 private $cfgDb;
29 /**
30 * Keys which will be always written to config file
31 * @var array
33 private $persistKeys;
35 /**
36 * ConfigFile instance
37 * @var ConfigFile
39 private static $_instance;
41 /**
42 * Private constructor, use {@link getInstance()}
44 private function __construct()
46 // load default config values
47 $cfg = &$this->cfg;
48 require './libraries/config.default.php';
50 // load additionsl config information
51 $cfg_db = &$this->cfgDb;
52 $persist_keys = array();
53 require './setup/lib/config_info.inc.php';
55 // apply default values overrides
56 if (count($cfg_db['_overrides'])) {
57 foreach ($cfg_db['_overrides'] as $path => $value) {
58 array_write($path, $cfg, $value);
62 // checking key presence is much faster than searching so move values to keys
63 $this->persistKeys = array_flip($persist_keys);
66 /**
67 * Returns class instance
69 * @return ConfigFile
71 public static function getInstance()
73 if (is_null(self::$_instance)) {
74 self::$_instance = new ConfigFile();
76 return self::$_instance;
79 /**
80 * Sets config value
82 * @param string $path
83 * @param mixed $value
84 * @param string $canonical_path
86 public function set($path, $value, $canonical_path = null)
88 if ($canonical_path === null) {
89 $canonical_path = $this->getCanonicalPath($path);
91 // remove if the path isn't protected and it's empty or has a default value
92 $default_value = $this->getDefault($canonical_path);
93 if (!isset($this->persistKeys[$canonical_path])
94 && (($value == $default_value) || (empty($value) && empty($default_value)))) {
95 array_remove($path, $_SESSION['ConfigFile']);
96 } else {
97 array_write($path, $_SESSION['ConfigFile'], $value);
102 * Returns config value or $default if it's not set
104 * @param string $path
105 * @param mixed $default
106 * @return mixed
108 public function get($path, $default = null)
110 return array_read($path, $_SESSION['ConfigFile'], $default);
114 * Returns default config value or $default it it's not set ie. it doesn't
115 * exist in config.default.php ($cfg) and config_info.inc.php
116 * ($_cfg_db['_overrides'])
118 * @param string $canonical_path
119 * @param mixed $default
120 * @return mixed
122 public function getDefault($canonical_path, $default = null)
124 return array_read($canonical_path, $this->cfg, $default);
128 * Returns config value, if it's not set uses the default one; returns
129 * $default if the path isn't set and doesn't contain a default value
131 * @param string $path
132 * @param mixed $default
133 * @return mixed
135 public function getValue($path, $default = null)
137 $v = array_read($path, $_SESSION['ConfigFile'], null);
138 if ($v !== null) {
139 return $v;
141 $path = $this->getCanonicalPath($path);
142 return $this->getDefault($path, $default);
146 * Returns canonical path
148 * @param string $path
149 * @return string
151 public function getCanonicalPath($path) {
152 return preg_replace('#^Servers/([\d]+)/#', 'Servers/1/', $path);
156 * Returns config database entry for $path ($cfg_db in config_info.php)
158 * @param string $path
159 * @param mixed $default
160 * @return mixed
162 public function getDbEntry($path, $default = null)
164 return array_read($path, $this->cfgDb, $default);
168 * Returns server count
170 * @return int
172 public function getServerCount()
174 return isset($_SESSION['ConfigFile']['Servers'])
175 ? count($_SESSION['ConfigFile']['Servers'])
176 : 0;
180 * Returns DSN of given server
182 * @param integer $server
183 * @return string
185 function getServerDSN($server)
187 if (!isset($_SESSION['ConfigFile']['Servers'][$server])) {
188 return '';
191 $path = 'Servers/' . $server;
192 $dsn = $this->getValue("$path/extension") . '://';
193 if ($this->getValue("$path/auth_type") == 'config') {
194 $dsn .= $this->getValue("$path/user");
195 if (!$this->getValue("$path/nopassword")) {
196 $dsn .= ':***';
198 $dsn .= '@';
200 if ($this->getValue("$path/connect_type") == 'tcp') {
201 $dsn .= $this->getValue("$path/host");
202 $port = $this->getValue("$path/port");
203 if ($port) {
204 $dsn .= ':' . $port;
206 } else {
207 $dsn .= $this->getValue("$path/socket");
209 return $dsn;
213 * Returns server name
215 * @param int $id
216 * @return string
218 public function getServerName($id)
220 if (!isset($_SESSION['ConfigFile']['Servers'][$id])) {
221 return '';
223 $verbose = $this->get("Servers/$id/verbose");
224 if (!empty($verbose)) {
225 return $verbose;
227 $host = $this->get("Servers/$id/host");
228 return empty($host) ? 'localhost' : $host;
232 * Removes server
234 * @param int $server
236 public function removeServer($server)
238 if (!isset($_SESSION['ConfigFile']['Servers'][$server])) {
239 return;
241 $last_server = $this->getServerCount();
243 for ($i = $server; $i < $last_server; $i++) {
244 $_SESSION['ConfigFile']['Servers'][$i] = $_SESSION['ConfigFile']['Servers'][$i+1];
246 unset($_SESSION['ConfigFile']['Servers'][$last_server]);
248 if (isset($_SESSION['ConfigFile']['ServerDefault'])
249 && $_SESSION['ConfigFile']['ServerDefault'] >= 0) {
250 unset($_SESSION['ConfigFile']['ServerDefault']);
255 * Returns config file path
257 * @return unknown
259 public function getFilePath()
261 return $this->getDbEntry('_config_file_path');
265 * Creates config file
267 * @return string
269 public function getConfigFile()
271 $crlf = (isset($_SESSION['eol']) && $_SESSION['eol'] == 'win') ? "\r\n" : "\n";
272 $c = $_SESSION['ConfigFile'];
274 // header
275 $ret = '<?php' . $crlf
276 . '/*' . $crlf
277 . ' * Generated configuration file' . $crlf
278 . ' * Generated by: phpMyAdmin '
279 . $GLOBALS['PMA_Config']->get('PMA_VERSION')
280 . ' setup script by Piotr Przybylski <piotrprz@gmail.com>' . $crlf
281 . ' * Date: ' . date(DATE_RFC1123) . $crlf
282 . ' */' . $crlf . $crlf;
284 // servers
285 if ($this->getServerCount() > 0) {
286 $ret .= "/* Servers configuration */$crlf\$i = 0;" . $crlf . $crlf;
287 foreach ($c['Servers'] as $id => $server) {
288 $ret .= '/* Server: ' . strtr($this->getServerName($id), '*/', '-') . " [$id] */" . $crlf
289 . '$i++;' . $crlf;
290 foreach ($server as $k => $v) {
291 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
292 $ret .= "\$cfg['Servers'][\$i]['$k'] = "
293 . var_export($v, true) . ';' . $crlf;
295 $ret .= $crlf;
297 $ret .= '/* End of servers configuration */' . $crlf . $crlf;
299 unset($c['Servers']);
301 // other settings
302 $persistKeys = $this->persistKeys;
303 foreach ($c as $k => $v) {
304 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
305 $ret .= "\$cfg['$k'] = " . var_export($v, true) . ';' . $crlf;
306 if (isset($persistKeys[$k])) {
307 unset($persistKeys[$k]);
310 // keep 1d array keys which are present in $persist_keys (config_info.inc.php)
311 foreach (array_keys($persistKeys) as $k) {
312 if (strpos($k, '/') === false) {
313 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
314 $ret .= "\$cfg['$k'] = " . var_export($this->getDefault($k), true) . ';' . $crlf;
317 $ret .= '?>';
319 return $ret;