3 * Config file management and generation class
5 * @package phpMyAdmin-setup
6 * @author Piotr Przybylski <piotrprz@gmail.com>
7 * @license http://www.gnu.org/licenses/gpl.html GNU GPL 2.0
13 * Stores default PMA config from config.default.php
19 * Stores allowed values for non-standard fields
25 * Keys which will be always written to config file
34 private static $_instance;
37 * Private constructor, use {@link getInstance()}
39 private function __construct()
41 // load default config values
43 require './libraries/config.default.php';
45 // load additionsl config information
46 $cfg_db = &$this->cfgDb
;
47 $persist_keys = array();
48 require './setup/lib/config_info.inc.php';
50 // apply default values overrides
51 if (count($cfg_db['_overrides'])) {
52 foreach ($cfg_db['_overrides'] as $path => $value) {
53 array_write($path, $cfg, $value);
57 // checking key presence is much faster than searching so move values to keys
58 $this->persistKeys
= array_flip($persist_keys);
62 * Returns class instance
66 public static function getInstance()
68 if (is_null(self
::$_instance)) {
69 self
::$_instance = new ConfigFile();
71 return self
::$_instance;
79 * @param string $canonical_path
81 public function set($path, $value, $canonical_path = null)
83 if ($canonical_path === null) {
84 $canonical_path = $this->getCanonicalPath($path);
86 // remove if the path isn't protected and it's empty or has a default value
87 $default_value = $this->getDefault($canonical_path);
88 if (!isset($this->persistKeys
[$canonical_path])
89 && (($value == $default_value) ||
(empty($value) && empty($default_value)))) {
90 array_remove($path, $_SESSION['ConfigFile']);
92 array_write($path, $_SESSION['ConfigFile'], $value);
97 * Returns config value or $default if it's not set
100 * @param mixed $default
103 public function get($path, $default = null)
105 return array_read($path, $_SESSION['ConfigFile'], $default);
109 * Returns default config value or $default it it's not set ie. it doesn't
110 * exist in config.default.php ($cfg) and config_info.inc.php
111 * ($_cfg_db['_overrides'])
113 * @param string $canonical_path
114 * @param mixed $default
117 public function getDefault($canonical_path, $default = null)
119 return array_read($canonical_path, $this->cfg
, $default);
123 * Returns config value, if it's not set uses the default one; returns
124 * $default if the path isn't set and doesn't contain a default value
126 * @param string $path
127 * @param mixed $default
130 public function getValue($path, $default = null)
132 $v = array_read($path, $_SESSION['ConfigFile'], null);
136 $path = $this->getCanonicalPath($path);
137 return $this->getDefault($path, $default);
141 * Returns canonical path
143 * @param string $path
146 public function getCanonicalPath($path) {
147 return preg_replace('#^Servers/([\d]+)/#', 'Servers/1/', $path);
151 * Returns config database entry for $path ($cfg_db in config_info.php)
153 * @param string $path
154 * @param mixed $default
157 public function getDbEntry($path, $default = null)
159 return array_read($path, $this->cfgDb
, $default);
163 * Returns server count
167 public function getServerCount()
169 return isset($_SESSION['ConfigFile']['Servers'])
170 ?
count($_SESSION['ConfigFile']['Servers'])
175 * Returns DSN of given server
177 * @param integer $server
180 function getServerDSN($server)
182 if (!isset($_SESSION['ConfigFile']['Servers'][$server])) {
186 $path = 'Servers/' . $server;
187 $dsn = $this->getValue("$path/extension") . '://';
188 if ($this->getValue("$path/auth_type") == 'config') {
189 $dsn .= $this->getValue("$path/user");
190 if (!$this->getValue("$path/nopassword")) {
195 if ($this->getValue("$path/connect_type") == 'tcp') {
196 $dsn .= $this->getValue("$path/host");
197 $port = $this->getValue("$path/port");
202 $dsn .= $this->getValue("$path/socket");
208 * Returns server name
213 public function getServerName($id)
215 if (!isset($_SESSION['ConfigFile']['Servers'][$id])) {
218 $verbose = $this->get("Servers/$id/verbose");
219 if (!empty($verbose)) {
222 $host = $this->get("Servers/$id/host");
223 return empty($host) ?
'localhost' : $host;
231 public function removeServer($server)
233 if (!isset($_SESSION['ConfigFile']['Servers'][$server])) {
236 $last_server = $this->getServerCount();
238 for ($i = $server; $i < $last_server; $i++
) {
239 $_SESSION['ConfigFile']['Servers'][$i] = $_SESSION['ConfigFile']['Servers'][$i+
1];
241 unset($_SESSION['ConfigFile']['Servers'][$last_server]);
243 if (isset($_SESSION['ConfigFile']['ServerDefault'])
244 && $_SESSION['ConfigFile']['ServerDefault'] >= 0) {
245 unset($_SESSION['ConfigFile']['ServerDefault']);
250 * Returns config file path
254 public function getFilePath()
256 return $this->getDbEntry('_config_file_path');
260 * Creates config file
264 public function getConfigFile()
266 $crlf = (isset($_SESSION['eol']) && $_SESSION['eol'] == 'win') ?
"\r\n" : "\n";
267 $c = $_SESSION['ConfigFile'];
270 $ret = '<?php' . $crlf
272 . ' * Generated configuration file' . $crlf
273 . ' * Generated by: phpMyAdmin '
274 . $_SESSION['PMA_Config']->get('PMA_VERSION')
275 . ' setup script by Piotr Przybylski <piotrprz@gmail.com>' . $crlf
276 . ' * Date: ' . date(DATE_RFC1123
) . $crlf
277 . ' */' . $crlf . $crlf;
280 if ($this->getServerCount() > 0) {
281 $ret .= "/* Servers configuration */$crlf\$i = 0;" . $crlf . $crlf;
282 foreach ($c['Servers'] as $id => $server) {
283 $ret .= '/* Server: ' . $this->getServerName($id) . " [$id] */" . $crlf
285 foreach ($server as $k => $v) {
286 $ret .= "\$cfg['Servers'][\$i]['$k'] = "
287 . var_export($v, true) . ';' . $crlf;
291 $ret .= '/* End of servers configuration */' . $crlf . $crlf;
293 unset($c['Servers']);
296 $persistKeys = $this->persistKeys
;
297 foreach ($c as $k => $v) {
298 $ret .= "\$cfg['$k'] = " . var_export($v, true) . ';' . $crlf;
299 if (isset($persistKeys[$k])) {
300 unset($persistKeys[$k]);
303 // keep 1d array keys which are present in $persist_keys (config_info.inc.php)
304 foreach (array_keys($persistKeys) as $k) {
305 if (strpos($k, '/') === false) {
306 $ret .= "\$cfg['$k'] = " . var_export($this->getDefault($k), true) . ';' . $crlf;