Просмотр файла katrinka.ru/chat_sign.php

Размер файла: 10.63Kb
<?php
  include 'config.php';
  include 'guestbook/mylib.php';
  session_start();
  $pflag=0;
  if (!checkagent($_SERVER['HTTP_USER_AGENT'])) { noemul(); exit; }
  list ($opname, $chatok) = check_op($_SERVER['REMOTE_ADDR']);
  if (!$chatok) { unk_op(); exit; }
  $message=$_POST['message'];
  $agent=$_SERVER['HTTP_USER_AGENT'];
  $addr=$_SERVER['REMOTE_ADDR'];
  $r=htmlspecialchars(trim($_POST['r']),ENT_QUOTES);
  if (!$r) exit;
  $to=htmlspecialchars(trim($_POST['to']),ENT_QUOTES);
  $tto=htmlspecialchars(trim($_POST['tto']),ENT_QUOTES);
  $n=htmlspecialchars(trim($_POST['n']),ENT_QUOTES);
  $tr=htmlspecialchars(trim($_POST['tr']),ENT_QUOTES);
  $sie=0;
  if ($tr) $message=tr_conv($message);
  $message=htmlspecialchars(trim($message),ENT_QUOTES);
#  if ($addr=='213.33.251.8') $sie=1;
  if (ereg("^SIE-.*",$_SERVER['HTTP_USER_AGENT'])) $sie=1;
  if (ereg("^Alcatel-.*",$_SERVER['HTTP_USER_AGENT'])) $sie=1;
  if (ereg("^MOT-.*",$_SERVER['HTTP_USER_AGENT'])) $sie=1;
  if (ereg("^SEC-.*",$_SERVER['HTTP_USER_AGENT'])) $sie=1;
  if (ereg("^Panasonic-.*",$_SERVER['HTTP_USER_AGENT'])) $sie=1;

  if ($to=='') $to=0;
  if ($r==18) $n=0;
  $uid=0;
  $access=0;
  $flag=0;
  $lang=$_POST['lang'];
  if ((trim($_COOKIE['Login'])!='')&&(trim($_COOKIE['Password'])!='')) {
	$login=htmlspecialchars(trim($_COOKIE['Login']),ENT_QUOTES);
	$pass=htmlspecialchars(trim($_COOKIE['Password']),ENT_QUOTES);
  } else {
	$login=htmlspecialchars(trim($_POST['login']),ENT_QUOTES);
	$pass=htmlspecialchars(trim($_POST['pass']),ENT_QUOTES);
#	$sie=1;
  }
    if (!($conn=pg_connect("host=$dbhost port=$dbport dbname=$dbname user=$dbuser password=$dbpass"))) exit;
	$res=pg_query($conn,"select flag from chat_rooms where id='$r';");
	if (!pg_numrows($res)) { pg_close($conn); exit; }
	$flag=pg_result($res,0,0);
	if (isset($_SESSION['id'])) {
		$uid=$_SESSION['id'];
		$res=pg_query($conn,"select moder,access,lastagent,temp_ban,lasttag from users where id='$uid';");
		if (pg_result($res,0,0)) $uid=0;
		$access=pg_result($res,0,1);
		$lastagent=pg_result($res,0,2);
		$temp_ban=pg_result($res,0,3);
		$lasttag=pg_result($res,0,4);
		$_SESSION['access']=$access;
	} else {
	$res=pg_query($conn,"select id,access,lastagent,temp_ban,lasttag from users where login='$login' and passwd='$pass' and moder=0;");
	if (pg_numrows($res)==1) {
		$uid=pg_result($res,0,0);
		$access=pg_result($res,0,1);
		$lastagent=pg_result($res,0,2);
		$temp_ban=pg_result($res,0,3);
		$lasttag=pg_result($res,0,4);
		$_SESSION['id']=$uid;
		$_SESSION['access']=$access;
	}
	}
