Repository navigation
Expand file tree
/
Copy pathadmin-panel.php
More file actions
125 lines (93 loc) · 3.34 KB
/
Copy pathadmin-panel.php
File metadata and controls
125 lines (93 loc) · 3.34 KB
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
<?php
// Initialize the session
session_start();
function databaseExists() {
$testdb = new PDO("mysql:host=localhost;dbname=information_schema", "root", "");
$testdb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try{
$result = $testdb->query("SELECT SCHEMA_NAME
FROM INFORMATION_SCHEMA.SCHEMATA
WHERE SCHEMA_NAME = 'ergasiadb'");
return (bool) $result->fetchAll();
}catch(PDOException $e) {
return false;
}
}
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: login.php");
exit;
}
require_once 'config.php';
try{
$pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$id = $_SESSION['id'];
$sql = "SELECT user_type
FROM users
WHERE id=$id";
$q = $pdo->query($sql);
$user = $q->fetch();
$type = $user['user_type'];
if($type != 'admin'){
header("location: form.php");
exit;
}
}catch(PDOException $e){
echo "ERROR: " . $e->getMessage();
}
$pdo = null;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin Panel</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
body{ font: 14px sans-serif; text-align: center; }
.btn-custom{
margin: 1em;
background-color:#708090;
color:white;
border-style:dotted;
border-color:black;
}
.btn{ font-size: 3ch; border: 2px solid black; }
.btn-custom{ margin-bottom:2em; }
</style>
</head>
<body>
<h1 class="my-5">Hi, <b><?php echo htmlspecialchars($_SESSION["username"]); ?></b>. Welcome to the administration panel.</h1>
<?php
if(array_key_exists('button1', $_POST)) {
if(include("createDB.php")){
echo '<script>alert("Database created successfully. / Already exists.")</script>';
}else {
echo '<script>alert("Error creating database.")</script>';
}
}
if(array_key_exists('button2', $_POST)){
if(DatabaseExists()){
header("location: showDB.php");
exit;
}
if(!DatabaseExists()) echo '<script>alert("Cannot show database because database does not exist.")</script>';
}
if(array_key_exists('button3', $_POST)){
if(DatabaseExists()){
header("location: searchDB.php");
exit;
}
if(!DatabaseExists()) echo '<script>alert("Cannot search database because database does not exist.")</script>';
}
?>
<form method="post">
<input type="submit" name="button1" class="btn btn-custom" value="Create DB" />
<input type="submit" name="button2" class="btn btn-custom" value="Show DB" />
<input type="submit" name="button3" class="btn btn-custom" value="Search DB" />
</form>
<a href="reset-password.php" class="btn btn-warning">Reset Your Password</a>
<a href="logout.php" class="btn btn-danger ml-3">Sign Out of Your Account</a>
</p>
</body>
</html>