ConfigFile.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Config file management
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. /**
  9. * Config file management class.
  10. * Stores its data in $_SESSION
  11. *
  12. * @package PhpMyAdmin
  13. */
  14. class ConfigFile
  15. {
  16. /**
  17. * Stores default PMA config from config.default.php
  18. * @var array
  19. */
  20. private $_cfg;
  21. /**
  22. * Stores original PMA_Config object, not modified by user preferences
  23. * @var PMA_Config
  24. */
  25. private $_orgCfgObject;
  26. /**
  27. * Stores allowed values for non-standard fields
  28. * @var array
  29. */
  30. private $_cfgDb;
  31. /**
  32. * Keys which will be always written to config file
  33. * @var array
  34. */
  35. private $_persistKeys = array();
  36. /**
  37. * Changes keys while updating config in {@link updateWithGlobalConfig()}
  38. * or reading by {@link getConfig()} or {@link getConfigArray()}
  39. * @var array
  40. */
  41. private $_cfgUpdateReadMapping = array();
  42. /**
  43. * Key filter for {@link set()}
  44. * @var array|null
  45. */
  46. private $_setFilter;
  47. /**
  48. * Instance id (key in $_SESSION array, separate for each server -
  49. * ConfigFile{server id})
  50. * @var string
  51. */
  52. private $_id;
  53. /**
  54. * Result for {@link _flattenArray()}
  55. * @var array
  56. */
  57. private $_flattenArrayResult;
  58. /**
  59. * ConfigFile instance
  60. * @var ConfigFile
  61. */
  62. private static $_instance;
  63. /**
  64. * Private constructor, use {@link getInstance()}
  65. *
  66. */
  67. private function __construct()
  68. {
  69. // load default config values
  70. $cfg = &$this->_cfg;
  71. include './libraries/config.default.php';
  72. $cfg['fontsize'] = '82%';
  73. // create PMA_Config to read config.inc.php values
  74. $this->_orgCfgObject = new PMA_Config(CONFIG_FILE);
  75. // load additional config information
  76. $cfg_db = &$this->_cfgDb;
  77. include './libraries/config.values.php';
  78. // apply default values overrides
  79. if (count($cfg_db['_overrides'])) {
  80. foreach ($cfg_db['_overrides'] as $path => $value) {
  81. PMA_arrayWrite($path, $cfg, $value);
  82. }
  83. }
  84. $this->_id = 'ConfigFile' . $GLOBALS['server'];
  85. if (!isset($_SESSION[$this->_id])) {
  86. $_SESSION[$this->_id] = array();
  87. }
  88. }
  89. /**
  90. * Returns class instance
  91. *
  92. * @return ConfigFile
  93. */
  94. public static function getInstance()
  95. {
  96. if (is_null(self::$_instance)) {
  97. self::$_instance = new ConfigFile();
  98. }
  99. return self::$_instance;
  100. }
  101. /**
  102. * Returns PMA_Config without user preferences applied
  103. *
  104. * @return PMA_Config
  105. */
  106. public function getOrgConfigObj()
  107. {
  108. return $this->_orgCfgObject;
  109. }
  110. /**
  111. * Sets names of config options which will be placed in config file even if
  112. * they are set to their default values (use only full paths)
  113. *
  114. * @param array $keys
  115. *
  116. * @return void
  117. */
  118. public function setPersistKeys($keys)
  119. {
  120. // checking key presence is much faster than searching so move values
  121. // to keys
  122. $this->_persistKeys = array_flip($keys);
  123. }
  124. /**
  125. * Returns flipped array set by {@link setPersistKeys()}
  126. *
  127. * @return array
  128. */
  129. public function getPersistKeysMap()
  130. {
  131. return $this->_persistKeys;
  132. }
  133. /**
  134. * By default ConfigFile allows setting of all configuration keys, use
  135. * this method to set up a filter on {@link set()} method
  136. *
  137. * @param array|null $keys array of allowed keys or null to remove filter
  138. *
  139. * @return void
  140. */
  141. public function setAllowedKeys($keys)
  142. {
  143. if ($keys === null) {
  144. $this->_setFilter = null;
  145. return;
  146. }
  147. // checking key presence is much faster than searching so move values
  148. // to keys
  149. $this->_setFilter = array_flip($keys);
  150. }
  151. /**
  152. * Sets path mapping for updating config in
  153. * {@link updateWithGlobalConfig()} or reading
  154. * by {@link getConfig()} or {@link getConfigArray()}
  155. *
  156. * @param array $mapping
  157. *
  158. * @return void
  159. */
  160. public function setCfgUpdateReadMapping(array $mapping)
  161. {
  162. $this->_cfgUpdateReadMapping = $mapping;
  163. }
  164. /**
  165. * Resets configuration data
  166. *
  167. * @return void
  168. */
  169. public function resetConfigData()
  170. {
  171. $_SESSION[$this->_id] = array();
  172. }
  173. /**
  174. * Sets configuration data (overrides old data)
  175. *
  176. * @param array $cfg
  177. *
  178. * @return void
  179. */
  180. public function setConfigData(array $cfg)
  181. {
  182. $_SESSION[$this->_id] = $cfg;
  183. }
  184. /**
  185. * Sets config value
  186. *
  187. * @param string $path
  188. * @param mixed $value
  189. * @param string $canonical_path
  190. *
  191. * @return void
  192. */
  193. public function set($path, $value, $canonical_path = null)
  194. {
  195. if ($canonical_path === null) {
  196. $canonical_path = $this->getCanonicalPath($path);
  197. }
  198. // apply key whitelist
  199. if ($this->_setFilter !== null
  200. && ! isset($this->_setFilter[$canonical_path])
  201. ) {
  202. return;
  203. }
  204. // remove if the path isn't protected and it's empty or has a default
  205. // value
  206. if (!isset($this->_persistKeys[$canonical_path])) {
  207. $default_value = $this->getDefault($canonical_path);
  208. // we need original config values not overwritten by user
  209. // preferences to allow for overwriting options set in
  210. // config.inc.php with default values
  211. $instance_default_value = PMA_arrayRead(
  212. $canonical_path,
  213. $this->_orgCfgObject->settings
  214. );
  215. if (($value === $default_value && (defined('PMA_SETUP')
  216. || $instance_default_value === $default_value))
  217. || (empty($value) && empty($default_value) && (defined('PMA_SETUP')))
  218. ) {
  219. PMA_arrayRemove($path, $_SESSION[$this->_id]);
  220. return;
  221. }
  222. }
  223. PMA_arrayWrite($path, $_SESSION[$this->_id], $value);
  224. }
  225. /**
  226. * Flattens multidimensional array, changes indices to paths
  227. * (eg. 'key/subkey').
  228. * Used as array_walk() callback.
  229. *
  230. * @param mixed $value
  231. * @param mixed $key
  232. * @param mixed $prefix
  233. *
  234. * @return void
  235. */
  236. private function _flattenArray($value, $key, $prefix)
  237. {
  238. // no recursion for numeric arrays
  239. if (is_array($value) && !isset($value[0])) {
  240. $prefix .= $key . '/';
  241. array_walk($value, array($this, '_flattenArray'), $prefix);
  242. } else {
  243. $this->_flattenArrayResult[$prefix . $key] = $value;
  244. }
  245. }
  246. /**
  247. * Returns default config in a flattened array
  248. *
  249. * @return array
  250. */
  251. public function getFlatDefaultConfig()
  252. {
  253. $this->_flattenArrayResult = array();
  254. array_walk($this->_cfg, array($this, '_flattenArray'), '');
  255. $flat_cfg = $this->_flattenArrayResult;
  256. $this->_flattenArrayResult = null;
  257. return $flat_cfg;
  258. }
  259. /**
  260. * Updates config with values read from given array
  261. * (config will contain differences to defaults from config.defaults.php).
  262. *
  263. * @param array $cfg
  264. *
  265. * @return void
  266. */
  267. public function updateWithGlobalConfig(array $cfg)
  268. {
  269. // load config array and flatten it
  270. $this->_flattenArrayResult = array();
  271. array_walk($cfg, array($this, '_flattenArray'), '');
  272. $flat_cfg = $this->_flattenArrayResult;
  273. $this->_flattenArrayResult = null;
  274. // save values map for translating a few user preferences paths,
  275. // should be complemented by code reading from generated config
  276. // to perform inverse mapping
  277. foreach ($flat_cfg as $path => $value) {
  278. if (isset($this->_cfgUpdateReadMapping[$path])) {
  279. $path = $this->_cfgUpdateReadMapping[$path];
  280. }
  281. $this->set($path, $value, $path);
  282. }
  283. }
  284. /**
  285. * Returns config value or $default if it's not set
  286. *
  287. * @param string $path
  288. * @param mixed $default
  289. *
  290. * @return mixed
  291. */
  292. public function get($path, $default = null)
  293. {
  294. return PMA_arrayRead($path, $_SESSION[$this->_id], $default);
  295. }
  296. /**
  297. * Returns default config value or $default it it's not set ie. it doesn't
  298. * exist in config.default.php ($cfg) and config.values.php
  299. * ($_cfg_db['_overrides'])
  300. *
  301. * @param string $canonical_path
  302. * @param mixed $default
  303. *
  304. * @return mixed
  305. */
  306. public function getDefault($canonical_path, $default = null)
  307. {
  308. return PMA_arrayRead($canonical_path, $this->_cfg, $default);
  309. }
  310. /**
  311. * Returns config value, if it's not set uses the default one; returns
  312. * $default if the path isn't set and doesn't contain a default value
  313. *
  314. * @param string $path
  315. * @param mixed $default
  316. *
  317. * @return mixed
  318. */
  319. public function getValue($path, $default = null)
  320. {
  321. $v = PMA_arrayRead($path, $_SESSION[$this->_id], null);
  322. if ($v !== null) {
  323. return $v;
  324. }
  325. $path = $this->getCanonicalPath($path);
  326. return $this->getDefault($path, $default);
  327. }
  328. /**
  329. * Returns canonical path
  330. *
  331. * @param string $path
  332. *
  333. * @return string
  334. */
  335. public function getCanonicalPath($path)
  336. {
  337. return preg_replace('#^Servers/([\d]+)/#', 'Servers/1/', $path);
  338. }
  339. /**
  340. * Returns config database entry for $path ($cfg_db in config_info.php)
  341. *
  342. * @param string $path
  343. * @param mixed $default
  344. *
  345. * @return mixed
  346. */
  347. public function getDbEntry($path, $default = null)
  348. {
  349. return PMA_arrayRead($path, $this->_cfgDb, $default);
  350. }
  351. /**
  352. * Returns server count
  353. *
  354. * @return int
  355. */
  356. public function getServerCount()
  357. {
  358. return isset($_SESSION[$this->_id]['Servers'])
  359. ? count($_SESSION[$this->_id]['Servers'])
  360. : 0;
  361. }
  362. /**
  363. * Returns server list
  364. *
  365. * @return array|null
  366. */
  367. public function getServers()
  368. {
  369. return isset($_SESSION[$this->_id]['Servers'])
  370. ? $_SESSION[$this->_id]['Servers']
  371. : null;
  372. }
  373. /**
  374. * Returns DSN of given server
  375. *
  376. * @param integer $server
  377. *
  378. * @return string
  379. */
  380. function getServerDSN($server)
  381. {
  382. if (!isset($_SESSION[$this->_id]['Servers'][$server])) {
  383. return '';
  384. }
  385. $path = 'Servers/' . $server;
  386. $dsn = $this->getValue("$path/extension") . '://';
  387. if ($this->getValue("$path/auth_type") == 'config') {
  388. $dsn .= $this->getValue("$path/user");
  389. if (!$this->getValue("$path/nopassword")) {
  390. $dsn .= ':***';
  391. }
  392. $dsn .= '@';
  393. }
  394. if ($this->getValue("$path/connect_type") == 'tcp') {
  395. $dsn .= $this->getValue("$path/host");
  396. $port = $this->getValue("$path/port");
  397. if ($port) {
  398. $dsn .= ':' . $port;
  399. }
  400. } else {
  401. $dsn .= $this->getValue("$path/socket");
  402. }
  403. return $dsn;
  404. }
  405. /**
  406. * Returns server name
  407. *
  408. * @param int $id
  409. *
  410. * @return string
  411. */
  412. public function getServerName($id)
  413. {
  414. if (!isset($_SESSION[$this->_id]['Servers'][$id])) {
  415. return '';
  416. }
  417. $verbose = $this->get("Servers/$id/verbose");
  418. if (!empty($verbose)) {
  419. return $verbose;
  420. }
  421. $host = $this->get("Servers/$id/host");
  422. return empty($host) ? 'localhost' : $host;
  423. }
  424. /**
  425. * Removes server
  426. *
  427. * @param int $server
  428. *
  429. * @return void
  430. */
  431. public function removeServer($server)
  432. {
  433. if (!isset($_SESSION[$this->_id]['Servers'][$server])) {
  434. return;
  435. }
  436. $last_server = $this->getServerCount();
  437. for ($i = $server; $i < $last_server; $i++) {
  438. $_SESSION[$this->_id]['Servers'][$i]
  439. = $_SESSION[$this->_id]['Servers'][$i + 1];
  440. }
  441. unset($_SESSION[$this->_id]['Servers'][$last_server]);
  442. if (isset($_SESSION[$this->_id]['ServerDefault'])
  443. && $_SESSION[$this->_id]['ServerDefault'] >= 0
  444. ) {
  445. unset($_SESSION[$this->_id]['ServerDefault']);
  446. }
  447. }
  448. /**
  449. * Returns configuration array (full, multidimensional format)
  450. *
  451. * @return array
  452. */
  453. public function getConfig()
  454. {
  455. $c = $_SESSION[$this->_id];
  456. foreach ($this->_cfgUpdateReadMapping as $map_to => $map_from) {
  457. PMA_arrayWrite($map_to, $c, PMA_arrayRead($map_from, $c));
  458. PMA_arrayRemove($map_from, $c);
  459. }
  460. return $c;
  461. }
  462. /**
  463. * Returns configuration array (flat format)
  464. *
  465. * @return array
  466. */
  467. public function getConfigArray()
  468. {
  469. $this->_flattenArrayResult = array();
  470. array_walk($_SESSION[$this->_id], array($this, '_flattenArray'), '');
  471. $c = $this->_flattenArrayResult;
  472. $this->_flattenArrayResult = null;
  473. $persistKeys = array_diff(
  474. array_keys($this->_persistKeys),
  475. array_keys($c)
  476. );
  477. foreach ($persistKeys as $k) {
  478. $c[$k] = $this->getDefault($k);
  479. }
  480. foreach ($this->_cfgUpdateReadMapping as $map_to => $map_from) {
  481. if (!isset($c[$map_from])) {
  482. continue;
  483. }
  484. $c[$map_to] = $c[$map_from];
  485. unset($c[$map_from]);
  486. }
  487. return $c;
  488. }
  489. }
  490. ?>