<?

function on_k($id)
{
  global $set;
$usk=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_user` WHERE `id` = '$id'"));
$us=$usk;
$t=time()-$set['timeOnline'];
if($usk['date_last']<=$t)
{
if($us['pol']==0)
{
if($usk['level']>=0 && $usk['level']<30)$img="user_female_offline.png";
elseif($usk['level']>=30 && $usk['level']<35)$img="user_female_gold_offline.png";
elseif($usk['level']>=35 && $usk['level']<40)$img="user_female_platin_offline.png";
elseif($usk['level']>=40 && $usk['level']<45)$img="user_female_green_offline.png";
else $img="user_female_45_offline.png";
}
else
{
if($usk['level']>=0 && $usk['level']<30)$img="user_male_offline.png";
elseif($usk['level']>=30 && $usk['level']<35)$img="user_male_gold_offline.png";
elseif($usk['level']>=35 && $usk['level']<40)$img="user_male_platin_offline.png";
elseif($usk['level']>=40 && $usk['level']<45)$img="user_male_green_offline.png";
else $img="user_male_45_offline.png";
}

}
else
{
if($us['pol']==0)
{
if($usk['level']>=0 && $usk['level']<30)$img="user_female.png";
elseif($usk['level']>=30 && $usk['level']<35)$img="user_female_gold.png";
elseif($usk['level']>=35 && $usk['level']<40)$img="user_female_platin.png";
elseif($usk['level']>=40 && $usk['level']<45)$img="user_female_green.png";
else $img="user_female_45.png";
}
else
{
if($usk['level']>=0 && $usk['level']<30)$img="user_male.png";
elseif($usk['level']>=30 && $usk['level']<35)$img="user_male_gold.png";
elseif($usk['level']>=35 && $usk['level']<40)$img="user_male_platin.png";
elseif($usk['level']>=40 && $usk['level']<45)$img="user_male_green.png";
else $img="user_male_45.png";
}
}

if(!empty($usk['icon'])){
if($usk['date_last']<=$t){$prefix='_off';}else{$prefix='';}
$img=''.$usk['icon'].''.$prefix.'.png';
$papka='icon';
$size=23;
if($usk['id']==86)$size=21;
}else{
    $papka='icons';
    $size=16;
    $size2=16;
    
}
if($usk['time_vip']>time() && $usk['me_icon']>='' && (is_file("images/my_iconka/$usk[me_icon].png")))
{
    $papka='my_iconka';
    $size=23;

$img=$usk['me_icon'].'.png';

}

return" <img width='".$size."'  alt='*' src='images/".$papka."/".$img."'/>";
}
function time_left($tl)
{
if($tl<=0)$tl=1;
$d=3600*24;
$day=floor($tl/$d);
$tl=$tl-($d*$day);

$hour=floor($tl/3600);
$tl=$tl-(3600*$hour);

$minute=floor($tl/60);
$tl=$tl-(60*$minute);

$second=floor($tl);

$dayt="".($day>0?"$day д. ":null)."";
$hourt="".($hour>0?"$hour ч. ":null)."";
$minutet="".($minute>0?"$minute м. ":null)."";
$secondt="".($second>0?"$second с. ":null)."";
if($day>0)
{
$minutet=NULL;
$secondt=NULL;
}
if($hour>0 && $day==0)
{
$secondt=NULL;
$dayt=NULL;
}
return "$dayt$hourt$minutet$secondt";
}

function rating($rating)
{
$t=floor($rating/1000000000000);
$q=floor($rating/1000000000000000);
$qu=floor($rating/1000000000000000000);
$s=floor($rating/1000000000000000000000);

$g=floor($rating/1000000000);
$m=floor($rating/1000000);
$k=floor($rating/1000);
if($s!=0)
{
$s=$rating/1000000000000000000000;
$rating=round($s,2);
$rating="$rating s";
}
elseif($qu!=0)
{
$qu=$rating/1000000000000000000;
$rating=round($qu,2);
$rating="$rating qu";
}
elseif($q!=0)
{
$q=$rating/1000000000000000;
$rating=round($q,2);
$rating="$rating q";
}
elseif($t!=0)
{
$t=$rating/1000000000000;
$rating=round($t,2);
$rating="$rating t";
}
elseif($g!=0)
{
$g=$rating/1000000000;
$rating=round($g,2);
$rating="$rating g";
}
elseif($m!=0)
{
$m=$rating/1000000;
$rating=round($m,2);
$rating="$rating m";
}
elseif($k!=0)
{
$k=$rating/1000;
$rating=round($k,2);
$rating="$rating k";
}
else $rating=(int)$rating;
return "$rating";
}
////////////////////////////////Стоимость техники 1////////////////////////////
function rl($lr)
{
$llr=$lr+1;
if($llr==2)$lr=3;
elseif($llr==3)$lr=5;
elseif($llr==4)$lr=10;
elseif($llr==5)$lr=25;
elseif($llr==6)$lr=50;
elseif($llr==7)$lr=100;
elseif($llr==8)$lr=250;
elseif($llr==9)$lr=500;
elseif($llr==10)$lr=1000;
elseif($llr==11)$lr=2000;
elseif($llr==12)$lr=2500;
elseif($llr>=13 && $llr<=19)$lr=2000;
elseif($llr==20)$lr=4000;
else $lr=2000;
return $lr;
}
//////////////////////////////
function rl2($lr)
{
$llr=$lr+1;
if($llr==2)$lr=5;
elseif($llr==3)$lr=15;
elseif($llr==4)$lr=30;
elseif($llr==5)$lr=60;
elseif($llr==6)$lr=90;
elseif($llr==7)$lr=180;
elseif($llr==8)$lr=360;
elseif($llr==9)$lr=720;
elseif($llr==10)$lr=1440;
elseif($llr==11)$lr=2880;
elseif($llr==12)$lr=5760;
elseif($llr>=13 && $llr<=19)$lr=6000;
elseif($llr==20)$lr=7000;
else $lr=1000;
return $lr;
}
////////////////////////////////Стоимость техники 3////////////////////////////
function rl3($lr)
{
$llr=$lr+1;
if($llr==2)$lr=10;
elseif($llr==3)$lr=30;
elseif($llr==4)$lr=60;
elseif($llr==5)$lr=120;
elseif($llr==6)$lr=300;
elseif($llr==7)$lr=600;
elseif($llr==8)$lr=1000;
elseif($llr==9)$lr=2000;
elseif($llr==10)$lr=4000;
elseif($llr==11)$lr=8000;
elseif($llr==12)$lr=12000;
elseif($llr>=13 && $llr<=19)$lr=10000;
elseif($llr==20)$lr=15000;
else $lr=10000;
return $lr;
}

////////////////////////////////Стоимость колхозных построек 1////////////////////////////
function mc5000($lr)
{
if($lr==0)$cena=15000;
else if($lr<12)$cena=5000;
else $cena=($lr-10)*5000;
return $cena;
}
/////////////////////////////////////////////////Стоимость грядок/////////////////////////////////////////////////
function cpg($count)
{
global $id_action;
global $ku;
$count=$count+1;
if($count==5)$cena=10;
elseif($count==6)$cena=25;
elseif($count==7)$cena=50;
elseif($count==8)$cena=100;
elseif($count==9)$cena=250;
elseif($count>=10 && $count<=12)$cena=500;
elseif($count>=13 && $count<=20)$cena=1000;
elseif($count>=21 && $count<=25)$cena=3000;
elseif($count>=26 && $count<=35)$cena=6000;
elseif($count>=36 && $count<=40)$cena=10000;
else $cena=0;
if($ku['action_time']>$time)$cena=round($cena/2);
return $cena;
}

function cpf($count)
{
global $ku;

$count=$count+1;
if($count==5)$cena=10;
elseif($count==6)$cena=30;
elseif($count==7)$cena=60;
elseif($count==8)$cena=120;
elseif($count==9)$cena=250;
elseif($count==10)$cena=500;
elseif($count==11)$cena=900;
elseif($count==12)$cena=1300;
elseif($count==13)$cena=1800;
elseif($count>=14 && $count<=16)$cena=2000;
elseif($count>=17 && $count<=18)$cena=5000;
elseif($count>=19 && $count<=20)$cena=15000;
else $cena=10000;
if($ku['action_time']>$time)$cena=round($cena/2);
return $cena;
}
//////////////////////////////Cultivator rating//////////////////////
function cr($level)
{
if($level>=0 && $level<=10)$rat = 2;
else if($level>=11 && $level<=20)$rat = 5;
else if($level>=21 && $level<=30)$rat = 15;
else if($level>=31 && $level<=40)$rat = 40;
else if($level>=41 && $level<=50)$rat = 100;
else if($level>=51 && $level<=60)$rat = 500;
else if($level>=61)$rat = 1000;
return $rat;
}
//////////////////////////////Irragate rating//////////////////////
function ir($level)
{
if($level>=0 && $level<=10)$rat = 1;
else if($level>=11 && $level<=20)$rat = 3;
else if($level>=21 && $level<=30)$rat = 10;
else if($level>=31 && $level<=40)$rat = 30;
else if($level>=41 && $level<=50)$rat = 110;
else if($level>=51 && $level<=60)$rat = 600;
else if($level>=61)$rat = 1200;
return $rat;
}
//////////////////////////////Cultivator2 rating//////////////////////
function c2r($level)
{
if($level>=0 && $level<=10)$rat = 2;
else if($level>=11 && $level<=20)$rat = 5;
else if($level>=21 && $level<=30)$rat = 50;
else if($level>=31 && $level<=40)$rat = 500;
else if($level>=41 && $level<=50)$rat = 3000;
else if($level>=51 && $level<=60)$rat = 20000;
else if($level>=61)$rat = 200000;
return $rat;
}
/////////////////////////////////////////////////////////////////////////////////
function cpm($count)
{
$count=$count+1;
$cena=$count*10000000-40000000;
if($count>30)$cena=$cena*10;
if($cena<0)$cena=10000000;
return $cena;
}

function cpw($count)
{
$count=$count+1;
if($count==2)$cena=50;
elseif($count==3)$cena=100;
elseif($count==4)$cena=500;
elseif($count==5)$cena=1500;
elseif($count==5)$cena=2500;
else $cena=3000;
return $cena;
}
function err_game($msg)
{
echo "<div class='feedback'><ul class='feedbackPanel'><li class='feedbackPanelERROR'><span class='feedbackPanelERROR'>$msg</span></li></ul></div>";
}
//////////////////////////////////////////////////Цвет ника//////////////////////////////////////////////
function nc($id)
{
if(mysql_result(mysql_query("SELECT COUNT(id) FROM `kolhoz_ban` WHERE `id_user` = '$id' AND (`time` > '".time()."' OR (`izg` = '1' AND `time`='0')) AND `inlock` IS NULL"),0)!=0){
$ank=mysql_fetch_array(mysql_query("SELECT nick,NewYearDay,id,greynick,premium_time,date_last,level FROM `kolhoz_user` WHERE `id` = '$id' LIMIT 1"));
$t=time()-600;
$ti=time()-120;
if($ank['date_last']>=$t && $ank['date_last']<=$ti)$on_z="<span class='minor'>*</span>"; else $on_z='';
$anku=$ank;
return "<a class='' href='/user/$ank[id]'><span
class='nick".(mysql_result(mysql_query("SELECT COUNT(id) FROM `kolhoz_ban` WHERE `id_user` = '$id' AND  (`izg` = '1' AND ((`time`>'".(time())."' OR `time`='0') AND `inlock`IS NULL))"),0)!=0?"Banned":"Blocked")."'>".$anku['nick']."</span></a>";
}else{
$ank=mysql_fetch_array(mysql_query("SELECT status,NewYearDay,WingsLove,WingsLove_time,storona,id,nick,badge,premium_time,greynick,date_last,level,time_vip FROM `kolhoz_user` WHERE `id` = '$id' LIMIT 1"));
$t=time()-600;
$ti=time()-120;
if($ank['date_last']>=$t && $ank['date_last']<=$ti)$on_z="<span class='minor'>*</span>"; else $on_z='';
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_collective_user` WHERE `id_user` = '$id'"),0)==0 && $ank['level']>=10)
$on_k="<img width='16' height='16' alt='' src='/images/icons/nocollective.png'/>"; else $on_k="";
$anku=$ank;

if($ank['greynick']>0)$style="<font style='background: url(/pavelgrey/img/nickname/bling$ank[greynick].gif) repeat scroll 9% 0% transparent;'";
if($ank['premium_time']>=time())$premium=' <img width="16" src="/pavelgrey/img/gold.png">';
if($ank['badge']>'')$badge=' <img width="18" src="/pavelgrey/img/badge/'.$ank['badge'].'.png">';

if($ank['WingsLove_time']>=time())$WingsLove=' <a href="/?user='.$ank['WingsLove'].'"><img  width="16" src="/pavelgrey/img/WingsLove.png"></a>';

if($ank['id']<1)$span=" <a class='' href='/myfarm'>[Игрок удален]</a>";

if($ank['id']>0)$span="<a class='' href='/user/$ank[id]'>$style<span class='nick".($ank['status']==1?"Supporter":null)."".($ank['status']==2?"Moderator":null)."".($ank['status']==3?"Admin":null)."".($ank['time_vip']>=$time && $ank['status']<1?"Gold":null)."'>$anku[nick]</span></a></font>$premium $badge $WingsLove";
return "$span$on_z$on_k";
}


}
function ncTwo($id)
{
if(mysql_result(mysql_query("SELECT COUNT(id) FROM `kolhoz_ban` WHERE `id_user` = '$id' AND (`time` > '".time()."' OR (`izg` = '1' AND `time`='0')) AND `inlock` IS NULL"),0)!=0){
$ank=mysql_fetch_array(mysql_query("SELECT nick,NewYearDay,id,greynick,premium_time,date_last,level FROM `kolhoz_user` WHERE `id` = '$id' LIMIT 1"));$anku=$ank;
return "<a class='' href='/user/$ank[id]'><span
class='nick".(mysql_result(mysql_query("SELECT COUNT(id) FROM `kolhoz_ban` WHERE `id_user` = '$id' AND  (`izg` = '1' AND ((`time`>'".(time())."' OR `time`='0') AND `inlock`IS NULL))"),0)!=0?"Banned":"Blocked")."'>".$anku['nick']."</span></a>";
}else{
$ank=mysql_fetch_array(mysql_query("SELECT status,NewYearDay,storona,id,nick,star,premium_time,greynick,date_last,level,time_vip FROM `kolhoz_user` WHERE `id` = '$id' LIMIT 1"));
$anku=$ank;

if($ank['greynick']>0)$style="<font style='background: url(/pavelgrey/img/nickname/bling$ank[greynick].gif) repeat scroll 9% 0% transparent;'";

$span="<a class='' href='/user/$ank[id]'>$style<span class='nick".($ank['status']==1?"Supporter":null)."".($ank['status']==2?"Moderator":null)."".($ank['status']==3?"Admin":null)."".($ank['time_vip']>=$time && $ank['status']<1?"Gold":null)."'>$anku[nick]</span></a></font>";
return "$span";
}
    
}

