<?

function check_email($email) {
if(!preg_match("/^(?:[a-z0-9]+(?:[-_]?[a-z0-9\.\-\_]+)?@[a-z0-9]+(?:\.?[a-z0-9]+)?\.[a-z]{2,5})$/i", substr($email, 0, 50))) { 
    return ''; 
} else {
  return $email;
}
}

function get_us_name($id){
$sql = mysql_query("SELECT `login` FROM `nano_user` WHERE `id`='$id' LIMIT 1");
if(mysql_num_rows($sql) > 0){
return mysql_result($sql,0);
}else{
return "Неизвестный";
}
}

function get_us_id($id){
$sql = mysql_query("SELECT `id` FROM `nano_user` WHERE `login`='$id' LIMIT 1");
if(mysql_num_rows($sql) > 0){
return mysql_result($sql,0);
}else{
return "";
}
}

function check_text($text){
	$text=str_replace('`','',$text);
	$text=str_replace('$','',$text);
	$text=str_replace('{','',$text);
	$text=str_replace('}','',$text);
	$text=str_replace(']','',$text);
	$text=str_replace('[','',$text);
	$text=str_replace('|','',$text);
	$text=str_replace('&','',$text);
	$text=str_replace('~','',$text);
	$text=trim($text);
	return $text;
}

function getHost($url) {
	$host = str_replace("www.www.","www.", trim($url));
	$host = @parse_url($host);
	$host = trim($host['host'] ? $host['host'] : array_shift(explode('/', $host['path'], 2)));

	if(in_array("www", explode(".", $host))) {
		$just_domain = explode("www.", $host);
		return $just_domain[1];
	}else{
		return $host;
	}
}

function translit($str){
    $alphavit = array(
    "а"=>"a","б"=>"b","в"=>"v","г"=>"g","д"=>"d","е"=>"e",
    "ё"=>"yo","ж"=>"j","з"=>"z","и"=>"i","й"=>"i","к"=>"k","л"=>"l", "м"=>"m",
    "н"=>"n","о"=>"o","п"=>"p","р"=>"r","с"=>"s","т"=>"t",
    "у"=>"y","ф"=>"f","х"=>"h","ц"=>"c","ч"=>"ch", "ш"=>"sh","щ"=>"sh",
    "ы"=>"i","э"=>"e","ю"=>"u","я"=>"ya"," "=>"_",","=>"","."=>"","/"=>"",
	"А"=>"a","Б"=>"b","В"=>"v","Г"=>"g","Д"=>"d","Е"=>"e",
    "Ё"=>"yo","Ж"=>"j","З"=>"z","И"=>"i","Й"=>"i","К"=>"k","Л"=>"l", "М"=>"m",
    "Н"=>"n","О"=>"o","П"=>"p","Р"=>"r","С"=>"s","Т"=>"t",
    "У"=>"y","Ф"=>"f","Х"=>"h","Ц"=>"c","Ч"=>"ch", "Ш"=>"sh","Щ"=>"sh",
    "Ы"=>"i","Э"=>"e","Ю"=>"u","Я"=>"ya","ъ"=>"","ь"=>"","!"=>"","("=>"",")"=>"",
    );
    return strtr($str, $alphavit);
}

function translit_p($str){
    $alphavit = array(
    "а"=>"a","б"=>"b","в"=>"v","г"=>"g","д"=>"d","е"=>"e",
    "ё"=>"yo","ж"=>"j","з"=>"z","и"=>"i","й"=>"i","к"=>"k","л"=>"l", "м"=>"m",
    "н"=>"n","о"=>"o","п"=>"p","р"=>"r","с"=>"s","т"=>"t",
    "у"=>"y","ф"=>"f","х"=>"h","ц"=>"c","ч"=>"ch", "ш"=>"sh","щ"=>"sh",
    "ы"=>"i","э"=>"e","ю"=>"u","я"=>"ya"," "=>"_",","=>"","/"=>"",
	"А"=>"a","Б"=>"b","В"=>"v","Г"=>"g","Д"=>"d","Е"=>"e",
    "Ё"=>"yo","Ж"=>"j","З"=>"z","И"=>"i","Й"=>"i","К"=>"k","Л"=>"l", "М"=>"m",
    "Н"=>"n","О"=>"o","П"=>"p","Р"=>"r","С"=>"s","Т"=>"t",
    "У"=>"y","Ф"=>"f","Х"=>"h","Ц"=>"c","Ч"=>"ch", "Ш"=>"sh","Щ"=>"sh",
    "Ы"=>"i","Э"=>"e","Ю"=>"u","Я"=>"ya","ъ"=>"","ь"=>"","!"=>"","("=>"",")"=>"",
    );
    return strtr($str, $alphavit);
}

