1<?php
2
3namespace dokuwiki\plugin\dw2pdf;
4
5use Mpdf\Image\ImageProcessor;
6
7class DokuImageProcessorDecorator extends ImageProcessor
8{
9    /**
10     * Override the mpdf _getImage function
11     *
12     * This function takes care of gathering the image data from HTTP or
13     * local files before passing the data back to mpdf's original function
14     * making sure that only cached file paths are passed to mpdf. It also
15     * takes care of checking image ACls.
16     */
17    public function getImage(
18        &$file,
19        $firsttime = true,
20        $allowvector = true,
21        $orig_srcpath = false,
22        $interpolation = false
23    ) {
24        [$file, $orig_srcpath] = self::adjustGetImageLinks($file, $orig_srcpath);
25
26        return parent::getImage($file, $firsttime, $allowvector, $orig_srcpath, $interpolation);
27    }
28
29
30    public static function adjustGetImageLinks($file, $orig_srcpath)
31    {
32        global $conf;
33
34        // build regex to parse URL back to media info
35        $re = preg_quote(ml('xxx123yyy', '', true, '&', true), '/');
36        $re = str_replace('xxx123yyy', '([^&\?]*)', $re);
37
38        // extract the real media from a fetch.php uri and determine mime
39        if (
40            preg_match("/^$re/", $file, $m) ||
41            preg_match('/[&?]media=([^&?]*)/', $file, $m)
42        ) {
43            $media = rawurldecode($m[1]);
44            [$ext, $mime] = mimetype($media);
45        } else {
46            [$ext, $mime] = mimetype($file);
47        }
48
49        // local files
50        $local = '';
51        if (substr($file, 0, 9) == 'dw2pdf://') {
52            // support local files passed from plugins
53            $local = substr($file, 9);
54        } elseif (!preg_match('/(\.php|\?)/', $file)) {
55            $re = preg_quote(DOKU_URL, '/');
56            // directly access local files instead of using HTTP, skip dynamic content
57            $local = preg_replace("/^$re/i", DOKU_INC, $file);
58        }
59
60        if (substr($mime, 0, 6) == 'image/') {
61            if (!empty($media)) {
62                // any size restrictions?
63                $w = 0;
64                $h = 0;
65                $rev = '';
66                if (preg_match('/[?&]w=(\d+)/', $file, $m)) {
67                    $w = $m[1];
68                }
69                if (preg_match('/[?&]h=(\d+)/', $file, $m)) {
70                    $h = $m[1];
71                }
72                if (preg_match('/[&?]rev=(\d+)/', $file, $m)) {
73                    $rev = $m[1];
74                }
75
76                if (media_isexternal($media)) {
77                    $local = media_get_from_URL($media, $ext, -1);
78                    if (!$local) {
79                        // let mpdf try again
80                        $local = $media;
81                    }
82                } else {
83                    $media = cleanID($media);
84                    //check permissions (namespace only)
85                    if (auth_quickaclcheck(getNS($media) . ':X') < AUTH_READ) {
86                        $file = '';
87                        $local = '';
88                    } else {
89                        $local = mediaFN($media, $rev);
90                    }
91                }
92
93                //handle image resizing/cropping
94                if ($w && file_exists($local)) {
95                    if ($h) {
96                        $local = media_crop_image($local, $ext, $w, $h);
97                    } else {
98                        $local = media_resize_image($local, $ext, $w, $h);
99                    }
100                }
101            } elseif (!file_exists($local) && media_isexternal($file)) { // fixed external URLs
102                $local = media_get_from_URL($file, $ext, $conf['cachetime']);
103            }
104
105            if ($local) {
106                $file = $local;
107                $orig_srcpath = $local;
108            }
109        }
110
111        return [$file, $orig_srcpath];
112    }
113}
114