From ae27f360ca4498356913fa9398f1acb241ddae69 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Wed, 22 Feb 2023 17:40:41 +0200 Subject: [PATCH] command io readSecretLine --- .../Core/Console/Commands/SetPassword.php | 2 +- application/Espo/Core/Console/IO.php | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/application/Espo/Core/Console/Commands/SetPassword.php b/application/Espo/Core/Console/Commands/SetPassword.php index 1dfa829a80..d1f0d681a6 100644 --- a/application/Espo/Core/Console/Commands/SetPassword.php +++ b/application/Espo/Core/Console/Commands/SetPassword.php @@ -76,7 +76,7 @@ class SetPassword implements Command $io->writeLine("Enter a new password:"); - $password = $io->readLine(); + $password = $io->readSecretLine(); if (!$password) { $io->writeLine("Password can not be empty."); diff --git a/application/Espo/Core/Console/IO.php b/application/Espo/Core/Console/IO.php index d985c50490..fb165cbcd8 100644 --- a/application/Espo/Core/Console/IO.php +++ b/application/Espo/Core/Console/IO.php @@ -64,6 +64,19 @@ class IO * Read a line from input. A string is trimmed. */ public function readLine(): string + { + return $this->readLineInternal(); + } + + /** + * Read a secret line from input. A string is trimmed. + */ + public function readSecretLine(): string + { + return $this->readLineInternal(true); + } + + private function readLineInternal(bool $secret = false): string { $resource = fopen('php://stdin', 'r'); @@ -71,8 +84,16 @@ class IO throw new RuntimeException("Could not open stdin."); } + if ($secret && !self::isWindows()) { + shell_exec('stty -echo'); + } + $readString = fgets($resource); + if ($secret && !self::isWindows()) { + shell_exec('stty echo'); + } + if ($readString === false) { $readString = ''; } @@ -84,6 +105,11 @@ class IO return $string; } + private static function isWindows(): bool + { + return strcasecmp(substr(PHP_OS, 0, 3), 'WIN') === 0; + } + /** * Set exit-status. *