[0] | 1 | <?php |
---|
[3731] | 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 | |
---|
[3730] | 12 | if (!defined('DC_RC_PATH')) {return;} |
---|
[0] | 13 | |
---|
[3730] | 14 | require dirname(__FILE__) . '/_widgets.php'; |
---|
[0] | 15 | |
---|
| 16 | # Blogroll template functions |
---|
[3874] | 17 | $core->tpl->addValue('Blogroll', ['tplBlogroll', 'blogroll']); |
---|
| 18 | $core->tpl->addValue('BlogrollXbelLink', ['tplBlogroll', 'blogrollXbelLink']); |
---|
[0] | 19 | |
---|
[3874] | 20 | $core->url->register('xbel', 'xbel', '^xbel(?:/?)$', ['urlBlogroll', 'xbel']); |
---|
[0] | 21 | |
---|
| 22 | class tplBlogroll |
---|
| 23 | { |
---|
[3730] | 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>'; |
---|
[2566] | 29 | |
---|
[3730] | 30 | if (isset($attr['category'])) { |
---|
| 31 | $category = addslashes($attr['category']); |
---|
| 32 | } |
---|
[2566] | 33 | |
---|
[3730] | 34 | if (isset($attr['block'])) { |
---|
| 35 | $block = addslashes($attr['block']); |
---|
| 36 | } |
---|
[2566] | 37 | |
---|
[3730] | 38 | if (isset($attr['item'])) { |
---|
| 39 | $item = addslashes($attr['item']); |
---|
| 40 | } |
---|
[2566] | 41 | |
---|
[3730] | 42 | $only_cat = 'null'; |
---|
| 43 | if (!empty($attr['only_category'])) { |
---|
| 44 | $only_cat = "'" . addslashes($attr['only_category']) . "'"; |
---|
| 45 | } |
---|
[2566] | 46 | |
---|
[3730] | 47 | return |
---|
| 48 | '<?php ' . |
---|
| 49 | "echo tplBlogroll::getList('" . $category . "','" . $block . "','" . $item . "'," . $only_cat . "); " . |
---|
| 50 | '?>'; |
---|
| 51 | } |
---|
[2566] | 52 | |
---|
[3730] | 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 | } |
---|
[2566] | 58 | |
---|
[3730] | 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); |
---|
[2566] | 62 | |
---|
[3730] | 63 | try { |
---|
| 64 | $links = $blogroll->getLinks(); |
---|
| 65 | } catch (Exception $e) { |
---|
| 66 | return false; |
---|
| 67 | } |
---|
[2566] | 68 | |
---|
[3730] | 69 | $res = ''; |
---|
[2566] | 70 | |
---|
[3730] | 71 | $hierarchy = $blogroll->getLinksHierarchy($links); |
---|
[2566] | 72 | |
---|
[3730] | 73 | if ($category) { |
---|
| 74 | if (!isset($hierarchy[$category])) { |
---|
| 75 | return ''; |
---|
| 76 | } |
---|
[3874] | 77 | $hierarchy = [$hierarchy[$category]]; |
---|
[3730] | 78 | } |
---|
[2566] | 79 | |
---|
[3730] | 80 | foreach ($hierarchy as $k => $v) { |
---|
| 81 | if ($k != '') { |
---|
| 82 | $res .= sprintf($cat_title, html::escapeHTML($k)) . "\n"; |
---|
| 83 | } |
---|
[2566] | 84 | |
---|
[3730] | 85 | $res .= self::getLinksList($v, $block, $item); |
---|
| 86 | } |
---|
[2566] | 87 | |
---|
[3730] | 88 | return $res; |
---|
| 89 | } |
---|
[2566] | 90 | |
---|
[3730] | 91 | private static function getLinksList($links, $block = '<ul>%s</ul>', $item = '<li%2$s>%1$s</li>') |
---|
| 92 | { |
---|
| 93 | $list = ''; |
---|
[2566] | 94 | |
---|
[3730] | 95 | # Find current link item if any |
---|
| 96 | $current = -1; |
---|
| 97 | $current_size = 0; |
---|
| 98 | $self_uri = http::getSelfURI(); |
---|
[2566] | 99 | |
---|
[3730] | 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 | } |
---|
[2566] | 109 | |
---|
[3730] | 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']; |
---|
[2566] | 116 | |
---|
[3730] | 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>'; |
---|
[2566] | 125 | |
---|
[3730] | 126 | $current_class = $current == $k ? ' class="active"' : ''; |
---|
[2566] | 127 | |
---|
[3730] | 128 | $list .= sprintf($item, $link, $current_class) . "\n"; |
---|
| 129 | } |
---|
[2566] | 130 | |
---|
[3730] | 131 | return sprintf($block, $list) . "\n"; |
---|
| 132 | } |
---|
[2566] | 133 | |
---|
[3730] | 134 | # Widget function |
---|
| 135 | public static function linksWidget($w) |
---|
| 136 | { |
---|
| 137 | global $core; |
---|
[2566] | 138 | |
---|
[3730] | 139 | if ($w->offline) { |
---|
| 140 | return; |
---|
| 141 | } |
---|
[2778] | 142 | |
---|
[3730] | 143 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
---|
| 144 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
---|
| 145 | return; |
---|
| 146 | } |
---|
[2566] | 147 | |
---|
[3730] | 148 | $links = self::getList($w->renderSubtitle('', false), '<ul>%s</ul>', '<li%2$s>%1$s</li>', $w->category); |
---|
[2566] | 149 | |
---|
[3730] | 150 | if (empty($links)) { |
---|
| 151 | return; |
---|
| 152 | } |
---|
[2566] | 153 | |
---|
[3730] | 154 | return $w->renderDiv($w->content_only, 'links ' . $w->class, '', |
---|
| 155 | ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . |
---|
| 156 | $links); |
---|
| 157 | } |
---|
[0] | 158 | } |
---|
| 159 | |
---|
| 160 | class urlBlogroll extends dcUrlHandlers |
---|
| 161 | { |
---|
[3730] | 162 | public static function xbel($args) |
---|
| 163 | { |
---|
| 164 | $blogroll = new dcBlogroll($GLOBALS['core']->blog); |
---|
[2566] | 165 | |
---|
[3730] | 166 | try { |
---|
| 167 | $links = $blogroll->getLinks(); |
---|
| 168 | } catch (Exception $e) { |
---|
| 169 | self::p404(); |
---|
| 170 | return; |
---|
| 171 | } |
---|
[2566] | 172 | |
---|
[3730] | 173 | if ($args) { |
---|
| 174 | self::p404(); |
---|
| 175 | return; |
---|
| 176 | } |
---|
[2566] | 177 | |
---|
[3730] | 178 | http::cache($GLOBALS['mod_files'], $GLOBALS['mod_ts']); |
---|
[2566] | 179 | |
---|
[3730] | 180 | header('Content-Type: text/xml; charset=UTF-8'); |
---|
[2566] | 181 | |
---|
[3730] | 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"; |
---|
[2566] | 189 | |
---|
[3730] | 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 | } |
---|
[2566] | 197 | |
---|
[3730] | 198 | foreach ($links as $k => $v) { |
---|
| 199 | $lang = $v['link_lang'] ? ' xml:lang="' . $v['link_lang'] . '"' : ''; |
---|
[2566] | 200 | |
---|
[3730] | 201 | echo |
---|
| 202 | '<bookmark href="' . $v['link_href'] . '"' . $lang . '>' . "\n" . |
---|
| 203 | '<title>' . html::escapeHTML($v['link_title']) . "</title>\n"; |
---|
[2566] | 204 | |
---|
[3730] | 205 | if ($v['link_desc']) { |
---|
| 206 | echo '<desc>' . html::escapeHTML($v['link_desc']) . "</desc>\n"; |
---|
| 207 | } |
---|
[2566] | 208 | |
---|
[3730] | 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 | } |
---|
[2566] | 215 | |
---|
[3730] | 216 | echo |
---|
| 217 | "</bookmark>\n"; |
---|
| 218 | } |
---|
[2566] | 219 | |
---|
[3730] | 220 | if ($cat_title != '') { |
---|
| 221 | echo "</folder>\n"; |
---|
| 222 | } |
---|
[2566] | 223 | |
---|
[3730] | 224 | $i++; |
---|
| 225 | } |
---|
[2566] | 226 | |
---|
[3730] | 227 | echo |
---|
| 228 | '</xbel>'; |
---|
| 229 | } |
---|
[0] | 230 | } |
---|