Login   Register  
PHP Classes
elePHPant
Icontem

File: sauthclass.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of markitos  >  sauth  >  sauthclass.php  >  Download  
File: sauthclass.php
Role: ???
Content type: text/plain
Description: A easly way to access control and track user's sessions. Is very easy.
Class: sauth
Author: By
Last change:
Date: 13 years ago
Size: 7,695 bytes
 

Contents

Class file image Download
<?
/* #: sauth class @@ mArkitos]|) ))  ))) */
class sauth {
	var $_UsernameDb;
	var $_PasswordDb;
	var $_HostDb;
	var $_Database;			
	var $_UsernameSession;
	var $_PasswordSession;
	var $_UniqIdSession;
	var $_DbLink;
	
  Function sauth ($ServerName, $UserDb, $PassDb, $DatabaseName)
  {
  	if ((!$ServerName)||(!$UserDb)||(!$PassDb)||(!$DatabaseName)){    	
    	return FALSE;
    }
		$this->UsernameDb($UserDb);
		$this->PasswordDb($PassDb);
		$this->HostDb($ServerName);
		$this->Database($DatabaseName);
		$this->UsernameSession(' ');
		$this->PasswordSession(' ');
		$this->UniqIdSession(' ');
		$this->DbLink(' ');
  }
  Function StartSession ($Login, $Password, $ParanoicMode)
  {
  	if ((!$Login)||(!$Password)){
  		return FALSE;
    }
    $this->UsernameSession($Login);
    $this->PasswordSession($Password);
    $Status = $this->ValidateUser();
    if ((!$Status)||($Status<1)){    	
    	return FALSE;
    }
    $UserId = $Status;
    $Atemp = 1;
    while (1){
    	if ($Atemp >= 5){
    		return FALSE;
    	}
    	$SessionId = $this->GetSessionId();
    	if (($SessionId) || (strlen ($SessionId)>15)){	
    		break;
    	}
    	$Atemp++;
    }
    $this->UniqIdSession($SessionId);
    $Status = $this->UpdateSessionId($UserId, $ParanoicMode);
    if (!$Status){
    	return FALSE;
    }
    return $this->UniqIdSession('');    	 
  }    
  Function CheckSessionId ($CheckSessionId, $UserIp)
  {
  	$Fields = "id,timestart,userdataid,paranoical,sessionip";
  	$Tables = "session";
  	$Data = array (
  		"uniqid"	=>	$CheckSessionId
  	);
  	list ($SessionId, $TimeStartSession, $UserDataId, $IsInParaMode, $RemoteIp) = $this->GetUserData ($Fields, $Tables, $Data);
		if (($UserDataId < 1)||($TimeStartSession<1)||($SessionId<1)){
			return FALSE;
		}
		$this->UniqIdSession($CheckSessionId);
		
		
  	$Fields = "timeout";
  	$Tables = "userdata";
  	$Data = array (
  		"id"	=>	$UserDataId
  	);
  	list ($UserTimeOut) = $this->GetUserData ($Fields, $Tables, $Data);
		
		$CurrentTime = time();
		$SessionTime = $CurrentTime-$TimeStartSession;
		if ($SessionTime > $UserTimeOut){
			$this->DeleteSessionId ();
			return FALSE;
		}
    if ($IsInParaMode==1){
    	if ($RemoteIp != $UserIp){
      	return FALSE;
      }
    }
		$this->UpdateTimeSessionId();
		return TRUE;
  }
  Function ConnectMysql()
  {     
  	$Status = mysql_connect ($this->HostDb(''), $this->UsernameDb(''), $this->PasswordDb(''));
    if (!$Status){    	
    	return FALSE;
    }
    $this->DbLink($Status);
    $Status = mysql_select_db ($this->Database(''));
    if (!$Status){
    	return FALSE;
    }
    return TRUE;
  }
  Function CloseMysql()
  {
  	if ($this->DbLink('')){
    	mysql_close ($this->DbLink(''));
    }		
    return TRUE;
  }
  Function ValidateUser()
  {
     
   	$Fields = "id";
    $Tables = "userdata";
    $Data = array (
    	"name"		=>	$this->UsernameSession(''),
      "passname"	=>	$this->PasswordSession('')
     );   
    list ($Id) = $this->GetUserData ($Fields, $Tables, $Data);
		if ($Id < 1){
    	return FALSE;
    }
   	$Fields = "name, passname";
    $Tables = "userdata";
    $Data = array (
    	"id"		=>	"$Id"
     );
    list ($NameUser, $PassUser) = $this->GetUserData ($Fields, $Tables, $Data);
    
    if ((ereg("^$NameUser$", $this->UsernameSession('')))&&(ereg("^$PassUser$", $this->PasswordSession('')))){
    	return $Id;
    }
    return FALSE;		
  }
  Function GetUserData ($Selected, $Table, $Data)
  {
		$NumDatas = 0;
    while (each ($Data)){
    	$NumDatas++;
    }
		if ((!$Selected)|($NumDatas<1)){
    	return FALSE;
    }
    reset ($Data);
    $Sql = "SELECT $Selected FROM $Table ";
    if ($NumDatas>0){
    	$Sql.= "WHERE ";
      $Counter = 1;
      while (list ($Key, $Value) = each ($Data)){
      	$Sql .= $Key."='".$Value."' ";
         if ($Counter<$NumDatas){
         	$Sql .= "AND ";
         }
         $Counter++;
     	}
		}
 		$Status = $this->ConnectMysql();
    if (!$Status){
    	return FALSE;
    }
		$Query = mysql_query ($Sql, $this->DbLink(''));
    if (!$Query){
      $Status = $this->CloseMysql();    
    	return FALSE;
    }
    $Status = mysql_num_rows ($Query);
    if ($Status < 1){
    	$Status = $this->CloseMysql();
    	return FALSE;     
    }
    $Data = mysql_fetch_row ($Query);
      $Status = $this->CloseMysql();    
		return $Data;
  }  
  Function UpdateSessionId ($UpdateId, $ParanoicMode)
  {
  	$ParanoicalValue = 0;
    $ParanoikalIp = 0;
		if ((!$this->UniqIdSession('')) || (strlen ($this->UniqIdSession(''))<15)){
			return FALSE;
		}
		$Sql = "INSERT INTO session (id, uniqid, timestart, userdataid, paranoical, sessionip) ";
		if (($ParanoicMode)&&(ereg("[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+",$ParanoicMode))){
    	$ParanoicalValue = 1;
      $ParanoikalIp = $ParanoicMode;
      unset ($ParanoicMode);
    }
		$Sql .= "VALUES ('', '";
    $Sql .= $this->UniqIdSession('');
    $Sql .=  "', '". time();
    $Sql .= "', '$UpdateId', '$ParanoicalValue', '$ParanoikalIp')";    
		$Status = $this->ExecSql ($Sql);
		if (!$Status){
			return FALSE;
		}
		return TRUE;
  }
  Function ExecSql ($Statment)
  {
  	$Status = $this->ConnectMysql();
    if (!$Status){
    	return FALSE;
    }
    $Query = mysql_query ($Statment);
    $this->CloseMysql();
 		if (!$Query){ 			
 			return FALSE;
 		}
 		return TRUE;    
  }
  Function GetSessionId()
  {
		$NewId = time();
		$NewId .= crypt ($NewId);
		$NewId .= uniqid ($NewId);
    if ((!$NewId)||(strlen($NewId)<15)){
    	return FALSE;
    }		
    $Fields = "id";
    $Tables = "session";
    $Data = array (
    	"uniqid"	=>	$NewId    	
    );
    $Status = $this->GetUserData ($Fields, $Tables, $Data);
    if (!$Status){
    	return $NewId;
    }
  	return FALSE;
  }

