1<?php
2
3namespace dokuwiki\plugin\dw2pdf;
4
5require_once __DIR__ . '/vendor/autoload.php';
6
7class DokuImageProcessorDecorator extends \Mpdf\Image\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 (&$file, $firsttime = true, $allowvector = true, $orig_srcpath = false, $interpolation = false) {
18        list($file, $orig_srcpath) = self::adjustGetImageLinks($file, $orig_srcpath);
19
20        return parent::getImage($file, $firsttime, $allowvector, $orig_srcpath, $interpolation);
21    }
22
23
24    public static function adjustGetImageLinks($file, $orig_srcpath) {
25        global $conf;
26
27        // build regex to parse URL back to media info
28        $re = preg_quote(ml('xxx123yyy', '', true, '&', true), '/');
29        $re = str_replace('xxx123yyy', '([^&\?]*)', $re);
30
31        // extract the real media from a fetch.php uri and determine mime
32        if(preg_match("/^$re/", $file, $m) ||
33            preg_match('/[&?]media=([^&?]*)/', $file, $m)
34        ) {
35            $media = rawurldecode($m[1]);
36            list($ext, $mime) = mimetype($media);
37        } else {
38            list($ext, $mime) = mimetype($file);
39        }
40
41        // local files
42        $local = '';
43        if(substr($file, 0, 9) == 'dw2pdf://') {
44            // support local files passed from plugins
45            $local = substr($file, 9);
46        } elseif(!preg_match('/(\.php|\?)/', $file)) {
47            $re = preg_quote(DOKU_URL, '/');
48            // directly access local files instead of using HTTP, skip dynamic content
49            $local = preg_replace("/^$re/i", DOKU_INC, $file);
50        }
51
52        if(substr($mime, 0, 6) == 'image/') {
53            if(!empty($media)) {
54                // any size restrictions?
55                $w = $h = 0;
56                $rev = '';
57                if(preg_match('/[?&]w=(\d+)/', $file, $m)) $w = $m[1];
58                if(preg_match('/[?&]h=(\d+)/', $file, $m)) $h = $m[1];
59                if(preg_match('/[&?]rev=(\d+)/', $file, $m)) $rev = $m[1];
60
61                if(media_isexternal($media)) {
62                    $local = media_get_from_URL($media, $ext, -1);
63                    if(!$local) $local = $media; // let mpdf try again
64                } else {
65                    $media = cleanID($media);
66                    //check permissions (namespace only)
67                    if(auth_quickaclcheck(getNS($media) . ':X') < AUTH_READ) {
68                        $file = '';
69                        $local = '';
70                    } else {
71                        $local = mediaFN($media, $rev);
72                    }
73                }
74
75                //handle image resizing/cropping
76                if($w && file_exists($local)) {
77                    if($h) {
78                        $local = media_crop_image($local, $ext, $w, $h);
79                    } else {
80                        $local = media_resize_image($local, $ext, $w, $h);
81                    }
82                }
83            } elseif(!file_exists($local) && media_isexternal($file)) { // fixed external URLs
84                $local = media_get_from_URL($file, $ext, $conf['cachetime']);
85            }
86
87            if($local) {
88                $file = $local;
89                $orig_srcpath = $local;
90            }
91        }
92
93        return [$file, $orig_srcpath];
94    }
95}
96