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