View sourcecode

The following files exists in this folder. Click to view.

register.class.php

63 lines UTF-8 Unix (LF)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php 
class Register{
    private 
$username;
    private 
$raw_password;
    private 
$enc_password;
    public 
$error;
    public 
$success;
    private 
$storage "data.json";
    private 
$stored_users;
    private 
$new_user;

    public function 
__construct($username$password){
        
$this->username trim($username);
        
$this->username filter_var($this->usernameFILTER_SANITIZE_STRING);

        
$this->raw_password filter_var(trim($password), FILTER_SANITIZE_STRING);
        
$this->enc_password password_hash($this->raw_passwordPASSWORD_DEFAULT);

        
$this->stored_users json_decode(file_get_contents($this->storage), true);

        
$this->new_user = [
            
"username" => $this->username,
            
"password" => $this->enc_password,
            
"balance" => array(),
        ];

        if(
$this->checkFieldValues()){
            
$this->insertUser();
        }
    }

    public function 
checkFieldValues(){
        if(empty(
$this->username) || empty($this->raw_password)){
            
$this->error "Bägge fälten behöver fyllas i";

            return 
false;
        }else{
            return 
true;
        }
    }

    private function 
usernameExists(){
        foreach(
$this->stored_users as $user){
            if(
$this->username == $user["username"]){
                
$this->error "Användarnamnet finns redan, var god välj ett annat.";
                return 
true;
            }
        }
        return 
false;
    }

    private function 
insertUser(){
        if(
$this->usernameExists() == FALSE){
            
array_push($this->stored_users$this->new_user);
            if(
file_put_contents($this->storagejson_encode($this->stored_usersJSON_PRETTY_PRINT))){
                return 
$this->success "Kontot har skapats.";
            }else{
                return 
$this->error "Något gick åt helvete. Försök igen.";
            }
        }
    }
}
?>