]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Session.php
Merge branch 'master' into testing
[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             self::logdeb("Couldn't find '$id'");
68             return '';
69         } else {
70             self::logdeb("Found '$id', returning " .
71                          strlen($session->session_data) .
72                          " chars of data");
73             return (string)$session->session_data;
74         }
75     }
76
77     static function write($id, $session_data)
78     {
79         self::logdeb("Writing session '$id'");
80
81         $session = Session::staticGet('id', $id);
82
83         if (empty($session)) {
84             self::logdeb("'$id' doesn't yet exist; inserting.");
85             $session = new Session();
86
87             $session->id           = $id;
88             $session->session_data = $session_data;
89             $session->created      = common_sql_now();
90             $session->modified     = common_sql_now();
91
92             $result = $session->insert();
93
94             if (!$result) {
95                 common_log_db_error($session, 'INSERT', __FILE__);
96                 self::logdeb("Failed to insert '$id'.");
97             } else {
98                 self::logdeb("Successfully inserted '$id' (result = $result).");
99             }
100             return $result;
101         } else {
102             self::logdeb("'$id' already exists; updating.");
103             if (strcmp($session->session_data, $session_data) == 0) {
104                 self::logdeb("Not writing session '$id'; unchanged");
105                 return true;
106             } else {
107                 self::logdeb("Session '$id' data changed; updating");
108
109                 $orig = clone($session);
110
111                 $session->session_data = $session_data;
112                 $session->modified     = common_sql_now();
113
114                 $result = $session->update($orig);
115
116                 if (!$result) {
117                     common_log_db_error($session, 'UPDATE', __FILE__);
118                     self::logdeb("Failed to update '$id'.");
119                 } else {
120                     self::logdeb("Successfully updated '$id' (result = $result).");
121                 }
122
123                 return $result;
124             }
125         }
126     }
127
128     static function destroy($id)
129     {
130         self::logdeb("Deleting session $id");
131
132         $session = Session::staticGet('id', $id);
133
134         if (empty($session)) {
135             self::logdeb("Can't find '$id' to delete.");
136         } else {
137             $result = $session->delete();
138             if (!$result) {
139                 common_log_db_error($session, 'DELETE', __FILE__);
140                 self::logdeb("Failed to delete '$id'.");
141             } else {
142                 self::logdeb("Successfully deleted '$id' (result = $result).");
143             }
144             return $result;
145         }
146     }
147
148     static function gc($maxlifetime)
149     {
150         self::logdeb("garbage collection (maxlifetime = $maxlifetime)");
151
152         $epoch = common_sql_date(time() - $maxlifetime);
153
154         $ids = array();
155
156         $session = new Session();
157         $session->whereAdd('modified < "'.$epoch.'"');
158         $session->selectAdd();
159         $session->selectAdd('id');
160
161         $limit = common_config('sessions', 'gc_limit');
162         if ($limit > 0) {
163             // On large sites, too many sessions to expire
164             // at once will just result in failure.
165             $session->limit($limit);
166         }
167
168         $session->find();
169
170         while ($session->fetch()) {
171             $ids[] = $session->id;
172         }
173
174         $session->free();
175
176         self::logdeb("Found " . count($ids) . " ids to delete.");
177
178         foreach ($ids as $id) {
179             self::logdeb("Destroying session '$id'.");
180             self::destroy($id);
181         }
182     }
183
184     static function setSaveHandler()
185     {
186         self::logdeb("setting save handlers");
187         $result = session_set_save_handler('Session::open', 'Session::close', 'Session::read',
188                                            'Session::write', 'Session::destroy', 'Session::gc');
189         self::logdeb("save handlers result = $result");
190
191         // PHP 5.3 with APC ends up destroying a bunch of object stuff before the session
192         // save handlers get called on request teardown.
193         // Registering an explicit shutdown function should take care of this before
194         // everything breaks on us.
195         register_shutdown_function('Session::cleanup');
196         
197         return $result;
198     }
199
200     static function cleanup()
201     {
202         session_write_close();
203     }
204 }