<?php
#функции

 function bar_output($str)
    {
        $str = htmlentities(trim($str), ENT_QUOTES, 'UTF-8');
        $str = nl2br($str);
        $str = strtr($str, array(
                                chr(0) => '',
                                chr(1) => '',
                                chr(2) => '',
                                chr(3) => '',
                                chr(4) => '',
                                chr(5) => '',
                                chr(6) => '',
                                chr(7) => '',
                                chr(8) => '',
                                chr(9) => '',
                                chr(10) => '',
                                chr(11) => '',
                                chr(12) => '',
                                chr(13) => '',
                                chr(14) => '',
                                chr(15) => '',
                                chr(16) => '',
                                chr(17) => '',
                                chr(18) => '',
                                chr(19) => '',
                                chr(20) => '',
                                chr(21) => '',
                                chr(22) => '',
                                chr(23) => '',
                                chr(24) => '',
                                chr(25) => '',
                                chr(26) => '',
                                chr(27) => '',
                                chr(28) => '',
                                chr(29) => '',
                                chr(30) => '',
                                chr(31) => ''
                           ));
        $str = str_replace("'", "&#39;", $str);
        $str = str_replace('\\', "&#92;", $str);
        $str = str_replace("|", "I", $str);
        $str = str_replace("||", "I", $str);
        $str = str_replace("/\\\$/", "&#36;", $str);
        $str = mysql_real_escape_string($str);
        return $str;
    }

function bar_date($time=NULL)
{

if ($time==NULL)$time=time();
$timep="".date("j M Y в H:i", $time)."";
$time_p[0]=date("j n Y", $time);
$time_p[1]=date("H:i", $time);
if ($time_p[0]==date("j n Y"))$timep=date("H:i:s", $time);

if ($time_p[0]==date("j n Y"))$timep=date("H:i:s", $time);
if ($time_p[0]==date("j n Y", time()-60*60*24))$timep="Вчера в $time_p[1]";
$timep=str_replace("Jan","Янв",$timep);
$timep=str_replace("Feb","Фев",$timep);
$timep=str_replace("Mar","Марта",$timep);
$timep=str_replace("May","Мая",$timep);
$timep=str_replace("Apr","Апр",$timep);
$timep=str_replace("Jun","Июня",$timep);
$timep=str_replace("Jul","Июля",$timep);
$timep=str_replace("Aug","Авг",$timep);
$timep=str_replace("Sep","Сент",$timep);
$timep=str_replace("Oct","Окт",$timep);
$timep=str_replace("Nov","Ноября",$timep);
$timep=str_replace("Dec","Дек",$timep);
return $timep;
}

function bar_pagenav($base_url, $start, $max_value, $num_per_page) {
    ////////////////////////////////////////////////////////////
    // Функция постраничной навигации                         //
    ////////////////////////////////////////////////////////////
    // За основу взята аналогичная функция от форума SMF2.0   //
    ////////////////////////////////////////////////////////////
    $pgcont = 4;
    $pgcont = (int)($pgcont - ($pgcont % 2)) / 2;
    if ($start >= $max_value)
        $start = max(0, (int)$max_value - (((int)$max_value % (int)$num_per_page) == 0 ? $num_per_page : ((int)$max_value % (int)$num_per_page)));
    else
        $start = max(0, (int)$start - ((int)$start % (int)$num_per_page));
    $base_link = '<a class="navpg" href="' . strtr($base_url, array ('%' => '%%')) . 'start=%d' . '">%s</a> ';
    $pageindex = $start == 0 ? '' : sprintf($base_link, $start - $num_per_page, '&lt;-');
    if ($start > $num_per_page * $pgcont)
        $pageindex .= sprintf($base_link, 0, '1');
    if ($start > $num_per_page * ($pgcont + 1))
        $pageindex .= '<span style="font-weight: bold;"> ... </span>';
    for ($nCont = $pgcont; $nCont >= 1; $nCont--)
        if ($start >= $num_per_page * $nCont) {
            $tmpStart = $start - $num_per_page * $nCont;
            $pageindex .= sprintf($base_link, $tmpStart, $tmpStart / $num_per_page + 1);
        }
    $pageindex .= '[<b>' . ($start / $num_per_page + 1) . '</b>] ';
    $tmpMaxPages = (int)(($max_value - 1) / $num_per_page) * $num_per_page;
    for ($nCont = 1; $nCont <= $pgcont; $nCont++)
        if ($start + $num_per_page * $nCont <= $tmpMaxPages) {
            $tmpStart = $start + $num_per_page * $nCont;
            $pageindex .= sprintf($base_link, $tmpStart, $tmpStart / $num_per_page + 1);
        }
    if ($start + $num_per_page * ($pgcont + 1) < $tmpMaxPages)
        $pageindex .= '<span style="font-weight: bold;"> ... </span>';
    if ($start + $num_per_page * $pgcont < $tmpMaxPages)
        $pageindex .= sprintf($base_link, $tmpMaxPages, $tmpMaxPages / $num_per_page + 1);
    if ($start + $num_per_page < $max_value) {
        $display_page = ($start + $num_per_page) > $max_value ? $max_value : ($start + $num_per_page);
        $pageindex .= sprintf($base_link, $display_page, '-&gt;');
    }
    return $pageindex;
}

