<?php
include_once 'dbconnect.php';
$HOME = 'http://'.$_SERVER['HTTP_HOST'].'';
function filter($msg){
$msg = trim($msg);
$msg = strip_tags($msg);
$msg = htmlspecialchars($msg);
return $msg;
}

function time_out($time
=0){
$h=floor($time/60/60);
$i=floor($time/60)-$h*60;
$s=$time-$h*60*60-$i*60;
$h=(strlen($h)==1? '0'.$h:$h);
$i=(strlen($i)==1? '0'.$i:$i);
$s=(strlen($s)==1? '0'.$s:$s);
$out = $h.':'.$i.':'.$s;
return $out;
}
function timeout($mysqlDate) {
$seconds = strtotim($mysqlDate) - time();
$days = floor($seconds / 86400);
$seconds %= 86400;
$hours = floor($seconds / 3600);
$seconds %= 3600;
$minutes = floor($seconds / 60);
$seconds %= 60;
if ($days) {
return "$days д $hours ч";
}
if ($hours) {
return "$hours:$minutes:$seconds";
}
if ($minutes) {
return "$minutes:$seconds";
}
return $seconds;
}
function seter($str){
$strold=array("Q", "W", "E", "R","T", "Y", "U", "I", "O", "P", "A","S", "D", "F", "G", "H", "J", "K","L", "Z", "X", "C", "V", "B", "N", "M");
$strnew=array("q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s","d", "f", "g", "h", "j", "k", "l", "z", "x","c", "v", "b", "n", "m");
$str=str_replace($strold,$strnew, $str);
return $str;
}
foreach ($_GET as $links) {
if (!is_string($links) || !preg_match('#^(?:[a-z0-9_\-/]+|\.+(?!/))*$#i', $links)) {
header ('Location: impwar.tk');
exit;
} 
} 
unset($links);

$blok_text = array('javascript','alert','javascript:alert','meta');
foreach($_POST as $key=>
$set){
$fotr = seter($_POST[$key]);
if(in_array($fotr, $blok_text)){
unset($_POST[$key]);
}
}


function nick($id){
global $pdo;
$user = $pdo->query("SELECT * FROM users
WHERE id='".$id."' LIMIT 1")->fetch();
$g = $user['gruppa'];
if($g=='dolg'){$gruppa='<img src="/img/ico/dolg.png" height="12" width="12"/>';}
elseif($g=='naemniki'){$gruppa='<img src="../img/ico/naemniki.png" height="12" width="12"/>';}
elseif($g=='svoboda'){$gruppa='<img src="/img/ico/svoboda.png" height="12" width="12"/>';}
elseif($g=='renegat'){$gruppa='<img src="../img/ico/renegat.png" height="12" width="12"/>';}
elseif($g=='chistoenebo'){$gruppa='<img src="/img/ico/chistoenebo.png" height="12" width="12"/>';}
return (empty($user)?'[Удален]':'<a href="/profile.php?id='.$user['id'].'">'.$gruppa.' <b>'.$user['nick'].'</b></a>');
}

function vremja($time = NULL) {
if(!$time) $time = time();
$data = date('j.n.y', $time);
if($data == date('j.n.y')) $res = ' '. date('G:i', $time);
elseif($data == date('j.n.y', time() - 86400)) $res = 'Вчера в '. date('G:i', $time);else {
$m = array('0',
'Янв', 'Фев', 
'Мар', 'Апр', 'Май', 
'Июн', 'Июл', 'Авг', 
'Сен', 'Окт', 'Ноя', 
'Дек');
$res = date('j '. $m[date('n', $time)] .' Y в G:i', $time);
}
return $res;
}


function page($k_page=1) {
$page = 1;
$page = filter($page);
$k_page = filter($k_page);
if(isset($_GET['selection'])) {
if ($_GET['selection']=='top')
$page = filter(intval($k_page));
elseif(is_numeric($_GET['selection'])) 
$page = filter(intval($_GET['selection']));
}
if ($page<1)$page=1;
if ($page>$k_page)$page=$k_page;
return $page;
}

function k_page($k_post = 0,$k_p_str = 10) {
if ($k_post != 0) {
$v_pages = ceil($k_post/$k_p_str);
return $v_pages;
}
else return 1;
}

function str($link='?',$k_page=1,$page=1){
if ($page<1)$page=1;
$page = filter($page);
$k_page = filter($k_page);
echo '<div class="links">';

if ($page>1)echo '<a href="'.$link.'selection='.($page-1).'">&lt;&lt; Назад</a> ';
else echo "&lt;&lt; Назад ";
echo " | ";
if ($page<$k_page)echo ' <a href="'.$link.'selection='.($page+1).'" >Вперед &gt;&gt;</a>';
else echo " Вперед &gt;&gt;";
echo '<br />';
if ($page != 1)
echo '<a href="'.$link.'selection=1" >1</a>';
else echo '<b>1</b>';
for ($ot=-3; $ot<=3; $ot++){
if ($page+$ot>1 && $page+$ot<$k_page){
if ($ot==-3 && $page+$ot>2)echo " ..";
if ($ot!=0)echo '|<a href="'.$link.'selection='.($page+$ot).'" >'.($page+$ot).'</a>';
else echo '|<b>'.($page+$ot).'</b>';
if ($ot==3 && $page+$ot<$k_page-1)echo "|..";}}
if ($page!=$k_page)echo '|<a href="'.$link.'selection=top" >'.$k_page.'</a>';
elseif ($k_page>1)echo '|<b>'.$k_page.'</b>';
echo '</div>';
}
function smile($msg){
global $pdo;
$msg = trim($msg);
$s = $pdo->query("SELECT * FROM `smile` ORDER BY `id` DESC");
while($smile = $s->fetch()){
$msg = str_replace($smile['name'],' <img src="'.$HOME.'/smile/'.$smile['icon'].'" alt="'.$smile['name'].'"/> ',$msg);
}
$b = $pdo->query("SELECT * FROM `bbcode` ORDER BY `id` DESC");
while($bbcode = $b->fetch()){
$msg = str_replace($bbcode['code'],' '.$bbcode['text'].'  ',$msg);
}
return $msg;
}
?>