<?php
if (eregi("ClassAccount.php",$_SERVER['PHP_SELF'])) {
   die("Non potete accedere direttamente a questo file");
}
else{
// ############# //
// INIZIO FILE   //
// ############# //
class ClassAccount{
// ############### //
// INIZIO CLASSE   //
// ############### //
// Determina come passare il valore delle sessioni
var $_AUTH = array();
function ClassAccount(){
  // Vedi default.configuration.php
  // Setto il tipo di passaggio
	$this->_AUTH["TRANSICTION METHOD"] = _TRANSACTION_METHOD_;
}
function auth_get_option($opt_name){
  // Recupero il tipo di passaggio
	return is_null($this->_AUTH[$opt_name]) ? NULL : $this->_AUTH[$opt_name];
}
function auth_get_uid(){
  // Setto l'id a nullo per assicurarmi che venga recuperato quello corretto
	$uid = NULL;
  // Controllo come passare l'id
	switch($this->auth_get_option("TRANSICTION METHOD")){
    // Se uso i cookie
		case AUTH_USE_COOKIE:
		  global $_COOKIE;
			$uid = $_COOKIE['uid'];
		break;
		// Se uso le sessioni
		case AUTH_USE_SESSION:
		  global $_SESSION;
			$uid = $_SESSION['uid'];
		break;
		// Se uso la querystring tramite link
		case AUTH_USE_LINK:
		  global $_GET;
			$uid = $_GET['uid'];
		break;
	}
  // ritorno il valore trovato
	return $uid ? $uid : NULL;
}
function auth_clean_expired(){
	global $MySql;
  // Recupero l'id
  $uid = $this->auth_get_uid();
  
  // Cerco se nel DB esiste una sessione per l'id recuperato
	$result = $MySql->SqlQuery("SELECT creation_date FROM b2b_sessioni WHERE uid='".$uid."'");
	if($result){
    // Se trovato recupero le info
  	$data = $MySql->SqlFetchArray($result);
		if($data['creation_date']){
      // Controllo che non sia scaduta
			if($data['creation_date'] + _SESSION_GC_TIME_ <= time()){
				switch($this->auth_get_option("TRANSICTION METHOD")){
          // Caso con passaggio cookie
					case AUTH_USE_COOKIE:
						setcookie('uid');
					break;
					// Caso con passaggio sessioni
					case AUTH_USE_SESSION:
						$_SESSION['uid'] = NULL;
					break;
          // Caso con passaggio link
					case AUTH_USE_LINK:
						global $_GET;
						$_GET['uid'] = NULL;
					break;
				}
      
			}
		}
	}
  $MySql->SqlQuery("
	DELETE FROM b2b_carrello_temp
	WHERE creation_date + "._SESSION_GC_TIME_." <= ".time()
 );
	$MySql->SqlQuery("
	DELETE FROM b2b_sessioni
	WHERE creation_date + "._SESSION_GC_TIME_." <= ".time()
 );
}
function auth_get_status(){
	global $MySql;
  
  // Recupero l'id
  $uid = $this->auth_get_uid();
  // Se l'id esiste aggiorno le sessione evitando che scadano
  if($uid != NULL){
  $result = $MySql->SqlQuery("UPDATE  b2b_sessioni
  	 SET creation_date='" . time() . "' WHERE uid = '".$uid."'");
  $result = $MySql->SqlQuery("UPDATE  b2b_carrello_temp
  	 SET creation_date='" . time() . "' WHERE id_sessione = '".$uid."'");
  }
  // Avvio la procedura per cancellare le sessioni scadute
  $this->auth_clean_expired();
	if(is_null($uid)){
    // Se nullo allora ritorno AUTH_NOT_LOGGED
		return array(100, NULL);
  }else{
  // Altrimenti cerco nel db l'id
	$result = $MySql->SqlQuery("SELECT *
	FROM b2b_sessioni S,b2b_account U
	WHERE S.user_id = U.id_account and S.uid = '".$uid."'");
  $num = $MySql->SqlNumRows($result);
  
	if($num != 1){
    // Se non trovato allora passo AUTH_NOT_LOGGED
  	return array(100, NULL);
	}else{
    // Se ttrovato passo AUTH_LOGGED e le info dell'account
		$user_data = $MySql->SqlFetchAssoc($result);
		return array(99, array_merge($user_data, array('uid' => $uid)));
	}
 }
}
function auth_login($uname, $passw){
	global $MySql;
  // Controllo nel DB se esiste un REC con i val passati
	$result = $MySql->SqlQuery("SELECT * FROM b2b_account	WHERE user='".$uname."' and pwd=MD5('".$passw."')");
  // Controllo che esista un risultato
  $num = $MySql->SqlNumRows($result);
	if($num != 1){
    // Se non esiste passo AUTH_INVALID_PARAMS
		return array(104, NULL);
	}else{
    // Se esiste passo AUTH_LOGEDD_IN
		$data = $MySql->SqlFetchArray($result);
		return array(105, $data);
	}
}
function auth_generate_uid(){
  // Genero un'id casuale
	list($usec, $sec) = explode(' ', microtime());
	mt_srand((float) $sec + ((float) $usec * 100000));
	// Restituisco l'id
	return md5(uniqid(mt_rand(), true));
}
function auth_register_session($udata){
	global $MySql;
	// Genero l'id
	$uid = $this->auth_generate_uid();
  // Carico la sessione nel DB
	$id_query = $MySql->SqlQuery("INSERT INTO b2b_sessioni (uid, user_id, creation_date) VALUES ('".$uid."', '".$udata['id_account']."', ". time().")");
  // Controllo che sia stato effettuato un'inserimento
  $UidInsert = $MySql->SqlInsertId();
	if(isset($UidInsert) || $UidInsert != 0 || $UidInsert != NULL){
		return array(105, $uid);
	}else{
		return array(106, NULL);
	}
}
function auth_logout(){
	global $MySql;
  // Recupero l'id
	$uid = $this->auth_get_uid();
	if(is_null($uid)){
    // Se gia nullo allora niente
		return false;
	}else{
    // Cancello la sessione dal DB
		$result = $MySql->SqlQuery("DELETE FROM b2b_sessioni WHERE uid = '".$uid."'");
    // Cancello il carrello per quella sessione
		$result = $MySql->SqlQuery("DELETE FROM b2b_carrello_temp WHERE id_sessione ='" . $uid . "'");
		
    return true;
	}
}
// ############# //
// FINE CLASSE   //
// ############# //
}
// ########### //
// FINE FILE   //
// ########### //
}