xref: /plugin/dw2pdf/DokuPDF.class.php (revision 1c9fae9eda14b27299d8f89b406a1d73eade9c42)
11ef68647SAndreas Gohr<?php
25db42babSAndreas Gohr/**
35db42babSAndreas Gohr * Wrapper around the mpdf library class
45db42babSAndreas Gohr *
55db42babSAndreas Gohr * This class overrides some functions to make mpdf make use of DokuWiki'
65db42babSAndreas Gohr * standard tools instead of its own.
75db42babSAndreas Gohr *
85db42babSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
95db42babSAndreas Gohr */
101ef68647SAndreas Gohr
11*1c9fae9eSAndreas Gohrif(!defined('_MPDF_TEMP_PATH')) define('_MPDF_TEMP_PATH', $conf['tmpdir'].'/dwpdf/'.rand(1,1000).'/');
12b2853a2aSAndreas Gohrif(!defined('_MPDF_TTFONTDATAPATH')) define('_MPDF_TTFONTDATAPATH',$conf['cachedir'].'/mpdf_ttf/');
131ef68647SAndreas Gohrrequire_once(dirname(__FILE__)."/mpdf/mpdf.php");
141ef68647SAndreas Gohr
151ef68647SAndreas Gohrclass DokuPDF extends mpdf {
161ef68647SAndreas Gohr
171ef68647SAndreas Gohr    function __construct(){
18b2853a2aSAndreas Gohr        io_mkdir_p(_MPDF_TTFONTDATAPATH);
19*1c9fae9eSAndreas Gohr        io_mkdir_p(_MPDF_TEMP_PATH);
201ef68647SAndreas Gohr
211ef68647SAndreas Gohr        // we're always UTF-8
221ef68647SAndreas Gohr        parent::__construct('UTF-8-s');
231ef68647SAndreas Gohr        $this->SetAutoFont(AUTOFONT_ALL);
241ef68647SAndreas Gohr        $this->ignore_invalid_utf8 = true;
25*1c9fae9eSAndreas Gohr    }
261ef68647SAndreas Gohr
27*1c9fae9eSAndreas Gohr    /**
28*1c9fae9eSAndreas Gohr     * Cleanup temp dir
29*1c9fae9eSAndreas Gohr     */
30*1c9fae9eSAndreas Gohr    function __destruct(){
31*1c9fae9eSAndreas Gohr        $this->deletedir(_MPDF_TEMP_PATH);
32*1c9fae9eSAndreas Gohr    }
33*1c9fae9eSAndreas Gohr
34*1c9fae9eSAndreas Gohr    /**
35*1c9fae9eSAndreas Gohr     * Recursively delete a directory and its contents
36*1c9fae9eSAndreas Gohr     *
37*1c9fae9eSAndreas Gohr     * @link http://de3.php.net/manual/en/function.rmdir.php#108113
38*1c9fae9eSAndreas Gohr     */
39*1c9fae9eSAndreas Gohr    function deletedir($dir){
40*1c9fae9eSAndreas Gohr        foreach(glob($dir . '/*') as $file) {
41*1c9fae9eSAndreas Gohr            if(is_dir($file))
42*1c9fae9eSAndreas Gohr                $this->deletedir($file);
43*1c9fae9eSAndreas Gohr            else
44*1c9fae9eSAndreas Gohr                @unlink($file);
45*1c9fae9eSAndreas Gohr        }
46*1c9fae9eSAndreas Gohr        @rmdir($dir);
471ef68647SAndreas Gohr    }
481ef68647SAndreas Gohr
49a06728a6SAndreas Gohr    /**
50a06728a6SAndreas Gohr     * Decode all paths, since DokuWiki uses XHTML compliant URLs
51a06728a6SAndreas Gohr     */
52a06728a6SAndreas Gohr    function GetFullPath(&$path,$basepath=''){
53a06728a6SAndreas Gohr        $path = htmlspecialchars_decode($path);
54a06728a6SAndreas Gohr        parent::GetFullPath($path, $basepath);
55a06728a6SAndreas Gohr    }
561ef68647SAndreas Gohr
571ef68647SAndreas Gohr    /**
581ef68647SAndreas Gohr     * Override the mpdf _getImage function
591ef68647SAndreas Gohr     *
601ef68647SAndreas Gohr     * This function takes care of gathering the image data from HTTP or
611ef68647SAndreas Gohr     * local files before passing the data back to mpdf's original function
621ef68647SAndreas Gohr     * making sure that only cached file paths are passed to mpdf. It also
631ef68647SAndreas Gohr     * takes care of checking image ACls.
641ef68647SAndreas Gohr     */
651ef68647SAndreas Gohr    function _getImage(&$file, $firsttime=true, $allowvector=true, $orig_srcpath=false){
661ef68647SAndreas Gohr        list($ext,$mime) = mimetype($file);
67bfbc63f7SAndreas Gohr
681ef68647SAndreas Gohr        // build regex to parse URL back to media info
69eac16b1dSAndreas Gohr        $re = preg_quote(ml('xxx123yyy','',true,'&',true),'/');
7008b99b84SAndreas Gohr        $re = str_replace('xxx123yyy','([^&\?]*)',$re);
711ef68647SAndreas Gohr
72bfbc63f7SAndreas Gohr        // extract the real media from a fetch.php uri and determine mime
73eac16b1dSAndreas Gohr        if(preg_match("/^$re/",$file,$m) ||
7408b99b84SAndreas Gohr            preg_match('/[&\?]media=([^&\?]*)/',$file,$m)){
751ef68647SAndreas Gohr            $media = rawurldecode($m[1]);
76bfbc63f7SAndreas Gohr            list($ext,$mime) = mimetype($media);
77bfbc63f7SAndreas Gohr        }else{
78bfbc63f7SAndreas Gohr            list($ext,$mime) = mimetype($file);
79bfbc63f7SAndreas Gohr        }
801ef68647SAndreas Gohr
815aa96d34SAndreas Gohr        // local files
825aa96d34SAndreas Gohr        if(substr($file,0,9) == 'dw2pdf://'){
835aa96d34SAndreas Gohr            // support local files passed from plugins
845aa96d34SAndreas Gohr            $local = substr($file,9);
855aa96d34SAndreas Gohr        }elseif(!preg_match('/(\.php|\?)/',$file)){
865aa96d34SAndreas Gohr            $re = preg_quote(DOKU_URL,'/');
875aa96d34SAndreas Gohr            // directly access local files instead of using HTTP, skip dynamic content
885aa96d34SAndreas Gohr            $local = preg_replace("/^$re/i",DOKU_INC,$file);
895aa96d34SAndreas Gohr        }
905aa96d34SAndreas Gohr
91bfbc63f7SAndreas Gohr        if(substr($mime,0,6) == 'image/'){
92bfbc63f7SAndreas Gohr            if(!empty($media)){
93bfbc63f7SAndreas Gohr                // any size restrictions?
9408b99b84SAndreas Gohr                if(preg_match('/[\?&]w=(\d+)/',$file, $m)) $w = $m[1];
9508b99b84SAndreas Gohr                if(preg_match('/[\?&]h=(\d+)/',$file, $m)) $h = $m[1];
9608b99b84SAndreas Gohr
9708b99b84SAndreas Gohr                if(preg_match('/^https?:\/\//',$media)){
9808b99b84SAndreas Gohr                    $local = media_get_from_URL($media,$ext,-1);
9908b99b84SAndreas Gohr                    if(!$local) $local = $media; // let mpdf try again
1001ef68647SAndreas Gohr                }else{
1011ef68647SAndreas Gohr                    $media = cleanID($media);
1021ef68647SAndreas Gohr                    //check permissions (namespace only)
1031ef68647SAndreas Gohr                    if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ){
1041ef68647SAndreas Gohr                        $file = '';
1051ef68647SAndreas Gohr                    }
1061ef68647SAndreas Gohr                    $local  = mediaFN($media);
1071ef68647SAndreas Gohr                }
1081ef68647SAndreas Gohr
1091ef68647SAndreas Gohr                //handle image resizing/cropping
110ecaaf7b9SAndreas Gohr                if($w && file_exists($local)){
11108b99b84SAndreas Gohr                    if($h){
1121ef68647SAndreas Gohr                        $local = media_crop_image($local,$ext,$w,$h);
1131ef68647SAndreas Gohr                    }else{
1141ef68647SAndreas Gohr                        $local = media_resize_image($local,$ext,$w,$h);
1151ef68647SAndreas Gohr                    }
1161ef68647SAndreas Gohr                }
11708b99b84SAndreas Gohr            }elseif(preg_match('/^https?:\/\//',$file)){ // fixed external URLs
11808b99b84SAndreas Gohr                $local = media_get_from_URL($file,$ext,$conf['cachetime']);
1191ef68647SAndreas Gohr            }
1201ef68647SAndreas Gohr
1211ef68647SAndreas Gohr            if($local){
122a06728a6SAndreas Gohr                $file = $local;
1231ef68647SAndreas Gohr                $orig_srcpath = $local;
1241ef68647SAndreas Gohr            }
1251ef68647SAndreas Gohr        }
1261ef68647SAndreas Gohr
1271ef68647SAndreas Gohr        return parent::_getImage($file, $firsttime, $allowvector, $orig_srcpath);
1281ef68647SAndreas Gohr    }
1291ef68647SAndreas Gohr
1301ef68647SAndreas Gohr}
131