From 7d62bc241761053d1e4b60e4e928465c3e08573c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Tue, 15 Sep 2015 22:08:04 +0200 Subject: [PATCH] MDL-51403 install: Improve the language validation in the CLI installer There are two essential improvements here. Firstly, by replacing the file_exists() check with array_key_exists() we make sure that only actual language code will be written into the config.php file (and not the empty value in case of input that does not pass the PARAM_SAFEDIR cleaning). Additionally, we no longer display the full list of available languages by default. The list can be displayed in the interactive mode by typing the ? character instead of the language code. This makes the overall interface cleaner, does not cause the header information (such as the Moodle version) to scroll away and makes the nice cli logo more visible (which was the main motivation for the whole patch anyway ;-). --- admin/cli/install.php | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/admin/cli/install.php b/admin/cli/install.php index 131056d7e67..51364b9f149 100644 --- a/admin/cli/install.php +++ b/admin/cli/install.php @@ -273,7 +273,8 @@ $interactive = empty($options['non-interactive']); // set up language $lang = clean_param($options['lang'], PARAM_SAFEDIR); -if (file_exists($CFG->dirroot.'/install/lang/'.$lang)) { +$languages = get_string_manager()->get_list_of_translations(); +if (array_key_exists($lang, $languages)) { $CFG->lang = $lang; } @@ -295,23 +296,34 @@ echo get_string('cliinstallheader', 'install', $CFG->target_release)."\n"; //Fist select language if ($interactive) { cli_separator(); - $languages = get_string_manager()->get_list_of_translations(); // Do not put the langs into columns because it is not compatible with RTL. - $langlist = implode("\n", $languages); $default = $CFG->lang; - cli_heading(get_string('availablelangs', 'install')); - echo $langlist."\n"; + cli_heading(get_string('chooselanguagehead', 'install')); + if (array_key_exists($default, $languages)) { + echo $default.' - '.$languages[$default]."\n"; + } + if ($default !== 'en') { + echo 'en - English (en)'."\n"; + } + echo '? - '.get_string('availablelangs', 'install')."\n"; $prompt = get_string('clitypevaluedefault', 'admin', $CFG->lang); $error = ''; do { echo $error; $input = cli_input($prompt, $default); - $input = clean_param($input, PARAM_SAFEDIR); - if (!file_exists($CFG->dirroot.'/install/lang/'.$input)) { - $error = get_string('cliincorrectvalueretry', 'admin')."\n"; + if ($input === '?') { + echo implode("\n", $languages)."\n"; + $error = "\n"; + } else { - $error = ''; + $input = clean_param($input, PARAM_SAFEDIR); + + if (!array_key_exists($input, $languages)) { + $error = get_string('cliincorrectvalueretry', 'admin')."\n"; + } else { + $error = ''; + } } } while ($error !== ''); $CFG->lang = $input;