[0] | 1 | <?php |
---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
| 3 | # |
---|
| 4 | # This file is part of Dotclear 2. |
---|
| 5 | # |
---|
| 6 | # Copyright (c) 2003-2010 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 | 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>'; |
---|
| 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->getBase("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 = array($hierarchy[$category]); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | foreach ($hierarchy as $k => $v) |
---|
| 81 | { |
---|
| 82 | if ($k != '') { |
---|
| 83 | $res .= sprintf($cat_title,html::escapeHTML($k))."\n"; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | $res .= self::getLinksList($v,$block,$item); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | return $res; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | private static function getLinksList($links,$block='<ul>%s</ul>',$item='<li%2$s>%1$s</li>') |
---|
| 93 | { |
---|
| 94 | $list = ''; |
---|
| 95 | |
---|
| 96 | # Find current link item if any |
---|
| 97 | $current = -1; |
---|
| 98 | $current_size = 0; |
---|
| 99 | $self_uri = http::getSelfURI(); |
---|
| 100 | |
---|
| 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 | } |
---|
| 111 | |
---|
| 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']; |
---|
| 119 | |
---|
| 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>'; |
---|
| 128 | |
---|
| 129 | $current_class = $current == $k ? ' class="active"' : ''; |
---|
| 130 | |
---|
| 131 | $list .= sprintf($item,$link,$current_class)."\n"; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | return sprintf($block,$list)."\n"; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | # Widget function |
---|
| 138 | public static function linksWidget($w) |
---|
| 139 | { |
---|
| 140 | global $core; |
---|
| 141 | |
---|
| 142 | if ($w->homeonly && $core->url->type != 'default') { |
---|
| 143 | return; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | $links = self::getList('<h3>%s</h3>','<ul>%s</ul>','<li%2$s>%1$s</li>',$w->category); |
---|
| 147 | |
---|
| 148 | if (empty($links)) { |
---|
| 149 | return; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | return |
---|
| 153 | '<div class="links">'. |
---|
| 154 | ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : ''). |
---|
| 155 | $links. |
---|
| 156 | '</div>'; |
---|
| 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 | { |
---|
| 193 | if ($cat_title != '') { |
---|
| 194 | echo |
---|
| 195 | '<folder>'."\n". |
---|
| 196 | "<title>".html::escapeHTML($cat_title)."</title>\n"; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | foreach ($links as $k => $v) |
---|
| 200 | { |
---|
| 201 | $lang = $v['link_lang'] ? ' xml:lang="'.$v['link_lang'].'"' : ''; |
---|
| 202 | |
---|
| 203 | echo |
---|
| 204 | '<bookmark href="'.$v['link_href'].'"'.$lang.'>'."\n". |
---|
| 205 | '<title>'.html::escapeHTML($v['link_title'])."</title>\n"; |
---|
| 206 | |
---|
| 207 | if ($v['link_desc']) { |
---|
| 208 | echo '<desc>'.html::escapeHTML($v['link_desc'])."</desc>\n"; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | if ($v['link_xfn']) { |
---|
| 212 | echo |
---|
| 213 | "<info>\n". |
---|
| 214 | '<metadata owner="http://gmpg.org/xfn/">'.$v['link_xfn']."</metadata>\n". |
---|
| 215 | "</info>\n"; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | echo |
---|
| 219 | "</bookmark>\n"; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | if ($cat_title != '') { |
---|
| 223 | echo "</folder>\n"; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | $i++; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | echo |
---|
| 230 | '</xbel>'; |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | ?> |
---|