<?php
###############################
### WCore v5.0 Beta 
### PHP 7.2.x
### Powered by pimnik98
###############################
# Запуск сессии
session_start();
ob_start();
### Loading libs
#### Loading to Config...
require_once 'config.php';
if (wcore_debug == 1) {
    error_reporting(E_ALL | E_STRICT);
    ini_set('display_errors', 'On');
}
#### Loading to Twig...
require_once WCORE_ROOT . '/wcore/libs/Twig/Autoloader.php';
Twig_Autoloader::register();
### Loading SMTP...
require_once WCORE_ROOT . '/wcore/libs/SendMailSmtpClass/SendMailSmtpClass.php';
require_once WCORE_ROOT . '/wcore/libs/SendMailSmtpClass/HelperFilePiminoff.php';
$mailsend = new HelperFilePiminoff(smtp_cfg['server'],smtp_cfg['username'],smtp_cfg['password'],smtp_cfg['port']);
$mailsend->setStatus(smtp_cfg['activated']);
$mailsend->setWho(smtp_cfg['who']);
### Loading to MySQLi...
@$mysqli = mysqli_connect(mysqli_host, mysqli_login, mysqli_ps, mysqli_db);
if (!$mysqli) {
    printf("Невозможно подключиться к базе данных. <br>Код ошибки: %s\n", mysqli_connect_error());
    exit;
}
### Loading function...

function go($url)
{
    header("Location: " . $url);
    exit();
}

function _csrf()
{
    return $_SESSION['csrf_token'] = substr(str_shuffle('qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'), 0, 16);
}

function antixs($data)
{
    global $mysqli;
    return trim(htmlspecialchars(mysqli_real_escape_string($mysqli, $data)));
}

mysqli_query($mysqli, "SET NAMES 'utf8'");
if (isset($_COOKIE['login']) && isset($_COOKIE['ps'])) {
    $sql_user_2 = mysqli_query($mysqli, "SELECT * FROM `users` WHERE `login` = '" . antixs($_COOKIE['login']) . "' AND `ps` = '" . antixs($_COOKIE['ps']) . "' ");
    if (mysqli_num_rows($sql_user_2) != 0) {
        $ank = mysqli_fetch_object($sql_user_2);
    } else {
        go('/modules/user/exit.php');
    }
} else {
    $ank = NULL;
}

function msg_err($type, $message)
{
    global $twig, $lang, $_SERVER;
    return $twig->render('mess_err.tpl', array('lang' => $lang, 'notify' => array('type' => $type, 'msg' => $message,'referer'=>@$_SERVER['HTTP_REFERER'])));
}

function iank($access_level = NULL)
{
    global $ank, $lang, $thecfg, $twig;
    if (!isset($ank) || empty($ank)) {
        echo msg_err('err', $lang['iank']);
        require_once '../../wcore/foot.php';
        exit();
    }
    if (isset($access_level) && $ank->prv < $access_level) {
        go('/');
    }
}

function bbcode($msg)
{
    $bb = array('~\[url=(.*?)?\](.*?)\[\/url\]~si' => '<a href="$1" target="_blank">$2</a>',
        '/\[b\](.+)\[\/b\]/isU' => '<b>$1</b>',
        '/\[u\](.+)\[\/u\]/isU' => '<span style="text-decoration:underline;">$1</span>',
        '/\[s\](.+)\[\/s\]/isU' => '<s>$1</s>',
        '/\[i\](.+)\[\/i\]/isU' => '<i>$1</i>',
        '/\[color=(.+)\](.+)\[\/color\]/isU' => '<span style="color:$1;">$2</span>',
        '/\[quote\](.+)\[\/quote\]/isU' => '<div class="quote">$1</div>');
    $msg = preg_replace(array_keys($bb), array_values($bb), $msg);
    return $msg;
}


function output($msg)
{
    $msg = bbcode($msg);
    $msg = preg_replace('/\\r\\n/si', '<br/>', $msg);
    return $msg;
}

