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