Location: PHPKode > projects > Jethro Pastoral Ministry Manager > jethro-pmm/include/user_system.class.php
<?php
require_once dirname(__FILE__).'/general.php';
class User_System
{
	var $_error; 

	function User_System()
	{
		if (!empty($_REQUEST['logout'])) {
			$_SESSION['user'] = NULL;
		} else if (empty($_SESSION['user']) && !empty($_POST['username'])) {
			// process the login form
			if (array_get($_SESSION, 'login_key', NULL) != $_POST['login_key']) {
				$this->_error = 'Login Key Incorrect.  Please try again.';
				return;
			}
			$user_details = $this->_findUser($_POST['username'], $_POST['password']);
			if (is_null($user_details)) {
				$this->_error = 'Incorrect username or password';
			} else {
				$_SESSION['user'] = $user_details;
			}
		}

	}//end constructor


	function setError($s)
	{
		$this->_error = $s;
	}


	function hasUsers()
	{
		$sql = 'SELECT count(*) FROM staff_member';
		$res = $GLOBALS['db']->queryRow($sql);
		if (PEAR::isError($res)) {
			$res = 0;
		}
		return (bool)$res;
	}



	function getCurrentUser($field='')
	{
		if (empty($_SESSION['user'])) {
			return NULL;
		} else {
			if (empty($field)) {
				return $_SESSION['user'];
			} else {
				return array_get($_SESSION['user'], $field, '');
			}
		}

	}//end getCurrentUser()


	function printLogin()
	{
		$_SESSION['login_key'] = $login_key = $this->_generateLoginKey();
		require TEMPLATE_DIR.'/login_form.template.php';

	}//end printLogin()


	function _generateLoginKey()
	{
		$res = '';
		for ($i=0; $i < 256; $i++) {
			$res .= ord(rand(32, 126));
		}
		return $res;

	}//end _generateLoginKey()


	function _findUser($username, $password)
	{
		$db =& $GLOBALS['db'];
		$sql = 'SELECT *
				FROM staff_member sm
					JOIN person p ON sm.id = p.id
				WHERE sm.username = '.$db->quote($username).'
					AND active = 1';
		$row = $db->queryRow($sql);
		if (PEAR::isError($row)) {
			return NULL;
		}
		if (!empty($row) && crypt($password, $row['password']) == $row['password']) {
			return $row;
		}
		return NULL;

	}//end _validateUser()


}//end class
?>
Return current item: Jethro Pastoral Ministry Manager