xref: /plugin/dw2pdf/DokuPDF.class.php (revision f706fbc4dbd88d2ca5005faec3fdd6d2df467c54)
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 */
10global $conf;
11if(!defined('_MPDF_TEMP_PATH')) define('_MPDF_TEMP_PATH', $conf['tmpdir'].'/dwpdf/'.rand(1,1000).'/');
12if(!defined('_MPDF_TTFONTDATAPATH')) define('_MPDF_TTFONTDATAPATH',$conf['cachedir'].'/mpdf_ttf/');
13
14require_once(dirname(__FILE__)."/mpdf/mpdf.php");
15
16/**
17 * Class DokuPDF
18 * Some DokuWiki specific extentions
19 */
20class DokuPDF extends mpdf {
21
22    function __construct($pagesize='A4', $orientation='portrait'){
23        global $conf;
24
25        io_mkdir_p(_MPDF_TTFONTDATAPATH);
26        io_mkdir_p(_MPDF_TEMP_PATH);
27
28        $format = $pagesize;
29        if($orientation == 'landscape') {
30            $format .= '-L';
31        }
32
33        switch($conf['lang']) {
34            case 'zh':
35            case 'zh-tw':
36            case 'ja':
37            case 'ko':
38                $mode = '+aCJK';
39                break;
40            default:
41                $mode = 'UTF-8-s';
42
43        }
44
45        // we're always UTF-8
46        parent::__construct($mode, $format);
47        $this->autoScriptToLang = true;
48        $this->baseScript = 1;
49        $this->autoVietnamese = true;
50        $this->autoArabic = true;
51        $this->autoLangToFont = true;
52
53        $this->ignore_invalid_utf8 = true;
54        $this->tabSpaces = 4;
55    }
56
57    /**
58     * Cleanup temp dir
59     */
60    function __destruct(){
61        io_rmdir(_MPDF_TEMP_PATH, true);
62    }
63
64    /**
65     * Decode all paths, since DokuWiki uses XHTML compliant URLs
66     */
67    function GetFullPath(&$path,$basepath=''){
68        $path = htmlspecialchars_decode($path);
69        parent::GetFullPath($path, $basepath);
70    }
71
72    /**
73     * Override the mpdf _getImage function
74     *
75     * This function takes care of gathering the image data from HTTP or
76     * local files before passing the data back to mpdf's original function
77     * making sure that only cached file paths are passed to mpdf. It also
78     * takes care of checking image ACls.
79     */
80    function _getImage(&$file, $firsttime=true, $allowvector=true, $orig_srcpath=false){
81        global $conf;
82
83        // build regex to parse URL back to media info
84        $re = preg_quote(ml('xxx123yyy','',true,'&',true),'/');
85        $re = str_replace('xxx123yyy','([^&\?]*)',$re);
86
87        // extract the real media from a fetch.php uri and determine mime
88        if(preg_match("/^$re/",$file,$m) ||
89            preg_match('/[&\?]media=([^&\?]*)/',$file,$m)){
90            $media = rawurldecode($m[1]);
91            list($ext,$mime) = mimetype($media);
92        }else{
93            list($ext,$mime) = mimetype($file);
94        }
95
96        // local files
97        $local = '';
98        if(substr($file,0,9) == 'dw2pdf://'){
99            // support local files passed from plugins
100            $local = substr($file,9);
101        }elseif(!preg_match('/(\.php|\?)/',$file)){
102            $re = preg_quote(DOKU_URL,'/');
103            // directly access local files instead of using HTTP, skip dynamic content
104            $local = preg_replace("/^$re/i",DOKU_INC,$file);
105        }
106
107        if(substr($mime,0,6) == 'image/'){
108            if(!empty($media)){
109                // any size restrictions?
110                $w = $h = 0;
111                if(preg_match('/[\?&]w=(\d+)/',$file, $m)) $w = $m[1];
112                if(preg_match('/[\?&]h=(\d+)/',$file, $m)) $h = $m[1];
113
114                if(media_isexternal($media)){
115                    $local = media_get_from_URL($media,$ext,-1);
116                    if(!$local) $local = $media; // let mpdf try again
117                }else{
118                    $media = cleanID($media);
119                    //check permissions (namespace only)
120                    if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ){
121                        $file = '';
122                    }
123                    $local  = mediaFN($media);
124                }
125
126                //handle image resizing/cropping
127                if($w && file_exists($local)){
128                    if($h){
129                        $local = media_crop_image($local,$ext,$w,$h);
130                    }else{
131                        $local = media_resize_image($local,$ext,$w,$h);
132                    }
133                }
134            }elseif(media_isexternal($file)){ // fixed external URLs
135                $local = media_get_from_URL($file,$ext,$conf['cachetime']);
136            }
137
138            if($local){
139                $file = $local;
140                $orig_srcpath = $local;
141            }
142        }
143
144        return parent::_getImage($file, $firsttime, $allowvector, $orig_srcpath);
145    }
146
147}
148