$value) $this->$key = $value; } /** * 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 */ function add_meta(string $key, string $value) { if (!$key || !$value) return false; $meta = [ "id" => $this->id, "key" => $key, "value" => $value ]; $array['meta'] = $meta; $array['user'] = $this; Hook::run(HOOKTYPE_USERMETA_ADD, $array); } /** * Delete user meta data by key * @param string $key */ function delete_meta(string $key) { if (!$key ) return false; $meta = [ "id" => $this->id, "key" => $key, ]; Hook::run(HOOKTYPE_USERMETA_DEL, $meta); } /** PERMISSIONS */ function add_permission($permission) { $meta = (isset($this->user_meta['permissions'])) ? unserialize($this->user_meta['permissions']) : []; if (!in_array($permission,$meta)) $meta[] = $permission; $this->add_meta("permissions", serialize($meta)); // updet de dettabess $this->user_meta['permissions'] = serialize($meta); // put it back in our object in case it still needs to be used } function delete_permission($permission) { $meta = (isset($this->user_meta['permissions'])) ? unserialize($this->user_meta['permissions']) : []; foreach($meta as $key => $value) { if (!strcmp($permission, $value)) unset($meta[$key]); } $this->add_meta("permissions", serialize($meta)); $this->user_meta['permissions'] = serialize($meta); } } /** * This class looks up and returns any user meta. * This is used by PanelUser, so you won't need to * call it separately from PanelUser. */ class PanelUser_Meta { public $list = []; function __construct($id) { $array = []; $arr["id"] = $id; $arr['meta'] = &$array; Hook::run(HOOKTYPE_USERMETA_GET, $arr); do_log($array); $this->list = $arr['meta']; } } /** * Array of user * * Required: * user_name * user_pass * * Optional: * user_fname * user_lname * * @param array $user * @throws Exception * @return bool */ function create_new_user(array &$user) : bool { if (!isset($user['user_name']) || !isset($user['user_pass'])) throw new Exception("Attempted to add user without specifying user_name or user_pass"); $user['user_name'] = htmlspecialchars($user['user_name']); $user['user_pass'] = password_hash($user['user_pass'], PASSWORD_ARGON2ID); $user['fname'] = (isset($user['fname'])) ? htmlspecialchars($user['fname']) : NULL; $last['lname'] = (isset($user['lname'])) ? htmlspecialchars($user['lname']) : NULL; $user['user_bio'] = (isset($user['user_bio'])) ? htmlspecialchars($user['user_bio']) : NULL; if (($u = new PanelUser($user['user_name']))->id) { $user['err'] = "User already exists"; return false; } // internal use $user['success'] = false; $user['errmsg'] = []; Hook::run(HOOKTYPE_USER_CREATE, $user); if (!$user['success']) return false; return true; } /** * Gets the user object for the current session * @return PanelUser|bool */ function unreal_get_current_user() : PanelUser|bool { if (!isset($_SESSION)) { session_set_cookie_params(3600); session_start(); } if (isset($_SESSION['id'])) { $user = new PanelUser(NULL, $_SESSION['id']); if ($user->id) return $user; } return false; } /** * Checks if a user can do something * @param string $permission * @return bool */ function current_user_can($permission) : bool { $user = unreal_get_current_user(); do_log($user); if (!$user) return false; do_log($user); if (isset($user->user_meta['permissions'])) { $perms = unserialize($user->user_meta['permissions']); if (in_array($permission, $perms)) { return true; } } return false; } /** * Delete a user and related meta * @param int $id The ID of the user in the SQL database. * @param array $info This will fill with a response. * @return int * * Return values: * 1 The user was successfully deleted. * 0 The user was not found * -1 The admin does not have permission to delete users [TODO] */ function delete_user(int $id, &$info = []) : int { $user = new PanelUser(NULL, $id); if (!$user->id) { $info[] = "Could not find user"; return 0; } $arr = ["user" => $user, "info" => &$info, "boolint" => 0]; Hook::run(HOOKTYPE_USER_DELETE, $arr); return $arr["boolint"]; }