MyBB Community Forums

Full Version: Who's Online
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Removed and fixed, thanks to SentoWeb
online.php

<?php
/**
 * MyBB 1.8
 * Copyright 2014 MyBB Group, All Rights Reserved
 *
 * Website: http://www.mybb.com
 * License: http://www.mybb.com/about/license
 *
 */

define("IN_MYBB", 1);
define('THIS_SCRIPT', 'online.php');

$templatelist = "online,online_row,online_row_ip,online_today,online_today_row,online_row_ip_lookup,multipage,multipage_end,multipage_jump_page,multipage_nextpage,multipage_page,multipage_page_current,multipage_page_link_current,multipage_prevpage,multipage_start";

require_once "./global.php";
require_once MYBB_ROOT."inc/functions_post.php";
require_once MYBB_ROOT."inc/functions_online.php";
require_once MYBB_ROOT."inc/class_parser.php";
$parser = new postParser;

// Load global language phrases
$lang->load("online");

if($mybb->usergroup['canviewonline'] == 0)
{
error_no_permission();
}

// Make navigation
add_breadcrumb($lang->nav_online, "online.php");

if($mybb->get_input('action') == "5_minutes")
{
    add_breadcrumb($lang->nav_onlinetoday);

    $plugins->run_hooks("online_today_start");

    $threshold = TIME_NOW-300;
    $query = $db->simple_select("users", "COUNT(uid) AS users", "lastactive > '{$threshold}'");
    $todaycount = $db->fetch_field($query, "users");

    $query = $db->simple_select("users", "COUNT(uid) AS users", "lastactive > '{$threshold}' AND invisible = '1'");
    $invis_count = $db->fetch_field($query, "users");

    if(!$mybb->settings['threadsperpage'] || (int)$mybb->settings['threadsperpage'] < 1)
    {
        $mybb->settings['threadsperpage'] = 20;
    }

    // Add pagination
    $perpage = $mybb->settings['threadsperpage'];

    if($mybb->get_input('page', MyBB::INPUT_INT) > 0)
    {
        $page = $mybb->get_input('page', MyBB::INPUT_INT);
        $start = ($page-1) * $perpage;
        $pages = ceil($todaycount / $perpage);
        if($page > $pages)
        {
            $start = 0;
            $page = 1;
        }
    }
    else
    {
        $start = 0;
        $page = 1;
    }

    $query = $db->simple_select("users", "*", "lastactive > '{$threshold}'", array("order_by" => "lastactive", "order_dir" => "desc", "limit" => $perpage, "limit_start" => $start));

    $todayrows = '';
    while($online = $db->fetch_array($query))
    {
        $invisiblemark = '';
        if($online['invisible'] == 1)
        {
            $invisiblemark = "*";
        }

        if($online['invisible'] != 1 || $mybb->usergroup['canviewwolinvis'] == 1 || $online['uid'] == $mybb->user['uid'])
        {
            $username = $online['username'];
            $username = format_name($username, $online['usergroup'], $online['displaygroup']);
            $online['profilelink'] = build_profile_link($username, $online['uid']);
            $onlinetime = my_date($mybb->settings['timeformat'], $online['lastactive']);

            eval("\$todayrows .= \"".$templates->get("online_today_row")."\";");
        }
    }

    $todaycount = my_number_format($todaycount);
    $invis_count = my_number_format($invis_count);

    if($todaycount == 1)
    {
        $onlinetoday = $lang->member_online_today;
    }
    else
    {
        $onlinetoday = $lang->sprintf($lang->members_were_online_today, $todaycount);
    }

    if($invis_count)
    {
        $string = $lang->members_online_hidden;

        if($invis_count == 1)
        {
            $string = $lang->member_online_hidden;
        }

        $onlinetoday .= $lang->sprintf($string, $invis_count);
    }

    $multipage = multipage($todaycount, $perpage, $page, "online.php?action=today");

    $plugins->run_hooks("online_today_end");

    eval("\$today = \"".$templates->get("online_today")."\";");
    output_page($today);
} elseif($mybb->get_input('action') == "10_minutes")
{
    add_breadcrumb($lang->nav_onlinetoday);

    $plugins->run_hooks("online_today_start");

    $threshold = TIME_NOW-600;
    $query = $db->simple_select("users", "COUNT(uid) AS users", "lastactive > '{$threshold}'");
    $todaycount = $db->fetch_field($query, "users");

    $query = $db->simple_select("users", "COUNT(uid) AS users", "lastactive > '{$threshold}' AND invisible = '1'");
    $invis_count = $db->fetch_field($query, "users");

    if(!$mybb->settings['threadsperpage'] || (int)$mybb->settings['threadsperpage'] < 1)
    {
        $mybb->settings['threadsperpage'] = 20;
    }

    // Add pagination
    $perpage = $mybb->settings['threadsperpage'];

    if($mybb->get_input('page', MyBB::INPUT_INT) > 0)
    {
        $page = $mybb->get_input('page', MyBB::INPUT_INT);
        $start = ($page-1) * $perpage;
        $pages = ceil($todaycount / $perpage);
        if($page > $pages)
        {
            $start = 0;
            $page = 1;
        }
    }
    else
    {
        $start = 0;
        $page = 1;
    }

    $query = $db->simple_select("users", "*", "lastactive > '{$threshold}'", array("order_by" => "lastactive", "order_dir" => "desc", "limit" => $perpage, "limit_start" => $start));

    $todayrows = '';
    while($online = $db->fetch_array($query))
    {
        $invisiblemark = '';
        if($online['invisible'] == 1)
        {
            $invisiblemark = "*";
        }

        if($online['invisible'] != 1 || $mybb->usergroup['canviewwolinvis'] == 1 || $online['uid'] == $mybb->user['uid'])
        {
            $username = $online['username'];
            $username = format_name($username, $online['usergroup'], $online['displaygroup']);
            $online['profilelink'] = build_profile_link($username, $online['uid']);
            $onlinetime = my_date($mybb->settings['timeformat'], $online['lastactive']);

            eval("\$todayrows .= \"".$templates->get("online_today_row")."\";");
        }
    }

    $todaycount = my_number_format($todaycount);
    $invis_count = my_number_format($invis_count);

    if($todaycount == 1)
    {
        $onlinetoday = $lang->member_online_today;
    }
    else
    {
        $onlinetoday = $lang->sprintf($lang->members_were_online_today, $todaycount);
    }

    if($invis_count)
    {
        $string = $lang->members_online_hidden;

        if($invis_count == 1)
        {
            $string = $lang->member_online_hidden;
        }

        $onlinetoday .= $lang->sprintf($string, $invis_count);
    }

    $multipage = multipage($todaycount, $perpage, $page, "online.php?action=today");

    $plugins->run_hooks("online_today_end");

    eval("\$today = \"".$templates->get("online_today")."\";");
    output_page($today);
} elseif($mybb->get_input('action') == "today")
{
add_breadcrumb($lang->nav_onlinetoday);

$plugins->run_hooks("online_today_start");

$threshold = TIME_NOW-(60*60*24);
$query = $db->simple_select("users", "COUNT(uid) AS users", "lastactive > '{$threshold}'");
$todaycount = $db->fetch_field($query, "users");

$query = $db->simple_select("users", "COUNT(uid) AS users", "lastactive > '{$threshold}' AND invisible = '1'");
$invis_count = $db->fetch_field($query, "users");

if(!$mybb->settings['threadsperpage'] || (int)$mybb->settings['threadsperpage'] < 1)
{
$mybb->settings['threadsperpage'] = 20;
}

// Add pagination
$perpage = $mybb->settings['threadsperpage'];

if($mybb->get_input('page', MyBB::INPUT_INT) > 0)
{
$page = $mybb->get_input('page', MyBB::INPUT_INT);
$start = ($page-1) * $perpage;
$pages = ceil($todaycount / $perpage);
if($page > $pages)
{
$start = 0;
$page = 1;
}
}
else
{
$start = 0;
$page = 1;
}

$query = $db->simple_select("users", "*", "lastactive > '{$threshold}'", array("order_by" => "lastactive", "order_dir" => "desc", "limit" => $perpage, "limit_start" => $start));

$todayrows = '';
while($online = $db->fetch_array($query))
{
$invisiblemark = '';
if($online['invisible'] == 1)
{
$invisiblemark = "*";
}

if($online['invisible'] != 1 || $mybb->usergroup['canviewwolinvis'] == 1 || $online['uid'] == $mybb->user['uid'])
{
$username = $online['username'];
$username = format_name($username, $online['usergroup'], $online['displaygroup']);
$online['profilelink'] = build_profile_link($username, $online['uid']);
$onlinetime = my_date($mybb->settings['timeformat'], $online['lastactive']);

eval("\$todayrows .= \"".$templates->get("online_today_row")."\";");
}
}

$todaycount = my_number_format($todaycount);
$invis_count = my_number_format($invis_count);

if($todaycount == 1)
{
$onlinetoday = $lang->member_online_today;
}
else
{
$onlinetoday = $lang->sprintf($lang->members_were_online_today, $todaycount);
}

if($invis_count)
{
$string = $lang->members_online_hidden;

if($invis_count == 1)
{
$string = $lang->member_online_hidden;
}

$onlinetoday .= $lang->sprintf($string, $invis_count);
}

$multipage = multipage($todaycount, $perpage, $page, "online.php?action=today");

$plugins->run_hooks("online_today_end");

eval("\$today = \"".$templates->get("online_today")."\";");
output_page($today);
}
else
{
$plugins->run_hooks("online_start");

// Custom sorting options
if($mybb->get_input('sortby') == "username")
{
$sql = "u.username ASC, s.time DESC";
$refresh_string = "?sortby=username";
}
elseif($mybb->get_input('sortby') == "location")
{
$sql = "s.location, s.time DESC";
$refresh_string = "?sortby=location";
}
// Otherwise sort by last refresh
else
{
switch($db->type)
{
case "sqlite":
case "pgsql":
$sql = "s.time DESC";
break;
default:
$sql = "IF( s.uid >0, 1, 0 ) DESC, s.time DESC";
break;
}
$refresh_string = '';
}

$timesearch = TIME_NOW - $mybb->settings['wolcutoffmins']*60;

// Exactly how many users are currently online?
switch($db->type)
{
case "sqlite":
$sessions = array();
$query = $db->simple_select("sessions", "sid", "time > {$timesearch}");
while($sid = $db->fetch_field($query, "sid"))
{
$sessions[$sid] = 1;
}
$online_count = count($sessions);
unset($sessions);
break;
case "pgsql":
default:
$query = $db->simple_select("sessions", "COUNT(sid) as online", "time > {$timesearch}");
$online_count = $db->fetch_field($query, "online");
break;
}

if(!$mybb->settings['threadsperpage'] || (int)$mybb->settings['threadsperpage'] < 1)
{
$mybb->settings['threadsperpage'] = 20;
}

// How many pages are there?
$perpage = $mybb->settings['threadsperpage'];

if($mybb->get_input('page', MyBB::INPUT_INT) > 0)
{
$page = $mybb->get_input('page', MyBB::INPUT_INT);
$start = ($page-1) * $perpage;
$pages = ceil($online_count / $perpage);
if($page > $pages)
{
$start = 0;
$page = 1;
}
}
else
{
$start = 0;
$page = 1;
}

// Assemble page URL
$multipage = multipage($online_count, $perpage, $page, "online.php".$refresh_string);

// Query for active sessions
$query = $db->query("
SELECT DISTINCT s.sid, s.ip, s.uid, s.time, s.location, u.username, s.nopermission, u.invisible, u.usergroup, u.displaygroup
FROM ".TABLE_PREFIX."sessions s
LEFT JOIN ".TABLE_PREFIX."users u ON (s.uid=u.uid)
WHERE s.time>'$timesearch'
ORDER BY $sql
LIMIT {$start}, {$perpage}
");

// Fetch spiders
$spiders = $cache->read("spiders");

while($user = $db->fetch_array($query))
{
$plugins->run_hooks("online_user");

// Fetch the WOL activity
$user['activity'] = fetch_wol_activity($user['location'], $user['nopermission']);

$botkey = my_strtolower(str_replace("bot=", '', $user['sid']));

// Have a registered user
if($user['uid'] > 0)
{
if(empty($users[$user['uid']]) || $users[$user['uid']]['time'] < $user['time'])
{
$users[$user['uid']] = $user;
}
}
// Otherwise this session is a bot
else if(my_strpos($user['sid'], "bot=") !== false && $spiders[$botkey])
{
$user['bot'] = $spiders[$botkey]['name'];
$user['usergroup'] = $spiders[$botkey]['usergroup'];
$guests[] = $user;
}
// Or a guest
else
{
$guests[] = $user;
}
}

// Now we build the actual online rows - we do this separately because we need to query all of the specific activity and location information
$online_rows = '';
if(isset($users) && is_array($users))
{
reset($users);
foreach($users as $user)
{
$online_rows .= build_wol_row($user);
}
}
if(isset($guests) && is_array($guests))
{
reset($guests);
foreach($guests as $user)
{
$online_rows .= build_wol_row($user);
}
}

// Fetch the most online information
$most_online = $cache->read("mostonline");
$record_count = $most_online['numusers'];
$record_date = my_date('relative', $most_online['time']);

// Set automatic refreshing if enabled
if($mybb->settings['refreshwol'] > 0)
{
$refresh_time = $mybb->settings['refreshwol'] * 60;
$refresh = "<meta http-equiv=\"refresh\" content=\"{$refresh_time};URL=online.php{$refresh_string}\" />";
}

$plugins->run_hooks("online_end");

eval("\$online = \"".$templates->get("online")."\";");
output_page($online);
}


online.php?action=5_minutes
online.php?action=10_minutes

Alternatively you could hook into online_start and manually set the $mybb->settings['wolcutoffmins'] but that would require a plugin.

Quick who's online extender plugin. Makes it possible to set the minutes as a parameter in the url.

inc/plugins/whosonline.php
<?php
/**
 * MyBB 1.8
 * Copyright 2014 MyBB Group, All Rights Reserved
 *
 * Website: http://www.mybb.com
 * License: http://www.mybb.com/about/license
 *
 */

// Make sure we can't access this file directly from the browser.
if(!defined('IN_MYBB'))
{
	die('This file cannot be accessed directly.');
}

if(!defined('IN_ADMINCP')){
    $plugins->add_hook('online_start', 'whosonline_extend');
}

function whosonline_info() {
	return array(
		'name'			=> 'Who\'s online',
		'description'	=> '',
		'website'		=> 'http://mybb.com',
		'author'		=> 'Ervin',
		'version'		=> '2.0',
		'compatibility'	=> '18*',
		'codename'		=> 'whosonline'
	);
}

function whosonline_extend() {
    global $mybb;

    $minutes = $mybb->get_input('minutes');

    if (is_numeric($minutes) && $minutes > 0) {
        $mybb->settings['wolcutoffmins'] = $minutes;
    }
}

online.php?minutes=10

You can also "fake" the URL so that visitors are not even aware of the parameter. Just add this to your .htaccess:

RewriteRule ^online5.php$ online.php?minutes=5
Nothing changes, is this what i mean? I guess you did something else Big Grin
I just want another line above the current one which shows members logged in the past 24 hours, to show also who logged in the last 5 minutes.
(2015-10-08, 06:13 PM)Avanis Wrote: [ -> ]Nothing changes, is this what i mean? I guess you did something else Big Grin
I just want another line above the current one which shows members logged in the past 24 hours, to show also who logged in the last 5 minutes.


Index Page Templates > index_whosonline

<tr>
<td class="trow1"><span class="smalltext">{$online_note}<br />{$onlinemembers}</span></td>
</tr>

index.php

Replace
// Get the online users.
 if($mybb->settings['wolorder'] == 'username')
{
$order_by = 'u.username ASC';
$order_by2 = 's.time DESC';
}
else
{
$order_by = 's.time DESC';
$order_by2 = 'u.username ASC';
}

$timesearch = TIME_NOW - (int)$mybb->settings['wolcutoff'];
$comma = '';
$query = $db->query("
SELECT s.sid, s.ip, s.uid, s.time, s.location, s.location1, u.username, u.invisible, u.usergroup, u.displaygroup
FROM ".TABLE_PREFIX."sessions s
LEFT JOIN ".TABLE_PREFIX."users u ON (s.uid=u.uid)
WHERE s.time > '".$timesearch."'
ORDER BY {$order_by}, {$order_by2}
");

$forum_viewers = $doneusers = array();
$membercount = $guestcount = $anoncount = $botcount = 0;
$onlinemembers = $comma = '';

// Fetch spiders
$spiders = $cache->read('spiders');

// Loop through all users.
while($user = $db->fetch_array($query))
{
// Create a key to test if this user is a search bot.
$botkey = my_strtolower(str_replace('bot=', '', $user['sid']));

// Decide what type of user we are dealing with.
if($user['uid'] > 0)
{
// The user is registered.
if(empty($doneusers[$user['uid']]) || $doneusers[$user['uid']] < $user['time'])
{
// If the user is logged in anonymously, update the count for that.
if($user['invisible'] == 1)
{
++$anoncount;
}
++$membercount;
if($user['invisible'] != 1 || $mybb->usergroup['canviewwolinvis'] == 1 || $user['uid'] == $mybb->user['uid'])
{
// If this usergroup can see anonymously logged-in users, mark them.
if($user['invisible'] == 1)
{
$invisiblemark = '*';
}
else
{
$invisiblemark = '';
}

// Properly format the username and assign the template.
$user['username'] = format_name($user['username'], $user['usergroup'], $user['displaygroup']);
$user['profilelink'] = build_profile_link($user['username'], $user['uid']);
eval('$onlinemembers .= "'.$templates->get('index_whosonline_memberbit', 1, 0).'";');
$comma = $lang->comma;
}
// This user has been handled.
$doneusers[$user['uid']] = $user['time'];
}
}
elseif(my_strpos($user['sid'], 'bot=') !== false && $spiders[$botkey])
{
// The user is a search bot.
$onlinemembers .= $comma.format_name($spiders[$botkey]['name'], $spiders[$botkey]['usergroup']);
$comma = $lang->comma;
++$botcount;
}
else
{
// The user is a guest.
++$guestcount;
}

if($user['location1'])
{
++$forum_viewers[$user['location1']];
}
}

// Build the who's online bit on the index page.
$onlinecount = $membercount + $guestcount + $botcount;

if($onlinecount != 1)
{
$onlinebit = $lang->online_online_plural;
}
else
{
$onlinebit = $lang->online_online_singular;
}
if($membercount != 1)
{
$memberbit = $lang->online_member_plural;
}
else
{
$memberbit = $lang->online_member_singular;
}
if($anoncount != 1)
{
$anonbit = $lang->online_anon_plural;
}
else
{
$anonbit = $lang->online_anon_singular;
}
if($guestcount != 1)
{
$guestbit = $lang->online_guest_plural;
}
else
{
$guestbit = $lang->online_guest_singular;
}
$lang->online_note = $lang->sprintf($lang->online_note, my_number_format($onlinecount), $onlinebit, $mybb->settings['wolcutoffmins'], my_number_format($membercount), $memberbit, my_number_format($anoncount), $anonbit, my_number_format($guestcount), $guestbit);
eval('$whosonline = "'.$templates->get('index_whosonline').'";');

with:

    $whosonline = '';
 foreach ([5, 10, 15] as $limit) {
        // Get the online users.
        if($mybb->settings['wolorder'] == 'username')
        {
            $order_by = 'u.username ASC';
            $order_by2 = 's.time DESC';
        }
        else
        {
            $order_by = 's.time DESC';
            $order_by2 = 'u.username ASC';
        }

        $timesearch = TIME_NOW - $limit * 60;
        $comma = '';
        $query = $db->query("
SELECT s.sid, s.ip, s.uid, s.time, s.location, s.location1, u.username, u.invisible, u.usergroup, u.displaygroup
FROM ".TABLE_PREFIX."sessions s
LEFT JOIN ".TABLE_PREFIX."users u ON (s.uid=u.uid)
WHERE s.time > '".$timesearch."'
ORDER BY {$order_by}, {$order_by2}
");

        $forum_viewers = $doneusers = array();
        $membercount = $guestcount = $anoncount = $botcount = 0;
        $onlinemembers = $comma = '';

        // Fetch spiders
        $spiders = $cache->read('spiders');

        // Loop through all users.
        while($user = $db->fetch_array($query))
        {
            // Create a key to test if this user is a search bot.
            $botkey = my_strtolower(str_replace('bot=', '', $user['sid']));

            // Decide what type of user we are dealing with.
            if($user['uid'] > 0)
            {
                // The user is registered.
                if(empty($doneusers[$user['uid']]) || $doneusers[$user['uid']] < $user['time'])
                {
                    // If the user is logged in anonymously, update the count for that.
                    if($user['invisible'] == 1)
                    {
                        ++$anoncount;
                    }
                    ++$membercount;
                    if($user['invisible'] != 1 || $mybb->usergroup['canviewwolinvis'] == 1 || $user['uid'] == $mybb->user['uid'])
                    {
                        // If this usergroup can see anonymously logged-in users, mark them.
                        if($user['invisible'] == 1)
                        {
                            $invisiblemark = '*';
                        }
                        else
                        {
                            $invisiblemark = '';
                        }

                        // Properly format the username and assign the template.
                        $user['username'] = format_name($user['username'], $user['usergroup'], $user['displaygroup']);
                        $user['profilelink'] = build_profile_link($user['username'], $user['uid']);
                        eval('$onlinemembers .= "'.$templates->get('index_whosonline_memberbit', 1, 0).'";');
                        $comma = $lang->comma;
                    }
                    // This user has been handled.
                    $doneusers[$user['uid']] = $user['time'];
                }
            }
            elseif(my_strpos($user['sid'], 'bot=') !== false && $spiders[$botkey])
            {
                // The user is a search bot.
                $onlinemembers .= $comma.format_name($spiders[$botkey]['name'], $spiders[$botkey]['usergroup']);
                $comma = $lang->comma;
                ++$botcount;
            }
            else
            {
                // The user is a guest.
                ++$guestcount;
            }

            if($user['location1'])
            {
                ++$forum_viewers[$user['location1']];
            }
        }

        // Build the who's online bit on the index page.
        $onlinecount = $membercount + $guestcount + $botcount;

        if($onlinecount != 1)
        {
            $onlinebit = $lang->online_online_plural;
        }
        else
        {
            $onlinebit = $lang->online_online_singular;
        }
        if($membercount != 1)
        {
            $memberbit = $lang->online_member_plural;
        }
        else
        {
            $memberbit = $lang->online_member_singular;
        }
        if($anoncount != 1)
        {
            $anonbit = $lang->online_anon_plural;
        }
        else
        {
            $anonbit = $lang->online_anon_singular;
        }
        if($guestcount != 1)
        {
            $guestbit = $lang->online_guest_plural;
        }
        else
        {
            $guestbit = $lang->online_guest_singular;
        }


        $online_note = $lang->sprintf($lang->online_note, my_number_format($onlinecount), $onlinebit, $limit, my_number_format($membercount), $memberbit, my_number_format($anoncount), $anonbit, my_number_format($guestcount), $guestbit);
        eval('$whosonline .= "'.$templates->get('index_whosonline').'";');
    }
I've done the first code, 
<tr>
<td class="trow1"><span class="smalltext">{$online_note}<br />{$onlinemembers}</span></td>
</tr>

Can you add the other stuff in my index.php, because i feel that i will mix them up.
Attached.
Thanks a lot Big Grin