1<?php
2
3/* Facebook Like-Button Plugin for Dokuwiki
4 *
5 * Copyright (C) 2012 Marvin Thomas Rabe (marvinrabe.de)
6 *
7 * This program is free software; you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along with this program;
16 * if not, see <http://www.gnu.org/licenses/>. */
17
18/**
19 * Embed a Facebook like-button onto any page
20 * @license GNU General Public License 3 <http://www.gnu.org/licenses/>
21 * @author Marvin Thomas Rabe <mrabe@marvinrabe.de>
22 */
23
24if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
25if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
26require_once(DOKU_PLUGIN.'syntax.php');
27require_once(DOKU_INC.'inc/auth.php');
28
29class syntax_plugin_facebooklike extends DokuWiki_Syntax_Plugin {
30
31	private $data;
32
33	/**
34	 * General information about the plugin.
35	 */
36	public function getInfo(){
37		return array(
38			'author' => 'Marvin Thomas Rabe',
39			'email'  => 'mrabe@marvinrabe.de',
40			'date'	 => '2012-06-06',
41			'name'	 => 'Facebook Like-Button',
42			'desc'	 => 'Adds Facebook like-buttons',
43			'url'	 => 'https://github.com/marvinrabe/dokuwiki-facebook',
44		);
45	}
46
47	/**
48	 * What kind of syntax are we?
49	 */
50	public function getType(){
51		return 'container';
52	}
53
54	/**
55	 * What about paragraphs?
56	 */
57	public function getPType(){
58		return 'block';
59	}
60
61	/**
62 	 * Where to sort in?
63 	 */
64	public function getSort(){
65		return 309;
66	}
67
68	/**
69 	 * Connect pattern to lexer.
70 	 */
71	public function connectTo($mode) {
72		$this->Lexer->addSpecialPattern('\{\{like>[^}]*\}\}',$mode,'plugin_facebooklike');
73	}
74
75	/**
76	 * Handle the match
77	 */
78	public function handle($match, $state, $pos, &$handler){
79		if (isset($_REQUEST['comment']))
80		    return false;
81
82		$match = substr($match,7,-2); //strip markup from start and end
83
84		$data = array();
85
86		//handle params
87		$params = explode('|',$match);
88		foreach($params as $param){
89			$splitparam = explode('=',$param);
90			$data[$splitparam[0]] = $splitparam[1];
91		}
92		return $data;
93	}
94
95	/**
96	 * Create output.
97	 */
98	public function render($mode, &$renderer, $data) {
99		if($mode == 'xhtml'){
100			// Next line just for developing purposes.
101			// Disables Dokuwiki cache.
102			// $renderer->info['cache'] = false;
103			$renderer->doc .= $this->_button($data);
104			return true;
105		}
106		return false;
107	}
108
109	/**
110	 * Does the contact form XHTML creation. Adds some JavaScript to validate the form
111	 * and creates the input form.
112	 */
113	protected function _button($data){
114		global $ID;
115		global $conf;
116		$this->data = $data;
117
118		$ret = '<fb:like href="'.(empty($data['url'])?wl($ID,'',true):$data['url']).'" '
119			. 'layout="'.$this->_setting('layout', $conf).'" '
120			. 'show_faces="'.$this->_setting('faces', $conf).'" '
121			. 'width="'.$this->_setting('width', $conf).'" '
122			. 'action="'.$this->_setting('action', $conf).'" '
123			. 'font="'.$this->_setting('font', $conf).'" '
124			. 'colorscheme="'.$this->_setting('colorscheme', $conf).'"'
125			. '></fb:like>';
126
127		return $ret;
128	}
129
130	/**
131	 * Returns valid setting.
132	 */
133	protected function _setting($name, $gconf) {
134		include dirname(__FILE__).'/conf/default.php';
135
136		if(strtolower($name) == 'faces') {
137			if(empty($this->data['show_faces'])) {
138				if(!isset($gconf['plugin']['facebooklike']['show_faces']))
139					return 'true';
140				else
141					return ($gconf['plugin']['facebooklike']['show_faces'] == 1)?'true':'false';
142			} else
143				return $this->data['show_faces'];
144		} else {
145			if(empty($this->data[$name])) {
146				if(!isset($gconf['plugin']['facebooklike'][$name]))
147					return $conf[$name];
148				else
149					return $gconf['plugin']['facebooklike'][$name];
150			} else
151				return $this->data[$name];
152		}
153	}
154
155}
156