1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
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 | } |
---|
23 | elseif (preg_match('!<opml(\s+version)?!', $data)) { |
---|
24 | $this->_parseOPML($data); |
---|
25 | } |
---|
26 | else { |
---|
27 | throw new Exception(__('You need to provide a XBEL or OPML file.')); |
---|
28 | } |
---|
29 | } |
---|
30 | |
---|
31 | |
---|
32 | protected function _parseOPML($data) |
---|
33 | { |
---|
34 | $xml = @simplexml_load_string($data); |
---|
35 | if (!$xml) throw new Exception(__('File is not in XML format.')); |
---|
36 | |
---|
37 | $outlines = $xml->xpath("//outline"); |
---|
38 | |
---|
39 | $this->entries = array(); |
---|
40 | foreach ($outlines as $outline) { |
---|
41 | if (isset($outline['htmlUrl'])) { |
---|
42 | $link = $outline['htmlUrl']; |
---|
43 | } |
---|
44 | elseif (isset($outline['url'])) { |
---|
45 | $link = $outline['url']; |
---|
46 | } |
---|
47 | else continue; |
---|
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) throw new Exception(__('File is not in XML format.')); |
---|
63 | |
---|
64 | $outlines = $xml->xpath("//bookmark"); |
---|
65 | |
---|
66 | $this->entries = array(); |
---|
67 | foreach ($outlines as $outline) { |
---|
68 | if (!isset($outline['href'])) continue; |
---|
69 | $entry = new StdClass(); |
---|
70 | $entry->link = $outline['href']; |
---|
71 | $entry->title = (!empty($outline->title))?$outline->title:''; |
---|
72 | if (empty($entry->title)) { |
---|
73 | $entry->title = $entry->link; |
---|
74 | } |
---|
75 | $entry->desc = (!empty($outline->desc))?$outline->desc:''; |
---|
76 | $this->entries[] = $entry; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | public function getAll() |
---|
82 | { |
---|
83 | if (!$this->entries) return null; |
---|
84 | return $this->entries; |
---|
85 | } |
---|
86 | |
---|
87 | } |
---|
88 | |
---|
89 | class dcImportBlogroll { |
---|
90 | |
---|
91 | public static function loadFile($file) |
---|
92 | { |
---|
93 | if (file_exists($file) && is_readable($file)) { |
---|
94 | $importer = new linksImporter(); |
---|
95 | $importer->parse(file_get_contents($file)); |
---|
96 | return $importer->getAll(); |
---|
97 | } |
---|
98 | return false; |
---|
99 | } |
---|
100 | } |
---|