<?

/**
*
* @package ppkBB3cker
* @version $Id: form.php 1.000 2009-04-02 13:54:00 PPK $
* @copyright (c) 2008, 2009 PPK
* http://ppkbb3cker.rx22.ru, http://protoneutron.narod.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

require($phpbb_root_path.'config.'.$phpEx);

if($dbms!='mysql')
{
	err('Only mysql supported');
}
$c=@mysql_connect($dbhost, $dbuser, $dbpasswd);
if(!$c)
{
	err('Error connecting database: '.mysql_error());
}

$s=@mysql_select_db($dbname, $c);
if(!$s)
{
	err('Error selecting database: '.mysql_error());
}

define('CONFIG_TABLE',				$table_prefix . 'config');
define('SMILIES_TABLE',				$table_prefix . 'smilies');
define('CHAT_USERS_TABLE',			$table_prefix . 'chat_users');
define('CHAT_MESSAGES_TABLE',			$table_prefix . 'chat_messages');

my_sql_query("SET NAMES 'utf8'");
$config=array();
$sql="SELECT config_name, config_value FROM ".CONFIG_TABLE." WHERE config_name LIKE 'ppkbb\_chat%'";
$result=my_sql_query($sql);
while($row=mysql_fetch_row($result))
{
	$config[$row[0]]=$row[1];
}
mysql_free_result($result);

$dt = time();
$gmq_gpc = get_magic_quotes_gpc() ? 1 : 0;

$forum_id=intval(@$_GET['f']);
$forum_id < 0 ? $forum_id=0 : '';
$chat_key=$gmq_gpc ? @$_GET['chatkey'] : mysql_real_escape_string(@$_GET['chatkey']);
$lang='ru';
$css='"chat/stylesheet.css"';

$sql='SELECT * FROM ' .CHAT_USERS_TABLE. " WHERE chatkey='$chat_key' AND room='$forum_id' LIMIT 1";
$result=my_sql_query($sql);
$chat_user=mysql_fetch_array($result);
mysql_free_result($result);

if($chat_user)
{
	list($lang, $css, $rights)=explode('//', $chat_user['rights']);
	//0-$is_adm 1-$is_mod 2-$is_canaddchatm 3-$is_canviewchatu
	//4-$is_canviewchatm 5-$is_candelchatm 6-$is_canskipchatktime
	//7-$is_canhiddeninchat 8-$is_canviewhiddinchat 9-$is_cansendchatpm
	//10-$is_canskipdisbutton 11-$is_canusesmilies 12-$is_canusebbcodes
	$rights=explode(' ', $rights);
	require("{$phpbb_root_path}language/$lang/chat.$phpEx");

	if($rights[2] && $rights[4])
	{
		$form= '
		<form target="message" autocomplete="off" action="'.$phpbb_root_path.'chat/message_user.'.$phpEx.'?f='.$forum_id.'&chatkey='.$chat_key.'" name="post_form" method="POST" onsubmit="submitonce();'.($rights[10] ? '' : 'fulltime();').'">
			<input type="hidden" name="chat_sent" value="" />
			<span class="textform">'.$lang['CHAT_MESSAGE'].': </span><input class="textinput" type="text" name="chat_message" size="60" value="" maxlength="'.$config['ppkbb_chat_messlength'].'" />
			<input class="resbtn" type="reset" value="'.$lang['CHAT_RESET'].'" />
			<input class="subbtn" type="submit" name="submit" value="'.$lang['CHAT_SEND'].'" />
		</form>';
	}
	else
	{
		$form= '
			<tr valign="top">
				<td>
					<span class="textmess">'.$lang['CHAT_CANT_VIEWCHATM'].'</span>
				</td>
			</tr>';
	}
}
else
{
	$form='';
	require("{$phpbb_root_path}language/$lang/chat.$phpEx");
}

$css_link=stripslashes($css);

intval($config['ppkbb_chat_waittime']) ? '' : $config['ppkbb_chat_waittime']=5;

?>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="<?=$phpbb_root_path.$css_link?>.chat.css" type="text/css">
<script language="JavaScript">
<!--
function fulltime()
{
	document.post_form.submit.disabled=true;
	if(document.post_form.submit.value > 0)
	{
		document.post_form.submit.value-=1;
	}
	else if(document.post_form.submit.value == 0)
	{
		document.post_form.submit.value = '<?=$lang['CHAT_SEND']?>';
		document.post_form.submit.disabled=false;
		document.post_form.chat_message.focus();
		return;
	}
	else
	{
		document.post_form.submit.value = <?=$config['ppkbb_chat_waittime']?>;
	}

	setTimeout('fulltime()', 1000);

}

function submitonce()
{
	if(document.post_form.chat_message.value == "")
	{
		tf_text=prompt('<?=$lang['CHAT_MESSAGE']?>:');
		document.post_form.chat_message.value=tf_text;
	}
	document.post_form.chat_sent.value = document.post_form.chat_message.value;
	document.post_form.chat_message.value = "";
}
//-->
</script>

</head>
<body>
<table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
	<td align="center" nowrap="nowrap">
		<?=$form?>
	</td>
</tr>
</table>
</body>
</html>
<?
mysql_close();

exit();

//############################################################

function my_sql_query($query)
{
	global $startsqlcount, $c;
	$startsqlcount+=1;

	return mysql_query($query);
}

function err($str)
{
	echo $str;
	exit();
}
?>