core/api/php/core.php

187 lines
8.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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']);
$strana_id = \DB::getValue('SELECT `id` FROM `strana` WHERE `txt` = "' . $res['country'] . '"');
if (!$strana_id && $res['country'])$strana_id=\DB::add("INSERT INTO `strana` (`txt`) VALUES (?)", array($res['country']));
$region_id = \DB::getValue('SELECT `id` FROM `region` WHERE `region` = "' . $res['regionName'] . '" AND `strana_id` = ' . $strana_id);
if ( !$region_id && $res['regionName'] )$region_id = \DB::add("INSERT INTO `region` (`region`, `strana_id`) VALUES (?, ?)", 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 && $res['city'] ){
$city_id = \DB::add("INSERT INTO `city` (`city`, `strana_id`, `region_id`) VALUES (?, ?, ?)" , array( $res['city'], $strana_id, $region_id) );
$_SESSION['city']=$city_id;
}
}
/* ----------------------------------------------------------------------
08.12.2022
Берем настройки модуля/сайта
---------------------------------------------------------------------- */
static function getSettings($set, $mod='global'){
$txt = \DB::getValue('SELECT `json` FROM `settings` WHERE `mod` = "' . $mod . '" LIMIT 1');
$massiv = \json::from_j($txt);
return $massiv[$set];
}
/* ----------------------------------------------------------------------
21.12.2022
Берем пишем модуля/сайта
---------------------------------------------------------------------- */
static function setSettings($param, $val, $mod='global'){
return $massiv[$set];
}
/* ----------------------------------------------------------------------
08.12.2022
Авторизация по куки
---------------------------------------------------------------------- */
static function loginCookies ( ) {
if ( @$_COOKIE['cookies'] && !@$_SESSION['user_id']){
$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');
if ( $users[0]['dostup'] ) {
$_SESSION['user_id'] = $res['user_id'];
$_SESSION['dostup'] = $users[0]['dostup'];
}
}
}
/* ----------------------------------------------------------------------
09.12.2022
Получаем данные корзины
Выводим массив, в массиве cart - все содержимое корзины, в summ - итог корзины
---------------------------------------------------------------------- */
static function getCart ( ) {
$session_id = ( $_SESSION['user_id'] ) ? $_SESSION['user_id'] : session_id();
$cart = \DB::getAll( "SELECT * FROM `cart` WHERE `user_id`=?", $session_id );
$summ = 0;
//Получаем название товаров и цены
for ( $i = 0; $i < count( $cart ); $i++ ){
$cart[$i]['title'] = \DB::getValue( "SELECT `title` FROM `tovar` WHERE `id`=? LIMIT 1", $cart[$i]['tovar_id'] );
$cart[$i]['img'] = \DB::getValue( "SELECT `filename` FROM `tovar_img` WHERE `tovar_id`=? LIMIT 1", $cart[$i]['tovar_id'] );
$cart[$i]['cena'] = \DB::getValue( "SELECT `cena` FROM `tovar_price_history` WHERE `tovar_id`=? ORDER BY `t` DESC LIMIT 1", $cart[$i]['tovar_id'] );
$cart[$i]['st'] = $cart[$i]['kolvo'] * $cart[$i]['cena'];
$summ = $summ + $cart[$i]['st'];
}
$result['cart'] = $cart;
$result['summ'] = $summ;
return $result;
}
/* ----------------------------------------------------------------------
20.12.2022
Получаем данные об основной организации пользователя
---------------------------------------------------------------------- */
static function getUserMainOrg ( $user_id ) {
}
/* ----------------------------------------------------------------------
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/' );
if ( $dostup=='u' ) header( 'Location: /' . self::getSettings('default_mod_auth') . '/' );
}
static function login ( ) {
$user = \DB::getAll( 'SELECT `id`, `dostup` FROM `users` WHERE `email` = ? AND `pwd`=? LIMIT 1', array( $_POST['email'], md5( $_POST['pwd'] ) ) );
if ( $user[0]['dostup'] ) {
$_SESSION['dostup'] = $user[0]['dostup'];
$_SESSION['user_id'] = $user[0]['id'];
}
if ( $_POST['remember'] == 'on' && $user[0]['dostup'] ) {
$_SESSION['pwd'] = md5($_POST['pwd']);
$cookies = base64_encode ( \json::to_j ( $_SESSION ) );
$tri_mes = time() + 31536000;
setcookie ( 'cookies', $cookies, $tri_mes, '/', $_SERVER['SERVER_NAME'] );
}
self::redirectAfterLogin( $user[0]['dostup'] );
}
/* ----------------------------------------------------------------------
12.12.2022
Получаем данные о пользователе
---------------------------------------------------------------------- */
static function getUserInfo ( $id ) {
$user = \DB::getAll( 'SELECT * FROM `users` WHERE `id` = ? LIMIT 1', array( $id ) );
return $user;
}
/* ----------------------------------------------------------------------
13.12.2022
Шифровальщик
---------------------------------------------------------------------- */
static function crypt ( $String, $act='crypt', $pwd ) {
if ($act=='crypt')$String=base64_encode($String);
$Salt='BGuxLWQtKweKEMV4';
$StrLen = strlen($String);
$Seq = $Password;
$Gamma = '';
while (strlen($Gamma)<$StrLen){
$Seq = pack("H*",sha1($Gamma.$Seq.$Salt));
$Gamma.=substr($Seq,0,8);
}
$result = $String^$Gamma;
if ($act=='decrypt')$String=base64_decode($String);
return $result;
}
/* ----------------------------------------------------------------------
15.12.2022
Генератор паролей
---------------------------------------------------------------------- */
static function genpassword ( $number=10 ) {
$arr = array('a','b','c','d','e','f', 'g','h','i','j','k','l','m','n','o','p','r','s','t','u','v','x','y','z','1','2','3','4','5','6','7','8','9','0');
$pass = "";
for($i = 0; $i < $number; $i++){
$index = rand(0, count($arr) - 1);
$pass .= $arr[$index];
}
return $pass;
}
/* ----------------------------------------------------------------------
28.12.2022
Получаем мета-теги
---------------------------------------------------------------------- */
static function getMeta ( $mod, $id=0) {
$massiv= \DB::getAll( 'SELECT * FROM `meta` WHERE `id` = ? AND `mod`=? LIMIT 1', array( $id, $mod ) );
if (count($massiv)==0) \DB::add("INSERT INTO `meta` (`id`, `mod`) VALUES (?, ?)", array( $id, $mod ));
$res=$massiv[0];
return $res;
}
}