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