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 | require dirname(__FILE__) . '/_widgets.php'; |
---|
15 | |
---|
16 | # Blogroll template functions |
---|
17 | $core->tpl->addValue('Blogroll', ['tplBlogroll', 'blogroll']); |
---|
18 | $core->tpl->addValue('BlogrollXbelLink', ['tplBlogroll', 'blogrollXbelLink']); |
---|
19 | |
---|
20 | $core->url->register('xbel', 'xbel', '^xbel(?:/?)$', ['urlBlogroll', 'xbel']); |
---|
21 | |
---|
22 | class tplBlogroll |
---|
23 | { |
---|
24 | public static function blogroll($attr) |
---|
25 | { |
---|
26 | $category = '<h3>%s</h3>'; |
---|
27 | $block = '<ul>%s</ul>'; |
---|
28 | $item = '<li%2$s>%1$s</li>'; |
---|
29 | |
---|
30 | if (isset($attr['category'])) { |
---|
31 | $category = addslashes($attr['category']); |
---|
32 | } |
---|
33 | |
---|
34 | if (isset($attr['block'])) { |
---|
35 | $block = addslashes($attr['block']); |
---|
36 | } |
---|
37 | |
---|
38 | if (isset($attr['item'])) { |
---|
39 | $item = addslashes($attr['item']); |
---|
40 | } |
---|
41 | |
---|
42 | $only_cat = 'null'; |
---|
43 | if (!empty($attr['only_category'])) { |
---|
44 | $only_cat = "'" . addslashes($attr['only_category']) . "'"; |
---|
45 | } |
---|
46 | |
---|
47 | return |
---|
48 | '<?php ' . |
---|
49 | "echo tplBlogroll::getList('" . $category . "','" . $block . "','" . $item . "'," . $only_cat . "); " . |
---|
50 | '?>'; |
---|
51 | } |
---|
52 | |
---|
53 | public static function blogrollXbelLink($attr) |
---|
54 | { |
---|
55 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
56 | return '<?php echo ' . sprintf($f, '$core->blog->url.$core->url->getURLFor("xbel")') . '; ?>'; |
---|
57 | } |
---|
58 | |
---|
59 | public static function getList($cat_title = '<h3>%s</h3>', $block = '<ul>%s</ul>', $item = '<li>%s</li>', $category = null) |
---|
60 | { |
---|
61 | $blogroll = new dcBlogroll($GLOBALS['core']->blog); |
---|
62 | |
---|
63 | try { |
---|
64 | $links = $blogroll->getLinks(); |
---|
65 | } catch (Exception $e) { |
---|
66 | return false; |
---|
67 | } |
---|
68 | |
---|
69 | $res = ''; |
---|
70 | |
---|
71 | $hierarchy = $blogroll->getLinksHierarchy($links); |
---|
72 | |
---|
73 | if ($category) { |
---|
74 | if (!isset($hierarchy[$category])) { |
---|
75 | return ''; |
---|
76 | } |
---|
77 | $hierarchy = [$hierarchy[$category]]; |
---|
78 | } |
---|
79 | |
---|
80 | foreach ($hierarchy as $k => $v) { |
---|
81 | if ($k != '') { |
---|
82 | $res .= sprintf($cat_title, html::escapeHTML($k)) . "\n"; |
---|
83 | } |
---|
84 | |
---|
85 | $res .= self::getLinksList($v, $block, $item); |
---|
86 | } |
---|
87 | |
---|
88 | return $res; |
---|
89 | } |
---|
90 | |
---|
91 | private static function getLinksList($links, $block = '<ul>%s</ul>', $item = '<li%2$s>%1$s</li>') |
---|
92 | { |
---|
93 | $list = ''; |
---|
94 | |
---|
95 | # Find current link item if any |
---|
96 | $current = -1; |
---|
97 | $current_size = 0; |
---|
98 | $self_uri = http::getSelfURI(); |
---|
99 | |
---|
100 | foreach ($links as $k => $v) { |
---|
101 | if (!preg_match('$^([a-z][a-z0-9.+-]+://)$', $v['link_href'])) { |
---|
102 | $url = http::concatURL($self_uri, $v['link_href']); |
---|
103 | if (strlen($url) > $current_size && preg_match('/^' . preg_quote($url, '/') . '/', $self_uri)) { |
---|
104 | $current = $k; |
---|
105 | $current_size = strlen($url); |
---|
106 | } |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | foreach ($links as $k => $v) { |
---|
111 | $title = $v['link_title']; |
---|
112 | $href = $v['link_href']; |
---|
113 | $desc = $v['link_desc']; |
---|
114 | $lang = $v['link_lang']; |
---|
115 | $xfn = $v['link_xfn']; |
---|
116 | |
---|
117 | $link = |
---|
118 | '<a href="' . html::escapeHTML($href) . '"' . |
---|
119 | ((!$lang) ? '' : ' hreflang="' . html::escapeHTML($lang) . '"') . |
---|
120 | ((!$desc) ? '' : ' title="' . html::escapeHTML($desc) . '"') . |
---|
121 | ((!$xfn) ? '' : ' rel="' . html::escapeHTML($xfn) . '"') . |
---|
122 | '>' . |
---|
123 | html::escapeHTML($title) . |
---|
124 | '</a>'; |
---|
125 | |
---|
126 | $current_class = $current == $k ? ' class="active"' : ''; |
---|
127 | |
---|
128 | $list .= sprintf($item, $link, $current_class) . "\n"; |
---|
129 | } |
---|
130 | |
---|
131 | return sprintf($block, $list) . "\n"; |
---|
132 | } |
---|
133 | |
---|
134 | # Widget function |
---|
135 | public static function linksWidget($w) |
---|
136 | { |
---|
137 | global $core; |
---|
138 | |
---|
139 | if ($w->offline) { |
---|
140 | return; |
---|
141 | } |
---|
142 | |
---|
143 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
---|
144 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
---|
145 | return; |
---|
146 | } |
---|
147 | |
---|
148 | $links = self::getList($w->renderSubtitle('', false), '<ul>%s</ul>', '<li%2$s>%1$s</li>', $w->category); |
---|
149 | |
---|
150 | if (empty($links)) { |
---|
151 | return; |
---|
152 | } |
---|
153 | |
---|
154 | return $w->renderDiv($w->content_only, 'links ' . $w->class, '', |
---|
155 | ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . |
---|
156 | $links); |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | class urlBlogroll extends dcUrlHandlers |
---|
161 | { |
---|
162 | public static function xbel($args) |
---|
163 | { |
---|
164 | $blogroll = new dcBlogroll($GLOBALS['core']->blog); |
---|
165 | |
---|
166 | try { |
---|
167 | $links = $blogroll->getLinks(); |
---|
168 | } catch (Exception $e) { |
---|
169 | self::p404(); |
---|
170 | return; |
---|
171 | } |
---|
172 | |
---|
173 | if ($args) { |
---|
174 | self::p404(); |
---|
175 | return; |
---|
176 | } |
---|
177 | |
---|
178 | http::cache($GLOBALS['mod_files'], $GLOBALS['mod_ts']); |
---|
179 | |
---|
180 | header('Content-Type: text/xml; charset=UTF-8'); |
---|
181 | |
---|
182 | echo |
---|
183 | '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . |
---|
184 | '<!DOCTYPE xbel PUBLIC "+//IDN python.org//DTD XML Bookmark Exchange ' . |
---|
185 | 'Language 1.0//EN//XML"' . "\n" . |
---|
186 | '"http://www.python.org/topics/xml/dtds/xbel-1.0.dtd">' . "\n" . |
---|
187 | '<xbel version="1.0">' . "\n" . |
---|
188 | '<title>' . html::escapeHTML($GLOBALS['core']->blog->name) . " blogroll</title>\n"; |
---|
189 | |
---|
190 | $i = 1; |
---|
191 | foreach ($blogroll->getLinksHierarchy($links) as $cat_title => $links) { |
---|
192 | if ($cat_title != '') { |
---|
193 | echo |
---|
194 | '<folder>' . "\n" . |
---|
195 | "<title>" . html::escapeHTML($cat_title) . "</title>\n"; |
---|
196 | } |
---|
197 | |
---|
198 | foreach ($links as $k => $v) { |
---|
199 | $lang = $v['link_lang'] ? ' xml:lang="' . $v['link_lang'] . '"' : ''; |
---|
200 | |
---|
201 | echo |
---|
202 | '<bookmark href="' . $v['link_href'] . '"' . $lang . '>' . "\n" . |
---|
203 | '<title>' . html::escapeHTML($v['link_title']) . "</title>\n"; |
---|
204 | |
---|
205 | if ($v['link_desc']) { |
---|
206 | echo '<desc>' . html::escapeHTML($v['link_desc']) . "</desc>\n"; |
---|
207 | } |
---|
208 | |
---|
209 | if ($v['link_xfn']) { |
---|
210 | echo |
---|
211 | "<info>\n" . |
---|
212 | '<metadata owner="http://gmpg.org/xfn/">' . $v['link_xfn'] . "</metadata>\n" . |
---|
213 | "</info>\n"; |
---|
214 | } |
---|
215 | |
---|
216 | echo |
---|
217 | "</bookmark>\n"; |
---|
218 | } |
---|
219 | |
---|
220 | if ($cat_title != '') { |
---|
221 | echo "</folder>\n"; |
---|
222 | } |
---|
223 | |
---|
224 | $i++; |
---|
225 | } |
---|
226 | |
---|
227 | echo |
---|
228 | '</xbel>'; |
---|
229 | } |
---|
230 | } |
---|