Файл: system/functions.php
Строк: 132
<?php
function redirect($to = '/', $time = 0) {
$string = sprintf('Refresh: %d; url=%s', $time, $to);
header($string);
}
function random_string($length = 8) {
$string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
return substr(str_shuffle($string), 0, $length);
}
function get_ip()
{
$value = '';
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$value = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$value = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (!empty($_SERVER['REMOTE_ADDR'])) {
$value = $_SERVER['REMOTE_ADDR'];
}
return $value;
}
function date_ru($timestamp, $show_time = false)
{
if (empty($timestamp)) {
return '-';
} else {
$now = explode(' ', date('Y n j H i'));
$value = explode(' ', date('Y n j H i', $timestamp));
if ($now[0] == $value[0] && $now[1] == $value[1] && $now[2] == $value[2]) {
return 'Сегодня в ' . $value[3] . ':' . $value[4];
} else {
$month = array(
'', 'янв.', 'фев.', 'мар.', 'апр.', 'мая', 'июн.',
'июл.', 'авг.', 'сен.', 'окт.', 'нояб.', 'дек.'
);
$out = $value[2] . ' ' . $month[$value[1]] . ' ' . $value[0];
if ($show_time) {
$out .= ' в ' . $value[3] . ':' . $value[4];
}
return $out;
}
}
}
function encrypt($plaintext){
$ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC");
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext_raw = openssl_encrypt($plaintext, $cipher, ENCRYPTION_KEY, $options=OPENSSL_RAW_DATA, $iv);
$hmac = hash_hmac('sha256', $ciphertext_raw, ENCRYPTION_KEY, $as_binary=true);
$ciphertext = base64_encode( $iv.$hmac.$ciphertext_raw );
return $ciphertext;
}
function decrypt($ciphertext){
$c = base64_decode($ciphertext);
$ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC");
$iv = substr($c, 0, $ivlen);
$hmac = substr($c, $ivlen, $sha2len=32);
$ciphertext_raw = substr($c, $ivlen+$sha2len);
$plaintext = openssl_decrypt($ciphertext_raw, $cipher, ENCRYPTION_KEY, $options=OPENSSL_RAW_DATA, $iv);
$calcmac = hash_hmac('sha256', $ciphertext_raw, ENCRYPTION_KEY, $as_binary=true);
if (hash_equals($hmac, $calcmac))
{
return $plaintext;
}
}
function pr($idshop,$method){
global $db;
$shop = $db->prepare('SELECT * FROM `shop` WHERE `id` = ?');
$shop -> execute([$idshop]);
$shop = $shop -> fetch(PDO :: FETCH_ASSOC);
$m = $db->prepare('SELECT * FROM `method` WHERE `id` = ?');
$m -> execute([$shop['method']]);
$m = $m -> fetch(PDO :: FETCH_ASSOC);
return $m[$method];
}
function money($pr,$sum){
if($pr != 0) $pr = $pr+3;
$sum = str_replace(',','',$sum);
$amount = ($pr/100*$sum)+$sum;
return number_format($amount, 2, '.', '');
}
define('TOKEN_TG', '1296529639:AAEBq-tH5rGDjDxQ3FV40gyQPmPgrr6nBEE');
define('PROXY', '80.187.140.26:8080');
function sendTelegram($user, $text)
{
$response = [
'chat_id' => $user,
'text' => $text
];
$ch = curl_init('https://api.telegram.org/bot' . TOKEN_TG . '/sendMessage');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $response);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_PROXY, PROXY);
curl_setopt($ch, CURLOPT_HEADER, false);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
function notifSend($id,$text){
global $db,$mailSMTP,$template;
$us = $db->prepare('SELECT * FROM `users` WHERE `user_id` = ?');
$us -> execute([$id]);
$us = $us -> fetch(PDO :: FETCH_ASSOC);
$notif = $db->prepare('UPDATE `users` SET `notif` = ? WHERE `user_id` = ?');
$notif -> execute([1,$id]);
if(!empty($us['telegram'])) sendTelegram($us['telegram'], $text);
$sql = $db->prepare("INSERT INTO `mail` (`user`,`read`,`text`,`time`) VALUES (?,?,?,?)");
$sql->execute([$id,0,$text,time()]);
$from = array(
"Robot USPAY.RU", // Имя отправителя
"robot@uspay.ru" // почта отправителя
);
$msg = nl2br('Здравствуйте, '.$us['nick'].PHP_EOL.$text);
$mail = $template->render('email.notice', [
'notice' => $msg
]);
$mailSMTP->send($us['email'], 'Уведомление USPAY.RU', $mail, $from);
}
function setting($var){
global $db;
$setting = $db->prepare('select * from config where name = ?');
$setting -> execute([$var]);
$setting = $setting->fetch(PDO :: FETCH_OBJ);
return $setting->value;
}
function paysum(){
global $db,$user;
$sql = $db->prepare('select * from `shop` where `user` = ?');
$sql -> execute([$user->get()->user_id]);
$sql = $sql -> fetchAll();
$sum = 0;
foreach($sql as $res){
$month = $db->prepare('SELECT SUM(`summa`) as sum FROM `payments` WHERE `test` = ? and `status` = ? and `shop` = ?');
$month -> execute([0,3,$res['id']]);
$month = $month ->fetch(PDO :: FETCH_ASSOC);
if($month['sum'] > 0) $sum = $sum + $month['sum'];
}
return $sum;
}
function shop($id){
global $db;
$shop = $db->prepare('select * from shop where id = ?');
$shop -> execute([$id]);
$shop = $shop->fetch(PDO :: FETCH_OBJ);
return $shop;
}
function method($id){
global $db;
$method = $db->prepare('select * from method where id = ?');
$method -> execute([$id]);
$method = $method->fetch(PDO :: FETCH_OBJ);
return $method;
}
function Curl(){
$ch = curl_init('https://uspay.ru/result/callback');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
$html = curl_exec($ch);
curl_close($ch);
}