koblog/bl-plugins/rss/plugin.php

131 lines
3.5 KiB
PHP
Raw Normal View History

2016-01-11 23:51:00 +01:00
<?php
class pluginRSS extends Plugin {
2021-09-25 20:28:17 +02:00
public function init() {
$this->dbFields = array(
2018-08-06 21:46:58 +02:00
'numberOfItems'=>5
);
}
2021-09-25 20:28:17 +02:00
public function form() {
global $L;
2021-09-25 20:28:17 +02:00
$html = '<div class="mb-3">';
$html .= '<label class="form-label" for="label">'.$L->get('RSS URL').'</label>';
$html .= '<a href="'.DOMAIN_BASE.'rss.xml">'.DOMAIN_BASE.'rss.xml</a>';
$html .= '</div>';
2018-07-02 00:24:53 +02:00
2021-09-25 20:28:17 +02:00
$html = '<div class="mb-3">';
$html .= '<label class="form-label" for="numberOfItems">'.$L->get('Number of items').'</label>';
$html .= '<input class="form-control" id="numberOfItems" name="numberOfItems" type="text" value="'.$this->getValue('numberOfItems').'">';
$html .= '<div class="form-text">'.$L->get('The number of items to display in the feed').'</div>';
$html .= '</div>';
return $html;
}
2021-03-19 20:48:04 +01:00
2021-09-25 20:28:17 +02:00
private function encodeURL($url) {
return preg_replace_callback('/[^\x20-\x7f]/', function($match) { return urlencode($match[0]); }, $url);
}
2021-09-25 20:28:17 +02:00
private function createXML() {
global $site;
2018-08-03 18:59:23 +02:00
global $pages;
global $url;
2016-01-11 23:51:00 +01:00
2021-09-25 20:28:17 +02:00
// Number of pages to show
2018-08-06 21:46:58 +02:00
$numberOfItems = $this->getValue('numberOfItems');
// Get the list of public pages (sticky and static included)
$list = $pages->getList(
$pageNumber=1,
$numberOfItems,
$published=true,
$static=true,
$sticky=true,
$draft=false,
$scheduled=false
);
2016-01-11 23:51:00 +01:00
$xml = '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';
2016-01-11 23:51:00 +01:00
$xml .= '<channel>';
$xml .= '<atom:link href="'.DOMAIN_BASE.'rss.xml" rel="self" type="application/rss+xml" />';
$xml .= '<title>'.$site->title().'</title>';
2019-09-19 11:52:28 +02:00
$xml .= '<link>'.$this->encodeURL($site->url()).'</link>';
$xml .= '<description>'.$site->description().'</description>';
2019-02-12 08:13:18 +01:00
$xml .= '<lastBuildDate>'.date(DATE_RSS).'</lastBuildDate>';
2016-01-11 23:51:00 +01:00
// Get keys of pages
2018-09-05 22:55:14 +02:00
foreach ($list as $pageKey) {
try {
// Create the page object from the page key
2018-08-02 17:06:53 +02:00
$page = new Page($pageKey);
$xml .= '<item>';
$xml .= '<title>'.$page->title().'</title>';
2019-09-19 11:52:28 +02:00
$xml .= '<link>'.$this->encodeURL($page->permalink()).'</link>';
if ($page->coverImage()) {
$xml .= '<image>'.$page->coverImage().'</image>';
}
$xml .= '<description>'.Sanitize::html($page->contentBreak()).'</description>';
2019-09-16 11:57:51 +02:00
$xml .= '<pubDate>'.date(DATE_RSS,strtotime($page->getValue('dateRaw'))).'</pubDate>';
$xml .= '<guid isPermaLink="false">'.$page->uuid().'</guid>';
$xml .= '</item>';
} catch (Exception $e) {
// Continue
}
2016-01-11 23:51:00 +01:00
}
$xml .= '</channel></rss>';
// New DOM document
$doc = new DOMDocument();
$doc->formatOutput = true;
$doc->loadXML($xml);
2018-09-05 22:55:14 +02:00
return $doc->save($this->workspace().'rss.xml');
2016-01-11 23:51:00 +01:00
}
2021-09-25 20:28:17 +02:00
public function install($position=0) {
2016-01-11 23:51:00 +01:00
parent::install($position);
2018-09-05 22:55:14 +02:00
return $this->createXML();
2016-01-11 23:51:00 +01:00
}
2021-09-25 20:28:17 +02:00
public function afterPageCreate() {
2016-01-11 23:51:00 +01:00
$this->createXML();
}
2021-09-25 20:28:17 +02:00
public function afterPageModify() {
2016-01-11 23:51:00 +01:00
$this->createXML();
}
2021-09-25 20:28:17 +02:00
public function afterPageDelete() {
2016-01-11 23:51:00 +01:00
$this->createXML();
}
2021-09-25 20:28:17 +02:00
public function siteHead() {
return '<link rel="alternate" type="application/rss+xml" href="'.DOMAIN_BASE.'rss.xml" title="RSS Feed">'.PHP_EOL;
2016-01-17 23:05:46 +01:00
}
2021-09-25 20:28:17 +02:00
public function beforeAll() {
2017-07-29 01:20:47 +02:00
$webhook = 'rss.xml';
2018-07-02 00:24:53 +02:00
if ($this->webhook($webhook)) {
2016-01-11 23:51:00 +01:00
// Send XML header
header('Content-type: text/xml');
$doc = new DOMDocument();
// Load XML
libxml_disable_entity_loader(false);
$doc->load($this->workspace().'rss.xml');
libxml_disable_entity_loader(true);
2016-01-11 23:51:00 +01:00
// Print the XML
echo $doc->saveXML();
2018-07-02 00:24:53 +02:00
// Stop Bludit execution
exit(0);
2016-01-11 23:51:00 +01:00
}
}
2021-09-25 20:28:17 +02:00
2019-02-12 08:13:18 +01:00
}