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 65 $thumb = new thumbnail($mediapath_file,$this); 66 if ($thumb->create()) { 67 return array($mediapath_file,$thumb->getMediapath()); 68 } 69 70 return array($mediapath_file); 71 } 72 73 /** 74 * Render xhtml output or metadata 75 * 76 * @param string $mode Renderer mode (supported modes: xhtml) 77 * @param Doku_Renderer $renderer The renderer 78 * @param array $data The data from the handler() function 79 * 80 * @return bool If rendering was successful. 81 */ 82 public function render($mode, Doku_Renderer $renderer, $data) 83 { 84 list ($mediapath_file, $mediapath_thumbnail) = $data; 85 86 if ($mode == 'xhtml') { 87 88 // check if a thumbnail file was found 89 if (!$mediapath_thumbnail) { 90 if ($this->getConf('show_missing_thumb_error')) { 91 $renderer->doc .= trim($this->getConf('no_thumb_error_message')) . " " . $mediapath_file; 92 return true; 93 } else { 94 return false; 95 } 96 } 97 98 $src = ml($mediapath_thumbnail,array()); 99 100 $i = array(); 101 $i['width'] = $this->getConf('thumb_width'); 102 //$i['height'] = ''; 103 $i['title'] = $mediapath_file; 104 $i['class'] = 'tn'; 105 $iatt = buildAttributes($i); 106 107 $renderer->doc .= ($this->getConf('link_to_media_file') ? '<a href="/lib/exe/fetch.php?media=' . $mediapath_file . '">' : '') . 108 '<img src="'.$src.'" '.$iatt.' />' . 109 ($this->getConf('link_to_media_file') ? '</a>' : ''); 110 return true; 111 112 } elseif ($mode == 'odt') { 113 114 // TODO: yet to implement 115 $renderer->cdata(""); 116 return true; 117 118 } 119 120 return false; 121 } 122} 123 124class thumbnail { 125 126 private $source_filepath; 127 private $source_mediapath; 128 private ?thumb_engine $thumb_engine = null; 129 130 public function __construct(string $source_filepath, DokuWiki_Syntax_Plugin $plugin, bool $ismediapath = true) { 131 132 if ($ismediapath) { 133 $this->source_mediapath = $source_filepath; 134 $this->source_filepath = mediaFN($source_filepath); 135 } else { 136 $this->source_mediapath = false; 137 $this->source_filepath = $source_filepath; 138 } 139 140 // Now attach the correct thumb_engine for the file type of the source file 141 //TODO: check for extension "fileinfo", then check for MIME type: if (mime_content_type($filepath_local_file) == "application/pdf") { 142 if (substr($this->source_filepath,-4) == ".pdf") { 143 $this->thumb_engine = new thumb_pdf_engine($this,$plugin->getConf('thumb_width')); 144 } else { 145 $this->thumb_engine = new thumb_zip_engine($this,$plugin->getConf('thumb_width'),$plugin->getConf('thumb_paths')); 146 } 147 } 148 149 public function create() { 150 if (!$this->thumb_engine) { 151 return false; 152 } 153 154 return $this->thumb_engine->act(); 155 } 156 157 public function getSourceFilepath() { 158 return $this->source_filepath; 159 } 160 161 protected function getFilename() { 162 163 return basename($this->source_filepath) . ".thumb.".$this->thumb_engine->getFileSuffix(); 164 } 165 166 public function getFilepath() { 167 return dirname($this->source_filepath) . DIRECTORY_SEPARATOR . $this->getFilename(); 168 } 169 170 public function getMediapath() { 171 if ($this->source_mediapath !== false) { 172 return substr($this->source_mediapath,0,strrpos($this->source_mediapath,':')) . ":" . $this->getFilename(); 173 } else { 174 return false; 175 } 176 } 177 178 public function getTimestamp() { 179 return file_exists($this->getFilepath()) ? filemtime($this->getFilepath()) : false; 180 } 181} 182 183abstract class thumb_engine { 184 185 private ?thumbnail $thumbnail = null; 186 private int $width; 187 188 public function __construct(thumbnail $thumbnail, int $width) { 189 $this->thumbnail = $thumbnail; 190 $this->width = $width; 191 } 192 193 protected function getSourceFilepath() { 194 return $this->thumbnail->getSourceFilepath(); 195 } 196 197 protected function getTargetFilepath() { 198 return $this->thumbnail->getFilepath(); 199 } 200 201 protected function getTargetWidth() { 202 return $this->width; 203 } 204 205 public function act() { 206 if ($this->act_internal()) { 207 // Set timestamp to the source file's timestamp (this is used to check in later passes if the file already exists in the correct version). 208 if (filemtime($this->getSourceFilepath()) !== filemtime($this->getTargetFilepath())) { 209 touch($this->getTargetFilepath(), filemtime($this->getSourceFilepath())); 210 } 211 return true; 212 } 213 return false; 214 } 215 216 // Checks if a thumbnail file for the current file version has already been created 217 protected function thumb_needs_update() { 218 return file_exists($this->getTargetFilepath()) && filemtime($this->getTargetFilepath()) !== filemtime($this->getSourceFilepath()); 219 } 220 221 public abstract function act_internal(); 222 223 public abstract function getFileSuffix(); 224} 225 226class thumb_pdf_engine extends thumb_engine { 227 228 public function getFileSuffix() { 229 return "jpg"; 230 } 231 232 public function act_internal() { 233 if ($this->thumb_needs_update()) { 234 $im = new imagick( $this->getSourceFilepath()."[0]" ); 235 $im->setImageColorspace(255); 236 $im->setResolution(300, 300); 237 $im->setCompressionQuality(95); 238 $im->setImageFormat('jpeg'); 239 //$im->resizeImage(substr($this->getConf('thumb_width'),-2),0,imagick::FILTER_LANCZOS,0.9); 240 $im->writeImage($this->getTargetFilepath()); 241 $im->clear(); 242 $im->destroy(); 243 244 return true; 245 } else { 246 return true; 247 } 248 } 249} 250 251class thumb_zip_engine extends thumb_engine { 252 253 private array $thumb_paths; 254 private $file_suffix = ""; 255 256 public function __construct(thumbnail $thumbnail, int $width, array $thumb_paths) { 257 parent::__construct($thumbnail,$width); 258 $this->thumb_paths = $thumb_paths; 259 } 260 261 public function getFileSuffix() { 262 return $this->file_suffix; 263 } 264 265 public function act_internal() { 266 267 $zip = new ZipArchive; 268 if ($zip->open($this->getSourceFilepath()) !== true) { 269 // file is no zip or cannot be opened 270 return false; 271 } 272 273 // The media file exists and acts as a zip file! 274 275 // Check all possible paths (configured in configuration key 'thumb_paths') if there is a file available 276 foreach($this->thumb_paths as $thumbnail_path) { 277 $this->file_suffix = substr(strrchr($thumbnail_path,'.'),1); 278 279 if ($zip->locateName($thumbnail_path) !== false) { 280 281 if (!$this->thumb_needs_update()) { 282 return true; 283 } 284 285 // Get the thumbnail file! 286 $fp = $zip->getStream($thumbnail_path); 287 if(!$fp) { 288 return false; 289 } 290 291 $thumbnaildata = ''; 292 while (!feof($fp)) { 293 $thumbnaildata .= fread($fp, 8192); 294 } 295 296 fclose($fp); 297 298 // Write thumbnail file to media folder 299 file_put_contents($this->getTargetFilepath(), $thumbnaildata); 300 301 return true; 302 } 303 } 304 305 return true; 306 } 307}