<?php

// Если запросили восстановление пароля не подтвердили, то очищаем нужные нам колонки 
if (isset($b)) {
$res = DB::$dbs->queryFetch("SELECT * FROM blogger WHERE id = ? LIMIT 1",[$b['id']]);
if ($res['rec_time'] < time()) {
DB::$dbs->query("UPDATE blogger SET hash_code = ?, rec_time = ? WHERE id = ?",['','',$res['id']]);
}
}
// Обновляем данные мастера
if (isset ($_COOKIE['id'], $_COOKIE['pass'])) {
$query = DB :: $dbs -> query ("SELECT * FROM `blogger` WHERE (`id`=?) AND (`pass`=?)", array (intval ($_COOKIE['id']), addslashes ($_COOKIE['pass'])));
if ($query -> rowCount () != 0) {
$b = $query -> fetch ();

DB :: $dbs -> query("UPDATE `blogger` SET `last`='" . time () . "',`ip`=?,`soft`=? WHERE (`id`='" . $b['id'] . "')", array ($_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT']));
if ((time () - $b['last']) < 3600) {
DB::$dbs ->query("UPDATE `blogger` SET `total_onl`=`total_onl` + ? WHERE `id`= ?", array ((time() - $b['last']),$b['id']));
}
$tmvisit = date('d');
if ($b['vis'] != $tmvisit) DB::$dbs -> query("UPDATE blogger SET vis= ? WHERE id = ?", [$tmvisit,$b['id']]);
}
}
// Запись гостей
if (empty($_COOKIE['id']) && empty($_SESSION['pass'])) {
if (DB::$dbs->querySingle("SELECT COUNT(ip) FROM guests WHERE id_session = ?", [session_id()])==0) {
DB::$dbs->query("INSERT INTO guests SET ip = ?, ua = ?, id_session = ?, date = ?",[htmlentities($_SERVER['REMOTE_ADDR']),htmlentities($_SERVER['HTTP_USER_AGENT']),session_id(),time()]);
} else {
DB::$dbs->query("UPDATE guests SET ip = ?, ua = ?, date = ? WHERE id_session = ?",[htmlentities($_SERVER['REMOTE_ADDR']),htmlentities($_SERVER['HTTP_USER_AGENT']),time(),session_id()]);
}
DB::$dbs->query("DELETE FROM guests WHERE date < ?",[time()-1200]);
}
// Обратное время
function minus($all, $minus)
{
	$all -= $minus;
	if (0 > $all) $all = 0;
	return $all;
}
// Считаем время бана
function kikt($t)
{
	$t2 = minus($t, time());
	if (empty($t2)) $t3 = 'Банн уже закончился';
	elseif ($t2 > (60 * 60 * 24 * 30)) $t3 = round($t2 / (60 * 60 * 24 * 30)) . ' месяцев';
	elseif ($t2 > (60 * 60 * 24)) $t3 = round($t2 / (60 * 60 * 24)) . ' дней';
	elseif ($t2 > (60 * 60)) $t3 = round($t2 / (60 * 60)) . ' часов';
	elseif ($t2 > 60) $t3 = round($t2 / 60) . ' минут';
	else $t3 = $t2 . ' секунд';
	return $t3;
}

function getbrowser_( $us_browser = null ) {
if ($us_browser!= null ) {
switch ($us_browser) {
// по умолчанию
default:
$var = "<img src='/assets/browser/mobile.png'>";
break;
//Выбираем браузер
case 'Netscape':
$var = "<img src='/assets/browser/netscape.png'>";
break;
case 'Opera Mini':
$var = "<img src='/assets/browser/Opera_Mini.png'>";
break;
		case 'Chrome': $var = "<img src='/assets/browser/Chrome.png'>"; break;
		case 'safari': $var = "<img src='/assets/browser/safari.png'>"; break;
		case 'MSIE 6.0': $var = "<img src='/assets/browser/Explorer.png'>"; break;
		case 'MSIE 7.0': $var = "<img src='/assets/browser/Explorer.png'>"; break;
		case 'MSIE 8.0': $var = "<img src='/assets/browser/Explorer.png'>"; break;
		case 'Firefox': $var = "<img src='/assets/browser/Firefox.png'>"; break;
		case 'Opera': $var = "<img src='/assets/browser/Opera_Mini.png'>"; break;
		case 'Android': $var = "<img src='/assets/browser/android.png'>"; break;
		case 'iPhone': $var = "<img src='/assets/browser/iPhone.png'>"; break;
		case 'Windows': $var = "<img src='/assets/browser/windows.png'>"; break;
		case 'WindowsPhone': $var = "<img src='/assets/browser/WindowsPhone.png'>"; break;
		case 'iPod': $var = "<img src='/assets/browser/iPod.png'>"; break;
		case 'BlackBerry': $var = "<img src='/assets/browser/BlackBerry.png'>"; break;
		case 'Ucweb': $var = "<img src='/assets/browser/ucweb.ico'>"; break;
}
return $var;
}
}
// Пагинация
function page($k_page=1){
$page=1;
if (isset($_GET['page'])){
if ($_GET['page']=='end') $page = intval($k_page);elseif(is_numeric($_GET['page'])) $page = intval($_GET['page']);
}
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;
echo '</br><center>';
$str_div = 'btn btn-info';
if ($page!=1)
echo '<a href=\''.$link.'page=1\' title="Страница в 1"><span class="'.$str_div.'">1</span></a>';
else echo '<span class="'.$str_div.'">[<b>1</b>]</span>';
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.'page='.($page+$ot).'" title="Страница в'.($page+$ot).'"><span class="'.$str_div.'">'.($page+$ot).'</span></a> ';
else
echo ' <span class="'.$str_div.'" style="color:#fff"> [<b>'.($page+$ot).'</b>] </span>';
if ($ot==3 && $page+$ot<$k_page-1)echo '<span class="'.$str_div.'"> ...</span>';
}
}
if ($page!=$k_page) echo ' <a href="'.$link.'page='.$k_page.'" title="Страница в'.$k_page.'"><span class="'.$str_div.'">'.$k_page.'</span></a>';
elseif ($k_page>1)echo '<span class="'.$str_div.'"> [<b>'.$k_page.'</b>]</span>';
echo '</center></br>';
}

