Created
January 14, 2019 18:42
-
-
Save deepdarksky/d4e351314895215268589d06bbaac90a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$configs = [ | |
//Подключение к базе | |
'database' => [ | |
'host' => '127.0.0.1', | |
'username' => 'mysql', | |
'password' => 'mysql', | |
'database' => 'pw', | |
], | |
//Настройки регистрации | |
'setting' => [ | |
'encode_type' => 1, // 1 - '0x' . md5($login . $password), 2 - base64_encode(md5($login . $password, true)) | |
'gold_count' => 1000, // Количество голда. 0 - отключить начисление голда. | |
'zone_id' => 1, | |
], | |
//Настройки reCaptcha | |
'reCaptcha' => [ | |
'enable' => false, // Включение/Отключение капчи | |
'public' => '6Lc2pIkUAAAAAHcgrXT39DK7ikfdU9iuFQWIrM7W', | |
'secret' => '6Lc2pIkUAAAAANDa0lAG12njKzhYOvwCs1IG81Pv', | |
], | |
//Настройки валидации полей | |
'validation' => [ | |
'login' => [ | |
'preg_match' => '/[^0-9A-Za-z]/', | |
'min_length' => '3', | |
'max_length' => '32', | |
], | |
'password' => [ | |
'preg_match' => '/[^0-9A-Za-z]/', | |
'min_length' => '3', | |
'max_length' => '32', | |
], | |
'repeat_password' => | |
[ | |
'preg_match' => '/[^0-9A-Za-z]/', | |
'min_length' => '3', | |
'max_length' => '32', | |
], | |
'email' => [ | |
'min_length' => '3', | |
'max_length' => '32', | |
], | |
], | |
]; | |
$dsn = sprintf('mysql:host=%s;dbname=%s', $configs['database']['host'], $configs['database']['database']); | |
$db = new PDO($dsn, $configs['database']['username'], $configs['database']['password']); | |
$view_success = false; | |
$register = isset($_POST['register']) ? (array)$_POST['register'] : false; | |
$errors = []; | |
if ($register) { | |
foreach ($register as $key => $row) { | |
$row = trim($row); | |
if (empty($row)) { | |
$errors[$key] = 'Поле обязательно к заполнению.'; | |
continue; | |
} | |
if (strlen($row) < $configs['validation'][$key]['min_length']) { | |
$errors[$key] = "Длина поля должна быть минимум {$configs['validation'][$key]['min_length']} символов."; | |
continue; | |
} | |
if (strlen($row) > $configs['validation'][$key]['max_length']) { | |
$errors[$key] = "Длина поля должна быть максимум {$configs['validation'][$key]['max_length']} символов."; | |
continue; | |
} | |
if ($key !== 'email' && preg_match($configs['validation'][$key]['preg_match'], $row)) { | |
$errors[$key] = 'В поле содержатся недоступстимые символы.'; | |
continue; | |
} | |
if ($key === 'email' && !filter_var($row, FILTER_VALIDATE_EMAIL)) { | |
$errors[$key] = 'Неверный Email адрес.'; | |
continue; | |
} | |
if ($key === 'login') { | |
$stmt = $db->prepare('SELECT 1 FROM users WHERE name=?'); | |
$stmt->execute([$row]); | |
if ($stmt->fetchColumn()) { | |
$errors[$key] = 'Логин уже используется.'; | |
continue; | |
} | |
} | |
if ($key === 'email') { | |
$stmt = $db->prepare('SELECT 1 FROM users WHERE email=?'); | |
$stmt->execute([$row]); | |
if ($stmt->fetchColumn()) { | |
$errors[$key] = 'Email уже используется.'; | |
continue; | |
} | |
} | |
} | |
if (!count($errors)) { | |
if (checkCaptcha()) { | |
$max_user_id = $db->prepare('SELECT MAX(id) AS id FROM users LIMIT 1'); | |
$max_user_id->execute([]); | |
$max_user_id = $max_user_id->fetchColumn(); | |
if (!$max_user_id) { | |
$max_user_id = 16; | |
} | |
$new_user_id = $max_user_id += 16; | |
$date = date('Y-m-d H:i:s'); | |
$new_user = [ | |
$new_user_id, | |
$register['login'], | |
passwordHash($register['login'], $register['password']), | |
$register['email'], | |
$date, | |
$_SERVER['REMOTE_ADDR'], | |
passwordHash($register['login'], $register['password']), | |
]; | |
$create = $db->prepare('INSERT INTO users SET id=?,name=?,passwd=?,email=?,creatime=?,qq=?,passwd2=?'); | |
$create->execute($new_user); | |
if ($configs['setting']['gold_count'] > 0) { | |
$new_cash = [ | |
$new_user_id, | |
$configs['setting']['zone_id'], | |
$configs['setting']['zone_id'], | |
$configs['setting']['gold_count'], | |
$date, | |
]; | |
$gold = $db->prepare("INSERT INTO usecashnow SET userid=?,zoneid=?,aid=?,cash=?,status='1',creatime=?"); | |
$gold->execute($new_cash); | |
} | |
$view_success = true; | |
} else { | |
$reCaptcha_error = 'Повторите попытку.'; | |
} | |
} | |
} | |
function passwordHash($login, $password) | |
{ | |
global $configs; | |
switch ($configs['setting']['encode_type']) { | |
case 1: | |
$salt = '0x' . md5($login . $password); | |
break; | |
case 2: | |
$salt = base64_encode(md5($login . $password, true)); | |
break; | |
default: | |
$salt = '0x' . md5($login . $password); | |
break; | |
} | |
return $salt; | |
} | |
function checkCaptcha() | |
{ | |
global $configs; | |
if (!$configs['reCaptcha']['enable']) { | |
return true; | |
} | |
$g_response = isset($_POST['g-recaptcha-response']) ? (string)$_POST['g-recaptcha-response'] : 0; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, "secret={$configs['reCaptcha']['secret']}&response=$g_response"); | |
$result = json_decode(curl_exec($ch), true); | |
curl_close($ch); | |
return isset($result['success']) && $result['success'] === true; | |
} | |
function getValue($input) | |
{ | |
if (isset($_POST['register'][$input])) { | |
return $_POST['register'][$input]; | |
} | |
return false; | |
} | |
function errorDisplay($text) | |
{ | |
if (isset($text)) { | |
echo sprintf('<p class="text text-danger">%s</p>', $text); | |
} | |
return false; | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="ru"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | |
<meta name="description" content="Регистрация"> | |
<title>Регистрация</title> | |
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous"> | |
<?php if ($configs['reCaptcha']['enable']): ?> | |
<script src='https://www.google.com/recaptcha/api.js'></script> | |
<?php endif; ?> | |
<body> | |
<div class="container"> | |
<div style="margin-top: 25px" class="col-6 offset-3"> | |
<div class="card"> | |
<?php if (!$view_success): ?> | |
<div class="card-header text-center">Регистрация на сервере</div> | |
<form name="register[]" action="register.php" method="post" accept-charset="utf-8"> | |
<div class="card-body"> | |
<div class="form-group"> | |
<label for="login">Логин</label> | |
<input id="login" name="register[login]" type="text" value="<?php echo getValue('login'); ?>" class="form-control"> | |
<small class="form-text text-muted">Логин аккаунта | |
от <?php echo $configs['validation']['login']['min_length']; ?> | |
до <?php echo $configs['validation']['login']['max_length']; ?> символов. | |
</small> | |
<?php isset($errors['password']) ? errorDisplay($errors['login']) : false; ?> | |
</div> | |
<div class="form-group"> | |
<label for="password">Пароль</label> | |
<input id="password" name="register[password]" type="password" value="<?php echo getValue('password'); ?>" class="form-control"> | |
<?php isset($errors['password']) ? errorDisplay($errors['password']) : false; ?> | |
</div> | |
<div class="form-group"> | |
<label for="email">Email</label> | |
<input id="email" name="register[email]" type="text" value="<?php echo getValue('email'); ?>" class="form-control"> | |
<small class="form-text text-muted">Email адресс используйется для восстановления пароля. | |
</small> | |
<?php isset($errors['email']) ? errorDisplay($errors['email']) : false; ?> | |
</div> | |
<?php if ($configs['reCaptcha']['enable']): ?> | |
<div class="form-group"> | |
<div class="g-recaptcha" data-sitekey="<?php echo $configs['reCaptcha']['public']; ?>"></div> | |
<?php isset($reCaptcha_error) ? errorDisplay($reCaptcha_error) : false; ?> | |
</div> | |
<?php endif; ?> | |
</div> | |
<div class="card-footer text-muted"> | |
<button type="submit" | |
class="btn btn-success btn-block">Создать аккаунт | |
</button> | |
</div> | |
</form> | |
<?php else: ?> | |
<div class="card-header text-center"> | |
Регистрация успешно завершена! | |
</div> | |
<div class="card-body"> | |
<p>Ваш логин: <b><?php echo getValue('login'); ?></b></p> | |
<p>Ваш пароль: <b><?php echo getValue('login'); ?></b></p> | |
</div> | |
<?php endif; ?> | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment