3 require_once "SQL/sql.php";
4 require_once "SQL/settings.php";
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";
14 function __construct()
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;
30 if (defined('SQL_DEFAULT_USER')) // we've got a default account
32 $lkup = new PanelUser(SQL_DEFAULT_USER
['username']);
34 if (!$lkup->id
) // doesn't exist, add it with full privileges
37 $user['user_name'] = SQL_DEFAULT_USER
['username'];
38 $user['user_pass'] = SQL_DEFAULT_USER
['password'];
40 create_new_user($user);
42 $lkup = new PanelUser(SQL_DEFAULT_USER
['username']);
43 if (!user_can($lkup, PERMISSION_MANAGE_USERS
))
44 $lkup->add_permission(PERMISSION_MANAGE_USERS
);
49 public static function add_footer_info($empty)
51 if (!($user = unreal_get_current_user()))
55 echo "<code>Admin Panel v" . WEBPANEL_VERSION
. "</code>";
59 public static function add_pre_overview_card($empty)
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.");
66 public static function session_start($n)
68 $current_page = $_SERVER['REQUEST_URI'];
69 if (str_ends_with($current_page,"setup.php"))
72 if (!isset($_SESSION))
74 session_set_cookie_params(3600);
77 if (!isset($_SESSION['id']) || empty($_SESSION))
80 $tok = split($_SERVER['SCRIPT_FILENAME'], "/");
81 if ($check = security_check() && $tok[count($tok) - 1] !== "error.php") {
82 header("Location: " . get_config("base_url") . "plugins/sql_auth/error.php");
85 header("Location: ".get_config("base_url")."login/?redirect=".urlencode($current_page));
90 if (!unreal_get_current_user()) // user no longer exists
93 header("Location: ".get_config("base_url")."login");
96 // you'll be automatically logged out after one hour of inactivity
97 $_SESSION['last-activity'] = time();
103 * Create the tables we'll be using in the SQLdb
106 public static function create_tables()
108 $script = $_SERVER['SCRIPT_FILENAME'];
109 if (str_ends_with($script,"setup.php"))
112 $stmt = $conn->query("SHOW TABLES LIKE '".get_config("mysql::table_prefix")."%'");
113 if ($stmt->rowCount() < 4)
115 header("Location: ".get_config("base_url")."plugins/sql_auth/setup.php");
120 /* We convert $u with a full user as an object ;D*/
121 public static function get_user(&$u)
129 $prep = $conn->prepare("SELECT * FROM " . get_config("mysql::table_prefix") . "users WHERE user_id = :id LIMIT 1");
130 $prep->execute(["id" => strtolower($id)]);
134 $prep = $conn->prepare("SELECT * FROM " . get_config("mysql::table_prefix") . "users WHERE LOWER(user_name) = :name LIMIT 1");
135 $prep->execute(["name" => strtolower($name)]);
140 $data = $prep->fetchAll();
141 if (isset($data[0]) && $data = $data[0])
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'];
150 $obj->email
= $data['user_email'];
151 $obj->user_meta
= (new PanelUser_Meta($obj->id
))->list;
156 public static function get_usermeta(&$u)
163 $prep = $conn->prepare("SELECT * FROM " . get_config("mysql::table_prefix") . "user_meta WHERE user_id = :id");
164 $prep->execute(["id" => $id]);
166 foreach ($prep->fetchAll() as $row)
168 $list[$row['meta_key']] = $row['meta_value'];
172 public static function add_usermeta(&$meta)
174 $meta = $meta['meta'];
176 /* check if it exists first, update it if it does */
177 $query = "SELECT * FROM " . get_config("mysql::table_prefix") . "user_meta WHERE user_id = :id AND meta_key = :key";
178 $stmt = $conn->prepare($query);
179 $stmt->execute(["id" => $meta['id'], "key" => $meta['key']]);
180 if ($stmt->rowCount()) // it exists, update instead of insert
182 $query = "UPDATE " . get_config("mysql::table_prefix") . "user_meta SET meta_value = :value WHERE user_id = :id AND meta_key = :key";
183 $stmt = $conn->prepare($query);
184 $stmt->execute($meta);
185 if ($stmt->rowCount())
192 $query = "INSERT INTO " . get_config("mysql::table_prefix") . "user_meta (user_id, meta_key, meta_value) VALUES (:id, :key, :value)";
193 $stmt = $conn->prepare($query);
194 $stmt->execute($meta);
195 if ($stmt->rowCount())
200 public static function del_usermeta(&$u)
203 $query = "DELETE FROM " . get_config("mysql::table_prefix") . "user_meta WHERE user_id = :id AND meta_key = :key";
204 $stmt = $conn->prepare($query);
205 $stmt->execute($u['meta']);
206 if ($stmt->rowCount())
210 public static function user_create(&$u)
212 $username = $u['user_name'];
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;
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)");
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")]);
221 if ($prep->rowCount())
222 $u['success'] = true;
224 $u['errmsg'][] = "Could not add user";
227 public static function get_user_list(&$list)
230 $result = $conn->query("SELECT user_id FROM " . get_config("mysql::table_prefix") . "users");
231 if (!$result) // impossible
233 die("Something went wrong.");
236 while($row = $result->fetch())
238 $userlist[] = new PanelUser(NULL, $row['user_id']);
240 if (!empty($userlist))
244 public static function user_delete(&$u)
247 $query = "DELETE FROM " . get_config("mysql::table_prefix") . "users WHERE user_id = :id";
249 $stmt = $conn->prepare($query);
250 $stmt->execute(["id" => $user->id
]);
251 $deleted = $stmt->rowCount();
254 $u['info'][] = "Successfully deleted user \"$user->username\"";
257 $u['info'][] = "Unknown error";
262 public static function edit_core($arr)
265 $user = $arr['user'];
266 $info = $arr['info'];
267 foreach($info as $key => $val)
270 if (!$val || !strlen($val) || BadPtr($val))
272 if (!strcmp($key,"update_fname") && $val != $user->first_name
)
274 $value = "user_fname";
275 $valuestr = "first name";
277 elseif (!strcmp($key,"update_lname") && $val != $user->last_name
)
279 $value = "user_lname";
280 $valuestr = "last name";
282 elseif (!strcmp($key,"update_bio") && $val != $user->bio
)
287 elseif (!strcmp($key,"update_pass") || !strcmp($key,"update_pass_conf"))
289 $value = "user_pass";
290 $valuestr = "password";
292 elseif(!strcmp($key,"update_email") && $val != $user->email
)
294 $value = "user_email";
295 $valuestr = "email address";
300 $query = "UPDATE " . get_config("mysql::table_prefix") . "users SET $value=:value WHERE user_id = :id";
301 $stmt = $conn->prepare($query);
302 $stmt->execute(["value" => $val, "id" => $user->id
]);
304 if (!$stmt->rowCount() && $stmt->errorInfo()[0] != "00000")
305 Message
::Fail("Could not update $valuestr for $user->username: ".$stmt->errorInfo()[0]." (CODE: ".$stmt->errorCode().")");
308 Message
::Success("Successfully updated the $valuestr for $user->username");
314 function security_check()
316 $ip = $_SERVER['REMOTE_ADDR'];
317 if (dnsbl_check($ip))
320 else if (fail2ban_check($ip))
326 function dnsbl_check($ip)
329 if (!defined('DNSBL'))
331 $dnsbl_lookup = DNSBL
;
333 // clear variable just in case
336 // if the IP was not given because you're an idiot, stop processing
337 if (!$ip) { return; }
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?
345 // exempt local connections because sometimes they get a false positive
346 if ($to == "192.168." || $to == "127.0.") { return NULL; }
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, ".")), ".");
352 foreach ($dnsbl_lookup as $host) {
355 if (checkdnsrr($reverse_ip . "." . $host . ".", "A")) {
362 // if it was safe, return NOTHING
367 // else, you guessed it, return where it was listed
373 function fail2ban_check($ip)