CMS  Version 3.9
session_manager.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39 Fakoli::using("settings", "session_persistance");
40 
42 {
43  static $lifeTime;
44  static $hash;
45 
46  static function createDefaultSettings()
47  {
48  Settings::setDefaultValue("session_persistance", "enable_database_persistance", "", "Boolean");
49  }
50 
51  static function onInitialize()
52  {
53  if (!Settings::getValue("session_persistance", "enable_database_persistance")) return;
54 
55  SessionManager::$lifeTime = get_cfg_var("session.gc_maxlifetime");
57 
58  session_set_save_handler(
59  array(SessionManager, "open"),
60  array(SessionManager, "close"),
61  array(SessionManager, "read"),
62  array(SessionManager, "write"),
63  array(SessionManager, "destroy"),
64  array(SessionManager, "gc")
65  );
66 
67  register_shutdown_function(session_write_close);
68  }
69 
70  static function open($save_path, $session_name)
71  {
72  return true;
73  }
74 
75  static function close()
76  {
77  return true;
78  }
79 
80  static function read($id)
81  {
82  trace("Session Read $id", 3);
83 
84  try
85  {
86  $session = PHPSession::read($id);
87  if ($session->expires < time()) return null;
88  return $session->data;
89  }
90  catch(Exception $e)
91  {
92  return null;
93  }
94  }
95 
96  static function write($id, $data)
97  {
98  trace("Session Write $id", 3);
99 
100  try
101  {
102  $session = PHPSession::read($id);
103  if (!$session) $session = new PHPSession();
104 
105  $expiry = time() + SessionManager::$lifeTime;
106 
107  if ($session->session_id && ($data == $session->data))
108  {
109  $session->filter = new ExclusionFilter("data", "id");
110  }
111  else
112  {
113  $session->filter = null;
114  }
115 
116  $session->id = $id;
117  $session->data = $data;
118  $session->expires = $expiry;
119  $session->save();
120  }
121  catch(Exception $e)
122  {
123  }
124 
125  }
126 
127  static function destroy($id)
128  {
129  trace("Session Destroy $id", 3);
130 
131  try
132  {
133  $session = PHPSession::read($id);
134  if ($session) $session->delete();
135  }
136  catch(Exception $e)
137  {
138  }
139  }
140 
141  static function gc()
142  {
143  $session = new PHPSession();
144  $session->delete("WHERE expires < ".time());
145  }
146 
147  static function upgradeComponent($version)
148  {
150  $mgr->upgrade($version);
151  }
152 }?>
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
static read($id)
Definition: php_session.inc:49
static createDefaultSettings()
static upgradeComponent($version)
static read($id)
static write($id, $data)
static onInitialize()
static open($save_path, $session_name)
static destroy($id)
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
static setDefaultValue($component, $name, $value, $field_type="String", $annotation="", $category="", $options="", $weight=0)
Sets the default value of the given component setting.
Definition: settings.inc:174