function strkol($money)
{
$kols = number_format($money, 0, ',', ' ');
return ''.(str_replace(' ','\'', $kols)).'';
}


function no_rubies($rubies)
{
echo "<div class='feedback'><ul><li class='title'>У Вас не хватает <span>рубинов</span>:<img width='16' height='16' src='images/icons/ruby.png' alt='o'> <span>".strkol(floor($rubies))."</span></li><li></li><li><img width='16' height='16' src='images/icons/ruby.png' alt='o'>
<a href='?payment'>Купить</a></li><li><img width='16' height='16' src='images/icons/converter.png' alt='o'> <a href='?converter'>Обменять</a></li></ul></div>";
}

function no_money($money)
{
echo "<div class='feedback'><ul><li class='title'>У Вас не хватает <span>монет</span>:<img width='16' height='16' src='images/icons/money.png' alt='o'> <span>".strkol(floor($money))."</span></li><li></li><li><img width='16' height='16' src='images/icons/warehouse.png' alt='o'>
<a href='?warehouse'>Продать товар из амбара</a></li><li><img width='16' height='16' src='images/icons/converter.png' alt='o'> <a href='?converter'>Обменять</a></li></ul></div>";
}

function no_money_mc($money)
{
echo "<div class='feedback'><ul><li class='title'>В колхозе не хватает <span>монет</span>:<img width='16' height='16' src='images/icons/money.png' alt='o'> <span>".strkol(floor($money))."</span></li></ul></div>";
}

