plugins/WhosOnline.php
author dan@fuhry
Wed, 13 Jun 2007 22:35:49 -0400
changeset 0 a314569f8829
child 1 5cf660b4dce2
permissions -rw-r--r--
Initial repository population
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();
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    68
  while ( $row = $db->fetchrow() )
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
    {
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    73
      switch($row['user_level'])
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    74
      {
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    75
        case USER_LEVEL_MEMBER:
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    76
        default:
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    77
          $color = '303030';
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    78
          $weight = 'normal';
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    79
          break;
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    80
        case USER_LEVEL_MOD:
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    81
          $color = '00AA00';
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    82
          $weight = 'bold';
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    83
          break;
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    84
        case USER_LEVEL_ADMIN:
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    85
          $color = 'AA0000';
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    86
          $weight = 'bold';
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    87
          break;
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    88
      }
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    89
      $users[] = "<a href='".makeUrlNS('User', str_replace(' ', '_', $row['username']))."' style='color: #$color; font-weight: $weight'>{$row['username']}</a>";
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    90
      $whos_online['users'][] = $row['username'];
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    91
    }
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    92
    else
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    93
    {
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    94
      $whos_online['guests'][] = $row['username'];
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    95
    }
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    96
  }
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    97
  $total = $num_guests + $num_users;
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    98
  $ms = ( $num_users == 1 ) ? '' : 's';
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
    99
  $gs = ( $num_guests == 1 ) ? '' : 's';
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   100
  $ts = ( $total == 1 ) ? '' : 's';
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   101
  $is_are = ( $total == 1 ) ? 'is' : 'are';
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   102
  $users = implode(', ', $users);
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   103
  $online_main = ( $num_users > 0 ) ? "<br />
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   104
               Users online right now:
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   105
               <div style='max-height: 100px; clip: rect(0px,auto,auto,0px); overflow: auto;'>
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   106
               $users
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   107
               </div>
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   108
               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
   109
               : '';
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   110
  $html = "<div style='padding: 5px;'>
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   111
             <small>
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   112
               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
   113
               $online_main
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   114
             </small>
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   115
           </div>";
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   116
  $template->sidebar_widget('Who\'s Online', $html);
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   117
}
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   118
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   119
function __WhosOnline_logonhandler()
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   120
{
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   121
  global $db, $session, $paths, $template, $plugins; // Common objects
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   122
  $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
   123
  if(!$q)
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   124
    echo $db->get_error();
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   125
  if(!$session->theme)
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   126
    $session->register_guest_session();
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   127
  $template->load_theme($session->theme, $session->style);
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   128
  __WhosOnline_UserCount();
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   129
}
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   130
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   131
function __WhosOnline_logoffhandler($username, $user_id, $level)
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   132
{
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   133
  if($level <= USER_LEVEL_MEMBER)
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   134
  {
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   135
    global $db, $session, $paths, $template, $plugins; // Common objects
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   136
    $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
   137
    if(!$q)
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   138
      echo $db->get_error();
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   139
    $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
   140
    if(!$q)
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   141
      echo $db->get_error();
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   142
  }
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   143
}
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   144
 
a314569f8829 Initial repository population
dan@fuhry
parents:
diff changeset
   145
?>