<h2></h2>
<?php
$count=10;//   $count /
//
if (isset($_GET['lim'])) $lim=preg_replace("/[^0-9]/i","",$_GET['lim']);else $lim=0;
if (isset($_GET['ord'])) $ord=preg_replace("/[^a-z]/i","",$_GET['ord']);else $ord='lmtime';

	if (isset($_GET['sform']))
		switch ($_GET['sform']) {
			case 'nmsg':
				if (isset($_GET['tid'])) {
					$tid=preg_replace("/[^0-9]/i","",$_GET['tid']);
					if (mysql_num_rows(mysql_query("select * from topics where (id=$tid and lvl<=$_SESSION[lvl])")))
						echo "<form action=index.php><table border=1 width=200>
						<input type=hidden name=page value=posting>
						<input type=hidden name=apl value=nmsg>
						<input type=hidden name=tid value=$tid>
						<input type=hidden name=lim value=$lim>
						<tr><td align=center><b>  </b></td></tr>
						<tr><td><textarea name=nmsg rows=5 cols=30></textarea></td></tr>
						<tr><td align=center><input type=submit value=ok></td></tr>
						<tr><td>    ,  , ,  0-9,  : / @ _ ! = ) ~ { } <br>
						   :</font><li>b{...}b ~ < b >...< / b> </li><li>u{...}u ~ < u >...< / u> </li>
						<li>i{...}i ~ < i >...< / i> </li><li>s{...}s ~ < s >...< / s> </li><li>n{}    </li></td></tr>
						</table></form>";
					else echo "<h3 class=bold>AntiHack system:</h3>         !<br>";
				} else echo "<h3 class=bold>AntiHack system:</h3>     !<br>";
				break;
			case 'emsg':
				if ($_SESSION['login']!='Guest') {
					if (isset($_GET['mid'])) {
						$mid=preg_replace("/[^0-9]/i","",$_GET['mid']);
						if (isset($_GET['tid'])) $tid=preg_replace("/[^0-9]/i","",$_GET['tid']);
						$line=mysql_fetch_array(mysql_query("select * from posts where id=$mid"));
						$arr_tpc=mysql_fetch_array(mysql_query("select * from topics where id=$line[tid]"));
						if ($_SESSION['lvl']>=$arr_tpc['lvl'] && ($_SESSION['lvl']>=2 || $line['from']==$_SESSION['login'])) {
							echo "<form action=index.php method=get>
							<table border=1 width=200>
							<input type=hidden name=page value=posting>
							<input type=hidden name=apl value=emsg>
							<input type=hidden name=tid value=$tid>
							<input type=hidden name=lim value=$lim>
							<input type=hidden name=mid value=$mid>
							<tr><td align=center><b> </b></td></tr>
							<tr><td><textarea name=nmsg rows=5 cols=30>$line[msg]</textarea></td></tr>
							<tr><td align=center><input type=submit value=ok></td></tr></table></form>";
						} else "   !<br>";
					} else echo "<h3 class=bold>AntiHack system:</h3>         !<br>";
				} else echo "<h3 class=bold>AntiHack system:</h3>       !<br>";
				break;
			case 'ntpc':
				if ($_SESSION['lvl']>=1)
					echo "<form action=index.php><table border=1 width=200>
					<input type=hidden name=page value=posting>
					<input type=hidden name=apl value=ntpc>
					<tr><td align=center><b>  </b></td></tr>
					<tr><td align=center><input type=text name=nname size=20><input type=submit value=ok></td></tr>
					<tr><td align=center> :
						<select name=nlvl>
							<option value=0 selected> (0)
							<option value=1>- (1)
							<option value=2> (2)
							<option value=3> (3)
							<option value=4> (4)
						</select>
					</td></tr>
					<tr><td align=center> <input type=checkbox name=hide></td></tr>
					<tr><td align=justify>      ,  ,  0-9,  : / @ _ -</td></tr>
					</table></form>";
				else echo "<h3 class=bold>AntiHack system:</h3>      !<br>";
				break;
			case 'etpc':
				if ($_SESSION['lvl']>=2) {
					if (isset($_GET['etid'])) $etid=preg_replace("/[^0-9]/i","",$_GET['etid']);else $etid=0;
					if ($line=mysql_fetch_array(mysql_query("select * from topics where id=$etid"))) {
						if ($_SESSION['lvl']>=$line['lvl']) {
							echo "<form action=index.php><table border=1 width=200>
							<input type=hidden name=page value=posting>
							<input type=hidden name=apl value=etpc>
							<input type=hidden name=etid value=$etid>
							<tr><td align=center><b> </b></td></tr>
							<tr><td align=center><input type=text name=nname size=20 value='$line[name]'><input type=submit value=ok></td></tr>
							<tr><td align=center> :
							<select name=nlvl>
								<option value=0";if ($line['lvl']==0) echo " selected";echo "> (0)
								<option value=1";if ($line['lvl']==1) echo " selected";echo ">- (1)
								<option value=2";if ($line['lvl']==2) echo " selected";echo "> (2)
								<option value=3";if ($line['lvl']==3) echo " selected";echo "> (3)
								<option value=4";if ($line['lvl']==4) echo " selected";echo "> (4)
							</select>
							</td></tr>
							<tr><td align=center> <input type=checkbox name=hide";if ($line['hide']) echo " checked";echo "></td></tr>
							<tr><td align=justify>      ,  ,  0-9,  : / @ _ -</td></tr>
							</table></form>";
						} else echo "<h3 class=bold>AntiHack system:</h3>    (       )<br>";
					} else echo "<h3 class=bold>AntiHack system:</h3>       !<br>";
				} else echo "<h3 class=bold>AntiHack system:</h3>        !<br>";
				break;
		}				

