<?php 
ob_start();
session_start();
//Настройки скрипта
function set($var, $db){
	$set = $db->getRow("SELECT `set` FROM ?n WHERE `name` = ?s LIMIT ?i",'settings',$var,1);
	return $set['set'];
}

function str2url($str) {
    $str = mb_strtolower($str, 'UTF-8');
    $str = preg_replace('~[^-a-zа-я0-9_]+~u', '-', $str);
    $str = trim($str, "-");
    return $str;
}

function plural_form($n, $forms) {
  return $n%10==1&&$n%100!=11?$forms[0]:($n%10>=2&&$n%10<=4&&($n%100<10||$n%100>=20)?$forms[1]:$forms[2]);
}

function getSize($url){
	$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url); //set url
    curl_setopt($ch, CURLOPT_HEADER, true); //get header
    curl_setopt($ch, CURLOPT_NOBODY, true); //do not include response body
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //do not show in browser the response
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //follow any redirects
    curl_exec($ch);
    $new_url = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD); //extract the url from the header response
    curl_close($ch);
	return $new_url;
}

function curl($url){
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl,CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 5.1; U; ru) Presto/2.2.15 Version/10.10');
	curl_setopt($curl, CURLOPT_REFERER, "http://google.ru/");
	curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($curl, CURLOPT_TIMEOUT, 30);
	$result = curl_exec($curl);
	curl_close($curl);

	return $result;
	}

function rus2eng($string) {

    $converter = array(

        'а' => 'a',   'б' => 'b',   'в' => 'v',

        'г' => 'g',   'д' => 'd',   'е' => 'e',

        'ё' => 'e',   'ж' => 'zh',  'з' => 'z',

        'и' => 'i',   'й' => 'y',   'к' => 'k',

        'л' => 'l',   'м' => 'm',   'н' => 'n',

        'о' => 'o',   'п' => 'p',   'р' => 'r',

        'с' => 's',   'т' => 't',   'у' => 'u',

        'ф' => 'f',   'х' => 'h',   'ц' => 'c',

        'ч' => 'ch',  'ш' => 'sh',  'щ' => 'sch',

        'ь' => '',  'ы' => 'y',   'ъ' => '',

        'э' => 'e',   'ю' => 'yu',  'я' => 'ya',



        '50 Cent' =>'50 центов', 'А' => 'A',   'Б' => 'B',   'В' => 'V',

        'Г' => 'G',   'Д' => 'D',   'Е' => 'E',

        'Ё' => 'E',   'Ж' => 'Zh',  'З' => 'Z',

        'И' => 'I',   'Й' => 'Y',   'К' => 'K',

        'Л' => 'L',   'М' => 'M',   'Н' => 'N',

        'О' => 'O',   'П' => 'P',   'Р' => 'R',

        'С' => 'S',   'Т' => 'T',   'У' => 'U',

        'Ф' => 'F',   'Х' => 'H',   'Ц' => 'C',

        'Ч' => 'Ch',  'Ш' => 'Sh',  'Щ' => 'Sch',

        'Ь' => '',  'Ы' => 'Y',   'Ъ' => '',

        'Э' => 'E',   'Ю' => 'Yu',  'Я' => 'Ya', 
		' ' => '_', '#' => '',
		'`' => '');

    return strtr($string, $converter);

}

function formatSize($size) { 
  $units = array(' Б', ' Кб', ' Мб', ' Гб', ' Тб'); 
  for ($i = 0; $size >= 1024 && $i < 4; $i++) {
    $size /= 1024; 
  }
 
  return round($size, 2).$units[$i]; 
}

