Advertisement
fudo

AuthMe PHP integration | Full

Jul 6th, 2018
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.84 KB | None | 0 0
  1.  
  2. <?php
  3.  
  4.     // Пример авторизаций через AuthMe на сайте / Example for authorization via AuthMe on website.
  5.      
  6.     $mysql = array( // Настройка базы / DataBase configuration ===============
  7.      
  8.         "HOST" => "localhost",
  9.         "USER" => "root",
  10.         "PASS" => "",
  11.         "BASE" => "authme",
  12.         "TABL" => "authme"
  13.      
  14.     ); // ===========================================
  15.  
  16.     $con = mysqli_connect($mysql['HOST'], $mysql['USER'], $mysql['PASS'], $mysql['BASE']);
  17.  
  18.     if (mysqli_connect_errno()) {
  19.         die("Failed to connect to MySQL: " . mysqli_connect_error());
  20.     }
  21.  
  22.     function createSalt() {
  23.         return substr(md5(uniqid(rand(), TRUE)), 0, 3);
  24.     }
  25.  
  26.     function testAccountExists($name) {
  27.         global $con, $mysql;
  28.  
  29.         $query = mysqli_query($con, 'SELECT `id` FROM `'.$mysql['TABL'].'` where `username` = "'.mysqli_real_escape_string($con, $name).'"');
  30.         $rowcount=mysqli_num_rows($query);
  31.         if ($rowcount == 0 || $rowcount > 1 || 1 > $rowcount) {
  32.             return false;
  33.         }else{
  34.             return true;
  35.         }
  36.     }
  37.  
  38.     function checkAccountPassword($name, $password) {
  39.         global $con, $mysql;
  40.  
  41.         if (testAccountExists($name) == false) { return false; }
  42.  
  43.         $query = mysqli_query($con, 'SELECT `password` FROM `'.$mysql['TABL'].'` where `username` = "'.mysqli_real_escape_string($con, $name).'"');
  44.  
  45.         while ($row = mysqli_fetch_row($query))
  46.         {
  47.             $sha_info = explode('$', $row[0]);
  48.             if( $sha_info[1] === "SHA" ) {
  49.                 $salt = $sha_info[0];
  50.                 $sha256_password = hash('sha256', $password);
  51.                 $sha256_password .= $sha_info[2];
  52.                 if( strcasecmp(trim($sha_info[3]),hash('sha256', $sha256_password) ) == 0 ){
  53.                     return true;
  54.                 } else {
  55.                     return false;
  56.                 }
  57.             }
  58.         }
  59.     }
  60.  
  61.     function createHash($password) {
  62.         $salt = createSalt();
  63.  
  64.         return '$SHA$' . $salt . '$' . hash('sha256', hash('sha256', $password) . $salt);
  65.     }
  66.  
  67.     function registerAccount($name, $password, $regtime, $regip) {
  68.         global $con, $mysql;
  69.  
  70.         if (testAccountExists($name) == true) { return false; }
  71.  
  72.         $sql = 'INSERT INTO `'.$mysql['TABL'].'`(`username`, `realname`, `password`, `ip`, `lastlogin`, `regdate`, `regip`) VALUES ("'.mysqli_real_escape_string($con, $name).'", "'.mysqli_real_escape_string($con, strtolower($name)).'", "'.createHash($password).'", "'.$regip.'", "'.$regtime.'", "'.$regtime.'", "'.$regip.'")';
  73.        
  74.         if (mysqli_query($con, $sql)) {
  75.             return true;
  76.         } else {
  77.             return false;
  78.         }
  79.     }
  80.  
  81.  
  82.     // Код формы / Code of form
  83.  
  84.     if (!empty($_POST['itsAuth'])) {
  85.         switch ($_POST['itsAuth']) {
  86.             case '1': // Авторизация / Authorization
  87.  
  88.                 if (!empty($_POST['login']) && !empty($_POST['password'])) {
  89.                     if (checkAccountPassword($_POST['login'], $_POST['password']) == true) {
  90.                         echo "<div class='alert'>".$_POST['login'].", вы успешно вошли в аккаунт!</div>";
  91.                     } else {
  92.                         echo "<div class='alert'>Неверный логин или пароль!</div>";
  93.                     }
  94.                 }
  95.  
  96.                 break;
  97.            
  98.             case '2': // Регистрация / Register
  99.                 if (!empty($_POST['login']) && !empty($_POST['password']) && !empty($_POST['password2'])) {
  100.                     if ($_POST['password'] == $_POST['password2']) {
  101.  
  102.                         // Тут нет проверки на валидность ника, и пароля (preg_match) / There is no check on the validity of the nickname, and the password (preg_match)
  103.                         if (registerAccount($_POST['login'], $_POST['password'], time(), $_SERVER['REMOTE_ADDR']) == true) {
  104.                             echo "<div class='alert'>Успешно!</div>";
  105.                         } else {
  106.                             echo "<div class='alert'>Данный аккаунт уже существует!</div>";
  107.                         }
  108.                     } else {
  109.                         echo "<div class='alert'>Пароли не совпадают!</div>";
  110.                     }
  111.                 }
  112.  
  113.                 break;
  114.         }
  115.  
  116.     }
  117.  
  118.  
  119. ?>
  120. <!DOCTYPE html>
  121. <html>
  122. <head>
  123.     <title>AuthMe Auth via php script!</title>
  124.     <meta charset="utf-8">
  125. </head>
  126. <body>
  127.  
  128.     <style type="text/css">
  129.         body{
  130.             background-color: grey;
  131.         }
  132.         .block_form {
  133.             display: inline-block;
  134.             min-width: 100px;
  135.             max-width: 300px;
  136.             width: 100%;
  137.             margin-left: 10px;
  138.             margin-right: 10px;
  139.             margin-top: 20px;
  140.             margin-bottom: 20px;
  141.         }
  142.         .form_wrap {
  143.             display: block;
  144.             width: 320px;
  145.             margin-left: auto;
  146.             margin-right: auto;
  147.         }
  148.         .center_text {
  149.             text-align: center;
  150.         }
  151.         .wrapper {
  152.             width: 100%;
  153.             display: flex;
  154.         }
  155.         .block_header {
  156.             height: 60px;
  157.             background-color: brown;
  158.             border-radius: 5px 5px 0px 0;
  159.         }
  160.         .block_header > h3 {
  161.             line-height: 60px;
  162.             color: white;
  163.             font-weight: 700;
  164.         }
  165.         .block_content {
  166.             background-color: #efbaba;
  167.             padding-top: 10px;
  168.             padding-bottom: 20px;
  169.             padding-right: 10px;
  170.             padding-left: 10px;
  171.             border-radius: 0px 0px 5px 5px;
  172.         }
  173.         input[type=text], input[type=password] {
  174.             height: 20px;
  175.             padding: 5px;
  176.             color: black;
  177.             text-align: center;
  178.             background-color: white;
  179.             width: 100%;
  180.             margin-bottom: 10px;
  181.         }
  182.         input[type=submit] {
  183.             float: right;
  184.             height: 40px;
  185.         }
  186.         .btn-secondary{
  187.             float: right;
  188.             background-color: brown;
  189.         }
  190.         .btn-secondary:hover{
  191.             float: right;
  192.             background-color: brown;
  193.         }
  194.         .alert {
  195.             width: 300px;
  196.             position: absolute;
  197.             top: 5px;
  198.             left: 5px;
  199.             background-color: black;
  200.             color: white;
  201.             line-height: 40px;
  202.             padding-left: 10px;
  203.         }
  204.         .title {
  205.             padding-top: 30px;
  206.             padding-bottom: 10px;
  207.         }
  208.         .title > h1 {
  209.             text-align: center;
  210.         }
  211.     </style>
  212.  
  213.     <div class="wrapper">
  214.         <div class="form_wrap">
  215.             <div class="title">
  216.                 <h1>AuthMe скрипт</h1>
  217.             </div>
  218.             <div class="block_form">
  219.                 <form method="POST">
  220.                     <div class="block_header">
  221.                         <h3 class="center_text">Регистрация</h3> <!-- Registration -->
  222.                     </div>
  223.  
  224.                     <div class="block_content">
  225.                         <input type="text" name="login" placeholder="Ваш ник-нейм">
  226.                         <input type="password" name="password" placeholder="Ваш пароль">
  227.                         <input type="password" name="password2" placeholder="Повторите пароль">
  228.                        
  229.  
  230.                         <input type="hidden" name="itsAuth" value="2">
  231.  
  232.                         <br>
  233.  
  234.                         <input type="submit" value="Регистрация"></input> <!-- Registration -->
  235.                     </div>
  236.  
  237.                 </form>
  238.             </div>
  239.  
  240.             <div class="block_form">
  241.                 <form method="POST">
  242.                     <div class="block_header">
  243.                         <h3 class="center_text">Авторизация</h3> <!-- Authorization -->
  244.                     </div>
  245.  
  246.                     <div class="block_content">
  247.                         <input type="text" name="login" placeholder="Ваш ник-нейм">
  248.                         <input type="password" name="password" placeholder="Ваш пароль">
  249.  
  250.                         <input type="hidden" name="itsAuth" value="1">
  251.  
  252.                         <br>
  253.  
  254.                         <input type="submit" value="Авторизация"></input> <!-- Authorization -->
  255.                     </div>
  256.  
  257.                 </form>
  258.             </div>
  259.  
  260.         </div>
  261.  
  262.     </div>
  263.  
  264. </body>
  265. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement