Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class register {
- public static function createAccount($data, $conn) {
- $birthday = "1990-01-01";
- $ipaddr = $_SERVER['REMOTE_ADDR'];
- $username = $data['musername'];
- $password = sha1($data['mpass']);
- $email = $data['memail'];
- $tempban = '1970-01-01 12:50:00';
- if (self::validate($data, $conn)) {
- echo '<div class="alert alert-success">You have succesfully registered!</div>';
- $stmt = $conn->prepare("INSERT INTO accounts (`name`, `password`, `ip`, `email`, `birthday`, `tempban`) VALUES (?, ?, ?, ?, ?, ?)");
- $stmt->bind_param("ssssss", $username, $password, $ipaddr, $email, $birthday, $tempban);
- $stmt->execute();
- }
- }
- public static function validate($data, $conn) {
- if (!self::inputValidation($data["musername"], "username")) {
- self::throwMessage(6);
- return false;
- }
- if (!self::inputValidation($data['memail'], "email")) {
- self::throwMessage(3);
- return false;
- }
- if (!self::inputValidation($data['mpass'], "password")) {
- self::throwMessage(5);
- return false;
- }
- if ($data['mpass'] != $data['mpwcheck']) {
- self::throwMessage(4);
- return false;
- }
- if (!self::checkAvailability($data, $conn)) {
- self::throwMessage(2);
- return false;
- }
- return true;
- }
- public static function inputValidation($object, $code) {
- switch($code) {
- case "username":
- if ((strlen($object) > 4) && (strlen($object) <= 12)) {
- return true;
- }
- break;
- case "email":
- if (strlen($object) >= 1) {
- if (filter_var($object, FILTER_VALIDATE_EMAIL)) {
- return true;
- } else {
- return false;
- }
- } else {
- return false;
- }
- break;
- case "password":
- if (strlen($object) >= 6 && (strlen($object) <= 30)) {
- return true;
- } else {
- return false;
- }
- break;
- default:
- self::throwMessage("WRONG");
- break;
- }
- }
- public static function checkAvailability($data, $conn) {
- $result1 = 0;
- $result2 = 0;
- $username = $data['musername'];
- $email = $data['memail'];
- $user = $conn->prepare("SELECT * FROM accounts WHERE name = ?");
- $user->bind_param("s", $username);
- $user->execute();
- $user->store_result();
- if ($user->num_rows() > 0) {
- $result1 = 1;
- }
- $email = $conn->prepare("SELECT * from accounts WHERE email = ?");
- $email->bind_param("s", $email);
- $user->execute();
- $user->store_result();
- if ($email->num_rows() > 0) {
- $result2 = 1;
- }
- $results = $result1 + $result2;
- if ($results > 0) {
- return false;
- } else {
- return true;
- }
- }
- public static function throwMessage($code) {
- switch($code) {
- case 1:
- echo '<div class="alert alert-success">You have succesfully registered!</div>';
- break;
- case 2:
- echo '<div class="alert alert-danger">The entered username or e-mail adress is already taken.</div>';
- break;
- case 3:
- echo '<div class="alert alert-danger">Your e-mail did not pass our validation.</div>';
- break;
- case 4:
- echo '<div class="alert alert-danger">Your passwords did not equal!</div>';
- break;
- case 5:
- echo '<div class="alert alert-danger">Your password did not pass our validation!</div>';
- break;
- case 6:
- echo '<div class="alert alert-danger">Your username did not pass our validation!</div>';
- break;
- default:
- echo '<div class="alert alert-danger">An unexpected error occured!</div>';
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement