0
+ − 1
<?php
+ − 2
/*
+ − 3
Plugin Name: Who's Online
+ − 4
Plugin URI: javascript: // No URL yet, stay tuned!
+ − 5
Description: This plugin tracks who is currently online. 3 queries per page request. This plugin works ONLY with MySQL and will likely be difficult to port because it uses unique indices and the REPLACE command.
+ − 6
Author: Dan Fuhry
+ − 7
Version: 0.1
+ − 8
Author URI: http://www.enanocms.org/
+ − 9
*/
+ − 10
+ − 11
/*
+ − 12
* Who's Online plugin for Enano
+ − 13
* Version 0.1
+ − 14
* Copyright (C) 2007 Dan Fuhry
+ − 15
*
+ − 16
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
+ − 17
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
+ − 18
*
+ − 19
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
+ − 20
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
+ − 21
*/
+ − 22
+ − 23
global $whos_online;
+ − 24
$whos_online = Array('not_yet_initialized');
+ − 25
+ − 26
// First things first - create the table if needed
+ − 27
$ver = getConfig('whos_online_version');
+ − 28
if($ver != '0.1')
+ − 29
{
+ − 30
if(!
+ − 31
$db->sql_query('DROP TABLE IF EXISTS '.table_prefix.'online;')
+ − 32
) $db->_die('Could not clean out old who\'s-online table');
+ − 33
// The key on username allows the REPLACE command later, to save queries
+ − 34
if(!$db->sql_query('CREATE TABLE '.table_prefix.'online(
+ − 35
entry_id int(12) UNSIGNED NOT NULL auto_increment,
+ − 36
user_id int(12) NOT NULL,
+ − 37
username varchar(63) NOT NULL,
+ − 38
last_load int(12) NOT NULL,
+ − 39
PRIMARY KEY ( entry_id ),
+ − 40
KEY ( username )
+ − 41
);')
+ − 42
) $db->_die('Could not create new who\'s-online table');
+ − 43
if(!$db->sql_query('CREATE UNIQUE INDEX '.table_prefix.'onluser ON '.table_prefix.'online(username);'))
+ − 44
$db->_die('Could not create index on username column.');
+ − 45
setConfig('whos_online_version', '0.1');
+ − 46
}
+ − 47
+ − 48
$plugins->attachHook('session_started', '__WhosOnline_UserCount();');
+ − 49
$plugins->attachHook('login_success', '__WhosOnline_logonhandler();');
+ − 50
$plugins->attachHook('logout_success', '__WhosOnline_logoffhandler($ou, $oid, $level);');
+ − 51
+ − 52
function __WhosOnline_UserCount()
+ − 53
{
+ − 54
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 55
global $whos_online;
+ − 56
$whos_online = Array();
+ − 57
$whos_online['users'] = Array();
+ − 58
$whos_online['guests'] = Array();
+ − 59
$q = $db->sql_query('REPLACE INTO '.table_prefix.'online SET user_id='.$session->user_id.',username=\''.$db->escape($session->username).'\',last_load='.time().';'); if(!$q) $db->_die('');
+ − 60
$q = $db->sql_query('DELETE FROM '.table_prefix.'online WHERE last_load<'.( time() - 60*60*24 ).';'); if(!$q) $db->_die('');
+ − 61
$q = $db->sql_query('SELECT o.username,o.user_id,u.user_level FROM '.table_prefix.'online AS o
+ − 62
LEFT JOIN '.table_prefix.'users AS u
+ − 63
ON u.user_id=o.user_id
+ − 64
WHERE last_load>'.( time() - 60*5 - 1 ).' ORDER BY username ASC'); if(!$q) $db->_die('');
+ − 65
$num_guests = 0;
+ − 66
$num_users = 0;
+ − 67
$users = Array();
1
+ − 68
while ( $row = $db->fetchrow($q) )
0
+ − 69
{
+ − 70
( $row['user_id'] == 1 ) ? $num_guests++ : $num_users++;
+ − 71
if($row['user_id'] > 1)
+ − 72
{
1
+ − 73
if ( defined('RANK_ID_MEMBER') )
+ − 74
{
+ − 75
// We're in an Enano with rank support
+ − 76
$rankinfo = $session->get_user_rank($row['user_id']);
+ − 77
$rank_style = $rankinfo['rank_style'];
+ − 78
}
+ − 79
else
+ − 80
{
+ − 81
switch($row['user_level'])
+ − 82
{
+ − 83
case USER_LEVEL_MEMBER:
+ − 84
default:
+ − 85
$color = '303030';
+ − 86
$weight = 'normal';
+ − 87
break;
+ − 88
case USER_LEVEL_MOD:
+ − 89
$color = '00AA00';
+ − 90
$weight = 'bold';
+ − 91
break;
+ − 92
case USER_LEVEL_ADMIN:
+ − 93
$color = 'AA0000';
+ − 94
$weight = 'bold';
+ − 95
break;
+ − 96
}
+ − 97
$rank_style = "color: #$color; font-weight: $weight";
+ − 98
}
+ − 99
$users[] = "<a href='".makeUrlNS('User', str_replace(' ', '_', $row['username']))."' style=\"$rank_style\">{$row['username']}</a>";
0
+ − 100
$whos_online['users'][] = $row['username'];
+ − 101
}
+ − 102
else
+ − 103
{
+ − 104
$whos_online['guests'][] = $row['username'];
+ − 105
}
+ − 106
}
+ − 107
$total = $num_guests + $num_users;
+ − 108
$ms = ( $num_users == 1 ) ? '' : 's';
+ − 109
$gs = ( $num_guests == 1 ) ? '' : 's';
+ − 110
$ts = ( $total == 1 ) ? '' : 's';
+ − 111
$is_are = ( $total == 1 ) ? 'is' : 'are';
+ − 112
$users = implode(', ', $users);
+ − 113
$online_main = ( $num_users > 0 ) ? "<br />
+ − 114
Users online right now:
+ − 115
<div style='max-height: 100px; clip: rect(0px,auto,auto,0px); overflow: auto;'>
+ − 116
$users
+ − 117
</div>
+ − 118
Legend:<br /><span style='color: #00AA00; font-weight: bold;'>Moderators</span> :: <span style='color: #AA0000; font-weight: bold;'>Administrators</span>"
+ − 119
: '';
+ − 120
$html = "<div style='padding: 5px;'>
+ − 121
<small>
+ − 122
There $is_are <b>$total</b> user$ts online :: <b>$num_guests</b> guest$gs and <b>$num_users</b> member$ms
+ − 123
$online_main
+ − 124
</small>
+ − 125
</div>";
+ − 126
$template->sidebar_widget('Who\'s Online', $html);
+ − 127
}
+ − 128
+ − 129
function __WhosOnline_logonhandler()
+ − 130
{
+ − 131
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 132
$q = $db->sql_query('DELETE FROM '.table_prefix.'online WHERE user_id=1 AND username=\''.$db->escape($_SERVER['REMOTE_ADDR']).'\';');
+ − 133
if(!$q)
+ − 134
echo $db->get_error();
+ − 135
if(!$session->theme)
+ − 136
$session->register_guest_session();
+ − 137
$template->load_theme($session->theme, $session->style);
+ − 138
__WhosOnline_UserCount();
+ − 139
}
+ − 140
+ − 141
function __WhosOnline_logoffhandler($username, $user_id, $level)
+ − 142
{
+ − 143
if($level <= USER_LEVEL_MEMBER)
+ − 144
{
+ − 145
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 146
$q = $db->sql_query('DELETE FROM '.table_prefix.'online WHERE user_id=\''.intval($user_id).'\' AND username=\''.$db->escape($username).'\';');
+ − 147
if(!$q)
+ − 148
echo $db->get_error();
+ − 149
$q = $db->sql_query('REPLACE INTO '.table_prefix.'online SET user_id=1,username=\''.$db->escape($_SERVER['REMOTE_ADDR']).'\',last_load='.time().';'); if(!$q) $db->_die('');
+ − 150
if(!$q)
+ − 151
echo $db->get_error();
+ − 152
}
+ − 153
}
+ − 154
+ − 155
?>