X-Git-Url: https://jfr.im/git/irc/unrealircd/unrealircd-webpanel.git/blobdiff_plain/08ce3aa715e748017fc9b3b48b6f4e416ab1896e..0b82418ba05d25a5f643b09c3e1b3f68d48e4832:/plugins/sql_auth/sql_auth.php?ds=inline
diff --git a/plugins/sql_auth/sql_auth.php b/plugins/sql_auth/sql_auth.php
index 44ab3f7..f3ffcf5 100644
--- a/plugins/sql_auth/sql_auth.php
+++ b/plugins/sql_auth/sql_auth.php
@@ -1,7 +1,7 @@
id) // doesn't exist, add it with full privileges
{
- create_new_user(["user_name" => SQL_DEFAULT_USER['username'], "user_pass" => SQL_DEFAULT_USER['password']]);
+ $user = [];
+ $user['user_name'] = SQL_DEFAULT_USER['username'];
+ $user['user_pass'] = SQL_DEFAULT_USER['password'];
+ $user['err'] = "";
+ create_new_user($user);
}
}
}
- public static function add_navbar(&$pages)
+
+ public static function add_footer_info($empty)
{
- session_start();
-
- $pages["Panel Access"] = "plugins/sql_auth/";
- if (isset($_SESSION['id']))
- {
- $pages["Logout"] = "plugins/sql_auth/login.php?logout=true";
+ if (!($user = unreal_get_current_user()))
+ return;
+
+ else {
+ echo "Admin Panel v" . WEBPANEL_VERSION . "
";
}
}
+ /* pre-Header hook */
public static function session_start($n)
{
- if (!isset($_SESSION['id']))
+ if (!isset($_SESSION))
+ {
+ session_set_cookie_params(3600);
+ session_start();
+ }
+ do_log($_SESSION);
+ if (!isset($_SESSION['id']) || empty($_SESSION))
{
- header("Location: ".BASE_URL."plugins/sql_auth/login.php");
+ $secure = ($_SERVER['HTTPS'] == 'on') ? "https://" : "http://";
+ $current_url = "$secure$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
+ $tok = split($_SERVER['SCRIPT_FILENAME'], "/");
+ if ($check = security_check() && $tok[count($tok) - 1] !== "error.php") {
+ header("Location: " . BASE_URL . "plugins/sql_auth/error.php");
+ die();
+ }
+ header("Location: ".BASE_URL."login/?redirect=".urlencode($current_url));
+ die();
}
else
{
- $user = new SQLA_User(NULL, $_SESSION['id']);
- if (!$user->id)
+ if (!unreal_get_current_user()->id) // user no longer exists
{
session_destroy();
- header("Location: ".BASE_URL."plugins/sql_auth/login.php");
+ header("Location: ".BASE_URL."login");
+ die();
}
+ // you'll be automatically logged out after one hour of inactivity
}
}
+ /**
+ * Create the tables we'll be using in the SQLdb
+ * @return void
+ */
public static function create_tables()
{
$conn = sqlnew();
@@ -77,41 +108,221 @@ class sql_auth
meta_value VARCHAR(255),
PRIMARY KEY (meta_id)
)");
+ $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX . "auth_settings (
+ id int AUTO_INCREMENT NOT NULL,
+ setting_key VARCHAR(255) NOT NULL,
+ setting_value VARCHAR(255),
+ PRIMARY KEY (id)
+ )");
+ $conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX . "fail2ban (
+ id int AUTO_INCREMENT NOT NULL,
+ ip VARCHAR(255) NOT NULL,
+ count VARCHAR(255),
+ PRIMARY KEY (id)
+ )");
+ new AuthSettings();
+ }
+
+ /* We convert $u with a full user as an object ;D*/
+ public static function get_user(&$u)
+ {
+ $id = $u['id'];
+ $name = $u['name'];
+ $conn = sqlnew();
+
+ if ($id)
+ {
+ $prep = $conn->prepare("SELECT * FROM " . SQL_PREFIX . "users WHERE user_id = :id LIMIT 1");
+ $prep->execute(["id" => strtolower($id)]);
+ }
+ elseif ($name)
+ {
+ $prep = $conn->prepare("SELECT * FROM " . SQL_PREFIX . "users WHERE LOWER(user_name) = :name LIMIT 1");
+ $prep->execute(["name" => strtolower($name)]);
+ }
+ $data = NULL;
+ $obj = (object) [];
+ if ($prep)
+ $data = $prep->fetchAll();
+ if (isset($data[0]) && $data = $data[0])
+ {
+ $obj->id = $data['user_id'];
+ $obj->username = $data['user_name'];
+ $obj->passhash = $data['user_pass'];
+ $obj->first_name = $data['user_fname'] ?? NULL;
+ $obj->last_name = $data['user_lname'] ?? NULL;
+ $obj->created = $data['created'];
+ $obj->bio = $data['user_bio'];
+ $obj->user_meta = (new PanelUser_Meta($obj->id))->list;
+ }
+ $u['object'] = $obj;
+ }
+
+ public static function get_usermeta(&$u)
+ {
+ //do_log($u);
+ $list = &$u['meta'];
+ $id = $u['id'];
+ $conn = sqlnew();
+ if (isset($id))
+ {
+ $prep = $conn->prepare("SELECT * FROM " . SQL_PREFIX . "user_meta WHERE user_id = :id");
+ $prep->execute(["id" => $id]);
+ }
+ foreach ($prep->fetchAll() as $row)
+ {
+ $list[$row['meta_key']] = $row['meta_value'];
+ }
+ }
+
+ public static function add_usermeta(&$meta)
+ {
+ $meta = $meta['meta'];
+ $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" => $meta['id'], "key" => $meta['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;
+ }
}
+ public static function del_usermeta(&$u)
+ {
+ $conn = sqlnew();
+ $query = "DELETE FROM " . SQL_PREFIX . "user_meta WHERE user_id = :id AND meta_key = :key";
+ $stmt = $conn->prepare($query);
+ $stmt->execute($u['meta']);
+ if ($stmt->rowCount())
+ return true;
+ return false;
+ }
+ public static function user_create(&$u)
+ {
+ $username = $u['user_name'];
+ $first_name = $u['fname'];
+ $last_name = $u['lname'];
+ $password = $u['user_pass'];
+ $user_bio = $u['user_bio'];
+ $conn = sqlnew();
+ $prep = $conn->prepare("INSERT INTO " . SQL_PREFIX . "users (user_name, user_pass, user_fname, user_lname, user_bio, created) VALUES (:name, :pass, :fname, :lname, :user_bio, :created)");
+ $prep->execute(["name" => $username, "pass" => $password, "fname" => $first_name, "lname" => $last_name, "user_bio" => $user_bio, "created" => date("Y-m-d H:i:s")]);
+ if ($prep->rowCount())
+ $u['success'] = true;
+ else
+ $u['errmsg'][] = "Could not add user";
+ }
+
+ public static function get_user_list(&$list)
+ {
+ $conn = sqlnew();
+ $result = $conn->query("SELECT user_id FROM " . SQL_PREFIX . "users");
+ if (!$result) // impossible
+ {
+ die("Something went wrong.");
+ }
+ $userlist = [];
+ while($row = $result->fetch())
+ {
+ $userlist[] = new PanelUser(NULL, $row['user_id']);
+ }
+ if (!empty($userlist))
+ $list = $userlist;
+
+ }
+ public static function user_delete(&$u)
+ {
+ $user = $u['user'];
+ $query = "DELETE FROM " . SQL_PREFIX . "users WHERE user_id = :id";
+ $conn = sqlnew();
+ $stmt = $conn->prepare($query);
+ $stmt->execute(["id" => $user->id]);
+ $deleted = $stmt->rowCount();
+ if ($deleted)
+ {
+ $u['info'][] = "Successfully deleted user \"$user->username\"";
+ $u['boolint'] = 1;
+ } else {
+ $u['info'][] = "Unknown error";
+ $u['boolint'] = 0;
+ }
+ }
+}
+
+
+function security_check()
+{
+ $ip = $_SERVER['REMOTE_ADDR'];
+ if (dnsbl_check($ip))
+ return true;
- public static function add_overview_card(&$stats)
+ else if (fail2ban_check($ip))
{
- $num_of_panel_admins = sqlnew()->query("SELECT COUNT(*) FROM " . SQL_PREFIX . "users")->fetchColumn();
- ?>
-
-