diff --git a/aura_bank/buy.php b/aura_bank/buy.php new file mode 100644 index 0000000..25bd615 --- /dev/null +++ b/aura_bank/buy.php @@ -0,0 +1,97 @@ +beginTransaction(); + + // Vérifie si le user possède déjà un item + $stmt = $pdo->prepare("SELECT ui.id, i.name + FROM user_items ui + JOIN items i ON ui.item_id = i.id + WHERE ui.user_id = :uid + LIMIT 1 FOR UPDATE"); + $stmt->execute([':uid' => $user_id]); + $existing = $stmt->fetch(); + + if ($existing) { + $pdo->rollBack(); + exit("Tu possèdes déjà un item actif : " . htmlspecialchars($existing['name'])); + } + + // Récupère item + $stmt = $pdo->prepare("SELECT id, name, price FROM items WHERE id = :id FOR UPDATE"); + $stmt->execute([':id' => $item_id]); + $item = $stmt->fetch(); + if (!$item) { + $pdo->rollBack(); + exit('Item introuvable.'); + } + + $total = (int)$item['price']; + + // Vérifie aura + $stmt = $pdo->prepare("SELECT aura FROM users WHERE id = :uid FOR UPDATE"); + $stmt->execute([':uid' => $user_id]); + $u = $stmt->fetch(); + if (!$u) { + $pdo->rollBack(); + exit('Utilisateur introuvable.'); + } + $aura = (int)$u['aura']; + + if ($aura < $total) { + $pdo->rollBack(); + exit('Tu n\'as pas assez d\'aura pour cet achat.'); + } + + // Débite l'aura + $stmt = $pdo->prepare("UPDATE users SET aura = aura - :amt WHERE id = :uid"); + $stmt->execute([':amt' => $total, ':uid' => $user_id]); + + // Ajoute item à user_items + $stmt = $pdo->prepare("INSERT INTO user_items (user_id, item_id) VALUES (:uid, :iid)"); + $stmt->execute([':uid' => $user_id, ':iid' => $item_id]); + + // Log (version simplifiée, sans actor_discord_id ni type) + $stmt = $pdo->prepare("INSERT INTO logs (user_id, amount, reason) + VALUES (:uid, :amount, :reason)"); + $stmt->execute([ + ':uid' => $user_id, + ':amount' => -$total, + ':reason' => 'Achat: ' . $item['name'] + ]); + + $pdo->commit(); + + header('Location: shop.php?buy=ok'); + exit; + +} catch (Exception $e) { + if ($pdo->inTransaction()) $pdo->rollBack(); + echo "Erreur détaillée : " . $e->getMessage(); + var_dump($item_id, $user_id, $total, $aura); + exit; +} diff --git a/aura_bank/callback.php b/aura_bank/callback.php new file mode 100644 index 0000000..e58acbe --- /dev/null +++ b/aura_bank/callback.php @@ -0,0 +1,120 @@ + DISCORD_CLIENT_ID, + 'client_secret' => DISCORD_CLIENT_SECRET, + 'grant_type' => 'authorization_code', + 'code' => $code, + 'redirect_uri' => DISCORD_REDIRECT_URI, + 'scope' => 'identify email' +]; + +$ch = curl_init($token_url); +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); +curl_setopt($ch, CURLOPT_POST, true); +curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_fields)); +curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Content-Type: application/x-www-form-urlencoded' +]); +// ⚠️ remet le SSL, mieux pour la prod +curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); +$response = curl_exec($ch); + +if ($response === false) { + exit("Erreur cURL token: " . curl_error($ch)); +} +curl_close($ch); + +$token_data = json_decode($response, true); +if (!isset($token_data['access_token'])) { + exit("Échec de l'échange de token: " . htmlspecialchars($response)); +} +$access_token = $token_data['access_token']; + +// Étape 2 : récupérer infos utilisateur +$ch = curl_init("https://discord.com/api/users/@me"); +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); +curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "Authorization: Bearer $access_token" +]); +$user_json = curl_exec($ch); +if ($user_json === false) { + exit("Erreur cURL user: " . curl_error($ch)); +} +curl_close($ch); + +$user_data = json_decode($user_json, true); +if (!isset($user_data['id'])) { + exit("Impossible de récupérer l'utilisateur Discord. Réponse: " . htmlspecialchars($user_json)); +} + +// Préparation des données +$discord_id = $user_data['id']; +$username = $user_data['username'] . (isset($user_data['discriminator']) && $user_data['discriminator'] !== "0" ? '#' . $user_data['discriminator'] : ""); +$email = $user_data['email'] ?? null; +$avatar = !empty($user_data['avatar']) + ? "https://cdn.discordapp.com/avatars/{$discord_id}/{$user_data['avatar']}.png" + : null; + +// Étape 3 : DB +try { + $pdo = pdo_connect(); +} catch (Exception $e) { + exit("Erreur DB : " . $e->getMessage()); +} + +$stmt = $pdo->prepare("SELECT id FROM users WHERE discord_id = :did LIMIT 1"); +$stmt->execute([':did' => $discord_id]); +$u = $stmt->fetch(); + +if ($u) { + $stmt = $pdo->prepare("UPDATE users SET username = :username, email = :email, profile_picture = :avatar WHERE discord_id = :did"); + $stmt->execute([ + ':username' => $username, + ':email' => $email, + ':avatar' => $avatar, + ':did' => $discord_id + ]); + $user_id = $u['id']; +} else { + $stmt = $pdo->prepare("INSERT INTO users (discord_id, username, email, profile_picture, aura, tier) + VALUES (:did, :username, :email, :avatar, 0, 'Aura')"); + $stmt->execute([ + ':did' => $discord_id, + ':username' => $username, + ':email' => $email, + ':avatar' => $avatar + ]); + $user_id = $pdo->lastInsertId(); +} + +// Étape 4 : session +$_SESSION['user_id'] = $user_id; +$_SESSION['discord_id'] = $discord_id; +$_SESSION['username'] = $username; +$_SESSION['profile_picture'] = $avatar; + +// Étape 5 : redirection +header("Location: index.php"); +exit; diff --git a/aura_bank/config.php b/aura_bank/config.php new file mode 100644 index 0000000..dac4851 --- /dev/null +++ b/aura_bank/config.php @@ -0,0 +1,25 @@ + PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4" +]; + +function pdo_connect(){ + global $options; + $dsn = "mysql:host=".DB_HOST.";port=".DB_PORT.";dbname=".DB_NAME.";charset=utf8mb4"; + return new PDO($dsn, DB_USER, DB_PASS, $options); +} diff --git a/aura_bank/functions.php b/aura_bank/functions.php new file mode 100644 index 0000000..f05ff9f --- /dev/null +++ b/aura_bank/functions.php @@ -0,0 +1,26 @@ +prepare("SELECT username, aura, discord_id, tier, profile_picture FROM users ORDER BY aura DESC LIMIT :lim"); + $stmt->bindValue(':lim', (int)$limit, PDO::PARAM_INT); + $stmt->execute(); + return $stmt->fetchAll(); +} + +function get_user_by_discord($discord_id) { + $pdo = pdo_connect(); + $stmt = $pdo->prepare("SELECT * FROM users WHERE discord_id = :did LIMIT 1"); + $stmt->execute([':did' => $discord_id]); + return $stmt->fetch(); +} + +function get_user_rank($aura) { + $pdo = pdo_connect(); + $stmt = $pdo->prepare("SELECT COUNT(*) + 1 AS 'rank' FROM users WHERE aura > :aura"); + $stmt->execute([':aura' => $aura]); + $r = $stmt->fetch(); + return $r ? (int)$r['rank'] : null; +} diff --git a/aura_bank/img/items/1.png b/aura_bank/img/items/1.png new file mode 100644 index 0000000..5b6ce2e Binary files /dev/null and b/aura_bank/img/items/1.png differ diff --git a/aura_bank/img/items/10.png b/aura_bank/img/items/10.png new file mode 100644 index 0000000..e848c2b Binary files /dev/null and b/aura_bank/img/items/10.png differ diff --git a/aura_bank/img/items/2.png b/aura_bank/img/items/2.png new file mode 100644 index 0000000..a5a5dd6 Binary files /dev/null and b/aura_bank/img/items/2.png differ diff --git a/aura_bank/img/items/3.png b/aura_bank/img/items/3.png new file mode 100644 index 0000000..a07056f Binary files /dev/null and b/aura_bank/img/items/3.png differ diff --git a/aura_bank/img/items/4.png b/aura_bank/img/items/4.png new file mode 100644 index 0000000..5fdc6cf Binary files /dev/null and b/aura_bank/img/items/4.png differ diff --git a/aura_bank/img/items/5.png b/aura_bank/img/items/5.png new file mode 100644 index 0000000..3b85613 Binary files /dev/null and b/aura_bank/img/items/5.png differ diff --git a/aura_bank/img/items/6.png b/aura_bank/img/items/6.png new file mode 100644 index 0000000..caf7d37 Binary files /dev/null and b/aura_bank/img/items/6.png differ diff --git a/aura_bank/img/items/7.png b/aura_bank/img/items/7.png new file mode 100644 index 0000000..2380290 Binary files /dev/null and b/aura_bank/img/items/7.png differ diff --git a/aura_bank/img/items/8.png b/aura_bank/img/items/8.png new file mode 100644 index 0000000..2380290 Binary files /dev/null and b/aura_bank/img/items/8.png differ diff --git a/aura_bank/img/items/9.png b/aura_bank/img/items/9.png new file mode 100644 index 0000000..6780871 Binary files /dev/null and b/aura_bank/img/items/9.png differ diff --git a/aura_bank/index.php b/aura_bank/index.php new file mode 100644 index 0000000..7c66db9 --- /dev/null +++ b/aura_bank/index.php @@ -0,0 +1,78 @@ + + + +
+ + +Le classement est vide.
+ +| # | Utilisateur | Aura | Grade |
|---|---|---|---|
| = $pos ?> | +
+
+ ID: = htmlspecialchars($row['discord_id']) ?>
+ |
+ = (int)$row['aura'] ?> | += htmlspecialchars($row['tier']) ?> | +
Se connecter avec Discord
+
+
+
diff --git a/aura_bank/logout.php b/aura_bank/logout.php
new file mode 100644
index 0000000..7889664
--- /dev/null
+++ b/aura_bank/logout.php
@@ -0,0 +1,5 @@
+
+
+
+
+
+ Aucun historique.
+ +| Date | +Montant | +Raison | +
|---|---|---|
| = htmlspecialchars($l['created_at']) ?> | += $amount ?> | += htmlspecialchars($l['reason']) ?> | +
+ = htmlspecialchars($it['name']) ?>
+