<?

/************************************************************************/
/* XChat Version 0.9.8                                                   */
/* ==================================================================== */
/* 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.       */
/************************************************************************/

?>
<html>
<head>
<script>
<?
  require("./inc/config.php");
  require("./inc/mysql.lib.php");
  require("./inc/smiley.php");

  if(file_exists("language/lang-$lang.php"))
    include("language/lang-$lang.php");
  else
    include("language/lang-english.php");

  set_magic_quotes_runtime(0);

  $DbLink = new DB($dbhost, $dbname, $dbuname, $dbpass);
  $DbUsers = new DB($dbhost, $dbname, $dbuname, $dbpass);
  $DbSmiles = new DB($dbhost, $dbname, $dbuname, $dbpass);

  $tstamp = time();
  $tstamp -= $usertime;
  $DbLink->query("DELETE from ".$prefix."_4nchatuser WHERE last < '".$tstamp."'");
  $DbLink->query("DELETE from ".$prefix."_4nchatmessage WHERE time < '".$tstamp."'");

  $toOut = "";
  if(isset($first)) {
    $toOut = "<font class=\"sysmessage\">"._WCCHATWELCOME."</font>";
    $msgLastCheck = time();
  }
  $usersounds = array();
  $DbLink->query("select m.send_id, m.rcpt_id, m.cmd, m.message, m.time from ".$prefix."_4nchatmessage as m, ".$prefix."_4nchatuser as u where m.time > '$msgLastCheck' and u.uid = $uid and m.rid = $rid and ((m.rcpt_id <= 0 ) or (m.rcpt_id = $uid )) order by m.time");
  if($DbLink->num_rows() > 0) {
     while(list($send_id, $rcpt_id, $cmd, $message, $time) = $DbLink->next_record()) {
      $message = smiley(rawurldecode($message));
      if ( $send_id != 0 ) {
        $DbUsers->query("select name from ".$prefix."_4nchatuser where uid = $send_id LIMIT 1");
        if($DbUsers->num_rows() > 0)
          list($name) = $DbUsers->next_record();
        else
        $name = "*empty*";
        $DbUsers->clean_results();
      }
      if($tf==12) {
        $timestamp = '<span class="timestamp">'.date("h:i:sa", ($time + (doubleval($tz)) * 60 * 60)).'&nbsp;</span>';
        } else {
        $timestamp = '<span class="timestamp">'.date("H:i:s", ($time + (doubleval($tz)) * 60 * 60)).'&nbsp;</span>';
        }

      if($cmd != "") {
        if ( $cmd == "USERENTER" ) {
          $toOut .= '<div class="sysmessage">'.$timestamp.'<img src="images/u3.gif" border="0" align="absmiddle">'.$message.' has entered the chat room</div>';          $uenter = true;
          ?>
          window.parent.frames['wUsers'].refresh();
          <?
        } elseif ( $cmd == "USERLEFT" ) {
          $toOut .= '<div class="sysmessage">'.$timestamp.'<img src="images/u4.gif" border="0" align="absmiddle">'.$message.' has left the chat room</div>';
          $uleft = true;
          ?>
          window.parent.frames['wUsers'].refresh();
          <?
        } elseif ($cmd == "MSG") {
          if (( $send_id != $uid ) and  ( $rcpt_id == $uid ))
            $toOut .= '<div class="message">'.$timestamp.'<font color="#5566BB"><b>(from '.$name.')</b> '.$message.'</font></div>';
        } else {
          if ( $rcpt_id == 0 ) {
            $usersounds[] = $cmd;
            $toOut .= '<div class="sysmessage">'.$timestamp.'<img src="images/beep.gif" border="0" align="absmiddle">&nbsp;'.$name.' is '.$message.' the room</div>';
          } elseif ( $rcpt_id == $uid ) {
            $usersounds[] = $cmd;
            $toOut .= '<div class="sysmessage">'.$timestamp.'<img src="images/beep.gif" border="0" align="absmiddle">&nbsp;'.$name.' is '.$message.' you</div>';
          }
        }
      } else {
        if ( $rcpt_id == 0 ) {
          if ( $send_id != $uid )
          $toOut .= '<div class="message">'.$timestamp.'<font color="red"><b>'.$name.'&gt;</b></font> '.$message.'</div>';
        } else if( $send_id != $uid )
          $toOut .= '<div class="message">'.$timestamp.'<font color="#5566BB"><b>(from '.$name.')</b> '.$message.'</font></div>';
      }
      if($time > $msgLastCheck)
        $msgLastCheck = $time;
    }
  }

  if($toOut != "") {
    ?>
    toOut = '<?=$toOut?>';
    if (document.all) {
      top.wOut.document.body.innerHTML += toOut;
    } else {
      top.wOut.document.write("<html><style>BODY {font-family: Verdana, sans-serif; color: #000055; font-size: 12px;} </style><body bgcolor='#FFFFFF'>");
      top.wOut.document.write(toOut);
    }
    <?
  }

  $DbLink->clean_results();

  $DbUsers->query("UPDATE ".$prefix."_4nchatuser SET last = ".time()." WHERE name = '$username'");
  if(!$DbUsers->affected_rows()) {

    $DbUsers->close();
    $DbLink->close();
    $DbSmiles->close();
    ?>
    top.location="quit.php?rid=<?=$rid; ?>&uid=<?=$uid; ?>&reason=1&lang=<?=$lang ?>";
    </script>
    </head>
    </html>
    <?
    exit;
  }