function hsc($text)
{
return htmlspecialchars($text);
}

function base_exp($rat)
{
global $ku,$mc;
    if($ku['iconExp'])$avatar = (($rat/100)*$ku['iconExp']); else $avatar=0;
        if($ku['expbonus'])$expbonus = (($rat/100)*$ku['expbonus']); else $expbonus=0;

    if($ku['chat']>=1000)$bronze = (($rat/100)*100); else $bronze=0;
    if($ku['forum']>=1000)$silver = (($rat/100)*150); else $silver=0;
    $sum3=$ku['chat']+$ku['forum'];
    if($sum3>=4000)$gold = (($rat/100)*200); else $gold=0;
  if($ku['premium_time']>time())$premium_rat=$rat*3; else $premium_rat=0;

  if($ku['scarecrow_time']>time())$scarecrow_rat=$rat; else $scarecrow_rat=0;
  if($ku['dog_time']>time())$dog_rat=($rat/100)*$ku['dog_type']; else $dog_rat=0;
    if($ku['WingsLove_time']>time())$wings_rat=($rat/100)*200; else $wings_rat=0;

  if($ku['time_drakon']>time())$drakon_rat=$rat*3; else $drakon_rat=0;
  if($ku['arena_rating']>1000)$arena_rat=floor(($rat/100)*(($ku['arena_rating']-1000)/20)); else $arena_rat=0;
  if(isset($mc) && $mc['school']>0 && $by['level']<=20+$mc['school']*2)$school_rat=$rat; else $school_rat=0;
  if(isset($mc) && $mc['lib']==1)$lib_rat=$rat/2; else $lib_rat=0;
  $time=time();
  if($ku['exp5_time']<$time) 
  {
      return $rat+$drakon_rat+$wings_rat+$premium_rat+$expbonus+$school_rat+$lib_rat+$scarecrow_rat+$silver+$gold+$bronze+$dog_rat+$avatar+$arena_rat;
}else{
         return (5*($rat+$wings_rat+$drakon_rat+$premium_rat+$expbonus+$school_rat+$lib_rat+$scarecrow_rat+$silver+$gold+$bronze+$dog_rat+$avatar+$arena_rat));
 
}
      
  }