function bar_timer($timediff){

$oneMinute=60;
$oneHour=60*60;
$oneDay=60*60*24;
$dayfield=floor($timediff/$oneDay);
$hourfield=floor(($timediff-$dayfield*$oneDay)/$oneHour);
$minutefield=floor(($timediff-$dayfield*$oneDay-$hourfield*$oneHour)/$oneMinute);
$secondfield=floor(($timediff-$dayfield*$oneDay-$hourfield*$oneHour-$minutefield*$oneMinute));
$time_1="$minutefield минут $secondfield секунд";
return $time_1;
}
function bar_msg($div,$txt){
echo "<div class='".$div."'>".$txt."</div>";
}
function ret($div,$txt,$link){
echo "<div class='".$div."'><img src='i/ret.png' /><a href='".$link."'>".$txt."</a></div>";
}
function nap($alc){
if($alc<100)
return "<img src='i/1.jpg' />";
elseif($alc>99 && $alc<200)
return "<img src='i/1.jpg' /><img src='i/2.jpg' />";
elseif($alc>199 && $alc<400)
return "<img src='i/1.jpg' /><img src='i/2.jpg' /><img src='i/3.jpg' />";
elseif($alc>399 && $alc<600)
return "<img src='i/1.jpg' /><img src='i/2.jpg' /><img src='i/3.jpg' /><img src='i/4.jpg' />";
elseif($alc>599 && $alc<800)
return "<img src='i/1.jpg' /><img src='i/2.jpg' /><img src='i/3.jpg' /><img src='i/4.jpg' /><img src='i/5.jpg' />";
elseif($alc>799)
return "<img src='i/1.jpg' /><img src='i/2.jpg' /><img src='i/3.jpg' /><img src='i/4.jpg' /><img src='i/5.jpg' /><img src='i/6.jpg' />";
}

function bar_in($rec){

$array=explode(';',$rec);

$in=mysql_result(mysql_query("SELECT `name` FROM `bar_ingridient` WHERE `id`='".$array[0]."'"),0);

if(isset($array[1]))
$in=$in.", ".mysql_result(mysql_query("SELECT `name` FROM `bar_ingridient` WHERE `id`='".$array[1]."'"),0);

if(isset($array[2]))
$in=$in.", ".mysql_result(mysql_query("SELECT `name` FROM `bar_ingridient` WHERE `id`='".$array[2]."'"),0);

return $in;
}
function get_file_type_bar($file,$arrays){
	$arr=explode(';',$arrays);
	$count=count($arr);
	$isset=0;
		for($i=0;$i<$count;$i++){
		if($arr[$i]==$file && $arr[$i]!='')
		$isset++;
		}
	if($isset!=0)
	return true;
	else
	return false;
	}
$user_id=$user['id'];
$user_login=$user['login'];

if(mysql_result(mysql_query("SELECT COUNT(*) FROM `bar_user` WHERE `id_u`='".$user_id."'"),0)==0){
mysql_query("INSERT INTO `bar_user`(`id_u`,`login`,`reg`) VALUES('".$user_id."','".$user_login."','".time()."')");
mysql_query("INSERT INTO `bar_tmp`(`id_u`) VALUES('".$user_id."')");
for($i=0;$i<3;$i++){
$name=array('Александр','Алексей','Андрей','Анатолий','Артур','Артем','Борис','Богдан','Вадим','Валерий','Виталий','Дмитрий','Егор','Ефим','Иван','Игорь','Кирилл','Максим','Николай','Филипп','Эдуард','Александра','Алина','Анжела','Анна','Вероника','Виктория','Екатерина','Елизавета','Елена','Ирина','Ксения','Кристина','Лилия','Мария','Марина','Оксана','Татьяна','Юлия','Яна');
$names=array_rand($name);
$name=$name[$names];
$recept=mysql_fetch_array(mysql_query("SELECT `id` FROM `bar_recept` WHERE `level`<='".$bar_user['level']."' ORDER BY RAND() DESC LIMIT 1"));
mysql_query("INSERT INTO `bar_zakaz`(`id_u`,`id_r`,`name`,`bot`) VALUES('".$user_id."','".$recept['id']."','".$name."','0')");
}mysql_query("UPDATE `bar_user` SET `time_to_next`='".(time()+60*30)."' WHERE `id_u`='".$user_id."'");
header("Location:index.php");
exit;
}

$bar_user=mysql_fetch_array(mysql_query("SELECT * FROM `bar_user` WHERE `id_u`='".$user_id."'"));


