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 touch($this->getTargetFilepath(), filemtime($this->getSourceFilepath())); 209 return true; 210 } 211 return false; 212 } 213 214 public abstract function act_internal(); 215 216 public abstract function getFileSuffix(); 217} 218 219class thumb_pdf_engine extends thumb_engine { 220 221 public function getFileSuffix() { 222 return "jpg"; 223 } 224 225 public function act_internal() { 226 $im = new imagick( $this->getSourceFilepath()."[0]" ); 227 $im->setImageColorspace(255); 228 $im->setResolution(300, 300); 229 $im->setCompressionQuality(95); 230 $im->setImageFormat('jpeg'); 231 //$im->resizeImage(substr($this->getConf('thumb_width'),-2),0,imagick::FILTER_LANCZOS,0.9); 232 $im->writeImage($this->getTargetFilepath()); 233 $im->clear(); 234 $im->destroy(); 235 236 return true; 237 } 238} 239 240class thumb_zip_engine extends thumb_engine { 241 242 private array $thumb_paths; 243 private $file_suffix = ""; 244 245 public function __construct(thumbnail $thumbnail, int $width, array $thumb_paths) { 246 parent::__construct($thumbnail,$width); 247 $this->thumb_paths = $thumb_paths; 248 } 249 250 public function getFileSuffix() { 251 return $this->file_suffix; 252 } 253 254 public function act_internal() { 255 $zip = new ZipArchive; 256 if ($zip->open($this->getSourceFilepath()) !== true) { 257 // file is no zip or cannot be opened 258 return false; 259 } 260 $timestamp_local_file = filemtime($this->getSourceFilepath()); 261 262 // The media file exists and acts as a zip file! 263 264 // Check all possible paths (configured in configuration key 'thumb_paths') if there is a file available 265 foreach($this->thumb_paths as $thumbnail_path) { 266 $this->file_suffix = substr(strrchr($thumbnail_path,'.'),1); 267 268 if ($zip->locateName($thumbnail_path) !== false) { 269 270 if (file_exists($this->getTargetFilepath()) && filemtime($this->getTargetFilepath()) == $timestamp_local_file) { 271 // A thumbnail file for the current file version has already been created, just report that the file is in place by returning true: 272 return true; 273 } 274 275 // Get the thumbnail file! 276 $fp = $zip->getStream($thumbnail_path); 277 if(!$fp) { 278 return false; 279 } 280 281 $thumbnaildata = ''; 282 while (!feof($fp)) { 283 $thumbnaildata .= fread($fp, 8192); 284 } 285 286 fclose($fp); 287 288 // Write thumbnail file to media folder 289 file_put_contents($this->getTargetFilepath(), $thumbnaildata); 290 291 return true; 292 } 293 } 294 295 return true; 296 } 297}