<?php

define('_IN_JOHNCMS', 1);
require('incfiles/core.php');
$textl = 'Hasap açmak';
$headmod = 'register';
require('incfiles/head.php');
$lng_reg = core::load_lng('registration');



$reg_nick = isset($_POST['nick']) ? trim($_POST['nick']) : '';
$lat_nick = functions::rus_lat(mb_strtolower($reg_nick));
$reg_pass = isset($_POST['password']) ? trim($_POST['password']) : '';
$reg_mail = isset($_POST['mail']) ? trim($_POST['mail']) : '';


if (isset($_POST['submit'])) {
    // Принимаем переменные
    $error = array();

    // Проверка Логина
    if (empty($reg_nick)) {
        $error['login'][] = $lng_reg['error_nick_empty'];
    } elseif (mb_strlen($reg_nick) < 2 || mb_strlen($reg_nick) > 15) {
        $error['login'][] = $lng_reg['error_nick_lenght'];
    }

    if (preg_match('/[^\da-z\-\@\*\(\)\?\!\~\_\=\[\]]+/', $lat_nick)) {
        $error['login'][] = $lng['error_wrong_symbols'];
    }
	
    // Проверка пароля
    if (empty($reg_pass)) {
        $error['password'][] = $lng['error_empty_password'];
    } elseif (mb_strlen($reg_pass) < 3 || mb_strlen($reg_pass) > 10) {
        $error['password'][] = $lng['error_wrong_lenght'];
    }

    if (preg_match('/[^\dA-Za-z]+/', $reg_pass)) {
        $error['password'][] = $lng['error_wrong_symbols'];
    }


    // Проверка переменных
    if (empty($error)) {
        $pass = md5(md5($reg_pass));
        // Проверка, занят ли ник
        $req = mysql_query("SELECT * FROM `users` WHERE `name_lat`='" . mysql_real_escape_string($lat_nick) . "'");
        if (mysql_num_rows($req) != 0) {
            $error['login'][] = 'Başga nik saýlaň!';
        }
        $req2 = mysql_query("SELECT * FROM `users` WHERE `mail`='" . mysql_real_escape_string($reg_mail) . "'");
        if (mysql_num_rows($req2) != 0) {
            $error['mail'][] = 'Başga poçta giriziň!';
        }
    }
    if (empty($error)) {
        $preg = $set['mod_reg'] > 1 ? 1 : 0;
        mysql_query("INSERT INTO `users` SET
            `name` = '" . mysql_real_escape_string($reg_nick) . "',
            `name_lat` = '" . mysql_real_escape_string($lat_nick) . "',
            `password` = '" . mysql_real_escape_string($pass) . "',
            `imname` = '" . mysql_real_escape_string($reg_nick) . "',
            `mail` = '" . mysql_real_escape_string($reg_mail) . "',
            `about` = '',
            `sex` = '',
            `rights` = '0',
            `ip` = '" . core::$ip . "',
            `ip_via_proxy` = '" . core::$ip_via_proxy . "',
            `browser` = '" . mysql_real_escape_string($agn) . "',
            `datereg` = '" . time() . "',
            `lastdate` = '" . time() . "',
            `sestime` = '" . time() . "',
            `preg` = '$preg',
            `set_user` = '',
            `set_forum` = '',
            `set_mail` = '',
            `smileys` = ''
        ") or exit(__LINE__ . ': ' . mysql_error());
        $usid = mysql_insert_id();

echo '
        <section id="app" class="py-4" >
            <div class="container">
        <div class="registration-form pt-5">
            <h1>Gutlaýarys!</h1>
            <h3>Hasabyňyz üstünlikli döredildi!<br/>
            Siziň ID: '.$usid.'<br/>
            Siziň LOGIN: '.$reg_nick.'<br/>
            Siziň AÇARSÖZ: '.$reg_pass.'<br/>
            Siziň EMAIL: '.$reg_mail.'<br/>
            Bellik, biziň email salgymyzdan siziň <b>'.$reg_mail.'</b> poçtaňyza kod barýar we siz şol kod arkaly öz profiliňiz aktiwlerşdirip bilýäňiz!
            </h3>
                        
                    <div class="sign-up"><a href="../">
                        <button class="btn">
                            Baş sahypa
                        </button></a>
                    </div>
                </div>';


            $_SESSION['uid'] = $usid;
            $_SESSION['ups'] = md5(md5($reg_pass));


        echo '</div>';
        require('incfiles/end.php');
        exit;
    }
}

/*
-----------------------------------------------------------------
Форма регистрации
-----------------------------------------------------------------
*/

echo '<form action="../registration.php" method="post">' .
    '<section id="app" class="py-4" >
            <div class="container">
        <div class="registration-form pt-5">
            <h1>Hoş geldiňiz</h1>
            <h3>Agza bolmak üçin boşluklary dolduryň</h3>
            <form action="registration.php" method="post" class="mx-4 mt-4">
                <input type="hidden" name="_token" value="">
                <div class="form-group">
                    <div class="input-group mb-3">
                        <div class="icon">
                            <img src="'.$set['homeurl'].'/icons/username.svg" alt="username">
                        </div>' .
    (isset($error['login']) ? '<span class="red"><small>' . implode('<br />', $error['login']) . '</small></span><br />' : '') .
    '<input type="text" name="nick" maxlength="15" value="' . htmlspecialchars($reg_nick) . '"' . (isset($error['login']) ? ' style="background-color: #FFCCCC"' : '') . ' class="login-form search form-control" placeholder="ulanyjy ady"/>' .
    '</div>' .
    '<div class="input-group mb-3"><div class="icon"><img src="'.$set['homeurl'].'/icons/password.svg" alt="password"></div>' .
    (isset($error['password']) ? '<span class="red"><small>' . implode('<br />', $error['password']) . '</small></span><br />' : '') .
    '<input type="text" name="password" maxlength="20" value="' . htmlspecialchars($reg_pass) . '"' . (isset($error['password']) ? ' style="background-color: #FFCCCC"' : '') . ' class="login-form search form-control" placeholder="açarsöz"/>' .
    '</div><div class="input-group mb-3">' .
    '<div class="icon"><img src="'.$set['homeurl'].'/icons/email2.svg" alt="email"></div>' .
    (isset($error['mail']) ? '<span class="red"><small>' . $error['mail'] . '</small></span><br />' : '') .
    '<input type="text" name="mail" maxlength="20" value="' . htmlspecialchars($reg_mail) . '"' . (isset($error['mail']) ? ' style="background-color: #FFCCCC"' : '') . ' class="login-form search form-control" placeholder="poçtaňyz"/>'.
    '</div><a href="'.$set['homeurl'].'/login" class="float-right hasap-bar">Hasabym bar</a>' .
    '<div class="sign-up"><input type="submit" name="submit" class="btn" value="Hasaby döret">' .
    '</div></from></div>';

require('incfiles/end.php');