function double_exp($rat)
{
global $ku;
  if($ku['expdouble']==date("D"))$dexp=1;
  //$dexp=1;////////Двойной опыт
  $dexp3=1;////////Тройной опыт
  if(isset($dexp) AND !isset($dexp3))$double_rat=$rat*2; elseif(isset($dexp3))$double_rat=$rat*3;
  if($ku['time_vip']>time())$vip_rat=$rat*2; else $vip_rat=0;
  $ratt=$double_rat+$vip_rat;
  if($ratt==0)$ratt=$rat;
  return $ratt;
}

function update_mc($rating)
{
global $mc,$mcu,$ku,$time;
mysql_query("UPDATE `kolhoz_user` SET `TournoumentExperience` = `TournoumentExperience` + '$rating' WHERE `id` = '$ku[id]'");

if($mcu)
{
$rating=$rating*1.6;

mysql_query("UPDATE `kolhoz_collective` SET `rating` = '".($mc['rating']+$rating)."' WHERE `id` = '$mc[id]'");
mysql_query("UPDATE `kolhoz_collective_user` SET `rating` = '".($mcu['rating']+$rating)."',`rating_week`='".($mcu['rating_week']+$rating)."' WHERE `id` = '$mcu[id]'");
}
}

function podtv($yes_link,$no_link)
{
echo '<div class="event"><h1>Подтверждение</h1></div><div class="content"><ul class="block"><li><span class="title">Вы уверены?</span></li><li class="pt"><img alt="" width="16" height="16" src="images/icons/tick.png"> <a href="'.$yes_link.'"><span>Да, подтверждаю</span></a></li><li><img alt="" width="16" height="16" src="images/icons/cross.png"> <a href="'.$no_link.'"><span>
Нет, отказываюсь</span></a></li></ul>';
}

