1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     FFTiger <fftiger@wikisquare.com>, myst6re <myst6re@wikisquare.com>
5 */
6
7if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
8if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
9require_once(DOKU_PLUGIN.'syntax.php');
10
11class syntax_plugin_imagebox extends DokuWiki_Syntax_Plugin {
12
13	function getType(){ return 'protected'; }
14	function getAllowedTypes() { return array('substition','protected','disabled','formatting'); }
15	function getSort(){ return 315; }
16	function getPType(){ return 'block'; }
17	function connectTo($mode) {	$this->Lexer->addEntryPattern('\[\{\{[^\|\}]+\|*(?=[^\}]*\}\}\])',$mode,'plugin_imagebox'); }
18	function postConnect() { $this->Lexer->addExitPattern('\}\}\]','plugin_imagebox'); }
19
20	function handle($match, $state, $pos, Doku_Handler $handler){
21		switch($state){
22			case DOKU_LEXER_ENTER:
23				$match=Doku_Handler_Parse_Media(substr($match,3));
24
25                $dispMagnify = ($match['width'] || $match['height']) && $this->getConf('display_magnify')=='If necessary' || $this->getConf('display_magnify')=='Always';
26
27				$image_size = false;
28
29                $result = explode('#',$match['src'],2);
30                if (isset($result[0])){
31                    $src = $result[0];
32                }
33                if(isset($result[1])){
34                    $hash = $result[1];
35                }
36
37                if(!empty($src)) {
38                    if ($match['type'] == 'internalmedia') {
39                        global $ID;
40                        $exists = false;
41                        resolve_mediaid(getNS($ID), $src, $exists);
42
43                        if ($dispMagnify) {
44                            $match['detail'] = ml($src, array('id' => $ID, 'cache' => $match['cache']), ($match['linking'] == 'direct'));
45                            if (isset($hash)) $match['detail'] .= '#' . $hash;
46                        }
47
48                        if ($exists){
49                            $image_size = @getImageSize(mediaFN($src));
50                            if(function_exists('mime_content_type')) {
51                                $mime_type = explode('/', mime_content_type(mediaFN($src)), 2);
52                            }
53                        }
54                    } else {
55                        if ($dispMagnify) {
56                            $match['detail'] = ml($src, array('cache' => 'cache'), false);
57                            if (isset($hash)) $match['detail'] .= '#' . $hash;
58                        }
59
60                        $image_size = @getImageSize($src);
61                        if(function_exists('mime_content_type')) {
62                            $mime_type = explode('/', mime_content_type($src), 2);
63                        }
64                    }
65                }
66
67				$match['exist'] = ($image_size!==false || (isset($mime_type[0]) && $mime_type[0] == "image")) ;
68
69                if($match['exist']){
70                    if(is_null($match['width']) && is_null($match['height'])) {
71                        $match['width'] = $image_size[0];
72                        $match['height'] = $image_size[1];
73                    }else {
74                        if (is_null($match['width'])) {
75                            $match['width'] = round($match['height'] * $image_size[0] / $image_size[1]);
76                        }
77                        if (is_null($match['height'])) {
78                            $match['height'] = round($match['width'] * $image_size[1] / $image_size[0]);
79                        }
80                    }
81                }
82
83				if(!$match['align'] /*|| $match['align']=='center'*/&&!$this->getConf('center_align'))
84					$match['align'] = 'rien';
85			return array($state,$match);
86
87			case DOKU_LEXER_UNMATCHED:
88			return array($state,$match);
89
90			case DOKU_LEXER_EXIT:
91			return array($state,$match);
92		}
93	}
94
95	function render($mode, Doku_Renderer $renderer, $data){
96		global $ID;
97
98        if($mode == 'metadata'){
99			list($state, $match) = $data;
100
101			switch($state){
102				case DOKU_LEXER_ENTER:
103					list ($src) = explode('#', $match['src'], 2);
104
105					// Use dokuwiki image metadata renderer
106					if(media_isexternal($src)) {
107						$renderer->externalmedia($src, $match['title']);
108					} else {
109						resolve_mediaid(getNS($ID), $src, $exists);
110						$renderer->internalmedia($src, $match['title']);
111
112						// Clear previous persistent data
113						if (isset($renderer->persistent['relation']['media'][$src])) {
114							unset($renderer->persistent['relation']['media'][$src]);
115						}
116					}
117
118            }
119        }
120
121
122		if($mode == 'xhtml'){
123			list($state,$match) = $data;
124
125			switch($state){
126				case DOKU_LEXER_ENTER:
127					$renderer->doc.= '<div class="thumb2 t'.$match['align'].'"><div class="thumbinner">';
128					if($match['exist'])
129						$renderer->{$match['type']}($match['src'],$match['title'],'box2',$match['width'],$match['height'],$match['cache'],$match['linking']);
130					else
131						$renderer->doc.= 'Invalid Link';
132					$renderer->doc.= '<div class="thumbcaption" style="max-width: '.($match['width']-6).'px">';
133					if(isset($match['detail'])) {
134						$renderer->doc.= '<div class="magnify">';
135						$renderer->doc.= '<a class="internal" title="'.$this->getLang('enlarge').'" href="'.$match['detail'].'" target="_blank">';
136						$renderer->doc.= '<img width="15" height="11" alt="" src="'.DOKU_BASE.'lib/plugins/imagebox/magnify-clip.png"/>';
137						$renderer->doc.= '</a></div>';
138					}
139				break;
140
141				case DOKU_LEXER_UNMATCHED:
142					$style=$this->getConf('default_caption_style');
143					if($style=='Italic')	$renderer->doc .= '<em>'.$renderer->_xmlEntities($match).'</em>';
144					elseif($style=='Bold')	$renderer->doc .= '<strong>'.$renderer->_xmlEntities($match).'</strong>';
145					else 					$renderer->doc .= $renderer->_xmlEntities($match);
146				break;
147
148				case DOKU_LEXER_EXIT:
149					$renderer->doc.= '</div></div></div>';
150				break;
151			}
152			return true;
153		}
154		return false;
155	}
156}
157
158//Setup VIM: ex: et ts=4 enc=utf-8 :
159