Advertisement
Guest User

Aaron's registration script

a guest
Nov 23rd, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.56 KB | None | 0 0
  1. <?php
  2.  
  3. class register {
  4.  
  5.     public static function createAccount($data, $conn)  {
  6.  
  7.         $birthday = "1990-01-01";
  8.         $ipaddr = $_SERVER['REMOTE_ADDR'];
  9.         $username = $data['musername'];
  10.         $password = sha1($data['mpass']);
  11.         $email = $data['memail'];
  12.         $tempban = '1970-01-01 12:50:00';
  13.  
  14.         if (self::validate($data, $conn))   {
  15.  
  16.             echo '<div class="alert alert-success">You have succesfully registered!</div>';
  17.  
  18.             $stmt = $conn->prepare("INSERT INTO accounts (`name`, `password`, `ip`, `email`, `birthday`, `tempban`) VALUES (?, ?, ?, ?, ?, ?)");
  19.             $stmt->bind_param("ssssss", $username, $password, $ipaddr, $email, $birthday, $tempban);
  20.             $stmt->execute();
  21.  
  22.         }
  23.  
  24.     }
  25.  
  26.     public static function validate($data, $conn)   {
  27.  
  28.         if (!self::inputValidation($data["musername"], "username")) {
  29.  
  30.             self::throwMessage(6);
  31.             return false;
  32.  
  33.         }
  34.  
  35.         if (!self::inputValidation($data['memail'], "email"))   {
  36.  
  37.             self::throwMessage(3);
  38.             return false;
  39.  
  40.         }
  41.  
  42.         if (!self::inputValidation($data['mpass'], "password")) {
  43.  
  44.             self::throwMessage(5);
  45.             return false;
  46.  
  47.         }
  48.  
  49.         if ($data['mpass'] != $data['mpwcheck'])    {
  50.  
  51.             self::throwMessage(4);
  52.             return false;
  53.  
  54.         }
  55.  
  56.         if (!self::checkAvailability($data, $conn)) {
  57.  
  58.             self::throwMessage(2);
  59.             return false;
  60.  
  61.         }
  62.  
  63.  
  64.  
  65.         return true;
  66.  
  67.     }
  68.  
  69.     public static function inputValidation($object, $code)  {
  70.  
  71.         switch($code)   {
  72.  
  73.             case "username":
  74.                 if ((strlen($object) > 4) && (strlen($object) <= 12))   {
  75.  
  76.                     return true;
  77.  
  78.                 }
  79.                 break;
  80.  
  81.             case "email":
  82.                 if (strlen($object) >= 1)   {
  83.  
  84.                     if (filter_var($object, FILTER_VALIDATE_EMAIL)) {
  85.  
  86.                         return true;
  87.  
  88.                     } else {
  89.  
  90.                         return false;
  91.  
  92.                     }
  93.  
  94.                 } else {
  95.  
  96.                     return false;
  97.  
  98.                 }
  99.                 break;
  100.  
  101.             case "password":
  102.                 if (strlen($object) >= 6 && (strlen($object) <= 30))    {
  103.  
  104.                     return true;
  105.  
  106.                 } else {
  107.  
  108.                     return false;
  109.  
  110.                 }
  111.                 break;
  112.  
  113.             default:
  114.  
  115.                 self::throwMessage("WRONG");
  116.  
  117.                 break;
  118.  
  119.         }
  120.  
  121.     }
  122.  
  123.     public static function checkAvailability($data, $conn)  {
  124.  
  125.         $result1 = 0;
  126.         $result2 = 0;
  127.  
  128.         $username = $data['musername'];
  129.         $email = $data['memail'];
  130.  
  131.         $user = $conn->prepare("SELECT * FROM accounts WHERE name = ?");
  132.         $user->bind_param("s", $username);
  133.         $user->execute();
  134.         $user->store_result();
  135.  
  136.         if ($user->num_rows() > 0)  {
  137.  
  138.             $result1 = 1;
  139.  
  140.         }
  141.  
  142.         $email = $conn->prepare("SELECT * from accounts WHERE email = ?");
  143.         $email->bind_param("s", $email);
  144.         $user->execute();
  145.         $user->store_result();
  146.  
  147.         if ($email->num_rows() > 0) {
  148.  
  149.             $result2 = 1;
  150.  
  151.         }
  152.  
  153.         $results = $result1 + $result2;
  154.  
  155.         if ($results > 0)   {
  156.  
  157.             return false;
  158.  
  159.         } else {
  160.  
  161.             return true;
  162.  
  163.         }
  164.  
  165.     }
  166.  
  167.     public static function throwMessage($code)  {
  168.  
  169.         switch($code)   {
  170.  
  171.             case 1:
  172.                 echo '<div class="alert alert-success">You have succesfully registered!</div>';
  173.                 break;
  174.  
  175.             case 2:
  176.                 echo '<div class="alert alert-danger">The entered username or e-mail adress is already taken.</div>';
  177.                 break;
  178.  
  179.             case 3:
  180.                 echo '<div class="alert alert-danger">Your e-mail did not pass our validation.</div>';
  181.                 break;
  182.  
  183.             case 4:
  184.                 echo '<div class="alert alert-danger">Your passwords did not equal!</div>';
  185.                 break;
  186.  
  187.             case 5:
  188.                 echo '<div class="alert alert-danger">Your password did not pass our validation!</div>';
  189.                 break;
  190.  
  191.             case 6:
  192.                 echo '<div class="alert alert-danger">Your username did not pass our validation!</div>';
  193.                 break;
  194.  
  195.             default:
  196.                 echo '<div class="alert alert-danger">An unexpected error occured!</div>';
  197.                 break;
  198.  
  199.         }
  200.  
  201.     }
  202.  
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement