1<?php
2/**
3 * This syntax plugin overwrites the defaultdokuwiki image rendering. It uses the fileid
4 * from the owncloud-database to link the image, and don't uses the imagepath.
5 * The Image also provides the imagebox from the imagebox-plugin written by
6 * FFTiger <fftiger@wikisquare.com>, myst6re <myst6re@wikisquare.com>. see
7 * https://www.dokuwiki.org/plugin:imagebox for the original code. The style.css and
8 * most code for the thumbnails from the imagebox-plugin is used here.
9
10 * @license    GPL 3 (http://www.gnu.org/licenses/gpl.html)
11 * @author     Martin Schulte <lebowski[at]corvus[dot]uberspace[dot]de>, 2013
12
13 */
14
15if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
16if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
17if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
18if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
19
20require_once(DOKU_PLUGIN.'syntax.php');
21
22class syntax_plugin_owncloud extends DokuWiki_Syntax_Plugin {
23
24	function getInfo(){
25		return array(
26			'author' => 'Martin Schulte',
27			'email'  => 'lebowski[at]corvus[dot]uberspace[dot]de',
28			'date'   => '2013-05-31',
29			'name'   => 'ownCloud Plugin',
30			'desc'   => 'Uses ownClouds fileID instead filepathes'
31		);
32	}
33
34	function getType(){
35		return 'substition';
36	}
37
38	function getAllowedTypes() {
39		return array('substition','disabled','formatting');
40	}
41
42	function getSort(){
43		return 319;  // before Dokuwiki-media-parser (320)
44	}
45	function getPType(){
46		return 'normal';
47	}
48
49	function connectTo($mode) {
50		$this->Lexer->addSpecialPattern("\{\{[^\}]+\}\}",$mode,'plugin_owncloud');
51		$this->Lexer->addSpecialPattern("\[\{\{[^\}]+\}\}\]",$mode,'plugin_owncloud');
52
53	}
54
55	function handle($match, $state, $pos, &$handler){
56		$imagebox =false;
57		$rawdata = $match;
58		if(preg_match('#\[(.*)\]#',$match,$inside)){
59			$match = $inside[1];
60			$imagebox = true;
61		}
62		$match= Doku_Handler_Parse_Media($match);
63		$match['imagebox'] = $imagebox;
64		$match['pos'] = $pos;
65		$match['raw'] = $rawdata;
66		return array($match, $state, $pos);
67	}
68
69	function render($mode, &$renderer, $data){
70		list($match, $state, $pos) = $data;
71		$helper = $this->loadHelper('owncloud',false);
72		if(!$helper) return false;
73		if($match['type']=='internalmedia'){
74			$match['fileid']=0;
75			if(preg_match('#fileid=(\d+)?#i',$match['raw'],$fileid)){
76				 ($fileid[1]) ? $match['fileid'] = $fileid[1]:"";
77				 // Because Doku_Handler_Parse_Media interprets fileid=40 as width 40
78				 if($match['fileid'] == $match['width']){
79					 // No width given
80					//if(!preg_match('#[\?|&]'.$match['width'].'[&|\|| ]#',$match['raw'])) $match['width'] = NULL;
81					//else{
82						$match['width'] = NULL;
83						if(preg_match('#[\?|&](\d+)(x(\d+))?#i',$match['raw'],$size)){
84							($size[1])?$match['width'] = $size[1]:$match['width'] = NULL;
85							($size[3])?$match['height'] = $size[3]:$match['height'] = NULL;
86						}
87					//}
88				 }
89			}
90			else $match['fileid'] = $helper->fileIDForWikiID($match['src']);
91		}
92		$opener = '';
93		$closer = '';
94		if($match['type'] != 'internalmedia') $match['title'] .= ' ('.$this->getLang('source').': '.$match['src'].')';
95		if($match['imagebox']){
96			$this->handleImageBox($match,$helper);
97			$match['linking'] = 'details'; // Detail when click on image, enlarge if click on magnify
98			list($opener,$closer) = $this->buildImagebox($match);
99			$match['align'] = 'box2'; // overwrite class to mediabox2, alignment from thumb.
100		}
101		$renderer->doc.=$opener;
102		if($match['type']!='internalmedia'){
103			if($helper->isAllowedExternalImage($match['src'])) $renderer->doc.= $helper->externalmedia($match['src'], $match['title'], $match['align'], $match['width'],$match['height'], $match['cache'], $match['linking']);
104		}else{
105			$renderer->doc.=  $helper->internalmedia($match['fileid'],$match['src'], $match['title'], $match['align'], $match['width'],$match['height'], $match['cache'], $match['linking']);
106		}
107		$renderer->doc.=$closer;
108		return true;
109	}
110
111
112	/**
113    * Expands the return from Doku_Handler_Parse_Media with settings for an imagebox.
114    * This code based on the code from the imagebox-plugin (https://www.dokuwiki.org/plugin:imagebox)
115    * written by FFTiger <fftiger@wikisquare.com> and myst6re <myst6re@wikisquare.com>
116    * licensed under the GPL 2 (http://www.gnu.org/licenses/gpl.html)
117    *
118    * @param $match return from Doku_Handler_Parse_Media()
119    *
120    */
121	function handleImageBox(&$match,&$helper){// Detail immer, M
122		$match['w'] = $match['width'];
123		$dispMagnify = ($match['w'] || $match['height']);
124		$gimgs = false;
125		list($src,$hash) = explode('#',$match['src'],2);
126		if($match['type']=='internalmedia') {
127			$exists = false;
128			resolve_mediaid(getNS($ID), $src, $exists);
129			$match['magnifyLink'] = $helper->ml($src,array('cache'=>$match['cache'],'fileid'=>$match['fileid']),true);
130			if($hash) $match['magnifyLink'] .= '#'.$hash;
131			if($exists)	$gimgs = @getImageSize(mediaFN($src));
132		}else{
133			$match['magnifyLink'] = ml($src,array('cache'=>'cache'),true);
134			if($hash) $match['detail'] .= '#'.$hash;
135			$gimgs = @getImageSize($src);
136		}
137		$match['exist'] = $gimgs!==false;
138		if(!$match['w'] && $match['exist']){
139				if($match['height']){
140					$match['w'] = $match['height']*$gimgs[0]/$gimgs[1];
141				}else{
142					$match['w'] = $gimgs[0];
143				}
144		}
145		if(!$match['align']) $match['align'] = 'rien';
146	}
147
148
149	/**
150    * Builds the div's arround an image to get an imagebox. This code based
151    * on the code from the imagebox-plugin (https://www.dokuwiki.org/plugin:imagebox)
152    * written by FFTiger <fftiger@wikisquare.com> and myst6re <myst6re@wikisquare.com>
153    * licensed under the GPL 2 (http://www.gnu.org/licenses/gpl.html)
154    *
155    * @param $url match (return from Doku_Handler_Parse_Media() + handleImageBox())
156    * @return @openAndClose Array with two elements: the opening div's and
157    *                       the closing div's
158    */
159	public function buildImagebox($match){
160		$opener  = '<div class="thumb2 t'.$match['align'].'" style="width:'.($match['w']?($match['w']+10).'px':'auto').';"><div class="thumbinner">';
161		$closer  = '<div class="thumbcaption">';
162		$closer .= '<div class="magnify">';
163		$closer .= '<a class="internal" title="'.$this->getLang('enlarge').'" href="'.$match['magnifyLink'].'">';
164		$closer .= '<img width="15" height="11" alt="" src="'.DOKU_BASE.'lib/plugins/owncloud/images/magnify-clip.png"></img>';
165		$closer .= '</a></div>';
166
167		$style=$this->getConf('default_caption_style');
168		if($style=='Italic')	$closer .= '<em>'.htmlspecialchars($match['title']).'</em>';
169		elseif($style=='Bold')	$closer .= '<strong>'.htmlspecialchars($match['title']).'</strong>';
170		else 					$closer .= '<span>'.htmlspecialchars($match['title']).'</span>';
171
172		$closer .= '</div></div></div>';
173		return array($opener,$closer);
174	}
175}
176