-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.php
205 lines (182 loc) · 9.41 KB
/
admin.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
session_start();
include('db.php');
error_reporting(E_ERROR | E_PARSE);
if(isset($_GET['accueil'])) {
$select_nombre_item = $bdd->prepare('SELECT id_from_user FROM cart WHERE id_from_user = ?');
$select_nombre_item->execute(array($_SESSION['id']));
$nombre_item_user = $select_nombre_item->rowCount();
$verif_id = $bdd->prepare('SELECT * FROM user WHERE user_id = ?');
$verif_id->execute(array($_GET['accueil']));
$user = $verif_id->fetch();
$id_exist = $verif_id->rowCount();
if($id_exist === 1) {
if($user['role'] == 1) {
if(isset($_POST['deco'])) {
session_destroy();
header('Location: index.php');
}?>
<link rel="stylesheet" href="css/admin.css" type="text/css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<title>ADMIN - silkroad</title>
<div>
<ul class="topnav">
<a href="index.php">
<img src="img/silkroad.png" class="logo_silkroad">
<h3 class="marque">Silkroad</h3>
</a><?php
if(isset($_SESSION['id'])) { ?>
<li class="Tosell"><a href="sell.php">+ Add item</a></li>
<li class="Tosell2"><a href="edit.php">My items</a></li>
<li><a href="cart.php">Cart - <?php echo $nombre_item_user; ?></a></li>
<li><a href="chat_list.php">Chat</a></li>
<li><a href="account.php">Account</a></li>
<?php
}
if(isset($_SESSION['id'])) {
$select_username = $bdd->prepare('SELECT * FROM user WHERE user_id = ?');
$select_username->execute(array($_SESSION['id']));
$user = $select_username->fetch();?>
<?php
} else { ?>
<li><a href="login.php">Log in</a></li>
<li><a href="register.php">Register</a></li>
<?php
} ?>
</ul>
</div>
<form method="POST">
<?php
if($_SESSION['id']) {
$select_username = $bdd->prepare('SELECT * FROM user WHERE user_id = ?');
$select_username->execute(array($_SESSION['id']));
$user = $select_username->fetch();?>
<?php
} else {?>
<input type="submit" name="Login" value="Login">
<input type="submit" name="register" value="register">
<?php
}?>
</form>
<?php
$select_ask_approve = $bdd->query('SELECT * FROM article WHERE approuve = 0');
$ask_approve = $select_ask_approve->rowCount(); ?>
<div>
<a href="list_to_approve.php">Request(s) pending: <?php echo $ask_approve; ?></a>
</div>
<?php
if(isset($_POST['update'])) {
$item_id = $_POST['item_id'];
$name = $_POST['name'];
$description = $_POST['describe'];
$price = $_POST['price'];
$stmt = $bdd->prepare("UPDATE article SET name = :name, description = :description, price = :price WHERE item_id = :item_id");
$stmt->execute(['name' => $name, 'description' => $description, 'price' => $price, 'item_id' => $item_id]);
} elseif(isset($_POST['del'])) {
$my_item = $_POST['item_id'];
$delete_query = $bdd->prepare("DELETE FROM article WHERE item_id = :item_id");
$delete_query->bindParam(":item_id", $my_item);
$delete_query->execute();
header("Location: admin.php");
}
if(isset($_POST['update_user'])) {
$user_id = $_POST['user_id'];
$username = $_POST['username'];
$mail = $_POST['mail'];
$password_crypted = password_hash($_POST['password'], PASSWORD_BCRYPT);
$stmt = $bdd->prepare("UPDATE user SET username = :username, mail = :mail, password = :password WHERE user_id = :user_id");
$stmt->execute(['username' => $username, 'mail' => $mail, 'password' => $password_crypted, 'user_id' => $user_id]);
} elseif(isset($_POST['del_user'])) {
$my_user = $_POST['user_id'];
$delete_query = $bdd->prepare("DELETE FROM user WHERE user_id = :user_id");
$delete_query->bindParam(":user_id", $my_user);
$delete_query->execute();
header("Location: admin.php");
}
$select_items = $bdd->query('SELECT * FROM article WHERE item_id > 0');
$select_users = $bdd->query('SELECT * FROM user WHERE user_id > 0'); ?>
<div style="display:flex; flex-wrap: wrap; margin-top: 20px">
<div style="flex: 1; margin: 0 10px;">
<label style="font-size: 200%">Item List</label>
<?php
while($item = $select_items->fetch()) {
?>
<div>
<form method="POST">
<img src="img/<?php echo $item['article_pic_link']; ?><?php echo $item['extension_image']; ?>" alt="">
<p>Name : <input name="name" value=<?=$item['name']?>></input></p>
<p>Describe : <input type="text" name="describe" value=<?=$item['description']?>></input></p>
<p>Price : <input name="price" value=<?=$item['price']?>></input>€</p>
<p>Id : <?php echo $item['item_id']; ?></p>
<input type="hidden" name="item_id" value="<?php echo $item['item_id']; ?>">
<input style="background-color: #98de98" type="submit" value="update" name="update">
<input style="background-color: #ff8c8c;" type="submit" value="delete" name="del">
</a>
</form>
</div>
<?php
} ?>
</div>
<br><br>
<div style="flex: 1; margin: 0 10px;">
<label style="font-size: 200%">User list</label>
<?php
while($user = $select_users->fetch()) { ?>
<div>
<form method="POST">
<img src="img_profile/<?php echo $user['profile_pic']; ?>" style="width: 20%;">
<p>Username : <input name="username" value=<?=$user['username']?>></input></p>
<p>Mail : <input name="mail" value=<?=$user['mail']?>></input></p>
<p>Password : <input name="password" value=<?=$user['password']?>></input></p>
<input type="hidden" name="user_id" value="<?php echo $user['user_id']; ?>">
<input style="background-color: #98de98" type="submit" value="update_user" name="update_user">
<input style="background-color: #ff8c8c;" type="submit" value="delete" name="del_user">
</form>
</div>
<br><br>
<?php
} ?>
</div>
</div>
<?php
} else {
echo "You are not an admin";
}
} else {
echo "Invalid credentials";
}
} else {
if(isset($_POST['submit'])) {
if(!empty($_POST['username']) AND !empty($_POST['password'])) {
$verif_username = $bdd->prepare('SELECT * FROM user WHERE username = ?');
$verif_username->execute(array($_POST['username']));
$user = $verif_username->fetch();
$username_exist = $verif_username->rowCount();
if($username_exist === 1) {
if($user['role'] == 1) {
if(password_verify($_POST['password'], $user['password'])) {
$_SESSION['id'] = $user['id'];
header('Location: index.php');
} else {
echo "wrong password";
}
} else {
echo "not an administator";
}
}
}
}
if(isset($_SESSION['id'])) {
header("Location: admin.php?accueil=" .$_SESSION['id']);
} else { ?>
<title>Login</title>
<div>
<form method="POST">
<input type="text" placeholder="Username" name="username">
<input type="password" placeholder="Password" name="password">
<input type="submit" value="submit" name="submit">
</form>
</div>
<?php
}
}