<?php
/************************************************************************
	UebiMiau is a GPL'ed software developed by 
	
	 - Aldoir Ventura - aldoir@users.sourceforge.net
	 - http://uebimiau.sourceforge.net
	
	Fell free to contact, send donations or anything to me :-)
	SåÐ Paulo - Brasil
	
	**********************************************
	Version 3.2.0 Upgrades and templates developed by
	 
	 - Todd Henderson - Lead Developer
	 - http://tdah.us
	 - Email: todd@tdah.us
	 
	 - Dave Rodgers - Project Consultant
	 - http://www.manvel.net 
	 - Email: Davenmanvel@inbox.com
	
	Feel free to contact us, send donations or anything to me as well...
	
	Special thanks to the developers at telaen.org, codeworxtech.com, tinymce.moxiecode.com
	without help this project wouldn't be possible 

	**********************************************
	- File:			message.php
	- Developer: 	Todd Henderson / Mods by Laurent (AdNovea)
	- Date:			November 4, 2008
	- version:		(3.2.0) 1.0
	- Description:  Manage the online entries

*************************************************************************/

	header("cache-Control: no-cache, must-revalidate"); 
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 

	require("./inc/config/config.uebimiau.php");
	require("./".PATH_LANGS."$chat_language.php");
  $chat_data_file 	= DB_CHAT_DATA;
  $chat_online_file 	= DB_CHAT_ONLINE;
  $chat_upload_dir 	= DB_CHAT_UPLOAD;
		
	session_start();
	
	// decode the message
	function decode_msg($line) {
		$text = "";
		$tmp = explode(':', $line);
		foreach ($tmp as $char) if ($char > 0 ) {
			if ($char < 32 || $char >= 128 )  $text .= "&#$char;";
			else $text .= chr($char);
		}
		return $text;
	}
	
	$nick = isset($_GET['nick']) ? $_GET['nick'] : CHAT_HIDDEN;
	$lid = $_GET['lid'];
	$tid = $_GET['tid'];
	$selected_theme 	= $themes[$tid]["path"];

	define("__BACK__","<br><br><input class='button' type='button' onclick=\"javascript:window.location.href='./startchat.php?lid=$lid&tid=$tid'\" value='".CHAT_BACK."'></center>");

	$echo = "<head><link rel='stylesheet' type='text/css' href='./themes/$selected_theme/$chat_default_theme'></head>";

// Upload file message
	if ( isset($_FILES['chatfile']) ) { 
		$filename = basename($_FILES['chatfile']['name']);
		$tmp_name = $_FILES['chatfile']['tmp_name'];
		$file_msg = CHAT_SEND."<b>$filename</b> ";
		
		if ( filesize($tmp_name) > $chat_max_file_size*1048576) {
			exit ("$echo<br><center><b>".$filename."</b> <font color=\"red\"><br>".CHAT_FILE_SIZE."$chat_max_file_size</font>".__BACK__);
		}
		if ( !is_uploaded_file($tmp_name) ) {
			exit ("$echo<br><center><b>".$filename."</b> <font color=\"red\"><br>".CHAT_FILE_MISSING."</font>".__BACK__);
		}
/*
	Doesn't work - this is suppose to avoid filename with illegal characters
		if ( preg_match('#[\x00-\x1F\x7F-\x9F/\\\\]#', $tmp_name) ) {
			exit ("<b>".$filename."</b> <font color=\"red\">".chat_info_file_INVALID."</font>".__BACK__);
		} else 
*/		if ( move_uploaded_file( $tmp_name, "$temporary_directory$chat_upload_dir$filename" ) ) {
			$file_msg .= "$echo<br>".CHAT_FILE_UPLOADED;
		} else {
			exit ("$echo<br><center><b>".$filename."</b> <font color=\"red\"><br>".CHAT_FILE_NOT_UPLOAD."</font>".__BACK__);
		}
		touch($temporary_directory.DB_CHAT_ONLINE); // Change the date of the users file to mention change and enable a chat update
	}