function no_money_ct($money)
{
global $mc;
echo "<div class='feedback'>\n
<ul>\n
<li class='title'>В колхозной кассе не хватает <span>монет</span>: <img width='16' height='16' src='images/icons/money.png' alt='o'> <span>".floor($money)."</span></li>\n
<li>В кассу может внести средства любой житель колхоза добровольно или через налоги.</li>\n
<li><a href='?collective=$mc[id]&cash'>Пополнить кассу</a></li>\n
<li><a href='?collective=$mc[id]&taxes'>В налоговую</a></li>\n
</ul>\n
</div>\n";
}

function no_rubies_ct($rubies)
{
global $mc;
echo "<div class='feedback'>\n
<ul>\n
<li class='title'>В колхозной кассе не хватает <span>рубинов</span>: <img width='16' height='16' src='images/icons/ruby.png' alt='o'> <span>".floor($rubies)."</span></li>\n
<li>В кассу может внести средства любой житель колхоза.</li>\n
<li><a href='?collective=$mc[id]&cash'>Пополнить кассу</a></li>\n
<li><a href='?collective=$mc[id]&flowerbed'>На клумбу</a></li>\n
</ul>\n
</div>\n";
}

function harvest($harvest)
{
echo "<div class='feedback'><img width='16' height='16' src='images/icons/seat.png' alt='o'> Урожай: <span>".strkol(floor($harvest))." шт.</span></div>";
}
function harvest2($harvest2)
{
if(floor($harvest2)==1){$tex="бaнкa";}
if(floor($harvest2)>=2 and floor($harvest2)<=4){$tex="бaнки";}
if(floor($harvest2)>=5 and floor($harvest2)<=20){$tex="бaнoк";}
if(floor($harvest2)>=21){$tex="шт.";}
echo "<div class='feedback'><img width='16' height='16' src='images/icons/recipes.png' alt='o'> Готово: <span>".strkol(floor($harvest2))." $tex</span></div>";
}


function ratingg($rating)
{
    global $ku;
echo "<div class='feedback'><img width='16' height='16' src='images/icons/experience.png' alt='*'> Опыт +<span>".strkol(floor($rating))."</span></div>";
mysql_query("UPDATE `kolhoz_user` SET `TournoumentExperience` = `TournoumentExperience` + '$rating' WHERE `id` = '$ku[id]'");

    
}

function money($money)
{
global $mc,$ku,$mcu;
echo "<div class='feedback'><img width='16' height='16' src='images/icons/money.png' alt='*'> Доход: <span>".strkol(floor($money))." мон.</span>";
if($mc['nalog']>0)
{
$nmoney=floor(($money/100)*$mc['nalog']);
mysql_query("UPDATE `kolhoz_user` SET `umoney` = '0' WHERE `id` = '$ku[id]'");
echo " <span class='minor'>(колхозный налог <span><img
width='16' height='16' src='images/icons/money.png' alt='o'> <span class='title'>".strkol($nmoney)."</span></span>)</span>";
}
mysql_query("UPDATE `kolhoz_user` SET `umoney` = '0' WHERE `id` = '$ku[id]'");
echo "</div>";
}

function msg_game($msg)
{
echo "<div class='feedback'>".bbcode($msg)."</div>";
}

function msg_err($msg)
{
echo "<div class='feedback'><span class='major'>".bbcode($msg)."</span></div>";
}

function br($msg,$br='<br/>'){return preg_replace("#((<br( ?/?)>)|\r\n)#i",$br, $msg);} // переносы строк


function my_esc($str){return mysql_real_escape_string($str);}
function antispam($str){return
preg_replace('#([a-z0-9\\s\-\.]){3,20}(\.|,)([a-z\\s]){2,4}#si', 'tilas.ru', $str);
}