function ip_cart($str){
    $ip_cart = array(
    "."=>"",
    );
    return strtr($str, $ip_cart);
}

function uc($mensaje){
if (!preg_match("/^[a-zA-Zа-яА-Я0-9\-_]{1,255}$/i", $mensaje)) {
	$mensaje = htmlentities(stripslashes(strtolower(trim($mensaje))));
	$mensaje = str_replace("'"," ",$mensaje);
	$mensaje = str_replace(";"," ",$mensaje);
	$mensaje = str_replace("$"," ",$mensaje);
	return $mensaje;
}else{ return $mensaje; }
}

function valideint($str) {
return (isset($str)) ? filter_var($str, FILTER_VALIDATE_INT) : false;
}
function valideurl($str) {
return (isset($str)) ? filter_var($str, FILTER_VALIDATE_URL) : false;
}
function validefloat($str) {
return (isset($str)) ? filter_var($str, FILTER_VALIDATE_FLOAT) : false;
}
function valideip($str) {
return (isset($str)) ? filter_var($str, FILTER_VALIDATE_IP) : false;
}
function validemail($str) {
return (isset($str)) ? filter_var($str, FILTER_VALIDATE_EMAIL) : false;
}

function getRealIP(){

   if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
      $client_ip =
         ( !empty($_SERVER['REMOTE_ADDR']) ) ?
            $_SERVER['REMOTE_ADDR']
            :
            ( ( !empty($_ENV['REMOTE_ADDR']) ) ?
               $_ENV['REMOTE_ADDR']
               :
               "unknown" );
      $entries = explode('[, ]', $_SERVER['HTTP_X_FORWARDED_FOR']);

      reset($entries);
      while (list(, $entry) = each($entries)){
         $entry = trim($entry);
         if ( preg_match("/^([d]{1,3}).([d]{1,3}).([d]{1,3}).([d]{1,3})$/", $entry, $ip_list) ){
            $private_ip = array(
                  '/^0\./',
                  '/^127\.0\.0\.1/',
                  '/^192\.168\..*/',
                  '/^172\.((1[6-9])|(2[0-9])|(3[0-1]))\..*/',
                  '/^10\..*/');

            $found_ip = preg_replace($private_ip, $client_ip, $ip_list[1]);

            if ($client_ip != $found_ip)
            {
               $client_ip = $found_ip;
               break;
            }
         }
      }
   }else{
      $client_ip =
         ( !empty($_SERVER['REMOTE_ADDR']) ) ?
            $_SERVER['REMOTE_ADDR']
            :
            ( ( !empty($_ENV['REMOTE_ADDR']) ) ?
               $_ENV['REMOTE_ADDR']
               :
               "unknown" );
   }

   return $client_ip;

}



// вывод даты 
function date_get($data){
$dates = date("d.m",$data);
$date = date("d.m");
if($date == $dates) {
$date = "Сегодня ";
$date .= date("H:i", $data);
}else{
$date = date("d.m.y", $data);
}
return $date;
}

// Функция поиска
function search ($query) { 
$text = '';
                    
$query = strip_tags($query);               
$query = mysql_real_escape_string($query); 

if (!empty($query)){
if (strlen($query) < 2) {
$text = '<p>короткий поисковый запрос.</p>';
}elseif (strlen($query) > 128) {
$text = '<p>длинный поисковый запрос.</p>';
}else{
$sql = "SELECT * FROM `nano_users` WHERE `name` LIKE '%$query%'";
$result = mysql_query($sql);
$num = mysql_num_rows($result);

if ( $num > 0) { 
$row = mysql_fetch_assoc($result); 
$text .= '<p>По вашему запросу  <strong>'.$query.'</strong>';
$text .= ' найдено '.$num.' совпадений</p>';
do {
$id = $row['id'];
$text .= '<p><a href="/id'.$id.'">';
$text .= $row['name'].'</a></p>';
$text .= '<p>'.$row['soname'].'</p>';
} while ($row = mysql_fetch_assoc($result)); 
}else{
$text = '<p>По вашему запросу ничего не найдено.</p>';
}
} 
}else{
$text = '<p>Задан пустой поисковый запрос.</p>';
}
return $text; 
}
?>