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