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/');
14
15/**
16 * All DokuWiki plugins to extend the parser/rendering mechanism
17 * need to inherit from this class
18 */
19class syntax_plugin_iframeinterwiki extends DokuWiki_Syntax_Plugin {
20
21	function getType() { return 'substition'; }
22	function getSort() { return 305; }
23	function connectTo($mode) { $this->Lexer->addSpecialPattern('{{url>.*?}}',$mode,'plugin_iframeinterwiki'); }
24
25	function handle($match, $state, $pos, Doku_Handler $handler){
26		$match = substr($match, 6, -2);
27
28		$exploded = explode('|', $match, 2);
29		$url = isset($exploded[0]) ? $exploded[0] : '';
30		$alt = isset($exploded[1]) ? $exploded[1] : '';
31
32		$explodedUrl = explode(' ', $url, 2);
33		$url = isset($explodedUrl[0]) ? $explodedUrl[0] : '';
34		$param = isset($explodedUrl[1]) ? $explodedUrl[1] : '';
35
36		// javascript pseudo uris allowed?
37		if (!$this->getConf('js_ok') && substr($url,0,11) == 'javascript:'){
38			$url = false;
39		}
40
41		// set defaults
42		$opts = array(
43				'url'    => $url,
44				'width'  => '98%',
45				'height' => '400px',
46				'alt'    => $alt,
47				'scroll' => true,
48				'border' => true,
49				'align'  => false,
50			     );
51
52		// handle size parameters
53		$matches=array();
54		if(preg_match('/\[?(\d+(em|%|pt|px)?)\s*([,xX]\s*(\d+(em|%|pt|px)?))?\]?/',$param,$matches)){
55			if(array_key_exists(4, $matches) && $matches[4]){
56				// width and height was given
57				$opts['width'] = $matches[1];
58				if(!$matches[2]) $opts['width'] .= 'px'; //default to pixel when no unit was set
59				$opts['height'] = $matches[4];
60				if(!$matches[5]) $opts['height'] .= 'px'; //default to pixel when no unit was set
61			}elseif(array_key_exists(2, $matches) && $matches[2]){
62				// only height was given
63				$opts['height'] = $matches[1];
64				if(!$matches[2]) $opts['height'] .= 'px'; //default to pixel when no unit was set
65			}
66		}
67
68		// handle other parameters
69		if(preg_match('/noscroll(bars?|ing)?/',$param)){
70			$opts['scroll'] = false;
71		}
72		if(preg_match('/no(frame)?border/',$param)){
73			$opts['border'] = false;
74		}
75		if(preg_match('/(left|right)/',$param,$matches)){
76			$opts['align'] = $matches[1];
77		}
78
79		return $opts;
80	}
81
82	// Replace on url all match from interwiki conf
83	function parse_uri_inter($url, $filename) {
84		if ( file_exists($filename) && ($file = fopen($filename, "r"))!==false ) {
85			while (($line = fgets($file)) !== false) {
86				$line = preg_replace("/\#.+/", '', trim($line));
87				preg_match("/^\s*?(.+?)\s+(.+?)$/i", $line, $matches);
88				if (isset($matches[1], $matches[2]) && strpos($url, $matches[1]) !== false) {
89					$url = str_replace($matches[1] . '>', '@REPLACE@', $url);
90					if(strpos($matches[2], '{NAME}') !== false) {
91						$url = str_replace('@REPLACE@' , str_replace('{NAME}', '', $matches[2]), $url);
92					} else if(strpos($matches[2], '{URL}') !== false) {
93						$url = str_replace('%40REPLACE%40', str_replace('{URL}', '', $matches[2]), urlencode($url));
94					}
95				}
96			}
97			fclose($file);
98		}
99		return $url;
100	}
101
102	function render($mode, Doku_Renderer $R, $data) {
103		if($mode != 'xhtml') return false;
104
105		if(!$data['url']) {
106			$R->doc .= '<div class="iframe">'.hsc($data['alt']).'</div>';
107		} else {
108
109
110			$opts = array(
111					'title' => $data['alt'],
112					'src'   => $this->parse_uri_inter($this->parse_uri_inter($data['url'], DOKU_INC . 'conf/interwiki.local.conf'), DOKU_INC . 'conf/interwiki.conf'),
113					'style' => 'width:'.$data['width'].'; height:'.$data['height'],
114				     );
115			if(!$data['border']) $opts['frameborder'] = 0;
116			if(!$data['scroll']) $opts['scrolling'] = 'no';
117			if($data['align'])   $opts['align'] = $data['align'];
118			$params = buildAttributes($opts);
119			if(!isset($alt)) {
120				$alt = '';
121			}
122			$R->doc .= "<iframe $params>".hsc($alt).'</iframe>';
123		}
124
125		return true;
126	}
127}
128