<?php


class AdminSession {
	// session-lifetime
	var $lifeTime;
	function open($savePath, $sessName) {
	   // get session-lifetime
	   $this->lifeTime = get_cfg_var("session.gc_maxlifetime");
	   // return success
	   if(!$GLOBALS["DB"]->DB_Connection)
	      return false;
	   return true;
	}
	function close() {
	   // mach nichts
	   return true;
	}
	function read($sessID) {
	   // fetch session-data
	   $res = go_db_query("SELECT cSessionData FROM eazysales_adminsession
	                       WHERE cSessionId = '$sessID'
	                       AND nSessionExpires > ".time(),$GLOBALS["DB"]->DB_Connection);
	   // return data or an empty string at failure
	   if($row = go_db_fetch_assoc($res))
	       return $row['cSessionData'];
	   return "";
	}
	function write($sessID,$sessData) {
	   // new session-expire-time
	   $newExp = time() + $this->lifeTime;
	   // is a session with this id in the database?
	   $res = go_db_query("SELECT * FROM eazysales_adminsession
	                       WHERE cSessionId = '$sessID'",$GLOBALS["DB"]->DB_Connection);
	   // if yes,
	   if(go_db_num_rows($res)) {
	       // ...update session-data
	       go_db_query("UPDATE eazysales_adminsession
	                     SET nSessionExpires = '$newExp',
	                     cSessionData = '$sessData'
	                     WHERE cSessionId = '$sessID'",$GLOBALS["DB"]->DB_Connection);
	       // if something happened, return true
	       if(go_db_affected_rows($GLOBALS["DB"]->DB_Connection))
	           return true;
	   }
	   // if no session-data was found,
	   else {
	       // create a new row
	       go_db_query("INSERT INTO eazysales_adminsession (
	                     cSessionId,
	                     nSessionExpires,
	                     cSessionData)
	                     VALUES(
	                     '$sessID',
	                     '$newExp',
	                     '$sessData')",$GLOBALS["DB"]->DB_Connection);
	       // if row was created, return true
	       if(go_db_affected_rows($GLOBALS["DB"]->DB_Connection))
	           return true;
	   }
	   // an unknown error occured
	   return false;
	}
	function destroy($sessID) {
	   // delete session-data
	   go_db_query("DELETE FROM eazysales_adminsession WHERE cSessionId = '$sessID'",$GLOBALS["DB"]->DB_Connection);
	   // if session was deleted, return true,
	   if(go_db_affected_rows($GLOBALS["DB"]->DB_Connection))
	       return true;
	   // ...else return false
	   return false;
	}
	function gc($sessMaxLifeTime) {
	   // delete old sessions
	   go_db_query("DELETE FROM eazysales_adminsession WHERE nSessionExpires < ".time(),$GLOBALS["DB"]->DB_Connection);
	   // return affected rows
	   return go_db_affected_rows($GLOBALS["DB"]->DB_Connection);
	}

	function AdminSession() {
		session_name("eSConnectorAdm");
		session_start();
	}
}