]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blame - plugins/sql_auth/sql_auth.php
Update the overview to get panel users
[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";
12
13 function __construct()
14 {
5015c85c 15 self::create_tables();
b44a2e97 16 Hook::func(HOOKTYPE_PRE_HEADER, 'sql_auth::session_start');
33f512fa 17 Hook::func(HOOKTYPE_FOOTER, 'sql_auth::add_footer_info');
6930484c
VP
18 Hook::func(HOOKTYPE_USER_LOOKUP, 'sql_auth::get_user');
19 Hook::func(HOOKTYPE_USERMETA_ADD, 'sql_auth::add_usermeta');
20 Hook::func(HOOKTYPE_USERMETA_DEL, 'sql_auth::del_usermeta');
21 Hook::func(HOOKTYPE_USERMETA_GET, 'sql_auth::get_usermeta');
180b8ec1
VP
22 Hook::func(HOOKTYPE_USER_CREATE, 'sql_auth::user_create');
23 Hook::func(HOOKTYPE_GET_USER_LIST, 'sql_auth::get_user_list');
24 Hook::func(HOOKTYPE_USER_DELETE, 'sql_auth::user_delete');
4d634d0a
VP
25
26 if (defined('SQL_DEFAULT_USER')) // we've got a default account
27 {
6930484c 28 $lkup = new PanelUser(SQL_DEFAULT_USER['username']);
4d634d0a
VP
29
30 if (!$lkup->id) // doesn't exist, add it with full privileges
31 {
180b8ec1
VP
32 $user = [];
33 $user['user_name'] = SQL_DEFAULT_USER['username'];
34 $user['user_pass'] = SQL_DEFAULT_USER['password'];
35 $user['err'] = "";
36 create_new_user($user);
4d634d0a
VP
37 }
38 }
ea27475b
VP
39 }
40
ea27475b 41
33f512fa
VP
42 public static function add_footer_info($empty)
43 {
44 if (!($user = unreal_get_current_user()))
45 return;
46
47 else {
48 echo "<code>Admin Panel v" . WEBPANEL_VERSION . "</code>";
49 }
50 }
51
3a8ffab8 52 /* pre-Header hook */
b44a2e97
VP
53 public static function session_start($n)
54 {
06369f59
VP
55 if (!isset($_SESSION))
56 {
57 session_set_cookie_params(3600);
58 session_start();
59 }
454379e3
VP
60 do_log($_SESSION);
61 if (!isset($_SESSION['id']) || empty($_SESSION))
b44a2e97 62 {
3a8ffab8
VP
63 $secure = ($_SERVER['HTTPS'] == 'on') ? "https://" : "http://";
64 $current_url = "$secure$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
ce9cf366
VP
65 $tok = split($_SERVER['SCRIPT_FILENAME'], "/");
66 if ($check = security_check() && $tok[count($tok) - 1] !== "error.php") {
67 header("Location: " . BASE_URL . "plugins/sql_auth/error.php");
68 die();
69 }
321b7b81 70 header("Location: ".BASE_URL."login/?redirect=".urlencode($current_url));
454379e3 71 die();
b44a2e97 72 }
08ce3aa7
VP
73 else
74 {
f5e3ecee 75 if (!unreal_get_current_user()->id) // user no longer exists
08ce3aa7
VP
76 {
77 session_destroy();
321b7b81 78 header("Location: ".BASE_URL."login");
f5e3ecee 79 die();
08ce3aa7 80 }
e3e93dde 81 // you'll be automatically logged out after one hour of inactivity
08ce3aa7 82 }
b44a2e97 83 }
ea27475b 84
ce9cf366
VP
85 /**
86 * Create the tables we'll be using in the SQLdb
87 * @return void
88 */
5015c85c
VP
89 public static function create_tables()
90 {
91 $conn = sqlnew();
92 $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX . "users (
93 user_id int AUTO_INCREMENT NOT NULL,
94 user_name VARCHAR(255) NOT NULL,
95 user_pass VARCHAR(255) NOT NULL,
96
97 user_fname VARCHAR(255),
98 user_lname VARCHAR(255),
99 user_bio VARCHAR(255),
100 created VARCHAR(255),
101 PRIMARY KEY (user_id)
102 )");
103 $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX . "user_meta (
104 meta_id int AUTO_INCREMENT NOT NULL,
105 user_id int NOT NULL,
106 meta_key VARCHAR(255) NOT NULL,
107 meta_value VARCHAR(255),
108 PRIMARY KEY (meta_id)
109 )");
ce9cf366
VP
110 $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX . "auth_settings (
111 id int AUTO_INCREMENT NOT NULL,
112 setting_key VARCHAR(255) NOT NULL,
113 setting_value VARCHAR(255),
114 PRIMARY KEY (id)
115 )");
33f512fa
VP
116 $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX . "fail2ban (
117 id int AUTO_INCREMENT NOT NULL,
118 ip VARCHAR(255) NOT NULL,
119 count VARCHAR(255),
120 PRIMARY KEY (id)
121 )");
9c643401 122 new AuthSettings();
5015c85c
VP
123 }
124
6930484c
VP
125 /* We convert $u with a full user as an object ;D*/
126 public static function get_user(&$u)
127 {
128 $id = $u['id'];
129 $name = $u['name'];
130 $conn = sqlnew();
131
132 if ($id)
133 {
134 $prep = $conn->prepare("SELECT * FROM " . SQL_PREFIX . "users WHERE user_id = :id LIMIT 1");
135 $prep->execute(["id" => strtolower($id)]);
136 }
137 elseif ($name)
138 {
139 $prep = $conn->prepare("SELECT * FROM " . SQL_PREFIX . "users WHERE LOWER(user_name) = :name LIMIT 1");
140 $prep->execute(["name" => strtolower($name)]);
141 }
142 $data = NULL;
143 $obj = (object) [];
144 if ($prep)
145 $data = $prep->fetchAll();
146 if (isset($data[0]) && $data = $data[0])
147 {
148 $obj->id = $data['user_id'];
149 $obj->username = $data['user_name'];
150 $obj->passhash = $data['user_pass'];
151 $obj->first_name = $data['user_fname'] ?? NULL;
152 $obj->last_name = $data['user_lname'] ?? NULL;
153 $obj->created = $data['created'];
154 $obj->bio = $data['user_bio'];
155 $obj->user_meta = (new PanelUser_Meta($obj->id))->list;
156 }
157 $u['object'] = $obj;
158 }
159
160 public static function get_usermeta(&$u)
161 {
162 //do_log($u);
163 $list = &$u['meta'];
164 $id = $u['id'];
165 $conn = sqlnew();
166 if (isset($id))
167 {
168 $prep = $conn->prepare("SELECT * FROM " . SQL_PREFIX . "user_meta WHERE user_id = :id");
169 $prep->execute(["id" => $id]);
170 }
171 foreach ($prep->fetchAll() as $row)
172 {
173 $list[$row['meta_key']] = $row['meta_value'];
174 }
175 }
176
177 public static function add_usermeta(&$meta)
178 {
da6fa2d1 179 $meta = $meta['meta'];
6930484c
VP
180 $conn = sqlnew();
181 /* check if it exists first, update it if it does */
182 $query = "SELECT * FROM " . SQL_PREFIX . "user_meta WHERE user_id = :id AND meta_key = :key";
183 $stmt = $conn->prepare($query);
184 $stmt->execute(["id" => $meta['id'], "key" => $meta['key']]);
185 if ($stmt->rowCount()) // it exists, update instead of insert
186 {
187 $query = "UPDATE " . SQL_PREFIX . "user_meta SET meta_value = :value WHERE user_id = :id AND meta_key = :key";
188 $stmt = $conn->prepare($query);
189 $stmt->execute($meta);
190 if ($stmt->rowCount())
191 return true;
192 return false;
193 }
194
195 else
196 {
197 $query = "INSERT INTO " . SQL_PREFIX . "user_meta (user_id, meta_key, meta_value) VALUES (:id, :key, :value)";
198 $stmt = $conn->prepare($query);
199 $stmt->execute($meta);
200 if ($stmt->rowCount())
201 return true;
202 return false;
203 }
204 }
205 public static function del_usermeta(&$u)
206 {
207 $conn = sqlnew();
208 $query = "DELETE FROM " . SQL_PREFIX . "user_meta WHERE user_id = :id AND meta_key = :key";
209 $stmt = $conn->prepare($query);
210 $stmt->execute($u['meta']);
211 if ($stmt->rowCount())
212 return true;
213 return false;
214 }
180b8ec1
VP
215 public static function user_create(&$u)
216 {
217 $username = $u['user_name'];
218 $first_name = $u['fname'];
219 $last_name = $u['lname'];
220 $password = $u['user_pass'];
221 $user_bio = $u['user_bio'];
222 $conn = sqlnew();
223 $prep = $conn->prepare("INSERT INTO " . SQL_PREFIX . "users (user_name, user_pass, user_fname, user_lname, user_bio, created) VALUES (:name, :pass, :fname, :lname, :user_bio, :created)");
224 $prep->execute(["name" => $username, "pass" => $password, "fname" => $first_name, "lname" => $last_name, "user_bio" => $user_bio, "created" => date("Y-m-d H:i:s")]);
225 if ($prep->rowCount())
226 $u['success'] = true;
227 else
228 $u['errmsg'][] = "Could not add user";
229 }
230
231 public static function get_user_list(&$list)
232 {
233 $conn = sqlnew();
234 $result = $conn->query("SELECT user_id FROM " . SQL_PREFIX . "users");
235 if (!$result) // impossible
236 {
237 die("Something went wrong.");
238 }
239 $userlist = [];
240 while($row = $result->fetch())
241 {
242 $userlist[] = new PanelUser(NULL, $row['user_id']);
243 }
244 if (!empty($userlist))
245 $list = $userlist;
246
247 }
248 public static function user_delete(&$u)
249 {
250 $user = $u['user'];
251 $query = "DELETE FROM " . SQL_PREFIX . "users WHERE user_id = :id";
252 $conn = sqlnew();
253 $stmt = $conn->prepare($query);
254 $stmt->execute(["id" => $user->id]);
255 $deleted = $stmt->rowCount();
256 if ($deleted)
257 {
258 $u['info'][] = "Successfully deleted user \"$user->username\"";
259 $u['boolint'] = 1;
260 } else {
261 $u['info'][] = "Unknown error";
262 $u['boolint'] = 0;
263 }
264 }
ce9cf366
VP
265}
266
267
268function security_check()
269{
270 $ip = $_SERVER['REMOTE_ADDR'];
271 if (dnsbl_check($ip))
272 return true;
273
274 else if (fail2ban_check($ip))
275 {
276
277 }
278}
279
280function dnsbl_check($ip)
281{
282 $dnsbl_lookup = DNSBL;
283
284 // clear variable just in case
285 $listed = NULL;
286
287 // if the IP was not given because you're an idiot, stop processing
288 if (!$ip) { return; }
289
290 // get the first two segments of the IPv4
291 $because = split($ip, "."); // why you
292 $you = $because[1]; // gotta play
293 $want = $because[2]; // that song
294 $to = $you.".".$want."."; // so loud?
295
296 // exempt local connections because sometimes they get a false positive
297 if ($to == "192.168." || $to == "127.0.") { return NULL; }
298
299 // you spin my IP right round, right round, to check the records baby, right round-round-round
300 $reverse_ip = glue(array_reverse(split($ip, ".")), ".");
301
302 // checkem
303 foreach ($dnsbl_lookup as $host) {
304
305 //if it was listed
306 if (checkdnsrr($reverse_ip . "." . $host . ".", "A")) {
307
308 //take note
309 $listed = $host;
310 }
311 }
312
313 // if it was safe, return NOTHING
314 if (!$listed) {
315 return NULL;
316 }
317
318 // else, you guessed it, return where it was listed
319 else {
320 return $listed;
321 }
322}
323
324function fail2ban_check($ip)
33f512fa
VP
325{
326
327}