Auth.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at the following url: |
  11. // | http://www.php.net/license/3_0.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | [email protected] so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Stig Bakken <[email protected]> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Auth.php,v 1.15 2004/01/08 17:33:13 sniper Exp $
  20. require_once "PEAR/Command/Common.php";
  21. require_once "PEAR/Remote.php";
  22. require_once "PEAR/Config.php";
  23. /**
  24. * PEAR commands for managing configuration data.
  25. *
  26. */
  27. class PEAR_Command_Auth extends PEAR_Command_Common
  28. {
  29. // {{{ properties
  30. var $commands = array(
  31. 'login' => array(
  32. 'summary' => 'Connects and authenticates to remote server',
  33. 'shortcut' => 'li',
  34. 'function' => 'doLogin',
  35. 'options' => array(),
  36. 'doc' => '
  37. Log in to the remote server. To use remote functions in the installer
  38. that require any kind of privileges, you need to log in first. The
  39. username and password you enter here will be stored in your per-user
  40. PEAR configuration (~/.pearrc on Unix-like systems). After logging
  41. in, your username and password will be sent along in subsequent
  42. operations on the remote server.',
  43. ),
  44. 'logout' => array(
  45. 'summary' => 'Logs out from the remote server',
  46. 'shortcut' => 'lo',
  47. 'function' => 'doLogout',
  48. 'options' => array(),
  49. 'doc' => '
  50. Logs out from the remote server. This command does not actually
  51. connect to the remote server, it only deletes the stored username and
  52. password from your user configuration.',
  53. )
  54. );
  55. // }}}
  56. // {{{ constructor
  57. /**
  58. * PEAR_Command_Auth constructor.
  59. *
  60. * @access public
  61. */
  62. function PEAR_Command_Auth(&$ui, &$config)
  63. {
  64. parent::PEAR_Command_Common($ui, $config);
  65. }
  66. // }}}
  67. // {{{ doLogin()
  68. /**
  69. * Execute the 'login' command.
  70. *
  71. * @param string $command command name
  72. *
  73. * @param array $options option_name => value
  74. *
  75. * @param array $params list of additional parameters
  76. *
  77. * @return bool TRUE on success, FALSE for unknown commands, or
  78. * a PEAR error on failure
  79. *
  80. * @access public
  81. */
  82. function doLogin($command, $options, $params)
  83. {
  84. $server = $this->config->get('master_server');
  85. $remote = new PEAR_Remote($this->config);
  86. $username = $this->config->get('username');
  87. if (empty($username)) {
  88. $username = @$_ENV['USER'];
  89. }
  90. $this->ui->outputData("Logging in to $server.", $command);
  91. list($username, $password) = $this->ui->userDialog(
  92. $command,
  93. array('Username', 'Password'),
  94. array('text', 'password'),
  95. array($username, '')
  96. );
  97. $username = trim($username);
  98. $password = trim($password);
  99. $this->config->set('username', $username);
  100. $this->config->set('password', $password);
  101. $remote->expectError(401);
  102. $ok = $remote->call('logintest');
  103. $remote->popExpect();
  104. if ($ok === true) {
  105. $this->ui->outputData("Logged in.", $command);
  106. $this->config->store();
  107. } else {
  108. return $this->raiseError("Login failed!");
  109. }
  110. }
  111. // }}}
  112. // {{{ doLogout()
  113. /**
  114. * Execute the 'logout' command.
  115. *
  116. * @param string $command command name
  117. *
  118. * @param array $options option_name => value
  119. *
  120. * @param array $params list of additional parameters
  121. *
  122. * @return bool TRUE on success, FALSE for unknown commands, or
  123. * a PEAR error on failure
  124. *
  125. * @access public
  126. */
  127. function doLogout($command, $options, $params)
  128. {
  129. $server = $this->config->get('master_server');
  130. $this->ui->outputData("Logging out from $server.", $command);
  131. $this->config->remove('username');
  132. $this->config->remove('password');
  133. $this->config->store();
  134. }
  135. // }}}
  136. }
  137. ?>