1<?php
2/**
3 * DokuWiki Plugin flexpaper (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Ethen Guo <Ethen.Guo@Gmail.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11if(!defined('DOKU_LF')) define('DOKU_LF',"\n");
12
13class syntax_plugin_flexpaper extends DokuWiki_Syntax_Plugin {
14    /**
15     * @return string Syntax mode type
16     */
17    public function getType() {
18        //return 'FIXME: container|baseonly|formatting|substition|protected|disabled|paragraphs';
19        return 'container';
20    }
21    /**
22     * @return string Paragraph type
23     */
24    public function getPType() {
25        //return 'FIXME: normal|block|stack';
26        return 'normal';
27    }
28    /**
29     * @return int Sort order - Low numbers go before high numbers
30     */
31    public function getSort() {
32        //return FIXME;
33        return 305;
34    }
35
36    /**
37     * Connect lookup pattern to lexer.
38     *
39     * @param string $mode Parser mode
40     */
41    public function connectTo($mode) {
42        //$this->Lexer->addSpecialPattern('<FIXME>',$mode,'plugin_flexpaper');
43        $this->Lexer->addSpecialPattern('\{\{flexpaper>.*?\}\}',$mode,'plugin_flexpaper');
44    }
45
46//    public function postConnect() {
47//        $this->Lexer->addExitPattern('</flex>','plugin_flexpaper');
48//    }
49
50    /**
51     * Handle matches of the flexpaper syntax
52     *
53     * @param string $match The match of the syntax
54     * @param int    $state The state of the handler
55     * @param int    $pos The position in the document
56     * @param Doku_Handler    $handler The handler
57     * @return array Data for the renderer
58     */
59    public function handle($match, $state, $pos, Doku_Handler &$handler){
60
61		# 提取匹配的文件名与参数
62		$match = trim(substr($match, 12, -2));
63		list($file, $params) = explode('?',$match);
64
65		# 检查是否是支持的文件类型 .swf, .pdf
66		if(!preg_match('/.swf$|.pdf$/i', $file)) {
67			$data['error'] = 'File <b>"<i>' . $file . '</i>"</b> is <b>NOT SUPPORT!</b>';
68			return $data;
69		}
70
71		# 检查文件是否存在
72		if(!file_exists(mediaFN($file))) {
73			$data['error'] = 'File <b>"<i>' . $file . '</i>"</b> is <b>NOT EXIST!</b>';
74			return $data;
75		}
76
77		# Default width, height and stratpage
78		$data['width'] = '100%';
79		$data['height'] = '588px';
80		$data['start'] = '';
81
82		# 分别处理 .swf 与 .pdf
83		if(preg_match('/.swf$/i', $file)) {
84			$data['swf'] = $file;
85		} else {
86			# 检查系统函数 exec 是否可用?
87			if(!function_exists('exec')) {
88				# 系统函数 exec 不可用,返回错误消息
89				$data['error'] = 'PHP Function <b>"<a href="http://php.net/manual/en/function.exec.php"><i>exec()</i></a>"</b> is <b>NOT EXIST!</b> Please contact your server administrator.';
90				return $data;
91			}
92
93			# 处理 .pdf: 用 SWFTOOLS 把 .pdf 转成 .swf 格式
94			# pdf2swf [-options] file.pdf -o file.swf
95			$swf = $file . '.swf';
96			# swf 文件是否存在,不存在则执行 pdf 转 swf 命令;存在则可以直接显示
97			if(!file_exists(mediaFN($swf))) {
98				# 需执行的命令
99				$command = '"' . DOKU_PLUGIN . 'flexpaper/SWFTools/pdf2swf" -f -T 9 "' . mediaFN($file) . '" -o "' . mediaFN($swf) . '"';
100				# 执行命令
101				$lastline = exec(escapeshellcmd($command),$output,$status);
102				# 如果命令执行失败,返回命令行最后一行消息
103				if($status) {
104					$data['error'] = $lastline;
105					return $data;
106				}
107				# .pdf 转换 .swf 文件成功
108			}
109			$data['swf'] = $swf;
110		}
111
112		# handle Parameters(width, height and stratpage)
113		$params = explode(';',$params);
114		if($params) {
115			foreach($params as $param) {
116				list($key, $value) = explode(':',$param);
117				switch($key) {
118					case 'width' :
119						$data[$key] = $value;
120						break;
121					case 'height' :
122						$data[$key] = $value;
123						break;
124					case 'start' :
125						$data[$key] = $value;
126						break;
127				}
128			}
129		}
130
131		# handle configuration of flexpaper
132		if(file_exists(mediaFN($foobar))) {
133			$data['Scale'] = $this->getConf('Scale');
134			$data['ZoomTransition'] = "'" . $this->getConf('ZoomTransition') . "'";
135			$data['ZoomTime'] = $this->getConf('ZoomTime');
136			$data['ZoomInterval'] = $this->getConf('ZoomInterval');
137			$data['FitPageOnLoad'] = $this->getConf('FitPageOnLoad')?'true':'false';
138			$data['FitWidthOnLoad'] = $this->getConf('FitWidthOnLoad')?'true':'false';
139
140			$data['FullScreenAsMaxWindow'] = $this->getConf('FullScreenAsMaxWindow')?'true':'false';
141			$data['ProgressiveLoading'] = $this->getConf('ProgressiveLoading')?'true':'false';
142			$data['MaxZoomSize'] = $this->getConf('MaxZoomSize');
143			$data['MinZoomSize'] = $this->getConf('MinZoomSize');
144			$data['SearchMatchAll'] = $this->getConf('SearchMatchAll')?'true':'false';
145			$data['InitViewMode'] = "'" . $this->getConf('InitViewMode') . "'";
146			$data['PrintPaperAsBitmap'] = $this->getConf('PrintPaperAsBitmap')?'true':'false';
147
148			$data['ViewModeToolsVisible'] = $this->getConf('ViewModeToolsVisible')?'true':'false';
149			$data['ZoomToolsVisible'] = $this->getConf('ZoomToolsVisible')?'true':'false';
150			$data['NavToolsVisible'] = $this->getConf('NavToolsVisible')?'true':'false';
151			$data['CursorToolsVisible'] = $this->getConf('CursorToolsVisible')?'true':'false';
152			$data['SearchToolsVisible'] = $this->getConf('SearchToolsVisible')?'true':'false';
153			$data['localeChain'] = "'" . $this->getConf('localeChain') . "'";
154		}
155		return $data;
156    }
157
158    /**
159     * Render xhtml output or metadata
160     *
161     * @param string         $mode      Renderer mode (supported modes: xhtml)
162     * @param Doku_Renderer  $renderer  The renderer
163     * @param array          $data      The data from the handler() function
164     * @return bool If rendering was successful.
165     */
166    public function render($mode, Doku_Renderer &$renderer, $data) {
167        if($mode != 'xhtml') return false;
168
169		if($data['error']) {
170			$renderer->doc .= '<div id="documentViewer" class="plugin_flexpaper" style="background: #FFB; width: 100%">' . $data['error'] . '</div>' . DOKU_LF;
171			return false;
172		}
173
174#		$renderer->doc .= '<div class="plugin_flexpaper" style="background: #DFD">' . DOKU_LF;
175		$renderer->doc .= '<div id="documentViewer" class="plugin_flexpaper" style="background: #DFD; width:' . $data['width'] . '; height:' . $data['height'] . '">' . mediaFN($data['swf']) . '</div>' . DOKU_LF;
176		$renderer->doc .= '<script type="text/javascript">/*<![CDATA[*/' . DOKU_LF;
177		$renderer->doc .= 'jQuery(\'#documentViewer\').FlexPaperViewer(' . DOKU_LF;
178		$renderer->doc .= '    { config : {' . DOKU_LF;
179		$renderer->doc .= '        jsDirectory : \'' . DOKU_REL . 'lib/plugins/flexpaper/FlexPaper/js/\',' . DOKU_LF;
180		$renderer->doc .= '        SWFFile : escape(\'../..' . DOKU_REL . 'lib/exe/fetch.php?media=' . $data['swf'] . '\'),' . DOKU_LF;
181
182		$renderer->doc .= '        Scale : ' . $data['Scale'] . ',' . DOKU_LF;
183		$renderer->doc .= '        ZoomTransition : ' . $data['ZoomTransition'] .',' . DOKU_LF;
184		$renderer->doc .= '        ZoomTime : ' . $data['ZoomTime'] . ',' . DOKU_LF;
185		$renderer->doc .= '        ZoomInterval : ' . $data['ZoomInterval'] . ',' . DOKU_LF;
186		$renderer->doc .= '        FitPageOnLoad : ' . $data['FitPageOnLoad'] . ',' . DOKU_LF;
187		$renderer->doc .= '        FitWidthOnLoad : ' . $data['FitWidthOnLoad'] . ',' . DOKU_LF;
188
189		$renderer->doc .= '        FullScreenAsMaxWindow : ' . $data['FullScreenAsMaxWindow'] . ',' . DOKU_LF;
190		$renderer->doc .= '        ProgressiveLoading : ' . $data['ProgressiveLoading'] . ',' . DOKU_LF;
191		$renderer->doc .= '        MaxZoomSize : ' . $data['MaxZoomSize'] . ',' . DOKU_LF;
192		$renderer->doc .= '        MinZoomSize : ' . $data['MinZoomSize'] . ',' . DOKU_LF;
193		$renderer->doc .= '        SearchMatchAll : ' . $data['SearchMatchAll'] . ',' . DOKU_LF;
194		$renderer->doc .= '        InitViewMode : ' . $data['InitViewMode'] . ',' . DOKU_LF;
195		$renderer->doc .= '        PrintPaperAsBitmap : ' . $data['PrintPaperAsBitmap'] . ',' . DOKU_LF;
196
197		$renderer->doc .= '        ViewModeToolsVisible : ' . $data['ViewModeToolsVisible'] . ',' . DOKU_LF;
198		$renderer->doc .= '        ZoomToolsVisible : ' . $data['ZoomToolsVisible'] . ',' . DOKU_LF;
199		$renderer->doc .= '        NavToolsVisible : ' . $data['NavToolsVisible'] . ',' . DOKU_LF;
200		$renderer->doc .= '        CursorToolsVisible : ' . $data['CursorToolsVisible'] . ',' . DOKU_LF;
201		$renderer->doc .= '        SearchToolsVisible : ' . $data['SearchToolsVisible'] . ',' . DOKU_LF;
202		$renderer->doc .= '        RenderingOrder : \'flash,html\',' . DOKU_LF;
203		$renderer->doc .= '        StartAtPage : \'' . $data['start'] . '\',' . DOKU_LF;
204
205#		$renderer->doc .= '        foobar : ' . $data['foobar'] . ',' . DOKU_LF;
206		$renderer->doc .= '        localeChain : ' . $data['localeChain'] . DOKU_LF;
207		$renderer->doc .= '    }}' . DOKU_LF;
208		$renderer->doc .= ');' . DOKU_LF;
209		$renderer->doc .= '/*!]]>*/</script>' . DOKU_LF;
210#		$renderer->doc .= '</div>' . DOKU_LF;
211
212        return true;
213    }
214}
215
216// vim:ts=4:sw=4:et:
217