1<?php
2/**
3 * DokuWiki Plugin socialite (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Fernando Ribeiro <pinguim.ribeiro@gmail.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12class syntax_plugin_socialite_button extends DokuWiki_Syntax_Plugin {
13    /**
14     * @return string Syntax mode type
15     */
16    public function getType() {
17        return 'substition';
18    }
19    /**
20     * @return string Paragraph type
21     */
22    public function getPType() {
23        return 'normal';
24    }
25    /**
26     * @return int Sort order - Low numbers go before high numbers
27     */
28    public function getSort() {
29        return 999;
30    }
31
32    /**
33     * Connect lookup pattern to lexer.
34     *
35     * @param string $mode Parser mode
36     */
37    public function connectTo($mode) {
38        $this->Lexer->addSpecialPattern('~~socialite\b.*?~~',$mode,'plugin_socialite_button');
39    }
40
41    /**
42     * Handle matches of the socialite syntax
43     *
44     * @param string $match The match of the syntax
45     * @param int    $state The state of the handler
46     * @param int    $pos The position in the document
47     * @param Doku_Handler    $handler The handler
48     * @return array Data for the renderer
49     */
50    public function handle($match, $state, $pos, Doku_Handler $handler){
51        $match = strtolower(trim(substr($match, 11, -2))); // strip markup
52
53        // checks if a display mode is passed
54        if (substr($match, 0, 1) === ':')  {
55            list($display) = explode(' ', $match);
56            $display = substr($display, 1);
57            $networks = trim(substr($match, strlen($display) + 1 ));
58        }
59
60        if (empty($networks)) {
61            $networks = strtolower(trim($this->getConf('networks')));
62        }
63
64        return array(
65            'display'  => $display,
66            'networks' => explode(' ', $networks)
67            );
68    }
69
70    /**
71     * Render xhtml output or metadata
72     *
73     * @param string         $mode      Renderer mode (supported modes: xhtml)
74     * @param Doku_Renderer  $renderer  The renderer
75     * @param array          $data      The data from the handler() function
76     * @return bool If rendering was successful.
77     */
78    public function render($mode, Doku_Renderer $renderer, $data) {
79
80        if($mode != 'xhtml') return false;
81
82        // validation list of available display modes
83        $valid_displays = array('name', 'icon', 'color');
84        // validation list of available social networks
85        $valid_networks = array('twitter', 'facebook', 'googleplus',
86                                'linkedin', 'pinterest', 'tumblr',
87                                'reddit', 'taringa', 'stumbleupon',
88                                'telegram', 'hackernews', 'xing', 'vk', 'email');
89
90        if (in_array($data['display'], $valid_displays)) {
91            $display = $data['display'];
92        } else {
93            $display = $this->getConf('display') ;
94        }
95
96        $renderer->doc .= '<ul class="socialite">';
97        foreach ($data['networks'] as $network) {
98            if (in_array($network, $valid_networks)) {
99                $renderer->doc .= $this->socialite_button($display, $network);
100            }
101        }
102        $renderer->doc .= '</ul>';
103
104        return true;
105    }
106
107    /**
108     * Render xhtml output for share buttons
109     *
110     * @param string    $display    The display mode
111     * @param string    $network    The social network
112     * @return string   Xhtml code for button.
113     */
114    protected function socialite_button ($display, $network) {
115        global $ID;
116        global $INFO;
117
118        $url      = rawurlencode(wl($INFO['id'], '', true));
119        $title    = rawurlencode(($INFO['meta']['title']) ? $INFO['meta']['title'] : $meta);
120        $abstract = rawurlencode($INFO['meta']['description']['abstract']);
121
122        $target = $this->getConf('target') ;
123
124        switch ($network) {
125            case 'twitter':
126                $name = 'Twitter';
127                $href = 'https://twitter.com/intent/tweet?url=' . $url . '&text='. $title;
128                if ($this->getConf('twitter_user')) { $href .= "&via=" . rawurlencode($this->getConf('twitter_user')); }
129                break;
130            case 'facebook':
131                $name = 'Facebook';
132                $href = 'http://www.facebook.com/sharer.php?u='. $url;
133                break;
134            case 'googleplus':
135                $name = 'Google+';
136                $href = 'https://plus.google.com/share?url='. $url;
137                break;
138            case 'linkedin':
139                $name = 'LinkedIn';
140                $href = 'https://www.linkedin.com/shareArticle?url='. $url .'&title=' . $title . '&summary=' . $abstract . '&mini=true&source=' . $url;
141                break;
142            case 'pinterest':
143                $name = 'Pinterest';
144                $href = 'https://pinterest.com/pin/create/button/?url='. $url .'&description=' . $title;
145                break;
146            case 'tumblr':
147                $name = 'Tumblr';
148                $href = 'http://www.tumblr.com/share/link?url='. $url .'&description=' . $title;
149                break;
150            case 'reddit':
151                $name = 'Reddit';
152                $href = 'http://www.reddit.com/submit?url='. $url .'&title=' . $title;
153                break;
154            case 'taringa':
155                $name = 'Taringa';
156                $href = 'http://www.taringa.net/widgets/share.php?url='. $url .'&body=' . $title;
157                break;
158            case 'stumbleupon':
159                $name = 'StumbleUpon';
160                $href = 'http://www.stumbleupon.com/submit?url='. $url .'&title=' . $title;
161                break;
162            case 'telegram':
163                $name = 'Telegram';
164                $href = 'https://telegram.me/share/url?url='. $url .'&text=' . $title;
165                break;
166            case 'hackernews':
167                $name = 'Hacker News';
168                $href = 'https://news.ycombinator.com/submitlink?u='. $url .'&t=' . $title;
169                break;
170            case 'xing':
171                $name = 'Xing';
172                $href = 'https://www.xing-share.com/app/user?op=share;sc_p=xing-share;url='. $url;
173                break;
174            case 'vk':
175                $name = 'Vk';
176                $href = 'https://vk.com/share.php?url='. $url .'&title=' . $title . '&description=' . $abstract . '&noparse=true';
177                break;
178            case 'email':
179                $name = 'Email';
180                $href = 'mailto:?subject='. $title .'&body=' . $url . ': '. $abstract;
181                break;
182        }
183
184        $xhtml  = '<li class="' . $display . '">';
185        $xhtml .= '<a class="' . $network . '" target="' . $target . '" href="' . $href . '">' . $name . '</a>';
186        $xhtml .= '</li>';
187
188        return $xhtml;
189    }
190
191}
192