From 8f0e8a0a63efd7d2c77a75a4a9b4c699f3a6143b Mon Sep 17 00:00:00 2001 From: Nereziel Date: Sat, 3 Feb 2024 16:43:50 +0100 Subject: [PATCH] forgot file and maybe min compatible php 5.5 --- website/class/database.php | 17 +++++---- website/class/header.php | 71 ++++++++++++++++++++++++++++++++++++++ website/class/utils.php | 41 +++++++++++----------- 3 files changed, 101 insertions(+), 28 deletions(-) create mode 100644 website/class/header.php diff --git a/website/class/database.php b/website/class/database.php index 10321207..654abda9 100644 --- a/website/class/database.php +++ b/website/class/database.php @@ -17,11 +17,12 @@ class DataBase { try { // Establish a connection to the database using PDO $this->PDO = new PDO( - "mysql:host=".DB_HOST."; port=".DB_PORT."; dbname=".DB_NAME, + "mysql:host=".DB_HOST.";port=".DB_PORT.";dbname=".DB_NAME, DB_USER, - DB_PASS, - array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") + DB_PASS ); + // Set the connection to use utf8 encoding + $this->PDO->exec("SET NAMES utf8"); } catch(PDOException $ex) { // Display error message if connection fails @@ -37,14 +38,16 @@ class DataBase { * @param array $bindings An associative array of parameters and their values. * @return array|false Returns an array of rows as associative arrays or false if no results are found. */ - public function select($query, $bindings = []) { + public function select($query, $bindings = array()) { // Prepare and execute the SQL query $STH = $this->PDO->prepare($query); $STH->execute($bindings); // Fetch the results as associative arrays $result = $STH->fetchAll(PDO::FETCH_ASSOC); - $result ??= false; // Set $result to false if it's null + if ($result === false) { + $result = array(); // Set $result to an empty array if no results found + } return $result; } @@ -55,9 +58,9 @@ class DataBase { * @param array $bindings An associative array of parameters and their values. * @return bool Returns true on success or false on failure. */ - public function query($query, $bindings = []) { + public function query($query, $bindings = array()) { // Prepare and execute the SQL query $STH = $this->PDO->prepare($query); return $STH->execute($bindings); } -} +} \ No newline at end of file diff --git a/website/class/header.php b/website/class/header.php new file mode 100644 index 00000000..8acb64ca --- /dev/null +++ b/website/class/header.php @@ -0,0 +1,71 @@ +query("INSERT INTO `wp_users` (`steamid`) VALUES ('{$steamid}') ON DUPLICATE KEY UPDATE `updated_at` = CURRENT_TIMESTAMP"); + + // Get user's database index + $userInfoQuery = $db->select("SELECT `id` FROM `wp_users` WHERE `steamid` = :steamid", ["steamid" => $steamid]); + $_SESSION['userDbIndex'] = $userDbIndex = (int)$userInfoQuery[0]['id']; + + // Get weapons and skins information + $weapons = UtilsClass::getWeaponsFromArray(); + $skins = UtilsClass::skinsFromJson(); + + // Retrieve user's selected skins and knife + $querySelected = $db->select("SELECT `weapon`, `paint`, `wear`, `seed`, `nametag` FROM `wp_users_items` WHERE `user_id` = :user_id", ["user_id" => $userDbIndex]); + $selectedSkins = UtilsClass::getSelectedSkins($querySelected); + $selectedKnifeResult = $db->select("SELECT `knife` FROM `wp_users_knife` WHERE `user_id` = :user_id", ["user_id" => $userDbIndex]); + + // Determine user's selected knife or set default knife + if (!empty($selectedKnifeResult)) { + $selectedKnife = $selectedKnifeResult[0]['knife']; + } else { + $selectedKnife = "weapon_knife"; + } + $knifes = UtilsClass::getKnifeTypes(); + + // Handle form submission + if (isset($_POST['forma'])) { + $ex = explode("-", $_POST['forma']); + + // Handle knife selection + if ($ex[0] == "knife") { + $db->query("INSERT INTO `wp_users_knife` (`user_id`, `knife`) VALUES(:user_id, :knife) ON DUPLICATE KEY UPDATE `knife` = :knife", ["user_id" => $userDbIndex, "knife" => $knifes[$ex[1]]['weapon_name']]); + } else { + // Handle skin selection + if (array_key_exists($ex[1], $skins[$ex[0]]) && isset($_POST['wear']) && $_POST['wear'] >= 0.00 && $_POST['wear'] <= 1.00 && isset($_POST['seed'])) { + $wear = floatval($_POST['wear']); // wear + $seed = intval($_POST['seed']); // seed + + // Check if the skin is already selected and update or insert accordingly + if (array_key_exists($ex[0], $selectedSkins)) { + $db->query("UPDATE wp_users_items SET paint = :weapon_paint_id, wear = :weapon_wear, seed = :weapon_seed WHERE user_id = :user_id AND weapon = :weapon_defindex", ["user_id" => $userDbIndex, "weapon_defindex" => $ex[0], "weapon_paint_id" => $ex[1], "weapon_wear" => $wear, "weapon_seed" => $seed]); + } else { + $db->query("INSERT INTO wp_users_items (`user_id`, `weapon`, `paint`, `wear`, `seed`) VALUES (:user_id, :weapon_defindex, :weapon_paint_id, :weapon_wear, :weapon_seed)", ["user_id" => $userDbIndex, "weapon_defindex" => $ex[0], "weapon_paint_id" => $ex[1], "weapon_wear" => $wear, "weapon_seed" => $seed]); + } + } + } + // Redirect to the same page after form submission + header("Location: {$_SERVER['PHP_SELF']}"); + } +} +?> diff --git a/website/class/utils.php b/website/class/utils.php index 323d3014..d97f2ea6 100644 --- a/website/class/utils.php +++ b/website/class/utils.php @@ -11,20 +11,20 @@ class UtilsClass * * @return array An associative array containing skin data. */ - public static function skinsFromJson(): array + public static function skinsFromJson() { - $skins = []; + $skins = array(); $jsonFilePath = __DIR__ . "/../data/skins.json"; if (file_exists($jsonFilePath) && is_readable($jsonFilePath)) { $json = json_decode(file_get_contents($jsonFilePath), true); foreach ($json as $skin) { - $skins[(int) $skin['weapon_defindex']][(int) $skin['paint']] = [ + $skins[(int) $skin['weapon_defindex']][(int) $skin['paint']] = array( 'weapon_name' => $skin['weapon_name'], 'paint_name' => $skin['paint_name'], 'image_url' => $skin['image'], - ]; + ); } } else { // Handle file not found or unreadable error @@ -39,17 +39,17 @@ class UtilsClass * * @return array An associative array containing weapon data. */ - public static function getWeaponsFromArray(): array + public static function getWeaponsFromArray() { - $weapons = []; + $weapons = array(); $skinsData = self::skinsFromJson(); foreach ($skinsData as $key => $value) { - $weapons[$key] = [ + $weapons[$key] = array( 'weapon_name' => $value[0]['weapon_name'], 'paint_name' => $value[0]['paint_name'], 'image_url' => $value[0]['image_url'], - ]; + ); } return $weapons; @@ -60,32 +60,32 @@ class UtilsClass * * @return array An associative array containing knife types data. */ - public static function getKnifeTypes(): array + public static function getKnifeTypes() { - $knifes = []; + $knifes = array(); $weaponsData = self::getWeaponsFromArray(); - $allowedKnifeKeys = [ + $allowedKnifeKeys = array( 500, 503, 505, 506, 507, 508, 509, 512, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 525 - ]; + ); foreach ($weaponsData as $key => $weapon) { if (in_array($key, $allowedKnifeKeys)) { - $knifes[$key] = [ + $knifes[$key] = array( 'weapon_name' => $weapon['weapon_name'], 'paint_name' => rtrim(explode("|", $weapon['paint_name'])[0]), 'image_url' => $weapon['image_url'], - ]; + ); } } // Add default knife - $knifes[0] = [ + $knifes[0] = array( 'weapon_name' => "weapon_knife", 'paint_name' => "Default knife", 'image_url' => "https://raw.githubusercontent.com/Nereziel/cs2-WeaponPaints/main/website/img/skins/weapon_knife.png", - ]; + ); ksort($knifes); return $knifes; @@ -97,19 +97,18 @@ class UtilsClass * @param array $temp An array containing the selected skins data. * @return array An associative array containing selected skins data. */ - public static function getSelectedSkins(array $temp): array + public static function getSelectedSkins($temp) { - $selected = []; + $selected = array(); foreach ($temp as $weapon) { - $selected[$weapon['weapon']] = [ + $selected[$weapon['weapon']] = array( 'weapon_paint_id' => $weapon['paint'], 'weapon_seed' => $weapon['seed'], 'weapon_wear' => $weapon['wear'], - ]; + ); } return $selected; } } -