|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
namespace ChatApp; |
|
4
|
|
|
use ChatApp\Session; |
|
5
|
|
|
require_once (dirname(__DIR__) . '/config/database.php'); |
|
6
|
|
|
|
|
7
|
|
|
class Login |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
protected $key; |
|
11
|
|
|
protected $error; |
|
12
|
|
|
protected $connect; |
|
13
|
|
|
protected $session; |
|
14
|
|
|
|
|
15
|
|
View Code Duplication |
public function __construct() |
|
|
|
|
|
|
16
|
|
|
{ |
|
17
|
|
|
$this->key = 0; |
|
18
|
|
|
$this->connect = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); |
|
19
|
|
|
$this->error = array(); |
|
20
|
|
|
$this->session = new Session(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function authLogin($login, $password) |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
$login = trim($login); |
|
27
|
|
|
$password = trim($password); |
|
28
|
|
|
|
|
29
|
|
|
if(empty($login)) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->key = 1; |
|
32
|
|
|
$this->error = array_merge($this->error, ["login" => " *Enter the login field"]); |
|
33
|
|
|
} |
|
34
|
|
|
elseif (preg_match("/^[@]{1}$/", $login)) |
|
35
|
|
|
{ |
|
36
|
|
|
if(filter_var($login, FILTER_VALIDATE_EMAIL) == false) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->key = 1; |
|
39
|
|
|
$this->error = array_merge($this->error, ["login" => " *Enter correct Email address"]); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
if(empty($password)) { |
|
43
|
|
|
$this->key = 1; |
|
44
|
|
|
$this->error = array_merge($this->error, ["password" => " *Enter the password"]); |
|
45
|
|
|
} |
|
46
|
|
|
else |
|
47
|
|
|
{ |
|
48
|
|
|
$password = md5($password); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if($this->key == 0) |
|
52
|
|
|
{ |
|
53
|
|
|
$query = "SELECT * FROM login WHERE email = '$login' or username = '$login'"; |
|
54
|
|
|
if ($result = $this->connect->query($query)) |
|
55
|
|
|
{ |
|
56
|
|
|
if ($result->num_rows > 0) |
|
57
|
|
|
{ |
|
58
|
|
|
$row = $result->fetch_assoc(); |
|
59
|
|
|
$loginID = $row['login_id']; |
|
60
|
|
|
$query = "SELECT id FROM register WHERE id = '$loginID' and password = '$password'"; |
|
61
|
|
|
if($result = $this->connect->query($query)) |
|
62
|
|
|
{ |
|
63
|
|
|
if ($result->num_rows > 0) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->session->put('start', $loginID); |
|
66
|
|
|
return json_encode([ |
|
67
|
|
|
"location" => URL."/account.php" |
|
68
|
|
|
]); |
|
69
|
|
|
} |
|
70
|
|
|
else |
|
71
|
|
|
{ |
|
72
|
|
|
$this->error = array_merge($this->error, ["password" => " *Invalid password"]); |
|
73
|
|
|
return json_encode($this->error); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
else |
|
78
|
|
|
{ |
|
79
|
|
|
$this->error = array_merge($this->error, ["login" => " *Invalid username or email"]); |
|
80
|
|
|
return json_encode($this->error); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
else |
|
86
|
|
|
{ |
|
87
|
|
|
return json_encode($this->error); |
|
88
|
|
|
} |
|
89
|
|
|
$this->connect->close(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.