?>
function refresh() {
  window.location='receive.php?rid=<?=$rid ?>&uid=<?=$uid ?>&username=<?=$username ?>&tz=<?=$tz ?>&msgLastCheck=<?=$msgLastCheck ?>&allowsounds=<?=$allowsounds ?>&lang=<?=$lang ?>';
}
</script>
<?
if($allowsounds) {
  $callowsounds = "checked";
  if($uenter) {
    ?>
    <BGSOUND SRC="<?=$soundsdir?>/hi.wav" LOOP="1">
    <?
  }
  if($uleft) {
    ?>
    <BGSOUND SRC="<?=$soundsdir?>/byebye.wav" LOOP="1">
    <?
  }
  $n_usersounds = count($usersounds);
  for ($i = 0; $i < $n_usersounds; $i++) {
    $DbLink->query("SELECT file FROM ".$prefix."_4nchatsound WHERE cmd = '$usersounds[$i]' LIMIT 1");
    list($sndfile) = $DbLink->next_record();
    ?>
    <!-- User sound -->
    <BGSOUND SRC="<?=$soundsdir.'/'.$sndfile ?>" LOOP="1">
    <?
    $DbLink->clean_results();
  }
}
?>
</head>
<body bgcolor="#EAEAEA" marginwidth="0" marginheight="3" leftmargin="0" topmargin="0">
<form action="receive.php?rid=<?=$rid ?>&uid=<?=$uid ?>&username=<?=$username ?>&tz=<?=$tz ?>&msgLastCheck=<?=$msgLastCheck ?>&lang=<?=$lang ?>" method="post">
<font size="-2" face="MS Sans Serif"><input type="checkbox" name="allowsounds" onClick="submit()" <?=$callowsounds ?>>Sound</font>
</form>
<script>
<!--
setTimeout('refresh()',<?=$msg_refresh?>);
// -->
</script>
</body>
</html>
<?
if($keeponline) {
  $DbLink2 = new DB($dbhost, $dbname, $dbuname, $dbpass);
  $ip = getenv("REMOTE_ADDR");
  if($uid < 0) {
    $guest = 1;
    $uname = $ip;
  } else {
    $guest = 0;
    $uname = $username;
  }
  $past = time() - 900;
  $DbLink->query("DELETE FROM ".$prefix."_session WHERE time < ".$past);
  $ctime = time();
  $DbLink->query("SELECT username from ".$prefix."_session WHERE username = '$uname' LIMIT 1");
  if($DbLink->num_rows() == 0)
    $DbLink2->query("INSERT INTO ".$prefix."_session (username, time, host_addr, guest) VALUES ('$uname', '$ctime', '$ip', '$guest')");
  else
    $DbLink2->query("UPDATE ".$prefix."_session SET time='$ctime', host_addr='$ip' WHERE username = '$uname'");
  $DbLink->clean_results();

  if($uid > 0) {
    $DbLink->query("SELECT * FROM ".$prefix."_lastseen WHERE username = '$username' LIMIT 1");
    if($DbLink->num_rows() > 0)
      $DbLink2->query("UPDATE ".$prefix."_lastseen SET date = ".$ctime." WHERE username = '$username'");
    else
      $DbLink2->query("INSERT INTO ".$prefix."_lastseen VALUES ('', '$username', ".time().", '$ip')");
  }
  $DbLink2->close();
  $DbLink->clean_results();
}
$DbLink->close();
$DbUsers->close();
$DbSmiles->close();
?>