Guest User

Untitled

a guest
Mar 15th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.00 KB | None | 0 0
  1. <?php
  2.  
  3. $gold = "true";
  4. $gold_count = "10000";
  5.  
  6. $mysql_host = "127.0.0.1";
  7. $mysql_username = "mysql";
  8. $mysql_password = "mysql";
  9. $mysql_database = "pw";
  10.  
  11. $recaptcha_key = "6LcDHRkUAAAAABhB-jxSs8uhY3DTKzNPU0U8ts3z";
  12. $recaptcha_secret_key = "6LcDHRkUAAAAAPPhmKzp6ESxLRRULnX7SCh7MJ2O";
  13.  
  14.  
  15. // 1 -  $salt = '0x' . md5($login . $password);
  16. // 2 -  $salt = base64_encode(md5($login . $password, true));
  17. $encode = '1';
  18.  
  19.  
  20. $validation_config = ['login' => ['preg_match' => '/[^0-9A-Za-z]/', 'min_length' => '3', 'max_length' => '32'],
  21.     'password' => ['preg_match' => '/[^0-9A-Za-z]/', 'min_length' => '3', 'max_length' => '32'],
  22.     'repeat_password' => ['preg_match' => '/[^0-9A-Za-z]/', 'min_length' => '3', 'max_length' => '32'],
  23.     'email' => ['min_length' => '3', 'max_length' => '32'],];
  24.  
  25. $errors = ['login' => null, 'password' => null, 'repeat_password' => null, 'email' => null];
  26. $view_success = false;
  27.  
  28. try {
  29.     $db = new PDO("mysql:host=$mysql_host;dbname=$mysql_database", $mysql_username, $mysql_password);
  30.     $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  31. } catch (PDOException $e) {
  32.     echo $e->getMessage();
  33. }
  34.  
  35. if (isset($_POST['register'])) {
  36.     $register = $_POST['register'];
  37.  
  38.     $check = 0;
  39.  
  40.     foreach ($register as $key => $row) {
  41.         $next_step = true;
  42.  
  43.         if ($row == null) {
  44.             $errors[$key] = "<p class='text text-danger'>Поле обязательно к заполнению.</p>";
  45.             $next_step = false;
  46.         }
  47.  
  48.         if ($next_step && strlen($row) < $validation_config[$key]['min_length']) {
  49.             $errors[$key] = "Длина поля должна быть минимум {$validation_config[$key]['min_length']} символов.";
  50.             $next_step = false;
  51.         }
  52.  
  53.         if ($next_step && strlen($row) > $validation_config[$key]['max_length']) {
  54.             $errors[$key] = "Длина поля должна быть максимум {$validation_config[$key]['max_length']} символов.";
  55.             $next_step = false;
  56.         }
  57.  
  58.         if ($next_step && $key != 'email' && preg_match($validation_config[$key]['preg_match'], $row)) {
  59.             $errors[$key] = "В поле содержатся недоступстимые символы.";
  60.             $next_step = false;
  61.         }
  62.  
  63.  
  64.         if ($next_step && $key == 'email' && !filter_var($row, FILTER_VALIDATE_EMAIL)) {
  65.             $errors[$key] = "Неверный Email адрес.";
  66.             $next_step = false;
  67.         }
  68.  
  69.         if ($next_step && $key == 'login') {
  70.             $stmt = $db->prepare("SELECT name FROM users WHERE name=?");
  71.             $stmt->execute([$row]);
  72.  
  73.             if ($stmt->fetchColumn()) {
  74.                 $errors[$key] = "Логин уже используется.";
  75.                 $next_step = false;
  76.             }
  77.         }
  78.  
  79.         if ($next_step && $key == 'email') {
  80.             $stmt = $db->prepare("SELECT email FROM users WHERE email=?");
  81.             $stmt->execute([$row]);
  82.  
  83.             if ($stmt->fetchColumn()) {
  84.                 $errors[$key] = "Email уже используется.";
  85.                 $next_step = false;
  86.             }
  87.         }
  88.  
  89.         if ($next_step) $check++;
  90.  
  91.     }
  92.  
  93.     if ($check == 3) {
  94.  
  95.         $url = "https://www.google.com/recaptcha/api/siteverify?secret=$recaptcha_secret_key&response="
  96.             . (array_key_exists('g-recaptcha-response', $_POST) ? $_POST["g-recaptcha-response"] : '')
  97.             . '&remoteip=' . $_SERVER['REMOTE_ADDR'];
  98.  
  99.         $response = json_decode(file_get_contents($url), true);
  100.  
  101.         if ($response['success'] == true) {
  102.  
  103.             $create = $db->prepare("call adduser(?, ?, '0','0', '0', '0', ?, '0', '0', '0', '0', '0', '0', '0', '0', ?, '0')");
  104.             $create->execute([$register['login'], password($register['login'], $register['password']), $register['email'], $_SERVER["REMOTE_ADDR"]]);
  105.  
  106.             if($gold) {
  107.                 $get_user_id = $db->prepare("SELECT ID FROM users WHERE name=?");
  108.                 $get_user_id->execute([$register['login']]);
  109.  
  110.                 $user_id = $get_user_id->fetchColumn();
  111.  
  112.                 $create = $db->prepare("call usecash(?,1,0,1,0,?,1,@error)");
  113.                 $create->execute([$user_id, $gold_count . '00']);
  114.             }
  115.  
  116.             $view_success = true;
  117.  
  118.         } else {
  119.             $recaptcha_error = "Повторите попытку.";
  120.         }
  121.  
  122.     }
  123. }
  124.  
  125. function password($login, $password)
  126. {
  127.     global $encode;
  128.  
  129.     switch ($encode) {
  130.         case 1:
  131.             $salt = '0x' . md5($login . $password);
  132.             break;
  133.         case 2:
  134.             $salt = base64_encode(md5($login . $password, true));
  135.             break;
  136.         default:
  137.             $salt = '0x' . md5($login . $password);
  138.             break;
  139.     }
  140.  
  141.     return $salt;
  142. }
  143.  
  144. function set_value($input)
  145. {
  146.     if (isset($_POST['register'][$input])) {
  147.         return $_POST['register'][$input];
  148.     } else {
  149.         return false;
  150.     }
  151. }
  152.  
  153. function error($text)
  154. {
  155.     if (isset($text)) {
  156.         return "<p class='text text-danger'>" . $text . "</p>";
  157.     } else {
  158.         return false;
  159.     }
  160. }
  161.  
  162. ?>
  163. <!DOCTYPE html>
  164. <html lang="ru">
  165. <head>
  166.     <meta charset="utf-8">
  167.     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  168.     <meta name="description" content="Регистрация">
  169.     <meta name="author" content="netmagic">
  170.  
  171.     <title>Регистрация</title>
  172.  
  173.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css"
  174.           integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
  175.     <script src='https://www.google.com/recaptcha/api.js'></script>
  176.  
  177.     <style>
  178.         .box {
  179.             padding: 15px;
  180.             margin-top: 10px;
  181.             background-color: rgba(0, 0, 0, 0.05);
  182.             border: rgba(0, 0, 0, 0.1) 1px solid;
  183.             border-top: transparent 3px solid;
  184.  
  185.             border-radius: 3px;
  186.             -moz-border-radius: 3px;
  187.             -webkit-border-radius: 3px;
  188.         }
  189.  
  190.         .box .box-title {
  191.             margin-bottom: 20px;
  192.             border-bottom: rgba(0, 0, 0, 0.1) 1px solid;
  193.             text-align: center;
  194.         }
  195.     </style>
  196.  
  197. <body>
  198. <div class="container-fluid">
  199.     <div class="col-md-4 offset-md-4">
  200.         <div class="box">
  201.  
  202.             <div class="box-title">
  203.                 <h3>Регистрация</h3>
  204.             </div>
  205.             <?php if (!$view_success): ?>
  206.                 <form name="register[]" action="register.php" method="post" accept-charset="utf-8">
  207.                     <div class="form-group">
  208.                         <label>Логин</label>
  209.                         <input name="register[login]" type="text" value="<?php echo set_value('login'); ?>"
  210.                                class="form-control">
  211.                         <small class="form-text text-muted">Логин аккаунта
  212.                             от <?php echo $validation_config['login']['min_length']; ?>
  213.                             до <?php echo $validation_config['login']['max_length']; ?> символов.
  214.                         </small>
  215.                         <?php echo error($errors['login']); ?>
  216.                     </div>
  217.  
  218.                     <div class="form-group">
  219.                         <label>Пароль</label>
  220.                         <input name="register[password]" type="password" value="<?php echo set_value('password'); ?>"
  221.                                class="form-control">
  222.                         <?php echo error($errors['password']); ?>
  223.                     </div>
  224.  
  225.                     <div class="form-group">
  226.                         <label>Email</label>
  227.                         <input name="register[email]" type="text" value="<?php echo set_value('email'); ?>"
  228.                                class="form-control">
  229.                         <small class="form-text text-muted">Email адресс используйется для восстановления пароля.
  230.                         </small>
  231.                         <?php echo error($errors['email']); ?>
  232.                     </div>
  233.  
  234.                     <div class="form-group">
  235.                         <div class="g-recaptcha" data-sitekey="<?php echo $recaptcha_key; ?>"></div>
  236.                         <?php echo error($recaptcha_error); ?>
  237.                     </div>
  238.  
  239.                     <button type="submit" class="btn btn-success">Создать аккаунт</button>
  240.  
  241.                 </form>
  242.             <?php else: ?>
  243.                 <div class="alert alert-success" role="alert">
  244.                     <p><strong>Регистрация</strong> успешно завершена!</p>
  245.                     <p>Ваш логин: <?php echo set_value('login'); ?></p>
  246.                     <p>Ваш пароль: <?php echo set_value('login'); ?></p>
  247.                 </div>
  248.             <?php endif; ?>
  249.         </div>
  250.     </div>
  251. </div>
  252.  
  253. </body>
  254. </html>
Add Comment
Please, Sign In to add comment