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