xref: /plugin/dw2pdf/DokuImageProcessorDecorator.php (revision 852931daed0aa7c73fc4da5d421d2c117decf509)
132ff69b6SAndreas Gohr<?php
232ff69b6SAndreas Gohr
332ff69b6SAndreas Gohrnamespace dokuwiki\plugin\dw2pdf;
432ff69b6SAndreas Gohr
5*852931daSAndreas Gohruse Mpdf\Image\ImageProcessor;
632ff69b6SAndreas Gohr
7*852931daSAndreas Gohrclass DokuImageProcessorDecorator extends ImageProcessor
8*852931daSAndreas 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     */
17*852931daSAndreas Gohr    public function getImage(
18*852931daSAndreas Gohr        &$file,
19*852931daSAndreas Gohr        $firsttime = true,
20*852931daSAndreas Gohr        $allowvector = true,
21*852931daSAndreas Gohr        $orig_srcpath = false,
22*852931daSAndreas Gohr        $interpolation = false
23*852931daSAndreas Gohr    ) {
24*852931daSAndreas 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
30*852931daSAndreas Gohr    public static function adjustGetImageLinks($file, $orig_srcpath)
31*852931daSAndreas 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
39*852931daSAndreas Gohr        if (
40*852931daSAndreas Gohr            preg_match("/^$re/", $file, $m) ||
4199b67893SAndreas Gohr            preg_match('/[&?]media=([^&?]*)/', $file, $m)
4232ff69b6SAndreas Gohr        ) {
4332ff69b6SAndreas Gohr            $media = rawurldecode($m[1]);
44*852931daSAndreas Gohr            [$ext, $mime] = mimetype($media);
4532ff69b6SAndreas Gohr        } else {
46*852931daSAndreas 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?
63*852931daSAndreas Gohr                $w = 0;
64*852931daSAndreas Gohr                $h = 0;
6532ff69b6SAndreas Gohr                $rev = '';
6699b67893SAndreas Gohr                if (preg_match('/[?&]w=(\d+)/', $file, $m)) $w = $m[1];
6799b67893SAndreas Gohr                if (preg_match('/[?&]h=(\d+)/', $file, $m)) $h = $m[1];
6899b67893SAndreas Gohr                if (preg_match('/[&?]rev=(\d+)/', $file, $m)) $rev = $m[1];
6932ff69b6SAndreas Gohr
7032ff69b6SAndreas Gohr                if (media_isexternal($media)) {
7132ff69b6SAndreas Gohr                    $local = media_get_from_URL($media, $ext, -1);
7232ff69b6SAndreas Gohr                    if (!$local) $local = $media; // let mpdf try again
7332ff69b6SAndreas Gohr                } else {
7432ff69b6SAndreas Gohr                    $media = cleanID($media);
7532ff69b6SAndreas Gohr                    //check permissions (namespace only)
7632ff69b6SAndreas Gohr                    if (auth_quickaclcheck(getNS($media) . ':X') < AUTH_READ) {
7732ff69b6SAndreas Gohr                        $file = '';
7832ff69b6SAndreas Gohr                        $local = '';
7932ff69b6SAndreas Gohr                    } else {
8032ff69b6SAndreas Gohr                        $local = mediaFN($media, $rev);
8132ff69b6SAndreas Gohr                    }
8232ff69b6SAndreas Gohr                }
8332ff69b6SAndreas Gohr
8432ff69b6SAndreas Gohr                //handle image resizing/cropping
8532ff69b6SAndreas Gohr                if ($w && file_exists($local)) {
8632ff69b6SAndreas Gohr                    if ($h) {
8732ff69b6SAndreas Gohr                        $local = media_crop_image($local, $ext, $w, $h);
8832ff69b6SAndreas Gohr                    } else {
8932ff69b6SAndreas Gohr                        $local = media_resize_image($local, $ext, $w, $h);
9032ff69b6SAndreas Gohr                    }
9132ff69b6SAndreas Gohr                }
9232ff69b6SAndreas Gohr            } elseif (!file_exists($local) && media_isexternal($file)) { // fixed external URLs
9332ff69b6SAndreas Gohr                $local = media_get_from_URL($file, $ext, $conf['cachetime']);
9432ff69b6SAndreas Gohr            }
9532ff69b6SAndreas Gohr
9632ff69b6SAndreas Gohr            if ($local) {
9732ff69b6SAndreas Gohr                $file = $local;
9832ff69b6SAndreas Gohr                $orig_srcpath = $local;
9932ff69b6SAndreas Gohr            }
10032ff69b6SAndreas Gohr        }
10132ff69b6SAndreas Gohr
10232ff69b6SAndreas Gohr        return [$file, $orig_srcpath];
10332ff69b6SAndreas Gohr    }
10432ff69b6SAndreas Gohr}
105