]>
Commit | Line | Data |
---|---|---|
ea27475b VP |
1 | <?php |
2 | ||
3 | require_once "SQL/sql.php"; | |
ce9cf366 | 4 | require_once "SQL/settings.php"; |
4d634d0a | 5 | |
ea27475b VP |
6 | class sql_auth |
7 | { | |
b44a2e97 | 8 | public $name = "SQLAuth"; |
ea27475b VP |
9 | public $author = "Valware"; |
10 | public $version = "1.0"; | |
11 | public $description = "Provides a User Auth and Management Panel with an SQL backend"; | |
b65f0496 | 12 | public $email = "v.a.pond@outlook.com"; |
ea27475b VP |
13 | |
14 | function __construct() | |
15 | { | |
5015c85c | 16 | self::create_tables(); |
b44a2e97 | 17 | Hook::func(HOOKTYPE_PRE_HEADER, 'sql_auth::session_start'); |
33f512fa | 18 | Hook::func(HOOKTYPE_FOOTER, 'sql_auth::add_footer_info'); |
6930484c VP |
19 | Hook::func(HOOKTYPE_USER_LOOKUP, 'sql_auth::get_user'); |
20 | Hook::func(HOOKTYPE_USERMETA_ADD, 'sql_auth::add_usermeta'); | |
21 | Hook::func(HOOKTYPE_USERMETA_DEL, 'sql_auth::del_usermeta'); | |
22 | Hook::func(HOOKTYPE_USERMETA_GET, 'sql_auth::get_usermeta'); | |
180b8ec1 VP |
23 | Hook::func(HOOKTYPE_USER_CREATE, 'sql_auth::user_create'); |
24 | Hook::func(HOOKTYPE_GET_USER_LIST, 'sql_auth::get_user_list'); | |
25 | Hook::func(HOOKTYPE_USER_DELETE, 'sql_auth::user_delete'); | |
5a7f0cde | 26 | Hook::func(HOOKTYPE_EDIT_USER, 'sql_auth::edit_core'); |
b6a7129b | 27 | Hook::func(HOOKTYPE_PRE_OVERVIEW_CARD, 'sql_auth::add_pre_overview_card'); |
c00c34d2 | 28 | AuthModLoaded::$status = 1; |
4d634d0a VP |
29 | |
30 | if (defined('SQL_DEFAULT_USER')) // we've got a default account | |
31 | { | |
6930484c | 32 | $lkup = new PanelUser(SQL_DEFAULT_USER['username']); |
4d634d0a VP |
33 | |
34 | if (!$lkup->id) // doesn't exist, add it with full privileges | |
35 | { | |
180b8ec1 VP |
36 | $user = []; |
37 | $user['user_name'] = SQL_DEFAULT_USER['username']; | |
38 | $user['user_pass'] = SQL_DEFAULT_USER['password']; | |
39 | $user['err'] = ""; | |
40 | create_new_user($user); | |
4d634d0a | 41 | } |
79d2194c VP |
42 | $lkup = new PanelUser(SQL_DEFAULT_USER['username']); |
43 | if (!user_can($lkup, PERMISSION_MANAGE_USERS)) | |
44 | $lkup->add_permission(PERMISSION_MANAGE_USERS); | |
4d634d0a | 45 | } |
ea27475b VP |
46 | } |
47 | ||
ea27475b | 48 | |
33f512fa VP |
49 | public static function add_footer_info($empty) |
50 | { | |
94890799 | 51 | if (!($user = unreal_get_current_user())) |
33f512fa VP |
52 | return; |
53 | ||
54 | else { | |
55 | echo "<code>Admin Panel v" . WEBPANEL_VERSION . "</code>"; | |
56 | } | |
57 | } | |
58 | ||
b6a7129b BM |
59 | public static function add_pre_overview_card($empty) |
60 | { | |
61 | if (defined('SQL_DEFAULT_USER')) | |
62 | Message::Fail("Warning: SQL_DEFAULT_USER is set in config.php. You should remove that item now, as it is only used during installation."); | |
63 | } | |
64 | ||
3a8ffab8 | 65 | /* pre-Header hook */ |
b44a2e97 VP |
66 | public static function session_start($n) |
67 | { | |
06369f59 VP |
68 | if (!isset($_SESSION)) |
69 | { | |
70 | session_set_cookie_params(3600); | |
71 | session_start(); | |
72 | } | |
454379e3 | 73 | if (!isset($_SESSION['id']) || empty($_SESSION)) |
b44a2e97 | 74 | { |
bc75e1cb | 75 | $current_page = $_SERVER['REQUEST_URI']; |
ce9cf366 VP |
76 | $tok = split($_SERVER['SCRIPT_FILENAME'], "/"); |
77 | if ($check = security_check() && $tok[count($tok) - 1] !== "error.php") { | |
78 | header("Location: " . BASE_URL . "plugins/sql_auth/error.php"); | |
79 | die(); | |
80 | } | |
bc75e1cb | 81 | header("Location: ".BASE_URL."login/?redirect=".urlencode($current_page)); |
454379e3 | 82 | die(); |
b44a2e97 | 83 | } |
08ce3aa7 VP |
84 | else |
85 | { | |
94890799 | 86 | if (!unreal_get_current_user()) // user no longer exists |
08ce3aa7 VP |
87 | { |
88 | session_destroy(); | |
321b7b81 | 89 | header("Location: ".BASE_URL."login"); |
f5e3ecee | 90 | die(); |
08ce3aa7 | 91 | } |
e3e93dde | 92 | // you'll be automatically logged out after one hour of inactivity |
08ce3aa7 | 93 | } |
b44a2e97 | 94 | } |
ea27475b | 95 | |
ce9cf366 VP |
96 | /** |
97 | * Create the tables we'll be using in the SQLdb | |
98 | * @return void | |
99 | */ | |
5015c85c VP |
100 | public static function create_tables() |
101 | { | |
46e8f612 VP |
102 | $script = $_SERVER['SCRIPT_FILENAME']; |
103 | if (str_ends_with($script,"setup.php")) | |
104 | return; | |
5015c85c | 105 | $conn = sqlnew(); |
46e8f612 VP |
106 | $stmt = $conn->query("SHOW TABLES LIKE '".SQL_PREFIX."%'"); |
107 | if ($stmt->rowCount() < 5) | |
108 | header("Location: ".BASE_URL."plugins/sql_auth/setup.php"); | |
5015c85c VP |
109 | } |
110 | ||
6930484c VP |
111 | /* We convert $u with a full user as an object ;D*/ |
112 | public static function get_user(&$u) | |
113 | { | |
114 | $id = $u['id']; | |
115 | $name = $u['name']; | |
116 | $conn = sqlnew(); | |
117 | ||
118 | if ($id) | |
119 | { | |
120 | $prep = $conn->prepare("SELECT * FROM " . SQL_PREFIX . "users WHERE user_id = :id LIMIT 1"); | |
121 | $prep->execute(["id" => strtolower($id)]); | |
122 | } | |
123 | elseif ($name) | |
124 | { | |
125 | $prep = $conn->prepare("SELECT * FROM " . SQL_PREFIX . "users WHERE LOWER(user_name) = :name LIMIT 1"); | |
126 | $prep->execute(["name" => strtolower($name)]); | |
127 | } | |
128 | $data = NULL; | |
129 | $obj = (object) []; | |
130 | if ($prep) | |
131 | $data = $prep->fetchAll(); | |
132 | if (isset($data[0]) && $data = $data[0]) | |
133 | { | |
134 | $obj->id = $data['user_id']; | |
135 | $obj->username = $data['user_name']; | |
136 | $obj->passhash = $data['user_pass']; | |
137 | $obj->first_name = $data['user_fname'] ?? NULL; | |
138 | $obj->last_name = $data['user_lname'] ?? NULL; | |
139 | $obj->created = $data['created']; | |
140 | $obj->bio = $data['user_bio']; | |
9a674833 | 141 | $obj->email = $data['user_email']; |
6930484c VP |
142 | $obj->user_meta = (new PanelUser_Meta($obj->id))->list; |
143 | } | |
144 | $u['object'] = $obj; | |
145 | } | |
146 | ||
147 | public static function get_usermeta(&$u) | |
148 | { | |
6930484c VP |
149 | $list = &$u['meta']; |
150 | $id = $u['id']; | |
151 | $conn = sqlnew(); | |
152 | if (isset($id)) | |
153 | { | |
154 | $prep = $conn->prepare("SELECT * FROM " . SQL_PREFIX . "user_meta WHERE user_id = :id"); | |
155 | $prep->execute(["id" => $id]); | |
156 | } | |
157 | foreach ($prep->fetchAll() as $row) | |
158 | { | |
159 | $list[$row['meta_key']] = $row['meta_value']; | |
160 | } | |
161 | } | |
162 | ||
163 | public static function add_usermeta(&$meta) | |
164 | { | |
da6fa2d1 | 165 | $meta = $meta['meta']; |
6930484c VP |
166 | $conn = sqlnew(); |
167 | /* check if it exists first, update it if it does */ | |
168 | $query = "SELECT * FROM " . SQL_PREFIX . "user_meta WHERE user_id = :id AND meta_key = :key"; | |
169 | $stmt = $conn->prepare($query); | |
170 | $stmt->execute(["id" => $meta['id'], "key" => $meta['key']]); | |
171 | if ($stmt->rowCount()) // it exists, update instead of insert | |
172 | { | |
173 | $query = "UPDATE " . SQL_PREFIX . "user_meta SET meta_value = :value WHERE user_id = :id AND meta_key = :key"; | |
174 | $stmt = $conn->prepare($query); | |
175 | $stmt->execute($meta); | |
176 | if ($stmt->rowCount()) | |
177 | return true; | |
178 | return false; | |
179 | } | |
180 | ||
181 | else | |
182 | { | |
183 | $query = "INSERT INTO " . SQL_PREFIX . "user_meta (user_id, meta_key, meta_value) VALUES (:id, :key, :value)"; | |
184 | $stmt = $conn->prepare($query); | |
185 | $stmt->execute($meta); | |
186 | if ($stmt->rowCount()) | |
187 | return true; | |
188 | return false; | |
189 | } | |
190 | } | |
191 | public static function del_usermeta(&$u) | |
192 | { | |
193 | $conn = sqlnew(); | |
194 | $query = "DELETE FROM " . SQL_PREFIX . "user_meta WHERE user_id = :id AND meta_key = :key"; | |
195 | $stmt = $conn->prepare($query); | |
196 | $stmt->execute($u['meta']); | |
197 | if ($stmt->rowCount()) | |
198 | return true; | |
199 | return false; | |
200 | } | |
180b8ec1 VP |
201 | public static function user_create(&$u) |
202 | { | |
203 | $username = $u['user_name']; | |
9f9d16d5 VP |
204 | $first_name = $u['fname'] ?? NULL; |
205 | $last_name = $u['lname'] ?? NULL; | |
206 | $password = $u['user_pass'] ?? NULL; | |
207 | $user_bio = $u['user_bio'] ?? NULL; | |
208 | $user_email = $u['user_email'] ?? NULL; | |
180b8ec1 | 209 | $conn = sqlnew(); |
9a674833 VP |
210 | $prep = $conn->prepare("INSERT INTO " . SQL_PREFIX . "users (user_name, user_pass, user_fname, user_lname, user_bio, user_email, created) VALUES (:name, :pass, :fname, :lname, :user_bio, :user_email, :created)"); |
211 | $prep->execute(["name" => $username, "pass" => $password, "fname" => $first_name, "lname" => $last_name, "user_bio" => $user_bio, "user_email" => $user_email, "created" => date("Y-m-d H:i:s")]); | |
180b8ec1 VP |
212 | if ($prep->rowCount()) |
213 | $u['success'] = true; | |
214 | else | |
215 | $u['errmsg'][] = "Could not add user"; | |
216 | } | |
217 | ||
218 | public static function get_user_list(&$list) | |
219 | { | |
220 | $conn = sqlnew(); | |
221 | $result = $conn->query("SELECT user_id FROM " . SQL_PREFIX . "users"); | |
222 | if (!$result) // impossible | |
223 | { | |
224 | die("Something went wrong."); | |
225 | } | |
226 | $userlist = []; | |
227 | while($row = $result->fetch()) | |
228 | { | |
229 | $userlist[] = new PanelUser(NULL, $row['user_id']); | |
230 | } | |
231 | if (!empty($userlist)) | |
232 | $list = $userlist; | |
233 | ||
234 | } | |
235 | public static function user_delete(&$u) | |
236 | { | |
237 | $user = $u['user']; | |
238 | $query = "DELETE FROM " . SQL_PREFIX . "users WHERE user_id = :id"; | |
239 | $conn = sqlnew(); | |
240 | $stmt = $conn->prepare($query); | |
241 | $stmt->execute(["id" => $user->id]); | |
242 | $deleted = $stmt->rowCount(); | |
243 | if ($deleted) | |
244 | { | |
245 | $u['info'][] = "Successfully deleted user \"$user->username\""; | |
246 | $u['boolint'] = 1; | |
247 | } else { | |
248 | $u['info'][] = "Unknown error"; | |
249 | $u['boolint'] = 0; | |
250 | } | |
251 | } | |
5a7f0cde VP |
252 | |
253 | public static function edit_core($arr) | |
254 | { | |
255 | $conn = sqlnew(); | |
256 | $user = $arr['user']; | |
257 | $info = $arr['info']; | |
258 | foreach($info as $key => $val) | |
259 | { | |
e9c858fd | 260 | $value = NULL; |
8a73256b | 261 | if (!$val || !strlen($val) || BadPtr($val)) |
5a7f0cde | 262 | continue; |
e9c858fd VP |
263 | if (!strcmp($key,"update_fname") && $val != $user->first_name) |
264 | { | |
5a7f0cde | 265 | $value = "user_fname"; |
e9c858fd VP |
266 | $valuestr = "first name"; |
267 | } | |
268 | elseif (!strcmp($key,"update_lname") && $val != $user->last_name) | |
269 | { | |
5a7f0cde | 270 | $value = "user_lname"; |
e9c858fd VP |
271 | $valuestr = "last name"; |
272 | } | |
273 | elseif (!strcmp($key,"update_bio") && $val != $user->bio) | |
274 | { | |
5a7f0cde | 275 | $value = "user_bio"; |
e9c858fd VP |
276 | $valuestr = "bio"; |
277 | } | |
5a7f0cde | 278 | elseif (!strcmp($key,"update_pass") || !strcmp($key,"update_pass_conf")) |
e9c858fd | 279 | { |
5a7f0cde | 280 | $value = "user_pass"; |
e9c858fd VP |
281 | $valuestr = "password"; |
282 | } | |
283 | elseif(!strcmp($key,"update_email") && $val != $user->email) | |
284 | { | |
5a7f0cde | 285 | $value = "user_email"; |
e9c858fd VP |
286 | $valuestr = "email address"; |
287 | } | |
288 | ||
289 | if (!$value) | |
290 | continue; | |
5a7f0cde VP |
291 | $query = "UPDATE " . SQL_PREFIX . "users SET $value=:value WHERE user_id = :id"; |
292 | $stmt = $conn->prepare($query); | |
293 | $stmt->execute(["value" => $val, "id" => $user->id]); | |
e9c858fd VP |
294 | |
295 | if (!$stmt->rowCount() && $stmt->errorInfo()[0] != "00000") | |
296 | Message::Fail("Could not update $valuestr for $user->username: ".$stmt->errorInfo()[0]." (CODE: ".$stmt->errorCode().")"); | |
297 | ||
298 | else | |
299 | Message::Success("Successfully updated the $valuestr for $user->username"); | |
5a7f0cde VP |
300 | } |
301 | } | |
ce9cf366 VP |
302 | } |
303 | ||
304 | ||
305 | function security_check() | |
306 | { | |
307 | $ip = $_SERVER['REMOTE_ADDR']; | |
308 | if (dnsbl_check($ip)) | |
309 | return true; | |
310 | ||
311 | else if (fail2ban_check($ip)) | |
312 | { | |
313 | ||
314 | } | |
315 | } | |
316 | ||
317 | function dnsbl_check($ip) | |
318 | { | |
5a7f0cde VP |
319 | |
320 | if (!defined('DNSBL')) | |
321 | return; | |
ce9cf366 VP |
322 | $dnsbl_lookup = DNSBL; |
323 | ||
324 | // clear variable just in case | |
325 | $listed = NULL; | |
326 | ||
327 | // if the IP was not given because you're an idiot, stop processing | |
328 | if (!$ip) { return; } | |
329 | ||
330 | // get the first two segments of the IPv4 | |
331 | $because = split($ip, "."); // why you | |
332 | $you = $because[1]; // gotta play | |
333 | $want = $because[2]; // that song | |
334 | $to = $you.".".$want."."; // so loud? | |
335 | ||
336 | // exempt local connections because sometimes they get a false positive | |
337 | if ($to == "192.168." || $to == "127.0.") { return NULL; } | |
338 | ||
339 | // you spin my IP right round, right round, to check the records baby, right round-round-round | |
340 | $reverse_ip = glue(array_reverse(split($ip, ".")), "."); | |
341 | ||
342 | // checkem | |
343 | foreach ($dnsbl_lookup as $host) { | |
344 | ||
345 | //if it was listed | |
346 | if (checkdnsrr($reverse_ip . "." . $host . ".", "A")) { | |
347 | ||
348 | //take note | |
349 | $listed = $host; | |
350 | } | |
351 | } | |
352 | ||
353 | // if it was safe, return NOTHING | |
354 | if (!$listed) { | |
355 | return NULL; | |
356 | } | |
357 | ||
358 | // else, you guessed it, return where it was listed | |
359 | else { | |
360 | return $listed; | |
361 | } | |
362 | } | |
363 | ||
364 | function fail2ban_check($ip) | |
33f512fa VP |
365 | { |
366 | ||
367 | } |