Файл: wenr.online/system/functions.php
Строк: 178
<?php
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 redirect($to = '/', $time = 0) {
global $r;
$parse = parse_url($to, PHP_URL_QUERY);
if(!isset($parse)) $to = $to.'?r='.$r;
if(isset($parse)) $to = $to.'&r='.$r;
$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 next_exp($lvl,$next_exp){
$exp = $next_exp + (($lvl + 1) * 500);
return $exp;
}
function skills($skill){
$return = 'Новичек';
if($skill > 30) $return = 'Боец';
if($skill > 80) $return = 'Мастер';
return $return;
}
function count_online(){
global $db;
$sql = $db->prepare('select `user_id` from `users` where `online` > ?');
$sql -> execute([time()-3600]);
return $sql -> rowCount();
}
function count_user(){
global $db;
$sql = $db->prepare('select `user_id` from `users`');
$sql -> execute();
return $sql -> rowCount();
}
function count_topic(){
global $db;
$sql = $db->prepare('select `id` from `forum_topic`');
$sql -> execute();
return $sql -> rowCount();
}
function count_msg(){
global $db;
$sql = $db->prepare('select `id` from `forum_msg`');
$sql -> execute();
return $sql -> rowCount();
}
function count_news(){
global $db;
$sql = $db->prepare('select `id` from `news`');
$sql -> execute();
return $sql -> rowCount();
}
function count_chat($clan = 0){
global $db;
$sql = $db->prepare('select `id` from `chat` where `clan` = ?');
$sql -> execute([$clan]);
return $sql -> rowCount();
}
function item_info($id){
global $db;
$sql = $db->prepare('select * from `shop` where `id` = ?');
$sql -> execute([$id]);
return $sql->fetch(PDO :: FETCH_OBJ);
}
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 notifSend($id,$text){
global $db;
$sysID = 1;
$sql = $db->prepare("INSERT INTO `mail` (`user`,`kont`,`read`,`msg`,`time`) VALUES (?,?,?,?,?)");
$sql->execute([$sysID,$id,0,$text,time()]);
$add_k = $db->prepare("INSERT INTO `kont` (`id_user`, `id_kont`, `time`) VALUES (?,?,?)");
$add_k -> execute([$sysID,$id,time()]);
$add_u = $db->prepare("INSERT INTO `kont` (`id_user`, `id_kont`, `time`) VALUES (?,?,?)");
$add_k -> execute([$id,$sysID,time()]);
$new = $db->prepare("UPDATE `kont` SET `new_msg` = `new_msg` + ?,`time` = ? WHERE `id_kont` = ? and `id_user` = ?");
$new -> execute([1,time(),$sysID,$id]);
$new2 = $db->prepare("UPDATE `kont` SET `time` = ? WHERE `id_kont` = ? and `id_user` = ?");
$new2 -> execute([time(),$id,$sysID]);
}
function num_word($value, $words, $show = true)
{
$num = $value % 100;
if ($num > 19) {
$num = $num % 10;
}
$out = ($show) ? $value . ' ' : '';
switch ($num) {
case 1: $out .= $words[0]; break;
case 2:
case 3:
case 4: $out .= $words[1]; break;
default: $out .= $words[2]; break;
}
return $out;
}
function secTo($secs)
{
if($secs < 0) $secs = 0;
$res = '';
$days = floor($secs / 86400);
$secs = $secs % 86400;
if($days > 0) $res .= num_word($days, array('день', 'дня', 'дней')) . ' ';
$hours = floor($secs / 3600);
$secs = $secs % 3600;
if($hours > 0) $res .= num_word($hours, array('час', 'часа', 'часов')) . ' ';
$minutes = floor($secs / 60);
$secs = $secs % 60;
if($minutes > 0) $res .= num_word($minutes, array('минута', 'минуты', 'минут')) . ' ';
$res .= num_word($secs, array('секунда', 'секунды', 'секунд'));
return $res;
}
function secTosrc($secs)
{
if($secs < 0) $secs = 0;
$res = '';
$days = floor($secs / 86400);
$secs = $secs % 86400;
if($days > 0) $res .= $days.' д. ';
$hours = floor($secs / 3600);
$secs = $secs % 3600;
if($hours > 0) $res .= $hours.' ч. ';
$minutes = floor($secs / 60);
$secs = $secs % 60;
if($minutes > 0) $res .= $minutes.' м. ';
$res .= $secs.' с.';
return $res;
}
function clan_info($id){
global $db;
$clan = $db->prepare('select * from `clan` where `id` = ?');
$clan -> execute([$id]);
$clan = $clan -> fetch(PDO :: FETCH_OBJ);
if(!isset($clan->id)) return 'Пусто';
return $clan;
}
function selected($var, $value)
{
if (!is_array($var)) {
$var = explode(',', $var);
}
return (in_array($value, $var)) ? ' selected' : '';
}
function checked($var, $value = null)
{
if (is_null($value)) {
return ($var) ? ' checked' : '';
} else {
if (!is_array($var)) {
$var = explode(',', $var);
}
return (in_array($value, $var)) ? ' checked' : '';
}
}