Source for file OCSP_SESSION.phpclass

Documentation is available at OCSP_SESSION.phpclass

  1. <?php
  2. /**
  3.   * session class it is planed to add different session types
  4.   * therefore this class is already implemented but uses standard php session
  5.   * for the moment
  6.   *
  7.   * @project    Open CSP-Management
  8.   * @package    common
  9.   * @category   session
  10.   *
  11.   * @author     Peter Krebs (pk)<pitlinz@users.sourceforge.net>
  12.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  13.   *
  14.   * @since pk-07-07-31
  15.   * @version $Id: OCSP_SESSION.phpclass,v 1.9 2008/11/19 07:40:07 pitlinz Exp $
  16.   */
  17.  
  18. /**
  19.   * Session Error Class
  20.   *
  21.   * @project    Open CSP-Management
  22.   * @package    common
  23.   * @category   session
  24.   *
  25.   * @author     Peter Krebs (pk)<pitlinz@users.sourceforge.net>
  26.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  27.   *
  28.   * @since pk-07-08-24
  29.   */
  30.     /** -----------------------------------------------------
  31.       * Compositions:
  32.       */
  33.  
  34.     /**
  35.       * @var OCSP_SESSION $mySession 
  36.       * @access public
  37.       */
  38.     var $mySession=NULL;
  39.  
  40.     /** -----------------------------------------------------
  41.       * Attributes:
  42.       */
  43.  
  44.     /**
  45.       * @var string $errMsg 
  46.       * @access public
  47.       */
  48.     var $errMsg="";
  49.  
  50.     /**
  51.       *
  52.       * METHODS _____________________________________________
  53.       *
  54.       */
  55.  
  56.     /**
  57.       * @param OCSP_SESSION $aSession 
  58.       * @param string $errMsg 
  59.       */
  60.     function __construct(&$aSession,$aMsg)
  61.     {
  62.         $this->mySession=&$aSession;
  63.         $this->errMsg=$aMsg;
  64.     }
  65. }
  66.  
  67.  
  68. /**
  69.   * Session Class
  70.   *
  71.   * @project    Open CSP-Management
  72.   * @package    common
  73.   * @category   session
  74.   *
  75.   * @author     Peter Krebs (pk)<pitlinz@users.sourceforge.net>
  76.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  77.   *
  78.   * @since pk-07-07-31
  79.   * @version pk-08-03-14 singleton
  80.   * 
  81.   * @version $Id: OCSP_SESSION.phpclass,v 1.9 2008/11/19 07:40:07 pitlinz Exp $
  82.   */
  83. class OCSP_SESSION {
  84.  
  85.     /*** class variables  --------------------------------------------- */
  86.  
  87.     /**
  88.       * @staticvar OCSP_SESSION $instance 
  89.       */
  90.     private static $instance=NULL;    
  91.  
  92.     /**
  93.       * @staticvar string $myName 
  94.       */
  95.     protected static $myName="";
  96.         
  97.     /** -----------------------------------------------------
  98.       * Aggregations:
  99.       */
  100.  
  101.     /** -----------------------------------------------------
  102.       * Compositions:
  103.       */
  104.  
  105.     /** -----------------------------------------------------
  106.       * Attributes:
  107.       */
  108.  
  109.     /**
  110.       * @var array $mySessionVals 
  111.       * @access protected
  112.       */
  113.     protected $mySessionVals=array();
  114.  
  115.     /**
  116.       * @var boolean $isOpened 
  117.       * @access protected
  118.       */
  119.     protected $isOpened=false;
  120.  
  121.     /**
  122.       * @var boolean $isWritten 
  123.       * @access protected
  124.       */
  125.     protected $isWritten=false;
  126.     
  127.     
  128.     /**
  129.       * @var boolean $hasChanged 
  130.       * @access protected
  131.       */
  132.     protected $hasChanged=false;
  133.  
  134.     /**
  135.       *
  136.       * METHODS _____________________________________________
  137.       *
  138.       */
  139.  
  140.     // --------------------------------------------------------------
  141.     // static method
  142.     // --------------------------------------------------------------    
  143.  
  144.     /**
  145.       * @param mixed $aValue 
  146.       *
  147.       * @return boolean 
  148.       */
  149.     public static function isSessionError(&$aValue)
  150.     {
  151.         return pcf_is_instance_of($aValue,'OCSP_SESSION_ERROR');
  152.     }    
  153.     
  154.     // --------------------------------------------------------------
  155.     // Constructors and descructors
  156.     // --------------------------------------------------------------
  157.     
  158.     /**
  159.      * constructor
  160.      */
  161.     protected function __construct()
  162.     {
  163.         if (empty(self::$myName))
  164.         {
  165.             if(($str_name ini_get('session.name')) && !empty($str_name)) {
  166.                 self::$myName ini_get('session.name');
  167.             else {
  168.                 self::$myName 'OCSP_SES';                    
  169.             }
  170.         }        
  171.     }
  172.     
  173.     /**
  174.       * save session on descruct
  175.       */
  176.     function __destruct()
  177.     {
  178.         if ($this->isOpened()) {
  179.             $this->closeSession();
  180.         }
  181.     }
  182.  
  183.     // --------------------------------------------------------------
  184.     // static instance functions
  185.     // --------------------------------------------------------------    
  186.     
  187.     /**
  188.      * returns the session object
  189.      * 
  190.      * @param boolean $open (open the session?)
  191.      *
  192.      * @return OCSP_SESSION 
  193.      */
  194.     public static function getInstance($open=false)
  195.     {
  196.         if (!self::$instance)
  197.         {
  198.             self::$instance new OCSP_SESSION();
  199.         }
  200.         if ($open && (!self::$instance->isOpened()))
  201.         {
  202.             self::$instance->openSession();
  203.             if (!self::$instance->isOpened())
  204.             {
  205.                 throw new Exception(_OCSP_SESSION_ERROR_": COULD NOT OPEN SESSION");
  206.             }
  207.         }
  208.         return self::$instance;
  209.     }    
  210.  
  211.     /**
  212.      * returns if we already have an instance
  213.      *
  214.      * @return boolean 
  215.      * 
  216.      * @since pk-08-03-14
  217.      */
  218.     public static function hasInstance()
  219.     {
  220.         if (self::$instance)
  221.         {
  222.             return true;
  223.         else {
  224.             return false;
  225.         }
  226.     }
  227.     
  228.     // --------------------------------------------------
  229.     // open/close
  230.     // --------------------------------------------------
  231.     
  232.     /**
  233.       * starts the session
  234.       * @param string $aName 
  235.       * @param boolean $debug 
  236.       *
  237.       * @return boolean 
  238.       */
  239.     function openSession()
  240.     {
  241.         if (!defined('__OCSP_SESSION_'.$this->getName().'__')) {
  242.             session_start();
  243.             $this->isOpened=true;
  244.             define('__OCSP_SESSION_'.$this->getName().'__',TRUE);
  245.         }
  246.         return $this->isOpened();
  247.     }
  248.  
  249.     /**
  250.       * closes a session and writes the data
  251.       */
  252.     function closeSession()
  253.     {
  254.         session_write_close();
  255.         $this->isOpened    =false;
  256.         $this->isWritten=true;
  257.     }
  258.  
  259.  
  260.     // --------------------------------------------------------------
  261.     // getter setter
  262.     // --------------------------------------------------------------
  263.           
  264.     /**
  265.       * @return boolean; 
  266.       */
  267.     function isOpened()
  268.     {
  269.         return $this->isOpened;
  270.     }
  271.  
  272.     /**
  273.       * @return boolean 
  274.       */
  275.     function hasChanged()
  276.     {
  277.         return $this->hasChanged;
  278.     }
  279.  
  280.     /**
  281.       * @return string 
  282.       */
  283.     function getName()
  284.     {
  285.         return self::$myName;
  286.     }
  287.  
  288.     /**
  289.       * @return string 
  290.       */
  291.     function getId()
  292.     {
  293.         return session_id();
  294.     }
  295.  
  296.     // --------------------------------------------------------------
  297.     // values
  298.     // --------------------------------------------------------------
  299.     
  300.     /**
  301.       * @param string $aVarName 
  302.       * @param boolean $debug 
  303.       *
  304.       * @return mixed 
  305.       */
  306.     function getValue($aVarName,$debug=FALSE)
  307.     {
  308.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_SESSION::getValue($aVarName)");
  309.         if (empty($aVarName)) return NULL;
  310.  
  311.         if (!$this->isOpened(&& !$this->openSession())
  312.         {
  313.             return new OCSP_SESSION_ERROR($this,"session could not be opened");
  314.         }
  315.  
  316.         if (isset($this->mySessionVals[$aVarName]))
  317.         {
  318.             return $this->mySessionVals[$aVarName];
  319.         }
  320.  
  321.         if (isset($_SESSION[$aVarName]))
  322.         {
  323.             return $_SESSION[$aVarName];
  324.         }
  325.         return Null;
  326.     }
  327.  
  328.     /**
  329.      * sets a session var value
  330.      * 
  331.      * @param string $aVarName 
  332.      * @param mixed $aValue 
  333.      * @param boolean $debug 
  334.      *
  335.      * @return mixed (TRUE in case of no error else an OCSP_SESSION_ERROR object)
  336.      */
  337.     function setValue($aVarName,$aValue,$debug=False)
  338.     {
  339.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_SESSION::setValue($aVarName)");
  340.  
  341.         if (!$this->isOpened(&& !$this->openSession())
  342.         {
  343.             return new OCSP_SESSION_ERROR($this,"session could not be opened");
  344.         }
  345.         
  346.         if ($this->isWritten)
  347.         {
  348.             throw new Exception(_OCSP_SESSION_ERROR_ ': SESSION ALREADY WRITTEN');
  349.         }
  350.         
  351.         $oldValue=$this->getValue($aVarName);
  352.  
  353.         if ($oldValue != $aValue)
  354.         {
  355.             $this->mySessionVals[$aVarName]=$aValue;
  356.             $_SESSION[$aVarName]=$aValue;
  357.             $this->hasChanged=True;
  358.         }
  359.         return True;
  360.     }
  361.  
  362.     /**
  363.      * sets a session value which is not set
  364.      *  
  365.      * if $aVarName is already set in the session nothing is done
  366.      * and False returned
  367.      * 
  368.      * @param string $aVarName 
  369.      * @param string $aValue 
  370.      * @param boolean $debug 
  371.      * 
  372.      * @return boolean if the value has changed
  373.      * 
  374.      * @since pk-08-06-11
  375.      */
  376.     public function setDefaultValue($aVarName,$aValue,$debug=False)
  377.     {
  378.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_SESSION::setDefaultValue(".$aVarName.")");
  379.         
  380.         $mix_sessValue $this->getValue($aVarName);
  381.         if (!empty($mix_sessValue&& ($mix_sessValue !== Null))
  382.         {
  383.              return False;
  384.         }
  385.         
  386.         return  $this->setValue($aVarName,$aValue,$debug);        
  387.     }
  388.     
  389.     /**
  390.       * @param string $aVarName 
  391.       * @param boolean $debug 
  392.       */
  393.     function unsetValue($aVarName,$debug=False)
  394.     {
  395.         if (!$this->isOpened(&& !$this->openSession())
  396.         {
  397.             return new OCSP_SESSION_ERROR($this,"session could not be opened");
  398.         }
  399.         
  400.         if ($this->isWritten)
  401.         {
  402.             throw new Exception(_OCSP_SESSION_ERROR_ ': SESSION ALREADY WRITTEN');
  403.         }
  404.         
  405.         if (isset($this->mySessionVals[$aVarName]))
  406.         {
  407.             $this->hasChanged=TRUE;
  408.             unset($this->mySessionVals);
  409.         }
  410.         if (isset($_SESSION[$aVarName]))
  411.         {
  412.             $this->hasChanged=TRUE;
  413.             unset($_SESSION[$aVarName]);
  414.         }
  415.     }
  416.  
  417.     // --------------------------------------------------------------
  418.     // user methods
  419.     // --------------------------------------------------------------
  420.     
  421.     /**
  422.       * clears all user data from the session
  423.       *
  424.       * @param boolean $debug 
  425.       */
  426.     function clearUser($debug=false)
  427.     {        
  428.         $this->unsetValue('CURRENTUSER',$debug);
  429.         $this->unsetValue('CURRENTUSERCHK',$debug);        
  430.     }
  431.  
  432.     /**
  433.      * returns the stored userId
  434.      *
  435.      * @param boolean $debug 
  436.      * 
  437.      * @return int 
  438.      * 
  439.      * @since pk-08-07-02
  440.      */
  441.     public function getUserId($debug=False)
  442.     {
  443.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_SESSION::getUserId");
  444.         
  445.         if ($arr_usrData $this->getValue('CURRENTUSER'))
  446.         {
  447.             return intval($arr_usrData['USR_ID']);
  448.         
  449.         return 0;        
  450.     }
  451.     
  452.     /**
  453.       * @param boolean $reqDB (establish a database connection)
  454.       * @param boolean $debug 
  455.       *
  456.       * @return OCSP_USER 
  457.       *
  458.       * @requires user/OCSP_USER.phpclass
  459.       *
  460.       */
  461.     function getUser($reqDB=FALSE,$debug=FALSE)
  462.     {
  463.         pcf_require_class('OCSP_USER',"user/");       
  464.         return OCSP_USER::factoryCurrentFromSession();
  465.     }
  466.  
  467.     /**
  468.       * refreshes the user session
  469.       *
  470.       * @param boolean $withoutUsrDb 
  471.       *
  472.       * @access public
  473.       */
  474.     function refreshUser($withoutUsrDb=FALSE)
  475.     {
  476.         echoDebugLine(__FILE__,__LINE__,"<p>NOT IMPLEMENTET CALL OCSP_SESSION::refreshUser()</p><pre>".print_r($_SESSION,True)."</pre>");
  477.         exit();
  478.     }
  479.  
  480.     // --------------------------------------------------------------
  481.     // en- / decryption
  482.     // --------------------------------------------------------------
  483.     
  484.     private static function get_rnd_iv($iv_len)
  485.     {
  486.         $iv "qe3jignfmas4qeeqfrfmas4qegnqw2egtjkn5lgdaaafaadsjpqd37qwet8zuiopl1";
  487.         while(strlen($iv$iv_len$iv.=$iv;
  488.         return substr($iv,0,$iv_len);        
  489.     }
  490.     
  491.     /**
  492.      * returns a default password for encrypt/decrypt
  493.      *
  494.      * @return string 
  495.      */
  496.     private function get_cryptPassword()
  497.     {
  498.         $password $this->getValue('MD5PWD');
  499.         if (empty($password))
  500.         {
  501.             $password md5("open".time().$_SERVER['REMOTE_ADDR'].$_SERVER['REMOTE_PORT']."CSP");
  502.             $this->setValue('MD5PWD',$password);
  503.         }
  504.         return $password;
  505.     }
  506.     
  507.     /**
  508.       * md5 encrypts a text
  509.       *
  510.       * if $password is empty $_SESSION['MD5PWD'] is used
  511.       *
  512.       * @param string $plain_text 
  513.       * @param string $password 
  514.       * @param int $iv_len 
  515.       *
  516.       * @returns string
  517.       *
  518.       * @see http://www.php.net/manual/en/function.md5.php
  519.       * 
  520.       * @since pk-08-03-14
  521.       */
  522.     public function encrypt($plain_text$password=""$iv_len 16
  523.     {
  524.         if (empty($password))
  525.         
  526.             $password=$this->get_cryptPassword();
  527.         }
  528.             
  529.         $password   =md5($password);
  530.         $plain_text =base64_encode($plain_text);
  531.     
  532.         $plain_text .= "\x13";
  533.         $n strlen($plain_text);
  534.         if ($n 16$plain_text .= str_repeat("\0"16 ($n 16));
  535.         $i 0;
  536.         $enc_text self::get_rnd_iv($iv_len);
  537.         $iv substr($password $enc_text0512);
  538.         while ($i $n{
  539.             $block substr($plain_text$i16pack('H*'md5($iv));
  540.             $enc_text .= $block;
  541.             $iv substr($block $iv0512$password;
  542.             $i += 16;
  543.         }
  544.         return base64_encode(urlencode($enc_text));
  545.     }    
  546.     
  547.     /**
  548.       * md5 decrypts a text
  549.       *
  550.       * if $password is empty $_SESSION['MD5PWD'] is used
  551.       *
  552.       * @param string $enc_text 
  553.       * @param string $password 
  554.       * @param int $iv_len 
  555.       *
  556.       * @returns string
  557.       *
  558.       * @see http://www.php.net/manual/en/function.md5.php
  559.       * 
  560.       * @since pk-08-03-14
  561.       */
  562.     public function decrypt($enc_text$password=""$iv_len 16{
  563.         if (empty($password))
  564.         
  565.             $password=$this->get_cryptPassword();
  566.         }
  567.     
  568.         // prepare password and enc_text
  569.         $password=md5($password);
  570.         $enc_text urldecode(base64_decode($enc_text));
  571.     
  572.         $n strlen($enc_text);
  573.         $i $iv_len;
  574.         $plain_text '';
  575.         $iv substr($password substr($enc_text0$iv_len)0512);
  576.         while ($i $n{
  577.             $block substr($enc_text$i16);
  578.             $plain_text .= $block pack('H*'md5($iv));
  579.             $iv substr($block $iv0512$password;
  580.             $i += 16;
  581.         }
  582.         return base64_decode(preg_replace('/\\x13\\x00*$/'''$plain_text));
  583.     }    
  584. }
  585.  
  586. ?>

Documentation generated on Thu, 08 Jan 2009 17:47:08 +0100 by phpDocumentor 1.4.0a2