]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Session.php
don't write session if it's unchanged
[quix0rs-gnu-social.git] / classes / Session.php
1 <?php
2 /**
3  * Table Definition for session
4  *
5  * StatusNet - the distributed open-source microblogging tool
6  * Copyright (C) 2009, StatusNet, Inc.
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
23
24 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
25
26 class Session extends Memcached_DataObject
27 {
28     ###START_AUTOCODE
29     /* the code below is auto generated do not remove the above tag */
30
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
36
37     /* Static get */
38     function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Session',$k,$v); }
39
40     /* the code above is auto generated do not remove the tag below */
41     ###END_AUTOCODE
42
43     static function logdeb($msg)
44     {
45         if (common_config('sessions', 'debug')) {
46             common_debug("Session: " . $msg);
47         }
48     }
49
50     static function open($save_path, $session_name)
51     {
52         return true;
53     }
54
55     static function close()
56     {
57         return true;
58     }
59
60     static function read($id)
61     {
62         self::logdeb("Fetching session '$id'");
63
64         $session = Session::staticGet('id', $id);
65
66         if (empty($session)) {
67             return '';
68         } else {
69             return (string)$session->session_data;
70         }
71     }
72
73     static function write($id, $session_data)
74     {
75         self::logdeb("Writing session '$id'");
76
77         $session = Session::staticGet('id', $id);
78
79         if (empty($session)) {
80             $session = new Session();
81
82             $session->id           = $id;
83             $session->session_data = $session_data;
84             $session->created      = common_sql_now();
85
86             return $session->insert();
87         } else {
88             if (strcmp($session->session_data, $session_data) == 0) {
89                 self::logdeb("Not writing session '$id'; unchanged");
90                 return true;
91             } else {
92                 self::logdeb("Session '$id' data changed; updating");
93
94                 $orig = clone($session);
95
96                 $session->session_data = $session_data;
97
98                 return $session->update($orig);
99             }
100         }
101     }
102
103     static function destroy($id)
104     {
105         self::logdeb("Deleting session $id");
106
107         $session = Session::staticGet('id', $id);
108
109         if (!empty($session)) {
110             return $session->delete();
111         }
112     }
113
114     static function gc($maxlifetime)
115     {
116         self::logdeb("garbage collection (maxlifetime = $maxlifetime)");
117
118         $epoch = common_sql_date(time() - $maxlifetime);
119
120         $ids = array();
121
122         $session = new Session();
123         $session->whereAdd('modified < "'.$epoch.'"');
124         $session->selectAdd();
125         $session->selectAdd('id');
126
127         $session->find();
128
129         while ($session->fetch()) {
130             $ids[] = $session->id;
131         }
132
133         $session->free();
134
135         foreach ($ids as $id) {
136             self::destroy($id);
137         }
138     }
139
140     static function setSaveHandler()
141     {
142         self::logdeb("setting save handlers");
143         $result = session_set_save_handler('Session::open', 'Session::close', 'Session::read',
144                                            'Session::write', 'Session::destroy', 'Session::gc');
145         self::logdeb("save handlers result = $result");
146         return $result;
147     }
148 }