xref: /plugin/mediathumbnails/syntax.php (revision b8b45234ad77bff2d27f31496b1dfd41d21356c4)
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		// this is where thumbnails are stored within an odt file (which essentially is a .zip file named .odt)
63		$thumbnail_path = "Thumbnails/thumbnail.png";
64		$thumbnail_ending = strrchr($thumbnail_path,'.');
65
66		$mediapath_file = substr($match, 12, -2); //strip markup
67
68		$filepath_local_file = mediaFN($mediapath_file);
69
70		$zip = new ZipArchive;
71
72		if ($zip->open($filepath_local_file) !== TRUE) {
73			// odt file does not exist
74			return array();
75		}
76
77		if ($zip->locateName($thumbnail_path) !== false) {
78			// thumbnail file exists
79			$fp = $zip->getStream($thumbnail_path);
80			if(!$fp) {
81				return array();
82			}
83
84			$thumbnaildata = '';
85			while (!feof($fp)) {
86				$thumbnaildata .= fread($fp, 8192);
87			}
88
89			fclose($fp);
90
91			// write thumbnail file to media folder
92			$filedir = dirname($filepath_local_file);
93			$filename = basename($filepath_local_file);
94			$extended_filename = substr($filename,0,strrpos($filename,'.')).".thumbnail".$thumbnail_ending;
95
96			$filepath_thumbnail = $filedir . DIRECTORY_SEPARATOR . $extended_filename;
97			file_put_contents($filepath_thumbnail, $thumbnaildata);
98
99			// give media path to renderer
100			$mediapath_thumbnail = substr($mediapath_file,0,strrpos($mediapath_file,':')) . ":" . $extended_filename;
101			return array($mediapath_file, $mediapath_thumbnail);
102		}
103
104		return array();
105    }
106
107    /**
108     * Render xhtml output or metadata
109     *
110     * @param string        $mode     Renderer mode (supported modes: xhtml)
111     * @param Doku_Renderer $renderer The renderer
112     * @param array         $data     The data from the handler() function
113     *
114     * @return bool If rendering was successful.
115     */
116    public function render($mode, Doku_Renderer $renderer, $data)
117    {
118		$mediapath_file = $data[0];
119		$mediapath_thumbnail = $data[1];
120
121        if ($mode == 'xhtml') {
122
123			$src = ml($mediapath_thumbnail,array());
124
125			$i             = array();
126			$i['width']    = $this->getConf('thumb_width');
127			//$i['height']   = '';
128			$i['title']      = $mediapath_file;
129			$i['class']    = 'tn';
130			$iatt = buildAttributes($i);
131
132			$renderer->doc .= 	'<a href="/lib/exe/fetch.php?media=' . $mediapath_file . '">' .
133								'<img src="'.$src.'" '.$iatt.' />' .
134								'</a>';
135            return true;
136
137        } elseif ($mode == 'odt') {
138
139			// TODO: yet to implement
140			$renderer->cdata("");
141			return true;
142
143		}
144
145        return false;
146    }
147}
148
149