Overview

Packages

  • None
  • Passwd

Classes

  • Passwd
  • Passwd_Driver
  • Passwd_Driver_Adsi
  • Passwd_Driver_Composite
  • Passwd_Driver_Expect
  • Passwd_Driver_Expectpecl
  • Passwd_Driver_Horde
  • Passwd_Driver_Http
  • Passwd_Driver_Kolab
  • Passwd_Driver_Ldap
  • Passwd_Driver_Pine
  • Passwd_Driver_Poppassd
  • Passwd_Driver_Procopen
  • Passwd_Driver_Pspasswd
  • Passwd_Driver_Servuftp
  • Passwd_Driver_Smbldap
  • Passwd_Driver_Smbpasswd
  • Passwd_Driver_Soap
  • Passwd_Driver_Sql
  • Passwd_Driver_Vmailmgr
  • Passwd_Driver_Vpopmail
  • Passwd_Exception
  • Passwd_Factory_Driver
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * The Poppassd class attempts to change a user's password via a poppassd
  4:  * server.
  5:  *
  6:  * Copyright 2000-2012 Horde LLC (http://www.horde.org/)
  7:  *
  8:  * See the enclosed file COPYING for license information (GPL). If you
  9:  * did not receive this file, see http://www.horde.org/licenses/gpl.php.
 10:  *
 11:  * @author  Eric Jon Rostetter <eric.rostetter@physics.utexas.edu>
 12:  * @package Passwd
 13:  */
 14: class Passwd_Driver_Poppassd extends Passwd_Driver
 15: {
 16:     /**
 17:      * Constructor.
 18:      *
 19:      * @param array $params  A hash containing connection parameters.
 20:      */
 21:     public function __construct($params = array())
 22:     {
 23:         $this->_params = array_merge(
 24:             array('host' => 'localhost',
 25:                   'port' => 106),
 26:             $params);
 27:     }
 28: 
 29:     /**
 30:      * Connects to the server.
 31:      */
 32:     protected function _connect()
 33:     {
 34:         $this->_fp = fsockopen($this->_params['host'],
 35:                                $this->_params['port'],
 36:                                $errno,
 37:                                $errstr,
 38:                                30);
 39:         if (!$this->_fp) {
 40:             throw new Passwd_Exception($errstr);
 41:         }
 42: 
 43:         $this->_getPrompt();
 44:     }
 45: 
 46:     /**
 47:      * Disconnects from the server.
 48:      */
 49:     protected function _disconnect()
 50:     {
 51:         if (isset($this->_fp)) {
 52:             fputs($this->_fp, "quit\n");
 53:             fclose($this->_fp);
 54:         }
 55:     }
 56: 
 57:     /**
 58:      * Parses a response from the server to see what it was.
 59:      */
 60:     protected function _getPrompt()
 61:     {
 62:         $prompt = fgets($this->_fp, 4096);
 63:         if (!$prompt) {
 64:             throw new Passwd_Exception(_("No prompt returned from server."));
 65:         }
 66: 
 67:         if (!preg_match('/^[1-5][0-9][0-9]/', $prompt)) {
 68:             throw new Passwd_Exception($prompt);
 69:         }
 70: 
 71:         /* This should probably be a regex match for 2?0 or 3?0, no? */
 72:         $rc = substr($prompt, 0, 3);
 73:         if ($rc != '200' && $rc != '220' && $rc != '250' && $rc != '300' ) {
 74:             throw new Passwd_Exception($prompt);
 75:         }
 76:     }
 77: 
 78:     /**
 79:      * Sends a command to the server.
 80:      */
 81:     protected function _sendCommand($cmd, $arg)
 82:     {
 83:         $line = $cmd . ' ' . $arg . "\n";
 84:         $res_fputs = fputs($this->_fp, $line);
 85:         if (!$res_fputs) {
 86:             throw new Passwd_Exception(_("Cannot send command to server."));
 87:         }
 88:         $this->_getPrompt();
 89:     }
 90: 
 91:     /**
 92:      * Changes the user's password.
 93:      *
 94:      * @param string $username      The user for which to change the password.
 95:      * @param string $old_password  The old (current) user password.
 96:      * @param string $new_password  The new user password to set.
 97:      *
 98:      * @throws Passwd_Exception
 99:      */
100:     public function changePassword($username, $old_password, $new_password)
101:     {
102:         $this->_connect();
103: 
104:         try {
105:             $this->_sendCommand('user', $username);
106:         } catch (Passwd_Exception $e) {
107:             $this->_disconnect();
108:             throw new Passwd_Exception(_("User not found") . ': ' . $e->getMessage());
109:         }
110: 
111:         try {
112:             $this->_sendCommand('pass', $old_password);
113:         } catch (Passwd_Exception $e) {
114:             $this->_disconnect();
115:             throw new Passwd_Exception(_("Incorrect old password.") . ': ' . $e->getMessage());
116:         }
117: 
118:         try {
119:             $this->_sendCommand('newpass', $new_password);
120:         } catch (Passwd_Exception $e) {
121:             $this->_disconnect();
122:             throw $e;
123:         }
124:     }
125: }
126: 
API documentation generated by ApiGen