refactor config file generation out of ConfigFile class
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
<?php
|
||||
/**
|
||||
* Config file management and generation
|
||||
* Config file management
|
||||
*
|
||||
* @license http://www.gnu.org/licenses/gpl.html GNU GPL 2.0
|
||||
* @package phpMyAdmin
|
||||
*/
|
||||
|
||||
/**
|
||||
* Config file management and generation class.
|
||||
* Config file management class.
|
||||
* Stores its data in $_SESSION
|
||||
*
|
||||
* @package phpMyAdmin
|
||||
@@ -110,6 +110,16 @@ class ConfigFile
|
||||
$this->persistKeys = array_flip($keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns flipped array set by {@link setPersistKeys()}
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getPersistKeysMap()
|
||||
{
|
||||
return $this->persistKeys;
|
||||
}
|
||||
|
||||
/**
|
||||
* By default ConfigFile allows setting of all configuration keys, use this method
|
||||
* to set up a filter on {@link set()} method
|
||||
@@ -397,7 +407,7 @@ class ConfigFile
|
||||
/**
|
||||
* Returns config file path, relative to phpMyAdmin's root path
|
||||
*
|
||||
* @return unknown
|
||||
* @return string
|
||||
*/
|
||||
public function getFilePath()
|
||||
{
|
||||
@@ -409,6 +419,16 @@ class ConfigFile
|
||||
return SETUP_CONFIG_FILE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns configuration array (full, multidimensional format)
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getConfig()
|
||||
{
|
||||
return $_SESSION[$this->id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns configuration array (flat format)
|
||||
*
|
||||
@@ -427,137 +447,5 @@ class ConfigFile
|
||||
}
|
||||
return $c;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates config file
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getConfigFile()
|
||||
{
|
||||
$crlf = (isset($_SESSION['eol']) && $_SESSION['eol'] == 'win') ? "\r\n" : "\n";
|
||||
$c = $_SESSION[$this->id];
|
||||
|
||||
// header
|
||||
$ret = '<?php' . $crlf
|
||||
. '/*' . $crlf
|
||||
. ' * Generated configuration file' . $crlf
|
||||
. ' * Generated by: phpMyAdmin '
|
||||
. $GLOBALS['PMA_Config']->get('PMA_VERSION')
|
||||
. ' setup script by Piotr Przybylski <piotrprz@gmail.com>' . $crlf
|
||||
. ' * Date: ' . date(DATE_RFC1123) . $crlf
|
||||
. ' */' . $crlf . $crlf;
|
||||
|
||||
// servers
|
||||
if ($this->getServerCount() > 0) {
|
||||
$ret .= "/* Servers configuration */$crlf\$i = 0;" . $crlf . $crlf;
|
||||
foreach ($c['Servers'] as $id => $server) {
|
||||
$ret .= '/* Server: ' . strtr($this->getServerName($id), '*/', '-') . " [$id] */" . $crlf
|
||||
. '$i++;' . $crlf;
|
||||
foreach ($server as $k => $v) {
|
||||
$k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
|
||||
$ret .= "\$cfg['Servers'][\$i]['$k'] = "
|
||||
. (is_array($v) && $this->_isZeroBasedArray($v)
|
||||
? $this->_exportZeroBasedArray($v, $crlf)
|
||||
: var_export($v, true))
|
||||
. ';' . $crlf;
|
||||
}
|
||||
$ret .= $crlf;
|
||||
}
|
||||
$ret .= '/* End of servers configuration */' . $crlf . $crlf;
|
||||
}
|
||||
unset($c['Servers']);
|
||||
|
||||
// other settings
|
||||
$persistKeys = $this->persistKeys;
|
||||
foreach ($c as $k => $v) {
|
||||
$k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
|
||||
$ret .= $this->_getVarExport($k, $v, $crlf);
|
||||
if (isset($persistKeys[$k])) {
|
||||
unset($persistKeys[$k]);
|
||||
}
|
||||
}
|
||||
// keep 1d array keys which are present in $persist_keys (config.values.php)
|
||||
foreach (array_keys($persistKeys) as $k) {
|
||||
if (strpos($k, '/') === false) {
|
||||
$k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
|
||||
$ret .= $this->_getVarExport($k, $this->getDefault($k), $crlf);
|
||||
}
|
||||
}
|
||||
$ret .= '?>';
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns exported configuration variable
|
||||
*
|
||||
* @param string $var_name
|
||||
* @param mixed $var_value
|
||||
* @param string $crlf
|
||||
* @return string
|
||||
*/
|
||||
private function _getVarExport($var_name, $var_value, $crlf)
|
||||
{
|
||||
if (!is_array($var_value) || empty($var_value)) {
|
||||
return "\$cfg['$var_name'] = " . var_export($var_value, true) . ';' . $crlf;
|
||||
}
|
||||
$ret = '';
|
||||
if ($this->_isZeroBasedArray($var_value)) {
|
||||
$ret = "\$cfg['$var_name'] = " . $this->_exportZeroBasedArray($var_value, $crlf)
|
||||
. ');' . $crlf;
|
||||
} else {
|
||||
// string keys: $cfg[key][subkey] = value
|
||||
foreach ($var_value as $k => $v) {
|
||||
$k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
|
||||
$ret .= "\$cfg['$var_name']['$k'] = " . var_export($v, true) . ';' . $crlf;
|
||||
}
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether $array is a continuous 0-based array
|
||||
*
|
||||
* @param array $array
|
||||
* @return boolean
|
||||
*/
|
||||
private function _isZeroBasedArray(array $array)
|
||||
{
|
||||
for ($i = 0; $i < count($array); $i++) {
|
||||
if (!isset($array[$i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Exports continuous 0-based array
|
||||
*
|
||||
* @param array $array
|
||||
* @param string $crlf
|
||||
* @return string
|
||||
*/
|
||||
private function _exportZeroBasedArray(array $array, $crlf)
|
||||
{
|
||||
$retv = array();
|
||||
foreach ($array as $v) {
|
||||
$retv[] = var_export($v, true);
|
||||
}
|
||||
$ret = "array(";
|
||||
if (count($retv) <= 4) {
|
||||
// up to 4 values - one line
|
||||
$ret .= implode(', ', $retv);
|
||||
} else {
|
||||
// more than 4 values - value per line
|
||||
$imax = count($retv)-1;
|
||||
for ($i = 0; $i <= $imax; $i++) {
|
||||
$ret .= ($i < $imax ? ($i > 0 ? ',' : '') : '') . $crlf . ' ' . $retv[$i];
|
||||
}
|
||||
}
|
||||
$ret .= ')';
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
?>
|
@@ -13,6 +13,7 @@
|
||||
require './lib/common.inc.php';
|
||||
require_once './libraries/config/Form.class.php';
|
||||
require_once './libraries/config/FormDisplay.class.php';
|
||||
require_once './setup/lib/ConfigGenerator.class.php';
|
||||
|
||||
require './libraries/config/setup.forms.php';
|
||||
|
||||
@@ -40,13 +41,13 @@ if (PMA_ifSetOr($_POST['submit_clear'], '')) {
|
||||
//
|
||||
header('Content-Type: text/plain');
|
||||
header('Content-Disposition: attachment; filename="config.inc.php"');
|
||||
echo ConfigFile::getInstance()->getConfigFile();
|
||||
echo ConfigGenerator::getConfigFile();
|
||||
exit;
|
||||
} elseif (PMA_ifSetOr($_POST['submit_save'], '')) {
|
||||
//
|
||||
// Save generated config file on the server
|
||||
//
|
||||
file_put_contents($config_file_path, ConfigFile::getInstance()->getConfigFile());
|
||||
file_put_contents($config_file_path, ConfigGenerator::getConfigFile());
|
||||
header('HTTP/1.1 303 See Other');
|
||||
header('Location: index.php');
|
||||
exit;
|
||||
|
@@ -16,6 +16,7 @@ if (!defined('PHPMYADMIN')) {
|
||||
*/
|
||||
require_once './libraries/config/FormDisplay.class.php';
|
||||
require_once './setup/lib/index.lib.php';
|
||||
require_once './setup/lib/ConfigGenerator.class.php';
|
||||
|
||||
$config_readable = false;
|
||||
$config_writable = false;
|
||||
@@ -29,7 +30,7 @@ check_config_rw($config_readable, $config_writable, $config_exists);
|
||||
<tr>
|
||||
<td>
|
||||
<textarea cols="50" rows="20" name="textconfig" id="textconfig" spellcheck="false"><?php
|
||||
echo htmlspecialchars(ConfigFile::getInstance()->getConfigFile())
|
||||
echo htmlspecialchars(ConfigGenerator::getConfigFile())
|
||||
?></textarea>
|
||||
</td>
|
||||
</tr>
|
||||
|
150
setup/lib/ConfigGenerator.class.php
Normal file
150
setup/lib/ConfigGenerator.class.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
/**
|
||||
* Config file generator
|
||||
*
|
||||
* @package phpMyAdmin-setup
|
||||
*/
|
||||
|
||||
/**
|
||||
* Config file generation class
|
||||
*
|
||||
* @package phpMyAdmin
|
||||
*/
|
||||
class ConfigGenerator
|
||||
{
|
||||
/**
|
||||
* Creates config file
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getConfigFile()
|
||||
{
|
||||
$cf = ConfigFile::getInstance();
|
||||
|
||||
$crlf = (isset($_SESSION['eol']) && $_SESSION['eol'] == 'win') ? "\r\n" : "\n";
|
||||
$c = $cf->getConfig();
|
||||
|
||||
// header
|
||||
$ret = '<?php' . $crlf
|
||||
. '/*' . $crlf
|
||||
. ' * Generated configuration file' . $crlf
|
||||
. ' * Generated by: phpMyAdmin '
|
||||
. $GLOBALS['PMA_Config']->get('PMA_VERSION')
|
||||
. ' setup script by Piotr Przybylski <piotrprz@gmail.com>' . $crlf
|
||||
. ' * Date: ' . date(DATE_RFC1123) . $crlf
|
||||
. ' */' . $crlf . $crlf;
|
||||
|
||||
// servers
|
||||
if ($cf->getServerCount() > 0) {
|
||||
$ret .= "/* Servers configuration */$crlf\$i = 0;" . $crlf . $crlf;
|
||||
foreach ($c['Servers'] as $id => $server) {
|
||||
$ret .= '/* Server: ' . strtr($cf->getServerName($id), '*/', '-') . " [$id] */" . $crlf
|
||||
. '$i++;' . $crlf;
|
||||
foreach ($server as $k => $v) {
|
||||
$k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
|
||||
$ret .= "\$cfg['Servers'][\$i]['$k'] = "
|
||||
. (is_array($v) && self::_isZeroBasedArray($v)
|
||||
? self::_exportZeroBasedArray($v, $crlf)
|
||||
: var_export($v, true))
|
||||
. ';' . $crlf;
|
||||
}
|
||||
$ret .= $crlf;
|
||||
}
|
||||
$ret .= '/* End of servers configuration */' . $crlf . $crlf;
|
||||
}
|
||||
unset($c['Servers']);
|
||||
|
||||
// other settings
|
||||
$persistKeys = $cf->getPersistKeysMap();
|
||||
|
||||
foreach ($c as $k => $v) {
|
||||
$k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
|
||||
$ret .= self::_getVarExport($k, $v, $crlf);
|
||||
if (isset($persistKeys[$k])) {
|
||||
unset($persistKeys[$k]);
|
||||
}
|
||||
}
|
||||
// keep 1d array keys which are present in $persist_keys (config.values.php)
|
||||
foreach (array_keys($persistKeys) as $k) {
|
||||
if (strpos($k, '/') === false) {
|
||||
$k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
|
||||
$ret .= self::_getVarExport($k, $cf->getDefault($k), $crlf);
|
||||
}
|
||||
}
|
||||
$ret .= '?>';
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns exported configuration variable
|
||||
*
|
||||
* @param string $var_name
|
||||
* @param mixed $var_value
|
||||
* @param string $crlf
|
||||
* @return string
|
||||
*/
|
||||
private static function _getVarExport($var_name, $var_value, $crlf)
|
||||
{
|
||||
if (!is_array($var_value) || empty($var_value)) {
|
||||
return "\$cfg['$var_name'] = " . var_export($var_value, true) . ';' . $crlf;
|
||||
}
|
||||
$ret = '';
|
||||
if (self::_isZeroBasedArray($var_value)) {
|
||||
$ret = "\$cfg['$var_name'] = " . self::_exportZeroBasedArray($var_value, $crlf)
|
||||
. ');' . $crlf;
|
||||
} else {
|
||||
// string keys: $cfg[key][subkey] = value
|
||||
foreach ($var_value as $k => $v) {
|
||||
$k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
|
||||
$ret .= "\$cfg['$var_name']['$k'] = " . var_export($v, true) . ';' . $crlf;
|
||||
}
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether $array is a continuous 0-based array
|
||||
*
|
||||
* @param array $array
|
||||
* @return boolean
|
||||
*/
|
||||
private static function _isZeroBasedArray(array $array)
|
||||
{
|
||||
for ($i = 0; $i < count($array); $i++) {
|
||||
if (!isset($array[$i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Exports continuous 0-based array
|
||||
*
|
||||
* @param array $array
|
||||
* @param string $crlf
|
||||
* @return string
|
||||
*/
|
||||
private static function _exportZeroBasedArray(array $array, $crlf)
|
||||
{
|
||||
$retv = array();
|
||||
foreach ($array as $v) {
|
||||
$retv[] = var_export($v, true);
|
||||
}
|
||||
$ret = "array(";
|
||||
if (count($retv) <= 4) {
|
||||
// up to 4 values - one line
|
||||
$ret .= implode(', ', $retv);
|
||||
} else {
|
||||
// more than 4 values - value per line
|
||||
$imax = count($retv)-1;
|
||||
for ($i = 0; $i <= $imax; $i++) {
|
||||
$ret .= ($i < $imax ? ($i > 0 ? ',' : '') : '') . $crlf . ' ' . $retv[$i];
|
||||
}
|
||||
}
|
||||
$ret .= ')';
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
?>
|
Reference in New Issue
Block a user