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