1<?php
2/**
3 * Plugin Iframe: Inserts an iframe element to include the specified url
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Christopher Smith <chris@jalakai.co.uk>
7 */
8 // must be run within Dokuwiki
9if(!defined('DOKU_INC')) die();
10
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12
13/**
14 * All DokuWiki plugins to extend the parser/rendering mechanism
15 * need to inherit from this class
16 */
17class syntax_plugin_iframe extends DokuWiki_Syntax_Plugin {
18
19    function getType() { return 'substition'; }
20    function getSort() { return 305; }
21    function connectTo($mode) { $this->Lexer->addSpecialPattern('{{url>.*?}}',$mode,'plugin_iframe'); }
22
23    function handle($match, $state, $pos, Doku_Handler $handler){
24        $match = substr($match, 6, -2);
25        list($url, $alt)   = array_pad(explode('|',$match,2), 2, null);
26        list($url, $param) = explode(' ',$url,2);
27
28        // javascript pseudo uris allowed?
29        if (!$this->getConf('js_ok') && substr($url,0,11) == 'javascript:'){
30            $url = false;
31        }
32
33        // set defaults
34        $opts = array(
35                    'url'    => $url,
36                    'width'  => '98%',
37                    'height' => '400px',
38                    'alt'    => $alt,
39                    'scroll' => true,
40                    'border' => true,
41                    'align'  => false,
42                    'fullscreen' => false,
43                );
44
45        // handle size parameters
46        $matches=array();
47        if(preg_match('/\[?(\d+(em|%|pt|px)?)\s*([,xX]\s*(\d+(em|%|pt|px)?))?\]?/',$param,$matches)){
48            if(count($matches) > 3) {
49                // width and height was given
50                $opts['width'] = $matches[1];
51                if(!$matches[2]) $opts['width'] .= 'px'; //default to pixel when no unit was set
52                $opts['height'] = $matches[4];
53                if(!$matches[5]) $opts['height'] .= 'px'; //default to pixel when no unit was set
54            }elseif (count($matches) > 1) {
55                // only height was given
56                $opts['height'] = $matches[1];
57                if(!$matches[2]) $opts['height'] .= 'px'; //default to pixel when no unit was set
58            }
59        }
60
61        // handle other parameters
62        if(preg_match('/noscroll(bars?|ing)?/',$param)){
63            $opts['scroll'] = false;
64        }
65        if(preg_match('/no(frame)?border/',$param)){
66            $opts['border'] = false;
67        }
68        if(preg_match('/fullscreen/',$param)){
69            $opts['fullscreen'] = true;
70        }
71        if(preg_match('/(left|right)/',$param,$matches)){
72            $opts['align'] = $matches[1];
73        }
74
75        return $opts;
76    }
77
78    function render($mode, Doku_Renderer $R, $data) {
79        if($mode != 'xhtml') return false;
80
81        if(!$data['url']){
82            $R->doc .= '<div class="iframe">'.hsc($data['alt']).'</div>';
83        }else{
84            $opts = array(
85                        'title' => $data['alt'],
86                        'src'   => $data['url'],
87                        'style' => 'width:'.$data['width'].'; height:'.$data['height'],
88                        );
89            if(!$data['border']) $opts['frameborder'] = 0;
90            if(!$data['scroll']) $opts['scrolling'] = 'no';
91            if($data['align'])   $opts['align'] = $data['align'];
92            $params = buildAttributes($opts);
93            if($data['fullscreen']) $params .= ' allowfullscreen';
94            $R->doc .= "<iframe $params>".(isset($alt) ? hsc($alt) : '').'</iframe>';
95        }
96
97        return true;
98    }
99}
100