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 $timestamp_local_file = file_exists($filepath_local_file) ? filemtime($filepath_local_file) : false; 66 67 $zip = new ZipArchive; 68 if ($zip->open($filepath_local_file) !== TRUE) { 69 // media file does not exist 70 return array($mediapath_file); 71 } 72 73 // The media file exists and acts as a zip file! 74 75 // Check all possible paths (configured in configuration key 'thumb_paths') if there is a file available 76 $thumb_paths_to_investigate = $this->getConf('thumb_paths'); 77 78 foreach($thumb_paths_to_investigate as $thumbnail_path) { 79 $thumbnail_ending = strrchr($thumbnail_path,'.'); 80 81 if ($zip->locateName($thumbnail_path) !== false) { 82 83 // The thumbnail file exists, so prepare more information, now! 84 $filedir = dirname($filepath_local_file); 85 $filename = basename($filepath_local_file); 86 $extended_filename = substr($filename,0,strrpos($filename,'.')).".thumbnail".$thumbnail_ending; 87 $filepath_thumbnail = $filedir . DIRECTORY_SEPARATOR . $extended_filename; 88 $mediapath_thumbnail = substr($mediapath_file,0,strrpos($mediapath_file,':')) . ":" . $extended_filename; 89 90 if (file_exists($filepath_thumbnail) && filemtime($filepath_thumbnail) == $timestamp_local_file) { 91 // A thumbnail file for the current file version has already been created, don't extract it again, but give the renderer all needed information! 92 return array($mediapath_file, $mediapath_thumbnail); 93 } 94 95 // Get the thumbnail file! 96 $fp = $zip->getStream($thumbnail_path); 97 if(!$fp) { 98 return array(); 99 } 100 101 $thumbnaildata = ''; 102 while (!feof($fp)) { 103 $thumbnaildata .= fread($fp, 8192); 104 } 105 106 fclose($fp); 107 108 // Write thumbnail file to media folder 109 file_put_contents($filepath_thumbnail, $thumbnaildata); 110 111 // Set timestamp to the media file's timestamp (this is used to check in later passes if the file already exists in the correct version). 112 touch($filepath_thumbnail, $timestamp_local_file); 113 114 // Give media path to renderer 115 return array($mediapath_file, $mediapath_thumbnail); 116 } 117 } 118 119 return array($mediapath_file); 120 } 121 122 /** 123 * Render xhtml output or metadata 124 * 125 * @param string $mode Renderer mode (supported modes: xhtml) 126 * @param Doku_Renderer $renderer The renderer 127 * @param array $data The data from the handler() function 128 * 129 * @return bool If rendering was successful. 130 */ 131 public function render($mode, Doku_Renderer $renderer, $data) 132 { 133 list ($mediapath_file, $mediapath_thumbnail) = $data; 134 135 if ($mode == 'xhtml') { 136 137 // check if a thumbnail file was found 138 if (!$mediapath_thumbnail) { 139 if ($this->getConf('show_no_thumb_error')) { 140 $renderer->doc .= trim($this->getConf('no_thumb_error_message')) . " " . $mediapath_file; 141 return true; 142 } else { 143 return false; 144 } 145 } 146 147 $src = ml($mediapath_thumbnail,array()); 148 149 $i = array(); 150 $i['width'] = $this->getConf('thumb_width'); 151 //$i['height'] = ''; 152 $i['title'] = $mediapath_file; 153 $i['class'] = 'tn'; 154 $iatt = buildAttributes($i); 155 156 $renderer->doc .= ($this->getConf('link_to_media_file') ? '<a href="/lib/exe/fetch.php?media=' . $mediapath_file . '">' : '') . 157 '<img src="'.$src.'" '.$iatt.' />' . 158 ($this->getConf('link_to_media_file') ? '</a>' : ''); 159 return true; 160 161 } elseif ($mode == 'odt') { 162 163 // TODO: yet to implement 164 $renderer->cdata(""); 165 return true; 166 167 } 168 169 return false; 170 } 171} 172 173