Source for file xt_sessionscope.class.php

Documentation is available at xt_sessionscope.class.php

  1. <?php   /*
  2.    *@=@=@=@ START LICENSE @=@=@=@*
  3.    Copyright or © or Copr. Stéphane TRICHET and Nicolas SOTRON
  4.    
  5.    stephane.t@simpliciweb.net and nicolas.s@simpliciweb.net
  6.    
  7.    This software is a computer program whose purpose is to make a
  8.    web hosting platform
  9.    
  10.    This software is governed by the CeCILL  license under French law and
  11.    abiding by the rules of distribution of free software.  You can  use, 
  12.    modify and/ or redistribute the software under the terms of the CeCILL
  13.    license as circulated by CEA, CNRS and INRIA at the following URL
  14.    "http://www.cecill.info". 
  15.    
  16.    As a counterpart to the access to the source code and  rights to copy,
  17.    modify and redistribute granted by the license, users are provided only
  18.    with a limited warranty  and the software's author,  the holder of the
  19.    economic rights,  and the successive licensors  have only  limited
  20.    liability. 
  21.    
  22.    In this respect, the user's attention is drawn to the risks associated
  23.    with loading,  using,  modifying and/or developing or reproducing the
  24.    software by the user in light of its specific status of free software,
  25.    that may mean  that it is complicated to manipulate,  and  that  also
  26.    therefore means  that it is reserved for developers  and  experienced
  27.    professionals having in-depth computer knowledge. Users are therefore
  28.    encouraged to load and test the software's suitability as regards their
  29.    requirements in conditions enabling the security of their systems and/or 
  30.    data to be ensured and,  more generally, to use and operate it in the 
  31.    same conditions as regards security. 
  32.    
  33.    The fact that you are presently reading this means that you have had
  34.    knowledge of the CeCILL license and that you accept its terms.
  35.    *@=@=@=@ END   LICENSE @=@=@=@*
  36.  
  37. */?><?php  
  38. /**
  39.  * xTSessionScope Class
  40.  * 
  41.  * 
  42.  * @author SimpliciWeb
  43.  * @version $Id: xt_sessionscope.class.php 602 2007-07-18 16:18:38Z stephanet $
  44.  * @package Common
  45.  * @subpackage FrameWork
  46.  */    
  47. /**
  48.  * Manage a session scope with PHP session and database for security
  49.  *
  50.  * @author SimpliciWeb
  51.  * @version $Id: xt_sessionscope.class.php 602 2007-07-18 16:18:38Z stephanet $
  52.  * @package Common
  53.  * @subpackage FrameWork
  54.  */
  55.  
  56.     class xTSessionScope {
  57.         
  58.     /**
  59.      * IS existin session
  60.      *
  61.      * @var bool 
  62.      */
  63.     var $m_is_existing_session = null;
  64.     /**
  65.      * Session id
  66.      *
  67.      * @var string 
  68.      */
  69.     var $m_session_id                 = NULL;
  70.  
  71.     /**
  72.      * Database Connexion object
  73.      *
  74.      * @var DbConnexion 
  75.      */
  76.     var $m_object_dbconnexion         = null;
  77.     /**
  78.      * Format Object
  79.      *
  80.      * @var Format 
  81.      */
  82.     var $m_object_format             = null;
  83.  
  84.     /**
  85.      * SQL Request : Update
  86.      *
  87.      * @var string 
  88.      */
  89.     var $req_update = "update  t_session set ses_modified=now()
  90.                                 where ses_id = '%s';";
  91.     /**
  92.      * SQL Request : Insert
  93.      *
  94.      * @var string 
  95.      */
  96.     var $req_insert = "insert into t_session(ses_id,ses_created,ses_modified)
  97.                                 values('%s',now(), now());";
  98.     /**
  99.      * SQL Request : exist
  100.      *
  101.      * @var string 
  102.      */
  103.     var $req_exist = "select ses_id from t_session 
  104.                                 where ses_id = '%s';";
  105.     /**
  106.      * SQL Request : Delete
  107.      *
  108.      * @var string 
  109.      */
  110.     var $req_delete = "delete from t_session 
  111.                                 where DATE_ADD(ses_modified, INTERVAL %d MINUTE) < NOW();";
  112.     /**
  113.      * Time cookie life
  114.      *
  115.      * @var integer 
  116.      */
  117.     var $m_time_cookie_life            = 0;//2678400; //3600*24*31; 1 mois
  118.     /**
  119.      * Session life time
  120.      *
  121.      * @var integer 
  122.      */
  123.     var $m_session_life_time_min    = 30// temps de vie d'une session en minutes
  124.         
  125.     /**
  126.      * Must be destroyed
  127.      *
  128.      * @var bool 
  129.      */
  130.     var $m_must_be_destroyed ;
  131.     /**
  132.      * Session name
  133.      *
  134.      * @var string 
  135.      */
  136.     var $m_session_name ;
  137.     /**
  138.      * 
  139.      *
  140.      * @var string 
  141.      */
  142.     var $test ;
  143.     
  144.     
  145.     /**
  146.      * Constructor, init the requests and objects. And attempt to load a session
  147.      *
  148.      * @param string $p_session_name 
  149.      * @return xTSessionScope 
  150.      */
  151.     function  xTSessionScope($p_session_name{
  152.       global $g_object_loader;
  153.       $this->m_object_format =$g_object_loader->getFormat();
  154.       $this->m_object_dbconnexion =$g_object_loader->getDbConnexion();
  155.  
  156.         $this->m_session_name = $p_session_name;
  157.         
  158.         $this->test = $this->generateUniqueId();
  159.         
  160.  
  161.         session_cache_expire($this->m_session_life_time_min);        
  162.  
  163.         
  164.           if ($this->m_time_cookie_life != 0)
  165.               session_set_cookie_params($this->m_time_cookie_life);
  166.  
  167.           session_name($this->m_session_name);
  168.         
  169.         
  170.         //echo("<br />INIT Session");
  171.         $this->xLoadSession();
  172.     }
  173.  
  174.   /**
  175.    * Force destroy
  176.    *
  177.    */
  178.   function forceDestroy({
  179.       $this->m_must_be_destroyed = true;
  180.   }    
  181.     
  182.   
  183.   /**
  184.    * Get database id for the session id
  185.    *
  186.    * @param string $p_id 
  187.    * @return string 
  188.    */
  189.   function getDatabaseId($p_id ""{
  190.       if ($p_id == "")
  191.           $p_id $this->m_session_id;
  192.       return $this->m_session_name."_".$p_id;
  193.   }
  194.   
  195.  
  196.   /**
  197.    * Destroy if needed (if exist)
  198.    *
  199.    */
  200.   function destroyIfNeeded({
  201.           //if ($this->m_must_be_destroyed)
  202.               //echo("<br />must be destruct");
  203.           //else 
  204.  
  205.           if ($this->m_must_be_destroyed{
  206.             
  207.             
  208.             
  209.             @session_unset();
  210.             if (session_id(<> '')
  211.                 @session_destroy();
  212.             
  213.  
  214.               $this->m_is_existing_session = false;
  215.               $this->m_must_be_destroyed = false;
  216.               $this->m_session_id = "";
  217.               //echo($this->m_session_name);
  218.               //$_COOKIES[$this->m_session_name] = "";
  219.               // session_set_cookie_params (time() - 3600);
  220.               //@setcookie($this->m_session_name, '', time() - 3600);
  221.               
  222.         }
  223.   }
  224.         
  225.  
  226.   /*function mustBeDestroyedDebuger() {
  227.           if ($this->m_must_be_destroyed)
  228.               echo("<br />must be destruct");
  229.           else 
  230.               echo("<br />must NOT be destruct");
  231.       
  232.   }*/
  233.   
  234.   /**
  235.    * unset
  236.    *
  237.    * @access private
  238.    * @param string $p_name 
  239.    */
  240.   function xUnset($p_name{
  241.               $_SESSION[$p_name'';
  242.             unset($_SESSION[$p_name]);
  243.             
  244.             
  245.             if (sizeof($_SESSION== 0{
  246.                 $this->forceDestroy();
  247.             }
  248.   }
  249.  
  250.  
  251.   /**
  252.    * load session
  253.    * 
  254.    * @access private
  255.    * @param bool $p_force_to_load 
  256.    */
  257.   function xLoadSession($p_force_to_load false{
  258.  
  259.             $len_of_php_self strlen($_SERVER['PHP_SELF']);
  260.  
  261.             if (((substr($_SERVER['PHP_SELF']$len_of_php_self 3== ".js")) ||
  262.                (substr($_SERVER['PHP_SELF']$len_of_php_self 4== ".css")) {
  263.                    
  264.                    return;
  265.             
  266.                
  267.                
  268.       
  269.            if ($this->m_is_existing_session)
  270.                   return;
  271.  
  272.           if (!isset($_COOKIE[$this->m_session_name]&& 
  273.               !isset($_REQUEST[$this->m_session_name]&& 
  274.               $p_force_to_load == false{
  275.  
  276.               $this->m_session_id = '';
  277.             $this->m_must_be_destroyed = false;
  278.               $this->m_is_existing_session = false;
  279.               
  280.               return ;
  281.               
  282.           }
  283.               
  284.           @session_start();
  285.         //echo("<br />run real or not Session");
  286.           
  287.           
  288.           $this->xExist(session_id()$is_existing_session);
  289.           
  290.           $this->m_is_existing_session = $is_existing_session;
  291.           
  292.           if (!$is_existing_session && !$p_force_to_load{
  293.               $this->m_must_be_destroyed = true;
  294.               $this->m_session_id = "";
  295.             //echo("<br />set to not real :".session_id());
  296.           else if (!$is_existing_session && $p_force_to_load{
  297.             $this->m_is_existing_session = true;
  298.             $this->m_must_be_destroyed = false;
  299.             $this->m_session_id = session_id();
  300.             $this->xInsert($this->m_session_id);
  301.             //echo("<br />is creating");
  302.           else  {
  303.             $this->m_session_id = session_id();
  304.               $this->xUpdate($this->m_session_id);
  305.             $this->m_must_be_destroyed = false;
  306.             //echo("<br />is real");
  307.           
  308.  
  309.           
  310.           
  311.           /*
  312.         echo("<br />end default loading");
  313.           if ($this->m_must_be_destroyed)
  314.               echo("<br />must be destruct");
  315.           else 
  316.               echo("<br />must NOT be destruct");
  317.           */
  318.           
  319.           
  320.       }
  321.  
  322.       /**
  323.        * Force to load a session
  324.        *
  325.        * @return unknown 
  326.        */
  327.       function forceToLoad({
  328.           $this->xForceToLoad();    
  329.       }
  330.       /**
  331.        * Force to load
  332.        *
  333.        * @access private
  334.        */
  335.       function xForceToLoad({
  336.           if (!$this->m_is_existing_session{
  337.               $this->xLoadSession(true);
  338.           }
  339.       }
  340.         
  341.  
  342.   /**
  343.    * Force to clean in database
  344.    *
  345.    */
  346.   function forceCleanning({
  347.       $this->xClean();
  348.   }
  349.       
  350.       
  351.       
  352.   /**
  353.    * Generate unique id : max of 35 charater generated
  354.    *
  355.    * @return string 
  356.    */
  357.   function generateUniqueId({
  358.     return md5(uniqid(rand()true));    
  359.   }
  360.     
  361.  
  362.   /**
  363.    * Is not valid id
  364.    *
  365.    * @param string $p_value 
  366.    * @return bool TRUE on error
  367.    */
  368.   function isNotValidId($p_value{
  369.       return $this->m_object_format->isNotVarchar($p_value45);
  370.   }
  371.  
  372.   /**
  373.    * Insert
  374.    *
  375.    * @access private
  376.    * @param string $p_id 
  377.    * @return bool TRUE on error
  378.    */
  379.   function xInsert($p_id{
  380.           
  381.          if ($this->isNotValidId($p_id)) {
  382.              return onErrorThrow(true,plici_lang("lang_error_field"));
  383.          }
  384.      
  385.          // on clean les sessions obsolètes
  386.          $this->xClean();
  387.          
  388.          // preparation des paramètres
  389.          $parameters array();
  390.          $parameters[sizeof($parameters)$this->getDatabaseId($p_id);
  391.         
  392.                                 
  393.         if ($this->m_object_dbconnexion->makeSqlQuery($this->req_insert$parameters$result)) {
  394.                 return onErrorThrow(true,plici_lang("lang_error_addingsession"));
  395.         }  
  396.         
  397.         return false;          
  398.   }
  399.  
  400.   /**
  401.    * Clean the session
  402.    *
  403.    * @access private
  404.    * @return bool TRUE on error
  405.    */
  406.   function xClean({
  407.      
  408.          // preparation des paramètres
  409.          $parameters array();
  410.          $parameters[sizeof($parameters)session_cache_expire()+1;
  411.         
  412.                                 
  413.         if ($this->m_object_dbconnexion->makeSqlQuery($this->req_delete$parameters$result)) {
  414.                 return onErrorThrow(true,plici_lang("lang_error_cleanningsession"));
  415.         }  
  416.         
  417.         return false;          
  418.   }  
  419.   
  420.   /**
  421.    * update
  422.    *
  423.    * @param string $p_id 
  424.    * @return bool TRUE on error
  425.    */
  426.   function xUpdate($p_id{
  427.          if ($this->isNotValidId($p_id)) {
  428.              return onErrorThrow(true,plici_lang("lang_error_field"));
  429.          }
  430.      
  431.          // preparation des paramètres
  432.          $parameters array();
  433.          $parameters[sizeof($parameters)$this->getDatabaseId($p_id);
  434.         
  435.                                 
  436.         if ($this->m_object_dbconnexion->makeSqlQuery($this->req_update$parameters$result)) {
  437.                 return onErrorThrow(true,plici_lang("lang_error_sessionupdate"));
  438.         }  
  439.         
  440.         return false;          
  441.   }
  442.     
  443.   /**
  444.    * Test if exist session
  445.    *
  446.    * @access private
  447.    * @param string $p_id 
  448.    * @param bool $r_bool_is_existing 
  449.    * @return bool TRUE on error
  450.    */
  451.   function xExist($p_id&$r_bool_is_existing{
  452.           $r_bool_is_existing false;
  453.          if ($this->isNotValidId($p_id)) {
  454.              return onErrorThrow(true,plici_lang("lang_error_field"));
  455.          }
  456.      
  457.          // preparation des paramètres
  458.          $parameters array();
  459.          $parameters[sizeof($parameters)$this->getDatabaseId($p_id);
  460.         
  461.                                 
  462.         if ($this->m_object_dbconnexion->makeSqlQuery($this->req_exist$parameters$result)) {
  463.                 return onErrorThrow(true,plici_lang("lang_error_sessionupdate"));
  464.         }  
  465.         
  466.         while ($row mysql_fetch_row($result)) {
  467.             $r_bool_is_existing true;
  468.         }
  469.         
  470.         return false;          
  471.   }
  472.     
  473.       
  474.       
  475.       
  476. }
  477.  
  478.  
  479.  
  480. ?>

Documentation generated on Tue, 25 Mar 2008 15:37:38 +0100 by phpDocumentor 1.3.2