This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | require_once("gfunctions.php"); |
||
3 | /** |
||
4 | * Class registration |
||
5 | * handles the user registration |
||
6 | */ |
||
7 | class Registration |
||
0 ignored issues
–
show
|
|||
8 | { |
||
9 | /** |
||
10 | * @var object $db_connection The database connection |
||
11 | */ |
||
12 | private $db_connection = null; |
||
13 | /** |
||
14 | * @var array $errors Collection of error messages |
||
15 | */ |
||
16 | public $errors = array(); |
||
17 | /** |
||
18 | * @var array $messages Collection of success / neutral messages |
||
19 | */ |
||
20 | public $messages = array(); |
||
21 | |||
22 | /** |
||
23 | * the function "__construct()" automatically starts whenever an object of this class is created, |
||
24 | * you know, when you do "$registration = new Registration();" |
||
25 | */ |
||
26 | public function __construct() |
||
27 | { |
||
28 | if (isset($_POST["register"])) { |
||
29 | $this->registerNewUser(); |
||
30 | } |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * handles the entire registration process. checks all error possibilities |
||
35 | * and creates a new user in the database if everything is fine |
||
36 | */ |
||
37 | private function registerNewUser() |
||
38 | { |
||
39 | $settings = require('config/settings.php'); |
||
40 | |||
41 | if (empty($_POST['user_name'])) { |
||
42 | $this->errors[] = "Empty Username"; |
||
43 | } elseif (empty($_POST['user_password_new']) || empty($_POST['user_password_repeat'])) { |
||
44 | $this->errors[] = "Empty Password"; |
||
45 | } elseif ($_POST['user_password_new'] !== $_POST['user_password_repeat']) { |
||
46 | $this->errors[] = "Password and password repeat are not the same"; |
||
47 | } elseif (strlen($_POST['user_password_new']) < 6) { |
||
48 | $this->errors[] = "Password has a minimum length of 6 characters"; |
||
49 | View Code Duplication | } elseif (strlen($_POST['user_name']) > 64 || strlen($_POST['user_name']) < 2) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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. ![]() |
|||
50 | $this->errors[] = "Username cannot be shorter than 2 or longer than 30 characters"; |
||
51 | } elseif (!preg_match('/^[a-z\d]{2,30}$/i', $_POST['user_name'])) { |
||
52 | $this->errors[] = "Username does not fit the name scheme: only a-Z and numbers are allowed, 2 to 64 characters"; |
||
53 | } elseif (empty($_POST['user_email'])) { |
||
54 | $this->errors[] = "Email cannot be empty"; |
||
55 | } elseif (strlen($_POST['user_email']) > 64) { |
||
56 | $this->errors[] = "Email cannot be longer than 64 characters"; |
||
57 | } elseif (!filter_var($_POST['user_email'], FILTER_VALIDATE_EMAIL)) { |
||
58 | $this->errors[] = "Your email address is not in a valid email format"; |
||
59 | } elseif (!empty($_POST['user_name']) |
||
60 | && strlen($_POST['user_name']) <= 64 |
||
61 | && strlen($_POST['user_name']) >= 2 |
||
62 | && preg_match('/^[a-z\d]{2,64}$/i', $_POST['user_name']) |
||
63 | && !empty($_POST['user_email']) |
||
64 | && strlen($_POST['user_email']) <= 64 |
||
65 | && filter_var($_POST['user_email'], FILTER_VALIDATE_EMAIL) |
||
66 | && !empty($_POST['user_password_new']) |
||
67 | && !empty($_POST['user_password_repeat']) |
||
68 | && ($_POST['user_password_new'] === $_POST['user_password_repeat']) |
||
69 | ) { |
||
70 | $temp_host = decrypt($settings['db']['host']); |
||
71 | $temp_user = decrypt($settings['db']['user']); |
||
72 | $temp_pass = decrypt($settings['db']['pass']); |
||
73 | $temp_name = decrypt($settings['db']['name']); |
||
74 | |||
75 | // create a database connection, using the constants from config/config.php (which we loaded in index.php) |
||
76 | $this->db_connection = new mysqli($temp_host, $temp_user, $temp_pass, $temp_name); |
||
77 | |||
78 | // change character set to utf8 and check it |
||
79 | if (!$this->db_connection->set_charset("utf8")) { |
||
80 | $this->errors[] = $this->db_connection->error; |
||
81 | } |
||
82 | |||
83 | // if no connection errors (= working database connection) |
||
84 | if (!$this->db_connection->connect_errno) { |
||
85 | |||
86 | // escaping, additionally removing everything that could be (html/javascript-) code |
||
87 | $user_name = $this->db_connection->real_escape_string(strip_tags($_POST['user_name'], ENT_QUOTES)); |
||
88 | $user_email = $this->db_connection->real_escape_string(strip_tags($_POST['user_email'], ENT_QUOTES)); |
||
89 | if(isset($_POST['player_id'])) { |
||
90 | $playerid = $this->db_connection->real_escape_string(strip_tags($_POST['player_id'], ENT_QUOTES)); |
||
91 | } |
||
92 | $user_password = $_POST['user_password_new']; |
||
93 | $user_pic = $_POST['profile_pic']; |
||
94 | $user_lvl = $_POST['user_lvl']; |
||
95 | |||
96 | // Ecrypt the user's password with PHP 5.5's password_hash() function, results in a 60 character |
||
97 | // hash string. the PASSWORD_DEFAULT constant is defined by the PHP 5.5, or if you are using |
||
98 | // PHP 5.3/5.4, by the password hashing compatibility library |
||
99 | $user_password_hash = password_hash($user_password, PASSWORD_DEFAULT); |
||
100 | |||
101 | // check if user or email address already exists |
||
102 | $sql = "SELECT * FROM `users` WHERE `user_name` = '" . $user_name . "' OR `user_email = '" . $user_email . "';"; |
||
103 | $query_check_user_name = $this->db_connection->query($sql); |
||
0 ignored issues
–
show
|
|||
104 | |||
105 | if ($query_check_user_name->num_rows == 1) { |
||
106 | $this->errors[] = "Sorry, that username / email address is already taken."; |
||
107 | |||
108 | } else { |
||
109 | |||
110 | $permissions = include 'config/permissions.php'; |
||
111 | $userPerms = json_encode($permissions[$user_lvl]); |
||
112 | // write new user's data into database |
||
113 | if (!empty($playerid)) { |
||
114 | |||
115 | $sql = "INSERT INTO `users` (`user_name`, `user_password_hash`, `user_email`, `playerid`, `user_level`, `permissions`, `user_profile`) VALUES |
||
116 | ('" . $user_name . "', '" . $user_password_hash . "', '" . $user_email . "', '" . $playerid . "', '" . $user_lvl . "', '" . $userPerms . "', '" . $user_pic . "');"; |
||
117 | } else { |
||
118 | $sql = "INSERT INTO `users` (`user_name`, `user_password_hash`, `user_email`, `user_level`, `permissions`, `user_profile`) VALUES |
||
119 | ('" . $user_name . "', '" . $user_password_hash . "', '" . $user_email . "', '" . $user_lvl . "', '" . $userPerms . "', '" . $user_pic . "');"; |
||
120 | } |
||
121 | $query_new_user_insert = $this->db_connection->query($sql); |
||
0 ignored issues
–
show
|
|||
122 | |||
123 | // if user has been added successfully |
||
124 | if ($query_new_user_insert) { |
||
125 | $this->messages[] = "Your account has been created"; |
||
126 | } else { |
||
127 | $this->errors[] = "Sorry, your registration failed. Please go back and try again."; |
||
128 | } |
||
129 | } |
||
130 | } else { |
||
131 | $this->errors[] = "Sorry, no database connection."; |
||
132 | } |
||
133 | } else { |
||
134 | $this->errors[] = "An unknown error occurred."; |
||
135 | } |
||
136 | } |
||
137 | } |
||
0 ignored issues
–
show
|
|||
138 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.