This repository has been archived by the owner on Dec 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.php
185 lines (161 loc) · 6.38 KB
/
helper.php
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
<?php
/**
* Flattr Plugin
*
* Inserts a flattr button into the current wikipage
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Gina Haeussge <[email protected]>
*/
if (!defined('DOKU_INC'))
define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/');
if (!defined('DOKU_PLUGIN'))
define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
require_once (DOKU_INC . 'inc/template.php');
require_once (DOKU_INC . 'inc/pageutils.php');
class helper_plugin_flattr extends DokuWiki_Plugin {
var $validParameters = array(
'uid', 'title', 'description', 'category', 'language', 'tag', 'url', 'button', 'align', 'thing'
);
function validateParameters(&$params) {
if (isset($params['align'])) {
if (!in_array($params['align'], array('left', 'center', 'right')))
$params['align'] = 'left';
}
if (isset($params['button'])) {
if (!in_array($params['button'], array('normal', 'compact', 'static')))
$params['button'] = 'normal';
}
if (isset($params['category'])) {
if (!in_array($params['category'], array('text', 'images', 'video', 'audio', 'software', 'rest')))
$params['category'] = $this->getConf('default_category');
}
if (isset($params['uid'])) {
if (preg_match('#^[0-9a-z]+$#', $params['uid']) != 1)
unset($params['uid']);
}
if (isset($params['thing'])) {
if (preg_match('#^[0-9]+$#', $params['thing']) != 1)
unset($params['thing']);
}
}
function insertMissingParameters(&$params, $title=false, $description=false, $tag=false) {
global $INFO;
$meta = p_get_metadata($INFO['id']);
// Support deprecated parameters
$params = array_merge($params, array_filter(compact('title', 'description', 'tag')));
foreach ($this->validParameters as $p) {
if (!isset($params[$p])) {
switch ($p) {
case 'uid': {
if (trim($this->getConf('default_uid')) != '') {
$params['uid'] = $this->getConf('default_uid');
}
break;
}
case 'category': {
if (trim($this->getConf('default_category')) != '') {
$params['category'] = $this->getConf('default_category');
}
break;
}
case 'title': {
$params['title'] = tpl_pagetitle($INFO['id'], true);
break;
}
case 'description': {
$params['description'] = $meta['description']['abstract'];
break;
}
case 'language': {
if ($this->getConf('default_language')) {
$params['language'] = $this->getConf('default_language');
}
break;
}
case 'url': {
$params['url'] = wl($INFO['id'], '', true);
break;
}
case 'align': {
$params['align'] = 'left';
break;
}
case 'tag': {
$tags = $meta['subject'];
if (!is_array($tags)) $tags = explode(' ', $tags);
$params['tag'] = implode(',', $tags);
break;
}
}
}
}
}
function getEmbedCode($params) {
if (!isset($params['align']))
return '[n/a: alignment not set]';
$code = '<div class="flattr_'.$this->_xmlEntities($params['align']).'">';
switch ($params['button']) {
case 'static':
$code .= $this->getStaticEmbedCode($params);
break;
default:
$code .= $this->getJsEmbedCode($params);
break;
}
$code .= '</div>';
return $code;
}
function getJsEmbedCode($params) {
// Map param names to flattr rev attribute keys
$revmappings = array('uid' => 'uid',
'category' => 'category',
'language' => 'language',
'tag' => 'tags',
'button' => 'button');
$rev_params = array();
foreach ($revmappings as $from => $to) {
if (isset($params[$to])) {
$rev_params[$to] = $params[$to];
} elseif (isset($params[$from])) {
$rev_params[$to] = $params[$from];
}
}
// Check if mandatory params are given
$mandatories = array('uid', 'title', 'description', 'category', 'language', 'url');
$failed = array_diff($mandatories, array_keys($params));
if (count($failed) > 0) {
return '[n/a: ' . implode(', ', $failed) . ' not set]';
}
// Write flattr button definition
$code = '<a class="FlattrButton" style="display:none;" ';
$code .= 'title="'.hsc($params['title']).'" ';
$code .= 'href="'.hsc($params['url']).'" ';
$code .= 'rev="flattr;';
foreach ($rev_params as $key => $value) {
$code .= hsc($key . ':' . $value . ';');
}
$code .= '">';
$code .= str_replace("\n", "<br />", hsc($params['description']));
$code .= '</a>';
return $code;
}
function getStaticEmbedCode($params) {
if (!isset($params['thing'])) {
return '[n/a: thing id not set]';
}
$code = '<a href="https://flattr.com/thing/'.$this->_xmlEntities($params['thing']).'">'.DOKU_LF;
$code .= '<img src="'.DOKU_URL.'/lib/plugins/flattr/button-static.png" />'.DOKU_LF;
$code .= '</a>'.DOKU_LF;
return $code;
}
function tpl_flattrbtn($params = array(), $ret = false) {
$this->insertMissingParameters($params);
$btn = $this->getEmbedCode($params);
if ($ret) return $btn;
echo $btn;
}
function _xmlEntities($string) {
return htmlspecialchars($string,ENT_QUOTES,'UTF-8');
}
}