HordeVacationDriverPostfixadmin

From xbe wiki

Jump to: navigation, search

Vacation is a nice module and the horde driver modul makes it easy to implement own drivers. So I made one which supports the vacation.pl supplied with postfixadmin to work with Horde. vacation.pl supposes that the data lies in MySQL, so it was easy to make a driver for.

The code is not as nice as it could be and doesn't conform to the horde coding guidelines. Can't do everything..

Modified files

config/conf.xml

Inside

<configsection name="server">
 <configheader>Server configuration</configheader>
 <configswitch name="driver" desc="The driver to use">forwards

We define a new case tag..

  <case name="postfixadmin" desc="Own Postfixadmin thingy dude">
  </case>

I don't care about the name.. sounds chill...

The driver: lib/Driver/postfixadmin.php

Here the source for the driver

<?php

class Vacation_Driver_postfixadmin extends Vacation_Driver {


    function checkConfig(&$realm)
    {
        return true;
    }

        function init() {
                $conn mysql_connect("localhost","***","***");
                mysql_select_db("***");
        }

        function checkPW($user$pw) {
                $strQuery "select * from `mailbox` where username='{$user}' and clear=ENCRYPT('{$pw}','".substr($pw,0,3)."')";
                $rs mysql_query($strQuery);
                if(mysql_num_rows($rs) > 0) {
                        return true;
                }else{
                        return false;
                }
        }

        function checkIfAlias($user$domain) {
                $strQuery "select * from `alias` where `address`='{$user}'";
                $rs mysql_query($strQuery);
                if(mysql_num_rows($rs) < 1) {
                        $strQuery "insert into `alias` (`address`, `goto`, `domain`, `created`, `modified`)
                       VALUES ('{$user}', '{$user}', '{$domain}', now(), now())";
                        mysql_query($strQuery);
               }
               return true;
        }

    function setVacation($user$realm$password$message$alias)
    {
        $usernameshort $user;
        $user Auth::getAuth();
        $this->init();
        if($this->checkPW($user,$password)) {
                // insert vacation row
                $message mysql_escape_string($_POST["mess"]);
                $subject mysql_escape_string($_POST["subject"]);

                $domain substr($userstrpos($user,"@")+1);

                // cleanup
                $strQuery "delete from vacation where `email`='{$user}';";
                mysql_query($strQuery);

                $strQuery "insert into vacation (`email`, subject, `body`, `domain`, `created`, `active`)
                        VALUES ('{$user}', '{$subject}', '{$message}', '{$domain}', NOW(), 1)
                ";
                mysql_query($strQuery);
                if(mysql_affected_rows() < 1) {
                        $this->err_str "Konnte Vacation-Eintrag nicht einfügen";
                        return false;
                }

                // alias

                // alias schon vorhanden?
                $this->checkIfAlias($user$domain);

                // alias anpassen
                $strQuery "select goto from `alias` where `address`='{$user}' limit 1";
                $rs mysql_query($strQuery);
                $resi mysql_fetch_array($rs);
                $addresses explode(",",$resi["goto"]);
                $addresses[] = $user."@autoreply.domain.tld";
                $newaddresses implode(",",$addresses);

                $strQuery "update `alias` set goto='{$newaddresses}', `modified`=now() where `address`='{$user}' limit 1;";
                mysql_query($strQuery);

                if(mysql_affected_rows() > 0) return true;
                else return false;
        }else{
                $this->err_str _("Check your username and password.");
                return false;
        }
    }

    /**
     * Remove any existing vacation notices.
     *
     * @param string $user      The user to disable vacation notices for.
     * @param string $realm     The realm of the user.
     * @param string $password  The password of the user.
     *
     * @return boolean  Returns true on success, false on error.
     */
    function unsetVacation($user$realm$password)
    {
        $this->init();
        $user Auth::getAuth();
        if($this->checkPW($user,$password)) {
                $search $user."@autoreply.domain.tld";
                $strQuery "select goto from `alias` where `address`='{$user}' limit 1";
                $rs mysql_query($strQuery);
                $resi mysql_fetch_array($rs);

                $aliases explode(",",$resi["goto"]);
                foreach($aliases as $key => $alias) {
                        if($alias == $search) unset($aliases[$key]);
                }
                $newaliases implode(",",$aliases);

                $message mysql_escape_string($_POST["mess"]);
                $subject mysql_escape_string($_POST["subject"]);

                $strQuery "update `alias` set goto='{$newaliases}' where `address`='{$user}';";
                mysql_query($strQuery);

                // update vacation entry
                $strQuery "update `vacation` set active=0, subject='{$subject}', `body`='{$message}' where `email`='{$user}';";
                mysql_query($strQuery);
                return true;
        }else{
                $this->err_str _("Check your username and password.");
                return false;
        }
    }

    /**
     * Retrieve the current vacation details for the user.
     *
     * @param string $user      The username for which to retrieve details.
     * @param string $realm     The realm (domain) for the user.
     * @param string $password  The password for user.
     *
     * @return mixed  Vacation details or false.
     */
    function _getUserDetails($user$realm$password)
    {
        $this->init();
        $this->_details = array();
        $user Auth::getAuth();

        $strQuery "select * from `vacation` where `email`='{$user}';";
        $rs mysql_query($strQuery);

        if(mysql_num_rows($rs) > 0) {
                $resi mysql_fetch_array($rs);
                if(strlen(trim($resi["body"])) > 0) {
                        $message "Subject: {$resi["subject"]}\n{$resi["body"]}";
                }else{
                        $message "";
                }
                $this->_details['message'] = $message;
                if($resi["active"] == 1$this->_details['vacation'] = 'y';
                else $this->_details['vacation'] = "n";
        }else{
                $this->_details['vacation'] = 'n';
        }
        return $this->_details;
    }

}


?>

Remarks: If someone finds this via Google, some stuff to consider:

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox