xref: /plugin/dw2pdf/DokuPDF.class.php (revision 6ea88a05d080a9a9f6686e967ff0915b831ed25a)
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
17*6ea88a05SAndreas Gohr    function __construct($pagesize='A4', $orientation='portrait'){
18b2853a2aSAndreas Gohr        io_mkdir_p(_MPDF_TTFONTDATAPATH);
191c9fae9eSAndreas Gohr        io_mkdir_p(_MPDF_TEMP_PATH);
201ef68647SAndreas Gohr
21*6ea88a05SAndreas Gohr        $format = $pagesize;
22*6ea88a05SAndreas Gohr        if($orientation == 'landscape') $format .= '-L';
23*6ea88a05SAndreas Gohr
241ef68647SAndreas Gohr        // we're always UTF-8
25*6ea88a05SAndreas Gohr        parent::__construct('UTF-8-s', $format);
261ef68647SAndreas Gohr        $this->SetAutoFont(AUTOFONT_ALL);
271ef68647SAndreas Gohr        $this->ignore_invalid_utf8 = true;
282585efdfSKlap-in        $this->tabSpaces = 4;
291c9fae9eSAndreas Gohr    }
301ef68647SAndreas Gohr
311c9fae9eSAndreas Gohr    /**
321c9fae9eSAndreas Gohr     * Cleanup temp dir
331c9fae9eSAndreas Gohr     */
341c9fae9eSAndreas Gohr    function __destruct(){
351c9fae9eSAndreas Gohr        $this->deletedir(_MPDF_TEMP_PATH);
361c9fae9eSAndreas Gohr    }
371c9fae9eSAndreas Gohr
381c9fae9eSAndreas Gohr    /**
391c9fae9eSAndreas Gohr     * Recursively delete a directory and its contents
401c9fae9eSAndreas Gohr     *
411c9fae9eSAndreas Gohr     * @link http://de3.php.net/manual/en/function.rmdir.php#108113
421c9fae9eSAndreas Gohr     */
431c9fae9eSAndreas Gohr    function deletedir($dir){
441c9fae9eSAndreas Gohr        foreach(glob($dir . '/*') as $file) {
451c9fae9eSAndreas Gohr            if(is_dir($file))
461c9fae9eSAndreas Gohr                $this->deletedir($file);
471c9fae9eSAndreas Gohr            else
481c9fae9eSAndreas Gohr                @unlink($file);
491c9fae9eSAndreas Gohr        }
501c9fae9eSAndreas Gohr        @rmdir($dir);
511ef68647SAndreas Gohr    }
521ef68647SAndreas Gohr
53a06728a6SAndreas Gohr    /**
54a06728a6SAndreas Gohr     * Decode all paths, since DokuWiki uses XHTML compliant URLs
55a06728a6SAndreas Gohr     */
56a06728a6SAndreas Gohr    function GetFullPath(&$path,$basepath=''){
57a06728a6SAndreas Gohr        $path = htmlspecialchars_decode($path);
58a06728a6SAndreas Gohr        parent::GetFullPath($path, $basepath);
59a06728a6SAndreas Gohr    }
601ef68647SAndreas Gohr
611ef68647SAndreas Gohr    /**
621ef68647SAndreas Gohr     * Override the mpdf _getImage function
631ef68647SAndreas Gohr     *
641ef68647SAndreas Gohr     * This function takes care of gathering the image data from HTTP or
651ef68647SAndreas Gohr     * local files before passing the data back to mpdf's original function
661ef68647SAndreas Gohr     * making sure that only cached file paths are passed to mpdf. It also
671ef68647SAndreas Gohr     * takes care of checking image ACls.
681ef68647SAndreas Gohr     */
691ef68647SAndreas Gohr    function _getImage(&$file, $firsttime=true, $allowvector=true, $orig_srcpath=false){
702585efdfSKlap-in        global $conf;
711ef68647SAndreas Gohr        list($ext,$mime) = mimetype($file);
72bfbc63f7SAndreas Gohr
731ef68647SAndreas Gohr        // build regex to parse URL back to media info
74eac16b1dSAndreas Gohr        $re = preg_quote(ml('xxx123yyy','',true,'&',true),'/');
7508b99b84SAndreas Gohr        $re = str_replace('xxx123yyy','([^&\?]*)',$re);
761ef68647SAndreas Gohr
77bfbc63f7SAndreas Gohr        // extract the real media from a fetch.php uri and determine mime
78eac16b1dSAndreas Gohr        if(preg_match("/^$re/",$file,$m) ||
7908b99b84SAndreas Gohr            preg_match('/[&\?]media=([^&\?]*)/',$file,$m)){
801ef68647SAndreas Gohr            $media = rawurldecode($m[1]);
81bfbc63f7SAndreas Gohr            list($ext,$mime) = mimetype($media);
82bfbc63f7SAndreas Gohr        }else{
83bfbc63f7SAndreas Gohr            list($ext,$mime) = mimetype($file);
84bfbc63f7SAndreas Gohr        }
851ef68647SAndreas Gohr
865aa96d34SAndreas Gohr        // local files
875aa96d34SAndreas Gohr        if(substr($file,0,9) == 'dw2pdf://'){
885aa96d34SAndreas Gohr            // support local files passed from plugins
895aa96d34SAndreas Gohr            $local = substr($file,9);
905aa96d34SAndreas Gohr        }elseif(!preg_match('/(\.php|\?)/',$file)){
915aa96d34SAndreas Gohr            $re = preg_quote(DOKU_URL,'/');
925aa96d34SAndreas Gohr            // directly access local files instead of using HTTP, skip dynamic content
935aa96d34SAndreas Gohr            $local = preg_replace("/^$re/i",DOKU_INC,$file);
945aa96d34SAndreas Gohr        }
955aa96d34SAndreas Gohr
96bfbc63f7SAndreas Gohr        if(substr($mime,0,6) == 'image/'){
97bfbc63f7SAndreas Gohr            if(!empty($media)){
98bfbc63f7SAndreas Gohr                // any size restrictions?
9908b99b84SAndreas Gohr                if(preg_match('/[\?&]w=(\d+)/',$file, $m)) $w = $m[1];
10008b99b84SAndreas Gohr                if(preg_match('/[\?&]h=(\d+)/',$file, $m)) $h = $m[1];
10108b99b84SAndreas Gohr
1022585efdfSKlap-in                if(preg_match('/^(https?|ftp):\/\//',$media)){
10308b99b84SAndreas Gohr                    $local = media_get_from_URL($media,$ext,-1);
10408b99b84SAndreas Gohr                    if(!$local) $local = $media; // let mpdf try again
1051ef68647SAndreas Gohr                }else{
1061ef68647SAndreas Gohr                    $media = cleanID($media);
1071ef68647SAndreas Gohr                    //check permissions (namespace only)
1081ef68647SAndreas Gohr                    if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ){
1091ef68647SAndreas Gohr                        $file = '';
1101ef68647SAndreas Gohr                    }
1111ef68647SAndreas Gohr                    $local  = mediaFN($media);
1121ef68647SAndreas Gohr                }
1131ef68647SAndreas Gohr
1141ef68647SAndreas Gohr                //handle image resizing/cropping
115ecaaf7b9SAndreas Gohr                if($w && file_exists($local)){
11608b99b84SAndreas Gohr                    if($h){
1171ef68647SAndreas Gohr                        $local = media_crop_image($local,$ext,$w,$h);
1181ef68647SAndreas Gohr                    }else{
1191ef68647SAndreas Gohr                        $local = media_resize_image($local,$ext,$w,$h);
1201ef68647SAndreas Gohr                    }
1211ef68647SAndreas Gohr                }
1222585efdfSKlap-in            }elseif(preg_match('/^(https?|ftp):\/\//',$file)){ // fixed external URLs
12308b99b84SAndreas Gohr                $local = media_get_from_URL($file,$ext,$conf['cachetime']);
1241ef68647SAndreas Gohr            }
1251ef68647SAndreas Gohr
1261ef68647SAndreas Gohr            if($local){
127a06728a6SAndreas Gohr                $file = $local;
1281ef68647SAndreas Gohr                $orig_srcpath = $local;
1291ef68647SAndreas Gohr            }
1301ef68647SAndreas Gohr        }
1311ef68647SAndreas Gohr
1321ef68647SAndreas Gohr        return parent::_getImage($file, $firsttime, $allowvector, $orig_srcpath);
1331ef68647SAndreas Gohr    }
1341ef68647SAndreas Gohr
1351ef68647SAndreas Gohr}
136