"Fossies" - the Fresh Open Source Software Archive

Member "phpMyAdmin-5.1.0-english/libraries/classes/Setup/ConfigGenerator.php" (24 Feb 2021, 5623 Bytes) of package /linux/www/phpMyAdmin-5.1.0-english.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ConfigGenerator.php" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 5.0.4-english_vs_5.1.0-english.

    1 <?php
    2 /**
    3  * Config file generator
    4  */
    5 
    6 declare(strict_types=1);
    7 
    8 namespace PhpMyAdmin\Setup;
    9 
   10 use PhpMyAdmin\Config\ConfigFile;
   11 use const DATE_RFC1123;
   12 use function array_keys;
   13 use function count;
   14 use function gmdate;
   15 use function implode;
   16 use function is_array;
   17 use function mb_strpos;
   18 use function preg_replace;
   19 use function strtr;
   20 use function var_export;
   21 
   22 /**
   23  * Config file generation class
   24  */
   25 class ConfigGenerator
   26 {
   27     /**
   28      * Creates config file
   29      *
   30      * @param ConfigFile $cf Config file instance
   31      *
   32      * @return string
   33      */
   34     public static function getConfigFile(ConfigFile $cf)
   35     {
   36         $crlf = isset($_SESSION['eol']) && $_SESSION['eol'] === 'win'
   37             ? "\r\n"
   38             : "\n";
   39         $conf = $cf->getConfig();
   40 
   41         // header
   42         $ret = '<?php' . $crlf
   43             . '/*' . $crlf
   44             . ' * Generated configuration file' . $crlf
   45             . ' * Generated by: phpMyAdmin '
   46                 . $GLOBALS['PMA_Config']->get('PMA_VERSION')
   47                 . ' setup script' . $crlf
   48             . ' * Date: ' . gmdate(DATE_RFC1123) . $crlf
   49             . ' */' . $crlf . $crlf;
   50 
   51         //servers
   52         if (! empty($conf['Servers'])) {
   53             $ret .= self::getServerPart($cf, $crlf, $conf['Servers']);
   54             unset($conf['Servers']);
   55         }
   56 
   57         // other settings
   58         $persistKeys = $cf->getPersistKeysMap();
   59 
   60         foreach ($conf as $k => $v) {
   61             $k = preg_replace('/[^A-Za-z0-9_]/', '_', (string) $k);
   62             $ret .= self::getVarExport($k, $v, $crlf);
   63             if (! isset($persistKeys[$k])) {
   64                 continue;
   65             }
   66 
   67             unset($persistKeys[$k]);
   68         }
   69         // keep 1d array keys which are present in $persist_keys (config.values.php)
   70         foreach (array_keys($persistKeys) as $k) {
   71             if (mb_strpos($k, '/') !== false) {
   72                 continue;
   73             }
   74 
   75             $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
   76             $ret .= self::getVarExport($k, $cf->getDefault($k), $crlf);
   77         }
   78 
   79         return $ret . '?>';
   80     }
   81 
   82     /**
   83      * Returns exported configuration variable
   84      *
   85      * @param string $var_name  configuration name
   86      * @param mixed  $var_value configuration value(s)
   87      * @param string $crlf      line ending
   88      *
   89      * @return string
   90      */
   91     private static function getVarExport($var_name, $var_value, $crlf)
   92     {
   93         if (! is_array($var_value) || empty($var_value)) {
   94             return "\$cfg['" . $var_name . "'] = "
   95                 . var_export($var_value, true) . ';' . $crlf;
   96         }
   97         $ret = '';
   98         if (self::isZeroBasedArray($var_value)) {
   99             $ret = "\$cfg['" . $var_name . "'] = "
  100                 . self::exportZeroBasedArray($var_value, $crlf)
  101                 . ';' . $crlf;
  102         } else {
  103             // string keys: $cfg[key][subkey] = value
  104             foreach ($var_value as $k => $v) {
  105                 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
  106                 $ret .= "\$cfg['" . $var_name . "']['" . $k . "'] = "
  107                     . var_export($v, true) . ';' . $crlf;
  108             }
  109         }
  110 
  111         return $ret;
  112     }
  113 
  114     /**
  115      * Check whether $array is a continuous 0-based array
  116      *
  117      * @param array $array Array to check
  118      *
  119      * @return bool
  120      */
  121     private static function isZeroBasedArray(array $array)
  122     {
  123         for ($i = 0, $nb = count($array); $i < $nb; $i++) {
  124             if (! isset($array[$i])) {
  125                 return false;
  126             }
  127         }
  128 
  129         return true;
  130     }
  131 
  132     /**
  133      * Exports continuous 0-based array
  134      *
  135      * @param array  $array Array to export
  136      * @param string $crlf  Newline string
  137      *
  138      * @return string
  139      */
  140     private static function exportZeroBasedArray(array $array, $crlf)
  141     {
  142         $retv = [];
  143         foreach ($array as $v) {
  144             $retv[] = var_export($v, true);
  145         }
  146         $ret = 'array(';
  147         if (count($retv) <= 4) {
  148             // up to 4 values - one line
  149             $ret .= implode(', ', $retv);
  150         } else {
  151             // more than 4 values - value per line
  152             $imax = count($retv);
  153             for ($i = 0; $i < $imax; $i++) {
  154                 $ret .= ($i > 0 ? ',' : '') . $crlf . '    ' . $retv[$i];
  155             }
  156         }
  157 
  158         return $ret . ')';
  159     }
  160 
  161     /**
  162      * Generate server part of config file
  163      *
  164      * @param ConfigFile $cf      Config file
  165      * @param string     $crlf    Carriage return char
  166      * @param array      $servers Servers list
  167      *
  168      * @return string|null
  169      */
  170     protected static function getServerPart(ConfigFile $cf, $crlf, array $servers)
  171     {
  172         if ($cf->getServerCount() === 0) {
  173             return null;
  174         }
  175 
  176         $ret = '/* Servers configuration */' . $crlf . '$i = 0;' . $crlf . $crlf;
  177         foreach ($servers as $id => $server) {
  178             $ret .= '/* Server: '
  179                 . strtr($cf->getServerName($id) . ' [' . $id . '] ', '*/', '-')
  180                 . '*/' . $crlf
  181                 . '$i++;' . $crlf;
  182             foreach ($server as $k => $v) {
  183                 $k = preg_replace('/[^A-Za-z0-9_]/', '_', (string) $k);
  184                 $ret .= "\$cfg['Servers'][\$i]['" . $k . "'] = "
  185                     . (is_array($v) && self::isZeroBasedArray($v)
  186                         ? self::exportZeroBasedArray($v, $crlf)
  187                         : var_export($v, true))
  188                     . ';' . $crlf;
  189             }
  190             $ret .= $crlf;
  191         }
  192         $ret .= '/* End of servers configuration */' . $crlf . $crlf;
  193 
  194         return $ret;
  195     }
  196 }