]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blame - plugins/sql_auth/sql_auth.php
Move to new style config, with config in config/ directory.
[irc/unrealircd/unrealircd-webpanel.git] / plugins / sql_auth / sql_auth.php
CommitLineData
ea27475b
VP
1<?php
2
3require_once "SQL/sql.php";
ce9cf366 4require_once "SQL/settings.php";
4d634d0a 5
ea27475b
VP
6class 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 {
f2c7ac01
VP
68 $current_page = $_SERVER['REQUEST_URI'];
69 if (str_ends_with($current_page,"setup.php"))
70 return;
71
06369f59
VP
72 if (!isset($_SESSION))
73 {
74 session_set_cookie_params(3600);
75 session_start();
76 }
454379e3 77 if (!isset($_SESSION['id']) || empty($_SESSION))
b44a2e97 78 {
f2c7ac01 79
ce9cf366
VP
80 $tok = split($_SERVER['SCRIPT_FILENAME'], "/");
81 if ($check = security_check() && $tok[count($tok) - 1] !== "error.php") {
ea90b321 82 header("Location: " . get_config("base_url") . "plugins/sql_auth/error.php");
ce9cf366
VP
83 die();
84 }
ea90b321 85 header("Location: ".get_config("base_url")."login/?redirect=".urlencode($current_page));
454379e3 86 die();
b44a2e97 87 }
08ce3aa7
VP
88 else
89 {
94890799 90 if (!unreal_get_current_user()) // user no longer exists
08ce3aa7
VP
91 {
92 session_destroy();
ea90b321 93 header("Location: ".get_config("base_url")."login");
f5e3ecee 94 die();
08ce3aa7 95 }
e3e93dde 96 // you'll be automatically logged out after one hour of inactivity
39206f24
VP
97 $_SESSION['last-activity'] = time();
98
08ce3aa7 99 }
b44a2e97 100 }
ea27475b 101
ce9cf366
VP
102 /**
103 * Create the tables we'll be using in the SQLdb
104 * @return void
105 */
5015c85c
VP
106 public static function create_tables()
107 {
46e8f612
VP
108 $script = $_SERVER['SCRIPT_FILENAME'];
109 if (str_ends_with($script,"setup.php"))
110 return;
5015c85c 111 $conn = sqlnew();
ea90b321 112 $stmt = $conn->query("SHOW TABLES LIKE '".get_config("mysql::table_prefix")."%'");
64a29fa9 113 if ($stmt->rowCount() < 4)
f2c7ac01 114 {
ea90b321 115 header("Location: ".get_config("base_url")."plugins/sql_auth/setup.php");
f2c7ac01
VP
116 die();
117 }
5015c85c
VP
118 }
119
6930484c
VP
120 /* We convert $u with a full user as an object ;D*/
121 public static function get_user(&$u)
122 {
123 $id = $u['id'];
124 $name = $u['name'];
125 $conn = sqlnew();
126
127 if ($id)
128 {
ea90b321 129 $prep = $conn->prepare("SELECT * FROM " . get_config("mysql::table_prefix") . "users WHERE user_id = :id LIMIT 1");
6930484c
VP
130 $prep->execute(["id" => strtolower($id)]);
131 }
132 elseif ($name)
133 {
ea90b321 134 $prep = $conn->prepare("SELECT * FROM " . get_config("mysql::table_prefix") . "users WHERE LOWER(user_name) = :name LIMIT 1");
6930484c
VP
135 $prep->execute(["name" => strtolower($name)]);
136 }
137 $data = NULL;
138 $obj = (object) [];
139 if ($prep)
140 $data = $prep->fetchAll();
141 if (isset($data[0]) && $data = $data[0])
142 {
143 $obj->id = $data['user_id'];
144 $obj->username = $data['user_name'];
145 $obj->passhash = $data['user_pass'];
146 $obj->first_name = $data['user_fname'] ?? NULL;
147 $obj->last_name = $data['user_lname'] ?? NULL;
148 $obj->created = $data['created'];
149 $obj->bio = $data['user_bio'];
9a674833 150 $obj->email = $data['user_email'];
6930484c
VP
151 $obj->user_meta = (new PanelUser_Meta($obj->id))->list;
152 }
153 $u['object'] = $obj;
154 }
155
156 public static function get_usermeta(&$u)
157 {
6930484c
VP
158 $list = &$u['meta'];
159 $id = $u['id'];
160 $conn = sqlnew();
161 if (isset($id))
162 {
ea90b321 163 $prep = $conn->prepare("SELECT * FROM " . get_config("mysql::table_prefix") . "user_meta WHERE user_id = :id");
6930484c
VP
164 $prep->execute(["id" => $id]);
165 }
166 foreach ($prep->fetchAll() as $row)
167 {
168 $list[$row['meta_key']] = $row['meta_value'];
169 }
170 }
171
172 public static function add_usermeta(&$meta)
173 {
da6fa2d1 174 $meta = $meta['meta'];
6930484c
VP
175 $conn = sqlnew();
176 /* check if it exists first, update it if it does */
ea90b321 177 $query = "SELECT * FROM " . get_config("mysql::table_prefix") . "user_meta WHERE user_id = :id AND meta_key = :key";
6930484c
VP
178 $stmt = $conn->prepare($query);
179 $stmt->execute(["id" => $meta['id'], "key" => $meta['key']]);
180 if ($stmt->rowCount()) // it exists, update instead of insert
181 {
ea90b321 182 $query = "UPDATE " . get_config("mysql::table_prefix") . "user_meta SET meta_value = :value WHERE user_id = :id AND meta_key = :key";
6930484c
VP
183 $stmt = $conn->prepare($query);
184 $stmt->execute($meta);
185 if ($stmt->rowCount())
186 return true;
187 return false;
188 }
189
190 else
191 {
ea90b321 192 $query = "INSERT INTO " . get_config("mysql::table_prefix") . "user_meta (user_id, meta_key, meta_value) VALUES (:id, :key, :value)";
6930484c
VP
193 $stmt = $conn->prepare($query);
194 $stmt->execute($meta);
195 if ($stmt->rowCount())
196 return true;
197 return false;
198 }
199 }
200 public static function del_usermeta(&$u)
201 {
202 $conn = sqlnew();
ea90b321 203 $query = "DELETE FROM " . get_config("mysql::table_prefix") . "user_meta WHERE user_id = :id AND meta_key = :key";
6930484c
VP
204 $stmt = $conn->prepare($query);
205 $stmt->execute($u['meta']);
206 if ($stmt->rowCount())
207 return true;
208 return false;
209 }
180b8ec1
VP
210 public static function user_create(&$u)
211 {
212 $username = $u['user_name'];
9f9d16d5
VP
213 $first_name = $u['fname'] ?? NULL;
214 $last_name = $u['lname'] ?? NULL;
215 $password = $u['user_pass'] ?? NULL;
216 $user_bio = $u['user_bio'] ?? NULL;
217 $user_email = $u['user_email'] ?? NULL;
180b8ec1 218 $conn = sqlnew();
ea90b321 219 $prep = $conn->prepare("INSERT INTO " . get_config("mysql::table_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)");
9a674833 220 $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
221 if ($prep->rowCount())
222 $u['success'] = true;
223 else
224 $u['errmsg'][] = "Could not add user";
225 }
226
227 public static function get_user_list(&$list)
228 {
229 $conn = sqlnew();
ea90b321 230 $result = $conn->query("SELECT user_id FROM " . get_config("mysql::table_prefix") . "users");
180b8ec1
VP
231 if (!$result) // impossible
232 {
233 die("Something went wrong.");
234 }
235 $userlist = [];
236 while($row = $result->fetch())
237 {
238 $userlist[] = new PanelUser(NULL, $row['user_id']);
239 }
240 if (!empty($userlist))
241 $list = $userlist;
242
243 }
244 public static function user_delete(&$u)
245 {
246 $user = $u['user'];
ea90b321 247 $query = "DELETE FROM " . get_config("mysql::table_prefix") . "users WHERE user_id = :id";
180b8ec1
VP
248 $conn = sqlnew();
249 $stmt = $conn->prepare($query);
250 $stmt->execute(["id" => $user->id]);
251 $deleted = $stmt->rowCount();
252 if ($deleted)
253 {
254 $u['info'][] = "Successfully deleted user \"$user->username\"";
255 $u['boolint'] = 1;
256 } else {
257 $u['info'][] = "Unknown error";
258 $u['boolint'] = 0;
259 }
260 }
5a7f0cde
VP
261
262 public static function edit_core($arr)
263 {
264 $conn = sqlnew();
265 $user = $arr['user'];
266 $info = $arr['info'];
267 foreach($info as $key => $val)
268 {
e9c858fd 269 $value = NULL;
8a73256b 270 if (!$val || !strlen($val) || BadPtr($val))
5a7f0cde 271 continue;
e9c858fd
VP
272 if (!strcmp($key,"update_fname") && $val != $user->first_name)
273 {
5a7f0cde 274 $value = "user_fname";
e9c858fd
VP
275 $valuestr = "first name";
276 }
277 elseif (!strcmp($key,"update_lname") && $val != $user->last_name)
278 {
5a7f0cde 279 $value = "user_lname";
e9c858fd
VP
280 $valuestr = "last name";
281 }
282 elseif (!strcmp($key,"update_bio") && $val != $user->bio)
283 {
5a7f0cde 284 $value = "user_bio";
e9c858fd
VP
285 $valuestr = "bio";
286 }
5a7f0cde 287 elseif (!strcmp($key,"update_pass") || !strcmp($key,"update_pass_conf"))
e9c858fd 288 {
5a7f0cde 289 $value = "user_pass";
e9c858fd
VP
290 $valuestr = "password";
291 }
292 elseif(!strcmp($key,"update_email") && $val != $user->email)
293 {
5a7f0cde 294 $value = "user_email";
e9c858fd
VP
295 $valuestr = "email address";
296 }
297
298 if (!$value)
299 continue;
ea90b321 300 $query = "UPDATE " . get_config("mysql::table_prefix") . "users SET $value=:value WHERE user_id = :id";
5a7f0cde
VP
301 $stmt = $conn->prepare($query);
302 $stmt->execute(["value" => $val, "id" => $user->id]);
e9c858fd
VP
303
304 if (!$stmt->rowCount() && $stmt->errorInfo()[0] != "00000")
305 Message::Fail("Could not update $valuestr for $user->username: ".$stmt->errorInfo()[0]." (CODE: ".$stmt->errorCode().")");
306
307 else
308 Message::Success("Successfully updated the $valuestr for $user->username");
5a7f0cde
VP
309 }
310 }
ce9cf366
VP
311}
312
313
314function security_check()
315{
316 $ip = $_SERVER['REMOTE_ADDR'];
317 if (dnsbl_check($ip))
318 return true;
319
320 else if (fail2ban_check($ip))
321 {
322
323 }
324}
325
326function dnsbl_check($ip)
327{
5a7f0cde
VP
328
329 if (!defined('DNSBL'))
330 return;
ce9cf366
VP
331 $dnsbl_lookup = DNSBL;
332
333 // clear variable just in case
334 $listed = NULL;
335
336 // if the IP was not given because you're an idiot, stop processing
337 if (!$ip) { return; }
338
339 // get the first two segments of the IPv4
340 $because = split($ip, "."); // why you
341 $you = $because[1]; // gotta play
342 $want = $because[2]; // that song
343 $to = $you.".".$want."."; // so loud?
344
345 // exempt local connections because sometimes they get a false positive
346 if ($to == "192.168." || $to == "127.0.") { return NULL; }
347
348 // you spin my IP right round, right round, to check the records baby, right round-round-round
349 $reverse_ip = glue(array_reverse(split($ip, ".")), ".");
350
351 // checkem
352 foreach ($dnsbl_lookup as $host) {
353
354 //if it was listed
355 if (checkdnsrr($reverse_ip . "." . $host . ".", "A")) {
356
357 //take note
358 $listed = $host;
359 }
360 }
361
362 // if it was safe, return NOTHING
363 if (!$listed) {
364 return NULL;
365 }
366
367 // else, you guessed it, return where it was listed
368 else {
369 return $listed;
370 }
371}
372
373function fail2ban_check($ip)
33f512fa
VP
374{
375
376}