-
Notifications
You must be signed in to change notification settings - Fork 2
/
search.php
129 lines (119 loc) · 3.24 KB
/
search.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
<?php
require_once("scripts/directory.php");
require_once("scripts/login.php");
require_once("scripts/start.php");
require_once("scripts/error.php");
// Start the page with the home tag so that the navigation
// bar has something active (the home button)
startPage("home", "add_to_fav");
echo "<div class='main'>";
$search = "<<>>";
if(isset($_GET['q'])) {
$q = trim($_GET['q']);
$search = $q;
}
else {
$error = getErrorString("No search query provided", 400);
echo $error;
die();
}
if($q === "") {
$error = getErrorString("No search query provided", 400);
echo $error;
die();
}
$connection = new mysqli($db_hostname, $db_username, $db_password, $db_database);
if($connection->connect_errno) {
$error = getErrorString("Could not connect to the database.", $connection->connect_errno);
echo $error;
$connection->close();
die();
}
$stmnt = $connection->prepare("SELECT * FROM directories WHERE dirName LIKE CONCAT('%', ?, '%')");
$stmnt->bind_param("s", $search);
$stmnt->execute();
$results = $stmnt->get_result();
echo "<div class='row-flex'>";
if(!$results) {
echo "<div class='frame' style='width: 500px;'>";
echo "<div class='container'>";
$error = getErrorString("Query unsuccessful", $connection->errno);
$stmnt->close();
$connection->close();
echo "</div>";
echo "</div>";
die($error);
}
$rows = $results->num_rows;
if($rows > 0) {
while($row = $results->fetch_assoc()) {
$dirName = $row['dirName'];
$dirCover = $row['dirCover'];
$dirId = $row['dirId'];
// Check if the dirId is in favourites
$isFav = false;
$favStmnt = $connection->prepare("SELECT * FROM favourites WHERE dirId=(?)");
$favStmnt->bind_param("i", $dirId);
$favStmnt->execute();
$favs = $favStmnt->get_result();
if($favs->num_rows > 0) {
$isFav = true;
}
$favs->close();
$favStmnt->close();
// Check if the dirId is in bookmarks
$isBmk = false;
$bmkStmnt = $connection->prepare("SELECT * FROM bookmarks WHERE dirId=(?)");
$bmkStmnt->bind_param("i", $dirId);
$bmkStmnt->execute();
$bmks = $bmkStmnt->get_result();
if($bmks->num_rows > 0) {
$isBmk = true;
}
$bmks->close();
$bmkStmnt->close();
$coverPath = $relative_directory . $dirName . "/" . $dirCover;
if(!$dir_exists) {
$coverPath = "";
}
$urlData = array(
'dir' => $dirId,
);
$url = "/info.php?" . http_build_query($urlData);
$dirName = htmlspecialchars($dirName, ENT_QUOTES);
$div = <<<EOF
<div class="img-card">
<a id='$dirId-fav' class='favourite no'><i class='far fa-heart'></i><i class='fas fa-heart'></i></a>
<a id='$dirId-bmk' class='bookmark bmk-no'><i class='far fa-bookmark'></i><i class='fas fa-bookmark'></i></a>
<a href='$url'>
<div class="img-container">
<img src="$coverPath">
</div>
<div class="img-data">
<p class="img-data-text">$dirName</p>
</div>
</a>
</div>
EOF;
if($isFav) {
$div = str_replace("favourite no", "favourite yes", $div);
}
if($isBmk) {
$div = str_replace("bookmark bmk-no", "bookmark bmk-yes", $div);
}
echo $div;
}
}
else {
echo "<div class='frame' style='width: 500px;'>";
echo "<div class='container'>";
$error = getErrorString("No results for '$search'", 0);
echo $error;
echo "</div>";
echo "</div>";
}
$stmnt->close();
$connection->close();
echo "</div>";
// Closing main div
echo "</div>";