]>
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'); | |
aec8a198 | 18 | Hook::func(HOOKTYPE_OVERVIEW_CARD, 'sql_auth::add_overview_card'); |
4d634d0a VP |
19 | |
20 | if (defined('SQL_DEFAULT_USER')) // we've got a default account | |
21 | { | |
22 | $lkup = new SQLA_User(SQL_DEFAULT_USER['username']); | |
23 | ||
24 | if (!$lkup->id) // doesn't exist, add it with full privileges | |
25 | { | |
26 | create_new_user(["user_name" => SQL_DEFAULT_USER['username'], "user_pass" => SQL_DEFAULT_USER['password']]); | |
27 | } | |
28 | } | |
ea27475b VP |
29 | } |
30 | ||
31 | public static function add_navbar(&$pages) | |
32 | { | |
b44a2e97 | 33 | session_start(); |
4225314c VP |
34 | |
35 | $pages["Panel Access"] = "plugins/sql_auth/"; | |
36 | if (isset($_SESSION['id'])) | |
b44a2e97 VP |
37 | { |
38 | $pages["Logout"] = "plugins/sql_auth/login.php?logout=true"; | |
39 | } | |
ea27475b VP |
40 | } |
41 | ||
b44a2e97 VP |
42 | public static function session_start($n) |
43 | { | |
b44a2e97 VP |
44 | if (!isset($_SESSION['id'])) |
45 | { | |
46 | header("Location: ".BASE_URL."plugins/sql_auth/login.php"); | |
47 | } | |
08ce3aa7 VP |
48 | else |
49 | { | |
f5e3ecee | 50 | if (!unreal_get_current_user()->id) // user no longer exists |
08ce3aa7 VP |
51 | { |
52 | session_destroy(); | |
53 | header("Location: ".BASE_URL."plugins/sql_auth/login.php"); | |
f5e3ecee | 54 | die(); |
08ce3aa7 VP |
55 | } |
56 | } | |
b44a2e97 | 57 | } |
ea27475b | 58 | |
5015c85c VP |
59 | public static function create_tables() |
60 | { | |
61 | $conn = sqlnew(); | |
62 | $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX . "users ( | |
63 | user_id int AUTO_INCREMENT NOT NULL, | |
64 | user_name VARCHAR(255) NOT NULL, | |
65 | user_pass VARCHAR(255) NOT NULL, | |
66 | ||
67 | user_fname VARCHAR(255), | |
68 | user_lname VARCHAR(255), | |
69 | user_bio VARCHAR(255), | |
70 | created VARCHAR(255), | |
71 | PRIMARY KEY (user_id) | |
72 | )"); | |
73 | $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX . "user_meta ( | |
74 | meta_id int AUTO_INCREMENT NOT NULL, | |
75 | user_id int NOT NULL, | |
76 | meta_key VARCHAR(255) NOT NULL, | |
77 | meta_value VARCHAR(255), | |
78 | PRIMARY KEY (meta_id) | |
79 | )"); | |
80 | } | |
81 | ||
aec8a198 VP |
82 | public static function add_overview_card(&$stats) |
83 | { | |
84 | $num_of_panel_admins = sqlnew()->query("SELECT COUNT(*) FROM " . SQL_PREFIX . "users")->fetchColumn(); | |
85 | ?> | |
86 | ||
87 | <div class="container mt-5"> | |
88 | ||
89 | <div class="row"> | |
90 | <div class="col-sm-3"> | |
91 | <div class="card text-center"> | |
92 | <div class="card-header bg-success text-white"> | |
93 | <div class="row"> | |
94 | <div class="col"> | |
95 | <i class="fa fa-lock-open fa-3x"></i> | |
96 | </div> | |
97 | <div class="col"> | |
98 | <h3 class="display-4"><?php echo $num_of_panel_admins; ?></h3> | |
99 | </div> | |
100 | </div> | |
101 | </div> | |
102 | <div class="card-body"> | |
103 | <div class="row"> | |
104 | <div class="col"> | |
105 | <h6>Panel Users</h6> | |
106 | </div> | |
107 | <div class="col"> <a class="btn btn-primary" href="<?php echo BASE_URL; ?>plugins/sql_auth/">View</a></div> | |
108 | </div> | |
109 | </div> | |
110 | </div> | |
111 | </div> | |
112 | </div> | |
113 | </div> | |
114 | <?php | |
115 | } | |
116 | ||
ea27475b | 117 | } |