function ptime($time = NULL)
{
    ini_set('date.timezone', wcore_timezone);
    if ($time == NULL) $time = time();
    $settime = 1;
    $full_time = date('d.m.Y / H:i', $time);
    $date = date('d.m.Y', $time);
    $timep = date('H:i', $time);
    if ($date == date('d.m.Y')) $full_time = date('Сегодня H:i', $time);
    if ($date == date('d.m.Y', time() - 60 * 60 * 24)) $full_time = date('Вчера, H:i', $time);
    if ($settime == 1 && $date == date('d.m.Y')) $full_time = date('Сегодня, H:i', $time);
    return $full_time;
}

function security($string)
{
    $pass = md5(md5($string));
    $count_1 = strlen($pass);
    $return = NULL;
    for ($i = 1; $i <= $count_1; $i++) {
        $n = ($i == 1 ? 0 : $i - 1);
        $return .= md5(md5(substr($pass, $n, $i)));
    }
    return md5($return);
}

function page($k_page = 1)
{
    $page = 1;
    if (isset($_GET['page'])) {
        if ($_GET['page'] == 'end') $page = intval($k_page);
        elseif (is_numeric($_GET['page'])) $page = intval($_GET['page']);
    }
    if ($page < 1) $page = 1;
    if ($page > $k_page) $page = $k_page;
    return $page;
}

function k_page($k_post = 0, $k_p_str = 10)
{
    if ($k_post != 0) {
        $v_pages = ceil($k_post / $k_p_str);
        return $v_pages;
    } else {
        return 1;
    }
}

function str($link = '?', $k_page = 1, $page = 1)
{
    global $twig, $lang;
    if ($page < 1) $page = 1;
    if ($page > 1) $page_z[] = array('code' => 1, 'page' => ($page - 1), 'info' => 'back', 'href' => $link . 'page=' . ($page - 1));
    if ($page != 1) $page_z[] = array('code' => 3, 'page' => 1, 'info' => 'page1', 'href' => $link . 'page=1');
    else            $page_z[] = array('code' => 2, 'page' => 1, 'info' => 'selectpage', 'href' => '#');
    for ($ot = -3; $ot <= 3; $ot++) {
        if ($page + $ot > 1 && $page + $ot < $k_page) {
            if ($ot != 0) $page_z[] = array('code' => 3, 'page' => $page + $ot, 'info' => 'page', 'href' => $link . 'page=' . ($page + $ot));
            else            $page_z[] = array('code' => 2, 'page' => ($page + $ot), 'info' => 'selectpage', 'href' => '#');
        }
    }
    if ($page != $k_page) $page_z[] = array('code' => 3, 'page' => $k_page, 'info' => 'page', 'href' => $link . 'page=end');
    elseif ($k_page > 1) $page_z[] = array('code' => 4, 'page' => $k_page, 'info' => 'selectpage', 'href' => '#');
    if ($page < $k_page) $page_z[] = array('code' => 5, 'page' => ($page + 1), 'info' => 'end', 'href' => $link . 'page=' . ($page + 1));
    echo $twig->render('nav.tpl', array('nav' => $page_z, 'lang' => $lang));
}

function generate_password($number)
{
    $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', '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;
}

function acc($id)
{
    global $mysqli;
    $_tmp_d = mysqli_query($mysqli, "SELECT * FROM `users`  WHERE `id` = '$id' LIMIT 1");
    $_tmp_c = mysqli_num_rows($_tmp_d);
    if ($_tmp_c != 0) {
        $anks = mysqli_fetch_object($_tmp_d);
        return array('id' => $id, 'login' => $anks->login, 'data' => $anks);
    } else {
        return array('id' => $id, 'login' => 'Удален', 'data' => NULL);
    }
}

