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