BLOB streaming documentation
[phpmyadmin/crack.git] / setup / lib / ConfigFile.class.php
bloba8a1c244b6d5a150e5f0f597f46c380b8e48b08b
1 <?php
2 /**
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
8 * @version $Id$
9 */
10 class ConfigFile
12 /**
13 * Stores default PMA config from config.default.php
14 * @var array
16 private $cfg;
18 /**
19 * Stores allowed values for non-standard fields
20 * @var array
22 private $cfgDb;
24 /**
25 * Keys which will be always written to config file
26 * @var array
28 private $persistKeys;
30 /**
31 * ConfigFile instance
32 * @var ConfigFile
34 private static $_instance;
36 /**
37 * Private constructor, use {@link getInstance()}
39 private function __construct()
41 // load default config values
42 $cfg = &$this->cfg;
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);
61 /**
62 * Returns class instance
64 * @return ConfigFile
66 public static function getInstance()
68 if (is_null(self::$_instance)) {
69 self::$_instance = new ConfigFile();
71 return self::$_instance;
74 /**
75 * Sets config value
77 * @param string $path
78 * @param mixed $value
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']);
91 } else {
92 array_write($path, $_SESSION['ConfigFile'], $value);
96 /**
97 * Returns config value or $default if it's not set
99 * @param string $path
100 * @param mixed $default
101 * @return mixed
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
115 * @return mixed
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
128 * @return mixed
130 public function getValue($path, $default = null)
132 $v = array_read($path, $_SESSION['ConfigFile'], null);
133 if ($v !== null) {
134 return $v;
136 $path = $this->getCanonicalPath($path);
137 return $this->getDefault($path, $default);
141 * Returns canonical path
143 * @param string $path
144 * @return string
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
155 * @return mixed
157 public function getDbEntry($path, $default = null)
159 return array_read($path, $this->cfgDb, $default);
163 * Returns server count
165 * @return int
167 public function getServerCount()
169 return isset($_SESSION['ConfigFile']['Servers'])
170 ? count($_SESSION['ConfigFile']['Servers'])
171 : 0;
175 * Returns DSN of given server
177 * @param integer $server
178 * @return string
180 function getServerDSN($server)
182 if (!isset($_SESSION['ConfigFile']['Servers'][$server])) {
183 return '';
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")) {
191 $dsn .= ':***';
193 $dsn .= '@';
195 if ($this->getValue("$path/connect_type") == 'tcp') {
196 $dsn .= $this->getValue("$path/host");
197 $port = $this->getValue("$path/port");
198 if ($port) {
199 $dsn .= ':' . $port;
201 } else {
202 $dsn .= $this->getValue("$path/socket");
204 return $dsn;
208 * Returns server name
210 * @param int $id
211 * @return string
213 public function getServerName($id)
215 if (!isset($_SESSION['ConfigFile']['Servers'][$id])) {
216 return '';
218 $verbose = $this->get("Servers/$id/verbose");
219 if (!empty($verbose)) {
220 return $verbose;
222 $host = $this->get("Servers/$id/host");
223 return empty($host) ? 'localhost' : $host;
227 * Removes server
229 * @param int $server
231 public function removeServer($server)
233 if (!isset($_SESSION['ConfigFile']['Servers'][$server])) {
234 return;
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
252 * @return unknown
254 public function getFilePath()
256 return $this->getDbEntry('_config_file_path');
260 * Creates config file
262 * @return string
264 public function getConfigFile()
266 $crlf = (isset($_SESSION['eol']) && $_SESSION['eol'] == 'win') ? "\r\n" : "\n";
267 $c = $_SESSION['ConfigFile'];
269 // header
270 $ret = '<?php' . $crlf
271 . '/*' . $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;
279 // servers
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
284 . '$i++;' . $crlf;
285 foreach ($server as $k => $v) {
286 $ret .= "\$cfg['Servers'][\$i]['$k'] = "
287 . var_export($v, true) . ';' . $crlf;
289 $ret .= $crlf;
291 $ret .= '/* End of servers configuration */' . $crlf . $crlf;
293 unset($c['Servers']);
295 // other settings
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;
309 $ret .= '?>';
311 return $ret;