]>
Commit | Line | Data |
---|---|---|
ea27475b VP |
1 | <?php |
2 | ||
3 | require_once "SQL/sql.php"; | |
4d634d0a VP |
4 | require_once "SQL/user.php"; |
5 | ||
ea27475b VP |
6 | class 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 VP |
16 | Hook::func(HOOKTYPE_NAVBAR, 'sql_auth::add_navbar'); |
17 | Hook::func(HOOKTYPE_PRE_HEADER, 'sql_auth::session_start'); | |
4d634d0a VP |
18 | |
19 | if (defined('SQL_DEFAULT_USER')) // we've got a default account | |
20 | { | |
21 | $lkup = new SQLA_User(SQL_DEFAULT_USER['username']); | |
22 | ||
23 | if (!$lkup->id) // doesn't exist, add it with full privileges | |
24 | { | |
25 | create_new_user(["user_name" => SQL_DEFAULT_USER['username'], "user_pass" => SQL_DEFAULT_USER['password']]); | |
26 | } | |
27 | } | |
ea27475b VP |
28 | } |
29 | ||
30 | public static function add_navbar(&$pages) | |
31 | { | |
b44a2e97 | 32 | session_start(); |
ea27475b VP |
33 | $query = "SELECT * FROM INFORMATION_SCHEMA.TABLES |
34 | WHERE TABLE_TYPE = 'BASE TABLE' | |
35 | AND TABLE_NAME = '".SQL_PREFIX."users'"; | |
36 | ||
37 | $conn = sqlnew(); | |
38 | $result = $conn->query($query); | |
39 | $notifs = 0; | |
b44a2e97 | 40 | $link = ""; |
ea27475b VP |
41 | if (!$result || !$result->fetchColumn()) |
42 | { | |
43 | ++$notifs; | |
44 | $link = "error.php?errno=1"; | |
45 | } | |
46 | $label = ($notifs) ? "<span class=\"position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger\">$notifs</span>" : ""; | |
a3151e7c | 47 | $pages["Panel Access$label"] = "plugins/sql_auth/$link"; |
b44a2e97 VP |
48 | if ($_SESSION['id']) |
49 | { | |
50 | $pages["Logout"] = "plugins/sql_auth/login.php?logout=true"; | |
51 | } | |
ea27475b VP |
52 | } |
53 | ||
b44a2e97 VP |
54 | public static function session_start($n) |
55 | { | |
56 | session_start(); | |
57 | if (!isset($_SESSION['id'])) | |
58 | { | |
59 | header("Location: ".BASE_URL."plugins/sql_auth/login.php"); | |
60 | } | |
61 | } | |
ea27475b | 62 | |
5015c85c VP |
63 | public static function create_tables() |
64 | { | |
65 | $conn = sqlnew(); | |
66 | $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX . "users ( | |
67 | user_id int AUTO_INCREMENT NOT NULL, | |
68 | user_name VARCHAR(255) NOT NULL, | |
69 | user_pass VARCHAR(255) NOT NULL, | |
70 | ||
71 | user_fname VARCHAR(255), | |
72 | user_lname VARCHAR(255), | |
73 | user_bio VARCHAR(255), | |
74 | created VARCHAR(255), | |
75 | PRIMARY KEY (user_id) | |
76 | )"); | |
77 | $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX . "user_meta ( | |
78 | meta_id int AUTO_INCREMENT NOT NULL, | |
79 | user_id int NOT NULL, | |
80 | meta_key VARCHAR(255) NOT NULL, | |
81 | meta_value VARCHAR(255), | |
82 | PRIMARY KEY (meta_id) | |
83 | )"); | |
84 | } | |
85 | ||
ea27475b | 86 | } |