plugins/WhosOnline.php
changeset 0 a314569f8829
child 1 5cf660b4dce2
equal deleted inserted replaced
-1:000000000000 0:a314569f8829
       
     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();
       
    68   while ( $row = $db->fetchrow() )
       
    69   {
       
    70     ( $row['user_id'] == 1 ) ? $num_guests++ : $num_users++;
       
    71     if($row['user_id'] > 1)
       
    72     {
       
    73       switch($row['user_level'])
       
    74       {
       
    75         case USER_LEVEL_MEMBER:
       
    76         default:
       
    77           $color = '303030';
       
    78           $weight = 'normal';
       
    79           break;
       
    80         case USER_LEVEL_MOD:
       
    81           $color = '00AA00';
       
    82           $weight = 'bold';
       
    83           break;
       
    84         case USER_LEVEL_ADMIN:
       
    85           $color = 'AA0000';
       
    86           $weight = 'bold';
       
    87           break;
       
    88       }
       
    89       $users[] = "<a href='".makeUrlNS('User', str_replace(' ', '_', $row['username']))."' style='color: #$color; font-weight: $weight'>{$row['username']}</a>";
       
    90       $whos_online['users'][] = $row['username'];
       
    91     }
       
    92     else
       
    93     {
       
    94       $whos_online['guests'][] = $row['username'];
       
    95     }
       
    96   }
       
    97   $total = $num_guests + $num_users;
       
    98   $ms = ( $num_users == 1 ) ? '' : 's';
       
    99   $gs = ( $num_guests == 1 ) ? '' : 's';
       
   100   $ts = ( $total == 1 ) ? '' : 's';
       
   101   $is_are = ( $total == 1 ) ? 'is' : 'are';
       
   102   $users = implode(', ', $users);
       
   103   $online_main = ( $num_users > 0 ) ? "<br />
       
   104                Users online right now:
       
   105                <div style='max-height: 100px; clip: rect(0px,auto,auto,0px); overflow: auto;'>
       
   106                $users
       
   107                </div>
       
   108                Legend:<br /><span style='color: #00AA00; font-weight: bold;'>Moderators</span> :: <span style='color: #AA0000; font-weight: bold;'>Administrators</span>"
       
   109                : '';
       
   110   $html = "<div style='padding: 5px;'>
       
   111              <small>
       
   112                There $is_are <b>$total</b> user$ts online :: <b>$num_guests</b> guest$gs and <b>$num_users</b> member$ms
       
   113                $online_main
       
   114              </small>
       
   115            </div>";
       
   116   $template->sidebar_widget('Who\'s Online', $html);
       
   117 }
       
   118 
       
   119 function __WhosOnline_logonhandler()
       
   120 {
       
   121   global $db, $session, $paths, $template, $plugins; // Common objects
       
   122   $q = $db->sql_query('DELETE FROM '.table_prefix.'online WHERE user_id=1 AND username=\''.$db->escape($_SERVER['REMOTE_ADDR']).'\';');
       
   123   if(!$q)
       
   124     echo $db->get_error();
       
   125   if(!$session->theme)
       
   126     $session->register_guest_session();
       
   127   $template->load_theme($session->theme, $session->style);
       
   128   __WhosOnline_UserCount();
       
   129 }
       
   130 
       
   131 function __WhosOnline_logoffhandler($username, $user_id, $level)
       
   132 {
       
   133   if($level <= USER_LEVEL_MEMBER)
       
   134   {
       
   135     global $db, $session, $paths, $template, $plugins; // Common objects
       
   136     $q = $db->sql_query('DELETE FROM '.table_prefix.'online WHERE user_id=\''.intval($user_id).'\' AND username=\''.$db->escape($username).'\';');
       
   137     if(!$q)
       
   138       echo $db->get_error();
       
   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('');
       
   140     if(!$q)
       
   141       echo $db->get_error();
       
   142   }
       
   143 }
       
   144  
       
   145 ?>