2022-12-11 13:55:49 +05:00
< ? php
class core {
public static $settings = '' ;
/* ----------------------------------------------------------------------
08.12 . 2022
Наполняем базу городов , регионов и стран
---------------------------------------------------------------------- */
private static function get_geo_api ( $ip ){
$ch = curl_init ( 'http://ip-api.com/json/' . $ip . '?lang=ru' );
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true );
curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER , false );
curl_setopt ( $ch , CURLOPT_HEADER , false );
$res = curl_exec ( $ch );
curl_close ( $ch );
$res = json_decode ( $res , true );
return $res ;
}
static function geo (){
$res = self :: get_geo_api ( $_SERVER [ 'REMOTE_ADDR' ]);
2022-12-11 15:29:37 +05:00
$strana_id = \DB :: getValue ( 'SELECT `id` FROM `strana` WHERE `txt` = "' . $res [ 'country' ] . '"' );
if ( ! $strana_id ) $strana_id = \DB :: add ( " INSERT INTO `strana` SET `txt` = ? " , $res [ 'country' ] );
$region_id = \DB :: getValue ( 'SELECT `id` FROM `region` WHERE `region` = "' . $res [ 'regionName' ] . '" AND `strana_id` = ' . $strana_id );
if ( ! $region_id ) $region_id = \DB :: add ( " INSERT INTO `region` SET `region` = ?, `strana_id`=? " , array ( $res [ 'regionName' ], $strana_id ));
$city_id = \DB :: getValue ( 'SELECT `id` FROM `city` WHERE `city` = "' . $res [ 'city' ] . '" AND `strana_id` = ' . $strana_id . ' AND `region_id`=' . $region_id );
if ( ! $city_id ) $city_id = \DB :: add ( " INSERT INTO `city` SET `city` = ?, `strana_id`=?, `region_id`=? " , array ( $res [ 'city' ], $strana_id , $region_id ));
2022-12-11 13:55:49 +05:00
$_SESSION [ 'city' ] = $city_id ;
}
/* ----------------------------------------------------------------------
08.12 . 2022
Берем настройки модуля / сайта
---------------------------------------------------------------------- */
static function getSettings ( $set , $mod = 'global' ){
2022-12-11 15:29:37 +05:00
$txt = \DB :: getValue ( 'SELECT `json` FROM `settings` WHERE `mod` = "' . $mod . '"' );
$massiv = \json :: from_j ( $txt );
2022-12-11 13:55:49 +05:00
return $massiv [ $set ];
}
/* ----------------------------------------------------------------------
08.12 . 2022
Авторизация по куки
---------------------------------------------------------------------- */
static function loginCookies ( ) {
if ( @ $_COOKIE [ 'cookies' ] && !@ $_SESSION [ 'user_id' ]){
2022-12-11 15:29:37 +05:00
$res = \json :: from_j ( base64_decode ( $_COOKIE [ 'cookies' ] ) );
$users = \DB :: getAll ( 'SELECT `dostup`, `act` FROM `users` WHERE `id` = "' . $res [ 'user_id' ] . '" AND `pwd`="' . $res [ 'pwd' ] . '" LIMIT 1' );
2022-12-11 13:55:49 +05:00
if ( $users [ 0 ][ 'dostup' ] ) {
$_SESSION [ 'user_id' ] = $res [ 'user_id' ];
$_SESSION [ 'dostup' ] = $dostup ;
}
}
}
/* ----------------------------------------------------------------------
09.12 . 2022
Получаем данные корзины
---------------------------------------------------------------------- */
static function getCart ( ) {
$user_id = ( $_SESSION [ 'user_id' ] ? $_SESSION [ 'user_id' ] : session_id () );
2022-12-11 15:29:37 +05:00
$cart = \DB :: getAll ( 'SELECT * FROM `cart` WHERE `user_id` = ' . $user_id );
2022-12-11 13:55:49 +05:00
for ( $i = 0 ; $i < count ( $cart ); $i ++ ){
2022-12-11 15:29:37 +05:00
$cart2 = \DB :: getAll ( 'SELECT tovar.id, tovar.txt, tovar.cena, img.fileName FROM tovar, img WHERE tovar.id=' . $cart [ $i ][ 'pages_id' ] . ' AND img.page_id=' . $cart [ $i ][ 'pages_id' ] . ' LIMIT 1' );
2022-12-11 13:55:49 +05:00
}
}
/* ----------------------------------------------------------------------
09.12 . 2022
IP
---------------------------------------------------------------------- */
static function detect_ip ( ) {
$ip = false ;
if ( isset ( $_SERVER [ " HTTP_X_FORWARDED_FOR " ]) and preg_match ( " #^[0-9.]+ $ # " , $_SERVER [ " HTTP_X_FORWARDED_FOR " ])) {
$ip = $_SERVER [ " HTTP_X_FORWARDED_FOR " ];
}
else if ( isset ( $_SERVER [ " HTTP_X_REAL_IP " ]) and preg_match ( " #^[0-9.]+ $ # " , $_SERVER [ " HTTP_X_REAL_IP " ])) {
$ip = $_SERVER [ " HTTP_X_REAL_IP " ];
}
else if ( preg_match ( " #^[0-9.]+ $ # " , $_SERVER [ " REMOTE_ADDR " ])) {
$ip = $_SERVER [ " REMOTE_ADDR " ];
}
return $ip ;
}
/* ----------------------------------------------------------------------
09.12 . 2022
Авторизация
---------------------------------------------------------------------- */
private static function redirectAfterLogin ( $dostup ){
if ( $dostup == 'a' || $dostup == 'm' ) header ( 'Location: /admin/' );
2022-12-11 15:29:37 +05:00
if ( $dostup == 'u' ) header ( 'Location: /' . self :: getSettings ( 'default_mod_auth' ) . '/' );
2022-12-11 13:55:49 +05:00
}
static function login ( ) {
2022-12-11 15:29:37 +05:00
$user = \DB :: getAll ( 'SELECT `id`, `dostup` FROM `users` WHERE `email` = "' . $_POST [ 'email' ] . '" AND `pwd`="' . md5 ( $_POST [ 'pwd' ]) . '"' );
2022-12-11 13:55:49 +05:00
if ( $user [ 0 ][ 'dostup' ] ) {
$_SESSION [ 'dostup' ] = $dostup ;
$_SESSION [ 'user_id' ] = $user [ 0 ][ 'id' ];
}
if ( $_POST [ 'remember' ] == 'on' && $user [ 0 ][ 'dostup' ] ) {
$_SESSION [ 'pwd' ] = md5 ( $_POST [ 'pwd' ]);
2022-12-11 15:29:37 +05:00
$cookies = base64_encode ( \json :: to_j ( $_SESSION ) );
2022-12-11 13:55:49 +05:00
$tri_mes = time () + 31536000 ;
setcookie ( 'cookies' , $cookies , $tri_mes , '/' , $_SERVER [ 'SERVER_NAME' ] );
}
self :: redirectAfterLogin ( $user [ 0 ][ 'dostup' ] );
}
}