<?php
@error_reporting ( E_ALL ^ E_WARNING ^ E_NOTICE );
@ini_set ( 'display_errors', true );
@ini_set ( 'html_errors', false );
@ini_set ( 'error_reporting', E_ALL ^ E_WARNING ^ E_NOTICE );
define( 'DATALIFEENGINE', true );
define( 'ROOT_DIR', substr( dirname( __FILE__ ), 0, -12 ) );
define( 'ENGINE_DIR', ROOT_DIR . '/engine' );
require_once ENGINE_DIR.'/modules/functions.php';
require_once ENGINE_DIR.'/classes/mysql.php';
require_once ENGINE_DIR.'/data/dbconfig.php';
require_once ENGINE_DIR.'/data/config.php';
require_once ENGINE_DIR.'/data/rate_update_config.php';
require_once ENGINE_DIR.'/modules/sitelogin.php';
$act = $_GET['act'];
switch($act) {
case "newsarray":
if (!$rating_option['kp_field']) {
echo json_encode(array('error' => 'Доп. поле с ID КиноПоиска не выбрано!'));
return;
}
$from_news = $_POST['from'];
$from_news = is_numeric($from_news) ? intval($from_news) : false;
$to_news = $_POST['to'];
$to_news = is_numeric($to_news) ? intval($to_news) : false;
if ( $member_id['user_group'] != 1 ) {
echo json_encode(array('error' => 'Недостаточно прав для выполнения данного действия!'));
return;
}
if (!$from_news || !$to_news) {
echo json_encode(array('error' => 'Не удалось получить список новостей для обновления!'));
return;
}
$news = $db->query("SELECT id, xfields FROM ".PREFIX."_post WHERE id BETWEEN {$from_news} AND {$to_news} ORDER BY id");
$news_count = $news->num_rows;
if($news_count == 0) return;
$result_kp = array();
$count = 0;
while($temp_news = $db->get_row($news)) {
$id = intval($temp_news['id']);
$xfields = xfieldsdataload($temp_news['xfields']);
$kpid = $xfields[$rating_option['kp_field']];
$kpid = is_numeric($kpid) ? intval($kpid) : false;
if (!$kpid)
continue;
$result_kp[] = array(
'id' => $id,
'kpid' => $kpid
);
$count++;
}
if ($count > 0)
echo json_encode($result_kp);
else
echo json_encode(array('error' => 'Не удалось получить список новостей для обновления!'));
break;
case "updatenews":
if ( $member_id['user_group'] != 1 ) {
echo "denied";
return;
}
$id = $_POST['newsid'];
$id = is_numeric($id) ? intval($id) : false; if(!$id) return;
$kpid = $_POST['kpid'];
$kpid = is_numeric($kpid) ? intval($kpid) : false; if(!$kpid) return;
$url = "https://rating.kinopoisk.ru/{$kpid}.xml";
if($ch = curl_init()) {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($rating_option['proxy_scr'])
curl_setopt($ch, CURLOPT_PROXY, $rating_option['proxy_scr']);
if($rating_option['proxy_auth'])
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $rating_option['proxy_auth']);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
$xml_result = curl_exec($ch);
curl_close($ch);
} else {
if ($rating_option['proxy_scr']) {
if ($rating_option['proxy_auth']) {
$auth = base64_encode($rating_option['proxy_auth']);
$opts = array(
'http' => array (
'method' => 'GET',
'proxy' => 'tcp://' . $rating_option['proxy_scr'],
'request_fulluri' => true,
'header' => array("Proxy-Authorization: Basic $auth", "Authorization: Basic $auth")
)
);
} else {
$opts = array(
'http' => array (
'method' => 'GET',
'proxy' => 'tcp://' . $rating_option['proxy_scr'],
'request_fulluri' => true
)
);
}
$ctx = stream_context_create($opts);
$xml_result = file_get_contents($url, false, $ctx);
} else
$xml_result = file_get_contents($url);
}
$kp_rating = new DOMDocument();
$kp_rating->loadXML($xml_result);
$kinopoisk = $kp_rating->getElementsByTagName('kp_rating');
$rate_kp = $kinopoisk->item(0)->nodeValue;
$votes_kp = number_format(is_null($rate_kp) ? 0 : $kinopoisk->item(0)->getAttribute('num_vote'));
$imdbs = $kp_rating->getElementsByTagName('imdb_rating');
$rate_imdb = $imdbs->item(0)->nodeValue;
$votes_imdb = number_format(is_null($rate_imdb) ? 0 : $imdbs->item(0)->getAttribute('num_vote'));
$votes_kp = str_replace(",", "", $votes_kp);
$votes_imdb = str_replace(",", "", $votes_imdb);
$votes_format = $rating_option['votes_format'];
$rates_round = intval($rating_option['rates_round']);
$rewrite = (boolean)intval($rating_option['allow_rewrite']);
$rates_round = is_numeric($rates_round) ? intval($rates_round) : 3;
$kp_upd = false;
$imdb_upd = false;
if (!$rate_kp || $rate_kp == "0") $rate_kp = "none";
if (!$rate_imdb || $rate_imdb == "0") $rate_imdb = "none";
if (!$id || !$rate_kp || !$rate_imdb) return;
if (!$rating_option['kp_rate_field'] && !$rating_option['imdb_rate_field']){
echo 'error';
return;
}
$news = $db->query("SELECT xfields FROM ".PREFIX."_post WHERE id={$id}")->fetch_assoc();
if (!$news) return;
if (($votes_kp || $votes_imdb) && $votes_format == "type2") {
if ($votes_kp)
$votes_kp = number_format($votes_kp, 0, "", " ");
if ($votes_imdb)
$votes_imdb = number_format($votes_imdb, 0, "", " ");
}
if ($rates_round > 3 && $rates_round < 1)
$rates_round = 3;
$xfields = xfieldsdataload($news['xfields']);
if ($rate_kp != 'none' && $rating_option['kp_rate_field']) {
$rate_kp = round($rate_kp, $rates_round);
if ($rate_kp == intval($rate_kp))
$rate_kp = number_format($rate_kp, 1, ".", "");
if ($xfields[$rating_option['kp_rate_field']] != $rate_kp && !$rewrite) {
$xfields[$rating_option['kp_rate_field']] = $rate_kp;
if ($rating_option['kp_votes_field'])
$xfields[$rating_option['kp_votes_field']] = $votes_kp;
$kp_upd = true;
} else if ($rewrite) {
$xfields[$rating_option['kp_rate_field']] = $rate_kp;
if ($rating_option['kp_votes_field'])
$xfields[$rating_option['kp_votes_field']] = $votes_kp;
$kp_upd = true;
}
}
if ($rate_imdb != 'none' && $rating_option['imdb_rate_field']) {
$rate_imdb = round($rate_imdb, $rates_round);
if ($rate_imdb == intval($rate_imdb))
$rate_imdb = number_format($rate_imdb, 1, ".", "");
if ($xfields[$rating_option['imdb_rate_field']] != $rate_imdb && !$rewrite) {
$xfields[$rating_option['imdb_rate_field']] = $rate_imdb;
if ($rating_option['imdb_votes_field'])
$xfields[$rating_option['imdb_votes_field']] = $votes_imdb;
$imdb_upd = true;
} else if ($rewrite) {
$xfields[$rating_option['imdb_rate_field']] = $rate_imdb;
if ($rating_option['imdb_votes_field'])
$xfields[$rating_option['imdb_votes_field']] = $votes_imdb;
$imdb_upd = true;
}
}
$result = 'error';
if ($kp_upd || $imdb_upd) {
foreach($xfields as $key => &$value)
$arr_field[] = $key . "|" . str_replace('|', '|', $value);
$xfields = implode("||", $arr_field);
unset($arr_field);
$xfields = $db->safesql($xfields);
$db->query("UPDATE ". PREFIX ."_post SET xfields='" . $xfields . "' WHERE id=" . $id);
$result_kp = array(
'news_id' => $id,
'rate_kp' => $rate_kp,
'votes_kp' => $votes_kp,
'rate_imdb' => $rate_imdb,
'votes_imdb' => $votes_imdb
);
$result = json_encode($result_kp);
}
echo $result;
break;
default:
return;
}
?>