//       (   )	
if (isset($_GET['tid'])) {	
	//    ()	
	$tid=preg_replace("/[^0-9]/i","",$_GET['tid']);
	if ($arr_tpc=mysql_fetch_array(mysql_query("select * from topics where id=$tid"),MYSQL_ASSOC))
		if (!$arr_tpc['hide'] || ($_SESSION['lvl']>=$arr_tpc['lvl'])) {
			if ($mcount=mysql_num_rows(mysql_query("select * from posts where tid=$tid"))) {
				$sql=mysql_query("select * from posts where tid=$tid order by -`time` limit $lim,$count");
				echo "<i>$arr_tpc[name]</i>&nbsp;&nbsp;&nbsp;  : $mcount <br>";
				if ($mcount>$count) {
					echo ": ";
					for ($i=0;$i<$mcount;$i+=10) echo "<a href=index.php?page=msg&tid=$tid&lim=$i>",$i/10+1,"</a> ";
				}
				echo "<table border=1 width=100%><tr align=center><td width=2%>ID</td><td width=60%></td><td width=20%></td><td colspan=2 width=10%></td></tr>";
				for ($i=0;$i<($mcount<10?$mcount:10);$i++) {
					if ($line=mysql_fetch_array($sql,MYSQL_ASSOC)) {
						$msg=preg_replace(array("/b{/","/}b/","/u{/","/}u/","/i{/","/}i/","/s{/","/}s/","/n{}/"),array("<b>","</b>","<u>","</u>","<i>","</i>","<s>","</s>","<br>"),$line['msg']);
						echo "<tr><td>$line[id]</td><td>$msg</td><td>$line[from] - ",date("d.m H:i",$line['time']),"</td>";
						if (($_SESSION['lvl']>=$arr_tpc['lvl'] && $_SESSION['lvl']>=2) || ($_SESSION['lvl']>0 && $arr_tpc['lvl']<4 && $line['from']==$_SESSION['login'])) echo "
							<td width=15 height=15><a href='index.php?page=msg&sform=emsg&mid=$line[id]&tid=$tid&lim=$lim' title='edit message #$line[id]'><img src='img/edit.gif' width=15 height=15 alt=e></a></td>
							<td width=15 height=15><a href='index.php?page=posting&apl=dmsg&mid=$line[id]&tid=$tid&lim=$lim' title='delete message #$line[id]'><img src='img/delete.gif' width=15 height=15 alt=d></a></td>";
						else echo "
							<td width=15 height=15><img src='img/editdenied.gif' width=15 height=15 alt=e></td>
							<td width=15 height=15><img src='img/deletedenied.gif' width=15 height=15 alt=d></td>";
						echo "</tr>";
					}
				}
				echo "</table><br>";
			} else echo " !<br>";
			if ($_SESSION['lvl']>=$arr_tpc['lvl']) echo "<input type=button value=' ' onClick=location.href='index.php?page=msg&sform=nmsg&tid=$tid&lim=$lim'>";
		} else echo "<h3 class=bold>AntiHack system:</h3>     <br>";
	else echo "<h3 class=bold>:</h3>      <br>";
	echo "<input type=button value='  ' onClick=location.href='index.php?page=msg'>";
} 
else {
	//   
	$fcount=mysql_num_rows(mysql_query("select * from topics where (hide=0 or (hide=1 and lvl<=$_SESSION[lvl]))"));
	echo " : ",mysql_num_rows(mysql_query("select * from topics")),", : ",$fcount,"<br>";
	if ($fcount) {
		$sql=mysql_query("select * from topics where (hide=0 or (hide=1 and lvl<=$_SESSION[lvl])) order by -`$ord` limit $lim,$count");
		if ($fcount>$count) {
			echo ": ";
			for ($i=0;$i<$fcount;$i+=10) echo "<a href=index.php?page=msg&lim=$i&ord=$ord>",$i/10+1,"</a> ";
		}
		echo "<form action=index.php>
				<input type=hidden name=page value=msg>
				<select name=ord>
					<option value=id";if ($ord=='id') echo " selected";echo ">
					<option value=posts";if ($ord=='posts') echo " selected";echo "> 
					<option value=crtime";if ($ord=='crtime') echo " selected";echo "> 
					<option value=lmtime";if ($ord=='lmtime') echo " selected";echo "> . 
					<option value=lvl";if ($ord=='lvl') echo " selected";echo ">
				</select><input type=submit value=></form>";
		echo "<table border=1 width=100% align=center><tr align=center><td width=5%>ID</td><td width=45%></td><td width=5%></td><td width=5%></td><td width=20%></td><td width=20%>. .</td><td colspan=2 width=10%></td></tr>";
		for ($i=0;$i<($fcount<10?$fcount:10);$i++)
			if ($line=mysql_fetch_array($sql,MYSQL_ASSOC)) {
				echo "<tr align=center>
				<td width=5%>$line[id]</td>
				<td width=40%><a href='index.php?page=msg&tid=$line[id]'>$line[name]";if ($line['hide']==1) echo " [HIDDEN] ";echo "</a></td>
				<td width=5%>$line[posts]</td>
				<td width=5%>$line[lvl]</td>
				<td width=20%>",$line['crfrom'],"<br>",date("d.m H:i",$line['crtime']),"</td>
				<td width=20%>",$line['lmfrom'],"<br>";if ($line['lmtime']!=0) echo date("d.m H:i",$line['lmtime']);echo "</td>";
				if ($_SESSION['lvl']>=2 && $_SESSION['lvl']>=$line['lvl']) echo "
					<td width=15 height=15><a href='index.php?page=msg&sform=etpc&etid=$line[id]' title='edit topic #$line[id]'><img src='img/edit.gif' width=15 height=15 alt=e></a></td>
					<td width=15 height=15><a href='index.php?page=posting&apl=dtpc&etid=$line[id]' title='delete topic #$line[id]'><img src='img/delete.gif' width=15 height=15 alt=d></a></td>";
				else echo "
					<td width=15 height=15><img src='img/editdenied.gif' width=15 height=15 alt=e></td>
					<td width=15 height=15><img src='img/deletedenied.gif' width=15 height=15 alt=d></td>";				
				echo "</tr>";
			}	
		echo "</table>";
	}  else echo "  !<br>";
	if ($_SESSION['lvl']>=1) echo "<input type=button value=' ' onClick=location.href='index.php?page=msg&sform=ntpc'>";
}
?>