function wcore_mmenu()
{
    global $mysqli;
    $_tmp_mmenu = mysqli_query($mysqli, "SELECT * FROM `wcore_mmenu` ORDER BY `id`") or die("Ошибка запроса: " . mysqli_error($mysqli));
    $_tmp_mmenu_list = array();
    while ($mm = mysqli_fetch_assoc($_tmp_mmenu)) {
        $_tmp_mmenu_list[] = array(
            'id' => $mm['id'],
            'link' => $mm['link'],
            'icon' => $mm['icon'],
            'name' => $mm['name'],
            'count' => (!empty($mm['count_table']) ? mysqli_num_rows(mysqli_query($mysqli, "SELECT * FROM " . $mm['count_table'])) : 'n/a'),
        );
    }
    return $_tmp_mmenu_list;
}

function _mc($who, $where = NULL,$sum = false)
{
    global $mysqli;
    $_mc = mysqli_fetch_array(mysqli_query($mysqli, "SELECT ".($sum == false?'COUNT(*)':'SUM('.$sum.')')." FROM `$who` " . (isset($where) ? $where : null) . " "), MYSQLI_NUM);
    return $_mc[0]==""?0:$_mc[0];
}

function logFile($textLog)
{
    $file = WCORE_ROOT . '/data/logs/logFile.log';
    $text = '[' . date('Y-m-d H:i:s') . '] ' . $textLog . '
        ';//Выводим переданную переменную
    $fOpen = fopen($file, 'a');
    fwrite($fOpen, $text);
    fclose($fOpen);
}
function logPayment($textLog)
{
    $file = WCORE_ROOT . '/data/logs/logPayment.log';
    $text = '[' . date('Y-m-d H:i:s') . '] ' . $textLog . '
        ';//Выводим переданную переменную
    $fOpen = fopen($file, 'a');
    fwrite($fOpen, $text);
    fclose($fOpen);
}

function order_init($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    $returned = curl_exec($ch);
    curl_close($ch);
    return $returned;
}

function order_init_debug($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    $returned = curl_exec($ch);
    $error = curl_errno($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    return base64_encode(json_encode(array(
        'retur' =>  $returned ,
        'error' =>  $error,
        'info'  =>  $info
    )));
}

function isAjax()
{
    return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ? true : false);
}

/**
	 * Счетчик обратного отсчета
	 *
	 * @param mixed $date
	 * @return
	 */
	function downcounter($date,$stat=true){
	    $check_time = strtotime($date) - time();
	    if($check_time <= 0){
	        $false = true;
	    } else {
	        $false = false;
	    }

	    $days = floor($check_time/86400);
	    $hours = floor(($check_time%86400)/3600);
	    $minutes = floor(($check_time%3600)/60);
	    $seconds = $check_time%60; 

	    $str = '';
	    if($days > 0) $str .= declension($days,array('день','дня','дней')).' ';
	    if($hours > 0) $str .= declension($hours,array('час','часа','часов')).' ';
	    if($minutes > 0) $str .= declension($minutes,array('минута','минуты','минут')).' ';
	    if($seconds > 0) $str .= declension($seconds,array('секунда','секунды','секунд'));
	    
	    return $false == false?($stat == true?$str:$days.'д.'):$days.'д.';
	}


	/**
	 * Функция склонения слов
	 *
	 * @param mixed $digit
	 * @param mixed $expr
	 * @param bool $onlyword
	 * @return
	 */
	function declension($digit,$expr,$onlyword=false){
	    if(!is_array($expr)) $expr = array_filter(explode(' ', $expr));
	    if(empty($expr[2])) $expr[2]=$expr[1];
	    $i=preg_replace('/[^0-9]+/s','',$digit)%100;
	    if($onlyword) $digit='';
	    if($i>=5 && $i<=20) $res=$digit.' '.$expr[2];
	    else
	    {
	        $i%=10;
	        if($i==1) $res=$digit.' '.$expr[0];
	        elseif($i>=2 && $i<=4) $res=$digit.' '.$expr[1];
	        else $res=$digit.' '.$expr[2];
	    }
	    return trim($res);
	}
	
	function cunter_day_unix($data,$stat=true){
	    return downcounter(date("Y-m-d H:i:s",$data),$stat);
	}