// Основное время
function vr($times=NULL){
$time = time();
if (($time-$times)<=60) {
$timesp = slv((($time-$times)),'секунду','секунды','секунд').' назад';
return $timesp;
} else if(($time-$times)<=3600){$timesp = slv((($time-$times)/60),'минуту','минуты','минут').' назад';
return $timesp;
} else {
$today = date("j M Y", $time);
$today = date("j M Y", $time);
$yesterday = date("j M Y", strtotime("-1 day"));
$timesp=date("j M Y  в H:i", $times);
$timesp = str_replace($today, 'Сегодня', $timesp);
$timesp = str_replace($yesterday, 'Вчера', $timesp);
$timesp = strtr($timesp, array ("Jan" => "Янв","Feb" => "Фев","Mar" => "Марта","May" => "Мая","Apr" => "Апр","Jun" => "Июня","Jul" => "Июля","Aug" => "Авг","Sep" => "Сент","Oct" => "Окт","Nov" => "Ноября","Dec" => "Дек",));
return $timesp;
}
}
// Журнал
function jurnal($us,$text,$act) {
global $dbs;
/* 1 - Все, 2 - Форум, 3 - Блоги, 4 - Рейтинг, 5 - Прочее, - Биллинг*/
DB::$dbs->query("INSERT INTO jurnal SET user_id = ?, text = ?, type = ?, act = ?, time = ?", [$us,$text,1,$act,time()]);
}

// Ссылки
function url_replace($url) {
global $_SERVER;
if (!isset($url[4])) {
$target = (strpos($url[1], $_SERVER['HTTP_HOST']) === false) ? ' target="_blank" rel="nofollow"' : '';
$title = (mb_strlen($url[3]) > 80) ? mb_substr($url[3], 0, 70, 'UTF-8').'...' : $url[3];
return '<a href="/away.html?url='.str_rot13(base64_encode($url[1])).'"'.$target.'>'.check(rawurldecode(html_entity_decode($title, ENT_QUOTES, 'utf-8'))).'</a>';
} else {
$target = (strpos($url[4], $_SERVER['HTTP_HOST']) === false) ? ' target="_blank" rel="nofollow"' : '';
$title = (mb_strlen($url[4]) > 80) ? mb_substr($url[4], 0, 70, 'UTF-8').'...' : $url[4];
return '<a href="/away.html?url='.str_rot13(base64_encode($url[4])).'"'.$target.'>'.check(rawurldecode(html_entity_decode($title, ENT_QUOTES, 'utf-8'))).'</a>';
}
}

// Генерация пароля 
function pass($var){
return substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, $var);
}
// Лента 
function lenta($var) {
global $dbs;
DB::$dbs->query("INSERT INTO lenta SET text = ?, time = ?", [$var,time()]);
}

?>