[sork] vacation module updated in HEAD

Ilya mail@krel.org
Fri, 26 Jul 2002 16:19:56 -0400


---------------------- multipart/mixed attachment
here is an sql Driver. for it to work you also need to apply this diff to
main.php (not in cvs yet):

31c31
<     if (in_array($userid, $conf['user']['refused'])) {
---
>     if (in_array($user, $conf['user']['refused'])) {
53c53
<             if ($driver->set_vacation($userid, $realm, $oldpassword, $vacationmsg)) {
---
>             if ($driver->set_vacation($user, $realm, $oldpassword, $vacationmsg)) {
63c63
<         if ($driver->unset_vacation($userid, $realm, $oldpassword)) {
---
>         if ($driver->unset_vacation($user, $realm, $oldpassword)) {

and these are the settings for conf.php, this was done for exim type sql setup, 
so your milage may vary with different MTA and sql structure.
diff -r conf.php.dist conf.php
18c18,19
< $conf['server']['driver'] = 'forwards';
---
> // sql:               An sql driven driver for exim vacation driver
> $conf['server']['driver'] = 'sql';
35c36
< $conf['server']['params']['default'] = array(host => 'localhost', port => 21);
---
> //$conf['server']['params']['default'] = array(host => 'localhost', port => 21);
44a46,61
>
> // For sql we use usual horde sql settings plus some:
> $conf['server']['params'] = array();
> $conf['server']['params']['phptype'] = 'mysql';
> $conf['server']['params']['hostspec'] = 'localhost';
> $conf['server']['params']['username'] = 'user';
> $conf['server']['params']['password'] = '*****';
> $conf['server']['params']['vacation'] = 'vacation'; // y/n ENUM
> $conf['server']['params']['message'] = 'vacation_msg';
> $conf['server']['params']['subject'] = 'vacation_sub';
> $conf['server']['params']['database'] = 'users';
> $conf['server']['params']['table'] = 'users';
> $conf['server']['params']['user_col'] = 'user';
> $conf['server']['params']['pass_col'] = 'password';
>
>
48c65
< $conf['vacation']['path'] = '/usr/local/bin/vacation';
---
> //$conf['vacation']['path'] = '/usr/local/bin/vacation';




On Fri, Jul 26, 2002 at 03:01:45PM -0500, Eric Rostetter wrote:
> For those wanting the vacation module to be in the Driver class/subclass
> format, that is now done in CVS HEAD.  Let me know if you find any problems.
> 
> There's still a lot of work I want to do on it, but this is a start, and 
> sufficient for others to go ahead with for new backends (procmail, etc).
> 
> -- 
> Eric Rostetter
> The Department of Physics
> The University of Texas at Austin
> 
> "TAD (Technology Attachment Disorder) is an unshakable, impractical devotion
> to a brand, platform, product line, or programming language. It's relatively
> harmless among the rank and file, but when management is afflicted the damage
> can be measured in dollars. It's also contagious -- someone with sufficient
> political clout can infect an entire organization."
> 
> --"Enterprise Strategies" columnist Tom Yager.
> -- 
> Sork mailing list
> Frequently Asked Questions: http://horde.org/faq/
> To unsubscribe, mail: sork-unsubscribe@lists.horde.org
> 

---------------------- multipart/mixed attachment
<?php

/**
 * Vacation_Driver_sql:: implements the Forwards_Driver API for SQL servers.
 *
 * @author  Ilya Krel <mail@krel.org.org>
 * @author  Mike Cochrane <mike@graftonhall.co.nz>
 * @version $Revision: 1.1 $
 * @since   Vacation 
 * @package forwards
 */

class Vacation_Driver_sql extends Vacation_Driver {

	/** file pointer to the sql connection. */
	var $db;

	/** error string returned to user if an eror occurs. */
	var $err_str;

	/** boolean which contains state of sql connection */
	var $connected=false;

	var $params;

	/**
	 * Constructs a new sql Vacation_Driver object.
	 *
	 * @param array  $params	A hash containing connection parameters.
	 */
	function Vacation_Driver_sql($params = array())
	{
		
		$this->params = $params;
	
	}


	/**
	 * Do an sql connect and login as user with privilege to change passwd.
	 *
	 * @return   boolean   True or False based on success of connect
	 *
	 */

	function _connect()
	{

		if (!$this->connected) {
			if (!is_array($this->params)) {
				Horde::fatal(new PEAR_Error(
				_("No configuration information specified for SQL authentication.")),
				__FILE__, __LINE__);
			}
			if (!isset($this->params['phptype'])) {
				Horde::fatal(new PEAR_Error(
				_("Required 'phptype' not specified in authentication configuration.")),
				__FILE__, __LINE__);
			}
			if (!isset($this->params['hostspec'])) {
				Horde::fatal(new PEAR_Error(
				_("Required 'hostspec' not specified in authentication configuration.")),
				__FILE__, __LINE__);
			}
			if (!isset($this->params['username'])) {
				Horde::fatal(new PEAR_Error(
				_("Required 'username' not specified in authentication configuration.")), 
				__FILE__, __LINE__);
			}
			if (!isset($this->params['password'])) {
				Horde::fatal(new PEAR_Error(
				_("Required 'password' not specified in authentication configuration.")), 
				__FILE__, __LINE__);
			}
			if (!isset($this->params['database'])) {
				Horde::fatal(new PEAR_Error(
				_("Required 'database' not specified in authentication configuration.")), 
				__FILE__, __LINE__);
			}
			if (!isset($this->params['table'])) {
				Horde::fatal(new PEAR_Error(
				_("Required 'table' not specified in authentication configuration.")), 
				__FILE__, __LINE__);
			}

			/* Connect to the SQL server using the supplied parameters. */
			include_once 'DB.php';
			$this->db = &DB::connect($this->params, true);
			if (DB::isError($this->db)) {
				Horde::fatal(new PEAR_Error(_("Unable to connect to SQL server.")), __FILE__, __LINE__);
			}

			/* Enable the "portability" option. */
			$this->db->setOption('optimize', 'portability');
			$this->connected = true;
		}

		return true;
	}

  /**
	 * Disconnect from the SQL server and clean up the connection.
	 *
	 * @return boolean true on success, false on failure.
	 */
	function _disconnect()
	{

		if ($this->connected) {
			$this->connected = false;
			return $this->db->disconnect();
		}

		return true;

	}


	/**
	 * Begins forwarding of mail for a user.
	 *
	 * @param string	$user	   The username to enable forwarding for.
	 * @param string	$realm	  The realm of the user.
	 * @param string	$pass	   The password of the user.
	 *
	 * @param string	$target	 The email address that mail should be forwarded to.
	 *
	 * @param optional boolean $keeplocal A flag that if true causes a copy of 
	 *									forwarded email to be kept in the 
	 *									local mailbox.
	 *
	 * @return boolean  Returns true on success, false on error.
	 */
	function set_vacation($user, $realm, $password, $message)
	{

		/* _connect() will die with Horde::fatal() upon failure. */
		$this->_connect();

		// split message into parts to get subject	
		$myarray = explode("\n",$message);

		/* determine if $message  contains Subject: and if it does split it */
		if ( strstr($myarray[0], "Subject:")) {
			$mysubject = $myarray[0];
			$myarray[0] = "" ;
			$mymessage = implode("", $myarray);
		} else {
			$mysubject = "" ;
			$mymessage = $message ;
		}

		/* Build the SQL query. */
		$query = 'UPDATE ' . $this->params['table'];
		$query .= ' set ' . $this->params['vacation'] . ' = ' . $this->db->quote("y");
		$query .= ' WHERE ' . $this->params['user_col'] . ' = ' . $this->db->quote($user);
		$query .= ' and ' . $this->params['pass_col'] . ' = ' . $this->db->quote(md5($password));
 	
		$query1 = 'UPDATE ' . $this->params['table'];
		$query1 .= ' set ' . $this->params['message'] . ' = ' . $this->db->quote($mymessage); 
		$query1 .= ' ,  ' . $this->params['subject'] . ' = ' . $this->db->quote($mysubject);
		$query1 .= ' WHERE ' . $this->params['user_col'] . ' = ' . $this->db->quote($user);
		$query1 .= ' and ' . $this->params['pass_col'] . ' = ' . $this->db->quote(md5($password));

		/* Execute the query. */
		$result = $this->db->query($query);
		$result1 = $this->db->query($query1);

		if (!DB::isError($result) && !DB::isError($result1)) {
			if ($result === DB_OK && $result1 === DB_OK) {
				$this->_disconnect();
				return true;
			} else {
				$this->_disconnect();
				return false;
			}
		} else {
			$this->_disconnect();
			return false;
		}
		$this->_disconnect();
		return false;

	}

	/**
	 * Stops forwarding of mail for a user.
	 *
	 * @param string	$user	   The username of the user.
	 * @param string	$realm	  The realm of the user.
	 * @param string	$pass	   The password of the user.
	 *
	 * @return boolean  Returns true on success, false on error.
	 */
	function unset_vacation($user, $realm, $password)
	{

		// set forwarding flag
		/* _connect() will die with Horde::fatal() upon failure. */
		$this->_connect();

		/* Build the SQL query. */
		$query = 'UPDATE ' . $this->params['table'];
		$query .= ' set ' . $this->params['vacation'] . ' = ' . $this->db->quote("n");
		$query .= ' ,  ' . $this->params['message'] . ' = ' . $this->db->quote("");
		$query .= ' WHERE ' . $this->params['user_col'] . ' = ' . $this->db->quote($user);
		$query .= ' and ' . $this->params['pass_col'] . ' = ' . $this->db->quote(md5($password));

		/* Execute the query. */
		$result = $this->db->query($query);

		if (!DB::isError($result)) {
			if ($result === DB_OK) {
				$this->_disconnect();
				return true;
			} else { 
				$this->_disconnect();
				return false; 
			}
		} else { 
				$this->_disconnect();
				return false; 
		}
		$this->_disconnect();
		return false;

	}
	
	/**
	 * Retrieves status of mail redirection for a user
	 *
	 * @param string	$user	   The username of the user to check.
	 *
	 * @param string	$realm	  The realm of the user to check.
	 *
	 * @return boolean	Returns true if forwarding is enabled for the user or false if
	 *				  forwarding is currently disabled.
	 */
	function isEnabled($user, $realm, $password)
	{

		// get current details
		$current_details = $this->_getUserDetails($user, $realm, $password);
		if ($current_details === false) {
		   //$this->err_str = _("Not able to retrieve current details.");
		   return false;
		}
	
		// check forwarding flag
		if ( $current_details[$this->params['vacation']] === 'y' || 
			$current_details[$this->params['vacation']] === 'Y') {
			 return true;
		 } else { return false; }

	}

	/**
	 * Retrieves current target of mail redirection
	 *
	 * @param string	$user	   The username of the user.
	 * @param string	$realm	  The realm of the user.
	 *
	 * @return string   A string of current forwarding mail address or false.
	 */
	function currentMessage($user, $realm, $password)
	{

		$searchString = ",$user@$realm";
		$current_details = $this->_getUserDetails($user, $realm, $password);
	
		// check current forwarding mail address
		$message = $current_details[$this->params['message']];
		return $message;

	}
	
	/**
	 * Retreive relevant line from sql server
	 *
	 * @param   $user		 The username for which to retrieve detals..
	 * @param   $realm		The realm (domain) for the user.
	 * @param   $password	 The password for user.
	 *
	 * @return  Mixed		 Mysql result resource or (boolean) False
	 */
	function _getUserDetails($user, $realm, $password) 
	{

		/* _connect() will die with Horde::fatal() upon failure. */
		$this->_connect();

		/* Build the SQL query. */
		$query = 'SELECT * FROM ' . $this->params['table'];
		$query .= ' WHERE ' . $this->params['user_col'] . ' = ' . $this->db->quote($user);
		$query .= ' AND password = ' . $this->db->quote(md5($password));

		/* Execute the query. */
		$result = $this->db->query($query);
	
		if (!DB::isError($result)) {
			$row = $result->fetchRow(DB_FETCHMODE_ASSOC);
			 
		if (is_array($row)) {
				$this->_disconnect();
				return $row;
			} else {
				$this->_disconnect();
				$result->free();
				return false;
			}
		}
			$this->_disconnect();
			return false;

	}

}

---------------------- multipart/mixed attachment--