xref: /plugin/iframeinterwiki/syntax.php (revision e00731e6af5e4d3d3f1281871ad7b3cd14864179)
1<?php
2/**
3 * Plugin Iframeinterwiki: Inserts an iframe element to include the specified url
4 * Based on Iframe plugin written by Christopher Smith
5 * include interwiki urls
6 *
7 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author     Steve Roques <steve.roques@gmail.com>
9 */
10 // must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12
13if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
14require_once(DOKU_PLUGIN.'syntax.php');
15
16/**
17 * All DokuWiki plugins to extend the parser/rendering mechanism
18 * need to inherit from this class
19 */
20class syntax_plugin_iframeinterwiki extends DokuWiki_Syntax_Plugin {
21
22    function getType() { return 'substition'; }
23    function getSort() { return 305; }
24    function connectTo($mode) { $this->Lexer->addSpecialPattern('{{url>.*?}}',$mode,'plugin_iframeinterwiki'); }
25
26    function handle($match, $state, $pos, Doku_Handler $handler){
27        $match = substr($match, 6, -2);
28		list($url, $alt) = explode('|',$match,2);
29		list($url, $param) = explode(' ',$url,2);
30
31        // javascript pseudo uris allowed?
32        if (!$this->getConf('js_ok') && substr($url,0,11) == 'javascript:'){
33            $url = false;
34        }
35
36        // set defaults
37        $opts = array(
38                    'url'    => $url,
39                    'width'  => '98%',
40                    'height' => '400px',
41                    'alt'    => $alt,
42                    'scroll' => true,
43                    'border' => true,
44                    'align'  => false,
45                );
46
47        // handle size parameters
48        $matches=array();
49        if(preg_match('/\[?(\d+(em|%|pt|px)?)\s*([,xX]\s*(\d+(em|%|pt|px)?))?\]?/',$param,$matches)){
50            if($matches[4]){
51                // width and height was given
52                $opts['width'] = $matches[1];
53                if(!$matches[2]) $opts['width'] .= 'px'; //default to pixel when no unit was set
54                $opts['height'] = $matches[4];
55                if(!$matches[5]) $opts['height'] .= 'px'; //default to pixel when no unit was set
56            }elseif($matches[2]){
57                // only height was given
58                $opts['height'] = $matches[1];
59                if(!$matches[2]) $opts['height'] .= 'px'; //default to pixel when no unit was set
60            }
61        }
62
63        // handle other parameters
64        if(preg_match('/noscroll(bars?|ing)?/',$param)){
65            $opts['scroll'] = false;
66        }
67        if(preg_match('/no(frame)?border/',$param)){
68            $opts['border'] = false;
69        }
70        if(preg_match('/(left|right)/',$param,$matches)){
71            $opts['align'] = $matches[1];
72        }
73
74        return $opts;
75    }
76
77	function parse_uri_inter($url, $filename) {
78		$file = fopen($filename, 'r');
79		while (($line = fgets($file)) !== false) {
80			$line = preg_replace("/\#.+/", '', trim($line));
81			preg_match("/^\s*?(.+?)\s+(.+?)$/i", $line, $matches);
82			if (isset($matches[1], $matches[2]) && str_contains($url, $matches[1])) {
83				$url = str_replace($matches[1] . '>', '@REPLACE@', $url);
84				if(str_contains($matches[2], '{NAME}')) {
85					$url = str_replace('@REPLACE@' , str_replace('{NAME}', '', $matches[2]), $url);
86				} else if(str_contains($matches[2], '{URL}')) {
87					$url = str_replace('%40REPLACE%40', str_replace('{URL}', '', $matches[2]), urlencode($url));
88				}
89			}
90		}
91		fclose($file);
92		return $url;
93	}
94
95    function render($mode, Doku_Renderer $R, $data) {
96        if($mode != 'xhtml') return false;
97
98        if(!$data['url']) {
99            $R->doc .= '<div class="iframe">'.hsc($data['alt']).'</div>';
100        } else {
101
102
103            $opts = array(
104                        'title' => $data['alt'],
105                        'src'   => $this->parse_uri_inter($this->parse_uri_inter($data['url'], DOKU_INC . 'conf/interwiki.local.conf'), DOKU_INC . 'conf/interwiki.conf'),
106                        'style' => 'width:'.$data['width'].'; height:'.$data['height'],
107                        );
108            if(!$data['border']) $opts['frameborder'] = 0;
109            if(!$data['scroll']) $opts['scrolling'] = 'no';
110            if($data['align'])   $opts['align'] = $data['align'];
111            $params = buildAttributes($opts);
112			if(!isset($alt)) {
113				$alt = '';
114			}
115            $R->doc .= "<iframe $params>".hsc($alt).'</iframe>';
116        }
117
118        return true;
119    }
120}
121