1<?php
2/**
3 * Flattr Plugin
4 *
5 * Inserts a flattr button into the current wikipage
6 *
7 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author  Gina Haeussge <osd@foosel.net>
9 */
10
11if (!defined('DOKU_INC'))
12    define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/');
13if (!defined('DOKU_PLUGIN'))
14    define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
15require_once (DOKU_INC . 'inc/template.php');
16require_once (DOKU_INC . 'inc/pageutils.php');
17
18class helper_plugin_flattr extends DokuWiki_Plugin {
19
20    var $validParameters = array(
21        'uid', 'title', 'description', 'category', 'language', 'tag', 'url', 'button', 'align', 'thing'
22    );
23
24    function validateParameters(&$params) {
25        if (isset($params['align'])) {
26            if (!in_array($params['align'], array('left', 'center', 'right')))
27                $params['align'] = 'left';
28        }
29
30        if (isset($params['button'])) {
31            if (!in_array($params['button'], array('normal', 'compact', 'static')))
32                $params['button'] = 'normal';
33        }
34
35        if (isset($params['category'])) {
36            if (!in_array($params['category'], array('text', 'images', 'video', 'audio', 'software', 'rest')))
37                $params['category'] = $this->getConf('default_category');
38        }
39
40        if (isset($params['uid'])) {
41            if (preg_match('#^[0-9a-z]+$#', $params['uid']) != 1)
42                unset($params['uid']);
43        }
44
45        if (isset($params['thing'])) {
46            if (preg_match('#^[0-9]+$#', $params['thing']) != 1)
47                unset($params['thing']);
48        }
49    }
50
51    function insertMissingParameters(&$params, $title=false, $description=false, $tag=false) {
52        global $INFO;
53        $meta = p_get_metadata($INFO['id']);
54
55        // Support deprecated parameters
56        $params = array_merge($params, array_filter(compact('title', 'description', 'tag')));
57
58        foreach ($this->validParameters as $p) {
59            if (!isset($params[$p])) {
60                switch ($p) {
61                    case 'uid': {
62                        if (trim($this->getConf('default_uid')) != '') {
63                            $params['uid'] = $this->getConf('default_uid');
64                        }
65                        break;
66                    }
67                    case 'category': {
68                        if (trim($this->getConf('default_category')) != '') {
69                            $params['category'] = $this->getConf('default_category');
70                        }
71                        break;
72                    }
73                    case 'title': {
74                        $params['title'] = tpl_pagetitle($INFO['id'], true);
75                        break;
76                    }
77                    case 'description': {
78                        $params['description'] = $meta['description']['abstract'];
79                        break;
80                    }
81                    case 'language': {
82                        if ($this->getConf('default_language')) {
83                            $params['language'] = $this->getConf('default_language');
84                        }
85                        break;
86                    }
87                    case 'url': {
88                        $params['url'] = wl($INFO['id'], '', true);
89                        break;
90                    }
91                    case 'align': {
92                        $params['align'] = 'left';
93                        break;
94                    }
95                    case 'tag': {
96                        $tags = $meta['subject'];
97                        if (!is_array($tags)) $tags = explode(' ', $tags);
98                        $params['tag'] = implode(',', $tags);
99                        break;
100                    }
101                }
102            }
103        }
104    }
105
106    function getEmbedCode($params) {
107        if (!isset($params['align']))
108            return '[n/a: alignment not set]';
109
110        $code = '<div class="flattr_'.$this->_xmlEntities($params['align']).'">';
111        switch ($params['button']) {
112            case 'static':
113                $code .= $this->getStaticEmbedCode($params);
114                break;
115            default:
116                $code .= $this->getJsEmbedCode($params);
117                break;
118        }
119        $code .= '</div>';
120
121        return $code;
122    }
123
124    function getJsEmbedCode($params) {
125        // Map param names to flattr rev attribute keys
126        $revmappings = array('uid'      => 'uid',
127                             'category' => 'category',
128                             'language' => 'language',
129                             'tag'      => 'tags',
130                             'button'   => 'button');
131        $rev_params = array();
132        foreach ($revmappings as $from => $to) {
133            if (isset($params[$to])) {
134                $rev_params[$to] = $params[$to];
135            } elseif (isset($params[$from])) {
136                $rev_params[$to] = $params[$from];
137            }
138        }
139
140        // Check if mandatory params are given
141        $mandatories = array('uid', 'title', 'description', 'category', 'language', 'url');
142        $failed = array_diff($mandatories, array_keys($params));
143        if (count($failed) > 0) {
144            return '[n/a: ' . implode(', ', $failed) . ' not set]';
145        }
146
147        // Write flattr button definition
148        $code = '<a class="FlattrButton" style="display:none;" ';
149        $code .= 'title="'.hsc($params['title']).'" ';
150        $code .= 'href="'.hsc($params['url']).'" ';
151        $code .= 'rev="flattr;';
152        foreach ($rev_params as $key => $value) {
153            $code .= hsc($key . ':' . $value . ';');
154        }
155        $code .= '">';
156        $code .= str_replace("\n", "<br />", hsc($params['description']));
157        $code .= '</a>';
158
159        return $code;
160    }
161
162    function getStaticEmbedCode($params) {
163        if (!isset($params['thing'])) {
164            return '[n/a: thing id not set]';
165        }
166
167        $code = '<a href="https://flattr.com/thing/'.$this->_xmlEntities($params['thing']).'">'.DOKU_LF;
168        $code .= '<img src="'.DOKU_URL.'/lib/plugins/flattr/button-static.png" />'.DOKU_LF;
169        $code .= '</a>'.DOKU_LF;
170
171        return $code;
172    }
173
174    function tpl_flattrbtn($params = array(), $ret = false) {
175        $this->insertMissingParameters($params);
176        $btn = $this->getEmbedCode($params);
177        if ($ret) return $btn;
178        echo $btn;
179    }
180
181    function _xmlEntities($string) {
182        return htmlspecialchars($string,ENT_QUOTES,'UTF-8');
183    }
184
185}
186