<?php
$hache_salt="kq1azgj2fld3sUHF4dUw5dcf86egf7vf8dgbkfg9nnvi0mrhoufgjt893y2k59tllmudvpqi0IOoOHNpVRP";
$siteurl="https://santehnika-online.ru";
$sitemaps=Array("$siteurl/sitemap_1.xml","$siteurl/sitemap_2.xml","$siteurl/sitemap_3.xml","$siteurl/sitemap_4.xml"); //ссылки откуда брать карты сайта, может быть https
$must_have=Array("laufen", "duravit", "ideal_standart", "keramag","jacob_delafon", "jica","roca","villeroy", "hansgroe", "axor", "oras", "novellini", "ido", "ravak", "huppe", "kermi", "geberit", "grohe", "viega"); //В урле должно содержаться это, иначе не брать
$must_have2=Array(); //В урле должно содержаться это
$site_encoding="UTF-8"; //Исходная кодировка сайта
$save_page_enc="UTF-8"; //кодировка в которой сохранять страницы
$codepage="windows-1251"; //кодировка EWC
$strip_before="<div itemscope itemtype=\"http://schema.org/Product\">"; //брать контент отсюда
$strip_after="<div id='tovdob' class=\"notupdatebyshow\">"; //брать контент до сюда
$Delimiter="|";
$defbrand="";
$tvalues=Array(
"yes"=>"Есть",
"no"=>"Нет",
" kg"=>"",

);
$trans =Array (
" itemscope " => " ",
"src=\"/" => "src=\"$siteurl/",
"href=\"/" => "href=\"$siteurl/",
"href=\"assets/" => "href=\"$siteurl/assets/",
"alt=\"\" title=\"\" class=\"img-responsive\"" => "",
"<a href=\"javascript:window.print(); void 0;\" class=\"col-xs-6 icon-box\"><i class=\"icon-1\"></i><span>Распечатать</span></a>" => "",
); //заменять одно на другое
$dirtrans =Array (
"Каталог//Настенные газовые котлы" => "Котлы//Настенные газовые котлы",
); //заменять одно на другое

$Sku_container="<div class=\"col-md-12 readmore\">(.*)</div></div>"; //Контейнер где находятся характеристики
$Sku_row="<tr><td class=\"col-xs-7\"> (.*): </td><td> (.*)  </td></tr>";
$Arrays =Array (
"section" => "<ol class=\"breadcrumb\" itemtype=\"http://schema.org/BreadcrumbList\">(.*)</ol>".$Delimiter."<span itemprop=\"name\">(.*)</span>",
); //какие массивы возвращать
$Strokes =Array (
"name" => "<h1 itemprop=\"name\">(.*)</h1>",
"art" => "<tr><td class=\"col-xs-7\"> Артикул: </td><td> (.*) </td></tr>",
"description" => "<div class=\"col-md-6 info-box\" itemprop=\"description\">(.*)</div>",
); //какие строки возвращать
$Nums =Array (
"price" => "meta itemprop=\"price\" content=\"(.*)\">",
); //какие цифры возвращать
$Images=Array (
"foto" => "<div class=\"slider slider-for\"><div class=\"item\">(.*)</div>".$Delimiter."<a>",
"carousel" => "<div class=\"slider slider-for\">(.*)</div><div class=\"slider slider-nav\">".$Delimiter."<a>",
); //Картинки
$Attaches=Array (
"attach" => "<div class=\"row hidden-xs hidden-sm\">(.*)</div>".$Delimiter."<a>".$Delimiter."<span>(.*)</span>",
);
//functions
set_time_limit(0);