function isBot(&$botname = ''){
  $bots = array(
    'rambler','googlebot','aport','yahoo','msnbot','turtle','mail.ru','omsktele',
    'yetibot','picsearch','sape.bot','sape_context','gigabot','snapbot','alexa.com',
    'megadownload.net','askpeter.info','igde.ru','ask.com','qwartabot','yanga.co.uk',
    'scoutjet','similarpages','oozbot','shrinktheweb.com','aboutusbot','followsite.com',
    'dataparksearch','google-sitemaps','appEngine-google','feedfetcher-google',
    'liveinternet.ru','xml-sitemaps.com','agama','metadatalabs.com','h1.hrn.ru',
    'googlealert.com','seo-rus.com','yaDirectBot','yandeG','yandex','YandexBot',
    'yandexSomething','Copyscape.com','AdsBot-Google','domaintools.com','Baiduspider',
    'Nigma.ru','bing.com','dotnetdotcom','Barkrowler','MJ12bot'
  );
  foreach($bots as $bot)
    if(stripos($_SERVER['HTTP_USER_AGENT'], $bot) !== false){
      $botname = $bot;
      return true;
    }
  return false;
}

function page($url,$total,$page){
	if($total > 1){
					
		if ($page != 1 && $page > 5) $pervpage = '<a href= '.($url == '/page/' ? '/' : $url).'>1</a>'; 
		if ($page != $total && $page <= ($total-5)) $endpage = ' <a href= '.$url.$total .'/>'.$total.'</a>'; 

		if($page - 4 > 0) $page4left = ' <a href= '.(($page - 4) != 1 ? $url.($page - 4).'/' : ($url == '/page/' ? _URL : $url)).'>'. ($page - 4) .'</a>'; 
		if($page - 3 > 0) $page3left = ' <a href= '.(($page - 3) != 1 ? $url.($page - 3).'/' : ($url == '/page/' ? _URL : $url)).'>'. ($page - 3) .'</a>'; 
		if($page - 2 > 0) $page2left = ' <a href= '.(($page - 2) != 1 ? $url.($page - 2).'/' : ($url == '/page/' ? _URL : $url)).'>'. ($page - 2) .'</a>'; 
		if($page - 1 > 0) $page1left = '<a href= '.(($page - 1) != 1 ? $url.($page - 1).'/' : ($url == '/page/' ? _URL : $url)).'>'. ($page - 1) .'</a>'; 
		if($page + 4 <= $total) $page4right = '<a href= '.$url. ($page + 4) .'/>'. ($page + 4) .'</a>'; 
		if($page + 3 <= $total) $page3right = '<a href= '.$url. ($page + 3) .'/>'. ($page + 3) .'</a>'; 
		if($page + 2 <= $total) $page2right = '<a href= '.$url. ($page + 2) .'/>'. ($page + 2) .'</a>'; 
		if($page + 1 <= $total) $page1right = '<a href= '.$url. ($page + 1) .'/>'. ($page + 1) .'</a>'; 

		$view .= (isset($pervpage) ? '<li>'.$pervpage.'</li>' : null).(isset($page4left) ? '<li>'.$page4left.'</li>' : null);
		$view .= (isset($page3left) ? '<li>'.$page3left.'</li>' : null).(isset($page2left) ? '<li>'.$page2left.'</li>' : null);
		$view .= (isset($page1left) ? '<li>'.$page1left.'</li>' : null).(isset($page) ? '<li><em>'.$page.'</em></li>' : null);
		$view .= (isset($page1right) ? '<li>'.$page1right.'</li>' : null).(isset($page2right) ? '<li>'.$page2right.'</li>' : null);
		$view .= (isset($page3right) ? '<li>'.$page3right.'</li>' : null).(isset($page4right) ? '<li>'.$page4right.'</li>' : null);
		$view .= (isset($endpage) ? '<li>'.$endpage.'</li>' : null); 
		
		return '<ul class="listalka">'.$view.'</ul>';			
	}
}

function total_page($page,$total,$url){
	if($page > $total){
		header("HTTP/1.1 301 Moved Permanently"); 
		header('Location: '.$url); exit;
	}
}

define('URL', set('protocol', $db).set('site_url', $db));
define('_URL', set('protocol', $db).$_SERVER['HTTP_HOST']);

?>