1: <?php
2: // $Id: online.php,v 1.1 2007/05/15 02:34:37 minahito 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://xoopscube.jp/ //
29: // Project: The XOOPS Project //
30: // ------------------------------------------------------------------------- //
31: /**
32: * @package kernel
33: *
34: * @author Kazumi Ono <onokazu@xoops.org>
35: * @copyright copyright (c) 2000-2003 XOOPS.org
36: */
37:
38: /**
39: * A handler for "Who is Online?" information
40: *
41: * @package kernel
42: *
43: * @author Kazumi Ono <onokazu@xoops.org>
44: * @copyright copyright (c) 2000-2003 XOOPS.org
45: */
46: class XoopsOnlineHandler
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 &$db {@link XoopsHandlerFactory}
61: */
62: function XoopsOnlineHandler(&$db)
63: {
64: $this->db =& $db;
65: }
66:
67: /**
68: * Write online information to the database
69: *
70: * @param int $uid UID of the active user
71: * @param string $uname Username
72: * @param string $timestamp
73: * @param string $module Current module
74: * @param string $ip User's IP adress
75: *
76: * @return bool TRUE on success
77: */
78: function write($uid, $uname, $time, $module, $ip)
79: {
80: $uid = (int)$uid;
81: $ip = $this->db->quoteString($ip);
82: if ($uid > 0) {
83: $sql = "SELECT COUNT(*) FROM ".$this->db->prefix('online')." WHERE online_uid=".$uid;
84: } else {
85: $sql = "SELECT COUNT(*) FROM ".$this->db->prefix('online')." WHERE online_uid=".$uid." AND online_ip=".$ip;
86: }
87: list($count) = $this->db->fetchRow($this->db->queryF($sql));
88: if ( $count > 0 ) {
89: $sql = "UPDATE ".$this->db->prefix('online')." SET online_updated=".$time.", online_module = ".$module." WHERE online_uid = ".$uid;
90: if ($uid == 0) {
91: $sql .= " AND online_ip=".$ip;
92: }
93: } else {
94: $sql = sprintf("INSERT INTO %s (online_uid, online_uname, online_updated, online_ip, online_module) VALUES (%u, %s, %u, %s, %u)", $this->db->prefix('online'), $uid, $this->db->quoteString($uname), $time, $ip, $module);
95: }
96: if (!$this->db->queryF($sql)) {
97: return false;
98: }
99: return true;
100: }
101:
102: /**
103: * Delete online information for a user
104: *
105: * @param int $uid UID
106: *
107: * @return bool TRUE on success
108: */
109: function destroy($uid)
110: {
111: $sql = sprintf("DELETE FROM %s WHERE online_uid = %u", $this->db->prefix('online'), $uid);
112: if (!$result = $this->db->queryF($sql)) {
113: return false;
114: }
115: return true;
116: }
117:
118: /**
119: * Garbage Collection
120: *
121: * Delete all online information that has not been updated for a certain time
122: *
123: * @param int $expire Expiration time in seconds
124: */
125: function gc($expire)
126: {
127: $sql = sprintf("DELETE FROM %s WHERE online_updated < %u", $this->db->prefix('online'), time() - (int)$expire);
128: $this->db->queryF($sql);
129: }
130:
131: /**
132: * Get an array of online information
133: *
134: * @param object $criteria {@link CriteriaElement}
135: * @return array Array of associative arrays of online information
136: */
137: function &getAll($criteria = null)
138: {
139: $ret = array();
140: $limit = $start = 0;
141: $sql = 'SELECT * FROM '.$this->db->prefix('online');
142: if (is_object($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
143: $sql .= ' '.$criteria->renderWhere();
144: $limit = $criteria->getLimit();
145: $start = $criteria->getStart();
146: }
147: $result =& $this->db->query($sql, $limit, $start);
148: if (!$result) {
149: $ret = false;
150: return $ret;
151: }
152: while ($myrow = $this->db->fetchArray($result)) {
153: $ret[] =& $myrow;
154: unset($myrow);
155: }
156: return $ret;
157: }
158:
159: /**
160: * Count the number of online users
161: *
162: * @param object $criteria {@link CriteriaElement}
163: */
164: function getCount($criteria = null)
165: {
166: $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('online');
167: if (is_object($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
168: $sql .= ' '.$criteria->renderWhere();
169: }
170: if (!$result =& $this->db->query($sql)) {
171: $ret = false;
172: return $ret;
173: }
174: list($ret) = $this->db->fetchRow($result);
175: return $ret;
176: }
177: }
178: ?>
179: