koblog/bl-modules/atom/plugin.php
2025-07-03 21:54:56 +02:00

125 lines
3 KiB
PHP

<?php
class pluginAtom extends Plugin
{
private function encodeURL($url)
{
return preg_replace_callback('/[^\x20-\x7f]/', function ($match) {
return urlencode($match[0]);
}, $url);
}
private function createXML()
{
global $site;
global $pages;
global $url;
// Amount of pages to show
$numberOfItems = $site->itemsPerPage();
// 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
);
$xml = '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= '<feed xmlns="http://www.w3.org/2005/Atom">';
$xml .= '<title>' . $site->title() . '</title>';
$xml .= '<subtitle>' . $site->description() . '</subtitle>';
$xml .= '<link href="' . $this->encodeURL($site->url()) . '" />';
$xml .= '<updated>' . date(DATE_RSS) . '</updated>';
// Get keys of pages
foreach ($list as $pageKey) {
try {
// Create the page object from the page key
$page = new Page($pageKey);
$imagepath = parse_url($page->coverImage(true), PHP_URL_PATH);
$imagepath = $_SERVER['DOCUMENT_ROOT'] . $imagepath;
$xml .= '<entry>';
$xml .= '<title>' . $page->title() . '</title>';
$xml .= '<link rel="alternate" href="' . $this->encodeURL($page->permalink()) . '" />';
$xml .= '<link rel="enclosure" url="'.$page->coverImage(true).'" length="'.filesize($imagepath).'" type="'.mime_content_type($imagepath).'" />';
$xml .= '<summary>'.strip_tags( $page->contentBreak() ).'</summary>';
$xml .= '<updated>' . date(DATE_RSS, strtotime($page->getValue('dateRaw'))) . '</updated>';
$xml .= '<id>urn:uuid:' . $page->uuid() . '</id>';
$xml .= '<author>';
$xml .= '<name>'.$page->user('nickname').'</name>';
$xml .= '</author>';
$xml .= '</entry>';
} catch (Exception $e) {
// Continue
}
}
$xml .= '</feed>';
// New DOM document
$doc = new DOMDocument();
$doc->formatOutput = true;
$doc->loadXML($xml);
return $doc->save($this->workspace() . 'atom.xml');
}
public function install($position = 0)
{
parent::install($position);
return $this->createXML();
}
public function post()
{
parent::post();
return $this->createXML();
}
public function afterPageCreate()
{
$this->createXML();
}
public function afterPageModify()
{
$this->createXML();
}
public function afterPageDelete()
{
$this->createXML();
}
public function siteHead()
{
return '<link rel="alternate" type="application/atom+xml" href="' . DOMAIN_BASE . 'atom.xml" title="Atom Feed">' . PHP_EOL;
}
public function beforeAll()
{
$webhook = 'atom.xml';
if ($this->webhook($webhook)) {
// Send XML header
header('Content-type: text/xml');
$doc = new DOMDocument();
// Load XML
libxml_disable_entity_loader(false);
$doc->load($this->workspace() . 'atom.xml');
libxml_disable_entity_loader(true);
// Print the XML
echo $doc->saveXML();
// Stop Koblog execution
exit(0);
}
}
}