-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender_board.php
More file actions
executable file
·191 lines (161 loc) · 4.41 KB
/
render_board.php
File metadata and controls
executable file
·191 lines (161 loc) · 4.41 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
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
<?php
enum BoardType: string {
case DateFile = 'date';
case ConfigFile = 'config';
case Naked = 'naked';
}
function isLocalhost($whitelist = ['127.0.0.1', '::1']) {
return in_array($_SERVER['REMOTE_ADDR'], $whitelist);
}
class Board
{
public string $title;
public string $path;
private string $dir_name;
public BoardType $type;
public int $date_unix;
public string $description;
public bool $visible = true;
public function __construct(string $path, string $dir_name)
{
$this->path = $path;
$this->dir_name = $dir_name;
$this->type = $this->getBoardType();
$this->date_unix = $this->getBoardDate();
$this->title = $this->getBoardTitle();
$this->description = $this->getBoardDescription();
if ($this->type == BoardType::ConfigFile) {
$this->visible = $this->getBoardVisibility();
}
}
private function getBoardType(): BoardType
{
$files = scandir($this->path);
foreach ($files as $f) {
if (is_file("$this->path/$f") && $f === "config.ini") {
return BoardType::ConfigFile;
}
}
foreach ($files as $f) {
if (is_file("$this->path/$f") && $f === "date") {
return BoardType::DateFile;
}
}
return BoardType::Naked;
}
private function getBoardDate(): int
{
switch ($this->type) {
case BoardType::ConfigFile:
$ini_array = parse_ini_file("$this->path/config.ini", false);
/* echo $ini_array['date']; */
$date = strtotime($ini_array['date']);
return $date;
case BoardType::DateFile:
$d_contents = file_get_contents("$this->path/date", true);
$date = strtotime($d_contents);
return $date;
case BoardType::Naked:
$date = strtotime(date ("F d Y H:i:s.", filemtime($this->path)));
/* echo implode(',', $date); */
return $date;
}
}
public function getCleanPath(): string
{
return '/' . $this->dir_name . '/';
}
private function getBoardTitle(): string
{
switch ($this->type)
{
case BoardType::ConfigFile:
$ini_array = parse_ini_file("$this->path/config.ini", false);
$title = $ini_array['title'];
return $title;
default:
return $this->dir_name;
}
}
private function getBoardDescription(): string
{
switch ($this->type)
{
case BoardType::ConfigFile:
$ini_array = parse_ini_file("$this->path/config.ini", false);
if (array_key_exists('description', $ini_array)) {
$description = $ini_array['description'];
return $description;
}
else {
return "";
}
default:
return "";
}
}
private function getBoardVisibility(): bool
{
$ini_array = parse_ini_file("$this->path/config.ini", false);
if (array_key_exists("visible", $ini_array)) {
$visible = $ini_array['visible'];
return $visible;
}
else
{
return true;
}
}
}
class BoardListingsRenderer
{
private string $directory;
private array $boards = [];
public function __construct(string $directory = "")
{
$this->directory = rtrim($directory, DIRECTORY_SEPARATOR);
}
// sorts the $boards by date
public function populateBoards(): void
{
$files = scandir($this->directory);
foreach ($files as $f) {
if (in_array($f, ['.', '..', '.git', 'static', '.nova'])) continue;
$full_path = $this->directory . DIRECTORY_SEPARATOR . $f;
if (is_dir($full_path)) {
$this->boards[] = new Board($full_path, $f);
}
}
usort($this->boards, fn($b, $a) => $b->date_unix <=> $a->date_unix);
}
public function render(): void
{
foreach ($this->boards as $board) {
if (!$board->visible) { continue; }
$formattedDate = strtolower(date('M j, Y', $board->date_unix));
if ($board->type == BoardType::Naked) {
echo "<li>
<a href=\"{$board->getCleanPath()}\">{$board->title}</a>
</li>";
} else {
if ($board->description != "") {
echo "<li>
<a href=\"{$board->getCleanPath()}\">{$board->title}</a>
<span class='desc'>{$board->description} </span>";
} else {
echo "<li>
<a href=\"{$board->getCleanPath()}\">{$board->title}</a>
<span class='desc'>{$formattedDate}</span>";
}
if (!$board->visible) {
echo "<span style='color: red;'> not visible!</span";
}
echo "</li>";
}
}
}
public function getBoards() {
return $this->boards;
}
}
?>