]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blobdiff - plugins/sql_auth/SQL/user.php
Rather large update, please see commit notes
[irc/unrealircd/unrealircd-webpanel.git] / plugins / sql_auth / SQL / user.php
index 3ed7c1454778f74569ba683690d20e39a3b0e5fd..a05abb7b0cbf1c0332ed95244387bc423d8f046f 100644 (file)
@@ -20,7 +20,7 @@ class SQLA_User
      * @param string $name
      * @param mixed $id
      */
-    function __construct(string $name = NULL, $id = NULL)
+    function __construct(string $name = NULL, int $id = NULL)
     {
         $conn = sqlnew();
 
@@ -50,12 +50,86 @@ class SQLA_User
         }
     }
 
-    function password_verify(string $input)
+    /**
+     * Verify a user's password
+     * @param string $input
+     * @return bool
+     */
+    function password_verify(string $input) : bool
     {
         if (password_verify($input, $this->passhash))
             return true;
         return false;
     }
+
+    /**
+     * Add user meta data
+     * @param string $key
+     * @param string $value
+     * @return bool
+     */
+    function add_meta(string $key, string $value)
+    {
+        if (!$key || !$value)
+            return false;
+
+        $meta = [
+            "id" => $this->id,
+            "key" => $key,
+            "value" => $value
+        ];
+        
+        $conn = sqlnew();
+
+        /* check if it exists first, update it if it does */
+        $query = "SELECT * FROM " . SQL_PREFIX . "user_meta WHERE user_id = :id AND meta_key = :key";
+        $stmt = $conn->prepare($query);
+        $stmt->execute(["id" => $this->id, "key" => $key]);
+        if ($stmt->rowCount()) // it exists, update instead of insert
+        {
+            $query = "UPDATE " . SQL_PREFIX . "user_meta SET meta_value = :value WHERE user_id = :id AND meta_key = :key";
+            $stmt = $conn->prepare($query);
+            $stmt->execute($meta);
+            if ($stmt->rowCount())
+                return true;
+            return false;
+        }
+
+        else
+        {
+            $query = "INSERT INTO " . SQL_PREFIX . "user_meta (user_id, meta_key, meta_value) VALUES (:id, :key, :value)";
+            $stmt = $conn->prepare($query);
+            $stmt->execute($meta);
+            if ($stmt->rowCount())
+                return true;
+            return false;
+        }
+    }
+
+    /**
+     * Delete user meta data by key
+     * @param string $key
+     * @return bool
+     */
+    function delete_meta(string $key)
+    {
+        if (!$key )
+            return false;
+
+        $meta = [
+            "id" => $this->id,
+            "key" => $key,
+        ];
+        
+        $conn = sqlnew();
+        $query = "DELETE FROM " . SQL_PREFIX . "user_meta WHERE user_id = :id AND  meta_key = :key";
+        $stmt = $conn->prepare($query);
+        $stmt->execute($meta);
+        if ($stmt->rowCount())
+            return true;
+        return false;
+
+    }
 }
 
 
@@ -174,4 +248,5 @@ function delete_user(int $id, &$info = []) : int
     }
     $info[] = "Unknown error";
     return 0;
-}
\ No newline at end of file
+}
+