132ff69b6SAndreas Gohr<?php 232ff69b6SAndreas Gohr 332ff69b6SAndreas Gohrnamespace dokuwiki\plugin\dw2pdf; 432ff69b6SAndreas Gohr 5852931daSAndreas Gohruse Mpdf\Image\ImageProcessor; 632ff69b6SAndreas Gohr 7852931daSAndreas Gohrclass DokuImageProcessorDecorator extends ImageProcessor 8852931daSAndreas Gohr{ 932ff69b6SAndreas Gohr /** 1032ff69b6SAndreas Gohr * Override the mpdf _getImage function 1132ff69b6SAndreas Gohr * 1232ff69b6SAndreas Gohr * This function takes care of gathering the image data from HTTP or 1332ff69b6SAndreas Gohr * local files before passing the data back to mpdf's original function 1432ff69b6SAndreas Gohr * making sure that only cached file paths are passed to mpdf. It also 1532ff69b6SAndreas Gohr * takes care of checking image ACls. 1632ff69b6SAndreas Gohr */ 17852931daSAndreas Gohr public function getImage( 18852931daSAndreas Gohr &$file, 19852931daSAndreas Gohr $firsttime = true, 20852931daSAndreas Gohr $allowvector = true, 21852931daSAndreas Gohr $orig_srcpath = false, 22852931daSAndreas Gohr $interpolation = false 23852931daSAndreas Gohr ) { 24852931daSAndreas Gohr [$file, $orig_srcpath] = self::adjustGetImageLinks($file, $orig_srcpath); 2532ff69b6SAndreas Gohr 2632ff69b6SAndreas Gohr return parent::getImage($file, $firsttime, $allowvector, $orig_srcpath, $interpolation); 2732ff69b6SAndreas Gohr } 2832ff69b6SAndreas Gohr 2932ff69b6SAndreas Gohr 30852931daSAndreas Gohr public static function adjustGetImageLinks($file, $orig_srcpath) 31852931daSAndreas Gohr { 3232ff69b6SAndreas Gohr global $conf; 3332ff69b6SAndreas Gohr 3432ff69b6SAndreas Gohr // build regex to parse URL back to media info 3532ff69b6SAndreas Gohr $re = preg_quote(ml('xxx123yyy', '', true, '&', true), '/'); 3632ff69b6SAndreas Gohr $re = str_replace('xxx123yyy', '([^&\?]*)', $re); 3732ff69b6SAndreas Gohr 3832ff69b6SAndreas Gohr // extract the real media from a fetch.php uri and determine mime 39852931daSAndreas Gohr if ( 40852931daSAndreas Gohr preg_match("/^$re/", $file, $m) || 4199b67893SAndreas Gohr preg_match('/[&?]media=([^&?]*)/', $file, $m) 4232ff69b6SAndreas Gohr ) { 4332ff69b6SAndreas Gohr $media = rawurldecode($m[1]); 44852931daSAndreas Gohr [$ext, $mime] = mimetype($media); 4532ff69b6SAndreas Gohr } else { 46852931daSAndreas Gohr [$ext, $mime] = mimetype($file); 4732ff69b6SAndreas Gohr } 4832ff69b6SAndreas Gohr 4932ff69b6SAndreas Gohr // local files 5032ff69b6SAndreas Gohr $local = ''; 5132ff69b6SAndreas Gohr if (substr($file, 0, 9) == 'dw2pdf://') { 5232ff69b6SAndreas Gohr // support local files passed from plugins 5332ff69b6SAndreas Gohr $local = substr($file, 9); 5432ff69b6SAndreas Gohr } elseif (!preg_match('/(\.php|\?)/', $file)) { 5532ff69b6SAndreas Gohr $re = preg_quote(DOKU_URL, '/'); 5632ff69b6SAndreas Gohr // directly access local files instead of using HTTP, skip dynamic content 5732ff69b6SAndreas Gohr $local = preg_replace("/^$re/i", DOKU_INC, $file); 5832ff69b6SAndreas Gohr } 5932ff69b6SAndreas Gohr 6032ff69b6SAndreas Gohr if (substr($mime, 0, 6) == 'image/') { 6132ff69b6SAndreas Gohr if (!empty($media)) { 6232ff69b6SAndreas Gohr // any size restrictions? 63852931daSAndreas Gohr $w = 0; 64852931daSAndreas Gohr $h = 0; 6532ff69b6SAndreas Gohr $rev = ''; 66c4c95814SKlap-in if (preg_match('/[?&]w=(\d+)/', $file, $m)) { 67c4c95814SKlap-in $w = $m[1]; 68c4c95814SKlap-in } 69c4c95814SKlap-in if (preg_match('/[?&]h=(\d+)/', $file, $m)) { 70c4c95814SKlap-in $h = $m[1]; 71c4c95814SKlap-in } 72c4c95814SKlap-in if (preg_match('/[&?]rev=(\d+)/', $file, $m)) { 73c4c95814SKlap-in $rev = $m[1]; 74c4c95814SKlap-in } 7532ff69b6SAndreas Gohr 7632ff69b6SAndreas Gohr if (media_isexternal($media)) { 7732ff69b6SAndreas Gohr $local = media_get_from_URL($media, $ext, -1); 78c4c95814SKlap-in if (!$local) { 79*3cfec9f1SGerrit Uitslag // let mpdf try again 80c4c95814SKlap-in $local = $media; 81*3cfec9f1SGerrit Uitslag } 8232ff69b6SAndreas Gohr } else { 8332ff69b6SAndreas Gohr $media = cleanID($media); 8432ff69b6SAndreas Gohr //check permissions (namespace only) 8532ff69b6SAndreas Gohr if (auth_quickaclcheck(getNS($media) . ':X') < AUTH_READ) { 8632ff69b6SAndreas Gohr $file = ''; 8732ff69b6SAndreas Gohr $local = ''; 8832ff69b6SAndreas Gohr } else { 8932ff69b6SAndreas Gohr $local = mediaFN($media, $rev); 9032ff69b6SAndreas Gohr } 9132ff69b6SAndreas Gohr } 9232ff69b6SAndreas Gohr 9332ff69b6SAndreas Gohr //handle image resizing/cropping 9432ff69b6SAndreas Gohr if ($w && file_exists($local)) { 9532ff69b6SAndreas Gohr if ($h) { 9632ff69b6SAndreas Gohr $local = media_crop_image($local, $ext, $w, $h); 9732ff69b6SAndreas Gohr } else { 9832ff69b6SAndreas Gohr $local = media_resize_image($local, $ext, $w, $h); 9932ff69b6SAndreas Gohr } 10032ff69b6SAndreas Gohr } 10132ff69b6SAndreas Gohr } elseif (!file_exists($local) && media_isexternal($file)) { // fixed external URLs 10232ff69b6SAndreas Gohr $local = media_get_from_URL($file, $ext, $conf['cachetime']); 10332ff69b6SAndreas Gohr } 10432ff69b6SAndreas Gohr 10532ff69b6SAndreas Gohr if ($local) { 10632ff69b6SAndreas Gohr $file = $local; 10732ff69b6SAndreas Gohr $orig_srcpath = $local; 10832ff69b6SAndreas Gohr } 10932ff69b6SAndreas Gohr } 11032ff69b6SAndreas Gohr 11132ff69b6SAndreas Gohr return [$file, $orig_srcpath]; 11232ff69b6SAndreas Gohr } 11332ff69b6SAndreas Gohr} 114