-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme.php
301 lines (235 loc) · 9.35 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/**
This is the StartBootstrap-Blog-Home theme.
This file contains the theme class of the StartBootstrap-Blog-Home theme.
@package urlaube\startbootstrap-blog-home
@version 0.5a5
@author Yahe <hello@yahe.sh>
@since 0.1a0
*/
// ===== DO NOT EDIT HERE =====
// prevent script from getting called directly
if (!defined("URLAUBE")) { die(""); }
class StartBootstrapBlogHome extends BaseSingleton implements Handler, Theme {
// CONSTANTS
const REGEX = "~^\/startbootstrap\-blog\-home\.css$~";
// INTERFACE FUNCTIONS
public static function getContent($metadata, &$pagecount) {
return null;
}
public static function getUri($metadata) {
return value(Main::class, ROOTURI)."startbootstrap-blog-home.css";
}
public static function parseUri($uri) {
$result = null;
$metadata = preparecontent(parseuri($uri, static::REGEX));
if ($metadata instanceof Content) {
$result = $metadata;
}
return $result;
}
// HELPER FUNCTIONS
protected static function configureHandler() {
Themes::preset(CSS, "");
Themes::preset("dark_color", "#666");
Themes::preset("light_color", "#ccc");
}
protected static function configureTheme() {
// individual theme configuration
Themes::preset("copyright_html", null);
// recommended theme configuration
Themes::preset(AUTHOR, static::getDefaultAuthor());
Themes::preset(CANONICAL, static::getDefaultCanonical());
Themes::preset(CHARSET, static::getDefaultCharset());
Themes::preset(COPYRIGHT, static::getDefaultCopyright());
Themes::preset(DESCRIPTION, static::getDefaultDescription());
Themes::preset(FAVICON, null);
Themes::preset(KEYWORDS, static::getDefaultKeywords());
Themes::preset(LANGUAGE, static::getDefaultLanguage());
Themes::preset(LOGO, null);
Themes::preset(MENU, null);
Themes::preset(PAGENAME, static::getDefaultPagename());
Themes::preset(SITENAME, t("Deine Webseite", static::class));
Themes::preset(SITESLOGAN, t("Dein Slogan", static::class));
Themes::preset(TIMEFORMAT, "d.m.Y");
Themes::preset(TITLE, static::getDefaultTitle());
}
protected static function getDefaultAuthor() {
$result = null;
// try to retrieve the first author
foreach (value(Main::class, CONTENT) as $content_item) {
if ($content_item->isset(AUTHOR)) {
$result = value($content_item, AUTHOR);
break;
}
}
return $result;
}
protected static function getDefaultCanonical() {
$result = null;
// do not set a canonical URI on error
if (ErrorHandler::class !== Handlers::getActive()) {
$result = value(Main::class, URI);
}
return $result;
}
protected static function getDefaultCharset() {
return strtolower(value(Main::class, CHARSET));
}
protected static function getDefaultCopyright() {
return "Copyright ©".SP.value(Themes::class, SITENAME).SP.date("Y");
}
protected static function getDefaultDescription() {
$result = null;
// get the first entry of the content entries
if (0 < count(value(Main::class, CONTENT))) {
if (value(Main::class, CONTENT)[0]->isset(CONTENT)) {
// remove all HTML tags and replace line breaks with spaces
$result = substr(strtr(strip_tags(value(value(Main::class, CONTENT)[0], CONTENT)),
["\r\n" => SP, "\n" => SP, "\r" => SP]),
0, 300);
}
}
return $result;
}
protected static function getDefaultKeywords() {
$result = null;
// retrieve all words from the titles
$words = [];
foreach (value(Main::class, CONTENT) as $content_item) {
if ($content_item->isset(TITLE)) {
$words = array_merge($words, explode(SP, value($content_item, TITLE)));
}
}
// filter all words that do not fit the scheme
for ($index = count($words)-1; $index >= 0; $index--) {
if (1 !== preg_match("~^[0-9A-Za-z\-]+$~", $words[$index])) {
unset($words[$index]);
}
}
$result = implode(DP.SP, $words);
return $result;
}
protected static function getDefaultLanguage() {
$result = strtolower(value(Main::class, LANGUAGE));
// only take the first part if the language is of the form "ab_xy"
if (1 === preg_match("~^([a-z]+)\_[a-z]+$~", $result, $matches)) {
if (2 === count($matches)) {
$result = $matches[1];
}
}
return $result;
}
protected static function getDefaultPagename() {
$result = null;
// convert the METADATA to a pagename
$metadata = value(Main::class, METADATA);
if ($metadata instanceof Content) {
switch (Handlers::getActive()) {
case ArchiveHandler::class:
if ((null !== value($metadata, ArchiveHandler::DAY)) ||
(null !== value($metadata, ArchiveHandler::MONTH)) ||
(null !== value($metadata, ArchiveHandler::YEAR))) {
$result = t("Archiv", StartBootstrapBlogHome::class).COL.SP;
$parts = [];
if (null !== value($metadata, ArchiveHandler::DAY)) {
$parts[] .= t("Tag", StartBootstrapBlogHome::class).SP.value($metadata, ArchiveHandler::DAY);
}
if (null !== value($metadata, ArchiveHandler::MONTH)) {
$parts[] .= t("Monat", StartBootstrapBlogHome::class).SP.value($metadata, ArchiveHandler::MONTH);
}
if (null !== value($metadata, ArchiveHandler::YEAR)) {
$parts[] .= t("Jahr", StartBootstrapBlogHome::class).SP.value($metadata, ArchiveHandler::YEAR);
}
$result .= implode(DP.SP, $parts);
}
break;
case AuthorHandler::class:
$result = t("Autor", StartBootstrapBlogHome::class).COL.SP.str_replace("_", SP, value($metadata, AUTHOR));
break;
case CategoryHandler::class:
$result = t("Kategorie", StartBootstrapBlogHome::class).COL.SP.str_replace("_", SP, value($metadata, CATEGORY));
break;
case SearchHandler::class:
$result = t("Suche", StartBootstrapBlogHome::class).COL.SP.strtr(value($metadata, SEARCH), DOT, SP);
break;
}
}
return $result;
}
protected static function getDefaultTitle() {
$result = value(Themes::class, SITENAME).SP."-".SP.value(Themes::class, SITESLOGAN);
if (null !== value(Themes::class, PAGENAME)) {
$result = value(Themes::class, PAGENAME).SP."|".SP.$result;
} else {
// handle errors and pages
if ((ErrorHandler::class === Handlers::getActive()) ||
(PageHandler::class === Handlers::getActive())) {
// get the first entry of the content entries
if (0 < count(value(Main::class, CONTENT))) {
if (value(Main::class, CONTENT)[0]->isset(TITLE)) {
$result = value(value(Main::class, CONTENT)[0], TITLE).SP."|".SP.$result;
}
}
}
}
return $result;
}
// RUNTIME FUNCTIONS
public static function handler() {
$result = false;
// only proceed when this is the active theme
if (0 === strcasecmp(static::class, value(Main::class, THEMENAME))) {
$metadata = static::parseUri(relativeuri());
if (null !== $metadata) {
// check if the URI is correct
$fixed = static::getUri($metadata);
if (0 !== strcmp(value(Main::class, URI), $fixed)) {
relocate($fixed.querystring(), false, true);
} else {
// preset handler configuration
static::configureHandler();
// generate the CSS file output
require_once(__DIR__.DS."startbootstrap-blog-home.css.php");
}
// we handled this page
$result = true;
}
}
return $result;
}
public static function theme() {
$result = false;
// we do not handle empty content
$content = preparecontent(value(Main::class, CONTENT));
if (null !== $content) {
// make sure that we only handle arrays
if ($content instanceof Content) {
Main::set(CONTENT, [$content]);
}
// preset theme configuration
static::configureTheme();
// generate the head output
Plugins::run(BEFORE_HEAD);
require_once(__DIR__.DS."head.php");
Plugins::run(AFTER_HEAD);
// generate the body output
Plugins::run(BEFORE_BODY);
require_once(__DIR__.DS."body.php");
Plugins::run(AFTER_BODY);
// generate the footer output
Plugins::run(BEFORE_FOOTER);
require_once(__DIR__.DS."footer.php");
Plugins::run(AFTER_FOOTER);
// we handled this page
$result = true;
}
return $result;
}
}
// register handler
Handlers::register(StartBootstrapBlogHome::class, "handler", StartBootstrapBlogHome::REGEX, [GET], ADDSLASH);
// register theme
Themes::register(StartBootstrapBlogHome::class, "theme", StartBootstrapBlogHome::class);
// register translation
Translate::register(__DIR__.DS."lang".DS, StartBootstrapBlogHome::class);