| // +----------------------------------------------------------------------+ // // $Id$ require_once "PEAR/Command/Common.php"; require_once "PEAR/Remote.php"; require_once "PEAR/Config.php"; /** * PEAR commands for managing configuration data. * */ class PEAR_Command_Auth extends PEAR_Command_Common { // {{{ constructor /** * PEAR_Command_Auth constructor. * * @access public */ function PEAR_Command_Auth(&$ui, &$config) { parent::PEAR_Command_Common($ui, $config); } // }}} // {{{ getCommands() /** * Return a list of all the commands defined by this class. * @return array list of commands * @access public */ function getCommands() { return array('login', 'logout'); } // }}} // {{{ run() /** * Execute the command. * * @param string command name * * @param array option_name => value * * @param array list of additional parameters * * @return bool TRUE on success, FALSE for unknown commands, or * a PEAR error on failure * * @access public */ function run($command, $options, $params) { $cf = $this->config; $failmsg = ''; $server = $cf->get('master_server'); switch ($command) { case 'login': { $remote = new PEAR_Remote($cf); $username = $cf->get('username'); if (empty($username)) { $username = @$_ENV['USER']; } $this->ui->displayLine("Logging in to $server."); $username = trim($this->ui->userDialog('Username', 'text', $username)); $cf->set('username', $username); $password = trim($this->ui->userDialog('Password', 'password')); $cf->set('password', $password); $remote->expectError(401); $ok = $remote->call('logintest'); $remote->popExpect(); if ($ok === true) { $this->ui->displayLine("Logged in."); $cf->store(); } else { $this->ui->displayLine("Login failed!"); } break; } case 'logout': { $this->ui->displayLine("Logging out from $server."); $cf->remove('username'); $cf->remove('password'); $cf->store(); break; } default: { return false; } } if ($failmsg) { return $this->raiseError($failmsg); } return true; } // }}} } ?>