194 lines
7.5 KiB
PHP
Executable File
194 lines
7.5 KiB
PHP
Executable File
<?php
|
|
ini_set('display_errors', 0);
|
|
function html_to_obj($html)
|
|
{
|
|
$dom = new DOMDocument();
|
|
// $dom->loadHTML($html);
|
|
@$dom->loadHTML(mb_convert_encoding(@$html, 'HTML-ENTITIES', 'UTF-8'));
|
|
return element_to_obj($dom->documentElement);
|
|
}
|
|
|
|
function element_to_obj($element)
|
|
{
|
|
@$obj = array("tag" => @$element->tagName);
|
|
foreach (@$element->attributes as $attribute) {
|
|
$obj[$attribute->name] = $attribute->value;
|
|
}
|
|
foreach ($element->childNodes as $subElement) {
|
|
if ($subElement->nodeType == XML_TEXT_NODE) {
|
|
$obj["html"] = $subElement->wholeText;
|
|
} else {
|
|
$obj["children"][] = element_to_obj($subElement);
|
|
}
|
|
}
|
|
return $obj;
|
|
}
|
|
|
|
class Tavr
|
|
{
|
|
public static $catID = 0; // int
|
|
public static $html = ''; // int
|
|
public static $dataproductjson = ''; // int
|
|
public static $link = ''; // int
|
|
public static $globalArray = []; // int
|
|
static function findDPJ()
|
|
{
|
|
$h = file_get_contents(self::$link);
|
|
$array = html_to_obj($h);
|
|
$arr2 = $array['children'][1]['children'][1]['children'][0]['children'][1]['children'][1]['children'][1]['children'][0]['children'][1]['data-product-json'];
|
|
if ($arr2) {
|
|
self::$dataproductjson = \core::j($arr2);
|
|
}
|
|
|
|
|
|
|
|
// print_r(self::$dataproductjson);
|
|
|
|
|
|
return self::$dataproductjson;
|
|
|
|
}
|
|
|
|
|
|
static function getImgGidrootvet()
|
|
{
|
|
mkdir("img/tavrnt.ru/tovar/" . $_GET['id'], 0700);
|
|
$j = (self::$dataproductjson) ? self::$dataproductjson : $j = self::findDPJ();//$arr2['data-product-json'];
|
|
$fm2 = $j['images'];
|
|
if ($fm2) {
|
|
for ($i = 0; $i < count($fm2); $i++) {
|
|
$images[$i]['filename'] = \core::genpassword(30);
|
|
$images[$i]['tip'] = pathinfo($fm2[$i]['original_url'], PATHINFO_EXTENSION);
|
|
exec("wget -O img/tavrnt.ru/tovar/" . $_GET['id'] . '/' . $images[$i]['filename'] . '.' . strtolower($images[$i]['tip']) . ' ' . $fm2[$i]['original_url']);
|
|
}
|
|
$json = \core::j($images);
|
|
\DBmysql::set("UPDATE `tovar` SET `images`=? WHERE `id`=? LIMIT 1", [$json, $_GET['id']]);
|
|
|
|
}
|
|
//header("Location: /tovar_show/" . $_GET['id']);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static function getTxtGidrootvet($donor)
|
|
{
|
|
$h = file_get_contents($donor);
|
|
self::$globalArray = html_to_obj($h);
|
|
$arr = self::$globalArray['children'][1]['children'][1]['children'][0]['children'][1]['children'][1]['children'][2]['children'][0]['children'][0]['children'][0]['children'][1]['children'][0]['html'];
|
|
|
|
if ($arr) {
|
|
\DBmysql::set("UPDATE `tovar` SET `txt`=? WHERE `id`=? LIMIT 1", [trim($arr), $_GET['id']]);
|
|
} else {
|
|
$arr = self::$globalArray['children'][1]['children'][1]['children'][0]['children'][1]['children'][1]['children'][2]['children'][0]['children'][0]['children'][0]['children'][1]['html'];
|
|
if ($arr) {
|
|
\DBmysql::set("UPDATE `tovar` SET `txt`=? WHERE `id`=? LIMIT 1", [trim($arr), $_GET['id']]);
|
|
}else{
|
|
$arr = self::$globalArray['children'][1]['children'][1]['children'][0]['children'][1]['children'][1]['children'][1]['children'][1]['children'][0]['children'][0]['children'][0]['children'][1]['html'];
|
|
if ($arr) {
|
|
\DBmysql::set("UPDATE `tovar` SET `txt`=? WHERE `id`=? LIMIT 1", [trim($arr), $_GET['id']]);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static function getArtikulGidrootvet()
|
|
{
|
|
$j = (self::$dataproductjson) ? self::$dataproductjson : $j = self::findDPJ();//$arr2['data-product-json'];
|
|
if ($j['variants'][0]['sku']) {
|
|
\DBmysql::set("UPDATE `tovar` SET `artikul`=? WHERE `id`=? LIMIT 1", [$j['variants'][0]['sku'], $_GET['id']]);
|
|
}
|
|
}
|
|
|
|
|
|
static function getCenaGidrootvet()
|
|
{
|
|
$j = (self::$dataproductjson) ? self::$dataproductjson : $j = self::findDPJ();//$arr2['data-product-json'];
|
|
if ($j['price_max']) {
|
|
\DBmysql::set("UPDATE `tovar` SET `cena`=? WHERE `id`=? LIMIT 1", [$j['price_max'], $_GET['id']]);
|
|
}
|
|
}
|
|
|
|
|
|
static function getHarsGidrootvet2($donor)
|
|
{
|
|
$h = file_get_contents(self::$link);
|
|
$array = html_to_obj($h);
|
|
$arr = $array['children'][1]['children'][1]['children'][0]['children'][1]['children'][1]['children'][1]['children'][0]['children'][1]['children'][3]['children'][0]['children'][1]['children'][0]['children'];
|
|
if ($arr) {
|
|
$count = count($arr);
|
|
for ($i = 0; $i < $count; $i++) {
|
|
$p = trim($arr[$i]['children'][0]['html']);
|
|
$v = trim($arr[$i]['children'][1]['html']);
|
|
$v = str_replace(array("\r", "\n"), '', $v);
|
|
//ИД параметра из справочника
|
|
$id = \DBmysql::getValue("SELECT `id` FROM `tovar_har_sp` WHERE `site`=? AND `txt`=? LIMIT 1", [$_SERVER['SERVER_NAME'], $p]);
|
|
if (!$id)
|
|
$id = \DBmysql::add("INSERT INTO `tovar_har_sp` (`site`, `txt`) VALUES(?, ?)", [$_SERVER['SERVER_NAME'], $p]);
|
|
//Добавляем характеристику
|
|
$idh = \DBmysql::getValue("SELECT `id` FROM `tovar_har` WHERE `tovar_har_sp_id`=? AND `tovar_id`=? AND `txt`=? AND `site`=? LIMIT 1", [$id, $_GET['id'], $v, $_SERVER['SERVER_NAME']]);
|
|
if (!$idh)
|
|
\DBmysql::add("INSERT INTO `tovar_har` (`tovar_har_sp_id`, `tovar_id`, `txt`, `site`) VALUES (?, ?, ?, ?)", [$id, $_GET['id'], $v, $_SERVER['SERVER_NAME']]);
|
|
}
|
|
}else{
|
|
$arr = $array['children'][1]['children'][1]['children'][0]['children'][1]['children'][1]['children'][1]['children'][0]['children'][1]['children'][1]['children'][6]['children'][2]['children'][0]['children'][1]['children'][0]['children'];
|
|
if ($arr) {
|
|
$count = count($arr);
|
|
for ($i = 0; $i < $count; $i++) {
|
|
$p = trim($arr[$i]['children'][0]['html']);
|
|
$v = trim($arr[$i]['children'][1]['html']);
|
|
$v = str_replace(array("\r", "\n"), '', $v);
|
|
//ИД параметра из справочника
|
|
$id = \DBmysql::getValue("SELECT `id` FROM `tovar_har_sp` WHERE `site`=? AND `txt`=? LIMIT 1", [$_SERVER['SERVER_NAME'], $p]);
|
|
if (!$id)
|
|
$id = \DBmysql::add("INSERT INTO `tovar_har_sp` (`site`, `txt`) VALUES(?, ?)", [$_SERVER['SERVER_NAME'], $p]);
|
|
//Добавляем характеристику
|
|
$idh = \DBmysql::getValue("SELECT `id` FROM `tovar_har` WHERE `tovar_har_sp_id`=? AND `tovar_id`=? AND `txt`=? AND `site`=? LIMIT 1", [$id, $_GET['id'], $v, $_SERVER['SERVER_NAME']]);
|
|
if (!$idh)
|
|
\DBmysql::add("INSERT INTO `tovar_har` (`tovar_har_sp_id`, `tovar_id`, `txt`, `site`) VALUES (?, ?, ?, ?)", [$id, $_GET['id'], $v, $_SERVER['SERVER_NAME']]);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
Tavr::$link = $res['massiv']['donor'];
|
|
|
|
|
|
if (!$res['tovar']['images'])
|
|
Tavr::getImgGidrootvet();
|
|
|
|
if (!$res['tovar']['txt'] || $res['tovar']['txt'] == 'Array')
|
|
Tavr::getTxtGidrootvet($res['massiv']['donor']);
|
|
|
|
if (!$res['tovar']['artikul'])
|
|
Tavr::getArtikulGidrootvet();
|
|
if (!$res['tovar']['cena'])
|
|
Tavr::getCenaGidrootvet();
|
|
|
|
|
|
|
|
if (!$hars)
|
|
Tavr::getHarsGidrootvet2($res['massiv']['donor']);
|
|
|
|
|
|
|
|
//Tavr::findDPJ();
|
|
|
|
?>
|