| 1 | <?php |
|---|
| 2 | // $Id: session.php,v 1.2 2005/03/18 12:52:14 onokazu Exp $ |
|---|
| 3 | // ------------------------------------------------------------------------ // |
|---|
| 4 | // XOOPS - PHP Content Management System // |
|---|
| 5 | // Copyright (c) 2000 XOOPS.org // |
|---|
| 6 | // <http://www.xoops.org/> // |
|---|
| 7 | // ------------------------------------------------------------------------ // |
|---|
| 8 | // This program is free software; you can redistribute it and/or modify // |
|---|
| 9 | // it under the terms of the GNU General Public License as published by // |
|---|
| 10 | // the Free Software Foundation; either version 2 of the License, or // |
|---|
| 11 | // (at your option) any later version. // |
|---|
| 12 | // // |
|---|
| 13 | // You may not change or alter any portion of this comment or credits // |
|---|
| 14 | // of supporting developers from this source code or any supporting // |
|---|
| 15 | // source code which is considered copyrighted (c) material of the // |
|---|
| 16 | // original comment or credit authors. // |
|---|
| 17 | // // |
|---|
| 18 | // This program is distributed in the hope that it will be useful, // |
|---|
| 19 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
|---|
| 20 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
|---|
| 21 | // GNU General Public License for more details. // |
|---|
| 22 | // // |
|---|
| 23 | // You should have received a copy of the GNU General Public License // |
|---|
| 24 | // along with this program; if not, write to the Free Software // |
|---|
| 25 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // |
|---|
| 26 | // ------------------------------------------------------------------------ // |
|---|
| 27 | // Author: Kazumi Ono (AKA onokazu) // |
|---|
| 28 | // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ // |
|---|
| 29 | // Project: The XOOPS Project // |
|---|
| 30 | // ------------------------------------------------------------------------- // |
|---|
| 31 | /** |
|---|
| 32 | * @package kernel |
|---|
| 33 | * |
|---|
| 34 | * @author Kazumi Ono <[email protected]> |
|---|
| 35 | * @copyright copyright (c) 2000-2003 XOOPS.org |
|---|
| 36 | */ |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * Handler for a session |
|---|
| 41 | * @package kernel |
|---|
| 42 | * |
|---|
| 43 | * @author Kazumi Ono <[email protected]> |
|---|
| 44 | * @copyright copyright (c) 2000-2003 XOOPS.org |
|---|
| 45 | */ |
|---|
| 46 | class XoopsSessionHandler |
|---|
| 47 | { |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * Database connection |
|---|
| 51 | * |
|---|
| 52 | * @var object |
|---|
| 53 | * @access private |
|---|
| 54 | */ |
|---|
| 55 | var $db; |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | * Constructor |
|---|
| 59 | * |
|---|
| 60 | * @param object &$mf reference to a XoopsManagerFactory |
|---|
| 61 | * |
|---|
| 62 | */ |
|---|
| 63 | function XoopsSessionHandler(&$db) |
|---|
| 64 | { |
|---|
| 65 | $this->db =& $db; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * Open a session |
|---|
| 70 | * |
|---|
| 71 | * @param string $save_path |
|---|
| 72 | * @param string $session_name |
|---|
| 73 | * |
|---|
| 74 | * @return bool |
|---|
| 75 | */ |
|---|
| 76 | function open($save_path, $session_name) |
|---|
| 77 | { |
|---|
| 78 | return true; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | /** |
|---|
| 82 | * Close a session |
|---|
| 83 | * |
|---|
| 84 | * @return bool |
|---|
| 85 | */ |
|---|
| 86 | function close() |
|---|
| 87 | { |
|---|
| 88 | return true; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | /** |
|---|
| 92 | * Read a session from the database |
|---|
| 93 | * |
|---|
| 94 | * @param string &sess_id ID of the session |
|---|
| 95 | * |
|---|
| 96 | * @return array Session data |
|---|
| 97 | */ |
|---|
| 98 | function read($sess_id) |
|---|
| 99 | { |
|---|
| 100 | $sql = sprintf('SELECT sess_data FROM %s WHERE sess_id = %s', $this->db->prefix('session'), $this->db->quoteString($sess_id)); |
|---|
| 101 | if (false != $result = $this->db->query($sql)) { |
|---|
| 102 | if (list($sess_data) = $this->db->fetchRow($result)) { |
|---|
| 103 | return $sess_data; |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | return ''; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * Write a session to the database |
|---|
| 111 | * |
|---|
| 112 | * @param string $sess_id |
|---|
| 113 | * @param string $sess_data |
|---|
| 114 | * |
|---|
| 115 | * @return bool |
|---|
| 116 | **/ |
|---|
| 117 | function write($sess_id, $sess_data) |
|---|
| 118 | { |
|---|
| 119 | $sess_id = $this->db->quoteString($sess_id); |
|---|
| 120 | list($count) = $this->db->fetchRow($this->db->query("SELECT COUNT(*) FROM ".$this->db->prefix('session')." WHERE sess_id=".$sess_id)); |
|---|
| 121 | if ( $count > 0 ) { |
|---|
| 122 | $sql = sprintf('UPDATE %s SET sess_updated = %u, sess_data = %s WHERE sess_id = %s', $this->db->prefix('session'), time(), $this->db->quoteString($sess_data), $sess_id); |
|---|
| 123 | } else { |
|---|
| 124 | $sql = sprintf('INSERT INTO %s (sess_id, sess_updated, sess_ip, sess_data) VALUES (%s, %u, %s, %s)', $this->db->prefix('session'), $sess_id, time(), $this->db->quoteString($_SERVER['REMOTE_ADDR']), $this->db->quoteString($sess_data)); |
|---|
| 125 | } |
|---|
| 126 | if (!$this->db->queryF($sql)) { |
|---|
| 127 | return false; |
|---|
| 128 | } |
|---|
| 129 | return true; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | /** |
|---|
| 133 | * Destroy a session |
|---|
| 134 | * |
|---|
| 135 | * @param string $sess_id |
|---|
| 136 | * |
|---|
| 137 | * @return bool |
|---|
| 138 | **/ |
|---|
| 139 | function destroy($sess_id) |
|---|
| 140 | { |
|---|
| 141 | $sql = sprintf('DELETE FROM %s WHERE sess_id = %s', $this->db->prefix('session'), $this->db->quoteString($sess_id)); |
|---|
| 142 | if ( !$result = $this->db->queryF($sql) ) { |
|---|
| 143 | return false; |
|---|
| 144 | } |
|---|
| 145 | return true; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | /** |
|---|
| 149 | * Garbage Collector |
|---|
| 150 | * |
|---|
| 151 | * @param int $expire Time in seconds until a session expires |
|---|
| 152 | * @return bool |
|---|
| 153 | **/ |
|---|
| 154 | function gc($expire) |
|---|
| 155 | { |
|---|
| 156 | $mintime = time() - intval($expire); |
|---|
| 157 | $sql = sprintf('DELETE FROM %s WHERE sess_updated < %u', $this->db->prefix('session'), $mintime); |
|---|
| 158 | return $this->db->queryF($sql); |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | ?> |
|---|