function links_preg1($arr)
{
global $set;

if (preg_match('#^http://'.preg_quote($_SERVER['HTTP_HOST']).'#',$arr[1]) || !preg_match('#://#',$arr[1]))
return '<a href="'.$arr[1].'">'.$arr[2].'</a>';
else
return '<a'.($set['web']?' target="_blank"':null).' href="http://'.$_SERVER['HTTP_HOST'].'/go.php?go='.base64_encode(html_entity_decode($arr[1])).'">'.$arr[2].'</a>';

}
function links_preg2($arr)
{
global $set;
if (preg_match('#^http://'.preg_quote($_SERVER['HTTP_HOST']).'#',$arr[2]))
return $arr[1].'<a href="'.$arr[2].'">'.$arr[2].'</a>'.$arr[4];
else
return $arr[1].'<a'.($set['web']?' target="_blank"':null).' href="http://'.$_SERVER['HTTP_HOST'].'/go.php?go='.base64_encode(html_entity_decode($arr[2])).'">'.$arr[2].'</a>'.$arr[4];
}
function links($msg)
{
global $set;
$msg=preg_replace_callback('/\[url=(.+)\](.+)\[\/url\]/isU', 'links_preg1', $msg); 
$msg=preg_replace_callback('~(^|\s)([a-z]+://([^ \r\n\t`\'"]+))(\s|$)~iu', 'links_preg2', $msg);
return $msg;
}

function output_text($str,$user=0)
{
$uk=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_user` WHERE `id` = '$user'"));
if($user==0)$uk['status']=1;
$str=htmlentities($str, ENT_QUOTES, 'UTF-8'); // преобразуем все к нормальному перевариванию браузером
$q=mysql_query("SELECT * FROM `kolhoz_smiles` ORDER BY `id` ASC");
while($post=mysql_fetch_array($q))
{
$n=explode(',',$post['name']);
$nn=NULL;
foreach($n as $key => $value)
{
$str=str_replace($value,"<img src='images/smiles/$post[img]' alt='o'/>",$str);
}
}
if($uk['status']>0 || $uk['id']==701 || $uk['level']>=200 || $uk['time_vip']>time())
{
$str=links($str); // обработка ссылок
$str="<span class='nick".($uk['status']==1?"Supporter":null)."".($uk['status']==2?"Moderator":null)."".($uk['status']==3?"Admin":null)."'>$str</span>";
$bbcode=array();
$bbcode['/\[img](.+)\[\/img\]/isU']='<img src="$1" alt=""/>';
$bbcode['/\[imgfont:(.+)\](.+)\[\/imgfont\]/isU']='<img width="$1" src="$2" alt=""/>';
$bbcode['/\[small\](.+)\[\/small\]/isU']='<span class="txt-md">$1</span>';

$bbcode['/\[color=(.+)\](.+)\[\/color\]/isU']='<span style="color:$1">$2</span>';
$bbcode['/\[i\](.+)\[\/i\]/isU']='<i>$1</i>';
$bbcode['/\[s\](.+)\[\/s\]/isU']='<s>$1</s>';
$bbcode['/\[b\](.+)\[\/b\]/isU']='<b>$1</b>';
$bbcode['/\[u\](.+)\[\/u\]/isU']='<u>$1</u>';
$bbcode['/\[center\](.+)\[\/center\]/isU']='<center>$1</center>';
$bbcode['/\[marq\](.+)\[\/marq\]/isU']='<marquee behavior="scroll" direction="left">$1</marquee>';
$bbcode['/\[star](.+)\[\/star\]/isU']='<font style="color:rgba(36, 150, 188, 1); background: url(http://randomhack.wf/forum/public/style_images/blink/bling1.gif) repeat scroll 9% 0% transparent; font-weight: bold ; text-shadow:0px 0px 15px rgba(67, 69, 224, 1);">$1</font>';
$bbcode['/\[grey](.+)\[\/grey\]/isU']='
<font style="color:rgba(36, 150, 188, 1); background: url(http://randomhack.wf/forum/public/style_images/blink/bling6.gif) repeat scroll 9% 0% transparent; font-weight: bold ; text-shadow:0px 0px 15px rgba(67, 69, 224, 1);">$1</font>';
$bbcode['/\[zip](.+)\[\/zip\]/isU']='<font style="color:rgba(36, 150, 188, 1); background: url(http://randomhack.wf/forum/public/style_images/blink/bling9.gif) repeat scroll 9% 0% transparent; font-weight: bold ; text-shadow:0px 0px 15px rgba(67, 69, 224, 1);">$1</font>';
$bbcode['/\[size=([0-9]+)\](.+)\[\/size\]/isU']='<span style="font-size:$1px;">$2</span>';
if (count($bbcode))$str= preg_replace(array_keys($bbcode), array_values($bbcode), $str);
}
$str=nl2br($str); // переносы строк
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `complaint` WHERE `infringer` = '$uk[id]' AND `check` ='0'"),0)>=3)
{
$str='<s>Игрок '.$uk['nick'].' заморожен, сообщение недоступно</s>';
}
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_ban` WHERE `id_user` = '$uk[id]' AND (`izg` = '1' AND ((`time`>'$time' OR `time`='0') AND `inlock`IS NULL)) LIMIT 1"),0)!=0)
{
$str='<s>Игрок '.$uk['nick'].' заблокирован, сообщение недоступно</s>';
}
return stripslashes($str); // возвращаем обработанную строку
}

