-
Notifications
You must be signed in to change notification settings - Fork 0
/
buy.php
60 lines (43 loc) · 1.07 KB
/
buy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
session_start();
require_once "storage/UserStorage.php";
require_once "storage/CardStorage.php";
require_once "auth.php";
$userStorage = new UserStorage();
$cardStorage = new CardStorage();
if (!$auth->is_authenticated()) {
header('Location: index.php');
exit();
}
$id = isset($_GET["id"]) ? $_GET["id"] : '';
if (empty($id)) {
header("Location: index.php");
exit();
}
//Id of card to buy
$id = $_GET["id"];
//Find card by card id
$card = $cardStorage->findById($id);
//Handle if not found
if (empty($id)) {
header('Location: index.php');
exit();
}
//Get current user
$uid = $auth->authenticated_user()["id"];
$user = $userStorage->findById($uid);
if(!($user["credits"] >= $card["price"]) || !(count($cardStorage->findAll(["owner" => $user["username"]])) < 5))
{
header('Location: index.php');
exit();
}
//Update credits
$user["credits"] -= $card["price"];
$userStorage->update($uid, $user);
//Give card to user
$card["owner"] = $user["username"];
//Update card
$cardStorage->update($id, $card);
header('Location: index.php');
exit();
?>