Location: PHPKode > projects > JACR - Just Another Checkbook Register > jacr_0.0.4/jacr-platform/users.class.php
<?php
require_once('DBConn/table.class.php');

class Users extends DBTable
{
var $userId;
public function __construct()
{
	
	global $dbname, $dbTablPre, $installMode;
	$this->tablename       =  $dbTablPre.'users';
	$this->dbname          = $dbname;
	$this->rows_per_page   = 0;
	$this->userId = $userId;
	$this->fieldlist = array(
					 'id' => 			array('int(10) unsigned ', 'NOT NULL', NULL, 'auto_increment'),
					 'username' =>		array('varchar(255)', 'NOT NULL', NULL, NULL),
					 'password' =>		array('varchar(255)', 'NOT NULL', NULL, NULL),
					 'cookie' =>		array('varchar(255)', 'NOT NULL', NULL, NULL),
					 'expires' =>		array('DATETIME', 'NOT NULL', NULL, NULL, NULL),
					 'ip' =>		array('varchar(15)', 'NOT NULL', NULL, NULL)
					 );
	$this->pKEY='id';
	$this->uniqueKEY[] = 'username';
	if($installMode===true)
		$this->createUser();
	elseif ( isset($_POST['un']) && isset($_POST['pw']) )
		$this->login_session_start();
	else
		$this->verify_or_logIn();
}

private function login_session_start()
{

	$username = $_POST['un'];
	$password = $_POST['pw'];
	$userData = $this->getData('username = "'.$username.'"');
	if (is_array($userData)){
		if($this->cryptPassword($password, $userData[0]['password']) ==  $userData[0]['password']){
			$this->set_user_cookies($userData[0]['id']);
			return  $userData[0]['id'];
		}
	}
}
	
private function set_user_cookies($uid)
{
	$randomCookie = base64_encode($this->generateCookie());
	setcookie("uid", $uid, time()+3600);
	setcookie("xrc", $randomCookie, time()+3600);
	$date = new DateTime();
	$date->modify("+1 hour");
	$expires = $date->format("Y-m-d H:i:s");
	$setUserData = array(
					 'id'	=> $uid,
					 'expires'=> $expires,
					 'cookie' => base64_decode($randomCookie),
					 'ip'	=> $_SERVER['REMOTE_ADDR']
					 );
	$this->updateRecord($setUserData);
}
	
private function verify_or_logIn()
{
	if (isset($_GET['logout'])){
		setcookie("uid", NULL);
		setcookie("xrc", NULL);
			$fmsg = "You where successfully logged out. Please log in again:";
	}
	elseif ( isset($_COOKIE['uid']) ){
		$userData = $this->getRecord($_COOKIE['uid']);
		if( is_array($userData) && isset($_COOKIE['xrc']) ){
			if ( $userData['cookie'] == base64_decode($_COOKIE['xrc']) ){
				if ( $userData['ip'] == $_SERVER['REMOTE_ADDR'] ){
					$currentDate = new DateTime();
					$expDate = new DateTime($userData['expires']);
					if ($currentDate < $expDate ){
						$this->set_user_cookies($_COOKIE['uid']);	
						return true;
					}			
					else {
						$fmsg = "Your session has expired. Please log in again.";
					}
				}			
				else {
					$fmsg = "Verification failed due to change in IP address";
				}
			}
			else {
				$fmsg = "Verification failed";
			}
		}
		else {
			$fmsg = "Verification failed";
		}
	}
	else {
		$fmsg = "";
	}
	die($this->loginForm($fmsg));
	return false;		
}
private function loginForm($message)
{
	showHeader();
	echo '
	<fieldset style="float:left;"><legend>Please log in:</legend>
	<form action="" method="post" id="loginForm">
		<div class="systemnote">'.$message.'</div>
		<label><span>Username:</span><input type="text" value="" name="un"/></label>
		<label><span>Password:</span><input type="password" name="pw"/></label>
		<div class="cntr"><input type="submit" name="login" value="Login"/></div>
	</form>
	</fieldset>
	';
	showFooter();
}
private function createUser()
{

	if (isset($_GET['createuser'])){
		if( ($_POST['pw'][0] != $_POST['pw'][1]) || !isset($_POST['un']) ){
			echo "<big>Passwords do not match or username was empty</big>";
		}
		else{
			$un = $_POST['un'];
			$pw = $this->cryptPassword($_POST['pw'][0]);
			$checkUN = $this->getData("username=\"$un\"");
			if(is_array($checkUN)){
				echo "<big>Username $un already exists!</big>";
			}
			else{
				$this->insertRecord(array( 'username' => $un, 'password' => $pw));
				echo "$un created!<hr/><a href=\"index.php\">Go to Jacr!</a>";
				return true;
			}
		}
	}
	showHeader();
	?>
	<fieldset style="float:left;"><legend>Create a new user:</legend>
	<form id="createUserForm" method="post" action="?createuser">
		<label><span>Username:</span><input type="text" value="" name="un"></label>
		<label><span>Password:</span><input type="password" name="pw[]"></label>
		<label><span>Password:</span><input type="password" name="pw[]"></label>
		<div class="cntr"><input type="submit" name="createuser" value="Create User" /></div>
	</form>
	</fieldset>
	<?php
	showFooter();
}
private function generateCookie()
{
      $code = md5(uniqid(rand(), true));
      return $code;
}
private function cryptPassword($pw, $salt=NULL)
{
	return crypt($pw,$salt);
}
}
Return current item: JACR - Just Another Checkbook Register