ConfigGenerator.class.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Config file generator
  5. *
  6. * @package PhpMyAdmin-Setup
  7. */
  8. /**
  9. * Config file generation class
  10. *
  11. * @package PhpMyAdmin
  12. */
  13. class ConfigGenerator
  14. {
  15. /**
  16. * Creates config file
  17. *
  18. * @return string
  19. */
  20. public static function getConfigFile()
  21. {
  22. $cf = ConfigFile::getInstance();
  23. $crlf = (isset($_SESSION['eol']) && $_SESSION['eol'] == 'win') ? "\r\n" : "\n";
  24. $c = $cf->getConfig();
  25. // header
  26. $ret = '<?php' . $crlf
  27. . '/*' . $crlf
  28. . ' * Generated configuration file' . $crlf
  29. . ' * Generated by: phpMyAdmin '
  30. . $GLOBALS['PMA_Config']->get('PMA_VERSION')
  31. . ' setup script' . $crlf
  32. . ' * Date: ' . date(DATE_RFC1123) . $crlf
  33. . ' */' . $crlf . $crlf;
  34. // servers
  35. if ($cf->getServerCount() > 0) {
  36. $ret .= "/* Servers configuration */$crlf\$i = 0;" . $crlf . $crlf;
  37. foreach ($c['Servers'] as $id => $server) {
  38. $ret .= '/* Server: ' . strtr($cf->getServerName($id) . " [$id] ", '*/', '-') . "*/" . $crlf
  39. . '$i++;' . $crlf;
  40. foreach ($server as $k => $v) {
  41. $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
  42. $ret .= "\$cfg['Servers'][\$i]['$k'] = "
  43. . (is_array($v) && self::_isZeroBasedArray($v)
  44. ? self::_exportZeroBasedArray($v, $crlf)
  45. : var_export($v, true))
  46. . ';' . $crlf;
  47. }
  48. $ret .= $crlf;
  49. }
  50. $ret .= '/* End of servers configuration */' . $crlf . $crlf;
  51. }
  52. unset($c['Servers']);
  53. // other settings
  54. $persistKeys = $cf->getPersistKeysMap();
  55. foreach ($c as $k => $v) {
  56. $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
  57. $ret .= self::_getVarExport($k, $v, $crlf);
  58. if (isset($persistKeys[$k])) {
  59. unset($persistKeys[$k]);
  60. }
  61. }
  62. // keep 1d array keys which are present in $persist_keys (config.values.php)
  63. foreach (array_keys($persistKeys) as $k) {
  64. if (strpos($k, '/') === false) {
  65. $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
  66. $ret .= self::_getVarExport($k, $cf->getDefault($k), $crlf);
  67. }
  68. }
  69. $ret .= '?>';
  70. return $ret;
  71. }
  72. /**
  73. * Returns exported configuration variable
  74. *
  75. * @param string $var_name
  76. * @param mixed $var_value
  77. * @param string $crlf
  78. *
  79. * @return string
  80. */
  81. private static function _getVarExport($var_name, $var_value, $crlf)
  82. {
  83. if (!is_array($var_value) || empty($var_value)) {
  84. return "\$cfg['$var_name'] = " . var_export($var_value, true) . ';' . $crlf;
  85. }
  86. $ret = '';
  87. if (self::_isZeroBasedArray($var_value)) {
  88. $ret = "\$cfg['$var_name'] = " . self::_exportZeroBasedArray($var_value, $crlf)
  89. . ';' . $crlf;
  90. } else {
  91. // string keys: $cfg[key][subkey] = value
  92. foreach ($var_value as $k => $v) {
  93. $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
  94. $ret .= "\$cfg['$var_name']['$k'] = " . var_export($v, true) . ';' . $crlf;
  95. }
  96. }
  97. return $ret;
  98. }
  99. /**
  100. * Check whether $array is a continuous 0-based array
  101. *
  102. * @param array $array
  103. *
  104. * @return boolean
  105. */
  106. private static function _isZeroBasedArray(array $array)
  107. {
  108. for ($i = 0; $i < count($array); $i++) {
  109. if (! isset($array[$i])) {
  110. return false;
  111. }
  112. }
  113. return true;
  114. }
  115. /**
  116. * Exports continuous 0-based array
  117. *
  118. * @param array $array
  119. * @param string $crlf
  120. *
  121. * @return string
  122. */
  123. private static function _exportZeroBasedArray(array $array, $crlf)
  124. {
  125. $retv = array();
  126. foreach ($array as $v) {
  127. $retv[] = var_export($v, true);
  128. }
  129. $ret = "array(";
  130. if (count($retv) <= 4) {
  131. // up to 4 values - one line
  132. $ret .= implode(', ', $retv);
  133. } else {
  134. // more than 4 values - value per line
  135. $imax = count($retv);
  136. for ($i = 0; $i < $imax; $i++) {
  137. $ret .= ($i > 0 ? ',' : '') . $crlf . ' ' . $retv[$i];
  138. }
  139. }
  140. $ret .= ')';
  141. return $ret;
  142. }
  143. }
  144. ?>