xref: /plugin/dw2pdf/DokuPDF.class.php (revision 1ef68647990e8a746109cdd0e22aa1774399dc3c)
1<?php
2
3require_once(dirname(__FILE__)."/mpdf/mpdf.php");
4
5class DokuPDF extends mpdf {
6
7    function __construct(){
8        global $conf;
9        if(!defined('_MPDF_TEMP_PATH')) define("_MPDF_TEMP_PATH", $conf['tmpdir']);
10
11        // we're always UTF-8
12        parent::__construct('UTF-8-s');
13        $this->SetAutoFont(AUTOFONT_ALL);
14        $this->ignore_invalid_utf8 = true;
15
16        // allimage sources are local (see _getImage)
17        $this->basepathIsLocal;
18    }
19
20
21    /**
22     * Override the mpdf _getImage function
23     *
24     * This function takes care of gathering the image data from HTTP or
25     * local files before passing the data back to mpdf's original function
26     * making sure that only cached file paths are passed to mpdf. It also
27     * takes care of checking image ACls.
28     */
29    function _getImage(&$file, $firsttime=true, $allowvector=true, $orig_srcpath=false){
30        global $conf;
31
32        list($ext,$mime) = mimetype($file);
33        if(substr($mime,0,6) == 'image/'){
34            // build regex to parse URL back to media info
35            $re = preg_quote(ml('xxx123yyy'),'/');
36            $re = str_replace('xxx123yyy','([^&?]*)',$re);
37
38            if(preg_match('/^https?:\/\//',$file)){ // fixed external URLs
39                $local = media_get_from_URL($file,$ext,$conf['cachetime']);
40            }elseif(preg_match("/$re/",$file,$m)){  // media files
41                $media = rawurldecode($m[1]);
42                if(preg_match('/[?&]w=(\d+)/',$file, $m)) $w = $m[1];
43                if(preg_match('/[?&]h=(\d+)/',$file, $m)) $h = $m[1];
44
45                if(preg_match('/^https?:\/\//',$file)){
46                    $local = media_get_from_URL($media,$ext,$conf['cachetime']);
47                }else{
48                    $media = cleanID($media);
49                    //check permissions (namespace only)
50                    if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ){
51                        $file = '';
52                    }
53                    $local  = mediaFN($media);
54                }
55
56                //handle image resizing/cropping
57                if($w){
58                    if($w){
59                        $local = media_crop_image($local,$ext,$w,$h);
60                    }else{
61                        $local = media_resize_image($local,$ext,$w,$h);
62                    }
63                }
64            }
65
66            if($local){
67                $file = $local;
68                $orig_srcpath = $local;
69            }
70        }
71
72        return parent::_getImage($file, $firsttime, $allowvector, $orig_srcpath);
73    }
74
75}
76