<?
set_time_limit(5);
$lg='uz'; $language='uzbek';
setlocale(LC_ALL, 'uz_UZ.UTF-8'); mb_internal_encoding("UTF-8"); date_default_timezone_set('Europe/Moscow');
if (!preg_match("/firmscan.com/si", $_SERVER["HTTP_HOST"])) { header("Location: http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]); exit; }
header("Content-Type: text/html; charset=utf-8");

if (isset($_GET["f"])) { $f=trim($_GET["f"]); } else { $f=""; }
if ($f=="Shahar, firma, mol yoki xizmat nomini kiriting") { $f=""; }
if (isset($_GET["d"])) { $d=strtolower(trim($_GET["d"])); } else { $d=""; }
if (!preg_match("/^[a-z0-9\-]+.[a-z0-9]+$/sD", $d)) { $d=""; }
if ($d) { $whered=" domain like '%.$d' and "; } else { $whered=""; }
$fo=$f; $do=$d;

$title="Kompaniyalarni qidirish"; $text=""; $firms=0;
if ($f) {
	$title=htmlspecialchars($f);
	$f=mb_strtolower($f, 'UTF-8'); $f=str_replace("'", "`", $f);
	$f=preg_replace("/[\"'\/\_\+=,\\\.\#\$\!\?\*\^\&\[\]\|;\(\)[:space:]]+/s", " ", " $f ");
	$f=preg_replace("/ (http|www|at|in|on|from|to|since|till|into|onto|before|after|about|for|during|of|with|by|between|among|except|besides|over|below|out|behind|it|is|are|the|a) /su", " ", $f);
	$f=trim($f);
	if (preg_match("/^[^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ .*$/sDu", $f)) { $f=preg_replace("/^([^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+) .*$/sDu", "\\1", $f); }
	$e=urlencode($f);
	if (is_dir('/usr/home/pmm/memory/firmscan/stat')) {
		$ip=$_SERVER['REMOTE_ADDR'];
		$fs=trim(preg_replace("/['\"\|[:space:]]+/s", " ", $f));
		$fp=fopen('/usr/home/pmm/memory/firmscan/stat/'.md5($fs).'.txt', 'a');
		fwrite($fp, $lg.'|'.$fs.'|'.$ip."\n");
		fclose($fp);
	}
	$f=trim(preg_replace("/s /su", " ", $f));
	$f=str_replace("'", "", preg_replace("/(\d)[\(\)[:space:]\-](\d)/s", "\\1\\2", preg_replace("/[[:space:]]+/s", " ", trim($f))));
	$f=preg_replace("/(\d)[\(\)[:space:]\-](\d)/s", "\\1\\2", $f);
	$slov=explode(" ", $f); $nslov=count($slov);
	
	$qw='';
	for ($j=0; $j<$nslov; $j++) {
		$word=trim($slov[$j]);
		@ $b=mb_substr($word, 0, 3, 'utf-8'); $c=md5($b);
		$c1=substr($c, 0, 1); $c2=substr($c, 1, 1); $c3=substr($c, 2, 1); $c=$c1.$c2.$c3;
		if ($qw) { $qw.=' or'; }
		$qw.=" b='$c'";
	}
	
	if ($qw) {
		$dbi=new SQLite3('/usr/home/pmm/firmscan/search/'.$lg.'/words.db');
		$word=$dbi->querySingle("select b from words where$qw order by n limit 1");
		$dbi=null;
		$s1=substr($word, 0, 1); $s2=substr($word, 1, 1); $s3=substr($word, 2, 1);
		
		if ($word) {
			$where=" where$whered search like '% ".str_replace(" ", "%' and search like '% ", $f)."%'";
			$where=preg_replace("/ (\d)/s", "\\1", $where);
			$db=new SQLite3('/usr/home/pmm/firmscan/search/'.$lg.'/'.$s1.'/'.$s2.'/'.$s3.'.db');
			$firms=$db->querySingle("select count(*) from companies$where");
			$i=0; $result=$db->query("select * from companies$where order by rating desc, updated desc limit 50");
			while ($row=$result->fetchArray()) {
				$i++;
				$rating=$row["rating"];
				$updated=preg_replace("/^([0-9]{4})\-([0-9]{2})\-([0-9]{2})$/D", "\\3.\\2.\\1", $row["updated"]);
				$domain=$row["domain"];
				$domen=preg_replace("/^[^\.]*\./sD", "", $domain);
				$city=$row["city"];
				$company=$row["company"];
				$service=$row["service"];
				$service=preg_replace("/[^a-žA-Ž0-9]$/sDu", "", $service);
				if ($service) { if (substr($service, -1)<>".") { $service.="."; }}
				$address=$row["address"];
				$address=preg_replace("/[^a-žA-Ž0-9]$/sDu", "", $address);
				if ($address) { if (substr($address, -1)<>".") { $address.="."; }}
				$phone=$row["phone"];
				$search=$row["search"];
				for ($j=0; $j<$nslov; $j++) {
					$search=preg_replace("/(".$slov[$j]."[^ ]*)/su", "<u>$1</u>", $search);
				}
				if ($rating) { $rating="+$rating"; } else { $rating=""; }
				$search=preg_replace("/<\/u>[^<>]*$/sD", "", preg_replace("/^[^<>]*<u>/sD", "", preg_replace("/<\/u>[^<>]*<u>/s", " ", $search))).'</u>';
				$text.="
<div>
	<div>
		<div class=\"firm-counter\">$rating</div>
		<div class=\"firm-date\">$updated</div>
	</div>
	<div>
		<a href=\"http://www.$domain\" class=\"firm-name\" target=\"_blank\">$company</a>
		<span class=\"firm-desc\">$service</span>
		<span class=\"firm-address\">$address</span>
		<span class=\"firm-phone\">$phone</span>
		<div class=\"firm-type\">$search</div>
	</div>
	<div>
		<a href=\"/$lg.php?f=$e&d=$domen\" class=\"firm-city\">$city</a>
	</div>
</div>
				";
			}
		}
	}
	$text=str_replace("`", "'", $text);
	$text=str_replace("<span class=\"firm-address\"></span>", "", $text);
	$text=str_replace("<span class=\"firm-phone\"></span>", "", $text);
	$text=str_replace("<span class=\"firm-address\">", "<span class=\"firm-address\">Manzil: ", $text);
	$text=str_replace("<span class=\"firm-phone\">", "<span class=\"firm-phone\">Telefon: ", $text);
	$text=languages().$text;
	design($title, $text, 'default');
}
@ $text=file_get_contents("/usr/home/pmm/firmscan/search/$lg/top.html");
$text=str_replace("<span class=\"firm-address\"></span>", "", $text);
$text=str_replace("<span class=\"firm-phone\"></span>", "", $text);
$text=str_replace("<span class=\"firm-address\">", "<span class=\"firm-address\">Manzil: ", $text);
$text=str_replace("<span class=\"firm-phone\">", "<span class=\"firm-phone\">Telefon: ", $text);
$text.="<div>&nbsp;</div>";
design($title, $text, 'main');


# ****************************************************************
# Вывод страницы в дизайне кабинета компании
# ****************************************************************
function design($title, $text, $namedesign) {
	global $firms, $lg;
	$design=file_get_contents("design/$lg/$namedesign.html");
	$design=str_replace("@TITLE@", $title, $design);
	$text=str_replace("@TEXT@", $text, $design);
	$text=str_replace("@N@", $firms, $text);
	echo $text; exit;
}


function podpis($n) {
	return "shirkat";
}


# ****************************************************************
# Результаты поиска на всех языках
# ****************************************************************
function languages() {
	global $firms;
	$text="";
	$text.=english();
	$text.=spanish();
	$text.=russian();
	$text.=french();
	$text.=german();
	$text.=czech();
	$text.=latvian();
	#$text.=uzbek();
	if ($text) { $text='<p align="center"><font color=gray><b>'.$firms.'</b> '.podpis($firms).' (UZ)'.$text.'</font>'; }
	return $text;
}


# ****************************************************************
# Результаты поиска на английском языке
# ****************************************************************
function english() {
	global $whered, $do, $fo;
	$lang=$lg; $f=$fo; $d=$do;
	$lg="en"; if ($lang==$lg) { return; }
	$f=mb_strtolower($f, 'UTF-8');
	$f=preg_replace("/[\"'\/\_\+=,\\\.\#\$\!\?\*\^\&\[\]\|;\(\)[:space:]]+/s", " ", " $f ");
	$f=preg_replace("/ (http|www|at|in|on|from|to|since|till|into|onto|before|after|about|for|during|of|with|by|between|among|except|besides|over|below|out|behind|it|is|are|the|a) /su", " ", $f);
	$f=trim($f);
	if (preg_match("/^[^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ .*$/sDu", $f)) { $f=preg_replace("/^([^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+) .*$/sDu", "\\1", $f); }
	$f=trim(preg_replace("/s /su", " ", $f));
	$f=str_replace("'", "", preg_replace("/(\d)[\(\)[:space:]\-](\d)/s", "\\1\\2", preg_replace("/[[:space:]]+/s", " ", trim($f))));
	$f=preg_replace("/(\d)[\(\)[:space:]\-](\d)/s", "\\1\\2", $f);
	if (!$f) { return; }
	$slov=explode(" ", $f); $nslov=count($slov);
	$qw='';
	for ($j=0; $j<$nslov; $j++) {
		$word=trim($slov[$j]);
		@ $b=mb_substr($word, 0, 3, 'utf-8'); $c=md5($b);
		$c1=substr($c, 0, 1); $c2=substr($c, 1, 1); $c3=substr($c, 2, 1); $c=$c1.$c2.$c3;
		if ($qw) { $qw.=' or'; }
		$qw.=" b='$c'";
	}
	if (!$qw) { return; }
	$dbi=new SQLite3('/usr/home/pmm/firmscan/search/'.$lg.'/words.db');
	$word=$dbi->querySingle("select b from words where$qw order by n limit 1");
	$dbi=null;
	if (!$word) { return; }
	$s1=substr($word, 0, 1); $s2=substr($word, 1, 1); $s3=substr($word, 2, 1);
	$where=" where$whered search like '% ".str_replace(" ", "%' and search like '% ", $f)."%'";
	$where=preg_replace("/ (\d)/s", "\\1", $where);
	if (mb_strlen($word, 'utf-8')<3) { $where=""; }
	$db=new SQLite3('/usr/home/pmm/firmscan/search/'.$lg.'/'.$s1.'/'.$s2.'/'.$s3.'.db');
	$firms=$db->querySingle("select count(*) from companies$where");
	$db=null;
	if ($firms<1) { return; }
	return ', <a href="/'.$lg.'.php?f='.urlencode($fo).'&d='.urlencode($do).'">'.$firms.' '.podpis($firms).'</a> ('.strtoupper($lg).')';
}


# ****************************************************************
# Результаты поиска на русском языке
# ****************************************************************
function russian() {
	global $whered, $do, $fo;
	$lang=$lg; $f=$fo; $d=$do;
	$lg="ru"; if ($lang==$lg) { return; }
	$f=str_replace('ё', 'е', mb_strtolower($f, 'UTF-8')); 
	$f=preg_replace("/[\"'\/\_\+=,\\\.\#\$\!\?\*\^\&\[\]\|;\(\)[:space:]]+/s", " ", " $f ");
	$f=preg_replace("/ (в|без|до|из|к|на|по|о|от|перед|при|через|с|у|за|над|для|об|под|про|около|после|несмотря|спустя|г|ул|пр|и|или) /su", " ", $f);
	$f=trim($f);
	if (preg_match("/^[а-яa-z0-9]+ [а-яa-z0-9]+ [а-яa-z0-9]+ [а-яa-z0-9]+ [а-яa-z0-9]+ .*$/sDu", $f)) { $f=preg_replace("/^([а-яa-z0-9]+ [а-яa-z0-9]+ [а-яa-z0-9]+ [а-яa-z0-9]+ [а-яa-z0-9]+) .*$/sDu", "\\1", $f); }
	$f=trim(preg_replace("/\b([^[:space:]]{1,3})\b/su", "(\\1)", $f)); 
	$f=trim(preg_replace("/[аяиюыойуеьъ]{1,2} /su", " ", $f));
	$f=trim(preg_replace("/[\(\)]/su", "", $f));
	$f=str_replace("'", "", preg_replace("/(\d)[\(\)[:space:]\-](\d)/s", "\\1\\2", preg_replace("/[[:space:]]+/s", " ", trim($f))));
	$f=preg_replace("/(\d)[\(\)[:space:]\-](\d)/s", "\\1\\2", $f);
	if (!$f) { return; }
	$slov=explode(" ", $f); $nslov=count($slov);
	$qw='';
	for ($j=0; $j<$nslov; $j++) {
		$word=trim($slov[$j]);
		@ $b=mb_substr($word, 0, 3, 'utf-8'); $c=md5($b);
		$c1=substr($c, 0, 1); $c2=substr($c, 1, 1); $c3=substr($c, 2, 1); $c=$c1.$c2.$c3;
		if ($qw) { $qw.=' or'; }
		$qw.=" b='$c'";
	}
	if (!$qw) { return; }
	$dbi=new SQLite3('/usr/home/pmm/firmscan/search/'.$lg.'/words.db');
	$word=$dbi->querySingle("select b from words where$qw order by n limit 1");
	$dbi=null;
	if (!$word) { return; }
	$s1=substr($word, 0, 1); $s2=substr($word, 1, 1); $s3=substr($word, 2, 1);
	$where=" where$whered search like '% ".str_replace(" ", "%' and search like '% ", $f)."%'";
	$where=preg_replace("/ (\d)/s", "\\1", $where);
	if (mb_strlen($word, 'utf-8')<3) { $where=""; }
	$db=new SQLite3('/usr/home/pmm/firmscan/search/'.$lg.'/'.$s1.'/'.$s2.'/'.$s3.'.db');
	$firms=$db->querySingle("select count(*) from companies$where");
	$db=null;
	if ($firms<1) { return; }
	return ', <a href="/'.$lg.'.php?f='.urlencode($fo).'&d='.urlencode($do).'">'.$firms.' '.podpis($firms).'</a> ('.strtoupper($lg).')';
}


# ****************************************************************
# Результаты поиска на испанском языке
# ****************************************************************
function spanish() {
	global $whered, $do, $fo;
	$lang=$lg; $f=$fo; $d=$do;
	$lg="es"; if ($lang==$lg) { return; }
	$f=str_replace('ё', 'е', mb_strtolower($f, 'UTF-8')); 
	$f=preg_replace("/[\"'\/\_\+=,\\\.\#\$\!\?\*\^\&\[\]\|;\(\)[:space:]]+/s", " ", " $f ");
	$f=preg_replace("/ (http|www) /su", " ", $f);
	$f=trim($f);
	if (preg_match("/^[a-z0-9]+ [a-z0-9]+ [a-z0-9]+ [a-z0-9]+ [a-z0-9]+ .*$/sDu", $f)) { $f=preg_replace("/^([a-z0-9]+ [a-z0-9]+ [a-z0-9]+ [a-z0-9]+ [a-z0-9]+) .*$/sDu", "\\1", $f); }
	$f=trim(preg_replace("/\b([^[:space:]]{1,3})\b/su", "(\\1)", $f)); 
	$f=trim(preg_replace("/[s]{1,2} /su", " ", $f));
	$f=trim(preg_replace("/[\(\)]/su", "", $f));
	$f=str_replace("'", "", preg_replace("/(\d)[\(\)[:space:]\-](\d)/s", "\\1\\2", preg_replace("/[[:space:]]+/s", " ", trim($f))));
	$f=preg_replace("/(\d)[\(\)[:space:]\-](\d)/s", "\\1\\2", $f);
	if (!$f) { return; }
	$slov=explode(" ", $f); $nslov=count($slov);
	$qw='';
	for ($j=0; $j<$nslov; $j++) {
		$word=trim($slov[$j]);
		@ $b=mb_substr($word, 0, 3, 'utf-8'); $c=md5($b);
		$c1=substr($c, 0, 1); $c2=substr($c, 1, 1); $c3=substr($c, 2, 1); $c=$c1.$c2.$c3;
		if ($qw) { $qw.=' or'; }
		$qw.=" b='$c'";
	}
	if (!$qw) { return; }
	$dbi=new SQLite3('/usr/home/pmm/firmscan/search/'.$lg.'/words.db');
	$word=$dbi->querySingle("select b from words where$qw order by n limit 1");
	$dbi=null;
	if (!$word) { return; }
	$s1=substr($word, 0, 1); $s2=substr($word, 1, 1); $s3=substr($word, 2, 1);
	$where=" where$whered search like '% ".str_replace(" ", "%' and search like '% ", $f)."%'";
	$where=preg_replace("/ (\d)/s", "\\1", $where);
	if (mb_strlen($word, 'utf-8')<3) { $where=""; }
	$db=new SQLite3('/usr/home/pmm/firmscan/search/'.$lg.'/'.$s1.'/'.$s2.'/'.$s3.'.db');
	$firms=$db->querySingle("select count(*) from companies$where");
	$db=null;
	if ($firms<1) { return; }
	return ', <a href="/'.$lg.'.php?f='.urlencode($fo).'&d='.urlencode($do).'">'.$firms.' '.podpis($firms).'</a> ('.strtoupper($lg).')';
}


# ****************************************************************
# Результаты поиска на французском языке
# ****************************************************************
function french() {
	global $whered, $do, $fo;
	$lang=$lg; $f=$fo; $d=$do;
	$lg="fr"; if ($lang==$lg) { return; }
	$f=mb_strtolower($f, 'UTF-8'); 
	$f=preg_replace("/[\"'\/\_\+=,\\\.\#\$\!\?\*\^\&\[\]\|;\(\)[:space:]]+/s", " ", " $f ");
	$f=preg_replace("/ (http|www|à|après|avant|avec|chez|contre|dans|de|dedans|depuis|derrière|dès|dessous|dessus|devant|durant|en|entre|envers|environ|excepté|hors|hormis|jusque|malgré|outre|par|par-delà|parmi|pendant|pour|sans|sauf|selon|sous|sur|tandis|vers|vers|us|via|vu|un|une|des|le|la|les|du|d) /su", " ", $f);
	$f=trim($f);
	if (preg_match("/^[a-z0-9]+ [a-z0-9]+ [a-z0-9]+ [a-z0-9]+ [a-z0-9]+ .*$/sDu", $f)) { $f=preg_replace("/^([a-z0-9]+ [a-z0-9]+ [a-z0-9]+ [a-z0-9]+ [a-z0-9]+) .*$/sDu", "\\1", $f); }
	$f=trim(preg_replace("/\b([^[:space:]]{1,3})\b/su", "(\\1)", $f)); 
	$f=trim(preg_replace("/[seau]{1,2} /su", " ", $f));
	$f=trim(preg_replace("/[\(\)]/su", "", $f));
	$f=str_replace("'", "", preg_replace("/(\d)[\(\)[:space:]\-](\d)/s", "\\1\\2", preg_replace("/[[:space:]]+/s", " ", trim($f))));
	$f=preg_replace("/(\d)[\(\)[:space:]\-](\d)/s", "\\1\\2", $f);
	if (!$f) { return; }
	$slov=explode(" ", $f); $nslov=count($slov);
	$qw='';
	for ($j=0; $j<$nslov; $j++) {
		$word=trim($slov[$j]);
		@ $b=mb_substr($word, 0, 3, 'utf-8'); $c=md5($b);
		$c1=substr($c, 0, 1); $c2=substr($c, 1, 1); $c3=substr($c, 2, 1); $c=$c1.$c2.$c3;
		if ($qw) { $qw.=' or'; }
		$qw.=" b='$c'";
	}
	if (!$qw) { return; }
	$dbi=new SQLite3('/usr/home/pmm/firmscan/search/'.$lg.'/words.db');
	$word=$dbi->querySingle("select b from words where$qw order by n limit 1");
	$dbi=null;
	if (!$word) { return; }
	$s1=substr($word, 0, 1); $s2=substr($word, 1, 1); $s3=substr($word, 2, 1);
	$where=" where$whered search like '% ".str_replace(" ", "%' and search like '% ", $f)."%'";
	$where=preg_replace("/ (\d)/s", "\\1", $where);
	if (mb_strlen($word, 'utf-8')<3) { $where=""; }
	$db=new SQLite3('/usr/home/pmm/firmscan/search/'.$lg.'/'.$s1.'/'.$s2.'/'.$s3.'.db');
	$firms=$db->querySingle("select count(*) from companies$where");
	$db=null;
	if ($firms<1) { return; }
	return ', <a href="/'.$lg.'.php?f='.urlencode($fo).'&d='.urlencode($do).'">'.$firms.' '.podpis($firms).'</a> ('.strtoupper($lg).')';
}


# ****************************************************************
# Результаты поиска на немецком языке
# ****************************************************************
function german() {
	global $whered, $do, $fo;
	$lang=$lg; $f=$fo; $d=$do;
	$lg="de"; if ($lang==$lg) { return; }
	$f=mb_strtolower($f, 'UTF-8');
	$f=preg_replace("/[\"'\/\_\+=,\\\.\#\$\!\?\*\^\&\[\]\|;\(\)[:space:]]+/s", " ", " $f ");
	$f=preg_replace("/ (http|www|at|in|on|from|to|since|till|into|onto|before|after|about|for|during|of|with|by|between|among|except|besides|over|below|out|behind|it|is|are|the|a) /su", " ", $f);
	$f=trim($f);
	if (preg_match("/^[^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ .*$/sDu", $f)) { $f=preg_replace("/^([^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+) .*$/sDu", "\\1", $f); }
	$f=trim(preg_replace("/s /su", " ", $f));
	$f=str_replace("'", "", preg_replace("/(\d)[\(\)[:space:]\-](\d)/s", "\\1\\2", preg_replace("/[[:space:]]+/s", " ", trim($f))));
	$f=preg_replace("/(\d)[\(\)[:space:]\-](\d)/s", "\\1\\2", $f);
	if (!$f) { return; }
	$slov=explode(" ", $f); $nslov=count($slov);
	$qw='';
	for ($j=0; $j<$nslov; $j++) {
		$word=trim($slov[$j]);
		@ $b=mb_substr($word, 0, 3, 'utf-8'); $c=md5($b);
		$c1=substr($c, 0, 1); $c2=substr($c, 1, 1); $c3=substr($c, 2, 1); $c=$c1.$c2.$c3;
		if ($qw) { $qw.=' or'; }
		$qw.=" b='$c'";
	}
	if (!$qw) { return; }
	$dbi=new SQLite3('/usr/home/pmm/firmscan/search/'.$lg.'/words.db');
	$word=$dbi->querySingle("select b from words where$qw order by n limit 1");
	$dbi=null;
	if (!$word) { return; }
	$s1=substr($word, 0, 1); $s2=substr($word, 1, 1); $s3=substr($word, 2, 1);
	$where=" where$whered search like '% ".str_replace(" ", "%' and search like '% ", $f)."%'";
	$where=preg_replace("/ (\d)/s", "\\1", $where);
	if (mb_strlen($word, 'utf-8')<3) { $where=""; }
	$db=new SQLite3('/usr/home/pmm/firmscan/search/'.$lg.'/'.$s1.'/'.$s2.'/'.$s3.'.db');
	$firms=$db->querySingle("select count(*) from companies$where");
	$db=null;
	if ($firms<1) { return; }
	return ', <a href="/'.$lg.'.php?f='.urlencode($fo).'&d='.urlencode($do).'">'.$firms.' '.podpis($firms).'</a> ('.strtoupper($lg).')';
}


# ****************************************************************
# Результаты поиска на чешском языке
# ****************************************************************
function czech() {
	global $whered, $do, $fo;
	$lang=$lg; $f=$fo; $d=$do;
	$lg="cz"; if ($lang==$lg) { return; }
	$f=mb_strtolower($f, 'UTF-8');
	$f=preg_replace("/[\"'\/\_\+=,\\\.\#\$\!\?\*\^\&\[\]\|;\(\)[:space:]]+/s", " ", " $f ");
	$f=preg_replace("/ (в|без|до|из|к|на|по|о|от|перед|при|через|с|у|за|над|для|об|под|про|около|после|несмотря|спустя|г|ул|пр|и|или) /su", " ", $f);
	$f=trim($f);
	if (preg_match("/^[^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ .*$/sDu", $f)) { $f=preg_replace("/^([^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+) .*$/sDu", "\\1", $f); }
	$f=trim(preg_replace("/\b([^[:space:]]{1,3})\b/su", "(\\1)", $f)); 
	$f=trim(preg_replace("/[аяиюыойуеьъ]{1,2} /su", " ", $f));
	$f=trim(preg_replace("/[\(\)]/su", "", $f));
	$f=str_replace("'", "", preg_replace("/(\d)[\(\)[:space:]\-](\d)/s", "\\1\\2", preg_replace("/[[:space:]]+/s", " ", trim($f))));
	$f=preg_replace("/(\d)[\(\)[:space:]\-](\d)/s", "\\1\\2", $f);
	if (!$f) { return; }
	$slov=explode(" ", $f); $nslov=count($slov);
	$qw='';
	for ($j=0; $j<$nslov; $j++) {
		$word=trim($slov[$j]);
		@ $b=mb_substr($word, 0, 3, 'utf-8'); $c=md5($b);
		$c1=substr($c, 0, 1); $c2=substr($c, 1, 1); $c3=substr($c, 2, 1); $c=$c1.$c2.$c3;
		if ($qw) { $qw.=' or'; }
		$qw.=" b='$c'";
	}
	if (!$qw) { return; }
	$dbi=new SQLite3('/usr/home/pmm/firmscan/search/'.$lg.'/words.db');
	$word=$dbi->querySingle("select b from words where$qw order by n limit 1");
	$dbi=null;
	if (!$word) { return; }
	$s1=substr($word, 0, 1); $s2=substr($word, 1, 1); $s3=substr($word, 2, 1);
	$where=" where$whered search like '% ".str_replace(" ", "%' and search like '% ", $f)."%'";
	$where=preg_replace("/ (\d)/s", "\\1", $where);
	if (mb_strlen($word, 'utf-8')<3) { $where=""; }
	$db=new SQLite3('/usr/home/pmm/firmscan/search/'.$lg.'/'.$s1.'/'.$s2.'/'.$s3.'.db');
	$firms=$db->querySingle("select count(*) from companies$where");
	$db=null;
	if ($firms<1) { return; }
	return ', <a href="/'.$lg.'.php?f='.urlencode($fo).'&d='.urlencode($do).'">'.$firms.' '.podpis($firms).'</a> ('.strtoupper($lg).')';
}


# ****************************************************************
# Результаты поиска на латышском языке
# ****************************************************************
function latvian() {
	global $whered, $do, $fo;
	$lang=$lg; $f=$fo; $d=$do;
	$lg="lv"; if ($lang==$lg) { return; }
	$f=mb_strtolower($f, 'UTF-8');
	$f=preg_replace("/[\"'\/\_\+=,\\\.\#\$\!\?\*\^\&\[\]\|;\(\)[:space:]]+/s", " ", " $f ");
	$f=preg_replace("/ (uz|pie|no|aiz|virs|zem|pirms|vai|pēc|bez|dēļ|ar|par|pār|gar|pret|starp|ра|ap|ko|līdz|līdzi|caur) /su", " ", $f);
	$f=trim($f);
	if (preg_match("/^[^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ .*$/sDu", $f)) { $f=preg_replace("/^([^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+) .*$/sDu", "\\1", $f); }
	$f=trim(preg_replace("/\b([^[:space:]]{1,3})\b/su", "(\\1)", $f)); 
	$f=trim(preg_replace("/[aāeēiījouūs]{1,2} /su", " ", $f));
	$f=trim(preg_replace("/[\(\)]/su", "", $f));
	$f=str_replace("'", "", preg_replace("/(\d)[\(\)[:space:]\-](\d)/s", "\\1\\2", preg_replace("/[[:space:]]+/s", " ", trim($f))));
	$f=preg_replace("/(\d)[\(\)[:space:]\-](\d)/s", "\\1\\2", $f);
	if (!$f) { return; }
	$slov=explode(" ", $f); $nslov=count($slov);
	$qw='';
	for ($j=0; $j<$nslov; $j++) {
		$word=trim($slov[$j]);
		@ $b=mb_substr($word, 0, 3, 'utf-8'); $c=md5($b);
		$c1=substr($c, 0, 1); $c2=substr($c, 1, 1); $c3=substr($c, 2, 1); $c=$c1.$c2.$c3;
		if ($qw) { $qw.=' or'; }
		$qw.=" b='$c'";
	}
	if (!$qw) { return; }
	$dbi=new SQLite3('/usr/home/pmm/firmscan/search/'.$lg.'/words.db');
	$word=$dbi->querySingle("select b from words where$qw order by n limit 1");
	$dbi=null;
	if (!$word) { return; }
	$s1=substr($word, 0, 1); $s2=substr($word, 1, 1); $s3=substr($word, 2, 1);
	$where=" where$whered search like '% ".str_replace(" ", "%' and search like '% ", $f)."%'";
	$where=preg_replace("/ (\d)/s", "\\1", $where);
	if (mb_strlen($word, 'utf-8')<3) { $where=""; }
	$db=new SQLite3('/usr/home/pmm/firmscan/search/'.$lg.'/'.$s1.'/'.$s2.'/'.$s3.'.db');
	$firms=$db->querySingle("select count(*) from companies$where");
	$db=null;
	if ($firms<1) { return; }
	return ', <a href="/'.$lg.'.php?f='.urlencode($fo).'&d='.urlencode($do).'">'.$firms.' '.podpis($firms).'</a> ('.strtoupper($lg).')';
}


# ****************************************************************
# Результаты поиска на узбекском языке
# ****************************************************************
function uzbek() {
	global $whered, $do, $fo;
	$lang=$lg; $f=$fo; $d=$do;
	$lg="uz"; if ($lang==$lg) { return; }
	$f=mb_strtolower($f, 'UTF-8'); $f=str_replace("'", "`", $f);
	$f=preg_replace("/[\"'\/\_\+=,\\\.\#\$\!\?\*\^\&\[\]\|;\(\)[:space:]]+/s", " ", " $f ");
	$f=preg_replace("/ (http|www|at|in|on|from|to|since|till|into|onto|before|after|about|for|during|of|with|by|between|among|except|besides|over|below|out|behind|it|is|are|the|a) /su", " ", $f);
	$f=trim($f);
	if (preg_match("/^[^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ .*$/sDu", $f)) { $f=preg_replace("/^([^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+) .*$/sDu", "\\1", $f); }
	$f=trim(preg_replace("/s /su", " ", $f));
	$f=str_replace("'", "", preg_replace("/(\d)[\(\)[:space:]\-](\d)/s", "\\1\\2", preg_replace("/[[:space:]]+/s", " ", trim($f))));
	$f=preg_replace("/(\d)[\(\)[:space:]\-](\d)/s", "\\1\\2", $f);
	if (!$f) { return; }
	$slov=explode(" ", $f); $nslov=count($slov);
	$qw='';
	for ($j=0; $j<$nslov; $j++) {
		$word=trim($slov[$j]);
		@ $b=mb_substr($word, 0, 3, 'utf-8'); $c=md5($b);
		$c1=substr($c, 0, 1); $c2=substr($c, 1, 1); $c3=substr($c, 2, 1); $c=$c1.$c2.$c3;
		if ($qw) { $qw.=' or'; }
		$qw.=" b='$c'";
	}
	if (!$qw) { return; }
	$dbi=new SQLite3('/usr/home/pmm/firmscan/search/'.$lg.'/words.db');
	$word=$dbi->querySingle("select b from words where$qw order by n limit 1");
	$dbi=null;
	if (!$word) { return; }
	$s1=substr($word, 0, 1); $s2=substr($word, 1, 1); $s3=substr($word, 2, 1);
	$where=" where$whered search like '% ".str_replace(" ", "%' and search like '% ", $f)."%'";
	$where=preg_replace("/ (\d)/s", "\\1", $where);
	if (mb_strlen($word, 'utf-8')<3) { $where=""; }
	$db=new SQLite3('/usr/home/pmm/firmscan/search/'.$lg.'/'.$s1.'/'.$s2.'/'.$s3.'.db');
	$firms=$db->querySingle("select count(*) from companies$where");
	$db=null;
	if ($firms<1) { return; }
	return ', <a href="/'.$lg.'.php?f='.urlencode($fo).'&d='.urlencode($do).'">'.$firms.' '.podpis($firms).'</a> ('.strtoupper($lg).')';
}
?>