xref: /plugin/dw2pdf/DokuPDF.class.php (revision 2585efdfb5b70b0fcfe1fd81bad6fc0a13f16d4b)
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
111c9fae9eSAndreas 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);
191c9fae9eSAndreas 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*2585efdfSKlap-in        $this->tabSpaces = 4;
261c9fae9eSAndreas Gohr    }
271ef68647SAndreas Gohr
281c9fae9eSAndreas Gohr    /**
291c9fae9eSAndreas Gohr     * Cleanup temp dir
301c9fae9eSAndreas Gohr     */
311c9fae9eSAndreas Gohr    function __destruct(){
321c9fae9eSAndreas Gohr        $this->deletedir(_MPDF_TEMP_PATH);
331c9fae9eSAndreas Gohr    }
341c9fae9eSAndreas Gohr
351c9fae9eSAndreas Gohr    /**
361c9fae9eSAndreas Gohr     * Recursively delete a directory and its contents
371c9fae9eSAndreas Gohr     *
381c9fae9eSAndreas Gohr     * @link http://de3.php.net/manual/en/function.rmdir.php#108113
391c9fae9eSAndreas Gohr     */
401c9fae9eSAndreas Gohr    function deletedir($dir){
411c9fae9eSAndreas Gohr        foreach(glob($dir . '/*') as $file) {
421c9fae9eSAndreas Gohr            if(is_dir($file))
431c9fae9eSAndreas Gohr                $this->deletedir($file);
441c9fae9eSAndreas Gohr            else
451c9fae9eSAndreas Gohr                @unlink($file);
461c9fae9eSAndreas Gohr        }
471c9fae9eSAndreas Gohr        @rmdir($dir);
481ef68647SAndreas Gohr    }
491ef68647SAndreas Gohr
50a06728a6SAndreas Gohr    /**
51a06728a6SAndreas Gohr     * Decode all paths, since DokuWiki uses XHTML compliant URLs
52a06728a6SAndreas Gohr     */
53a06728a6SAndreas Gohr    function GetFullPath(&$path,$basepath=''){
54a06728a6SAndreas Gohr        $path = htmlspecialchars_decode($path);
55a06728a6SAndreas Gohr        parent::GetFullPath($path, $basepath);
56a06728a6SAndreas Gohr    }
571ef68647SAndreas Gohr
581ef68647SAndreas Gohr    /**
591ef68647SAndreas Gohr     * Override the mpdf _getImage function
601ef68647SAndreas Gohr     *
611ef68647SAndreas Gohr     * This function takes care of gathering the image data from HTTP or
621ef68647SAndreas Gohr     * local files before passing the data back to mpdf's original function
631ef68647SAndreas Gohr     * making sure that only cached file paths are passed to mpdf. It also
641ef68647SAndreas Gohr     * takes care of checking image ACls.
651ef68647SAndreas Gohr     */
661ef68647SAndreas Gohr    function _getImage(&$file, $firsttime=true, $allowvector=true, $orig_srcpath=false){
67*2585efdfSKlap-in        global $conf;
681ef68647SAndreas Gohr        list($ext,$mime) = mimetype($file);
69bfbc63f7SAndreas Gohr
701ef68647SAndreas Gohr        // build regex to parse URL back to media info
71eac16b1dSAndreas Gohr        $re = preg_quote(ml('xxx123yyy','',true,'&',true),'/');
7208b99b84SAndreas Gohr        $re = str_replace('xxx123yyy','([^&\?]*)',$re);
731ef68647SAndreas Gohr
74bfbc63f7SAndreas Gohr        // extract the real media from a fetch.php uri and determine mime
75eac16b1dSAndreas Gohr        if(preg_match("/^$re/",$file,$m) ||
7608b99b84SAndreas Gohr            preg_match('/[&\?]media=([^&\?]*)/',$file,$m)){
771ef68647SAndreas Gohr            $media = rawurldecode($m[1]);
78bfbc63f7SAndreas Gohr            list($ext,$mime) = mimetype($media);
79bfbc63f7SAndreas Gohr        }else{
80bfbc63f7SAndreas Gohr            list($ext,$mime) = mimetype($file);
81bfbc63f7SAndreas Gohr        }
821ef68647SAndreas Gohr
835aa96d34SAndreas Gohr        // local files
845aa96d34SAndreas Gohr        if(substr($file,0,9) == 'dw2pdf://'){
855aa96d34SAndreas Gohr            // support local files passed from plugins
865aa96d34SAndreas Gohr            $local = substr($file,9);
875aa96d34SAndreas Gohr        }elseif(!preg_match('/(\.php|\?)/',$file)){
885aa96d34SAndreas Gohr            $re = preg_quote(DOKU_URL,'/');
895aa96d34SAndreas Gohr            // directly access local files instead of using HTTP, skip dynamic content
905aa96d34SAndreas Gohr            $local = preg_replace("/^$re/i",DOKU_INC,$file);
915aa96d34SAndreas Gohr        }
925aa96d34SAndreas Gohr
93bfbc63f7SAndreas Gohr        if(substr($mime,0,6) == 'image/'){
94bfbc63f7SAndreas Gohr            if(!empty($media)){
95bfbc63f7SAndreas Gohr                // any size restrictions?
9608b99b84SAndreas Gohr                if(preg_match('/[\?&]w=(\d+)/',$file, $m)) $w = $m[1];
9708b99b84SAndreas Gohr                if(preg_match('/[\?&]h=(\d+)/',$file, $m)) $h = $m[1];
9808b99b84SAndreas Gohr
99*2585efdfSKlap-in                if(preg_match('/^(https?|ftp):\/\//',$media)){
10008b99b84SAndreas Gohr                    $local = media_get_from_URL($media,$ext,-1);
10108b99b84SAndreas Gohr                    if(!$local) $local = $media; // let mpdf try again
1021ef68647SAndreas Gohr                }else{
1031ef68647SAndreas Gohr                    $media = cleanID($media);
1041ef68647SAndreas Gohr                    //check permissions (namespace only)
1051ef68647SAndreas Gohr                    if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ){
1061ef68647SAndreas Gohr                        $file = '';
1071ef68647SAndreas Gohr                    }
1081ef68647SAndreas Gohr                    $local  = mediaFN($media);
1091ef68647SAndreas Gohr                }
1101ef68647SAndreas Gohr
1111ef68647SAndreas Gohr                //handle image resizing/cropping
112ecaaf7b9SAndreas Gohr                if($w && file_exists($local)){
11308b99b84SAndreas Gohr                    if($h){
1141ef68647SAndreas Gohr                        $local = media_crop_image($local,$ext,$w,$h);
1151ef68647SAndreas Gohr                    }else{
1161ef68647SAndreas Gohr                        $local = media_resize_image($local,$ext,$w,$h);
1171ef68647SAndreas Gohr                    }
1181ef68647SAndreas Gohr                }
119*2585efdfSKlap-in            }elseif(preg_match('/^(https?|ftp):\/\//',$file)){ // fixed external URLs
12008b99b84SAndreas Gohr                $local = media_get_from_URL($file,$ext,$conf['cachetime']);
1211ef68647SAndreas Gohr            }
1221ef68647SAndreas Gohr
1231ef68647SAndreas Gohr            if($local){
124a06728a6SAndreas Gohr                $file = $local;
1251ef68647SAndreas Gohr                $orig_srcpath = $local;
1261ef68647SAndreas Gohr            }
1271ef68647SAndreas Gohr        }
1281ef68647SAndreas Gohr
1291ef68647SAndreas Gohr        return parent::_getImage($file, $firsttime, $allowvector, $orig_srcpath);
1301ef68647SAndreas Gohr    }
1311ef68647SAndreas Gohr
1321ef68647SAndreas Gohr}
133