xref: /plugin/iframeinterwiki/syntax.php (revision 0d9fa8fe13874c98baee1cbc3ca1da74068ffb62)
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	// Replace on url all match from interwiki conf
78	function parse_uri_inter($url, $filename) {
79		$file = fopen($filename, 'r');
80		while (($line = fgets($file)) !== false) {
81			$line = preg_replace("/\#.+/", '', trim($line));
82			preg_match("/^\s*?(.+?)\s+(.+?)$/i", $line, $matches);
83			if (isset($matches[1], $matches[2]) && strpos($url, $matches[1]) !== false) {
84				$url = str_replace($matches[1] . '>', '@REPLACE@', $url);
85				if(strpos($matches[2], '{NAME}') !== false) {
86					$url = str_replace('@REPLACE@' , str_replace('{NAME}', '', $matches[2]), $url);
87				} else if(strpos($matches[2], '{URL}') !== false) {
88					$url = str_replace('%40REPLACE%40', str_replace('{URL}', '', $matches[2]), urlencode($url));
89				}
90			}
91		}
92		fclose($file);
93		return $url;
94	}
95
96	function render($mode, Doku_Renderer $R, $data) {
97		if($mode != 'xhtml') return false;
98
99		if(!$data['url']) {
100			$R->doc .= '<div class="iframe">'.hsc($data['alt']).'</div>';
101		} else {
102
103
104			$opts = array(
105					'title' => $data['alt'],
106					'src'   => $this->parse_uri_inter($this->parse_uri_inter($data['url'], DOKU_INC . 'conf/interwiki.local.conf'), DOKU_INC . 'conf/interwiki.conf'),
107					'style' => 'width:'.$data['width'].'; height:'.$data['height'],
108				     );
109			if(!$data['border']) $opts['frameborder'] = 0;
110			if(!$data['scroll']) $opts['scrolling'] = 'no';
111			if($data['align'])   $opts['align'] = $data['align'];
112			$params = buildAttributes($opts);
113			if(!isset($alt)) {
114				$alt = '';
115			}
116			$R->doc .= "<iframe $params>".hsc($alt).'</iframe>';
117		}
118
119		return true;
120	}
121}
122