function parse_html($data) {
global $Sku_container;
global $Sku_row;
global $Attaches;
global $Arrays;
global $Strokes;
global $Nums;
global $Images;
global $Delimiter;
global $ves;
global $dirtrans;
$ves="";
$ret=Array();
foreach($Arrays as $key => $v) {
unset($retkey);
$retkey=ExtrArray($data,$v);
foreach ($dirtrans as $dirs => $kd) {
$retkey=str_replace($dirs, $kd, $retkey);
}

if($key=="section") { unset($tmp); $tmp=explode("//",$retkey); array_shift($tmp);array_shift($tmp);array_pop($tmp); $ret["catalog"]=array_shift($tmp); $retkey=implode("//",$tmp); }
$ret[$key]=$retkey;
}
foreach($Strokes as $key => $v) {
$ret[$key]=Extr($data,$v);
}
foreach($Nums as $key => $v) {
$ret[$key]=(double)str_replace(",",".",str_replace(" ","",strip_tags(Extr($data,$v))));
}
foreach($Images as $key => $v) {
$ret[$key]=ExtrImg($data,$v);
}
foreach($Attaches as $key => $v) {
$ret[$key]=ExtrAttaches($data,$v);
}
$ret['sku']=ExtrSku($data,$Sku_container,$Sku_row);
$ret['ves']=$ves;
return $ret;
}
function ExtrArray($str, $row) {
global $Delimiter;
$ret=Array();
$e=explode($Delimiter, $row);
$z1=explode("(.*)",$e[0]);
$z2=explode("(.*)",$e[1]);
$tmp=explode($z1[0],$str);
$tmp2=explode($z1[1],$tmp[1]);
unset($z1,$tmp);
$tmp=explode($z2[0],$tmp2[0]);
unset($tmp2);
$ff=0;
foreach ($tmp as $val) {
$tmp2=explode($z2[1],$val);
$ret[$ff]=trim(trim(strip_tags($tmp2[0])));
$ff++;
}
return implode("//",$ret);
}

function ExtrSku($str, $container,$row) {
global $ves;
$z=explode("(.*)",$container);
$tmp=explode($z[0],$str);
$tmp2=explode($z[1],$tmp[1]);
unset($z,$tmp);
$z=explode("(.*)",$row);
$tmp=explode($z[0],$tmp2[0]);
unset($tmp2);
foreach ($tmp as $val) {
$tmp2=explode($z[1],$val);
unset($tmp3);
$tmp3=explode($z[2],$tmp2[1]);
$tmp2[0]=trim(trim(strip_tags($tmp2[0])));
if($tmp2[0]!="") {
$skuval=trim(trim(strip_tags($tmp3[0])));
unset($tmp4);
$tmp4=explode(" ",$skuval);
$skunit="";
if(("".doubleval($tmp4[0])==$tmp4[0])||($tmp4[1]=="мм")||($tmp4[1]=="шт")||($tmp4[1]=="л")) { $skuval=$tmp4[0]; array_shift($tmp4); $skunit=implode(" ",$tmp4); }
if($tmp2[0]=="Вес") { $ves="".$skuval;} else {
$ret.=$tmp2[0].":".$skuval.":".$skunit."^";
}
}
}
return $ret;
}
function utf8win1251($data) {
$data=str_replace(chr(0xa),"",$data);
$data=str_replace("45?","45 град.",$data);
$data=str_replace("„","\"",$data);
$data=str_replace("''","\"",$data);
$data=str_replace("«","\"",$data);
$data=str_replace("»","\"",$data);
$data=str_replace("90?","90 град.",$data);
$data=str_replace("°"," град.",$data);
$data=str_replace("º"," град.",$data);
$data=str_replace("±","+/-",$data);
$data=str_replace("÷","+/-",$data);
$data=str_replace("²",".кв.",$data);
$data=str_replace("³",".куб.",$data);
$data=str_replace("×","х",$data);
$data=str_replace("1\\\"½","1.1/2 дюйма",$data);
$data=str_replace("1? (44-50 мм)","1.1/2 дюйма (44-50 мм)",$data);
$data=str_replace("2\\\"½","2.1/2 дюйма",$data);
$data=str_replace("2? (75-80 мм)","2.1/2 дюйма (75-80 мм)",$data);
$data=str_replace("1\\\"¼","1.1/4 дюйма",$data);
$data=str_replace("1? (38-44 мм)","1.1/4 дюйма (38-44 мм)",$data);
$data=str_replace("½\\\"","1/2 дюйма",$data);
$data=str_replace("? (18-24 мм)","1/2 дюйма (18-24 мм)",$data);
$data=str_replace("¾\\\"","3/4 дюйма",$data);
$data=str_replace("? (25-30 мм)","3/4 дюйма (25-30 мм)",$data);
$data=str_replace("1и1/2\\\"", "1.1/2 дюйма",$data);
$data=str_replace("2и1/2\\\"", "2.1/2 дюйма",$data);
$data=str_replace("1и1/4\\\"", "1.1/4 дюйма",$data);
$data=str_replace("1и3/4\\\"", "1.3/4 дюйма",$data);
$data=str_replace("1-1/2\\\"", "1.1/2 дюйма",$data);
$data=str_replace("2-1/2\\\"", "2.1/2 дюйма",$data);
$data=str_replace("1-1/4\\\"", "1.1/4 дюйма",$data);
$data=str_replace("1-3/4\\\"", "1.3/4 дюйма",$data);
$data=str_replace("d?","d=",$data);
$data=str_replace("¼","1/4",$data);
$data=str_replace("½","1/2",$data);
$data=str_replace("¾","3/4",$data);
$data=str_replace("ø","диам.",$data);
$data=str_replace("Ø","диам.",$data);
$data=str_replace("Ü","U",$data);
$data=str_replace("ü","U",$data);
$data=str_replace("Ω","Ом",$data);
$data=str_replace("©","&copy;",$data);
$data=str_replace("®","",$data);
$data=str_replace("™","",$data);
$data=str_replace("—","-",$data);

$data = iconv('UTF-8', 'WINDOWS-1251//IGNORE', $data);
$data=trim(str_replace ("  "," ",$data));
return $data;
}

