forgot file and maybe min compatible php 5.5

This commit is contained in:
Nereziel
2024-02-03 16:43:50 +01:00
parent eb6cdfc98d
commit 8f0e8a0a63
3 changed files with 101 additions and 28 deletions

View File

@@ -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);
}
}
}

71
website/class/header.php Normal file
View File

@@ -0,0 +1,71 @@
<?php
// Set security headers to enhance security
header("X-Frame-Options: SAMEORIGIN");
header("X-XSS-Protection: 1; mode=block");
header("X-Content-Type-Options: nosniff");
header("Referrer-Policy: no-referrer-when-downgrade");
header("Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net https://code.jquery.com; style-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; img-src 'self' data: https://cdn.jsdelivr.net https://steamcommunity-a.akamaihd.net https://raw.githubusercontent.com;");
// Include necessary classes and files
require 'class/config.php';
require 'class/database.php';
require 'steamauth/steamauth.php';
require 'class/utils.php';
// Create a database instance
$db = new DataBase();
// Check if the user is logged in
if (isset($_SESSION['steamid'])) {
// Insert or update user's Steam ID in the database
$steamid = $_SESSION['steamid'];
$db->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']}");
}
}
?>

View File

@@ -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;
}
}