Test Setup Failed
Push — master ( a87e27...976b69 )
by Ankit
02:24
created

src/Login.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 7 and the first side effect is on line 5.

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.

Loading history...
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()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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