3 * Table Definition for session
5 * Laconica - a distributed open-source microblogging tool
6 * Copyright (C) 2009, Control Yourself, 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('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)) {
69 return (string)$session->session_data;
73 static function write($id, $session_data)
75 self::logdeb("Writing session '$id'");
77 $session = Session::staticGet('id', $id);
79 if (empty($session)) {
80 $session = new Session();
83 $session->session_data = $session_data;
84 $session->created = common_sql_now();
86 return $session->insert();
88 $session->session_data = $session_data;
90 return $session->update();
94 static function destroy($id)
96 self::logdeb("Deleting session $id");
98 $session = Session::staticGet('id', $id);
100 if (!empty($session)) {
101 return $session->delete();
105 static function gc($maxlifetime)
107 self::logdeb("garbage collection (maxlifetime = $maxlifetime)");
109 $epoch = common_sql_date(time() - $maxlifetime);
111 $session = new Session();
112 $session->whereAdd('modified < "'.$epoch.'"');
113 $result = $session->delete(DB_DATAOBJECT_WHEREADD_ONLY);
115 self::logdeb("garbage collection result = $result");
118 static function setSaveHandler()
120 self::logdeb("setting save handlers");
121 $result = session_set_save_handler('Session::open', 'Session::close', 'Session::read',
122 'Session::write', 'Session::destroy', 'Session::gc');
123 self::logdeb("save handlers result = $result");