<?php

if(!defined('IN_MYBB'))
	die('This file cannot be accessed directly.');

$plugins->add_hook('index_start', 'onlinetoday_hack');
$plugins->add_hook('index_end', 'onlinetoday_run');
$plugins->add_hook('global_start', 'onlinetoday_cachetemplate');

function onlinetoday_info()
{
	return array(
		'name'			=> 'Online Today',
		'description'	=> 'Shows the users online today on the index page.',
		'website'		=> 'http://mybbhacks.zingaburga.com/',
		'author'		=> 'ZiNgA BuRgA',
		'authorsite'	=> 'http://zingaburga.com/',
		'version'		=> '1.0',
		'compatibility'	=> '14*,16*',
		'guid'			=> ''
	);
}

function onlinetoday_activate()
{
	global $db;
	
	$new_template = '<tr>
<td class="tcat"><span class="smalltext"><strong>{$lang->whos_online_today}</strong> [<a href="online.php?action=today">{$lang->complete_list}</a>]</span></td>
</tr>
<tr>
<td class="trow1"><span class="smalltext">{$lang->online_today_note}<br />{$online_today}</span></td>
</tr>';
	$db->insert_query('templates', array(
		'title' => 'index_whosonline_today',
		'template' => $db->escape_string($new_template),
		'sid' => -1,
		'version' => $mybb->version_code
	));
}

function onlinetoday_deactivate()
{
	global $db;
	$db->delete_query('templates', 'title="index_whosonline_today" AND sid=-1');
}


// inject our template in
function onlinetoday_cachetemplate()
{
	global $templatelist, $current_page;
	if($current_page == 'index.php' && isset($templatelist))
		$templatelist = str_replace(',index_whosonline,', ',index_whosonline,index_whosonline_today,', $templatelist);
}

function onlinetoday_hack()
{
	global $mybb, $onlinetoday_showwol, $templates, $lang;
	$onlinetoday_showwol = ($mybb->settings['showwol'] != 0 && $mybb->usergroup['canviewonline'] != 0);
	// disable MyBB's Who's Online display to save a query
	$mybb->settings['showwol'] = 0;
	
	// template hack to allow us to put the wol back in
	if(!$templates->cache['index_boardstats'])
		$templates->cache('index_boardstats');
	$templates->cache['index_boardstats'] = str_replace('{$whosonline}', '<!-- ONLINETODAY_WOL -->', $templates->cache['index_boardstats']);
	
	
	
	
	
	
	
	/**********************************************************/
	/******* IF YOU WISH TO EDIT LANGVARS, DO SO BELOW ********/
	/**********************************************************/
	$lang->whos_online_today = "Who's Online Today";
	$lang->online_today_note = "{1} {2} active in the past day ({4} {5}, {6} of whom {7} invisible, and {8} {9}).";
	/**********************************************************/
	
	
	
	
}

function onlinetoday_run()
{
	global $mybb, $db, $lang, $online_today, $whosonline, $templates, $spiders, $onlinetoday_showwol, $boardstats;

	if(!is_array($spiders))
	{
		global $cache;
		$spiders = $cache->read('spiders');
	}
	
	// force wol time to be <= 1day
	if($mybb->settings['wolcutoff'] > 86400) $mybb->settings['wolcutoff'] = 86400;
	
	$membercount = $guestcount = $anoncount = 0;
	$membercount_day = $guestcount_day = $anoncount_day = 0;
	$doneusers = array();
	$onlinemembers = $online_today = $comma = $comma_day = '';
	$online_cutoff = ($onlinetoday_showwol ? TIME_NOW - intval($mybb->settings['wolcutoff']) : 0);

	// Loop through all users.
	$query = $db->query('
		SELECT s.sid, s.ip, s.uid, s.time, u.username, u.invisible, u.usergroup, u.displaygroup
		FROM '.$db->table_prefix.'sessions s
		LEFT JOIN '.$db->table_prefix.'users u ON (s.uid=u.uid)
		WHERE s.time>'.(TIME_NOW - 86400).'
		ORDER BY u.username ASC, s.time DESC
	');
	while($user = $db->fetch_array($query))
	{
		$append_val = '';
		if($online_cutoff)
			$show_on_online = ($user['time'] > $online_cutoff);
		
		if($user['uid'] > 0)
		{
			if($doneusers[$user['uid']] < $user['time'] || !$doneusers[$user['uid']])
			{
				++$membercount_day;
				if($show_on_online) ++$membercount;
				if($user['invisible'] == 1)
				{
					++$anoncount_day;
					if($show_on_online) ++$anoncount;
					$invisiblemark = '*';
				}
				else
					$invisiblemark = '';
				
				if($user['invisible'] != 1 || $mybb->usergroup['canviewwolinvis'] == 1 || $user['uid'] == $mybb->user['uid'])
				{
					$user['username'] = format_name($user['username'], $user['usergroup'], $user['displaygroup']);
					$user['profilelink'] = build_profile_link($user['username'], $user['uid']);
					$append_val = $user['profilelink'].$invisiblemark;
				}
				$doneusers[$user['uid']] = $user['time'];
			}
		}
		elseif(($botkey = my_strtolower(str_replace('bot=', '', $user['sid']))) !== $user['sid'] && $spiders[$botkey])
		{
			++$botcount_day;
			if($show_on_online) ++$botcount;
			$append_val = format_name($spiders[$botkey]['name'], $spiders[$botkey]['usergroup']);
		}
		else
		{
			++$guestcount_day;
			if($show_on_online) ++$guestcount;
		}
		
		if($append_val)
		{
			$online_today .= $comma_day.$append_val;
			if(!$comma_day) $comma_day = ', ';
			if($show_on_online) {
				$onlinemembers .= $comma.$append_val;
				if(!$comma) $comma = ', ';
			}
		}
	}

	// Build the who's online today bit on the index page.
	$lang->online_today_note = onlinetoday_buildlang($lang->online_today_note, $membercount_day, $anoncount_day, $guestcount_day);
	eval('$online_today = "'.$templates->get('index_whosonline_today').'";');
	
	$whosonline = '';
	if($onlinetoday_showwol)
	{
		// Build the who's online bit on the index page.
		$lang->online_note = onlinetoday_buildlang($lang->online_note, $membercount, $anoncount, $guestcount);
		eval('$whosonline = "'.$templates->get('index_whosonline').'";');
	}
	$whosonline .= $online_today;
	$boardstats = str_replace('<!-- ONLINETODAY_WOL -->', $whosonline, $boardstats);
}

function onlinetoday_buildlang(&$langvar, $membercount, $anoncount, $guestcount)
{
	global $lang, $mybb;
	$onlinecount = $membercount + $guestcount;
	$onlinebit = ($onlinecount != 1) ? $lang->online_online_plural : $lang->online_online_singular;
	$memberbit = ($membercount != 1) ? $lang->online_member_plural : $lang->online_member_singular;
	$anonbit   = ($anoncount != 1)   ? $lang->online_anon_plural   : $lang->online_anon_singular;
	$guestbit  = ($guestcount != 1)  ? $lang->online_guest_plural  : $lang->online_guest_singular;
	return $lang->sprintf($langvar, my_number_format($onlinecount), $onlinebit, $mybb->settings['wolcutoffmins'], my_number_format($membercount), $memberbit, my_number_format($anoncount), $anonbit, my_number_format($guestcount), $guestbit);
}

?>