Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.SessionUtils.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
8 {
9 
10  private $dbaccess;
11 
12  function __construct($dbaccess)
13  {
14  $this->dbaccess = $dbaccess;
15  }
16 
17  function getSessionMaxAge()
18  {
19  $query = new DbObj($this->dbaccess);
20  $err = $query->exec_query("SELECT val FROM paramv WHERE name = 'CORE_SESSIONMAXAGE'");
21  if ($err != "") {
22  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . "exec_query returned with error: " . $err);
23  return false;
24  }
25  if ($query->numrows() <= 0) {
26  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . "exec_query returned an empty result set");
27  return false;
28  }
29  $res = $query->fetch_array(0);
30  if (is_numeric($res['val'])) {
31  return $res['val'] . " seconds";
32  }
33  return $res['val'];
34  }
35 
36  function getSessionMaxAgeSeconds($default = "1 week")
37  {
38  $session_maxage = $this->getSessionMaxAge($default);
39  if ($session_maxage === false) {
40  return false;
41  }
42  if (preg_match('/^(\d+)\s+(\w+)/i', $session_maxage, $m)) {
43  $maxage = $m[1];
44  $unit = strtolower($m[2]);
45  switch (substr($unit, 0, 1)) {
46  case 'y':
47  $maxage = $maxage * 364 * 24 * 60 * 60;
48  break; # years
49 
50  case 'm':
51  if (substr($unit, 0, 2) == 'mo') {
52  $maxage = $maxage * 30 * 24 * 60 * 60;
53  break; # months
54 
55  } else {
56  $maxage = $maxage * 60;
57  break; # minutes
58 
59  }
60  case 'w':
61  $maxage = $maxage * 7 * 24 * 60 * 60;
62  break; # weeks
63 
64  case 'd':
65  $maxage = $maxage * 24 * 60 * 60;
66  break; # days
67 
68  case 'h':
69  $maxage = $maxage * 60 * 60;
70  break; # hours
71 
72  case 's':
73  break; # seconds
74 
75  default:
76  return FALSE;
77  }
78  return $maxage;
79  }
80  return FALSE;
81  }
82 
84  {
85  include_once ('WHAT/Lib.Prefix.php');
86 
87  global $pubdir;
88 
89  $session_maxage = $this->getSessionMaxAgeSeconds();
90  if ($session_maxage === false) {
91  $err = sprintf("Malformed CORE_SESSIONMAXAGE");
92  return $err;
93  }
94  $maxage = time() - $session_maxage;
95 
96  $sessionDir = sprintf("%s/var/session", $pubdir);
97  $dir = opendir($sessionDir);
98  if ($dir === false) {
99  $err = sprintf("Error opening directory '%s'.", $sessionDir);
100  return $err;
101  }
102 
103  $sessions = array();
104  while ($file = readdir($dir)) {
105  if (preg_match("/^sess_(.+)$/", $file, $m)) {
106  $sess_id = $m[1];
107  $sess_file = sprintf("%s/%s", $sessionDir, $file);
108  $stat = @stat($sess_file);
109  if ($stat !== false && $stat['mtime'] < $maxage) {
110  @unlink($sess_file);
111  }
112  }
113  }
114  closedir($dir);
115 
116  return "";
117  }
118 }
119 ?>
global $pubdir
Definition: vault_init.php:18
__construct($dbaccess)
$file
getSessionMaxAgeSeconds($default="1 week")
$dir
Definition: resizeimg.php:144
if(($docid!==0)&&(!is_numeric($docid))) $query
if($file) if($subject==""&&$file) if($subject=="") $err
← centre documentaire © anakeen