#	if ($uid) {
#		setcookie("Login", $login, time()+864000, "/");
#		setcookie("Password", $pass, time()+864000, "/"); 
#	}

  if ($agent!=$lastagent) {
	session_unset();
	setcookie("Login", "", time()-3600, "/");
	setcookie("Password", "", time()-3600, "/");
	pg_close($conn);
	if ($_GET['lang']=='eng') header("Location: index.php?lang=eng"); 
	else header("Location: index.php");
	exit;
  }

  if ($message=="")
  {
	  header("Content-type: text/vnd.wap.wml");
?>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml_1.2.dtd">
<wml>
<card id="SignErr" title="&#x041E;&#x0448;&#x0438;&#x0431;&#x043A;&#x0430;">
<p align="center">
&#x041D;&#x0435; &#x0432;&#x0441;&#x0435; &#x043F;&#x043E;&#x043B;&#x044F; &#x0437;&#x0430;&#x043F;&#x043E;&#x043B;&#x043D;&#x0435;&#x043D;&#x044B;<br/>
<a href="/">&#x041D;&#x0430; &#x0433;&#x043B;&#x0430;&#x0432;&#x043D;&#x0443;&#x044E;</a>
</p>
</card>
</wml>
<?php
	pg_close($conn);
	exit;
    }

  if (!test_chars($message))
  {
	  header("Content-type: text/vnd.wap.wml");
?>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml_1.2.dtd">
<wml>
<card id="SignErr" title="&#x041E;&#x0448;&#x0438;&#x0431;&#x043A;&#x0430;">
<p align="center">
Invalid chars entered...
<a href="/">&#x041D;&#x0430; &#x0433;&#x043B;&#x0430;&#x0432;&#x043D;&#x0443;&#x044E;</a>
</p>
</card>
</wml>
<?php
	pg_close($conn);
	exit;
    }
  
  if (($message==".")||($message=="..")||($message=="...")||($message=="....")||($message==",")||($message==",,")||($message==",,,")||($message==":")||($message==";"))
  {
	  header("Content-type: text/vnd.wap.wml");
?>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml_1.2.dtd">
<wml>
<card id="SignErr" title="&#x041E;&#x0448;&#x0438;&#x0431;&#x043A;&#x0430;">
<p align="center">
&#x041D;&#x0435; &#x0432;&#x0441;&#x0435; &#x043F;&#x043E;&#x043B;&#x044F; &#x0437;&#x0430;&#x043F;&#x043E;&#x043B;&#x043D;&#x0435;&#x043D;&#x044B;<br/>
<a href="/">&#x041D;&#x0430; &#x0433;&#x043B;&#x0430;&#x0432;&#x043D;&#x0443;&#x044E;</a>
</p>
</card>
</wml>
<?php
	pg_close($conn);
	exit;
    }
  
  if (!$uid)
  {
	  header("Content-type: text/vnd.wap.wml");
?>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml_1.2.dtd">
<wml>
<card id="SignErr" title="&#x041E;&#x0448;&#x0438;&#x0431;&#x043A;&#x0430;">
<p align="center">
Bad login or password.
<a href="/">&#x041D;&#x0430; &#x0433;&#x043B;&#x0430;&#x0432;&#x043D;&#x0443;&#x044E;</a>
</p>
</card>
</wml>
<?php
	pg_close($conn);
	exit;
    }
	
	if ($temp_ban > time())
	{
	  header("Content-type: text/vnd.wap.wml");
?>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml_1.2.dtd">
<wml>
<card id="SignErr" title="&#x041E;&#x0448;&#x0438;&#x0431;&#x043A;&#x0430;">
<p align="center">
<?php print "Banned till ".strftime("%d %b %G %T", $temp_ban)."<br/>\n"; ?>
<a href="/">&#x041D;&#x0430; &#x0433;&#x043B;&#x0430;&#x0432;&#x043D;&#x0443;&#x044E;</a>
</p>
</card>
</wml>
<?php
	pg_close($conn);
	exit;
	}
  
	if (($r==18)&&($to==0)) {
		$res=pg_query($conn,"select id from users where login='$tto' and moder=0;");
		if (!pg_numrows($res)) {
	  header("Content-type: text/vnd.wap.wml");
?>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml_1.2.dtd">
<wml>
<card id="SignErr" title="&#x041E;&#x0448;&#x0438;&#x0431;&#x043A;&#x0430;">
<p align="center">
No such user.<br/>
<a href="/">&#x041D;&#x0430; &#x0433;&#x043B;&#x0430;&#x0432;&#x043D;&#x0443;&#x044E;</a>
</p>
</card>
</wml>
<?php
	pg_close($conn);
	exit;
		}
		$to=pg_result($res,0,0);
	}

  if (((!$access)&&($flag==1)) || ($uid==$to))
  {
	  header("Content-type: text/vnd.wap.wml");
?>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml_1.2.dtd">
<wml>
<card id="SignErr" title="&#x041E;&#x0448;&#x0438;&#x0431;&#x043A;&#x0430;">
<p align="center">
Access DENIED.<br/>
<a href="/">&#x041D;&#x0430; &#x0433;&#x043B;&#x0430;&#x0432;&#x043D;&#x0443;&#x044E;</a>
</p>
</card>
</wml>
<?php
	pg_close($conn);
	exit;
    }

	if (($access < 4)&&($uid!=1)) $message=substr($message, 0, 300);

  
	if (($n)&&($to)) {
		$res=pg_query($conn,"select login from users where id='$to';");
		if (pg_numrows($res)) {
			$nick=pg_result($res,0,0);
		}
		$to=0;
		$message = sprintf("%s, %s", $nick, $message);
	}

	$dtime=time()-300;

	if (($message[0]=='/')&&(!$to)) {
		$bp=strpos($message, ' ');
		if ($bp) $tag=substr($message,1,$bp-1);
			else $tag=substr($message,1);
		$res=pg_query($conn,"select txt from chat_tags where tag='$tag' order by id;");
		$rows=pg_numrows($res);
		if ($rows) {
			if ($lasttag < $dtime) {
				if ($rows > 1) { $tn=rand(0,$rows-1); }
					else { $tn=0; }
				$message=pg_result($res,$tn,0);
				$pflag=1;
			} else {
	  			header("Content-type: text/vnd.wap.wml"); ?>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml_1.2.dtd">
<wml>
<?php
		print "<card id=\"SignDup\" title=\"Wait\" ontimer=\"http://wap.katrinka.ru/chat.php?lang=".$lang."&amp;r=".$r."&amp;".SID."\">\n";
?>
<timer value="20"/>
<p align="center">
Tags allowed only once per 5 minutes...<br/>
Please wait... We are taking you back to the chat...<br/>
<a href="/">&#x041D;&#x0430; &#x0433;&#x043B;&#x0430;&#x0432;&#x043D;&#x0443;&#x044E;</a>
</p>
</card>
</wml>
<?php			pg_close($conn);
				exit;
			}
		}
	}
	
	if ($r==18) {
		$res=pg_query($conn,"select message from chat_tet where fr_u='$uid' and time > '$dtime' order by id desc limit 1;");
	} else {
		$res=pg_query($conn,"select message from chat where fr_u='$uid' and time > '$dtime' order by id desc limit 1;");
	}

	if (pg_numrows($res)) {
		if ($message==pg_result($res,0,0)) {
	  header("Content-type: text/vnd.wap.wml");
?>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml_1.2.dtd">
<wml>
<?php
		print "<card id=\"SignDup\" title=\"Wait\" ontimer=\"http://wap.katrinka.ru/chat.php?lang=".$lang."&amp;r=".$r."&amp;".SID."\">\n";
?>
<timer value="20"/>
<p align="center">
Please wait... We are taking you back to the chat...<br/>
<a href="/">&#x041D;&#x0430; &#x0433;&#x043B;&#x0430;&#x0432;&#x043D;&#x0443;&#x044E;</a>
</p>
</card>
</wml>
<?php
		pg_close($conn);
		exit;
		}
	}
	
	$mtime=time();
	pg_query($conn,"begin;");
	if ((!$to)||(($n)&&($to))) pg_query($conn,"update users set lastact='$mtime',posts=posts+1,lastagent='$agent' where id='$uid';");
	if ($pflag==1) pg_query($conn,"update users set lasttag='$mtime' where id='$uid';");
	if ($r==18) {
    	$res=pg_query($conn,"insert into chat_tet (fr_u,to_u,message,time,host,agent) values ('$uid','$to','$message','$mtime','$addr','$agent');");
	} else {
    	$res=pg_query($conn,"insert into chat (room,fr_u,to_u,message,time,host,agent,flag) values ('$r','$uid','$to','$message','$mtime','$addr','$agent','$pflag');");
	}
	pg_query($conn,"end;");
	if (!$res) { pg_close($conn); exit; }
    pg_close($conn);
	$link=sprintf("Location: chat.php?lang=%s&r=%s&PHPSESSID=%s", $lang, $r, session_id());
	if (!$sie) {
		header($link);
	} else {
#		setcookie("Login", $login, time()+864000, "/");
#		setcookie("Password", $pass, time()+864000, "/");
	  header("Content-type: text/vnd.wap.wml");
?>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml_1.2.dtd">
<wml>
<?php
print "<card id=\"SignOk\" title=\"&#x0421;&#x043F;&#x0430;&#x0441;&#x0438;&#x0431;&#x043E;!\" ontimer=\"http://wap.katrinka.ru/chat.php?lang=".$lang."&amp;r=".$r."&amp;".SID."\" onenterbackward=\"http://wap.katrinka.ru/\">\n";
?>
<timer value="10"/>
<p align="center">
&#x0412;&#x0430;&#x0448;&#x0430; &#x0437;&#x0430;&#x043F;&#x0438;&#x0441;&#x044C; &#x0434;&#x043E;&#x0431;&#x0430;&#x0432;&#x043B;&#x0435;&#x043D;&#x0430;<br/>
<?php
print "<a href=\"chat.php?lang=".$lang."&amp;r=".$r."\">&#x041F;&#x0440;&#x043E;&#x0441;&#x043C;&#x043E;&#x0442;&#x0440;</a>\n";
?>
</p>
</card>
</wml>
<?php
	}
?>