-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
388 lines (311 loc) · 10.5 KB
/
plugin.php
File metadata and controls
388 lines (311 loc) · 10.5 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
class pluginAtomRSS extends Plugin {
// Begin - Initialize Database
public function init()
{
$this->dbFields = array(
'feedCopyright' => 'DISABLED',
'feedFileAtom' => 'feed.atom',
'feedFileRSS' => 'feed.rss',
'feedGenerator' => 'Bludit - Flat-File CMS',
'feedGeneratorEnable' => true,
'feedItemLimit' => 10,
'feedTTL' => 60
);
} // Cease - Initialize Database
// Begin - Plugin Settings Page
public function form()
{
// Language
global $L;
$feedCopyright = $this->getValue('feedCopyright');
$feedFileAtom = $this->getValue('feedFileAtom');
$feedFileRSS = $this->getValue('feedFileRSS');
$feedGeneratorEnable = $this->getValue('feedGeneratorEnable');
$feedItemLimit = $this->getValue('feedItemLimit');
$feedTTL = $this->getValue('feedTTL');
// Begin - HTML Template
$html = '<div class="alert alert-primary" role="alert">';
$html .= $this->description();
$html .= '</div>';
// Feed URL Atom
$html .= '<div>';
$html .= '<label>'.$L->get('feed-url-atom').'</label>';
$html .= '<a href="'.DOMAIN_BASE.$feedFileAtom.'">'.DOMAIN_BASE.$feedFileAtom.'</a>';
$html .= '<span class="tip">'.$L->get('feed-url-atom-tip').'</span>';
$html .= '</div>';
// Feed URL RSS
$html .= '<div>';
$html .= '<label>'.$L->get('feed-url-rss').'</label>';
$html .= '<a href="'.DOMAIN_BASE.$feedFileRSS.'">'.DOMAIN_BASE.$feedFileRSS.'</a>';
$html .= '<span class="tip">'.$L->get('feed-url-rss-tip').'</span>';
$html .= '</div>';
// Feed Item Limit
$html .= '<div>';
$html .= '<label>'.$L->get('feed-item-limit').'</label>';
// Reset to default if 0 is selected
if ($feedItemLimit === 0) {
$feedItemLimit = 10;
}
$html .= '<input id="jsfeedItemLimit" name="feedItemLimit" type="number" title="Valid input range: -1 - 100" min="-1" max="100" value="'.$feedItemLimit.'">';
$html .= '<span class="tip">'.$L->get('feed-item-limit-tip').'</span>';
$html .= '</div>';
// Feed Copyright
$html .= '<div>';
$html .= '<label>'.$L->get('feed-copyright').'</label>';
$html .= '<select id="jsfeedCopyright" name="feedCopyright">';
if ($feedCopyright !== 'DISABLED') {
$html .= '<option value="'.$feedCopyright.'">'.$feedCopyright.'</option>';
} else {
$html .= '<option value="DISABLED">Disabled</option>';
}
$html .= '<option value="CC BY 4.0">CC BY 4.0</option>';
$html .= '<option value="CC BY-SA 4.0">CC BY-SA 4.0</option>';
$html .= '<option value="CC BY-ND 4.0">CC BY-ND 4.0</option>';
$html .= '<option value="CC BY-NC 4.0">CC BY-NC 4.0</option>';
$html .= '<option value="CC BY-NC-SA 4.0">CC BY-NC-SA 4.0</option>';
$html .= '<option value="CC BY-NC-ND 4.0">CC BY-NC-ND 4.0</option>';
$html .= '<option value="DISABLED">Disabled</option>';
$html .= '</select>';
$html .= '<span class="tip">'.$L->get('feed-copyright-tip').' '.$L->get('Disabled').'</span>';
$html .= '</div>';
// Feed Generator
$html .= '<div>';
$html .= '<label>'.$L->get('feed-generator-tag').'</label>';
$html .= '<select name="feedGeneratorEnable">';
$html .= '<option value="true" '.($feedGeneratorEnable===true?'selected':'').'>'.$L->get('Enabled').'</option>';
$html .= '<option value="false" '.($feedGeneratorEnable===false?'selected':'').'>'.$L->get('Disabled').'</option>';
$html .= '</select>';
$html .= '<span class="tip">'.$L->get('feed-generator-tag-tip').' '.$L->get('Enabled').'</span>';
$html .= '</div>';
// Cease - HTML Template
// Output Template
return $html;
} // Cease - Plugin Settings Page
// Begin - Atom
private function createAtom()
{
global $site;
global $pages;
global $url;
$feedCopyright = $this->getValue('feedCopyright');
$feedDomain = parse_url(DOMAIN);
$feedFileAtom = $this->getValue('feedFileAtom');
$feedGenerator = $this->getValue('feedGenerator');
$feedGeneratorEnable = $this->getValue('feedGeneratorEnable');
$feedItemLimit = $this->getValue('feedItemLimit');
$feedSubtitle = $site->slogan();
$feedUUID = 'tag:'.$feedDomain['host'].',2019-03-31:1640';
// Reset to default if 0 is selected
if ($feedItemLimit === 0) {
$feedItemLimit = 10;
}
$list = $pages->getList(
$pageNumber = 1,
$feedItemLimit,
$published = true,
$static = true,
$sticky = true,
$draft = false,
$scheduled = false
);
// Begin - Atom Template
$atom = '<?xml version="1.0" encoding="utf-8" ?>';
$atom .= '<feed xmlns="http://www.w3.org/2005/Atom">';
$atom .= '<title>'.$site->title().'</title>';
if (!empty($feedSubtitle)) {
$atom .= '<subtitle>'.$site->slogan().'</subtitle>';
}
$atom .= '<id>'.$feedUUID.'</id>';
$atom .= '<updated>'.date(DATE_ATOM).'</updated>';
$atom .= '<link href="'.DOMAIN_BASE.$feedFileAtom.'" rel="self" type="application/atom+xml" />';
$atom .= '<link href="'.$site->domain().'" hreflang="'.Theme::lang().'" rel="alternate" type="text/html" />';
if ($feedCopyright !== 'DISABLED') {
$atom .= '<rights>'.$feedCopyright.'</rights>';
}
if ($feedGeneratorEnable === true) {
$atom .= '<generator>'.$feedGenerator.'</generator>';
}
foreach ($list as $pageKey) {
try {
// Create the page object from the page key
$page = new Page($pageKey);
$atom .= '<entry>';
$atom .= '<title>'.$page->title().'</title>';
$atom .= '<link href="'.$page->permalink().'" hreflang="'.Theme::lang().'" rel="alternate" />';
$atom .= '<id>'.$feedUUID.'.'.$page->uuid().'</id>';
$atom .= '<published>'.$page->date(DATE_ATOM).'</published>';
// Better way to do this?
if (!empty($page->dateModified())) {
$dm = $page->dateModified();
// Convert $dm to DATE_ATOM
$dmatom = Date::format($dm, DB_DATE_FORMAT, DATE_ATOM);
// Display DATE_ATOM
$atom .= '<updated>'.$dmatom.'</updated>';
} else {
$atom .= '<updated>'.$page->date(DATE_ATOM).'</updated>';
}
// Add category
if (!empty($page->category())) {
$atom .= '<category scheme="'.DOMAIN_BASE.'" term="'.$page->category().'" />';
}
$atom .= '<author><name>'.$page->username().'</name></author>';
$atom .= '<summary type="html">'.Sanitize::html($page->contentBreak()).'</summary>';
$atom .= '</entry>';
} catch (Exception $e) {
// Continue
}
}
$atom .= '</feed>';
// Cease - Atom Template
// Create Atom File
$doc = new DOMDocument();
$doc->formatOutput = true;
$doc->loadXML($atom);
// Save Atom File
return $doc->save($this->workspace().$feedFileAtom);
} // Cease - Atom
// Begin - RSS
private function createRSS()
{
global $site;
global $pages;
global $url;
$feedCopyright = $this->getValue('feedCopyright');
$feedFileRSS = $this->getValue('feedFileRSS');
$feedGenerator = $this->getValue('feedGenerator');
$feedGeneratorEnable = $this->getValue('feedGeneratorEnable');
$feedItemLimit = $this->getValue('feedItemLimit');
$feedTTL = $this->getValue('feedTTL');
// Reset to default if 0 is selected
if ($feedItemLimit === 0) {
$feedItemLimit = 10;
}
$list = $pages->getList(
$pageNumber = 1,
$feedItemLimit,
$published = true,
$static = true,
$sticky = true,
$draft = false,
$scheduled = false
);
// Begin - RSS Template
$rss = '<?xml version="1.0" encoding="UTF-8" ?>';
$rss .= '<rss version="2.0">';
$rss .= '<channel>';
$rss .= '<title>'.$site->title().'</title>';
$rss .= '<link>'.$site->url().'</link>';
$rss .= '<description>'.$site->description().'</description>';
$rss .= '<language>'.Theme::lang().'</language>';
if ($feedCopyright !== 'DISABLED') {
$rss .= '<copyright>'.$feedCopyright.'</copyright>';
}
$rss .= '<lastBuildDate>'.date(DATE_RSS).'</lastBuildDate>';
if ($feedGeneratorEnable === true) {
$rss .= '<generator>'.$feedGenerator.'</generator>';
}
if (!empty($feedTTL) && ($feedTTL !== 0)) {
$rss .= '<ttl>'.$feedTTL.'</ttl>';
}
foreach ($list as $pageKey) {
try {
$page = new Page($pageKey);
$rss .= '<item>';
$rss .= '<title>'.$page->title().'</title>';
$rss .= '<link>'.$page->permalink().'</link>';
$rss .= '<description>'.Sanitize::html($page->contentBreak()).'</description>';
if (!empty($page->category())) {
$rss .= '<category>'.$page->category().'</category>';
}
$rss .= '<pubDate>'.$page->date(DATE_RSS).'</pubDate>';
$rss .= '<guid isPermaLink="false">'.$page->uuid().'</guid>';
$rss .= '</item>';
} catch (Exception $e) {
// Continue
}
}
$rss .= '</channel>';
$rss .= '</rss>';
// Cease - RSS Template
// Create RSS File
$doc = new DOMDocument();
$doc->formatOutput = true;
$doc->loadXML($rss);
// Save RSS File
return $doc->save($this->workspace().$feedFileRSS);
} // Cease - RSS
public function install($position = 0)
{
parent::install($position);
$this->createAtom();
$this->createRSS();
return;
}
public function post()
{
parent::post();
$this->createAtom();
$this->createRSS();
return;
}
public function afterPageCreate()
{
$this->createAtom();
$this->createRSS();
}
public function afterPageModify()
{
$this->createAtom();
$this->createRSS();
}
public function afterPageDelete()
{
$this->createAtom();
$this->createRSS();
}
public function siteHead()
{
global $site;
$feedFileAtom = $this->getValue('feedFileAtom');
$feedFileRSS = $this->getValue('feedFileRSS');
// Add Atom Link
$addLink = '<link rel="alternate" type="application/atom+xml" href="'.DOMAIN_BASE.$feedFileAtom.'" title="'.$site->title().' - Atom Feed">'.PHP_EOL;
// Add RSS Link
$addLink .= '<link rel="alternate" type="application/rss+xml" href="'.DOMAIN_BASE.$feedFileRSS.'" title="'.$site->title().' - RSS Feed">'.PHP_EOL;
return $addLink;
}
public function beforeAll()
{
$feedFileAtom = $this->getValue('feedFileAtom');
$feedFileRSS = $this->getValue('feedFileRSS');
// Atom - http://example.com/feed.atom
if ($this->webhook($feedFileAtom)) {
// Send XML header
header('Content-type: application/atom+xml');
$doc = new DOMDocument();
// Load XML
libxml_disable_entity_loader(false);
$doc->load($this->workspace().$feedFileAtom);
libxml_disable_entity_loader(true);
// Print the XML
echo $doc->saveXML();
// Stop Bludit execution
exit(0);
}
// RSS - http://example.com/feed.rss
if ($this->webhook($feedFileRSS)) {
// Send XML header
header('Content-type: text/xml');
$doc = new DOMDocument();
// Load XML
libxml_disable_entity_loader(false);
$doc->load($this->workspace().$feedFileRSS);
libxml_disable_entity_loader(true);
// Print the XML
echo $doc->saveXML();
// Stop Bludit execution
exit(0);
}
}
}