Редакция 69 | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | RSS
Редакция | Автор | № строки | Строка |
---|---|---|---|
69 | alex-w | 1 | <?php |
2 | /** |
||
3 | * PEAR_Command_Auth (login, logout commands) |
||
4 | * |
||
5 | * PHP versions 4 and 5 |
||
6 | * |
||
7 | * LICENSE: This source file is subject to version 3.0 of the PHP license |
||
8 | * that is available through the world-wide-web at the following URI: |
||
9 | * http://www.php.net/license/3_0.txt. If you did not receive a copy of |
||
10 | * the PHP License and are unable to obtain it through the web, please |
||
11 | * send a note to license@php.net so we can mail you a copy immediately. |
||
12 | * |
||
13 | * @category pear |
||
14 | * @package PEAR |
||
15 | * @author Stig Bakken <ssb@php.net> |
||
16 | * @author Greg Beaver <cellog@php.net> |
||
17 | * @copyright 1997-2008 The PHP Group |
||
18 | * @license http://www.php.net/license/3_0.txt PHP License 3.0 |
||
19 | * @version CVS: $Id: Auth.php,v 1.31 2008/01/03 20:26:36 cellog Exp $ |
||
20 | * @link http://pear.php.net/package/PEAR |
||
21 | * @since File available since Release 0.1 |
||
22 | */ |
||
23 | |||
24 | /** |
||
25 | * base class |
||
26 | */ |
||
27 | require_once 'PEAR/Command/Common.php'; |
||
28 | require_once 'PEAR/Config.php'; |
||
29 | |||
30 | /** |
||
31 | * PEAR commands for login/logout |
||
32 | * |
||
33 | * @category pear |
||
34 | * @package PEAR |
||
35 | * @author Stig Bakken <ssb@php.net> |
||
36 | * @author Greg Beaver <cellog@php.net> |
||
37 | * @copyright 1997-2008 The PHP Group |
||
38 | * @license http://www.php.net/license/3_0.txt PHP License 3.0 |
||
39 | * @version Release: 1.7.2 |
||
40 | * @link http://pear.php.net/package/PEAR |
||
41 | * @since Class available since Release 0.1 |
||
42 | */ |
||
43 | class PEAR_Command_Auth extends PEAR_Command_Common |
||
44 | { |
||
45 | // {{{ properties |
||
46 | |||
47 | var $commands = array( |
||
48 | 'login' => array( |
||
49 | 'summary' => 'Connects and authenticates to remote server', |
||
50 | 'shortcut' => 'li', |
||
51 | 'function' => 'doLogin', |
||
52 | 'options' => array(), |
||
53 | 'doc' => '<channel name> |
||
54 | Log in to a remote channel server. If <channel name> is not supplied, |
||
55 | the default channel is used. To use remote functions in the installer |
||
56 | that require any kind of privileges, you need to log in first. The |
||
57 | username and password you enter here will be stored in your per-user |
||
58 | PEAR configuration (~/.pearrc on Unix-like systems). After logging |
||
59 | in, your username and password will be sent along in subsequent |
||
60 | operations on the remote server.', |
||
61 | ), |
||
62 | 'logout' => array( |
||
63 | 'summary' => 'Logs out from the remote server', |
||
64 | 'shortcut' => 'lo', |
||
65 | 'function' => 'doLogout', |
||
66 | 'options' => array(), |
||
67 | 'doc' => ' |
||
68 | Logs out from the remote server. This command does not actually |
||
69 | connect to the remote server, it only deletes the stored username and |
||
70 | password from your user configuration.', |
||
71 | ) |
||
72 | |||
73 | ); |
||
74 | |||
75 | // }}} |
||
76 | |||
77 | // {{{ constructor |
||
78 | |||
79 | /** |
||
80 | * PEAR_Command_Auth constructor. |
||
81 | * |
||
82 | * @access public |
||
83 | */ |
||
84 | function PEAR_Command_Auth(&$ui, &$config) |
||
85 | { |
||
86 | parent::PEAR_Command_Common($ui, $config); |
||
87 | } |
||
88 | |||
89 | // }}} |
||
90 | |||
91 | // {{{ doLogin() |
||
92 | |||
93 | /** |
||
94 | * Execute the 'login' command. |
||
95 | * |
||
96 | * @param string $command command name |
||
97 | * |
||
98 | * @param array $options option_name => value |
||
99 | * |
||
100 | * @param array $params list of additional parameters |
||
101 | * |
||
102 | * @return bool TRUE on success or |
||
103 | * a PEAR error on failure |
||
104 | * |
||
105 | * @access public |
||
106 | */ |
||
107 | function doLogin($command, $options, $params) |
||
108 | { |
||
109 | $reg = &$this->config->getRegistry(); |
||
110 | |||
111 | // If a parameter is supplied, use that as the channel to log in to |
||
112 | if (isset($params[0])) { |
||
113 | $channel = $params[0]; |
||
114 | } else { |
||
115 | $channel = $this->config->get('default_channel'); |
||
116 | } |
||
117 | |||
118 | $chan = $reg->getChannel($channel); |
||
119 | if (PEAR::isError($chan)) { |
||
120 | return $this->raiseError($chan); |
||
121 | } |
||
122 | $server = $this->config->get('preferred_mirror', null, $channel); |
||
123 | $remote = &$this->config->getRemote(); |
||
124 | $username = $this->config->get('username', null, $channel); |
||
125 | if (empty($username)) { |
||
126 | $username = isset($_ENV['USER']) ? $_ENV['USER'] : null; |
||
127 | } |
||
128 | $this->ui->outputData("Logging in to $server.", $command); |
||
129 | |||
130 | list($username, $password) = $this->ui->userDialog( |
||
131 | $command, |
||
132 | array('Username', 'Password'), |
||
133 | array('text', 'password'), |
||
134 | array($username, '') |
||
135 | ); |
||
136 | $username = trim($username); |
||
137 | $password = trim($password); |
||
138 | |||
139 | $ourfile = $this->config->getConfFile('user'); |
||
140 | if (!$ourfile) { |
||
141 | $ourfile = $this->config->getConfFile('system'); |
||
142 | } |
||
143 | |||
144 | $this->config->set('username', $username, 'user', $channel); |
||
145 | $this->config->set('password', $password, 'user', $channel); |
||
146 | |||
147 | if ($chan->supportsREST()) { |
||
148 | $ok = true; |
||
149 | } else { |
||
150 | $remote->expectError(401); |
||
151 | $ok = $remote->call('logintest'); |
||
152 | $remote->popExpect(); |
||
153 | } |
||
154 | if ($ok === true) { |
||
155 | $this->ui->outputData("Logged in.", $command); |
||
156 | // avoid changing any temporary settings changed with -d |
||
157 | $ourconfig = new PEAR_Config($ourfile, $ourfile); |
||
158 | $ourconfig->set('username', $username, 'user', $channel); |
||
159 | $ourconfig->set('password', $password, 'user', $channel); |
||
160 | $ourconfig->store(); |
||
161 | } else { |
||
162 | return $this->raiseError("Login failed!"); |
||
163 | } |
||
164 | return true; |
||
165 | } |
||
166 | |||
167 | // }}} |
||
168 | // {{{ doLogout() |
||
169 | |||
170 | /** |
||
171 | * Execute the 'logout' command. |
||
172 | * |
||
173 | * @param string $command command name |
||
174 | * |
||
175 | * @param array $options option_name => value |
||
176 | * |
||
177 | * @param array $params list of additional parameters |
||
178 | * |
||
179 | * @return bool TRUE on success or |
||
180 | * a PEAR error on failure |
||
181 | * |
||
182 | * @access public |
||
183 | */ |
||
184 | function doLogout($command, $options, $params) |
||
185 | { |
||
186 | $reg = &$this->config->getRegistry(); |
||
187 | $channel = $this->config->get('default_channel'); |
||
188 | $chan = $reg->getChannel($channel); |
||
189 | if (PEAR::isError($chan)) { |
||
190 | return $this->raiseError($chan); |
||
191 | } |
||
192 | $server = $this->config->get('preferred_mirror'); |
||
193 | $this->ui->outputData("Logging out from $server.", $command); |
||
194 | $this->config->remove('username'); |
||
195 | $this->config->remove('password'); |
||
196 | $this->config->store(); |
||
197 | return true; |
||
198 | } |
||
199 | |||
200 | // }}} |
||
201 | } |
||
202 | |||
203 | ?> |