function validate_regular($regular) {
$regular=str_replace("\\","\\\\", $regular);
$regular=str_replace("\"","\\\"", $regular);
$regular=str_replace("/","\\/", $regular);
$regular=str_replace("'","\'", $regular);
$regular=str_replace("_","\_", $regular);
$regular=str_replace("<","\<", $regular);
$regular=str_replace(">","\>", $regular);
$regular=str_replace(" ","\ ", $regular);
$regular=str_replace("=","\=", $regular);
$regular=str_replace("-","\-", $regular);
$regular=str_replace("#","\#", $regular);
$regular=str_replace(":","\:", $regular);
$regular=str_replace(";","\;", $regular);
return "/".$regular."/U";
}

function win2utf($data) {
$data=str_replace(0x0A,"",$data);
$data = iconv('WINDOWS-1251', 'UTF-8//IGNORE', $data);
$data=str_replace(0x0A,"",$data);
return $data;
}
function strtoken($str, $char) {
$strtoma=Array();
$strtoma=explode($char,$str);
return $strtoma[0];
}
function ExtractString($str, $start, $end) {
$tmp=explode($start,$str);
$tmp2=explode($end,$tmp[1]);
$ret=$tmp2[0];
return $ret;
}
function Extr($str, $v) {
$z=explode("(.*)",$v);
$tmp=explode($z[0],$str);
$tmp2=explode($z[1],$tmp[1]);
$ret=$tmp2[0];
return trim(strip_tags($ret,"<strong><i><span><div><p><b><ul><ol><li>"));
}
function ExtrImg($str, $row) {
global $Delimiter;
$e=explode($Delimiter,$row);
$z=explode("(.*)",$e[0]);
$tmp=explode($z[0],$str);
$tmp2=explode($z[1],$tmp[1]);
$ret=trim(strip_tags($tmp2[0],$e[1]));
unset($tmp,$tmp2);
$sr="src=";
if($e[1]=="<a>") { $sr="href="; }
$tmp=explode($sr,$ret);
array_shift($tmp);
unset($ret);
$ret=Array();
$f=0;
foreach($tmp as $v) {
if($v!="") {
$v=strtoken(strtoken(str_replace("\"","",str_replace("'","",$v))," "),">");
$ret[$f]=$v;
$f++;
}
}
return implode("^",$ret);
}
function ExtrAttaches($str, $row) {
global $Delimiter;
$e=explode($Delimiter,$row);
$z=explode("(.*)",$e[0]);
$z2=explode("(.*)",$e[2]);
$tmp=explode($z[0],$str);
$tmp2=explode($z[1],$tmp[1]);
$ret=trim(strip_tags($tmp2[0],$e[1]));
$ret2=trim($tmp2[0]);
unset($tmp,$tmp2);
$tmp=explode("href=",$ret);
array_shift($tmp);
unset($ret);
$ret=Array();

$f=0;
unset($tmp2);
$tmp4=explode($z2[0],$ret2);
array_shift($tmp4);
$f=0;
foreach($tmp as $v) {
if($v!="") {
$v=strtoken(strtoken(str_replace("\"","",str_replace("'","",$v))," "),">");
$ret[$f]=trim(strip_tags($tmp4[$f])).":".$v;
$f++;
}
}
return implode("^",$ret);
}
function normalize($data) {
$data=str_replace(" >",">", str_replace("> <", "><", str_replace("  "," ", str_replace("  "," ", str_replace("\r","",str_replace("\n","",str_replace("  "," ",str_replace("  "," ",str_replace("  "," ",str_replace("  "," ",str_replace(chr(0x9)," ",$data)))))))))));
return $data;
}
function geturl($URL,$ref='') {
$ua = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.13) Gecko/20101203 MRA 5.7 (build 03796) Firefox/3.6.13';
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
if($ref=='') { $ref=$URL; }
curl_setopt($ch, CURLOPT_REFERER, $ref);
// HTTPS
if(preg_match("#https#is",$URL)){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
ob_start();  //чтобы не выводить контент
curl_exec($ch);
echo $data;
curl_close ($ch);
$data=ob_get_contents();
ob_end_clean();
return $data;
}

