From 0b24df121ce7a2c3729103e7210ec970f5be82b8 Mon Sep 17 00:00:00 2001 From: Romain d'Alverny Date: Tue, 11 Jan 2022 18:44:50 +0100 Subject: Upgrade OPML format to v2 --- app/classes/Opml.php | 51 ++++++++++++++++++++++++++++++++++----------- app/classes/OpmlManager.php | 25 ++++++++++++++++++---- app/classes/Planet.php | 2 +- 3 files changed, 61 insertions(+), 17 deletions(-) diff --git a/app/classes/Opml.php b/app/classes/Opml.php index a9de2b0..c5f185f 100644 --- a/app/classes/Opml.php +++ b/app/classes/Opml.php @@ -3,9 +3,15 @@ class Opml { private $_xml = null; - private $_currentTag = ''; + private string $_currentTag = ''; - public $title = ''; + public string $ownerName = ''; + public string $ownerEmail = ''; + public string $ownerId = ''; + + public string $title = ''; + + /** @var array */ public $entries = array(); private $map = array( @@ -19,14 +25,18 @@ class Opml ); - public function parse($data) + /** + * @param string $data + * @return array + */ + public function parse(string $data) : array { $this->_xml = xml_parser_create('UTF-8'); //xml_parser_set_option($this->_xml, XML_OPTION_CASE_FOLDING, false); //xml_parser_set_option($this->_xml, XML_OPTION_SKIP_WHITE, true); xml_set_object($this->_xml, $this); - xml_set_element_handler($this->_xml, '_openTag', '_closeTag'); - xml_set_character_data_handler($this->_xml, '_cData'); + xml_set_element_handler($this->_xml, [$this, '_openTag'], [$this, '_closeTag']); + xml_set_character_data_handler($this->_xml, [$this, '_cData']); xml_parse($this->_xml, $data); xml_parser_free($this->_xml); @@ -34,7 +44,10 @@ class Opml } - private function _openTag($p, $tag, $attrs) + /** + * @param array $attrs + */ + private function _openTag($p, string $tag, array $attrs) : void { $this->_currentTag = $tag; @@ -48,24 +61,38 @@ class Opml } } - private function _closeTag($p, $tag) + private function _closeTag($p, string $tag) : void { $this->_currentTag = ''; } - private function _cData($p, $cdata) + private function _cData($p, string $cdata) : void { - if ($this->_currentTag == 'TITLE') { - $this->title = $cdata; + switch ($this->_currentTag) { + case 'TITLE': + $this->title = $cdata; + break; + case 'OWNERNAME': + $this->ownerName = $cdata; + break; + case 'OWNEREMAIL': + $this->ownerEmail = $cdata; + break; + case 'OWNERID': + $this->ownerId = $cdata; + break; } } - public function getTitle() + public function getTitle() : string { return $this->title; } - public function getPeople() + /** + * @return array + */ + public function getPeople() : array { return $this->entries; } diff --git a/app/classes/OpmlManager.php b/app/classes/OpmlManager.php index fe1adda..cd3d685 100644 --- a/app/classes/OpmlManager.php +++ b/app/classes/OpmlManager.php @@ -29,15 +29,32 @@ class OpmlManager public static function save($opml, $file) { $out = ''."\n"; - $out.= ''."\n"; + $out.= ''."\n"; $out.= ''."\n"; $out.= ''.htmlspecialchars($opml->getTitle()).''."\n"; - $out.= ''.date('c').''."\n"; - $out.= ''.date('c').''."\n"; + $out.= ''.gmdate('c').''."\n"; + $out.= ''.gmdate('c').''."\n"; + if ($opml->ownerName != '') { + $out.= ''.htmlspecialchars($opml->ownerName).''."\n"; + } + if ($opml->ownerEmail != '') { + $out.= ''.htmlspecialchars($opml->ownerEmail).''."\n"; + } + if ($opml->ownerId != '') { + $out.= ''.htmlspecialchars($opml->ownerId).''."\n"; + } + $out.= 'http://opml.org/spec2.opml'."\n"; + $out.= ''."\n"; $out.= ''."\n"; foreach ($opml->entries as $person) { - $out.= ''."\n"; + $out .= sprintf( + '', + htmlspecialchars($person['name'], ENT_QUOTES), + htmlspecialchars($person['website'], ENT_QUOTES), + htmlspecialchars($person['feed'], ENT_QUOTES), + htmlspecialchars($person['isDown'] ?? '', ENT_QUOTES) + ) . "\n"; } $out.= ''."\n"; $out.= ''; diff --git a/app/classes/Planet.php b/app/classes/Planet.php index dc540bb..d3e6149 100644 --- a/app/classes/Planet.php +++ b/app/classes/Planet.php @@ -128,7 +128,7 @@ class Planet $opml_person['name'], $opml_person['feed'], $opml_person['website'], - $opml_person['isDown'] + $opml_person['isDown'] ?? 0 ); $this->addPerson($person); } -- cgit v1.2.1