3 * Table Definition for session
5 * StatusNet - the distributed open-source microblogging tool
6 * Copyright (C) 2009, StatusNet, Inc.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
24 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
26 class Session extends Memcached_DataObject
29 /* the code below is auto generated do not remove the above tag */
31 public $__table = 'session'; // table name
32 public $id; // varchar(32) primary_key not_null
33 public $session_data; // text()
34 public $created; // datetime() not_null
35 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
38 function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Session',$k,$v); }
40 /* the code above is auto generated do not remove the tag below */
43 static function logdeb($msg)
45 if (common_config('sessions', 'debug')) {
46 common_debug("Session: " . $msg);
50 static function open($save_path, $session_name)
55 static function close()
60 static function read($id)
62 self::logdeb("Fetching session '$id'");
64 $session = Session::staticGet('id', $id);
66 if (empty($session)) {
67 self::logdeb("Couldn't find '$id'");
70 self::logdeb("Found '$id', returning " .
71 strlen($session->session_data) .
73 return (string)$session->session_data;
77 static function write($id, $session_data)
79 self::logdeb("Writing session '$id'");
81 $session = Session::staticGet('id', $id);
83 if (empty($session)) {
84 self::logdeb("'$id' doesn't yet exist; inserting.");
85 $session = new Session();
88 $session->session_data = $session_data;
89 $session->created = common_sql_now();
91 $result = $session->insert();
94 common_log_db_error($session, 'INSERT', __FILE__);
95 self::logdeb("Failed to insert '$id'.");
97 self::logdeb("Successfully inserted '$id' (result = $result).");
101 self::logdeb("'$id' already exists; updating.");
102 if (strcmp($session->session_data, $session_data) == 0) {
103 self::logdeb("Not writing session '$id'; unchanged");
106 self::logdeb("Session '$id' data changed; updating");
108 $orig = clone($session);
110 $session->session_data = $session_data;
112 $result = $session->update($orig);
115 common_log_db_error($session, 'UPDATE', __FILE__);
116 self::logdeb("Failed to update '$id'.");
118 self::logdeb("Successfully updated '$id' (result = $result).");
126 static function destroy($id)
128 self::logdeb("Deleting session $id");
130 $session = Session::staticGet('id', $id);
132 if (empty($session)) {
133 self::logdeb("Can't find '$id' to delete.");
135 $result = $session->delete();
137 common_log_db_error($session, 'DELETE', __FILE__);
138 self::logdeb("Failed to delete '$id'.");
140 self::logdeb("Successfully deleted '$id' (result = $result).");
146 static function gc($maxlifetime)
148 self::logdeb("garbage collection (maxlifetime = $maxlifetime)");
150 $epoch = common_sql_date(time() - $maxlifetime);
154 $session = new Session();
155 $session->whereAdd('modified < "'.$epoch.'"');
156 $session->selectAdd();
157 $session->selectAdd('id');
159 $limit = common_config('sessions', 'gc_limit');
161 // On large sites, too many sessions to expire
162 // at once will just result in failure.
163 $session->limit($limit);
168 while ($session->fetch()) {
169 $ids[] = $session->id;
174 self::logdeb("Found " . count($ids) . " ids to delete.");
176 foreach ($ids as $id) {
177 self::logdeb("Destroying session '$id'.");
182 static function setSaveHandler()
184 self::logdeb("setting save handlers");
185 $result = session_set_save_handler('Session::open', 'Session::close', 'Session::read',
186 'Session::write', 'Session::destroy', 'Session::gc');
187 self::logdeb("save handlers result = $result");
189 // PHP 5.3 with APC ends up destroying a bunch of object stuff before the session
190 // save handlers get called on request teardown.
191 // Registering an explicit shutdown function should take care of this before
192 // everything breaks on us.
193 register_shutdown_function('Session::cleanup');
198 static function cleanup()
200 session_write_close();