-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utility.php
146 lines (131 loc) · 3.5 KB
/
Utility.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
<?php
error_reporting(0);
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'custom';
#$path = 'Database';
#mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
function connect()
{
global $servername, $username, $password, $dbname;
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
return $conn;
}
function query($sql)
{
$conn = connect();
if ($conn->connect_error) {
die('Connection failed: ' . $conn->connect_error);
}
$result = $conn->query($sql);
$conn->close();
if ($result === true or $result === false or $result->num_rows > 0) {
return $result;
}
return null;
}
function checkValid($var)
{
return !is_null($var) and !empty($var);
}
function checkTable($table, $simple = false)
{
if (!checkValid($table)) {
goto end;
}
if (!$simple) {
if (!in_array($table, getTables())) {
goto end;
}
}
return;
end:
die("Tabella non valida");
}
function checkColumn($table, $column)
{
if (!checkValid($column)) {
goto end;
}
$columns = getColumns($table);
if (!in_array($column, array_column($columns, 'name'))) {
goto end;
}
return;
end:
die("Colonna non valida");
}
function getPrimaryKey($table)
{
$sql = "SHOW KEYS FROM $table WHERE Key_name = 'PRIMARY'";
$result = query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
return $row["Column_name"];
}
return null;
}
function bootstrap()
{
echo '<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>';
}
function getColumns($table)
{
$sql = "SHOW COLUMNS FROM $table";
$result = query($sql);
$fields = [];
while ($row = $result->fetch_assoc()) {
$fields[] = array(
'name' => $row["Field"],
'type' => $row["Type"]
);
}
return $fields;
}
function getTables()
{
global $dbname;
$sql = "SHOW TABLES";
$result = query($sql);
$tables = [];
while ($row = $result->fetch_assoc()) {
$tables[] = $row["Tables_in_$dbname"];
}
return $tables;
}
function bindQuery($sql, $params = [])
{
$conn = connect();
$stmt = $conn->prepare($sql);
$types = '';
$bindings = [
'integer' => 'i',
'double' => 'd',
'string' => 's',
'boolean' => 'b',
'blob' => 'b',
'NULL' => 's'
];
foreach ($params as $param)
$types .= $bindings[gettype($param)];
$stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
$conn->close();
return $result;
}
function overrideBack()
{
echo "<script type='text/javascript'>
history.pushState(null, null, '');
window.addEventListener('popstate', function(event) {
window.location.assign('index.php');
});
</script>";
}