diff --git a/src/Cli.php b/src/Cli.php index 5c09aa5..b687e56 100644 --- a/src/Cli.php +++ b/src/Cli.php @@ -341,4 +341,24 @@ class Cli { exit($exit_code); } + /** + * Helper method to ask user to enter a password + * Note: this method use the bash binary and a fatal error will be trigger if it's not available. + * @param string|null $prompt The prompt message (optional) + * @return string + */ + public static function prompt_for_password($prompt=null) { + // Check bash is available + $command = "/usr/bin/env bash -c 'echo OK'"; + if (rtrim(shell_exec($command)) !== 'OK') + Log::fatal(I18n::_("Can't invoke bash. Can't ask password prompt.")); + + $command = "/usr/bin/env bash -c 'read -s -p \""; + $command .= addslashes($prompt?_($prompt):I18n::_("Please enter password:")); + $command .= "\" mypassword && echo \$mypassword'"; + $password = rtrim(shell_exec($command)); + echo "\n"; + return $password; + } + }