function bbcode($str)
{

$str=htmlentities($str, ENT_QUOTES, 'UTF-8'); // преобразуем все к нормальному перевариванию браузером
/*
$q=mysql_query("SELECT * FROM `kolhoz_smiles` ORDER BY `id` ASC");
while($post=mysql_fetch_array($q))
{
$n=explode(',',$post['name']);
$nn=NULL;
foreach($n as $key => $value)
{
$str=str_replace($value,"<img src='images/smiles/$post[img]' alt='o'/>",$str);
}
}
*/

$str=links($str); // обработка ссылок
$bbcode=array();
$bbcode['/\[img](.+)\[\/img\]/isU']='<img src="$1" alt=""/>';
$bbcode['/\[imgfont:(.+)\](.+)\[\/imgfont\]/isU']='<img width="$1" src="$2" alt=""/>';
$bbcode['/\[color=(.+)\](.+)\[\/color\]/isU']='<span style="color:$1">$2</span>';
$bbcode['/\[colors=(.+)\](.+)\[\/colors\]/isU']='<span style="color: #$1">$2</span>';
$bbcode['/\[i\](.+)\[\/i\]/isU']='<i>$1</i>';
$bbcode['/\[s\](.+)\[\/s\]/isU']='<s>$1</s>';
$bbcode['/\[b\](.+)\[\/b\]/isU']='<b>$1</b>';
$bbcode['/\[u\](.+)\[\/u\]/isU']='<u>$1</u>';
$bbcode['/\[li\](.+)\[\/li\]/isU']='<li>$1</li>';

$bbcode['/\[size=([0-9]+)\](.+)\[\/size\]/isU']='<span style="font-size:$1px;">$2</span>';
if (count($bbcode))$str= preg_replace(array_keys($bbcode), array_values($bbcode), $str);

$str=br($str); // переносы строк
return stripslashes($str); // возвращаем обработанную строку
}


function strlen2($str)
{
$rus=array('й','ц','у','к','е','н','г','ш','щ','з','х','ъ','ф','ы','в','а','п','р','о','л','д','ж','э','я','ч','с','м','и','т','ь','б','ю','Й','Ц','У','К','Е','Н','Г','Ш','Щ','З','Х','Ъ','Ф','Ы','В','А','П','Р','О','Л','Д','Ж','Э','Я','Ч','С','М','И','Т','Ь','Б','Ю');
return strlen(str_replace($rus, '0', $str));
}

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 "<div class=\"str\">\n";
if ($page!=1)echo "<a href=\"".$link."page=1\" title='Первая страница'>&lt;&lt;</a> ";
if ($page>1)echo "<a href=\"".$link."page=".($page-1)."\" title='Предыдущая страница (№".($page-1).")'>&lt;назад</a> ";
echo " ";
if ($page<$k_page)echo " <a href=\"".$link."page=".($page+1)."\" title='Следующая страница (№".($page+1).")'>далее&gt;</a>";
if ($page!=$k_page)echo " <a href=\"".$link."page=end\" title='Последняя страница'>&gt;&gt;</a>";
echo "<br />\n";
if ($page!=1)echo "<a href=\"".$link."page=1\" title='Страница №1'>[1]</a>";else echo "<span class='minor'>[1]</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)."'>[".($page+$ot)."]</a>";else echo " <span class='minor'>[".($page+$ot)."]</span>";
if ($ot==3 && $page+$ot<$k_page-1)echo " ..";}}
if ($page!=$k_page)echo " <a href=\"".$link."page=end\" title='Страница №$k_page'>[$k_page]</a>";elseif ($k_page>1)echo " <span class='minor'>[$k_page]</span>";

echo "</div>\n";
}

function new_str($link='?',$k_page=1,$page=1){ // Вывод номеров страниц (только на первый взгляд кажется сложно ;))
if ($page<1)$page=1;
echo "<div class=\"str\">\n";
if ($page!=1)echo "<a href=\"/".$link."page-1\" title='Первая страница'>&lt;&lt;</a> ";
if ($page>1)echo "<a href=\"/".$link."page-".($page-1)."\" title='Предыдущая страница (№".($page-1).")'>&lt;назад</a> ";
echo " ";
if ($page<$k_page)echo " <a href=\"/".$link."page-".($page+1)."\" title='Следующая страница (№".($page+1).")'>далее&gt;</a>";
if ($page!=$k_page)echo " <a href=\"/".$link."page-end\" title='Последняя страница'>&gt;&gt;</a>";
echo "<br />\n";
if ($page!=1)echo "<a href=\"/".$link."page-1\" title='Страница №1'>[1]</a>";else echo "<span class='minor'>[1]</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)."'>[".($page+$ot)."]</a>";else echo " <span class='minor'>[".($page+$ot)."]</span>";
if ($ot==3 && $page+$ot<$k_page-1)echo " ..";}}
if ($page!=$k_page)echo " <a href=\"/".$link."page-end\" title='Страница №$k_page'>[$k_page]</a>";elseif ($k_page>1)echo " <span class='minor'>[$k_page]</span>";