// Add message line
	if ( isset($_GET['msg']) || isset($_FILES['chatfile']) ) {
	
	// On user input
		if (file_exists($temporary_directory.DB_CHAT_DATA)) {
			$f = fopen($temporary_directory.DB_CHAT_DATA,"a+");
		} else {
			$f = fopen($temporary_directory.DB_CHAT_DATA,"w+");
		}
		if (isset($_FILES['chatfile'])) 
			$msg  = $file_msg;
		else {
			$msg  = isset($_GET['msg']) ? decode_msg($_GET['msg']) : ".";
			if ($chat_pre_processor == "yes") $msg = pre_processor($msg);
		}
		if ($msg == "") return;
		if ($chat_timestamp  == "yes") {
			$date = date($chat_time_format);
			$line = "<p><span class=\"date\">[$date]</span> "; 
		} else 
			$line = "<p>";
		$line .= "<span class=\"name\">$nick</span><span class=\"txt\">&nbsp;&nbsp;$msg</span></p>";
		fwrite($f,$line."\n");
		fclose($f);

		if (isset($_FILES['chatfile'])) { 
			echo "<br><center>$line".__BACK__;
		} else
			echo $line;
		
	} else if (isset($_GET['all'])) {
	// As a refresh
		$flag = file($temporary_directory.DB_CHAT_DATA);
		if ($chat_auto_refresh == "yes") $chat_refresh = filemtime($temporary_directory.DB_CHAT_ONLINE);
		$content = "$chat_refresh:".$_SESSION['chatstatus'].":";	// Add Update timestamp and status
		foreach ($flag as $value) $content .= $value;
		echo $content;
	}

// Pre-process the message (EXPERIMENTAL !!!)
	function pre_processor($text) {
		
		global $smiley_code, $chat_smileys, $chat_smileys_ext, $key;
		global $nick,$chat_online_file;	// For nickname change
			
	// Process commands
		if ( substr(trim(strtolower($text)),0,1) == "/" ) {

			list($cmd, $arg1, $arg2) = split('[ ]',$text);

			switch($cmd) {
				case "/join":
				case "/alias":
				case "/beep":
				case "/close":
				case "/clear":
				case "/exit":
				case "/help":
				case "/load":
				case "/nick":
						if (!$f_text = file($temporary_directory.DB_CHAT_ONLINE)) echo CHAT_CANNOT_OPENFILE;
						else {
							$file_handle = fopen($temporary_directory.DB_CHAT_ONLINE,"w");
							foreach($f_text as $line) {				// Rewrite the file but remove the current user from list
								 if ( !strstr($line,$nick) ) fputs($file_handle,$line);
							}
							fclose($file_handle);
							$oldnick = $nick;
							$_SESSION["nickname"] = $nick = $arg1;
							$status_msg = CHAT_ONLINE; $pic = "pic_online"; $classname="login";
							$data = "<a href=\"newmsg.php?nameto=$nick&mailto=$f_user\" title=\"$status_msg ($f_user) - ".CHAT_CLICK2PM."\" target=\"blank\">$nick</a>";
							if (!$file_handle = fopen($temporary_directory.DB_CHAT_ONLINE,"a+")) echo CHAT_CANNOT_OPENFILE;
							if (!fwrite($file_handle, "<div class=\"user_online $pic\"> $data</div>\n")) echo CHAT_CANNOT_WRITEFILE;
							fclose($file_handle);
						}
						return 	"<p><span class=\"main\">$oldnick</span> <span class=\"login\"> ".CHAT_NICK_CHANGED."</span> $nick</p>";

						break;
				case "/pict":
				case "/quit": 
				return "<b>$cmd</b> - Sorry, Cmd pre-processor is under development!";
			}

	// Process smileys
		} else {
			$ReplacementValues = array();
			for ($i=0; $i<count($smiley_code); $i++)
				$ReplacementValues[$i] = "<img alt='' src='".$chat_smileys."$i.$chat_smileys_ext'>";
			return str_replace($smiley_code, $ReplacementValues, $text);
		}
	}
		
	
?>