<?php

define('_IN_JOHNCMS', 1);
require('incfiles/core.php');
$textl = 'Täze hasap döretmek';
$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_name))
        $error['imname'][] = $lng_reg['error_name_empty'];
    elseif (mb_strlen($reg_name) < 2 || mb_strlen($reg_name) > 30)
        $error['imname'][] = $lng_reg['error_wrong_lenght'];

    // Проверка пароля
    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));
        $reg_name = functions::check(mb_substr($reg_name, 0, 20));
        $reg_about = functions::check(mb_substr($reg_about, 0, 500));
        // Проверка, занят ли ник
        $req = mysql_query("SELECT * FROM `users` WHERE `name_lat`='" . mysql_real_escape_string($lat_nick) . "'");
        if (mysql_num_rows($req) != 0) {
            $error['login'][] = $lng_reg['error_nick_occupied'];
        }
    }
    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) . "',
            `about` = 'User',
            `mail` = '" . mysql_real_escape_string($reg_mail) . "',
            `sex` = 'm',
            `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">
                        <button href="../" class="btn">
                            Baş sahypa
                        </button>
                    </div>
                </div>';

        if ($set['mod_reg'] == 1) {
            echo '<p><span class="red"><b>' . $lng_reg['moderation_note'] . '</b></span></p>';
        } else {
            $_SESSION['uid'] = $usid;
            $_SESSION['ups'] = md5(md5($reg_pass));
            echo '<p><a href="' . $home . '">' . $lng_reg['enter'] . '</a></p>';
        }

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

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


echo '<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="'.$set['homeurl'].'/reg.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>

                        <input type="text" class="login-form search form-control" name="nick" value="" placeholder="ulanyjy ady">
                                            </div>

                    <div class="input-group mb-3">
                        <div class="icon">
                            <img src="'.$set['homeurl'].'/icons/password.svg" alt="password">
                        </div>
                        <input type="text" class="login-form search form-control" name="password" placeholder="açarsöz">
                                            </div>

                    <div class="input-group mb-3">
                        <div class="icon">
                            <img src="'.$set['homeurl'].'/icons/email2.svg" alt="email">
                        </div>

                        <input type="text" class="login-form search form-control" name="mail" value="" placeholder="poçta">
                                            </div>

                    <a href="'.$set['homeurl'].'/login" class="float-right hasap-bar">Hasabym bar</a>
                    <div class="sign-up">
                        <button type="submit" name="submit" class="btn">
                            Täze hasap döret
                        </button>
                    </div>
                </div>
            </form>';

require('incfiles/end.php');