1<?php
2/**
3 * Plugin googlecal: Inserts an Google Calendar iframe
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Kite <Kite@puzzlers.org>,  Christopher Smith <chris@jalakai.co.uk>
7 * @seealso    (http://www.dokuwiki.org/plugin:iframe)
8 */
9
10if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'syntax.php');
13
14/**
15 * All DokuWiki plugins to extend the parser/rendering mechanism
16 * need to inherit from this class
17 */
18class syntax_plugin_googlecal extends DokuWiki_Syntax_Plugin {
19
20    function getType() { return 'substition'; }
21
22    function getPType(){ return 'block'; }
23
24    function getSort() { return 319; }
25
26    function connectTo($mode) {
27        $this->Lexer->addSpecialPattern('{{cal>[^}]*?}}', $mode, 'plugin_googlecal');
28    }
29
30    function handle($match, $state, $pos, Doku_Handler $handler){
31        if(preg_match('/{{cal>(.*)/', $match)) {             // Hook for future features
32            // Handle the simplified style of calendar tag
33            $match = html_entity_decode(substr($match, 6, -2));
34            @list($url, $alt) = explode('|',$match,2);
35            $matches = array();
36
37            // '/^\s*([^\[|]+)(?:\[(?:([^,\]]*),)?([^,\]]*)\])?(?:\s*(?:\|\s*(.*))?)?$/mD'
38            if (preg_match('/(.*)\[(.*)\]$/', trim($url), $matches)) {
39                $url = $matches[1];
40                if (strpos($matches[2],',') !== false) {
41                    @list($w, $h) = explode(',',$matches[2],2);
42                } else {
43                    $h = $matches[2];
44                    $w = '98%';
45                }
46            } else {
47                $w = '98%';
48                $h = '600';
49            }
50            if (!isset($alt)) $alt = '';
51
52            if (!$this->getConf('js_ok') && substr($url,0,11) == 'javascript:') {
53                return array('error', $this->getLang('gcal_No_JS'));
54            }
55            return array('wiki', hsc(trim("$url")), hsc(trim($alt)), hsc(trim($w)), hsc(trim($h)));
56        } else {
57            return array('error', $this->getLang("gcal_Bad_iFrame"));  // this is an error
58        } // matched {{cal>...
59    }
60
61    function render($mode, Doku_Renderer $renderer, $data) {
62        list($style, $url, $alt, $w, $h) = $data;
63
64        if($mode == 'xhtml'){
65            // Two styles: wiki and error
66            switch($style) {
67                case 'wiki':
68                    $renderer->doc .= "<iframe src='https://www.google.com/calendar/embed?src=$url&amp;height=$h&amp;title=$alt'".
69                "title='$alt'  width='$w' height='$h' frameborder='0'></iframe>\n";
70                    break;
71                case 'error':
72                    $renderer->doc .= "<div class='error'>$url</div>";
73                    break;
74                default:
75                    $renderer->doc .= "<div class='error'>" . $this->getLang('gcal_Invalid_mode') . "</div>";
76                    break;
77            }
78            return true;
79        }
80        return false;
81    }
82}
83