Completed
Push — master ( 35e057...d55c4d )
by Ankit
02:09
created

source/Login.php (1 issue)

Severity

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
2
3
namespace AnkitJain\RegistrationModule;
4
use AnkitJain\RegistrationModule\Session;
5
require_once (dirname(__DIR__) . '/config/database.php');
6
7
class Login
8
{
9
10
	protected $flag;
11
	protected $error;
12
	protected $connect;
13
14
	public function __construct()
15
	{
16
		$this->flag = 0;
17
		$this->connect = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
18
		$this->error = array();
19
	}
20
21
	public function authLogin($data)
22
	{
23
		$data = $this->emptyValue($data);
24
25
		$login = $data["login"];
26
		$password = $data["passLogin"];
27
28
29
		if (preg_match("/^[@]{1}$/", $login))
30
		{
31
			if(filter_var($login, FILTER_VALIDATE_EMAIL) == false)
32
			{
33
			$this->onError("login", " *Enter correct Email address");
34
			}
35
		}
36
37
		if($this->flag == 0)
38
		{
39
			$password = md5($password);
40
			$query = "SELECT * FROM login WHERE email = '$login' or username = '$login'";
41
			if ($result = $this->connect->query($query))
42
			{
43
				if ($result->num_rows > 0)
44
				{
45
					$row = $result->fetch_assoc();
46
					$loginID = $row['login_id'];
47
					$query = "SELECT id FROM register WHERE id = '$loginID' and password = '$password'";
48
					if($result = $this->connect->query($query))
49
					{
50
						if ($result->num_rows > 0)
51
						{
52
							Session::put('start', $loginID);
53
							return json_encode([
54
								"location" => URL."/account.php"
55
							]);
56
						}
57
						$this->onError("passLogin", " *Invalid password");
58
						return json_encode($this->error);
59
					}
60
					return json_encode(["Error" => "You are not registered, ".$this->connect->error ]);
61
				}
62
				$this->onError("login", " *Invalid username or email");
63
				return json_encode($this->error);
64
			}
65
			return json_encode(["Error" => "You are not registered, ".$this->connect->error ]);
66
		}
67
		else
68
		{
69
			return json_encode($this->error);
70
		}
71
		$this->connect->close();
0 ignored issues
show
$this->connect->close(); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
72
	}
73
74
	public function onError($key, $value)
75
	{
76
		$this->flag = 1;
77
		$this->error = array_merge($this->error, [["key" => $key, "value" => $value]]);
78
	}
79
80
	public function emptyValue($data)
81
	{
82
		$errorCode = array(
83
			"login" => " *Enter the login field",
84
			"passRegister" => " *Enter the password"
85
		);
86
87 View Code Duplication
		foreach ($data as $key => $value) {
88
			$data[$key] = trim($data[$key]);
89
			$value = trim($value);
90
			if(empty($value))
91
			{
92
				$this->onError($key, $errorCode[$key]);
93
			}
94
		}
95
		return $data;
96
	}
97
}
98