xref: /plugin/dw2pdf/DokuPDF.class.php (revision ac654589b116aafb9d29d261a887239e67fd7aee)
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'].'/dwpdf/'.rand(1000));
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    }
26
27    /**
28     * Decode all paths, since DokuWiki uses XHTML compliant URLs
29     */
30    function GetFullPath(&$path,$basepath=''){
31        $path = htmlspecialchars_decode($path);
32        parent::GetFullPath($path, $basepath);
33    }
34
35    /**
36     * Override the mpdf _getImage function
37     *
38     * This function takes care of gathering the image data from HTTP or
39     * local files before passing the data back to mpdf's original function
40     * making sure that only cached file paths are passed to mpdf. It also
41     * takes care of checking image ACls.
42     */
43    function _getImage(&$file, $firsttime=true, $allowvector=true, $orig_srcpath=false){
44        list($ext,$mime) = mimetype($file);
45
46        // build regex to parse URL back to media info
47        $re = preg_quote(ml('xxx123yyy','',true,'&',true),'/');
48        $re = str_replace('xxx123yyy','([^&\?]*)',$re);
49
50        // extract the real media from a fetch.php uri and determine mime
51        if(preg_match("/^$re/",$file,$m) ||
52            preg_match('/[&\?]media=([^&\?]*)/',$file,$m)){
53            $media = rawurldecode($m[1]);
54            list($ext,$mime) = mimetype($media);
55        }else{
56            list($ext,$mime) = mimetype($file);
57        }
58
59        // local files
60        if(substr($file,0,9) == 'dw2pdf://'){
61            // support local files passed from plugins
62            $local = substr($file,9);
63        }elseif(!preg_match('/(\.php|\?)/',$file)){
64            $re = preg_quote(DOKU_URL,'/');
65            // directly access local files instead of using HTTP, skip dynamic content
66            $local = preg_replace("/^$re/i",DOKU_INC,$file);
67        }
68
69        if(substr($mime,0,6) == 'image/'){
70            if(!empty($media)){
71                // any size restrictions?
72                if(preg_match('/[\?&]w=(\d+)/',$file, $m)) $w = $m[1];
73                if(preg_match('/[\?&]h=(\d+)/',$file, $m)) $h = $m[1];
74
75                if(preg_match('/^https?:\/\//',$media)){
76                    $local = media_get_from_URL($media,$ext,-1);
77                    if(!$local) $local = $media; // let mpdf try again
78                }else{
79                    $media = cleanID($media);
80                    //check permissions (namespace only)
81                    if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ){
82                        $file = '';
83                    }
84                    $local  = mediaFN($media);
85                }
86
87                //handle image resizing/cropping
88                if($w && file_exists($local)){
89                    if($h){
90                        $local = media_crop_image($local,$ext,$w,$h);
91                    }else{
92                        $local = media_resize_image($local,$ext,$w,$h);
93                    }
94                }
95            }elseif(preg_match('/^https?:\/\//',$file)){ // fixed external URLs
96                $local = media_get_from_URL($file,$ext,$conf['cachetime']);
97            }
98
99            if($local){
100                $file = $local;
101                $orig_srcpath = $local;
102            }
103        }
104
105        return parent::_getImage($file, $firsttime, $allowvector, $orig_srcpath);
106    }
107
108}
109