ankitjain28may /
openchat
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
|
|||
| 2 | |||
| 3 | namespace ChatApp; |
||
| 4 | use ChatApp\Validate; |
||
| 5 | use ChatApp\Session; |
||
| 6 | require_once (dirname(__DIR__) . '/config/database.php'); |
||
| 7 | |||
| 8 | class Register |
||
| 9 | { |
||
| 10 | protected $error; |
||
| 11 | protected $key; |
||
| 12 | protected $obValidate; |
||
| 13 | protected $connect; |
||
| 14 | protected $session; |
||
| 15 | |||
| 16 | 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...
|
|||
| 17 | { |
||
| 18 | $this->error = array(); |
||
| 19 | $this->key = 0; |
||
| 20 | $this->connect = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); |
||
| 21 | $this->session = new Session(); |
||
| 22 | $this->obValidate = new Validate(); |
||
| 23 | |||
| 24 | } |
||
| 25 | |||
| 26 | public function authRegister($name, $email, $username, $password, $mob) |
||
| 27 | { |
||
| 28 | $name = trim($name); |
||
| 29 | $email = trim($email); |
||
| 30 | $username = trim($username); |
||
| 31 | $password = trim($password); |
||
| 32 | $mob = trim($mob); |
||
| 33 | $userId = ''; |
||
| 34 | |||
| 35 | if (empty($name)) { |
||
| 36 | $this->onError(["name" => " *Enter the name"]); |
||
| 37 | } |
||
| 38 | |||
| 39 | if(empty($email)) { |
||
| 40 | $this->onError(["email" => " *Enter the email address"]); |
||
| 41 | } |
||
| 42 | elseif(filter_var($email, FILTER_VALIDATE_EMAIL) == false) { |
||
| 43 | $this->onError(["email" => " *Enter correct Email address"]); |
||
| 44 | } |
||
| 45 | else |
||
| 46 | { |
||
| 47 | if($this->obValidate->validateEmailInDb($email) === 1) |
||
| 48 | { |
||
| 49 | |||
| 50 | $this->onError(["email" => " *Email is already registered"]); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | if(empty($username)) { |
||
| 55 | $this->onError(["username" => " *Enter the username"]); |
||
| 56 | } |
||
| 57 | else |
||
| 58 | { |
||
| 59 | if($this->obValidate->validateUsernameInDb($username) === 1) |
||
| 60 | { |
||
| 61 | |||
| 62 | $this->onError(["username" => " *Username is already registered"]); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | if(empty($password)) { |
||
| 67 | $this->onError(["password" => " *Enter the password"]); |
||
| 68 | } |
||
| 69 | |||
| 70 | if(empty($mob)) { |
||
| 71 | $this->onError(["mob" => " *Enter the Mobile Number"]); |
||
| 72 | } |
||
| 73 | elseif (!preg_match("/^[0-9]{10}$/", $mob)) { |
||
| 74 | $this->onError(["mob" => " *Enter correct Mobile Number"]); |
||
| 75 | } |
||
| 76 | |||
| 77 | if($this->key == 1) |
||
| 78 | { |
||
| 79 | return json_encode($this->error); |
||
| 80 | } |
||
| 81 | else |
||
| 82 | { |
||
| 83 | $this->key = 0; |
||
| 84 | $pass = md5($password); |
||
| 85 | $query = "INSERT INTO register VALUES(null, '$email', '$username', '$pass')"; |
||
| 86 | if(!$this->connect->query($query)) { |
||
| 87 | $this->key = 1; |
||
| 88 | echo "You are not registered || Error in registration2"; |
||
| 89 | } |
||
| 90 | else |
||
| 91 | { |
||
| 92 | $query = "SELECT id FROM register WHERE email = '$email'"; |
||
| 93 | if($result = $this->connect->query($query)) { |
||
| 94 | $row = $result->fetch_assoc(); |
||
| 95 | $userId = $row['id']; |
||
| 96 | |||
| 97 | $query = "INSERT INTO login VALUES('$userId', '$name', '$email', '$username', '$mob', 0)"; |
||
| 98 | if(!$this->connect->query($query)) { |
||
| 99 | $this->key = 1; |
||
| 100 | echo "You are not registered || Error in registration1"; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | if ($this->key == 0) { |
||
| 106 | $this->session->put('start', $userId); |
||
| 107 | return json_encode([ |
||
| 108 | "location" => URL."/account.php" |
||
| 109 | ]); |
||
| 110 | } |
||
| 111 | else |
||
| 112 | { |
||
| 113 | return json_encode($this->error); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | public function onError($value) |
||
| 118 | { |
||
| 119 | $this->key = 1; |
||
| 120 | $this->error = array_merge($this->error, $value); |
||
| 121 | } |
||
| 122 | } |
||
| 123 |
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.