xref: /plugin/mediathumbnails/syntax.php (revision da77d72d8cd8b9890419d63eb74ed094799a8bbe)
1<?php
2/**
3 * DokuWiki Plugin mediathumbnails (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Thomas Schäfer <thomas.schaefer@itschert.net>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14class syntax_plugin_mediathumbnails extends DokuWiki_Syntax_Plugin {
15
16	/**
17     * @return string Syntax mode type
18     */
19    public function getType()
20    {
21        return 'substition';
22    }
23
24    /**
25     * @return string Paragraph type
26     */
27    public function getPType()
28    {
29        return 'normal';
30    }
31
32    /**
33     * @return int Sort order - Low numbers go before high numbers
34     */
35    public function getSort()
36    {
37        return 1;
38    }
39
40    /**
41     * Connect lookup pattern to lexer.
42     *
43     * @param string $mode Parser mode
44     */
45    public function connectTo($mode)
46    {
47		$this->Lexer->addSpecialPattern("{{thumbnail>.+?}}", $mode, substr(get_class($this), 7));
48	}
49
50    /**
51     * Handle matches of the mediathumbnails 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     *
58     * @return array Data for the renderer
59     */
60    public function handle($match, $state, $pos, Doku_Handler $handler)
61    {
62		// Locate the given media file and check if it can be opened as zip
63		$mediapath_file = substr($match, 12, -2); //strip markup
64		$filepath_local_file = mediaFN($mediapath_file);
65
66		$zip = new ZipArchive;
67		if ($zip->open($filepath_local_file) !== TRUE) {
68			// media file does not exist
69			return array($mediapath_file);
70		}
71
72		// Check all possible paths (configured in configuration key 'thumb_paths') if there is a file available
73		$thumb_paths_to_investigate = $this->getConf('thumb_paths');
74
75		foreach($thumb_paths_to_investigate as $thumbnail_path) {
76			$thumbnail_ending = strrchr($thumbnail_path,'.');
77
78			if ($zip->locateName($thumbnail_path) !== false) {
79				// thumbnail file exists
80				$fp = $zip->getStream($thumbnail_path);
81				if(!$fp) {
82					return array();
83				}
84
85				$thumbnaildata = '';
86				while (!feof($fp)) {
87					$thumbnaildata .= fread($fp, 8192);
88				}
89
90				fclose($fp);
91
92				// write thumbnail file to media folder
93				$filedir = dirname($filepath_local_file);
94				$filename = basename($filepath_local_file);
95				$extended_filename = substr($filename,0,strrpos($filename,'.')).".thumbnail".$thumbnail_ending;
96
97				$filepath_thumbnail = $filedir . DIRECTORY_SEPARATOR . $extended_filename;
98				file_put_contents($filepath_thumbnail, $thumbnaildata);
99
100				// give media path to renderer
101				$mediapath_thumbnail = substr($mediapath_file,0,strrpos($mediapath_file,':')) . ":" . $extended_filename;
102				return array($mediapath_file, $mediapath_thumbnail);
103			}
104		}
105
106		return array($mediapath_file);
107    }
108
109    /**
110     * Render xhtml output or metadata
111     *
112     * @param string        $mode     Renderer mode (supported modes: xhtml)
113     * @param Doku_Renderer $renderer The renderer
114     * @param array         $data     The data from the handler() function
115     *
116     * @return bool If rendering was successful.
117     */
118    public function render($mode, Doku_Renderer $renderer, $data)
119    {
120		list ($mediapath_file, $mediapath_thumbnail) = $data;
121
122        if ($mode == 'xhtml') {
123
124			// check if a thumbnail file was found
125			if (!$mediapath_thumbnail) {
126				if ($this->getConf('show_no_thumb_error')) {
127					$renderer->doc .= trim($this->getConf('no_thumb_error_message')) . " " . $mediapath_file;
128					return true;
129				} else {
130					return false;
131				}
132			}
133
134			$src = ml($mediapath_thumbnail,array());
135
136			$i             = array();
137			$i['width']    = $this->getConf('thumb_width');
138			//$i['height']   = '';
139			$i['title']      = $mediapath_file;
140			$i['class']    = 'tn';
141			$iatt = buildAttributes($i);
142
143			$renderer->doc .= 	'<a href="/lib/exe/fetch.php?media=' . $mediapath_file . '">' .
144								'<img src="'.$src.'" '.$iatt.' />' .
145								'</a>';
146            return true;
147
148        } elseif ($mode == 'odt') {
149
150			// TODO: yet to implement
151			$renderer->cdata("");
152			return true;
153
154		}
155
156        return false;
157    }
158}
159
160