function nourl($url) {
unset($tmp);
$url=str_replace("//","",str_replace("https://","",str_replace("http://","",$url)));
$tmp=explode("/",$url);
array_shift($tmp);
return(implode("_",$tmp));
}


function save_get_file($URL='', $filename='', $cookie='', $ref='')
{
if (strlen($URL)<=0) return false;
if($filename=='') {
$filename = './'.date('YmdHis_').rand(99,9999999).'.xml';
}
$fp = fopen($filename, 'w');
if (!$fp)
return false;
else
{
$ua = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.13) Gecko/20101203 MRA 5.7 (build 03796) Firefox/3.6.13';
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
if($ref=='') { $ref=$URL; }
curl_setopt($ch, CURLOPT_REFERER, $ref);
// HTTPS
if(preg_match("#https#is",$URL)){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
curl_setopt($ch, CURLOPT_FILE, $fp); // чтобы выгрузить в файл;
if (strlen($cookie)>0)
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_exec ($ch);
curl_close ($ch);
return $filename;
}
}

if(!function_exists('toLower')) {
function toLower($str) {
$str = strtr($str, "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЪЫЭЮЯ",
"абвгдеёжзийклмнопрстуфхцчшщьъыэюя");
   return strtolower($str);
}
}
function dirs($dir) {
global $tdirs;
if(isset($tdirs[$dir])) { return $tdirs[$dir];}
return ($dir);
}
function params($par) {
global $tparams;
if(isset($tparams[$par])) { return $tparams[$par];}
return ($par);
}
function parsexmlstr($tag,$xml,$fpim,$fpse) {
global $filt;
global $classes;
global $units;
global $rcatalog;
global $rcatalog2;
global $icatalog;
global $translate;
global $par;
global $imfp;
$product = new SimpleXMLElement($xml);
$r=Array();
//return object
//print_r($product);
$subcatname=dirs(trim($product->categories->category[0]));
$tmp=Array();
$k=0;
foreach ($product->categoryRelations->categoryRelation as $cl) {
//print_r($cl);
$cln=dirs(trim($cl->name));
if(@$translate[md6($cln)]=="") {$translate[md6($cln)]=$cln;}
if($k==0) {$catname=$cln;} else {
//echo $k." ".$cl->name."<br>";
if (($k>3)||($cln==$catname)) { break;}
$tmp[$k]=$cln;
}
$k++;

}
if(count($tmp)>=1) {$subcatname=implode("//",$tmp);}

$r[0]=""; //RAEC ART
//$r[1]=str_replace("&quot;","\"",$catname);
if($catname=="") {$catname="Разное";}
if($subcatname=="") {$subcatname="Разное";}
$r[1]=$catname;
$r[2]=$subcatname;
$cname=translit(utf8win1251($r[1]."_".$r[2]."_"));
$class=md6($cname);
//echo $catname." ".$subcatname." ".$cname; exit;


if($class!="") {
$classes[$class]=$cname."|".utf8win1251(trim(str_replace("  ", " ",trim($r[1])))."|".trim(str_replace("  ", " ",$catname))."|".trim(str_replace("  ", " ",$subcatname)));
unset($facets);
//echo $classes[$class];
$facets=str_split($class);
$fdir="";
foreach($facets as $findex) {
$fdir.="/".$findex;
if (is_dir("./etim".$fdir)==FALSE) { mkdir("./etim".$fdir,0755); }
}
//echo "./etim".$fdir."/".$class.".txt";
if(!file_exists("./etim".$fdir."/".$class.".txt")) {
$ffs=fopen("./etim".$fdir."/".$class.".txt","w");
fputs($ffs, utf8win1251(trim(str_replace("  ", " ",$catname))));
fclose($ffs);
}


$r[3]=trim(str_replace("  ", " ",$product->number));
$price=0;
if(isset($product->price)) { $price=doubleval($product->price);}
$r[4]=$price;
$r[5]=$price;
$r[6]=$product->slug; //ART
unset ($fc);
unset($pictogram);
$pikts="";
$piktograms="";
if(isset($product->piktograms)) {
foreach ($product->piktograms as $fc) {
foreach ($fc as $pictogram) {
$pikts=trim($pictogram[0]);
if($pikts!="") {
echo $pikts."\n";
fputs ($imfp,$pikts);
unset($u);
$u=explode("/",$pikts);
$pict=array_pop($u);
if(!file_exists("./f/$pict")) {
if(!copy($pikts,"./f/$pict")) {
echo "<font color=red>$pikts</font> upload error\n";
}
}
if($pict!="") {
$piktograms.="<img src='f/$pict'>";
}
}
}
}
}
echo "\n";
if($piktograms!="") {$piktograms="<div class=piktograms>$piktograms</div>";}
$r[7]=$piktograms.trim($product->feature);
$r[8]="";
$foto1="";
$foto2="";
$s=0;
unset($fc);
unset($pic);
if(isset($product->pictures)) {
foreach ($product->pictures as $fc) {
foreach ($fc as $pic) {
$pic=trim($pic);
if($pic!="") {
fputs ($imfp,$pic);
unset($u);
$u=explode("/",$pic);
$pict=array_pop($u);
if(!file_exists("./f/$pict")) {
if(!copy($pic,"./f/$pict")) {
echo "<font color=red>$pic</font> upload error\n";
}
}
if($pict!="") {
if($s==0){
$foto1="<img src='f/$pict'>";
$foto2.="<img src='f/$pict'>";
} else {
$foto2.="<img src='f/$pict'>";
}
$s++;
}
}
}
}
}
$foto2=str_replace("><","><br><",$foto2);
$r[9]=$foto1;
$r[10]=$foto2;
$r[11]="";
$r[12]="1EUR";
$r[13]=$r[1];
$r[14]="";

$specs="";
$weight=0;
$i=1;
if (is_dir("./filters/$class")==FALSE) { mkdir("./filters/$class",0755); }
if(!isset($filt[$class])) {
$handle=opendir("./filters/$class/");
while (($file = readdir($handle))!==FALSE) {
if(substr($file,-4)==".txt") {
$idx=substr($file,0,-4);
$fpf=fopen("./filters/$class/".$file,"rt");
$filt[$class][$idx]=@fgets($fpf,@filesize("./filters/$class/".$file));
fclose($fpf);
}
}
closedir($handle);
}
unset($fc);
$cur=Array();
if(isset($product->parameters[0])) {
foreach ($product->parameters[0] as $fc) {
$fn=params(trim($fc->name));
$fcn=md6($fn);
if(@$par[$fcn]=="") {$par[$fcn]=$fn;}
$etim="";
$unit="";
$value="";
if(($fn=="Weight")||($fn=="Вес")) { $weight=str_replace(",",".",trim($fc->value));}
if(isset($fc->value)) {$value=$fc->value;}
if(($value=="true")||($value=="yes")) {$value="Есть";}
if(($value=="false")||($value=="no")) {$value="Нет";}
if($value=="*") {$value="не определено";}
if($value=="-") {$value="";}
//save etim DB

$etim=$fcn;

if($etim!="") {

unset($facets);
$facets=str_split($etim);
$fdir="";

foreach($facets as $findex) {
$fdir.="/".$findex;
if (is_dir("./etim".$fdir)==FALSE) { mkdir("./etim".$fdir,0755); }
}

if(!file_exists("./etim".$fdir."/".$etim.".txt")) {
$ffs=fopen("./etim".$fdir."/".$etim.".txt","w");
fputs($ffs, utf8win1251($fn."|||"));
fclose($ffs);
}

if(!file_exists("./filters/".$class."/".$etim.".txt")) {

$filtval=utf8win1251($fn."|||");
$ffs=fopen("./filters/".$class."/".$etim.".txt","w");
fputs($ffs, $filtval);
fclose($ffs);
$filt[$class][$etim]=$filtval;

}

$cur[$etim]=trim($value);
}

$i++;
}
}
if(isset($filt[$class])) {
foreach ($filt[$class] as $key => $etm) {
$specs.=$cur[$key]."|";
}
}




$sert="";
$r[15]=trim($product->url);
$stock=0;
if(isset($product->foreignstock)) {$stock=(int)$product->foreignstock;}
$r[16]=$stock;
$r[17]=$weight; //WEIGHT
$r[18]=""; //VOLUME
$r[19]=""; //НДС
$r[20]="";
$r[21]=""; //Подраздел4
$r[22]=""; //Meta Title
$r[23]=""; //Meta Description
$r[24]=""; //Meta Keywords
$r[25]=""; //Минимальный заказ шт
$r[26]=""; //g:country
$r[27]=""; //g:service
$r[28]=""; //g:price
$r[29]=""; //g:google_product_category
$r[30]=trim(str_replace("  ", " ",trim($product->description))); //g:product_type
$r[31]=trim(str_replace("  ", " ",trim($product->ean))); //g:gtin
$r[32]=""; //atachments
$r[33]=""; //g:options
$r[34]=$specs; //ФИЛЬТРЫ
ksort($r);
return implode("|",$r)."|";
}
}
function md6($str) {
global $hache_salt;
$ret="";
$r=$hache_salt.$hache_salt.$hache_salt.$hache_salt.$hache_salt.$hache_salt;
$md=md5($str);
for ($i=0; $i<6; $i++) {
$mmd=substr($md,0,2);
$md=substr($md,2);
$ret.=substr($r,hexdec($mmd),1);
}
return $ret;
}
function preparexml($tag,$st,$f2) {
global $st2;
$stn=trim(trim(trim(trim(str_replace("  "," ",str_replace("  "," ",str_replace("  "," ",str_replace(chr(0x9),"", $st))))))));
//echo "\n$stn\n$st\n\"".strtolower(substr(str_replace(chr(0x9),"",$stn),0,(strlen($tag)+2)))."\"\n";
if(strtolower(substr(str_replace(chr(0x9),"",$stn),0,(strlen($tag)+2)))==strtolower("<".$tag.">")){ $st2=""; $stn=""; }
if(strtolower(substr(str_replace(chr(0x9),"",$stn),0,(strlen($tag)+3)))==strtolower("</".$tag.">")){

fputs($f2,"<".$tag.">".$st2."</".$tag.">\n");
$st2="";
} else {
$st2.=$stn;
}
}
?>