if($bar_user['setting_diz']==0){
$diz=array('main'=>'bar_main','title2'=>'bar_title2','title'=>'bar_title','msg'=>'bar_msg','ret'=>'bar_ret','post_1'=>'bar_post_1','post_2'=>'bar_post_2','err'=>'bar_err');
?>
<style type="text/css">
.bar_title {text-align:center; background-image:url('i/h1.gif'); background-repeat:repeat;}
.bar_main {background:#00467d; color:#ffffff;}
.bar_main a{color:#fff;}
.bar_title2 {background: #0090C4;padding: 4px 2px;border: 1px solid #fff;}
.bar_post_1 {background: #0090C4;padding: 4px 2px;border: 1px solid #fff;}
.bar_post_2 {padding: 4px 2px;border: 1px solid #fff;}
.bar_msg {background: #0090C4;color:#fff;padding: 4px 2px;border: 1px solid #fff;}
.bar_ret {background: #0090C4;padding: 4px 2px;border-bottom: 1px solid #fff;}
.bar_ret a{color:#fff;}
</style>
<?
}
else
{
$diz=array('main'=>'post_0','title'=>'post_1','title2'=>'post_1','ret'=>'main','post_1'=>'post_0','post_2'=>'post_1','err'=>'err');
}
$page = isset ($_REQUEST['page']) && $_REQUEST['page'] > 0 ? intval($_REQUEST['page']) : 1;
$start = isset ($_GET['start']) ? abs(intval($_GET['start'])) : 0;

$start = isset ($_REQUEST['page']) ? $page * $kmess - $kmess : $start;


if($bar_user['time_to_next']<time() && mysql_result(mysql_query("SELECT COUNT(*) FROM `bar_zakaz` WHERE `id_u`='".$user_id."'"),0)<10){
$str=time()-$bar_user['time_to_next'];
if($str>(60*30) && $str<(60*60))
$str=3;
elseif($str>(60*50))
$str=4;
else
$str=2;
for($i=0;$i<$str;$i++){
$name=array('Александр','Алексей','Андрей','Анатолий','Артур','Артем','Борис','Богдан','Вадим','Валерий','Виталий','Дмитрий','Егор','Ефим','Иван','Игорь','Кирилл','Максим','Николай','Филипп','Эдуард','Александра','Алина','Анжела','Анна','Вероника','Виктория','Екатерина','Елизавета','Елена','Ирина','Ксения','Кристина','Лилия','Мария','Марина','Оксана','Татьяна','Юлия','Яна');
$names=array_rand($name);
$name=$name[$names];
$recept=mysql_fetch_array(mysql_query("SELECT `id` FROM `bar_recept` WHERE `level`<='".$bar_user['level']."' ORDER BY RAND() DESC LIMIT 1"));
mysql_query("INSERT INTO `bar_zakaz`(`id_u`,`id_r`,`name`,`bot`) VALUES('".$user_id."','".$recept['id']."','".$name."','0')");
bar_msg($diz['msg'],'У вас появился новый заказ!');
mysql_query("UPDATE `bar_user` SET `time_to_next`='".(time()+60*30)."' WHERE `id_u`='".$user_id."'");
}
}

if($bar_user['alc']>999){
mysql_query("UPDATE `bar_user` SET `alc`='0',`nap`=`nap`+1 WHERE `id_u`='".$user_id."'");
bar_msg($diz['msg'],'Поздравляем! Вы напились!=)');
}

if($bar_user['exp']>20 && $bar_user['level']==0)
$up=1;
elseif($bar_user['exp']>60 && $bar_user['level']==1)
$up=1;
elseif($bar_user['exp']>100 && $bar_user['level']==2)
$up=1;
elseif($bar_user['exp']>160 && $bar_user['level']==3)
$up=1;
elseif($bar_user['exp']>260 && $bar_user['level']==4)
$up=1;
elseif($bar_user['exp']>460 && $bar_user['level']==5)
$up=1;
elseif($bar_user['exp']>700 && $bar_user['level']==6)
$up=1;
elseif($bar_user['exp']>900 && $bar_user['level']==7)
$up=1;
elseif($bar_user['exp']>1290 && $bar_user['level']==8)
$up=1;
elseif($bar_user['exp']>2500 && $bar_user['level']==9)
$up=1;
elseif($bar_user['exp']>4000 && $bar_user['level']==10)
$up=1;
elseif($bar_user['exp']>6000 && $bar_user['level']==11)
$up=1;
elseif($bar_user['exp']>8500 && $bar_user['level']==12)
$up=1;
elseif($bar_user['exp']>9000 && $bar_user['level']==13)
$up=1;
else
$up=0;

if($up==1)
mysql_query("UPDATE `bar_user` SET `level`=`level`+1 WHERE `id_u`='".$user_id."'");

if(mysql_result(mysql_query("SELECT COUNT(*) FROM `bar_coctails` WHERE `id_u`='".$user_id."' AND `act`='0' AND `sender`!='0'"),0)!=0){
bar_msg($diz['msg'],'Вам подарили коктейль!');
mysql_query("UPDATE `bar_coctails` SET `act`='1' WHERE `act`!='2' AND `id_u`='".$user_id."'");
}
?>
