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