]>
jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - plugins/sql_auth/sql_auth.php
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');
27 if (defined('SQL_DEFAULT_USER')) // we've got a default account
29 $lkup = new PanelUser(SQL_DEFAULT_USER
['username']);
31 if (!$lkup->id
) // doesn't exist, add it with full privileges
34 $user['user_name'] = SQL_DEFAULT_USER
['username'];
35 $user['user_pass'] = SQL_DEFAULT_USER
['password'];
37 create_new_user($user);
39 $lkup = new PanelUser(SQL_DEFAULT_USER
['username']);
40 if (!user_can($lkup, PERMISSION_MANAGE_USERS
))
41 $lkup->add_permission(PERMISSION_MANAGE_USERS
);
46 public static function add_footer_info($empty)
48 if (!($user = unreal_get_current_user())->id
)
52 echo "<code>Admin Panel v" . WEBPANEL_VERSION
. "</code>";
57 public static function session_start($n)
59 if (!isset($_SESSION))
61 session_set_cookie_params(3600);
64 if (!isset($_SESSION['id']) || empty($_SESSION))
66 $secure = ($_SERVER['HTTPS'] == 'on') ? "https://" : "http://";
67 $current_url = "$secure$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
68 $tok = split($_SERVER['SCRIPT_FILENAME'], "/");
69 if ($check = security_check() && $tok[count($tok) - 1] !== "error.php") {
70 header("Location: " . BASE_URL
. "plugins/sql_auth/error.php");
73 header("Location: ".BASE_URL
."login/?redirect=".urlencode($current_url));
78 if (!unreal_get_current_user()->id
) // user no longer exists
81 header("Location: ".BASE_URL
."login");
84 // you'll be automatically logged out after one hour of inactivity
89 * Create the tables we'll be using in the SQLdb
92 public static function create_tables()
95 $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX
. "users (
96 user_id int AUTO_INCREMENT NOT NULL,
97 user_name VARCHAR(255) NOT NULL,
98 user_pass VARCHAR(255) NOT NULL,
99 user_email VARCHAR(255),
100 user_fname VARCHAR(255),
101 user_lname VARCHAR(255),
102 user_bio VARCHAR(255),
103 created VARCHAR(255),
104 PRIMARY KEY (user_id)
108 * Patch for beta users
109 * This adds the email column to existing tables without it
111 $columns = $conn->query("SHOW COLUMNS FROM " . SQL_PREFIX
. "users");
112 $column_names = array();
113 $c = $columns->fetchAll();
115 foreach($c as $column) {
116 $column_names[] = $column['Field'];
118 $column_exists = in_array("user_email", $column_names);
119 if (!$column_exists) {
120 $conn->query("ALTER TABLE " . SQL_PREFIX
. "users ADD COLUMN user_email varchar(255)");
124 * Another patch for beta users
125 * This changes the size of the meta_value so we can store more
127 $columns = $conn->query("SHOW COLUMNS FROM ".SQL_PREFIX
."user_meta");
128 $c = $columns->fetchAll();
130 $conn->query("ALTER TABLE `".SQL_PREFIX
."user_meta` CHANGE `meta_value` `meta_value` VARCHAR(5000) CHARACTER SET utf8mb3 COLLATE utf8mb3_bin NULL DEFAULT NULL");
133 $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX
. "user_meta (
134 meta_id int AUTO_INCREMENT NOT NULL,
135 user_id int NOT NULL,
136 meta_key VARCHAR(255) NOT NULL,
137 meta_value VARCHAR(255),
138 PRIMARY KEY (meta_id)
140 $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX
. "auth_settings (
141 id int AUTO_INCREMENT NOT NULL,
142 setting_key VARCHAR(255) NOT NULL,
143 setting_value VARCHAR(255),
146 $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX
. "fail2ban (
147 id int AUTO_INCREMENT NOT NULL,
148 ip VARCHAR(255) NOT NULL,
155 /* We convert $u with a full user as an object ;D*/
156 public static function get_user(&$u)
164 $prep = $conn->prepare("SELECT * FROM " . SQL_PREFIX
. "users WHERE user_id = :id LIMIT 1");
165 $prep->execute(["id" => strtolower($id)]);
169 $prep = $conn->prepare("SELECT * FROM " . SQL_PREFIX
. "users WHERE LOWER(user_name) = :name LIMIT 1");
170 $prep->execute(["name" => strtolower($name)]);
175 $data = $prep->fetchAll();
176 if (isset($data[0]) && $data = $data[0])
178 $obj->id
= $data['user_id'];
179 $obj->username
= $data['user_name'];
180 $obj->passhash
= $data['user_pass'];
181 $obj->first_name
= $data['user_fname'] ?? NULL;
182 $obj->last_name
= $data['user_lname'] ?? NULL;
183 $obj->created
= $data['created'];
184 $obj->bio
= $data['user_bio'];
185 $obj->email
= $data['user_email'];
186 $obj->user_meta
= (new PanelUser_Meta($obj->id
))->list;
191 public static function get_usermeta(&$u)
198 $prep = $conn->prepare("SELECT * FROM " . SQL_PREFIX
. "user_meta WHERE user_id = :id");
199 $prep->execute(["id" => $id]);
201 foreach ($prep->fetchAll() as $row)
203 $list[$row['meta_key']] = $row['meta_value'];
207 public static function add_usermeta(&$meta)
209 $meta = $meta['meta'];
211 /* check if it exists first, update it if it does */
212 $query = "SELECT * FROM " . SQL_PREFIX
. "user_meta WHERE user_id = :id AND meta_key = :key";
213 $stmt = $conn->prepare($query);
214 $stmt->execute(["id" => $meta['id'], "key" => $meta['key']]);
215 if ($stmt->rowCount()) // it exists, update instead of insert
217 $query = "UPDATE " . SQL_PREFIX
. "user_meta SET meta_value = :value WHERE user_id = :id AND meta_key = :key";
218 $stmt = $conn->prepare($query);
219 $stmt->execute($meta);
220 if ($stmt->rowCount())
227 $query = "INSERT INTO " . SQL_PREFIX
. "user_meta (user_id, meta_key, meta_value) VALUES (:id, :key, :value)";
228 $stmt = $conn->prepare($query);
229 $stmt->execute($meta);
230 if ($stmt->rowCount())
235 public static function del_usermeta(&$u)
238 $query = "DELETE FROM " . SQL_PREFIX
. "user_meta WHERE user_id = :id AND meta_key = :key";
239 $stmt = $conn->prepare($query);
240 $stmt->execute($u['meta']);
241 if ($stmt->rowCount())
245 public static function user_create(&$u)
247 $username = $u['user_name'];
248 $first_name = $u['fname'];
249 $last_name = $u['lname'];
250 $password = $u['user_pass'];
251 $user_bio = $u['user_bio'];
252 $user_email = $u['user_email'];
254 $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)");
255 $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")]);
256 if ($prep->rowCount())
257 $u['success'] = true;
259 $u['errmsg'][] = "Could not add user";
262 public static function get_user_list(&$list)
265 $result = $conn->query("SELECT user_id FROM " . SQL_PREFIX
. "users");
266 if (!$result) // impossible
268 die("Something went wrong.");
271 while($row = $result->fetch())
273 $userlist[] = new PanelUser(NULL, $row['user_id']);
275 if (!empty($userlist))
279 public static function user_delete(&$u)
282 $query = "DELETE FROM " . SQL_PREFIX
. "users WHERE user_id = :id";
284 $stmt = $conn->prepare($query);
285 $stmt->execute(["id" => $user->id
]);
286 $deleted = $stmt->rowCount();
289 $u['info'][] = "Successfully deleted user \"$user->username\"";
292 $u['info'][] = "Unknown error";
299 function security_check()
301 $ip = $_SERVER['REMOTE_ADDR'];
302 if (dnsbl_check($ip))
305 else if (fail2ban_check($ip))
311 function dnsbl_check($ip)
313 $dnsbl_lookup = DNSBL
;
315 // clear variable just in case
318 // if the IP was not given because you're an idiot, stop processing
319 if (!$ip) { return; }
321 // get the first two segments of the IPv4
322 $because = split($ip, "."); // why you
323 $you = $because[1]; // gotta play
324 $want = $because[2]; // that song
325 $to = $you.".".$want."."; // so loud?
327 // exempt local connections because sometimes they get a false positive
328 if ($to == "192.168." || $to == "127.0.") { return NULL; }
330 // you spin my IP right round, right round, to check the records baby, right round-round-round
331 $reverse_ip = glue(array_reverse(split($ip, ".")), ".");
334 foreach ($dnsbl_lookup as $host) {
337 if (checkdnsrr($reverse_ip . "." . $host . ".", "A")) {
344 // if it was safe, return NOTHING
349 // else, you guessed it, return where it was listed
355 function fail2ban_check($ip)