  Function UpdateTimeSessionId()
  {
  	$NewTime = time();
  	$Sql = "UPDATE session SET timestart='$NewTime' WHERE uniqid='";
 		$Sql .= $this->UniqIdSession(''); 	
 		$Sql .= "'";
 		$this->ExecSql ($Sql);
 		return;
  }
  Function DeleteSessionId()
  {
  	$Sql = "DELETE FROM session WHERE uniqid='";
  	$Sql .= $this->UniqIdSession('');
  	$Sql .= "'";
  	$this->ExecSql($Sql);
  	return;
  }
	Function UsernameDb ($UDbValue)
  {  	
  	if (!$UDbValue){    	  	
    	return $this->_UsernameDb;
    }
    $this->_UsernameDb = $UDbValue;
    return 1;    
  }
	Function PasswordDb ($PDbValue)
  {
  	if (!$PDbValue){
    	return $this->_PasswordDb;
    }
    $this->_PasswordDb = $PDbValue;
    return 1;    
  }
	Function HostDb ($HDbValue)
  {
  	if (!$HDbValue){
    	return $this->_HostDb;
    }
    $this->_HostDb = $HDbValue;    
    return 1;    
  }
	Function Database ($DbValue)
  {
  	if (!$DbValue){
    	return $this->_Database;
    }
    $this->_Database = $DbValue;
    return 1;    
  }
	Function UsernameSession ($UValue)
  {
  	if (!$UValue){
    	return $this->_UsernameSession;
    }
    $this->_UsernameSession = $UValue;
    return 1;    
  }
	Function PasswordSession ($PValue)
  {
  	if (!$PValue){
    	return $this->_PasswordSession;
    }
    $this->_PasswordSession = $PValue;
    return 1;    
  }
  Function UniqIdSession ($SIdValue)
  {
  	if (!$SIdValue){
    	return $this->_UniqIdSession;
    }
    $this->_UniqIdSession = $SIdValue;
    return 1;     
  }
  Function DbLink ($DbLinkValue)
  {
  	if (!$DbLinkValue){    	
    	return $this->_DbLink;
    }
    
    $this->_DbLink = $DbLinkValue;
    return 1;
  }
 
}			
?>