]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | require_once "SQL/sql.php"; | |
4 | require_once "SQL/settings.php"; | |
5 | ||
6 | class sql_auth | |
7 | { | |
8 | public $name = "SQLAuth"; | |
9 | public $author = "Valware"; | |
10 | public $version = "1.0"; | |
11 | public $description = "Provides a User Auth and Management Panel with an SQL backend"; | |
12 | public $email = "v.a.pond@outlook.com"; | |
13 | ||
14 | function __construct() | |
15 | { | |
16 | self::create_tables(); | |
17 | Hook::func(HOOKTYPE_PRE_HEADER, 'sql_auth::session_start'); | |
18 | Hook::func(HOOKTYPE_FOOTER, 'sql_auth::add_footer_info'); | |
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'); | |
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'); | |
26 | Hook::func(HOOKTYPE_EDIT_USER, 'sql_auth::edit_core'); | |
27 | Hook::func(HOOKTYPE_PRE_OVERVIEW_CARD, 'sql_auth::add_pre_overview_card'); | |
28 | AuthModLoaded::$status = 1; | |
29 | ||
30 | if (defined('SQL_DEFAULT_USER')) // we've got a default account | |
31 | { | |
32 | $lkup = new PanelUser(SQL_DEFAULT_USER['username']); | |
33 | ||
34 | if (!$lkup->id) // doesn't exist, add it with full privileges | |
35 | { | |
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); | |
41 | } | |
42 | $lkup = new PanelUser(SQL_DEFAULT_USER['username']); | |
43 | if (!user_can($lkup, PERMISSION_MANAGE_USERS)) | |
44 | $lkup->add_permission(PERMISSION_MANAGE_USERS); | |
45 | } | |
46 | } | |
47 | ||
48 | ||
49 | public static function add_footer_info($empty) | |
50 | { | |
51 | if (!($user = unreal_get_current_user())) | |
52 | return; | |
53 | ||
54 | else { | |
55 | echo "<code>Admin Panel v" . WEBPANEL_VERSION . "</code>"; | |
56 | } | |
57 | } | |
58 | ||
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 | ||
65 | /* pre-Header hook */ | |
66 | public static function session_start($n) | |
67 | { | |
68 | if (!isset($_SESSION)) | |
69 | { | |
70 | session_set_cookie_params(3600); | |
71 | session_start(); | |
72 | } | |
73 | if (!isset($_SESSION['id']) || empty($_SESSION)) | |
74 | { | |
75 | $current_page = $_SERVER['REQUEST_URI']; | |
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 | } | |
81 | header("Location: ".BASE_URL."login/?redirect=".urlencode($current_page)); | |
82 | die(); | |
83 | } | |
84 | else | |
85 | { | |
86 | if (!unreal_get_current_user()) // user no longer exists | |
87 | { | |
88 | session_destroy(); | |
89 | header("Location: ".BASE_URL."login"); | |
90 | die(); | |
91 | } | |
92 | // you'll be automatically logged out after one hour of inactivity | |
93 | } | |
94 | } | |
95 | ||
96 | /** | |
97 | * Create the tables we'll be using in the SQLdb | |
98 | * @return void | |
99 | */ | |
100 | public static function create_tables() | |
101 | { | |
102 | $script = $_SERVER['SCRIPT_FILENAME']; | |
103 | if (str_ends_with($script,"setup.php")) | |
104 | return; | |
105 | $conn = sqlnew(); | |
106 | $stmt = $conn->query("SHOW TABLES LIKE '".SQL_PREFIX."%'"); | |
107 | if ($stmt->rowCount() < 5) | |
108 | header("Location: ".BASE_URL."plugins/sql_auth/setup.php"); | |
109 | } | |
110 | ||
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']; | |
141 | $obj->email = $data['user_email']; | |
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 | { | |
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 | { | |
165 | $meta = $meta['meta']; | |
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 | } | |
201 | public static function user_create(&$u) | |
202 | { | |
203 | $username = $u['user_name']; | |
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; | |
209 | $conn = sqlnew(); | |
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")]); | |
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 | } | |
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 | { | |
260 | $value = NULL; | |
261 | if (!$val || !strlen($val) || BadPtr($val)) | |
262 | continue; | |
263 | if (!strcmp($key,"update_fname") && $val != $user->first_name) | |
264 | { | |
265 | $value = "user_fname"; | |
266 | $valuestr = "first name"; | |
267 | } | |
268 | elseif (!strcmp($key,"update_lname") && $val != $user->last_name) | |
269 | { | |
270 | $value = "user_lname"; | |
271 | $valuestr = "last name"; | |
272 | } | |
273 | elseif (!strcmp($key,"update_bio") && $val != $user->bio) | |
274 | { | |
275 | $value = "user_bio"; | |
276 | $valuestr = "bio"; | |
277 | } | |
278 | elseif (!strcmp($key,"update_pass") || !strcmp($key,"update_pass_conf")) | |
279 | { | |
280 | $value = "user_pass"; | |
281 | $valuestr = "password"; | |
282 | } | |
283 | elseif(!strcmp($key,"update_email") && $val != $user->email) | |
284 | { | |
285 | $value = "user_email"; | |
286 | $valuestr = "email address"; | |
287 | } | |
288 | ||
289 | if (!$value) | |
290 | continue; | |
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]); | |
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"); | |
300 | } | |
301 | } | |
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 | { | |
319 | ||
320 | if (!defined('DNSBL')) | |
321 | return; | |
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) | |
365 | { | |
366 | ||
367 | } |