<?php
/*
	Plugin:		Who Was Online Today
	Version:	1.2
	Author:		Maciek Koprek
	Nick:		"CichyWiatr" or "koperek18cm"
	Date:		05.03.2011
	
	Free for non-commercial purposes!
	License: Creative Commons Legal Code
	http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode
*/

define("IN_MYBB", 1);

$plugins->add_hook('index_start', 'wwot');

function wwot_info() {

global $lang;
	
$lang->load("config_wwot");

	return array(
		"name"			=>	$lang->wwot_info_name,
		"description"	=>	$lang->wwot_info_description,
		"website"		=>	"http://mybboard.pl/",
		"author"		=>	"Maciek Koprek (CichyWiatr)",
		"authorsite"	=>	"vodkamm@gmail.com",
		"version"		=>	"1.2",
		"compatibility" =>  "16*"
	);
}

function wwot_activate() {
	global $db, $mybb, $lang;
	
	$lang->load("config_wwot");
	
	$wwot_group_settings = array(
		"gid"               => NULL,
		"name"              => "wwot_option_category",
		"title"             => $lang->wwot_info_name,
		"description"       => $lang->wwot_setting_description,
		"disporder"         => "50",
		"isdefault"         => "no"
                        );
	$db->insert_query("settinggroups", $wwot_group_settings);
	
	$gid = $db->insert_id();
	
	$option_1 = array(
        "sid"               => NULL,
        "name"              => "wwot_enabled",
        "title"             => $lang->wwot_enable_title,
        "description"       => $lang->wwot_enable_description,
        "optionscode"       => "yesno",
        "value"             => "no",
        "disporder"         => "1",
        "gid"               => intval($gid)
						);
	$option_2 = array(
        "sid"               => NULL,
        "name"              => "wwot_mode",
        "title"             => $lang->wwot_mode_title,
        "description"       => $lang->wwot_mode_description,
        "optionscode"       => "select\n0=Simple Mode\n1=Complex Mode",
        "value"             => "0",
        "disporder"         => "2",
        "gid"               => intval($gid)
                     );
	$option_3 = array(
        "sid"               => NULL,
        "name"              => "wwot_sort",
        "title"             => $lang->wwot_sort_title,
        "description"       => $lang->wwot_sort_description,
        "optionscode"       => "select\n u.username=Username \n u.usergroup=User Group\n u.uid=User UID \n u.lastactive=Last Active",
        "value"             => "u.username",
        "disporder"         => "3",
        "gid"               => intval($gid)
                     );
	$option_4 = array(
        "sid"               => NULL,
        "name"              => "wwot_sortby",
        "title"             => $lang->wwot_sortby_title,
        "description"       => $lang->wwot_sortby_description,
        "optionscode"       => "select\n ASC=ASC \n DESC=DESC",
        "value"             => "ASC",
        "disporder"         => "4",
        "gid"               => intval($gid)
                     );
	$db->insert_query("settings", $option_1);
	$db->insert_query("settings", $option_2);
	$db->insert_query("settings", $option_3);
	$db->insert_query("settings", $option_4);
	
						   
	require MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets('index_boardstats', '#{\$whosonline}#', "{\$whosonline}\n{\$wwot}");
}

function wwot_deactivate() {

	global $db, $mybb;
	
	$db->delete_query('settinggroups', 'name = "wwot_option_category"');
	$db->delete_query('settings', 'name IN("wwot_enabled","wwot_mode","wwot_sort","wwot_sortby")');

	require MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets('index_boardstats', '#(\n?){\$wwot}#', '', 0);
}

function wwot() 
{
	global $db, $mybb, $templates, $lang, $wwot;

	$lang->load("wwot");
	
	if ($mybb->settings['wwot_enabled'] == 1)
	{

		$wwot_member = $wwot_anon = $wwot_guest = $wwot_bot = 0;

		if($mybb->settings['showwol'] && $mybb->usergroup['canviewonline'])
		{
		
			$wwot_time = TIME_NOW - 86400;
		
			 $query1 = $db->query("
				SELECT u.username, u.uid, u.usergroup, u.displaygroup, u.invisible, u.lastactive, s.sid
				FROM ".TABLE_PREFIX."users u LEFT JOIN ".TABLE_PREFIX."sessions s ON (u.uid=s.uid)
				WHERE (u.lastactive > $wwot_time OR s.time > $wwot_time)
				ORDER BY u.username 
			");

			while($wwot_line = $db->fetch_array($query1))
			{
				if ($wwot_nick_repeat != $wwot_line['username'] AND $wwot_line['usergroup'] != 0)
				{
					$wwot_invisible = '';
					if($wwot_line['invisible'] == 1)
					{
						$wwot_invisible = '*';
						$wwot_anon++;
					}else {$wwot_member++;}
					if(($mybb->usergroup['canviewwolinvis'] == 1)||($wwot_line['invisible'] != 1))
					{					
						$wwot_userstyle = format_name($wwot_line['username'], $wwot_line['usergroup'], $wwot_line['displaygroup']);
						$wwot_userlist .= build_profile_link($wwot_userstyle, $wwot_line['uid']).$wwot_invisible.', ';
					}
					$wwot_nick_repeat = $wwot_line['username'];
				}
				else
				{
					$wwot_guest++;
					if(strstr($wwot_line['sid'], "bot="))
						{$wwot_bot++;}
				}
			}
			$wwot_member += $wwot_anon;
			$wwot_userlist = substr($wwot_userlist,0,-2);
			
			if ($mybb->settings['wwot_mode'] == 0) 
			{
			$wwot =
				'<!--start Who Was Online Today-->
				<tr class="trow1">
				<tr class="trow_static"><td class="tcat_stats"><span class="smalltext"><img src="http://miastozbrodni.xaa.pl/images/reksio/birthday.png" alt="" /> <strong>Online przez ostatnie 24h</strong></span></td></tr>
				<tr class="trow1">
<td><span class="smalltext stats">'.$wwot_member.''.$lang->wwot_member.''.$wwot_anon.''.$lang->wwot_anon.'
				'.$wwot_guest.''.$lang->wwot_guest.''.$wwot_bot.''.$lang->wwot_bot.'
				<br /><span style="padding: 0 0 0 30px;">'.$lang->wwot_content_1.''.$wwot_userlist.'</span></span></td><tr/>
				</span></td>
				</tr>
				<!--I love you MM :* -->
				<!--end Who Was Online Today-->';
			}else
			{
			$wwot =
				'<!--start Who Was Online Today-->
				<tr>
				<td class="tcat" colspan="6"><span class="smalltext">
				<strong>'.$lang->wwot_tittle.' </strong>
				</span></td>
				</tr>
				<tr class="trow1">
				<tr class="trow_static"><td class="tcat_stats"><span class="smalltext"><img src="http://miastozbrodni.xaa.pl/images/reksio/birthday.png" alt="" /> <strong>'.$lang->wwot_content_1.'</strong></span></td></tr>

				'.$wwot_member.''.$lang->wwot_member.''.$wwot_anon.''.$lang->wwot_anon.'
				'.$wwot_guest.''.$lang->wwot_guest.''.$wwot_bot.''.$lang->wwot_bot.'
				<br />'.$wwot_userlist.'
				</span></td>
				</tr>
				<!--I love you MM :* -->
				<!--end Who Was Online Today-->';
			}
		}	
	}	
}
?>
