]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/commitdiff
Add the start of the SQL_Auth plugin
authorValerie Pond <redacted>
Tue, 17 Jan 2023 17:46:46 +0000 (17:46 +0000)
committerValerie Pond <redacted>
Tue, 17 Jan 2023 17:46:46 +0000 (17:46 +0000)
plugins/sql_auth/SQL/sql.php [new file with mode: 0644]
plugins/sql_auth/error.php [new file with mode: 0644]
plugins/sql_auth/index.php [new file with mode: 0644]
plugins/sql_auth/sql_auth.php [new file with mode: 0644]

diff --git a/plugins/sql_auth/SQL/sql.php b/plugins/sql_auth/SQL/sql.php
new file mode 100644 (file)
index 0000000..6de0d6c
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+
+function sqlnew()
+{
+       $host = SQL_IP;
+       $user = SQL_USERNAME;
+       $pass = SQL_PASSWORD;
+       $db = SQL_DATABASE;
+       $charset = 'utf8mb4';
+
+       $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
+       $options = [
+               PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
+               PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
+               PDO::ATTR_EMULATE_PREPARES => false,
+       ];
+       try {
+                $pdo = new PDO($dsn, $user, $pass, $options);
+       } catch (\PDOException $e) {
+                throw new \PDOException($e->getMessage(), (int)$e->getCode());
+       }
+       return $pdo;
+}
\ No newline at end of file
diff --git a/plugins/sql_auth/error.php b/plugins/sql_auth/error.php
new file mode 100644 (file)
index 0000000..f006048
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+require_once "../../common.php";
+require_once "../../header.php";
+
+if (!isset($_GET) || !isset($_GET['errno']))
+{
+    Message::Fail("Uh oh! Something went wrong. We don't know anything else.");
+}
+elseif ($_GET['errno'] == 1)
+{
+    Message::Fail("Looks like your SQL tables haven't been set up yet", 
+                    "SQL_Auth needs to create tables for users and permissions. Is that okay?<br>",
+                    "<form method=\"post\" action=\"index.php\">
+                    <button type=\"submit\" id=\"sql_setup\" name=\"sql_setup\" class=\"text-right btn btn-primary\" value=\"add_tables\">Yes, set up tables</button>
+                    </form>");
+    ?>
+    
+    <?php
+}
+?>
\ No newline at end of file
diff --git a/plugins/sql_auth/index.php b/plugins/sql_auth/index.php
new file mode 100644 (file)
index 0000000..bcd6d5d
--- /dev/null
@@ -0,0 +1,66 @@
+<?php
+$conn = NULL;
+
+require_once "../../common.php";
+require_once "../../header.php";
+do_log($_POST);
+
+if (isset($_POST))
+{
+    $p = $_POST;
+    if (isset($p['sql_setup']))
+    {
+        if ($p['sql_setup'] == "add_tables")
+        {
+            $conn = sqlnew();
+            $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX . "users (
+                user_id int AUTO_INCREMENT NOT NULL,
+                user_name VARCHAR(255) NOT NULL,
+                user_pass VARCHAR(255) NOT NULL,
+                
+                user_fname VARCHAR(255),
+                user_lname VARCHAR(255),
+                user_bio VARCHAR(255),
+                created VARCHAR(255),
+                PRIMARY KEY (user_id)
+            )");
+            $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX . "user_meta (
+                meta_id int AUTO_INCREMENT NOT NULL,
+                user_id int NOT NULL,
+                meta_key VARCHAR(255) NOT NULL,
+                meta_value VARCHAR(255),
+                PRIMARY KEY (meta_id)
+            )");
+            
+        }
+    }
+}
+
+
+
+$conn = sqlnew();
+$count = $conn->query("SELECT count(*) FROM ".SQL_PREFIX."users")->fetchColumn();
+?>
+<div class="mt-5">
+    <div class="card text-center" style="width: 18rem;">
+            <div class="card-header bg-warning">
+                <div class="row">
+                    <div class="col">
+                        <i class="fa fa-screwdriver-wrench fa-3x"></i>
+                    </div>
+                    <div class="col">
+                        <h3 class="display-4"><?php echo $count; ?></h3>
+                    </div>
+                </div>
+            </div>
+            <div class="card-body">
+                <div class="row">
+                    <div class="col">
+                        <h6>Panel Admins</h6>
+                    </div>
+                    <div class="col"> <a class="btn btn-primary" href="<?php echo BASE_URL; ?>users">View</a></div>
+                </div>
+            </div>
+        </div>
+    </div>
+</div>
\ No newline at end of file
diff --git a/plugins/sql_auth/sql_auth.php b/plugins/sql_auth/sql_auth.php
new file mode 100644 (file)
index 0000000..c3bef72
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+
+require_once "SQL/sql.php";
+class sql_auth
+{
+       public $name = "SQL_Auth";
+       public $author = "Valware";
+       public $version = "1.0";
+       public $description = "Provides a User Auth and Management Panel with an SQL backend";
+
+       function __construct()
+       {
+               Hook::func(HOOKTYPE_NAVBAR, 'sql_auth::add_navbar'); 
+       }
+
+       public static function add_navbar(&$pages)
+       {
+               $query = "SELECT * FROM INFORMATION_SCHEMA.TABLES
+               WHERE TABLE_TYPE = 'BASE TABLE'
+               AND TABLE_NAME = '".SQL_PREFIX."users'";
+
+               $conn = sqlnew();
+               $result = $conn->query($query);
+               $notifs = 0;
+               if (!$result || !$result->fetchColumn())
+               {
+                       ++$notifs;
+                       $link = "error.php?errno=1";
+               }
+               $label = ($notifs) ? "<span class=\"position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger\">$notifs</span>" : "";
+               $pages["SQL Auth$label"] = "plugins/sql_auth/$link";
+       }
+
+
+}
\ No newline at end of file