public $author = "Valware";
public $version = "1.0";
public $description = "Provides a User Auth and Management Panel with an SQL backend";
+ public $email = "v.a.pond@outlook.com";
function __construct()
{
self::create_tables();
- Hook::func(HOOKTYPE_NAVBAR, 'sql_auth::add_navbar');
Hook::func(HOOKTYPE_PRE_HEADER, 'sql_auth::session_start');
- Hook::func(HOOKTYPE_OVERVIEW_CARD, 'sql_auth::add_overview_card');
Hook::func(HOOKTYPE_FOOTER, 'sql_auth::add_footer_info');
Hook::func(HOOKTYPE_USER_LOOKUP, 'sql_auth::get_user');
Hook::func(HOOKTYPE_USERMETA_ADD, 'sql_auth::add_usermeta');
Hook::func(HOOKTYPE_USERMETA_DEL, 'sql_auth::del_usermeta');
Hook::func(HOOKTYPE_USERMETA_GET, 'sql_auth::get_usermeta');
+ Hook::func(HOOKTYPE_USER_CREATE, 'sql_auth::user_create');
+ Hook::func(HOOKTYPE_GET_USER_LIST, 'sql_auth::get_user_list');
+ Hook::func(HOOKTYPE_USER_DELETE, 'sql_auth::user_delete');
+ Hook::func(HOOKTYPE_EDIT_USER, 'sql_auth::edit_core');
+ Hook::func(HOOKTYPE_PRE_OVERVIEW_CARD, 'sql_auth::add_pre_overview_card');
+ AuthModLoaded::$status = 1;
if (defined('SQL_DEFAULT_USER')) // we've got a default account
{
if (!$lkup->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);
}
+ $lkup = new PanelUser(SQL_DEFAULT_USER['username']);
+ if (!user_can($lkup, PERMISSION_MANAGE_USERS))
+ $lkup->add_permission(PERMISSION_MANAGE_USERS);
}
}
- public static function add_navbar(&$pages)
- {
- $user = unreal_get_current_user();
- if (!$user)
- {
- $pages = NULL;
- return;
- }
- $pages["Panel Access"] = "plugins/sql_auth/";
- if (isset($_SESSION['id']))
- {
- $pages["Logout"] = "plugins/sql_auth/login.php?logout=true";
- }
- }
public static function add_footer_info($empty)
{
}
}
+ public static function add_pre_overview_card($empty)
+ {
+ if (defined('SQL_DEFAULT_USER'))
+ Message::Fail("Warning: SQL_DEFAULT_USER is set in config.php. You should remove that item now, as it is only used during installation.");
+ }
+
/* pre-Header hook */
public static function session_start($n)
{
session_set_cookie_params(3600);
session_start();
}
- do_log($_SESSION);
if (!isset($_SESSION['id']) || empty($_SESSION))
{
- $secure = ($_SERVER['HTTPS'] == 'on') ? "https://" : "http://";
- $current_url = "$secure$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
+ $current_page = $_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."plugins/sql_auth/login.php?redirect=".urlencode($current_url));
+ header("Location: ".BASE_URL."login/?redirect=".urlencode($current_page));
die();
}
else
{
- if (!unreal_get_current_user()->id) // user no longer exists
+ if (!unreal_get_current_user()) // 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
user_id int AUTO_INCREMENT NOT NULL,
user_name VARCHAR(255) NOT NULL,
user_pass VARCHAR(255) NOT NULL,
-
+ user_email VARCHAR(255),
user_fname VARCHAR(255),
user_lname VARCHAR(255),
user_bio VARCHAR(255),
created VARCHAR(255),
PRIMARY KEY (user_id)
)");
+
+ /**
+ * Patch for beta users
+ * This adds the email column to existing tables without it
+ */
+ $columns = $conn->query("SHOW COLUMNS FROM " . SQL_PREFIX . "users");
+ $column_names = array();
+ $c = $columns->fetchAll();
+
+ foreach($c as $column) {
+ $column_names[] = $column['Field'];
+ }
+ $column_exists = in_array("user_email", $column_names);
+ if (!$column_exists) {
+ $conn->query("ALTER TABLE " . SQL_PREFIX . "users ADD COLUMN user_email varchar(255)");
+ }
+
+ /**
+ * Another patch for beta users
+ * This changes the size of the meta_value so we can store more
+ */
+
$conn->query("CREATE TABLE IF NOT EXISTS " . SQL_PREFIX . "user_meta (
meta_id int AUTO_INCREMENT NOT NULL,
user_id int NOT NULL,
count VARCHAR(255),
PRIMARY KEY (id)
)");
- new AuthSettings();
- }
+ $c = [];
+ if (($columns = $conn->query("SHOW COLUMNS FROM ".SQL_PREFIX."user_meta")));
+ $c = $columns->fetchAll();
+ if (!empty($c))
+ $conn->query("ALTER TABLE `".SQL_PREFIX."user_meta` CHANGE `meta_value` `meta_value` VARCHAR(5000) CHARACTER SET utf8mb3 COLLATE utf8mb3_bin NULL DEFAULT NULL");
- /**
- * Summary of add_overview_card
- * @param mixed $stats
- * @return void
- */
- public static function add_overview_card(object &$stats) : void
- {
- $num_of_panel_admins = sqlnew()->query("SELECT COUNT(*) FROM " . SQL_PREFIX . "users")->fetchColumn();
- ?>
-
- <div class="container mt-5">
-
- <div class="row">
- <div class="col-sm-3">
- <div class="card text-center">
- <div class="card-header bg-success text-white">
- <div class="row">
- <div class="col">
- <i class="fa fa-lock-open fa-3x"></i>
- </div>
- <div class="col">
- <h3 class="display-4"><?php echo $num_of_panel_admins; ?></h3>
- </div>
- </div>
- </div>
- <div class="card-body">
- <div class="row">
- <div class="col">
- <h6>Panel Users</h6>
- </div>
- <div class="col"> <a class="btn btn-primary" href="<?php echo BASE_URL; ?>plugins/sql_auth/">View</a></div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- <?php
+
+ new AuthSettings();
}
/* We convert $u with a full user as an object ;D*/
$obj->last_name = $data['user_lname'] ?? NULL;
$obj->created = $data['created'];
$obj->bio = $data['user_bio'];
+ $obj->email = $data['user_email'];
$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();
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";
return true;
return false;
}
+ public static function user_create(&$u)
+ {
+ $username = $u['user_name'];
+ $first_name = $u['fname'] ?? NULL;
+ $last_name = $u['lname'] ?? NULL;
+ $password = $u['user_pass'] ?? NULL;
+ $user_bio = $u['user_bio'] ?? NULL;
+ $user_email = $u['user_email'] ?? NULL;
+ $conn = sqlnew();
+ $prep = $conn->prepare("INSERT INTO " . SQL_PREFIX . "users (user_name, user_pass, user_fname, user_lname, user_bio, user_email, created) VALUES (:name, :pass, :fname, :lname, :user_bio, :user_email, :created)");
+ $prep->execute(["name" => $username, "pass" => $password, "fname" => $first_name, "lname" => $last_name, "user_bio" => $user_bio, "user_email" => $user_email, "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;
+ }
+ }
+
+ public static function edit_core($arr)
+ {
+ $conn = sqlnew();
+ $user = $arr['user'];
+ $info = $arr['info'];
+ foreach($info as $key => $val)
+ {
+ $value = NULL;
+ if (!$val || !strlen($val) || BadPtr($val))
+ continue;
+ if (!strcmp($key,"update_fname") && $val != $user->first_name)
+ {
+ $value = "user_fname";
+ $valuestr = "first name";
+ }
+ elseif (!strcmp($key,"update_lname") && $val != $user->last_name)
+ {
+ $value = "user_lname";
+ $valuestr = "last name";
+ }
+ elseif (!strcmp($key,"update_bio") && $val != $user->bio)
+ {
+ $value = "user_bio";
+ $valuestr = "bio";
+ }
+ elseif (!strcmp($key,"update_pass") || !strcmp($key,"update_pass_conf"))
+ {
+ $value = "user_pass";
+ $valuestr = "password";
+ }
+ elseif(!strcmp($key,"update_email") && $val != $user->email)
+ {
+ $value = "user_email";
+ $valuestr = "email address";
+ }
+
+ if (!$value)
+ continue;
+ $query = "UPDATE " . SQL_PREFIX . "users SET $value=:value WHERE user_id = :id";
+ $stmt = $conn->prepare($query);
+ $stmt->execute(["value" => $val, "id" => $user->id]);
+
+ if (!$stmt->rowCount() && $stmt->errorInfo()[0] != "00000")
+ Message::Fail("Could not update $valuestr for $user->username: ".$stmt->errorInfo()[0]." (CODE: ".$stmt->errorCode().")");
+
+ else
+ Message::Success("Successfully updated the $valuestr for $user->username");
+ }
+ }
}
function dnsbl_check($ip)
{
+
+ if (!defined('DNSBL'))
+ return;
$dnsbl_lookup = DNSBL;
// clear variable just in case