-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
136 lines (117 loc) · 3.68 KB
/
index.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
<?php
require_once "vendor/autoload.php";
use Twig\Loader\FilesystemLoader;
use Twig\Environment;
$loader = new FilesystemLoader(["templates"]);
$twig = new Environment($loader);
$urlData = array_filter(explode("/", substr(explode("?", $_SERVER["REQUEST_URI"])[0], 1)));
$target = count($urlData) > 0 ? strtolower($urlData[0]) : null;
$value = count($urlData) > 0 ? $urlData[1] : null;
if ($value == NULL) $target = NULL; // We can't have a target without a value
$metadata = json_decode(file_get_contents("data/info.json"), true);
$tags = json_decode(file_get_contents("data/tags.json"), true);
$json = isset($_GET["json"]) && $_GET["json"] === "1";
if ($json) {
header('Content-Type: application/json; charset=utf-8');
}
function showAll($twig, $metadata)
{
$images = [];
foreach (array_reverse($metadata) as $info)
{
array_push($images, [ "id" => $info["id"], "format" => $info["format"]]);
}
if (isset($_GET["json"]) && $_GET["json"] === "1") {
echo json_encode($images);
} else {
echo $twig->render("index.html.twig", [
"css" => "index",
"images" => $images
]);
}
}
function getTagCount($tag, $tags) {
return [ "name" => $tag, "count" => count($tags[$tag]["images"]) ];
}
if ($target === "i")
{
$isOk = false;
foreach ($metadata as $info)
{
if ($value === $info["id"])
{
$tags = [
"authors" => [
getTagCount("author_" . $info["author"], $tags),
],
"names" => array_map(function($tag) use (&$tags) { return getTagCount("name_" . $tag, $tags); }, $info["tags"]["characters"]),
"parodies" => array_map(function($tag) use (&$tags) { return getTagCount($tag, $tags); }, $info["tags"]["parodies"]),
"others" => array_map(function($tag) use (&$tags) { return getTagCount($tag, $tags); }, $info["tags"]["others"])
];
$info["tags_cleaned"] = $tags;
unset($info["tags"]);
unset($info["author"]);
if ($json) {
echo json_encode($info);
} else {
echo $twig->render("page.html.twig", [
"css" => "page",
"metadata" => $info,
"isimage" => true
]);
}
$isOk = true;
break;
}
}
if (!$isOk)
{
showAll($twig, $metadata);
}
}
else if ($target === "t")
{
$isOk = false;
foreach ($tags as $key => $info) {
if ($value === $key) {
// For all ids, correct the image field to include the extension
for ($i = 0; $i < count($info["images"]); $i++)
{
$img = $info["images"][$i];
foreach ($metadata as $m)
{
if ($img == $m["id"])
{
$info["images"][$i] = [
"id" => $img,
"format" => $m["format"]
];
break;
}
}
}
if ($json) {
echo json_encode([
"name" => $key,
"tag" => $info
]);
} else {
echo $twig->render("tag.html.twig", [
"css" => "tag",
"name" => $key,
"tag" => $info
]);
}
$isOk = true;
break;
}
}
if (!$isOk)
{
showAll($twig, $metadata);
}
}
else
{
showAll($twig, $metadata);
}