-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme.php
190 lines (149 loc) · 5.17 KB
/
theme.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
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
<?php
if (!class_exists('RecentMedia')) {
// Wrap functions in a class so they don't interfere with normal Tinyboard operations
class RecentMedia {
public static function build($action, $settings, $board) {
// Possible values for $action:
// - all (rebuild everything, initialization)
// - news (news has been updated)
// - boards (board list changed)
// - post (a post has been made)
// - post-thread (a thread has been made)
if ($action == 'news') {
return;
};
try {
$b = new RecentMedia();
$b->build_main($action, $settings);
}
catch (Exception $e) {
error_log("vichan_theme_recent_media: " . $e->getMessage());
};
}
public function get_template_html_path($config, $settings) {
// compute relative path from vichan-root/templates/.
$path = $settings['themedir'];
$pos = strrpos($path, '/templates/themes/');
if ($pos === false) {
return 'templates/themes/recent_media/' . $settings['template'];
};
$p = substr($path, $pos + strlen('/templates/'));
$p .= '/' . $settings['template'];
return $p;
}
static function explode_noempty($str) {
return array_filter(explode(' ', $str), function($x) {
return $x !== '';
});
}
static function join_path() {
$paths = array();
foreach (func_get_args() as $arg) {
if ($arg !== '') { $paths[] = $arg; }
}
return preg_replace('#/+#','/',join('/', $paths));
}
public function build_main($action, $settings) {
global $config;
try {
$this->excluded = self::explode_noempty($settings['exclude']);
$this->included = self::explode_noempty($settings['include']);
if ($action == 'all' || $action == 'post' || $action == 'post-thread' || $action == 'post-delete') {
$action = generation_strategy('sb_recent_media', array());
if ($action == 'delete') {
file_unlink($config['dir']['home'] . $settings['html']);
}
elseif ($action == 'rebuild') {
file_write($config['dir']['home'] . $settings['html'], $this->generate_html($settings));
};
};
}
catch (Exception $e) {
$outputpath = join_path($config['dir']['home'], $settings['html']);
file_write($outputpath, $e->getMessage());
};
}
// Build news page
public function generate_html($settings) {
global $config;
$query = '';
foreach (listBoards() as $board) {
$b_uri = $board['uri'];
if (0 !== count($this->included)) {
if (!in_array($b_uri, $this->included)) {
continue;
};
};
if (in_array($b_uri, $this->excluded)) {
continue;
};
$query .= sprintf("SELECT *, '%s' AS `board` FROM ``posts_%s`` ", $b_uri, $b_uri)
. "WHERE `files` LIKE '%\"type\":\"image\\\\\\\\\\\\/%' "
. " OR `files` LIKE '%\"type\":\"video\\\\\\\\\\\\/%' "
. "UNION ALL ";
};
if ($query == '') {
error(_("Can't build the RecentMedia theme, because there are no boards to be fetched. Check 'Included boards' and 'Excluded boards'"));
};
$query = preg_replace('/UNION ALL $/', 'ORDER BY `time` DESC LIMIT ' . (int)$settings['limit_media'], $query);
$query = query($query) or error(db_error());
$recent_media = $this->pick_media($query, $settings);
$template_path = $this->get_template_html_path($config, $settings);
return Element($template_path, Array(
'settings' => $settings,
'config' => $config,
'recent_media' => $recent_media,
));
}
public function pick_media($query, $settings) {
global $config;
$media_limit = (int)$settings['limit_media'];
$recent_media = Array();
while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
openBoard($post['board']);
if (! isset($post['files'])) {
continue;
};
$files = json_decode($post['files']);
if (is_null($files)) {
continue;
};
if ($settings['files_sort_asc'] !== '1') {
$files = array_reverse($files);
};
foreach ($files as $postfile) {
if ($postfile->file == 'deleted'
|| $postfile->thumb == 'file'
|| $postfile->thumb == 'spoiler') {
continue;
};
$pass = Array();
$pass['isvideo'] = str_starts_with($postfile->type, 'video');
$pass['link'] = self::join_path(
$config['root'],
$post['board'],
$config['dir']['res'],
link_for($post) . '#' . $post['id']);
$pass['srcimg'] = self::join_path(
$config['root'],
$post['board'],
$config['dir']['img'],
$postfile->file);
$pass['thumbimg'] = self::join_path(
$config['root'],
$post['board'],
$config['dir']['thumb'],
$postfile->thumb);
$pass['thumbwidth'] = $postfile->thumbwidth;
$pass['thumbheight'] = $postfile->thumbheight;
$recent_media[] = $pass;
if ($media_limit <= count($recent_media)) {
return $recent_media;
};
};
};
return $recent_media;
}
};
};
?>