<?php
$pun_root = './';

if (!defined('PUN_ROOT')) define('PUN_ROOT','./');

require $pun_root.'include/common.php';

if ($pun_user['g_read_board'] == '0')
	message($lang_common['No view']);

//Hide ChatBox from Guest
//if ($pun_user['is_guest'])
//   message($lang_common['No permission']);

$page_title=pun_htmlspecialchars($pun_config['o_board_title']).' / ChatBox';
require $pun_root.'lang/'.$pun_user['language'].'/'.'/chatbox.php';

define('PUN_ALLOW_INDEX', 1);
require PUN_ROOT.'header.php';

###################### Configuration of ChatBox ########################

$hide_smilies = 0;			// 1 = hide smilies, 0 = show smilies
$msg_max = 40;				// max. messages in chatbox
$file_name = 'chatbox.txt'; // file where there are all messages
$box_height = '250px';		// height of chatbox

########################################################################

require PUN_ROOT.'include/parser.php';

$tpl_room = file_get_contents(PUN_ROOT.$file_name);

$messages = explode("</MESSAGE>", $tpl_room);

$nb_messages = count($messages);

$room = '';
$room_txt = '';
$anti_flood = 0;

if (isset($_GET['action']) && isset($_POST['req_message']))
  $room_tmp_flood = '<MESSAGE><ID>'.$pun_user['id'].'</ID><TEXT>'.$_POST['req_message'].'</TEXT>';
else
  $room_tmp_flood = '<no_post>';

  
for($i=0;$i<$nb_messages - 1;$i++)
{
  $grab = ereg("<ID>(.*)</ID>", $messages[$i], $mess_id);
  $grab = ereg("<PSEUDO>(.*)</PSEUDO>", $messages[$i], $mess_pseudo);
  $grab = ereg("<DATE>(.*)</DATE>", $messages[$i], $mess_date);
  $grab = ereg("<TEXT>(.*)</TEXT>", $messages[$i], $mess_text);

  $room .= '[<a href="profile.php?id='.$mess_id[1].'"><strong>'.pun_htmlspecialchars($mess_pseudo[1]).'</strong></a> - '.format_time($mess_date[1])."]".parse_message($mess_text[1], $hide_smilies)."\n".'<br />'."\n";
  if ($i+1 < $msg_max)
  {

  $room_txt_flood = '<MESSAGE><ID>'.$mess_id[1].'</ID><TEXT>'.$mess_text[1].'</TEXT>';

   if ($room_tmp_flood == $room_txt_flood)
    $anti_flood = 1;

    $room_txt .= '<MESSAGE>'."\r".' <ID>'.$mess_id[1].'</ID>'."\r".' <PSEUDO>'.$mess_pseudo[1].'</PSEUDO>'."\r".' <DATE>'.$mess_date[1].'</DATE>'."\r".' <TEXT>'.$mess_text[1].'</TEXT>'."\r".'</MESSAGE>'."\n\n";

  }
}

if (isset($_GET['action']) && isset($_POST['req_message']))
{
    if ($anti_flood == 0 && $_POST['req_message'] != '')
    {
      $fp = fopen(PUN_ROOT.$file_name, "w");
      if ($fp)
      {
        $room_txt = '<MESSAGE>'."\r".' <ID>'.$pun_user['id'].'</ID>'."\r".' <PSEUDO>'.$pun_user['username'].'</PSEUDO>'."\r".' <DATE>'.time().'</DATE>'."\r".' <TEXT>'.$_POST['req_message'].'</TEXT>'."\r".'</MESSAGE>'."\n\n".$room_txt;
        $r = fwrite($fp, $room_txt);
      }
      fclose($fp); 
      $room = '[<a href="profile.php?id='.$pun_user['id'].'"><strong>'.pun_htmlspecialchars($pun_user['username']).'</strong></a> - '.format_time(time())."]".parse_message($_POST['req_message'], $hide_smilies)."\n".'<br />'."\n".$room;
    }
    elseif ($anti_flood == 1)
    {
      $room = '<strong><font color=red>'.$lang_chatbox['Anti flood'].'</font></strong>'."\n".'<br />'."\n".'<br />'."\n".$room;
    }
    else
    {
      $room = '<strong><font color=red>'.$lang_chatbox['Anti blank'].'</font></strong>'."\n".'<br />'."\n".'<br />'."\n".$room;
    }
}
?>
<meta http-equiv="refresh" content="61">


<div class="block">
	<h2><span><?php echo $lang_chatbox['Chatbox']; ?></span></h2>
	<div class="box">
		<div class="inbox">
			<div class="inform">
				<fieldset>
					<div style="padding:5px;margin:0;overflow:auto;height:<?php echo $box_height; ?>;">
						<?php echo $room; ?>
					</div>
				</fieldset>
			</div>
<?php
if(!$pun_user['is_guest'])
{
?>     
			<script type="text/javascript">
			<!--
			function refreshRoom()
			{
				window.location="<?php echo $pun_root.'chatbox.php' ?>";
			}
			// -->
			</script>

			<form method="post" name="form_room" action="?action=send" onsubmit="this.submit.disabled=true;if(process_form(this)){return true;}else{this.submit.disabled=false;return false;}">
			<div class="inform">
				<fieldset>
					<legend><?php echo $lang_chatbox['Messages']; ?></legend>
          
          <label><textarea name="req_message" rows="5" cols="50" tabindex="1"></textarea></label>
					
				</fieldset>
			</div>
			
        <p>
          <input type="submit" name="send" tabindex="2" value="  <?php echo $lang_chatbox['Btn Send']; ?>  " />
          <input type="button" name="refresh" tabindex="3" value=" <?php echo $lang_chatbox['Btn Refresh']; ?> " onClick="refreshRoom()">
          <font size="1" face="Times New Roman, Times, serif"></font> </p>
			</form>
<?php } ?>
<?php

if ($pun_config['o_users_online'] == '1')
{
	// Fetch users online info and generate strings for output
	$num_guests = 0;
	$users = array();
	$result = $db->query('SELECT user_id, ident FROM '.$db->prefix.'online WHERE idle=0 ORDER BY ident', true) or error('Unable to fetch online list', __FILE__, __LINE__, $db->error());

	while ($pun_user_online = $db->fetch_assoc($result))
	{
		if ($pun_user_online['user_id'] > 1)
			$users[] = "\n\t\t\t\t".'<dd><a href="profile.php?id='.$pun_user_online['user_id'].'">'.pun_htmlspecialchars($pun_user_online['ident']).'</a>';
		else
			++$num_guests;
	}

	$num_users = count($users);
	
	if ($num_users > 0)
		echo "\t\t\t".'<dl id="onlinelist" class= "clearb">'."\n\t\t\t\t".'<dt><strong>'.$lang_chatbox['Online'].':&nbsp;</strong></dt>'."\t\t\t\t".implode(',</dd> ', $users).'</dd>'."\n\t\t\t".'</dl>'."\n";
	else
		echo "\t\t\t".'<div class="clearer"></div>'."\n";
}



?>
		</div>

		</div>
	</div>
</div>

<?php


require PUN_ROOT.'footer.php';