[horde] contributed: cpanel passwd Driver

eduin.carrillo at scoutsace.org eduin.carrillo at scoutsace.org
Thu Jun 10 16:11:34 PDT 2004



I'm Eduin from Colombia, a beauty country located in the head of southamerica.
I am an entusiast Horde user who wants to contribute to this great project.

This is a passwd Driver that uses webprovider cPanel administrative
interface to change IMAP/pop3 passwords.

More about cpanel at www.cpanel.net

It was developed for my Horde site (http://correo.scoutsace.org) that is
hosted at xeon1.datacities.com. This driver was tested with cPanel
version  9.2.0-STABLE_25

You should consider add this sample to the passwd/config/backends.php file:

$backends['cpanel'] = array(
    'name' => 'Example CPanel server',
    'preferred' => '',
    'password policy' => array(),
    'driver' => 'cpanel',
    'params' => array(
        'host' => 'cpanel.example.com',
        'port' => 2082,
        'adminname' => 'youraminuser',
        'adminpass' => 'youradminpassword'
    )
);

I'm working too in a Horde Port for the popular cms phpnuke (phpnuke.org).
You will hear about this project in a few months. Ths project is hosted at:
http://horde2nuke.sourceforge.net

Thank You

Eduin Yesid Carrillo
webmaster at scoutsace.org

_________________________________________________________
ASOCIACION COLOMBIANA DE ESCULTISMO SCOUTS A.C.E.
Visítanos en http://www.scoutsace.org
-------------- next part --------------
<?php
/**
 * $Horde$
 *
 * The cpanel class attempts to change a user's password via a
 * cpanel administrative interface.
 *
 * @author   Eduin Yezid Carrillo Vega <webmaster at scoutsace.org>
 * @package  passwd
 */
class Passwd_Driver_cpanel extends Passwd_Driver {

    /** Pointer to the socket connection. */
    var $_fp;

    /** Hash containing connection parameters. */
    var $params;

    /**
     * Constructs a new cpanel Passwd_Driver object.
     *
     * @param array  $params    A hash containing connection parameters.
     */
    function Passwd_Driver_cpanel($params = array())
    {
        $this->_params['host'] = array_key_exists('host', $params) ? $params['host'] : 'localhost';
        $this->_params['port'] = array_key_exists('port', $params) ? $params['port'] : 2082;
        $this->_params['adminname'] = array_key_exists('adminname', $params) ? $params['adminname'] : '';
        $this->_params['adminpass'] = array_key_exists('adminpass', $params) ? $params['adminpass'] : '';
        
        $splitted  = split("@", Auth::getAuth());
        $domain = @$splitted[1];
        $this->_params['domain'] = array_key_exists('domain', $params) ? $params['domain'] : $domain;
    }

    /**
     * Connect to the server
     */
    function _connect() {
      
        $this->_fp = fsockopen($this->_params['host'], $this->_params['port'], $err_no, $err_str, 30);
        if (!$this->_fp) {
            return PEAR::raiseError($err_str);
        } else {
            return true;
        }
    }

    /**
     * Disconnect from the server
     */
    function _disconnect() {
        if (isset($this->_fp)) {
            fclose($this->_fp);
        }
    }

    /**
     * Parse a response from the server to see what it was
     */
    function _getPrompt($action, $arg) {

        $headers = "";
        while ($str = trim(fgets($this->_fp, 4096))) {
          $headers .= "$str\n";
        }
        
        $prompt = "";
        while (!feof($this->_fp)) {
          $prompt .= fgets($this->_fp, 4096);
        }
        
        $this->_disconnect();
        
        if ($action == "testconn") {
            if (strpos($prompt, "Mail Manager Main Menu") === false) {
                return PEAR::raiseError(_("Password module is not properly configured"));
            } else {
                return true;
            }
        
        } elseif ($action == "testuser") {
            if (strpos($prompt, $arg['email'] . "@" . $arg['domain']) === false) {
                return PEAR::raiseError(_("User not found"));
            } else {
                return true;
            }

        } elseif ($action == "changepass") {

            if (strpos($prompt, "Ignore any messages of success this can only result in failure!") > 0) {
                return PEAR::raiseError(_("Password Restrcitions Error"));;
            } elseif (strpos($prompt, "was successfully modified") > 0) {
                return true;
            } else {
                return PEAR::raiseError(_("Undefined Password Error"));
            }

        } else {
          return PEAR::raiseError(_("Undefined Password Error"));
        }

    }

    /**
     * Send a command to the server
     */
    function _sendCommand($action, $page, $arg) {

        $res = $this->_connect();
        if (PEAR::isError($res)) {
            return $res;
        }
        
        fputs($this->_fp, "POST /frontend/x/mail/" . $page . ".html HTTP/1.0\r\n");
        fputs($this->_fp, "Authorization: Basic " . 
              base64_encode($this->_params['adminname'] . ":" . $this->_params['adminpass']) . "\r\n");

        reset ($arg);
        $data = "";
        while (list ($key, $val) = each ($arg)) {
            $data .= $key . "=" . $val . "&";
        }

        fputs($this->_fp, "Content-type: application/x-www-form-urlencoded\r\n");
        fputs($this->_fp, "Content-length: " . strlen($data) . "\r\n");
        fputs($this->_fp, "Accept: */*\r\n");
        fputs($this->_fp, "\r\n");
        fputs($this->_fp, $data . "\r\n");
        fputs($this->_fp, "\r\n");

        return $this->_getPrompt($action, $arg);
    }
    
    /**
     * Change the user's password.
     *
     * @param   $username    The user for which to change the password.
     * @param   $oldpassword  The old (current) user password.
     * @param   $newpassword  The new user password to set.
     *
     * @return  boolean    True or false based on success of the change.
     */
    function change_password($username,  $oldpassword, $newpassword) {
        
        //Test Connection
        $res = $this->_sendCommand("testconn", "email", array());
        if (PEAR::isError($res)) {
            return $res;
        }
        
        //Test User
        $res = $this->_sendCommand("testuser", "pops", array(
                                                        "email"    => $username ,
                                                        "domain"   => $this->_params['domain'] ,
                                                        "password" => $newpassword));
        if (PEAR::isError($res)) {
            return $res;
        }
        
        //Change Password
        $res = $this->_sendCommand("changepass" ,"dopasswdpop", array(
                                                        "email"    => $username ,
                                                        "domain"   => $this->_params['domain'] ,
                                                        "password" => $newpassword));
        if (PEAR::isError($res)) {
            return $res;
        } else {
            $this->reset_credentials($username, $oldpassword, $newpassword);
            return true;
        } 
    }
}


More information about the horde mailing list