| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * @brief blogroll, a plugin for Dotclear 2 |
|---|
| 4 | * |
|---|
| 5 | * @package Dotclear |
|---|
| 6 | * @subpackage Plugins |
|---|
| 7 | * |
|---|
| 8 | * @copyright Olivier Meunier & Association Dotclear |
|---|
| 9 | * @copyright GPL-2.0-only |
|---|
| 10 | */ |
|---|
| 11 | |
|---|
| 12 | if (!defined('DC_RC_PATH')) {return;} |
|---|
| 13 | |
|---|
| 14 | class linksImporter |
|---|
| 15 | { |
|---|
| 16 | protected $entries = null; |
|---|
| 17 | |
|---|
| 18 | public function parse($data) |
|---|
| 19 | { |
|---|
| 20 | if (preg_match('!<xbel(\s+version)?!', $data)) { |
|---|
| 21 | $this->_parseXBEL($data); |
|---|
| 22 | } elseif (preg_match('!<opml(\s+version)?!', $data)) { |
|---|
| 23 | $this->_parseOPML($data); |
|---|
| 24 | } else { |
|---|
| 25 | throw new Exception(__('You need to provide a XBEL or OPML file.')); |
|---|
| 26 | } |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | protected function _parseOPML($data) |
|---|
| 30 | { |
|---|
| 31 | $xml = @simplexml_load_string($data); |
|---|
| 32 | if (!$xml) { |
|---|
| 33 | throw new Exception(__('File is not in XML format.')); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | $outlines = $xml->xpath("//outline"); |
|---|
| 37 | |
|---|
| 38 | $this->entries = []; |
|---|
| 39 | foreach ($outlines as $outline) { |
|---|
| 40 | if (isset($outline['htmlUrl'])) { |
|---|
| 41 | $link = $outline['htmlUrl']; |
|---|
| 42 | } elseif (isset($outline['url'])) { |
|---|
| 43 | $link = $outline['url']; |
|---|
| 44 | } else { |
|---|
| 45 | continue; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | $entry = new StdClass(); |
|---|
| 49 | $entry->link = $link; |
|---|
| 50 | $entry->title = (!empty($outline['title'])) ? $outline['title'] : ''; |
|---|
| 51 | if (empty($entry->title)) { |
|---|
| 52 | $entry->title = (!empty($outline['text'])) ? $outline['text'] : $entry->link; |
|---|
| 53 | } |
|---|
| 54 | $entry->desc = (!empty($outline['description'])) ? $outline['description'] : ''; |
|---|
| 55 | $this->entries[] = $entry; |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | protected function _parseXBEL($data) |
|---|
| 60 | { |
|---|
| 61 | $xml = @simplexml_load_string($data); |
|---|
| 62 | if (!$xml) { |
|---|
| 63 | throw new Exception(__('File is not in XML format.')); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | $outlines = $xml->xpath("//bookmark"); |
|---|
| 67 | |
|---|
| 68 | $this->entries = []; |
|---|
| 69 | foreach ($outlines as $outline) { |
|---|
| 70 | if (!isset($outline['href'])) { |
|---|
| 71 | continue; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | $entry = new StdClass(); |
|---|
| 75 | $entry->link = $outline['href']; |
|---|
| 76 | $entry->title = (!empty($outline->title)) ? $outline->title : ''; |
|---|
| 77 | if (empty($entry->title)) { |
|---|
| 78 | $entry->title = $entry->link; |
|---|
| 79 | } |
|---|
| 80 | $entry->desc = (!empty($outline->desc)) ? $outline->desc : ''; |
|---|
| 81 | $this->entries[] = $entry; |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | public function getAll() |
|---|
| 86 | { |
|---|
| 87 | if (!$this->entries) { |
|---|
| 88 | return; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | return $this->entries; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | class dcImportBlogroll |
|---|
| 97 | { |
|---|
| 98 | |
|---|
| 99 | public static function loadFile($file) |
|---|
| 100 | { |
|---|
| 101 | if (file_exists($file) && is_readable($file)) { |
|---|
| 102 | $importer = new linksImporter(); |
|---|
| 103 | $importer->parse(file_get_contents($file)); |
|---|
| 104 | return $importer->getAll(); |
|---|
| 105 | } |
|---|
| 106 | return false; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|