123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- defined('GNUSOCIAL') || die();
- class InternalSessionHandler implements SessionHandlerInterface
- {
-
- public static function logdeb($msg)
- {
- if (common_config('sessions', 'debug')) {
- common_debug("Session: " . $msg);
- }
- }
-
- public function open($save_path, $session_name)
- {
- return true;
- }
-
- public function close()
- {
- return true;
- }
-
- public function read($id)
- {
- self::logdeb("Fetching session '$id'.");
- $session = Session::getKV('id', $id);
- if (empty($session)) {
- self::logdeb("Couldn't find '$id'.");
- return '';
- } else {
- self::logdeb("Found '$id', returning " .
- strlen($session->session_data) .
- " chars of data.");
- return (string)$session->session_data;
- }
- }
-
- public function write($id, $session_data)
- {
- self::logdeb("Writing session '$id'.");
- $session = Session::getKV('id', $id);
- if (empty($session)) {
- self::logdeb("'$id' doesn't yet exist; inserting.");
- $session = new Session();
- $session->id = $id;
- $session->session_data = $session_data;
- $session->created = common_sql_now();
- $result = $session->insert();
- if (!$result) {
- common_log_db_error($session, 'INSERT', __FILE__);
- self::logdeb("Failed to insert '$id'.");
- } else {
- self::logdeb("Successfully inserted '$id' (result = $result).");
- }
- return (bool) $result;
- } else {
- self::logdeb("'$id' already exists; updating.");
- if (strcmp($session->session_data, $session_data) == 0) {
- self::logdeb("Not writing session '$id' - unchanged.");
- return true;
- } else {
- self::logdeb("Session '$id' data changed - updating.");
-
- $orig = clone($session);
- $session->session_data = $session_data;
- $result = $session->update($orig);
- if (!$result) {
- common_log_db_error($session, 'UPDATE', __FILE__);
- self::logdeb("Failed to update '$id'.");
- } else {
- self::logdeb("Successfully updated '$id' (result = $result).");
- }
- return (bool) $result;
- }
- }
- }
-
- public function gc($maxlifetime)
- {
- self::logdeb("Garbage Collector has now started with maxlifetime = '$maxlifetime'.");
- $epoch = common_sql_date(time() - $maxlifetime);
- $ids = [];
- $session = new Session();
- $session->whereAdd('modified < "' . $epoch . '"');
- $session->selectAdd();
- $session->selectAdd('id');
- $limit = common_config('sessions', 'gc_limit');
- if ($limit > 0) {
-
-
- $session->limit($limit);
- }
- $session->find();
- while ($session->fetch()) {
- $ids[] = $session->id;
- }
- $session->free();
- self::logdeb("Garbage Collector found " . count($ids) . " ids to delete.");
- foreach ($ids as $id) {
- self::logdeb("Garbage Collector will now delete session '$id'.");
- self::destroy($id);
- }
- return true;
- }
-
- public function destroy($id)
- {
- self::logdeb("Destroying session '$id'.");
- $session = Session::getKV('id', $id);
- if (empty($session)) {
- self::logdeb("Can't find '$id' to destroy.");
- return false;
- } else {
- $result = $session->delete();
- if (!$result) {
- common_log_db_error($session, 'DELETE', __FILE__);
- self::logdeb("Failed to destroy '$id'.");
- } else {
- self::logdeb("Successfully destroyed '$id' (result = $result).");
- }
- return (bool) $result;
- }
- }
- }
|