<?php

if ( ! @include_once( './mainfile.php' ) ) { exit( '!! INCLUDE ERROR. ACCESS DENIED !!' ); }
error_reporting( _E_VALUE );

/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | myPHPNuke Version 188_8 RC2                                          |
// +----------------------------------------------------------------------+
// | License: GNU/GPL - http://www.gnu.org/copyleft/gpl.html              |
// +----------------------------------------------------------------------+
// | Support: http://myphpnuke.com/                                       |
// +----------------------------------------------------------------------+
// | Author: myPHPNuke Dev Team                                           |
// +----------------------------------------------------------------------+
// | Copyright: © 2001-2004 myPHPNuke Dev Team                            |
// +----------------------------------------------------------------------+
// $Id: chattop.php,v 1.10.2.5 2004/08/27 15:51:16 kirilt Exp $



######################################################################
# ChatBox II
# =============
#
# Copyright (C) 2001 by Gerard Samuel (trini0@optonline.net)
#
# Based on code found in myPHPNuke
# Ingo van Peeren (ingo@beginner-ag.de)
# Michael Yarbrough (http://www.comediccadavers.com/)
######################################################################
# 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.
######################################################################

$userinfo =& $GLOBALS['uinf'];
$username = ( _MPN_USER ) ? ( $userinfo['uname'] ) : ( (bool)FALSE );

if ( ! isset( $userinfo['theme'] ) || $userinfo['theme'] == '' || ! is_dir( './themes/' . $userinfo['theme'] ) )
{
    $getTheme = $Default_Theme;
}
else
{
    $getTheme = $userinfo['theme'];
}

include 'themes/' . $getTheme . '/theme.php';

$CachePlus  = NULL;

if ( ereg("MSIE [56789]" , ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) ? ( $_SERVER['HTTP_USER_AGENT'] ) : ( $_ENV['HTTP_USER_AGENT'] ) ) )
{
    $CachePlus = ", pre-check=0, post-check=0, max-age=0";
}

$now = gmdate('D, d M Y H:i:s') . ' GMT';

header("Expires: $now");
header("Last-Modified: $now");
header("Cache-Control: no-cache, must-revalidate".$CachePlus);
header("Pragma: no-cache");

echo "<!DOCTYPE html
    PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
   \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";

echo "<html id='chattop'>\n";

    echo "<head>\n";

    echo "<title>Chat Top</title>\n";

    echo "<meta http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-1\" />\n";

    if ( ! is_dir( './themes/' . $getTheme . '/style/' . $getTheme . '.css' ) )
    {
        $getCSS = './themes/' . $getTheme . '/style/' . $getTheme . '.css';

        echo "<link rel='stylesheet' href='$getCSS' type='text/css' />\n";
    }

    echo '<script type="text/javascript" src="' . _MPN_ROOT_URI . 'mpn_misc.js"></script>' . "\n";

    echo "</head>\n";

    echo "<body bgcolor=\"$bgcolor1\" color=\"$textcolor1\" onload=window.setTimeout(\"location.href='".$_SERVER['PHP_SELF']."'\",10000)>\n";


        if ( _MPN_USER )
        {
            $last_date = printPage();
            next_entries($last_date);
        }

echo "</body>\n</html>\n";



