<?php
/**
* UniSite CMS
*
* @copyright 2018 Artur Zhur
* @link https://unisitecms.ru
* @author Artur Zhur
*
*/
class Geo{
function getGeo(){
global $settings;
if($_SESSION["geo"]["alias"]){
return $_SESSION["geo"]["alias"];
}else{
if($settings["city_id"]){
$data = $this->getCity(intval($settings["city_id"]));
return $data["city_alias"];
}elseif($settings["region_id"]){
$data = $this->getRegion(intval($settings["region_id"]));
return $data["region_alias"];
}
}
}
function viewCity($id=0){
if(detectRobots($_SERVER['HTTP_USER_AGENT']) == false){
if($id){
if(!isset($_SESSION["view-city"][$id])){
update("UPDATE uni_city SET city_count_view=city_count_view+1 WHERE city_id=?", array($id));
$_SESSION["view-city"][$id] = 1;
}
}
}
}
function alias($alias = "", $request = ""){
global $settings;
if( isset($_SESSION["lang"]) ){
$request = explode("/", trim($request, "/") ); unset($request[0]);
$request = implode("/", $request);
}
if($_SESSION["route_name"] == "city_category" || $_SESSION["route_name"] == "category"){
if($request != "/board" && $request){
if(isset($_SESSION["geo"])){
$uri = explode("/", trim($request, "/"));
$uri[0] = $alias;
return _link(implode("/",$uri));
}else{
return _link($alias . "/" . trim($request, "/"));
}
}else{ return _link($alias); }
}else{
return _link($alias);
}
}
function getListCountry(){
global $languages_content,$settings;
$country = getAll("SELECT * FROM uni_country WHERE country_status='1'");
if(count($country) > 0){
foreach ($country as $value) {
$multilanguage_tables = multilanguage_tables(array("id_content" => $value["country_id"], "table_name" => "uni_country"));
$value["country_name"] = !empty($multilanguage_tables['name']) ? urldecode($multilanguage_tables['name']) : $value['country_name'];
if($_SESSION["geo"]["country-id"] == $value["country_id"]){
$name = $value["country_name"];
}
$out .= '<li><a href="'.$this->alias($value["country_alias"], REQUEST_URI).'">'.$value["country_name"].'</a></li>';
}
if($name){
if($_SESSION["route_name"] == "city_category" || $_SESSION["route_name"] == "category"){
$explode = explode("/", trim(REQUEST_URI, "/"));
if($settings["visible_lang_site"]){
unset($explode[0]); unset($explode[1]);
}else{
unset($explode[0]);
}
return '<span class="modal-change-country" >' . $languages_content["class-geo-title-1"].' - <a href="#">'.$name.'</a> <ul><li><a href="'._link(implode("/", $explode)).'">'.$languages_content["class-geo-title-2"].'</a></li>'.$out.'</ul></span>';
}else{
return '<span class="modal-change-country" >' . $languages_content["class-geo-title-1"].' - <a href="#">'.$name.'</a> <ul><li><a href="'._link("board").'">'.$languages_content["class-geo-title-2"].'</a></li>'.$out.'</ul></span>';
}
}else{
return '<span class="modal-change-country" >' . $languages_content["class-geo-title-1"].' - <a href="'._link("board").'">'.$languages_content["class-geo-title-2"].'</a> <ul>'.$out.'</ul></span>';
}
}
}
function outPopularCity(){
global $settings;
if(!$settings["city_id"]){
if($_SESSION["geo"]["country-id"]){
if(!$settings["region_id"]){
$sql = getAll("SELECT * FROM uni_city WHERE country_id='{$_SESSION["geo"]["country-id"]}' order by city_count_view desc limit 20");
}else{
$sql = getAll("SELECT * FROM uni_city WHERE country_id='{$_SESSION["geo"]["country-id"]}' and region_id='{$settings["region_id"]}' order by city_count_view desc limit 20");
}
}else{
if(!$settings["region_id"]){
$sql = getAll("select *, (select country_alias from uni_country where country_id = uni_city.country_id) as country FROM uni_city HAVING country = '".$settings["country_default"]."' order by city_count_view desc limit 20");
}else{
$sql = getAll("select *, (select country_alias from uni_country where country_id = uni_city.country_id) as country FROM uni_city WHERE region_id='{$settings["region_id"]}' HAVING country = '".$settings["country_default"]."' order by city_count_view desc limit 20");
}
}
if(count($sql) > 0){
$index = 1;
foreach ($sql as $key => $value) {
$multilanguage_tables = multilanguage_tables(array("id_content" => $value["city_id"], "table_name" => "uni_city"));
$value["city_name"] = !empty($multilanguage_tables['name']) ? urldecode($multilanguage_tables['name']) : $value['city_name'];
$list .= '<a href="'.$this->alias($value["city_alias"], REQUEST_URI).'">'.$value["city_name"].'</a>';
if($index == 5){
$return .= '<div class="col-lg-3 col-md-4 col-sm-6 col-6" ><div class="list-footer-cities" >'.$list.'</div></div>';
$index = 1;
$list = "";
}else{
$index++;
}
}
}
return $return;
}
}
function detect($ip="", $array = array(), $auto = false){
global $SxGeo,$settings;
if(count($array) == 0){
if($settings["city_id"]){
$data = $this->getCity($settings["city_id"]);
return array("city" => $data["city_name"], "region" => $data["region_name"], "country" => $data["country_name"], "lat" => $data["lat"], "lon" => $data["lon"], "city_id" => $data["city_id"], "region_id" => $data["region_id"], "country_id" => $data["country_id"]);
}else{
if($auto){
$array = $this->geoIp($ip, "array");
if($array["city"]){
$city = getOne("SELECT * FROM uni_city WHERE city_alias=?", array(translite($array["city"])));
}
}
}
}
return array("city" => $array["city"], "region" => $array["region"], "country" => $array["country"], "lat" => $array["lat"], "lon" => $array["lon"], "city_id" => $city["city_id"], "region_id" => $city["region_id"], "country_id" => $city["country_id"]);
}
function getCity($city_id = 0){
global $Cashed;
if($city_id){
$key = "city_id_".$city_id;
$get = $Cashed->get($key,"city");
if($get !== false){
return $get;
}else{
$data = getOne("SELECT *, (select country_name from uni_country where country_id = uni_city.country_id) as country_name, (select region_name from uni_region where region_id = uni_city.region_id) as region_name FROM uni_city WHERE city_id='{$city_id}'");
if($data){
$multilanguage_tables = multilanguage_tables(array("id_content" => $data["city_id"], "table_name" => "uni_city"));
$data["city_name"] = !empty($multilanguage_tables['name']) ? urldecode($multilanguage_tables['name']) : $data['city_name'];
$Cashed->set($data,$key,"city");
}
return $data;
}
}else{
return array();
}
}
function getRegion($region_id = 0){
global $Cashed;
if($region_id){
$key = "region_id_".$region_id;
$get = $Cashed->get($key,"region");
if($get !== false){
return $get;
}else{
$data = getOne("SELECT *, (select country_name from uni_country where country_id = uni_region.country_id) as country_name FROM uni_region WHERE region_id='{$region_id}'");
if($data){
$multilanguage_tables = multilanguage_tables(array("id_content" => $data["region_id"], "table_name" => "uni_region"));
$data["region_name"] = !empty($multilanguage_tables['name']) ? urldecode($multilanguage_tables['name']) : $data['region_name'];
$Cashed->set($data,$key,"region");
}
return $data;
}
}else{
return array();
}
}
function setGeo(){
global $Cashed;
$key = $_SESSION["geo"]["country-id"].$_SESSION["geo"]["region-id"].$_SESSION["geo"]["city-id"];
$data = $Cashed->get($key,"city");
if($data !== false){
$_SESSION["geo"]["change"] = $data;
}else{
if($_SESSION["geo"]["country-id"] != 0){
$_SESSION["geo"]["change"]["country"] = getOne("SELECT * FROM uni_country WHERE country_id='{$_SESSION["geo"]["country-id"]}'");
}
if($_SESSION["geo"]["region-id"] != 0){
$_SESSION["geo"]["change"]["region"] = getOne("SELECT * FROM uni_region WHERE region_id='{$_SESSION["geo"]["region-id"]}'");
}
if($_SESSION["geo"]["city-id"] != 0){
$_SESSION["geo"]["change"]["city"] = getOne("SELECT * FROM uni_city WHERE city_id='{$_SESSION["geo"]["city-id"]}'");
}
$Cashed->set($_SESSION["geo"]["change"],$key,"city");
}
}
function geoIp($ip, $view = "array"){
global $SxGeo;
if($ip){
$Geo = $SxGeo->getCityFull($ip);
if($view == "array"){
return array("city"=>$Geo["city"]["name_ru"],"region"=>$Geo["region"]["name_ru"],"country"=>$Geo["country"]["name_ru"]);
}else{
if(!empty($Geo["city"]["name_ru"]) && !empty($Geo["region"]["name_ru"])){
return $Geo["city"]["name_ru"].', '.$Geo["region"]["name_ru"];
}elseif(!empty($Geo["city"]["name_ru"])){
return $Geo["city"]["name_ru"];
}elseif(!empty($Geo["region"]["name_ru"])){
return $Geo["region"]["name_ru"];
}else{
return '-';
}
}
}
}
function vendorMap(){
global $settings;
if($settings["map_vendor"] == "google"){
return '<script src="https://maps.googleapis.com/maps/api/js?key='.$settings["map_google_key"].'&libraries=places"></script>';
}elseif($settings["map_vendor"] == "yandex"){
return '<script src="//api-maps.yandex.ru/2.1/?apikey=b570afd6-e567-4d2a-bc5c-741b70c25033&lang=ru_RU"></script>';
}
}
function metrics(){
global $prefix_dir;
?>
<script>
$(document).ready(function () {
window.onload = function () {
$.ajax({type: "POST",url: "<?php echo $prefix_dir; ?>ajax/metrics/",data: "city=®ion=&country=&enter="+location.href+"&referrer="+document.referrer+"&title="+$("title").html()+"&latitude=&longitude=", dataType: "html",cache: false,success: function (data) {
if(data != false){
$(".fade-change-city strong").html(data);
$(".fade-change-city").show();
}
}});
}
});
</script>
<?php
}
}
$Geo = new Geo();
?>