diff options
-rw-r--r-- | app/classes/Opml.php | 51 | ||||
-rw-r--r-- | app/classes/OpmlManager.php | 25 | ||||
-rw-r--r-- | 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<int, string> */ public $entries = array(); private $map = array( @@ -19,14 +25,18 @@ class Opml ); - public function parse($data) + /** + * @param string $data + * @return array<int, string> + */ + 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<string, string> $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<int, string> + */ + 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 = '<?xml version="1.0"?>'."\n"; - $out.= '<opml version="1.1">'."\n"; + $out.= '<opml version="2.0">'."\n"; $out.= '<head>'."\n"; $out.= '<title>'.htmlspecialchars($opml->getTitle()).'</title>'."\n"; - $out.= '<dateCreated>'.date('c').'</dateCreated>'."\n"; - $out.= '<dateModified>'.date('c').'</dateModified>'."\n"; + $out.= '<dateCreated>'.gmdate('c').'</dateCreated>'."\n"; + $out.= '<dateModified>'.gmdate('c').'</dateModified>'."\n"; + if ($opml->ownerName != '') { + $out.= '<ownerName>'.htmlspecialchars($opml->ownerName).'</ownerName>'."\n"; + } + if ($opml->ownerEmail != '') { + $out.= '<ownerEmail>'.htmlspecialchars($opml->ownerEmail).'</ownerEmail>'."\n"; + } + if ($opml->ownerId != '') { + $out.= '<ownerId>'.htmlspecialchars($opml->ownerId).'</ownerId>'."\n"; + } + $out.= '<docs>http://opml.org/spec2.opml</docs>'."\n"; + $out.= '</head>'."\n"; $out.= '<body>'."\n"; foreach ($opml->entries as $person) { - $out.= '<outline text="' . htmlspecialchars($person['name'], ENT_QUOTES) . '" htmlUrl="' . htmlspecialchars($person['website'], ENT_QUOTES) . '" xmlUrl="' . htmlspecialchars($person['feed'], ENT_QUOTES) . '" isDown="' . htmlspecialchars($person['isDown'] ?? '', ENT_QUOTES) . '"/>'."\n"; + $out .= sprintf( + '<outline text="%s" htmlUrl="%s" xmlUrl="%s" isDown="%s" />', + htmlspecialchars($person['name'], ENT_QUOTES), + htmlspecialchars($person['website'], ENT_QUOTES), + htmlspecialchars($person['feed'], ENT_QUOTES), + htmlspecialchars($person['isDown'] ?? '', ENT_QUOTES) + ) . "\n"; } $out.= '</body>'."\n"; $out.= '</opml>'; 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); } |