<?php
	/**
	* Authentication based on SQL table
	* @author Dan Kuykendall <seek3r@phpgroupware.org>
	* @author Joseph Engo <jengo@phpgroupware.org>
	* @copyright Copyright (C) 2000-2004 Free Software Foundation, Inc. http://www.fsf.org/
	* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
	* @package phpgwapi
	* @subpackage accounts
	* @version $Id: class.auth_sql.inc.php,v 1.1.1.2 2004/04/06 23:11:24 kazuyan Exp $
	*/

	/**
	* Authentication based on SQL table
	*
	* @package phpgwapi
	* @subpackage accounts
	*/
	class auth
	{
		var $previous_login = -1;
		var $xmlrpc_methods = array();

		function auth()
		{
			$this->xmlrpc_methods[] = array(
				'name'       => 'change_password',
				'decription' => 'Change the current users password'
			);
		}

		function authenticate($username, $passwd, $passwd_type)
		{
			$db = $GLOBALS['phpgw']->db;

			if ($passwd_type == 'text')
			{
				$_passwd = md5($passwd);
			}

			if ($passwd_type == 'md5')
			{
				$_passwd = $passwd;
			}

			$db->query("SELECT * FROM phpgw_accounts WHERE account_lid = '$username' AND "
				. "account_pwd='" . $_passwd . "' AND account_status ='A'",__LINE__,__FILE__);
			$db->next_record();

			if ($db->f('account_lid'))
			{
				$this->previous_login = $db->f('account_lastlogin');
				return True;
			}
			else
			{
				return False;
			}
		}

		function change_password($old_passwd, $new_passwd, $account_id = '')
		{
			// Don't allow passwords changes for other accounts when using XML-RPC
			if (! $account_id || $GLOBALS['phpgw_info']['flags']['currentapp'] == 'login')
			{
				$account_id = $GLOBALS['phpgw_info']['user']['account_id'];
				$pwd_check  = " and account_pwd='" . md5($old_passwd) . "'";
			}

			$encrypted_passwd = md5($new_passwd);

			$GLOBALS['phpgw']->db->query("update phpgw_accounts set account_pwd='" . md5($new_passwd) . "',"
				. "account_lastpwd_change='" . time() . "' where account_id='" . $account_id . "'" . $pwd_check,__LINE__,__FILE__);

			if ($GLOBALS['phpgw']->db->affected_rows())
			{
				$GLOBALS['phpgw']->session->appsession('password','phpgwapi',base64_encode($new_passwd));
				return $encrypted_passwd;
			}
			else
			{
				return False;
			}
		}

		function update_lastlogin($account_id, $ip)
		{
			$GLOBALS['phpgw']->db->query("update phpgw_accounts set account_lastloginfrom='"
				. "$ip', account_lastlogin='" . time()
				. "' where account_id='$account_id'",__LINE__,__FILE__);
		}
	}
?>
