]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blame - plugins/sql_auth/sql_auth.php
Forgot to push `SQL/user.php` =]
[irc/unrealircd/unrealircd-webpanel.git] / plugins / sql_auth / sql_auth.php
CommitLineData
ea27475b
VP
1<?php
2
3require_once "SQL/sql.php";
4d634d0a 4require_once "SQL/user.php";
ce9cf366
VP
5require_once "SQL/settings.php";
6new AuthSettings();
4d634d0a 7
ea27475b
VP
8class sql_auth
9{
b44a2e97 10 public $name = "SQLAuth";
ea27475b
VP
11 public $author = "Valware";
12 public $version = "1.0";
13 public $description = "Provides a User Auth and Management Panel with an SQL backend";
14
15 function __construct()
16 {
5015c85c 17 self::create_tables();
b44a2e97
VP
18 Hook::func(HOOKTYPE_NAVBAR, 'sql_auth::add_navbar');
19 Hook::func(HOOKTYPE_PRE_HEADER, 'sql_auth::session_start');
aec8a198 20 Hook::func(HOOKTYPE_OVERVIEW_CARD, 'sql_auth::add_overview_card');
4d634d0a
VP
21
22 if (defined('SQL_DEFAULT_USER')) // we've got a default account
23 {
24 $lkup = new SQLA_User(SQL_DEFAULT_USER['username']);
25
26 if (!$lkup->id) // doesn't exist, add it with full privileges
27 {
28 create_new_user(["user_name" => SQL_DEFAULT_USER['username'], "user_pass" => SQL_DEFAULT_USER['password']]);
29 }
30 }
ea27475b
VP
31 }
32
33 public static function add_navbar(&$pages)
34 {
b44a2e97 35 session_start();
ce9cf366
VP
36 if (!unreal_get_current_user()->id)
37 {
38 $pages = NULL;
39 return;
40 }
4225314c
VP
41 $pages["Panel Access"] = "plugins/sql_auth/";
42 if (isset($_SESSION['id']))
b44a2e97
VP
43 {
44 $pages["Logout"] = "plugins/sql_auth/login.php?logout=true";
45 }
ea27475b
VP
46 }
47
b44a2e97
VP
48 public static function session_start($n)
49 {
ce9cf366 50
b44a2e97
VP
51 if (!isset($_SESSION['id']))
52 {
ce9cf366
VP
53 $tok = split($_SERVER['SCRIPT_FILENAME'], "/");
54 if ($check = security_check() && $tok[count($tok) - 1] !== "error.php") {
55 header("Location: " . BASE_URL . "plugins/sql_auth/error.php");
56 die();
57 }
b44a2e97 58 }
08ce3aa7
VP
59 else
60 {
f5e3ecee 61 if (!unreal_get_current_user()->id) // user no longer exists
08ce3aa7
VP
62 {
63 session_destroy();
64 header("Location: ".BASE_URL."plugins/sql_auth/login.php");
f5e3ecee 65 die();
08ce3aa7
VP
66 }
67 }
b44a2e97 68 }
ea27475b 69
ce9cf366
VP
70 /**
71 * Create the tables we'll be using in the SQLdb
72 * @return void
73 */
5015c85c
VP
74 public static function create_tables()
75 {
76 $conn = sqlnew();
77 $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX . "users (
78 user_id int AUTO_INCREMENT NOT NULL,
79 user_name VARCHAR(255) NOT NULL,
80 user_pass VARCHAR(255) NOT NULL,
81
82 user_fname VARCHAR(255),
83 user_lname VARCHAR(255),
84 user_bio VARCHAR(255),
85 created VARCHAR(255),
86 PRIMARY KEY (user_id)
87 )");
88 $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX . "user_meta (
89 meta_id int AUTO_INCREMENT NOT NULL,
90 user_id int NOT NULL,
91 meta_key VARCHAR(255) NOT NULL,
92 meta_value VARCHAR(255),
93 PRIMARY KEY (meta_id)
94 )");
ce9cf366
VP
95 $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX . "auth_settings (
96 id int AUTO_INCREMENT NOT NULL,
97 setting_key VARCHAR(255) NOT NULL,
98 setting_value VARCHAR(255),
99 PRIMARY KEY (id)
100 )");
5015c85c
VP
101 }
102
ce9cf366
VP
103 /**
104 * Summary of add_overview_card
105 * @param mixed $stats
106 * @return void
107 */
108 public static function add_overview_card(object &$stats) : void
aec8a198
VP
109 {
110 $num_of_panel_admins = sqlnew()->query("SELECT COUNT(*) FROM " . SQL_PREFIX . "users")->fetchColumn();
111 ?>
112
113 <div class="container mt-5">
114
115 <div class="row">
116 <div class="col-sm-3">
117 <div class="card text-center">
118 <div class="card-header bg-success text-white">
119 <div class="row">
120 <div class="col">
121 <i class="fa fa-lock-open fa-3x"></i>
122 </div>
123 <div class="col">
124 <h3 class="display-4"><?php echo $num_of_panel_admins; ?></h3>
125 </div>
126 </div>
127 </div>
128 <div class="card-body">
129 <div class="row">
130 <div class="col">
131 <h6>Panel Users</h6>
132 </div>
133 <div class="col"> <a class="btn btn-primary" href="<?php echo BASE_URL; ?>plugins/sql_auth/">View</a></div>
134 </div>
135 </div>
136 </div>
137 </div>
138 </div>
139 </div>
140 <?php
141 }
142
ce9cf366
VP
143}
144
145
146function security_check()
147{
148 $ip = $_SERVER['REMOTE_ADDR'];
149 if (dnsbl_check($ip))
150 return true;
151
152 else if (fail2ban_check($ip))
153 {
154
155 }
156}
157
158function dnsbl_check($ip)
159{
160 $dnsbl_lookup = DNSBL;
161
162 // clear variable just in case
163 $listed = NULL;
164
165 // if the IP was not given because you're an idiot, stop processing
166 if (!$ip) { return; }
167
168 // get the first two segments of the IPv4
169 $because = split($ip, "."); // why you
170 $you = $because[1]; // gotta play
171 $want = $because[2]; // that song
172 $to = $you.".".$want."."; // so loud?
173
174 // exempt local connections because sometimes they get a false positive
175 if ($to == "192.168." || $to == "127.0.") { return NULL; }
176
177 // you spin my IP right round, right round, to check the records baby, right round-round-round
178 $reverse_ip = glue(array_reverse(split($ip, ".")), ".");
179
180 // checkem
181 foreach ($dnsbl_lookup as $host) {
182
183 //if it was listed
184 if (checkdnsrr($reverse_ip . "." . $host . ".", "A")) {
185
186 //take note
187 $listed = $host;
188 }
189 }
190
191 // if it was safe, return NOTHING
192 if (!$listed) {
193 return NULL;
194 }
195
196 // else, you guessed it, return where it was listed
197 else {
198 return $listed;
199 }
200}
201
202function fail2ban_check($ip)
203{}