Dotclear

source: plugins/blogroll/_public.php @ 1179:a43a29427ef3

Revision 1179:a43a29427ef3, 5.5 KB checked in by franck <carnet.franck.paul@…>, 11 years ago (diff)

Update copyright notice

Line 
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 -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14require 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
22class 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 = 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 == 1 && $core->url->type != 'default') ||
143               ($w->homeonly == 2 && $core->url->type == 'default')) {
144               return;
145          }
146         
147          $links = self::getList('<h3>%s</h3>','<ul>%s</ul>','<li%2$s>%1$s</li>',$w->category);
148         
149          if (empty($links)) {
150               return;
151          }
152         
153          return
154          ($w->content_only ? '' : '<div class="links'.($w->class ? ' '.html::escapeHTML($w->class) : '').'">').
155          ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
156          $links.
157          ($w->content_only ? '' : '</div>');
158     }
159}
160
161class urlBlogroll extends dcUrlHandlers
162{
163     public static function xbel($args)
164     {
165          $blogroll = new dcBlogroll($GLOBALS['core']->blog);
166         
167          try {
168               $links = $blogroll->getLinks();
169          } catch (Exception $e) {
170               self::p404();
171               return;
172          }
173         
174          if ($args) {
175               self::p404();
176               return;
177          }
178         
179          http::cache($GLOBALS['mod_files'],$GLOBALS['mod_ts']);
180         
181          header('Content-Type: text/xml; charset=UTF-8');
182         
183          echo
184          '<?xml version="1.0" encoding="UTF-8"?>'."\n".
185          '<!DOCTYPE xbel PUBLIC "+//IDN python.org//DTD XML Bookmark Exchange '.
186          'Language 1.0//EN//XML"'."\n".
187          '"http://www.python.org/topics/xml/dtds/xbel-1.0.dtd">'."\n".
188          '<xbel version="1.0">'."\n".
189          '<title>'.html::escapeHTML($GLOBALS['core']->blog->name)." blogroll</title>\n";
190         
191          $i = 1;
192          foreach ($blogroll->getLinksHierarchy($links) as $cat_title => $links)
193          {
194               if ($cat_title != '') {
195                    echo
196                    '<folder>'."\n".
197                    "<title>".html::escapeHTML($cat_title)."</title>\n";
198               }
199               
200               foreach ($links as $k => $v)
201               {
202                    $lang = $v['link_lang'] ? ' xml:lang="'.$v['link_lang'].'"' : '';
203                   
204                    echo
205                    '<bookmark href="'.$v['link_href'].'"'.$lang.'>'."\n".
206                    '<title>'.html::escapeHTML($v['link_title'])."</title>\n";
207                   
208                    if ($v['link_desc']) {
209                         echo '<desc>'.html::escapeHTML($v['link_desc'])."</desc>\n";
210                    }
211                   
212                    if ($v['link_xfn']) {
213                         echo
214                         "<info>\n".
215                         '<metadata owner="http://gmpg.org/xfn/">'.$v['link_xfn']."</metadata>\n".
216                         "</info>\n";
217                    }
218                   
219                    echo
220                    "</bookmark>\n";
221               }
222               
223               if ($cat_title != '') {
224                    echo "</folder>\n";
225               }
226               
227               $i++;
228          }
229         
230          echo
231          '</xbel>';
232     }
233}
234?>
Note: See TracBrowser for help on using the repository browser.

Sites map