Location: PHPKode > scripts > PHP poppassd client > php-poppassd-client/poppassd.php
<?php
/*
 *  PHP popassd client.
 *
 *  This class allow PHP communicate witha  PopPass server and changes the password
 *  of mail users.
 *
 *  The author disclaims the copyright over this class.
 *
 *  Cesar D. Rodas
 *  hide@address.com    
 */
define('PASS_CHANGED'      ,0);
define('ERROR_HOST'        ,1);
define('ERROR_PROTOCOL'    ,2);
define('ERROR_PASSWORD'    ,3);
define('ERROR_NPASSSHORT'  ,4);
define('ERROR_NPASSSIMPLE' ,5);
define('ERROR_ARGS'        ,6);

class poppassd {
    var $uname;
    var $pass;
    var $npass;
    var $server;
    var $port;
    var $debug=false;
    
    /**/
    var $_errStr = array(
        ERROR_HOST        => "The is not connection with the host",
        ERROR_PROTOCOL    => "Protocol Error",
        ERROR_PASSWORD    => "Incorrect new password",
        ERROR_NPASSSHORT  => "The new password is very short",
        ERROR_NPASSSIMPLE => "The new password is very simple",
        ERROR_ARGS        => "There is missing arguments"
    );
    var $_isOpenedSocket;
    var $_socket;
  
    function poppassd($uname='', $pass='', $npass='',$server='', $port=106) {
        $this->uname = $uname;
        $this->pass = $pass;
        $this->npass = $npass;
        $this->server = $server;
        $this->port = $port;
    }
    
    function errorMessage($errNro) {
        if ( ! is_int($errNro) ) return false;
        return $this->_errStr[$errNro];
    }
    function is_ValidArgs() {
        $i=$e=0; 
        
        foreach( func_get_args() as $arg) {
            if ( strlen($arg) < 1) {
                $i=$e;
                break;
            }
            $e++;
        }
        return $i;
    }
    
    function changePasswd() {
        if ( ($i=$this->is_ValidArgs($this->uname,$this->pass,$this->npass,$this->server, $this->port)) > 0) 
            return ERROR_ARGS;
            
        $this->_socket = $this->createSock();
        if ( $this->_socket === false ) 
            return ERROR_HOST;
        
        $msgNumber=0; 
        $msgText="";
        $itineration=1;
        
        while ( $this->readLine($msgNumber,$msgText)!==false ) {
            
            switch( $itineration ) {
                case 1:         //First message, should be a welcome note
                    if ($msgNumber != 200) 
                        return ERROR_HOST; //We're connecting not to a poppassd server
                    $this->writeLine("user ".$this->uname);
                    break;
                case 2:
                    if ($msgNumber != 200) 
                        return ERROR_HOST; //this error should never happend, but... ;-) always check
                    $this->writeLine("pass ".$this->pass);
                    break;
                case 3:
                    if ($msgNumber != 200) 
                        return ERROR_PASSWORD; //Error in password
                    $this->writeLine("newpass ".$this->npass);
                    break;
                case 4:
                    /*
                     *  Error changing the password, this may be by an PAM cracklib error
                     *  because the new-password is too weak
                     */
                    if ($msgNumber != 200) {
                        if ( strpos($msgText,'dictionary') !== false || strpos($msgText,'simplistic')!==false)
                            return ERROR_NPASSSIMPLE;
                        else 
                            return ERROR_NPASSSHORT;
                    }
            }
            if ($itineration==4) break; //last itineration, so quit of bucle
            $itineration++;
        }
        
        return PASS_CHANGED;
    }
    
    function readLine(&$number,&$text) 
    {
        $txt    =  socket_read($this->_socket, 150);
        if ($txt === false)
            return false;
        $tmp = explode(" ",$txt,2);
        
        $number = (int) $tmp[0];
        $text   = $tmp[1];
        if ($this->debug)
            echo "Server: <b>".$txt."</b><br/>";
        return true;
    }
    
    function writeLine($txt) {
        $txt.="\r\n";
        socket_write($this->_socket, $txt, strlen($txt));

        if ($this->debug)
            echo "Me: <b>".$txt."</b><br/>";
        
    }
    
    function createSock() {
        $this->_isOpenedSocket = false;
        if (($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) 
            return false;
        if ( socket_connect($socket, gethostbyname($this->server), $this->port) === false) 
            return false;
        $this->_isOpenedSocket = true;
        return $socket;
        
    }
}
?>
Return current item: PHP poppassd client