0
|
1 <?php
|
|
2
|
|
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
|
4
|
|
5 /**
|
|
6 * This file is part of the PEAR Console_CommandLine package.
|
|
7 *
|
|
8 * PHP version 5
|
|
9 *
|
|
10 * LICENSE: This source file is subject to the MIT license that is available
|
|
11 * through the world-wide-web at the following URI:
|
|
12 * http://opensource.org/licenses/mit-license.php
|
|
13 *
|
|
14 * @category Console
|
|
15 * @package Console_CommandLine
|
|
16 * @author David JEAN LOUIS <izimobil@gmail.com>
|
|
17 * @copyright 2007 David JEAN LOUIS
|
|
18 * @license http://opensource.org/licenses/mit-license.php MIT License
|
|
19 * @version CVS: $Id$
|
|
20 * @link http://pear.php.net/package/Console_CommandLine
|
|
21 * @since File available since release 0.1.0
|
|
22 * @filesource
|
|
23 */
|
|
24
|
|
25 /**
|
|
26 * Required by this class.
|
|
27 */
|
|
28 require_once 'Console/CommandLine/Action.php';
|
|
29
|
|
30 /**
|
|
31 * Class that represent the Password action, a special action that allow the
|
|
32 * user to specify the password on the commandline or to be prompted for
|
|
33 * entering it.
|
|
34 *
|
|
35 * @category Console
|
|
36 * @package Console_CommandLine
|
|
37 * @author David JEAN LOUIS <izimobil@gmail.com>
|
|
38 * @copyright 2007 David JEAN LOUIS
|
|
39 * @license http://opensource.org/licenses/mit-license.php MIT License
|
|
40 * @version Release: @package_version@
|
|
41 * @link http://pear.php.net/package/Console_CommandLine
|
|
42 * @since Class available since release 0.1.0
|
|
43 */
|
|
44 class Console_CommandLine_Action_Password extends Console_CommandLine_Action
|
|
45 {
|
|
46 // execute() {{{
|
|
47
|
|
48 /**
|
|
49 * Executes the action with the value entered by the user.
|
|
50 *
|
|
51 * @param mixed $value The option value
|
|
52 * @param array $params An array of optional parameters
|
|
53 *
|
|
54 * @return string
|
|
55 */
|
|
56 public function execute($value = false, $params = array())
|
|
57 {
|
|
58 $this->setResult(empty($value) ? $this->_promptPassword() : $value);
|
|
59 }
|
|
60 // }}}
|
|
61 // _promptPassword() {{{
|
|
62
|
|
63 /**
|
|
64 * Prompts the password to the user without echoing it.
|
|
65 *
|
|
66 * @return string
|
|
67 * @todo not echo-ing the password does not work on windows is there a way
|
|
68 * to make this work ?
|
|
69 */
|
|
70 private function _promptPassword()
|
|
71 {
|
|
72 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
|
|
73 fwrite(STDOUT,
|
|
74 $this->parser->message_provider->get('PASSWORD_PROMPT_ECHO'));
|
|
75 @flock(STDIN, LOCK_EX);
|
|
76 $passwd = fgets(STDIN);
|
|
77 @flock(STDIN, LOCK_UN);
|
|
78 } else {
|
|
79 fwrite(STDOUT, $this->parser->message_provider->get('PASSWORD_PROMPT'));
|
|
80 // disable echoing
|
|
81 system('stty -echo');
|
|
82 @flock(STDIN, LOCK_EX);
|
|
83 $passwd = fgets(STDIN);
|
|
84 @flock(STDIN, LOCK_UN);
|
|
85 system('stty echo');
|
|
86 }
|
|
87 return trim($passwd);
|
|
88 }
|
|
89 // }}}
|
|
90 }
|