<?php
/*
 * OPTIME Consulting, Inc.
 *
 * Description: Clase Generica para el manejo de las variables de session
 *
 * @author Miguel Aguero
 * @version 1.0  2010/10/21
 */
class SessionManager {
    /******************************************************************
    /*******  VARIABLES
    /******************************************************************/
    var $prefix=null;
    /******************************************************************
    /*******  CONSTRUCTOR
    /******************************************************************/
    public function __construct($prefix) {
	$this->prefix =$prefix.'_'.$_SERVER['SERVER_NAME']."_" ;
    }
    /******************************************************************
    /*******  FUNCIONES
    /******************************************************************/
    /*
     * Crea una variable de session incluyendo la fecha y hora de creacion
     */
    function write($name, $value){
        /*if(!$this->exist($name))
            
        else
            $created = $_SESSION[$name]['created'];*/
        $created = date('y-m-d h:i:s');   
        $_SESSION[$this->prefix.$name]['value'] = $value;
		$_SESSION[$this->prefix.$name]['created'] = $created;
        $_SESSION[$this->prefix.$name]['accessed'] = date('y-m-d h:i:s');
    }
    /*
     * Lee el contenido de la variable de session
     */
    function read($name, $delete = false){
        $value = '';
        $_SESSION[$this->prefix.$name]['accessed'] = date('y-m-d h:i:s');
        if (array_key_exists('value', $_SESSION[$this->prefix.$name])):
            $value = $_SESSION[$this->prefix.$name]['value']; 
        endif;
        if($delete) $this->destroy($name);
        return $value;
    }
	/**
     * Escribe una variable en la seccion
     * @param $name
     * @param $value
     */
    function w($name, $value){
        $this->write($name, $value);
    }
    /**
     * Lee una variable de seccion especifica
     * @param $name
     * @param $delete
     */
    function r($name, $delete = false){
        $this->read($name, $delete);
    }
    /**
     * Destruye una variable de seccion
     * @param $name
     */
    function destroy($name){
        unset($_SESSION[$this->prefix.$name]);
    }
    /**
     * Destruye toda la seccion
     */
    function destroyAll(){
        session_unset();
        session_destroy();
    }
    /**
     * Verifica si existe una varible de seccion
     * @param $name
     * @return
     */
    function exist($name){
        if(isset($_SESSION[$this->prefix.$name]))
            return true;
        else
            return false;
    }
}
?>