File: _public.php

package info (click to toggle)
dotclear 2.6.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 8,420 kB
  • sloc: php: 54,270; sql: 1,290; sh: 213; xml: 173; makefile: 158
file content (233 lines) | stat: -rw-r--r-- 5,508 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
# -- BEGIN LICENSE BLOCK ---------------------------------------
#
# This file is part of Dotclear 2.
#
# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
# Licensed under the GPL version 2.0 license.
# See LICENSE file or
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# -- END LICENSE BLOCK -----------------------------------------
if (!defined('DC_RC_PATH')) { return; }

require dirname(__FILE__).'/_widgets.php';

# Blogroll template functions
$core->tpl->addValue('Blogroll',array('tplBlogroll','blogroll'));
$core->tpl->addValue('BlogrollXbelLink',array('tplBlogroll','blogrollXbelLink'));

$core->url->register('xbel','xbel','^xbel(?:/?)$',array('urlBlogroll','xbel'));

class tplBlogroll
{
	public static function blogroll($attr)
	{
		$category='<h3>%s</h3>';
		$block='<ul>%s</ul>';
		$item='<li%2$s>%1$s</li>';

		if (isset($attr['category'])) {
			$category = addslashes($attr['category']);
		}

		if (isset($attr['block'])) {
			$block = addslashes($attr['block']);
		}

		if (isset($attr['item'])) {
			$item = addslashes($attr['item']);
		}

		$only_cat = 'null';
		if (!empty($attr['only_category'])) {
			$only_cat = "'".addslashes($attr['only_category'])."'";
		}

		return
		'<?php '.
		"echo tplBlogroll::getList('".$category."','".$block."','".$item."',".$only_cat."); ".
		'?>';
	}

	public static function blogrollXbelLink($attr)
	{
		$f = $GLOBALS['core']->tpl->getFilters($attr);
		return '<?php echo '.sprintf($f,'$core->blog->url.$core->url->getURLFor("xbel")').'; ?>';
	}

	public static function getList($cat_title='<h3>%s</h3>',$block='<ul>%s</ul>',$item='<li>%s</li>',$category=null)
	{
		$blogroll = new dcBlogroll($GLOBALS['core']->blog);

		try {
			$links = $blogroll->getLinks();
		} catch (Exception $e) {
			return false;
		}

		$res = '';

		$hierarchy = $blogroll->getLinksHierarchy($links);

		if ($category) {
			if (!isset($hierarchy[$category])) {
				return '';
			}
			$hierarchy = array($hierarchy[$category]);
		}

		foreach ($hierarchy as $k => $v)
		{
			if ($k != '') {
				$res .= sprintf($cat_title,html::escapeHTML($k))."\n";
			}

			$res .= self::getLinksList($v,$block,$item);
		}

		return $res;
	}

	private static function getLinksList($links,$block='<ul>%s</ul>',$item='<li%2$s>%1$s</li>')
	{
		$list = '';

		# Find current link item if any
		$current = -1;
		$current_size = 0;
		$self_uri = http::getSelfURI();

		foreach ($links as $k => $v)
		{
			if (!preg_match('$^([a-z][a-z0-9.+-]+://)$',$v['link_href'])) {
				$url = http::concatURL($self_uri,$v['link_href']);
				if (strlen($url) > $current_size && preg_match('/^'.preg_quote($url,'/').'/',$self_uri)) {
					$current = $k;
					$current_size = strlen($url);
				}
			}
		}

		foreach ($links as $k => $v)
		{
			$title = $v['link_title'];
			$href  = $v['link_href'];
			$desc = $v['link_desc'];
			$lang  = $v['link_lang'];
			$xfn = $v['link_xfn'];

			$link =
			'<a href="'.html::escapeHTML($href).'"'.
			((!$lang) ? '' : ' hreflang="'.html::escapeHTML($lang).'"').
			((!$desc) ? '' : ' title="'.html::escapeHTML($desc).'"').
			((!$xfn) ? '' : ' rel="'.html::escapeHTML($xfn).'"').
			'>'.
			html::escapeHTML($title).
			'</a>';

			$current_class = $current == $k ? ' class="active"' : '';

			$list .= sprintf($item,$link,$current_class)."\n";
		}

		return sprintf($block,$list)."\n";
	}

	# Widget function
	public static function linksWidget($w)
	{
		global $core;

		if (($w->homeonly == 1 && $core->url->type != 'default') ||
			($w->homeonly == 2 && $core->url->type == 'default')) {
			return;
		}

		$links = self::getList('<h3>%s</h3>','<ul>%s</ul>','<li%2$s>%1$s</li>',$w->category);

		if (empty($links)) {
			return;
		}

		return
		($w->content_only ? '' : '<div class="links'.($w->class ? ' '.html::escapeHTML($w->class) : '').'">').
		($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
		$links.
		($w->content_only ? '' : '</div>');
	}
}

class urlBlogroll extends dcUrlHandlers
{
	public static function xbel($args)
	{
		$blogroll = new dcBlogroll($GLOBALS['core']->blog);

		try {
			$links = $blogroll->getLinks();
		} catch (Exception $e) {
			self::p404();
			return;
		}

		if ($args) {
			self::p404();
			return;
		}

		http::cache($GLOBALS['mod_files'],$GLOBALS['mod_ts']);

		header('Content-Type: text/xml; charset=UTF-8');

		echo
		'<?xml version="1.0" encoding="UTF-8"?>'."\n".
		'<!DOCTYPE xbel PUBLIC "+//IDN python.org//DTD XML Bookmark Exchange '.
		'Language 1.0//EN//XML"'."\n".
		'"http://www.python.org/topics/xml/dtds/xbel-1.0.dtd">'."\n".
		'<xbel version="1.0">'."\n".
		'<title>'.html::escapeHTML($GLOBALS['core']->blog->name)." blogroll</title>\n";

		$i = 1;
		foreach ($blogroll->getLinksHierarchy($links) as $cat_title => $links)
		{
			if ($cat_title != '') {
				echo
				'<folder>'."\n".
				"<title>".html::escapeHTML($cat_title)."</title>\n";
			}

			foreach ($links as $k => $v)
			{
				$lang = $v['link_lang'] ? ' xml:lang="'.$v['link_lang'].'"' : '';

				echo
				'<bookmark href="'.$v['link_href'].'"'.$lang.'>'."\n".
				'<title>'.html::escapeHTML($v['link_title'])."</title>\n";

				if ($v['link_desc']) {
					echo '<desc>'.html::escapeHTML($v['link_desc'])."</desc>\n";
				}

				if ($v['link_xfn']) {
					echo
					"<info>\n".
					'<metadata owner="http://gmpg.org/xfn/">'.$v['link_xfn']."</metadata>\n".
					"</info>\n";
				}

				echo
				"</bookmark>\n";
			}

			if ($cat_title != '') {
				echo "</folder>\n";
			}

			$i++;
		}

		echo
		'</xbel>';
	}
}