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 if (!isset($_SESSION))
70 session_set_cookie_params(3600);
73 if (!isset($_SESSION['id']) || empty($_SESSION))
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");
81 header("Location: ".BASE_URL
."login/?redirect=".urlencode($current_page));
86 if (!unreal_get_current_user()) // user no longer exists
89 header("Location: ".BASE_URL
."login");
92 // you'll be automatically logged out after one hour of inactivity
97 * Create the tables we'll be using in the SQLdb
100 public static function create_tables()
102 $script = $_SERVER['SCRIPT_FILENAME'];
103 if (str_ends_with($script,"setup.php"))
106 $stmt = $conn->query("SHOW TABLES LIKE '".SQL_PREFIX
."%'");
107 if ($stmt->rowCount() < 5)
108 header("Location: ".BASE_URL
."plugins/sql_auth/setup.php");
111 /* We convert $u with a full user as an object ;D*/
112 public static function get_user(&$u)
120 $prep = $conn->prepare("SELECT * FROM " . SQL_PREFIX
. "users WHERE user_id = :id LIMIT 1");
121 $prep->execute(["id" => strtolower($id)]);
125 $prep = $conn->prepare("SELECT * FROM " . SQL_PREFIX
. "users WHERE LOWER(user_name) = :name LIMIT 1");
126 $prep->execute(["name" => strtolower($name)]);
131 $data = $prep->fetchAll();
132 if (isset($data[0]) && $data = $data[0])
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;
147 public static function get_usermeta(&$u)
154 $prep = $conn->prepare("SELECT * FROM " . SQL_PREFIX
. "user_meta WHERE user_id = :id");
155 $prep->execute(["id" => $id]);
157 foreach ($prep->fetchAll() as $row)
159 $list[$row['meta_key']] = $row['meta_value'];
163 public static function add_usermeta(&$meta)
165 $meta = $meta['meta'];
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
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())
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())
191 public static function del_usermeta(&$u)
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())
201 public static function user_create(&$u)
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;
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;
215 $u['errmsg'][] = "Could not add user";
218 public static function get_user_list(&$list)
221 $result = $conn->query("SELECT user_id FROM " . SQL_PREFIX
. "users");
222 if (!$result) // impossible
224 die("Something went wrong.");
227 while($row = $result->fetch())
229 $userlist[] = new PanelUser(NULL, $row['user_id']);
231 if (!empty($userlist))
235 public static function user_delete(&$u)
238 $query = "DELETE FROM " . SQL_PREFIX
. "users WHERE user_id = :id";
240 $stmt = $conn->prepare($query);
241 $stmt->execute(["id" => $user->id
]);
242 $deleted = $stmt->rowCount();
245 $u['info'][] = "Successfully deleted user \"$user->username\"";
248 $u['info'][] = "Unknown error";
253 public static function edit_core($arr)
256 $user = $arr['user'];
257 $info = $arr['info'];
258 foreach($info as $key => $val)
261 if (!$val || !strlen($val) || BadPtr($val))
263 if (!strcmp($key,"update_fname") && $val != $user->first_name
)
265 $value = "user_fname";
266 $valuestr = "first name";
268 elseif (!strcmp($key,"update_lname") && $val != $user->last_name
)
270 $value = "user_lname";
271 $valuestr = "last name";
273 elseif (!strcmp($key,"update_bio") && $val != $user->bio
)
278 elseif (!strcmp($key,"update_pass") || !strcmp($key,"update_pass_conf"))
280 $value = "user_pass";
281 $valuestr = "password";
283 elseif(!strcmp($key,"update_email") && $val != $user->email
)
285 $value = "user_email";
286 $valuestr = "email address";
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
]);
295 if (!$stmt->rowCount() && $stmt->errorInfo()[0] != "00000")
296 Message
::Fail("Could not update $valuestr for $user->username: ".$stmt->errorInfo()[0]." (CODE: ".$stmt->errorCode().")");
299 Message
::Success("Successfully updated the $valuestr for $user->username");
305 function security_check()
307 $ip = $_SERVER['REMOTE_ADDR'];
308 if (dnsbl_check($ip))
311 else if (fail2ban_check($ip))
317 function dnsbl_check($ip)
320 if (!defined('DNSBL'))
322 $dnsbl_lookup = DNSBL
;
324 // clear variable just in case
327 // if the IP was not given because you're an idiot, stop processing
328 if (!$ip) { return; }
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?
336 // exempt local connections because sometimes they get a false positive
337 if ($to == "192.168." || $to == "127.0.") { return NULL; }
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, ".")), ".");
343 foreach ($dnsbl_lookup as $host) {
346 if (checkdnsrr($reverse_ip . "." . $host . ".", "A")) {
353 // if it was safe, return NOTHING
358 // else, you guessed it, return where it was listed
364 function fail2ban_check($ip)