<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
defined('MAXIMUS') or die ('Access denied block who is online MAXIMUS directly...');

if (defined('ADMIN_ALLOW')) {
	$block_retention  = CACHE_BL_MAXWHOISONLINE;
	$block_name_cache = "WhoIsOnlineAdmin";
	$extension        = "php";
} elseif (defined('USER_ALLOW')) {
	$block_retention  = CACHE_BL_MAXWHOISONLINE;
	$block_name_cache = "WhoIsOnlineUser";
	$extension        = "html";
} else {
	$block_retention  = CACHE_BL_MAXWHOISONLINE;
	$block_name_cache = "WhoIsOnline";
	$extension        = "html";
}

if ( $content = opencacheb ( "".$block_name_cache."", "".$extension."", "".$block_retention."" ) ) {
 } else {

$block_name = 'Who_is_Online';
get_blang("$block_name");

$content = "";

global $user, $cookie, $prefix;

cookiedecode($user);
$ip = $_SERVER['REMOTE_ADDR'];
$uname = $cookie[1];
if (!isset($uname)) {
    $uname = "$ip";
    $guest = 1;
}
$sql = "SELECT * FROM `".TABLE_SESSION."` WHERE `guest`='1'";
if ( !($resultat   = $GLOBALS['db']->sql_query($sql)) )
{ query_die(CORE_ERROR, 'Could not obtain _session', '', __LINE__, __FILE__, $sql);}
$guest_online_num = $GLOBALS['db']->sql_numrows($resultat);
$sql1 = "SELECT * FROM `".TABLE_SESSION."` WHERE `guest`='0'";
if ( !($resultat1  = $GLOBALS['db']->sql_query($sql1)) )
{ query_die(CORE_ERROR, 'Could not obtain _session', '', __LINE__, __FILE__, $sql1);}
$member_online_num = $GLOBALS['db']->sql_numrows($resultat1);

$who_online_num = $guest_online_num + $member_online_num;
$who_online = ""._CURRENTLY." $guest_online_num "._GUESTS." $member_online_num "._MEMBERS."";
br('1');
$content = "$who_online";
if (defined('USER_ALLOW')) {
    if (is_active("Private_Messages")) {
        $sql2 = "SELECT `user_id` FROM `".TABLE_USERS."` WHERE `username`='$uname'";
	if ( !($result2 = $GLOBALS['db']->sql_query($sql2)) )
{ query_die(CORE_ERROR, 'Could not obtain _users', '', __LINE__, __FILE__, $sql2);}
	$row = $GLOBALS['db']->sql_fetchrow($result2);
	$uid   = $GLOBALS['db']->XMFnum($row2['user_id']);
        $sql3 = "SELECT * FROM `".$prefix."_bbprivmsgs` WHERE `privmsgs_to_userid`='$uid' AND (`privmsgs_type`='5' OR `privmsgs_type`='1')";
	if ( !($result3 = $GLOBALS['db']->sql_query($sql3)) )
{ query_die(CORE_ERROR, 'Could not obtain _bbprivmsgs', '', __LINE__, __FILE__, $sql3);}
	$newpm = $GLOBALS['db']->sql_numrows($result3);
	$GLOBALS['db']->sql_freeresult($result2);
        $GLOBALS['db']->sql_freeresult($result3);
    }
}
$sql4 = "SELECT `title` FROM `".TABLE_BLOCKS."` WHERE `bkey`='online'";
if ( !($result4 = $GLOBALS['db']->sql_query($sql4)) )
{ query_die(CORE_ERROR, 'Could not obtain _blocks', '', __LINE__, __FILE__, $sql4);}
$row4  = $GLOBALS['db']->sql_fetchrow($result4);
$title = stripslashes($row4['title']);
if (defined('USER_ALLOW')) {
        $content .= "<br />"._YOUARELOGGED." <b>$uname</b>.<br />";
    if (is_active("Forums")) {
        $sql5 = "SELECT `user_id` FROM `".TABLE_USERS."` WHERE `username`='$uname'";
        if ( !($result5 = $GLOBALS['db']->sql_query($sql5)) )
{ query_die(CORE_ERROR, 'Could not obtain _blocks', '', __LINE__, __FILE__, $sql5);}
	$row5 = $GLOBALS['db']->sql_fetchrow($result5);
	$uid  = $GLOBALS['db']->XMFnum($row5['user_id']);
	$sql6 = "SELECT `privmsgs_to_userid` FROM `".$prefix."_bbprivmsgs` WHERE `privmsgs_to_userid`='$uid' AND (`privmsgs_type`='1' OR `privmsgs_type`='5' OR `privmsgs_type`='0')";
        if ( !($result6 = $GLOBALS['db']->sql_query($sql6)) )
{ query_die(CORE_ERROR, 'Could not obtain _blocks', '', __LINE__, __FILE__, $sql6);}
	$numrow = $GLOBALS['db']->sql_numrows($result6);
	$content .= ""._YOUHAVE." <a href=\"modules.php?name=Private_Messages\"><b>$numrow</b></a> "._PRIVATEMSG."";
	$GLOBALS['db']->sql_freeresult($result5);
        $GLOBALS['db']->sql_freeresult($result6);
    }
    $content .= "";
} else {
    $content .= "<br />"._YOUAREANON."";
}

$GLOBALS['db']->sql_freeresult($resultat);
$GLOBALS['db']->sql_freeresult($resultat1);

closecacheb ( "".$content."", "".$block_name_cache."", "".$extension."", "".$block_retention."" );
}