xref: /plugin/dw2pdf/DokuImageProcessorDecorator.php (revision 99b678938172119a03be977293961083317b09d0)
132ff69b6SAndreas Gohr<?php
232ff69b6SAndreas Gohr
332ff69b6SAndreas Gohrnamespace dokuwiki\plugin\dw2pdf;
432ff69b6SAndreas Gohr
532ff69b6SAndreas Gohrrequire_once __DIR__ . '/vendor/autoload.php';
632ff69b6SAndreas Gohr
732ff69b6SAndreas Gohrclass DokuImageProcessorDecorator extends \Mpdf\Image\ImageProcessor {
832ff69b6SAndreas 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     */
1732ff69b6SAndreas Gohr    public function getImage (&$file, $firsttime = true, $allowvector = true, $orig_srcpath = false, $interpolation = false) {
1832ff69b6SAndreas Gohr        list($file, $orig_srcpath) = self::adjustGetImageLinks($file, $orig_srcpath);
1932ff69b6SAndreas Gohr
2032ff69b6SAndreas Gohr        return parent::getImage($file, $firsttime, $allowvector, $orig_srcpath, $interpolation);
2132ff69b6SAndreas Gohr    }
2232ff69b6SAndreas Gohr
2332ff69b6SAndreas Gohr
2432ff69b6SAndreas Gohr    public static function adjustGetImageLinks($file, $orig_srcpath) {
2532ff69b6SAndreas Gohr        global $conf;
2632ff69b6SAndreas Gohr
2732ff69b6SAndreas Gohr        // build regex to parse URL back to media info
2832ff69b6SAndreas Gohr        $re = preg_quote(ml('xxx123yyy', '', true, '&', true), '/');
2932ff69b6SAndreas Gohr        $re = str_replace('xxx123yyy', '([^&\?]*)', $re);
3032ff69b6SAndreas Gohr
3132ff69b6SAndreas Gohr        // extract the real media from a fetch.php uri and determine mime
3232ff69b6SAndreas Gohr        if(preg_match("/^$re/", $file, $m) ||
33*99b67893SAndreas Gohr            preg_match('/[&?]media=([^&?]*)/', $file, $m)
3432ff69b6SAndreas Gohr        ) {
3532ff69b6SAndreas Gohr            $media = rawurldecode($m[1]);
3632ff69b6SAndreas Gohr            list($ext, $mime) = mimetype($media);
3732ff69b6SAndreas Gohr        } else {
3832ff69b6SAndreas Gohr            list($ext, $mime) = mimetype($file);
3932ff69b6SAndreas Gohr        }
4032ff69b6SAndreas Gohr
4132ff69b6SAndreas Gohr        // local files
4232ff69b6SAndreas Gohr        $local = '';
4332ff69b6SAndreas Gohr        if(substr($file, 0, 9) == 'dw2pdf://') {
4432ff69b6SAndreas Gohr            // support local files passed from plugins
4532ff69b6SAndreas Gohr            $local = substr($file, 9);
4632ff69b6SAndreas Gohr        } elseif(!preg_match('/(\.php|\?)/', $file)) {
4732ff69b6SAndreas Gohr            $re = preg_quote(DOKU_URL, '/');
4832ff69b6SAndreas Gohr            // directly access local files instead of using HTTP, skip dynamic content
4932ff69b6SAndreas Gohr            $local = preg_replace("/^$re/i", DOKU_INC, $file);
5032ff69b6SAndreas Gohr        }
5132ff69b6SAndreas Gohr
5232ff69b6SAndreas Gohr        if(substr($mime, 0, 6) == 'image/') {
5332ff69b6SAndreas Gohr            if(!empty($media)) {
5432ff69b6SAndreas Gohr                // any size restrictions?
5532ff69b6SAndreas Gohr                $w = $h = 0;
5632ff69b6SAndreas Gohr                $rev = '';
57*99b67893SAndreas Gohr                if(preg_match('/[?&]w=(\d+)/', $file, $m)) $w = $m[1];
58*99b67893SAndreas Gohr                if(preg_match('/[?&]h=(\d+)/', $file, $m)) $h = $m[1];
59*99b67893SAndreas Gohr                if(preg_match('/[&?]rev=(\d+)/', $file, $m)) $rev = $m[1];
6032ff69b6SAndreas Gohr
6132ff69b6SAndreas Gohr                if(media_isexternal($media)) {
6232ff69b6SAndreas Gohr                    $local = media_get_from_URL($media, $ext, -1);
6332ff69b6SAndreas Gohr                    if(!$local) $local = $media; // let mpdf try again
6432ff69b6SAndreas Gohr                } else {
6532ff69b6SAndreas Gohr                    $media = cleanID($media);
6632ff69b6SAndreas Gohr                    //check permissions (namespace only)
6732ff69b6SAndreas Gohr                    if(auth_quickaclcheck(getNS($media) . ':X') < AUTH_READ) {
6832ff69b6SAndreas Gohr                        $file = '';
6932ff69b6SAndreas Gohr                        $local = '';
7032ff69b6SAndreas Gohr                    } else {
7132ff69b6SAndreas Gohr                        $local = mediaFN($media, $rev);
7232ff69b6SAndreas Gohr                    }
7332ff69b6SAndreas Gohr                }
7432ff69b6SAndreas Gohr
7532ff69b6SAndreas Gohr                //handle image resizing/cropping
7632ff69b6SAndreas Gohr                if($w && file_exists($local)) {
7732ff69b6SAndreas Gohr                    if($h) {
7832ff69b6SAndreas Gohr                        $local = media_crop_image($local, $ext, $w, $h);
7932ff69b6SAndreas Gohr                    } else {
8032ff69b6SAndreas Gohr                        $local = media_resize_image($local, $ext, $w, $h);
8132ff69b6SAndreas Gohr                    }
8232ff69b6SAndreas Gohr                }
8332ff69b6SAndreas Gohr            } elseif(!file_exists($local) && media_isexternal($file)) { // fixed external URLs
8432ff69b6SAndreas Gohr                $local = media_get_from_URL($file, $ext, $conf['cachetime']);
8532ff69b6SAndreas Gohr            }
8632ff69b6SAndreas Gohr
8732ff69b6SAndreas Gohr            if($local) {
8832ff69b6SAndreas Gohr                $file = $local;
8932ff69b6SAndreas Gohr                $orig_srcpath = $local;
9032ff69b6SAndreas Gohr            }
9132ff69b6SAndreas Gohr        }
9232ff69b6SAndreas Gohr
9332ff69b6SAndreas Gohr        return [$file, $orig_srcpath];
9432ff69b6SAndreas Gohr    }
9532ff69b6SAndreas Gohr}
96