echo "</div>\n";
}

function hours($time)
{
global $user;
if($time<3600){$minit=floor($time/60);$min=substr("$minit", -1);if($min==0 OR $min>=5)$tex='минут';elseif($min==1)$tex='минуту';elseif($min>=2 AND $min<=4)$tex='минуты';$promin=substr("$minit", -2); $hour='';$xtex=''; if($promin>=10 AND $promin<=20)$tex='минут';}
if($time>3600){$hour=floor($time/3600);$minit=floor(($time-$hour*3600)/60);$min=substr("$minit", -1);if($min==0 OR $min>=5)$tex='минут';elseif($min==1)$tex='минуту';elseif($min>=2 AND $min<=4)$tex='минуты'; $promin=substr("$minit", -2); if($promin>=10 AND $promin<=20)$tex='минут';
$xmin=substr("$hour", -1);if($xmin==0 OR $xmin>=5)$xtex='часов';elseif($xmin==1)$xtex='час';elseif($xmin>=2 OR $xmin<=4)$xtex='часа'; $proxmin=substr("$hour", -2); if($proxmin>=10 AND $proxmin<=20)$xtex='часов';
if($hour>=200){$minit=''; $tex='';}}

return "$hour $xtex $minit $tex";
}

function vremja($time=NULL)
{
global $user;
if ($time==NULL)$time=time();
if (isset($user))$time=$time+$user['set_timesdvig']*60*60;
$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 (isset($user)){
if ($time_p[0]==date("j n Y", time()+$user['set_timesdvig']*60*60))$timep=date("H:i:s", $time);
if ($time_p[0]==date("j n Y", time()-60*60*(24-$user['set_timesdvig'])))$timep="Вчера в $time_p[1]";}
else{
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 passgen($k_simb=8, $types=3) {
$password="";
$small="abcdefghijklmnopqrstuvwxyz";
$large="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$numbers="1234567890";
mt_srand((double)microtime()*1000000); 
for ($i=0; $i<$k_simb; $i++) { 
$type=mt_rand(1,min($types,3));
switch ($type) {
case 3:
$password.=$large[mt_rand(0,25)];
break;
case 2:
$password.=$small[mt_rand(0,25)];
break;
case 1:
$password.=$numbers[mt_rand(0,9)];
break;
}
}
return $password;
}
$passgen=&passgen();

function minus_money($money)
{
global $ku;
mysql_query("UPDATE `kolhoz_user` SET `money` = '".($ku['money']-$money)."' WHERE `id` = '$ku[id]'");
return ($ku['money']-$money);
}

function minus_rubies($rubies)
{
global $ku;
mysql_query("UPDATE `kolhoz_user` SET `rubies` = '".($ku['rubies']-$rubies)."' WHERE `id` = '$ku[id]'");
return ($ku['rubies']-$rubies);
}

function plus_money($money)
{
global $ku;
mysql_query("UPDATE `kolhoz_user` SET `money` = '".($ku['money']+$money)."' WHERE `id` = '$ku[id]'");
return ($ku['money']+$money);
}

function plus_rubies($rubies)
{
global $ku;
mysql_query("UPDATE `kolhoz_user` SET `rubies` = '".($ku['rubies']+$rubies)."' WHERE `id` = '$ku[id]'");
return ($ku['rubies']+$rubies);
}

function update_ku($field,$value)
{
global $ku;
mysql_query("UPDATE `kolhoz_user` SET `".$field."` = '".$value."' WHERE `id` = '$ku[id]'");
return $value;
}
function on_z($id)
{
$usk=mysql_fetch_array(mysql_query("SELECT `date_last` FROM `kolhoz_user` WHERE `id` = '$id' LIMIT 1"));
$t=time()-600;
$ti=time()-120;
if($usk['date_last']>=$t AND $usk['date_last']<=$ti )echo'<span class="minor">*</span>';
}

function require_header ($title) {
global $ku,$conf;
echo "<div class='eventfarm'>".($conf['balans']==1?"<div style='text-align:right;padding:4px;' class='small'>
<span><span class='nobr'><img width='16' height='16' src='/images/icons/money.png' alt=''/> <span class='title'>".(strkol($ku['money']))."</span></span></span>
<span><span class='nobr'><img width='16' height='16' src='/images/icons/ruby.png' alt=''/> <span class='title'>".(strkol($ku['rubies']))."</span></span></span>":"<h1>$title</h1>")."</div></div>";
}


function zl($lr)
{
$llr=$lr+1;
if($llr==21)$lr=1000;
elseif($llr==22)$lr=5000;
elseif($llr==23)$lr=10000;
elseif($llr==24)$lr=25000;
elseif($llr==25)$lr=50000;
elseif($llr==26)$lr=100000;
elseif($llr==27)$lr=250000;
elseif($llr==28)$lr=500000;
elseif($llr==29)$lr=1000000;
elseif($llr==30)$lr=5000000;
else $lr=2000;
return $lr;
}

?>