function viewsmile($message)
{
        $ipth = "<img src=\"images/forum/smilies/";
        $message = str_replace(":D", "".$ipth."icon_biggrin.gif\">", $message);
       $message = str_replace(":-D", "".$ipth."icon_biggrin.gif\">", $message);
        $message = str_replace(":grin:", "".$ipth."icon_biggrin.gif\">", $message);
        $message = str_replace(":)", "".$ipth."icon_smile.gif\">", $message);
        $message = str_replace(":-)", "".$ipth."icon_smile.gif\">", $message);
        $message = str_replace(":smile:", "".$ipth."icon_smile.gif\">", $message);
        $message = str_replace(":(", "".$ipth."icon_frown.gif\">", $message);
        $message = str_replace(":-(", "".$ipth."icon_frown.gif\">", $message);
        $message = str_replace(":sad:", "".$ipth."icon_frown.gif\">", $message);
        $message = str_replace(":O", "".$ipth."icon_eek.gif\">", $message);
        $message = str_replace(":-O", "".$ipth."icon_eek.gif\">", $message);
        $message = str_replace(":eek:", "".$ipth."icon_eek.gif\">", $message);
        $message = str_replace(":-?", "".$ipth."icon_confused.gif\">", $message);
        $message = str_replace(":???:", "".$ipth."icon_confused.gif\">", $message);
        $message = str_replace("8)", "".$ipth."icon_cool.gif\">", $message);
        $message = str_replace("8-)", "".$ipth."icon_cool.gif\">", $message);
        $message = str_replace(":cool:", "".$ipth."icon_cool.gif\">", $message);
        $message = str_replace(":lol:", "".$ipth."icon_lol.gif\">", $message);
        $message = str_replace(":x", "".$ipth."icon_mad.gif\">", $message);
        $message = str_replace(":-x", "".$ipth."icon_mad.gif\">", $message);
        $message = str_replace(":mad:", "".$ipth."icon_mad.gif\">", $message);
        $message = str_replace(":P", "".$ipth."icon_razz.gif\">", $message);
        $message = str_replace(":-P", "".$ipth."icon_razz.gif\">", $message);
        $message = str_replace(":razz:", "".$ipth."icon_razz.gif\">", $message);
        $message = str_replace(":oops:", "".$ipth."icon_redface.gif\">", $message);
        $message = str_replace(":cry:", "".$ipth."icon_cry.gif\">", $message);
        $message = str_replace(":evil:", "".$ipth."icon_evil.gif\">", $message);
        $message = str_replace(":roll:", "".$ipth."icon_rolleyes.gif\">", $message);
        $message = str_replace(":wink:", "".$ipth."icon_wink.gif\">", $message);
        $message = str_replace(";)", "".$ipth."icon_wink.gif\">", $message);
        $message = str_replace(":pint:", "".$ipth."icon_drink.gif\">", $message);
        $message = str_replace(":hammer:", "".$ipth."icon_hammer.gif\">", $message);
        $message = str_replace(":idea:", "".$ipth."icon_idea.gif\">", $message);
        return($message);
}

function printPage()
{
    global $mpnTables, $phpEx;
    $result2 = mysql_query("SELECT COUNT(*) FROM ".$mpnTables['chatboxII']."");
    $row = mysql_fetch_row($result2);
    $minposts = 20;
    if ($row[0] < $minposts) {
        $minposts = $row[0];
        $row = '0';
    } else {
        $row = $row[0] - $minposts;
    }

    $last_date = null;
    $result = mysql_query("SELECT username, message, date FROM ".$mpnTables['chatboxII']." ORDER BY date ASC LIMIT $row,$minposts");

    while(list($username, $message, $mdate) = mysql_fetch_array($result))
    {
        $message = viewsmile($message);
        $last_date = $mdate;
        if ($username != "" || $message != "")
        {
            print "<small><a href=\"user.$phpEx?op=userinfo&uname=$username\" target=\"_blank\">$username</a>> $message</small><br>\n";
            print "<script language=\"javascript\"><!--\n scrollen( '0', '500' ); //--></script>";
        }
    }

    return $last_date;
}

function next_entries($last_date)
{
    global $mpnTables;

    if (ob_get_length() > 0)
    {
        ob_end_flush();
    }

    ignore_user_abort(1);

    while(!connection_aborted())
    {
        $result = mysql_query("SELECT username, message, date FROM ".$mpnTables['chatboxII']." ORDER BY date ASC");

        if (mysql_num_rows($result) > 0)
        {
            while(list($username, $message, $mdate) = mysql_fetch_array($result))
            {
                if ($mdate > $last_date)
                {
                    $message = viewsmile($message);
                    $last_date = $mdate;
                    if ($username != "" || $message != "")
                    {
                        echo "<small><a href=\"user.php?op=userinfo&uname=$username\" target=\"_blank\">$username</a>> $message</small><br>\n";
                        echo "<script language=\"javascript\"><!--\n scrollen( '0', '500' );\n //--></script>";
                    }
                }
            }
        }

        flush();
        sleep(1);
        $test = ini_get('safe_mode');
        if ($test == "" || $test == 'off')
        {
            @set_time_limit(30);
        }
    }
}

?>