home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / hsession.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  5.5 KB  |  196 lines

  1. HSession 
  2.  
  3. Session management webapplication oriented. Variable registration from form or global, lifetime for each application, ... 
  4.  
  5.  
  6. --------------------------------------------------------------------------------
  7. <?php  
  8. // ------------------- 
  9. // HH 200004181107v0.2 
  10. // Herve HERITIER 
  11. // Hsession (file) 
  12. // ------------------- 
  13.  
  14. if (!defined( "__PHP_SESSION")) { 
  15. define ( "__PHP_SESSION",1); 
  16. // Directory for sessions files 
  17. define ( "PHP_SESSIONDIR", "/var/tmp/phpsession/");  
  18. // configuration file 
  19. // inf file contain on line by application 
  20. // for each application, three parameters, comma delimited 
  21. // application name, path for cookie, timeout 
  22. // 
  23. define ( "PHP_SESSIONINF",  "/etc/httpd/conf/phpsessions.inf"); 
  24. define ( "COOKIE_DOMAIN", getenv( "SERVER_NAME")); 
  25.  
  26.  
  27. class Csession { 
  28.   var $inf,$firstcall; 
  29.  
  30. // Private 
  31.  
  32.   function sendCookie ($value= "@#&") { 
  33.     if ($value ==  "@#&") 
  34.       $value = $this->cookie; 
  35.     setcookie($this->name, $value, 0, $this->cookiepath,COOKIE_DOMAIN);  
  36.   } 
  37.  
  38. // Private 
  39.   function loadInf () { 
  40.     $fp = fopen (PHP_SESSIONINF,  "r"); 
  41.     if (! $fp) { 
  42.       return; 
  43.     } 
  44.     flock($fp, 1); 
  45.     while ($line = trim(fgets($fp, 2048))) { 
  46.       $buffer = explode( ",", $line); 
  47.       $this->inf[$buffer[0]][ "path"]=$buffer[1]; 
  48.       $this->inf[$buffer[0]][ "timeout"]=$buffer[2]; 
  49.     } 
  50.     fclose($fp); 
  51.   } 
  52.  
  53. // Public 
  54.   function destructor() {$this->save();}     
  55. // Public 
  56.   function Csession($newname =  "PHPSESSID", $forcenew=0) { 
  57.  
  58.     $this->sessid =  ""; 
  59.     $this->sdata  = array(); 
  60.     $this->sfile =  ""; 
  61.     $this->cookie =  ""; 
  62.     $this->cookiepath =  "/"; 
  63.     $this->firstcall = 0; 
  64.     $this->forcenew = $forcenew; 
  65.     $this->client = $GLOBALS[ "REMOTE_ADDR"]; 
  66.     $this->name = $newname; 
  67.     $this->sessid = $this->name . date( "YmdHis") .  "_" . str_replace( ".",  "_", $this->client);  
  68.     $this->sfile = PHP_SESSIONDIR . $this->sessid; 
  69.     $this->cookie = $this->sessid; 
  70.      //register_shutdown_function("session_save"); 
  71.     $this->loadInf(); 
  72.     $this->cookiepath = $this->inf[$this->name][ "path"]; 
  73.     $this->timeout=$this->inf[$this->name][ "timeout"]; 
  74.     $this->start(); 
  75.   } 
  76. // Public 
  77.   function start () { 
  78.     global $HTTP_COOKIE_VARS; 
  79.  
  80.     $php_sessid = $HTTP_COOKIE_VARS[$this->name]; 
  81.     if ($php_sessid ==  "") { 
  82.       $this->sendCookie();  
  83.       $this->firstcall = true; 
  84.       return; 
  85.     } 
  86.     else { 
  87.       if (file_exists(PHP_SESSIONDIR . $php_sessid)) { 
  88.         $a_stats = stat(PHP_SESSIONDIR . $php_sessid); 
  89.         $delay = time() - $a_stats[ "9"]; 
  90.         if ($delay > $this->timeout) { 
  91.           @unlink (PHP_SESSIONDIR . $php_sessid); 
  92.           $this->firstcall = true; 
  93.           $this->sendCookie();  
  94.           return; 
  95.         } 
  96.       } 
  97.       if ($this->forcenew) { 
  98.         @unlink (PHP_SESSIONDIR . $php_sessid); 
  99.         $this->sendCookie();  
  100.         $this->sendCookie( "");  
  101.         $this->firstcall = true; 
  102.         return; 
  103.       } 
  104.       else { 
  105.         $this->cookie = $php_sessid; 
  106.         $this->sessid = $php_sessid; 
  107.         $this->sfile = PHP_SESSIONDIR . $php_sessid; 
  108.       } 
  109.     } 
  110.     @$fp = fopen($this->sfile,  "r"); 
  111.     if (! $fp) { 
  112.       $this->firstcall = true; 
  113.       $this->sendCookie();  
  114.       return; 
  115.     } 
  116.     flock($fp, 1);  // get shared lock and wait 
  117.     $line = trim(fgets($fp, 2048)); 
  118.     if ($line != $php_sessid) { 
  119.       fclose($fp); 
  120.       $this->sendCookie($php_sessid);  
  121.       return; 
  122.     } 
  123.     else 
  124.       $this->cookie = $php_sessid; 
  125.     while ($line = fgets($fp, 2048)) { 
  126.       list($name, $svar) = explode( "=", $line); 
  127.       $GLOBALS[$name] = unserialize($svar); 
  128.       $this->sdata[$name] = $name; 
  129.     } 
  130.     fclose($fp); 
  131.   } 
  132. // Public 
  133. // Reference a global variable into the session 
  134.   function register($var) { 
  135.     if (!isset($this->sdata[$var])) 
  136.       $this->sdata[$var] = $var; 
  137.   } 
  138. // Public 
  139. // unreference a global variable into the session 
  140.   function unregister($var) { 
  141.     unset ($this->sdata[$var]); 
  142.   } 
  143.   function registerFromPost ($var) { 
  144.     global $HTTP_POST_VARS; 
  145.     $this->sdata[$var] = $HTTP_POST_VARS[$var]; 
  146.     $GLOBALS[$var] = $this->sdata[$var]; 
  147.   } 
  148. // Public 
  149. // Reference all POST variables from a form 
  150. // except the exceptvars list (space delimited) 
  151.   function registerAllFromPost ($exceptvars= "") { 
  152.     global $HTTP_POST_VARS; 
  153.  
  154.     $exceptvars =  " $exceptvars "; 
  155.     reset ($HTTP_POST_VARS); 
  156.     while (list ($var,$value) = each($HTTP_POST_VARS)) { 
  157.       if (strpos($exceptvars, $var .  " ") == 0) 
  158.         $this->registerFromPost ($var); 
  159.     } 
  160.   } 
  161. // Public 
  162. // clear the session 
  163.   function destroy() { 
  164.     @unlink($this->sfile); 
  165.     unset($this->sdata); 
  166.   } 
  167.  
  168. // Public 
  169. // save the registred variable to the session file 
  170. function save() { 
  171.     if (! is_array($this->sdata)) 
  172.       return; 
  173.     if (file_exists($this->sfile)) { 
  174.       $fp = fopen($this->sfile,  "r+"); 
  175.     } 
  176.     else {   
  177.       $fp = fopen($this->sfile,  "w"); 
  178.     } 
  179.     flock($fp, 2);  // exclusive lock and wait 
  180.     fputs($fp, trim($this->cookie) .  "\n"); 
  181.     $i = 0; 
  182.     reset($this->sdata); 
  183.     while ($i++ < count($this->sdata)) { 
  184.       $var = key($this->sdata);    
  185.       $data = $GLOBALS[$var]; 
  186.       $svar = serialize($data); 
  187.       fputs($fp,$var .  "=" . $svar .  "\n"); 
  188.       next($this->sdata); 
  189.     } 
  190.     fclose($fp); 
  191.   } 
  192. }  // end of class session 
  193. }   
  194. ?>
  195.  
  196.