xref: /plugin/dw2pdf/DokuPDF.class.php (revision c1acf1fe2295e11bcd641208145d13ffe353cdda)
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 */
10*c1acf1feSGerrit Uitslagglobal $conf;
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/');
13ca0441e7SGerrit Uitslag
141ef68647SAndreas Gohrrequire_once(dirname(__FILE__)."/mpdf/mpdf.php");
151ef68647SAndreas Gohr
16ca0441e7SGerrit Uitslag/**
17ca0441e7SGerrit Uitslag * Class DokuPDF
18ca0441e7SGerrit Uitslag * Some DokuWiki specific extentions
19ca0441e7SGerrit Uitslag */
201ef68647SAndreas Gohrclass DokuPDF extends mpdf {
211ef68647SAndreas Gohr
226ea88a05SAndreas Gohr    function __construct($pagesize='A4', $orientation='portrait'){
235bd9be9cSGerrit Uitslag        global $conf;
245bd9be9cSGerrit Uitslag
25b2853a2aSAndreas Gohr        io_mkdir_p(_MPDF_TTFONTDATAPATH);
261c9fae9eSAndreas Gohr        io_mkdir_p(_MPDF_TEMP_PATH);
271ef68647SAndreas Gohr
286ea88a05SAndreas Gohr        $format = $pagesize;
296ea88a05SAndreas Gohr        if($orientation == 'landscape') $format .= '-L';
306ea88a05SAndreas Gohr
315bd9be9cSGerrit Uitslag        switch($conf['lang']) {
325bd9be9cSGerrit Uitslag            case 'zh':
335bd9be9cSGerrit Uitslag            case 'zh-tw':
345bd9be9cSGerrit Uitslag            case 'ja':
355bd9be9cSGerrit Uitslag            case 'ko':
365bd9be9cSGerrit Uitslag                $mode = '+aCJK';
375bd9be9cSGerrit Uitslag                break;
385bd9be9cSGerrit Uitslag            default:
395bd9be9cSGerrit Uitslag                $mode = 'UTF-8-s';
405bd9be9cSGerrit Uitslag
415bd9be9cSGerrit Uitslag        }
425bd9be9cSGerrit Uitslag
431ef68647SAndreas Gohr        // we're always UTF-8
445bd9be9cSGerrit Uitslag        parent::__construct($mode, $format);
451ef68647SAndreas Gohr        $this->SetAutoFont(AUTOFONT_ALL);
461ef68647SAndreas Gohr        $this->ignore_invalid_utf8 = true;
472585efdfSKlap-in        $this->tabSpaces = 4;
481c9fae9eSAndreas Gohr    }
491ef68647SAndreas Gohr
501c9fae9eSAndreas Gohr    /**
511c9fae9eSAndreas Gohr     * Cleanup temp dir
521c9fae9eSAndreas Gohr     */
531c9fae9eSAndreas Gohr    function __destruct(){
541c9fae9eSAndreas Gohr        $this->deletedir(_MPDF_TEMP_PATH);
551c9fae9eSAndreas Gohr    }
561c9fae9eSAndreas Gohr
571c9fae9eSAndreas Gohr    /**
581c9fae9eSAndreas Gohr     * Recursively delete a directory and its contents
591c9fae9eSAndreas Gohr     *
601c9fae9eSAndreas Gohr     * @link http://de3.php.net/manual/en/function.rmdir.php#108113
611c9fae9eSAndreas Gohr     */
621c9fae9eSAndreas Gohr    function deletedir($dir){
631c9fae9eSAndreas Gohr        foreach(glob($dir . '/*') as $file) {
641c9fae9eSAndreas Gohr            if(is_dir($file))
651c9fae9eSAndreas Gohr                $this->deletedir($file);
661c9fae9eSAndreas Gohr            else
671c9fae9eSAndreas Gohr                @unlink($file);
681c9fae9eSAndreas Gohr        }
691c9fae9eSAndreas Gohr        @rmdir($dir);
701ef68647SAndreas Gohr    }
711ef68647SAndreas Gohr
72a06728a6SAndreas Gohr    /**
73a06728a6SAndreas Gohr     * Decode all paths, since DokuWiki uses XHTML compliant URLs
74a06728a6SAndreas Gohr     */
75a06728a6SAndreas Gohr    function GetFullPath(&$path,$basepath=''){
76a06728a6SAndreas Gohr        $path = htmlspecialchars_decode($path);
77a06728a6SAndreas Gohr        parent::GetFullPath($path, $basepath);
78a06728a6SAndreas Gohr    }
791ef68647SAndreas Gohr
801ef68647SAndreas Gohr    /**
811ef68647SAndreas Gohr     * Override the mpdf _getImage function
821ef68647SAndreas Gohr     *
831ef68647SAndreas Gohr     * This function takes care of gathering the image data from HTTP or
841ef68647SAndreas Gohr     * local files before passing the data back to mpdf's original function
851ef68647SAndreas Gohr     * making sure that only cached file paths are passed to mpdf. It also
861ef68647SAndreas Gohr     * takes care of checking image ACls.
871ef68647SAndreas Gohr     */
881ef68647SAndreas Gohr    function _getImage(&$file, $firsttime=true, $allowvector=true, $orig_srcpath=false){
892585efdfSKlap-in        global $conf;
90bfbc63f7SAndreas Gohr
911ef68647SAndreas Gohr        // build regex to parse URL back to media info
92eac16b1dSAndreas Gohr        $re = preg_quote(ml('xxx123yyy','',true,'&',true),'/');
9308b99b84SAndreas Gohr        $re = str_replace('xxx123yyy','([^&\?]*)',$re);
941ef68647SAndreas Gohr
95bfbc63f7SAndreas Gohr        // extract the real media from a fetch.php uri and determine mime
96eac16b1dSAndreas Gohr        if(preg_match("/^$re/",$file,$m) ||
9708b99b84SAndreas Gohr            preg_match('/[&\?]media=([^&\?]*)/',$file,$m)){
981ef68647SAndreas Gohr            $media = rawurldecode($m[1]);
99bfbc63f7SAndreas Gohr            list($ext,$mime) = mimetype($media);
100bfbc63f7SAndreas Gohr        }else{
101bfbc63f7SAndreas Gohr            list($ext,$mime) = mimetype($file);
102bfbc63f7SAndreas Gohr        }
1031ef68647SAndreas Gohr
1045aa96d34SAndreas Gohr        // local files
105ca0441e7SGerrit Uitslag        $local = '';
1065aa96d34SAndreas Gohr        if(substr($file,0,9) == 'dw2pdf://'){
1075aa96d34SAndreas Gohr            // support local files passed from plugins
1085aa96d34SAndreas Gohr            $local = substr($file,9);
1095aa96d34SAndreas Gohr        }elseif(!preg_match('/(\.php|\?)/',$file)){
1105aa96d34SAndreas Gohr            $re = preg_quote(DOKU_URL,'/');
1115aa96d34SAndreas Gohr            // directly access local files instead of using HTTP, skip dynamic content
1125aa96d34SAndreas Gohr            $local = preg_replace("/^$re/i",DOKU_INC,$file);
1135aa96d34SAndreas Gohr        }
1145aa96d34SAndreas Gohr
115bfbc63f7SAndreas Gohr        if(substr($mime,0,6) == 'image/'){
116bfbc63f7SAndreas Gohr            if(!empty($media)){
117bfbc63f7SAndreas Gohr                // any size restrictions?
118ca0441e7SGerrit Uitslag                $w = $h = 0;
11908b99b84SAndreas Gohr                if(preg_match('/[\?&]w=(\d+)/',$file, $m)) $w = $m[1];
12008b99b84SAndreas Gohr                if(preg_match('/[\?&]h=(\d+)/',$file, $m)) $h = $m[1];
12108b99b84SAndreas Gohr
1222585efdfSKlap-in                if(preg_match('/^(https?|ftp):\/\//',$media)){
12308b99b84SAndreas Gohr                    $local = media_get_from_URL($media,$ext,-1);
12408b99b84SAndreas Gohr                    if(!$local) $local = $media; // let mpdf try again
1251ef68647SAndreas Gohr                }else{
1261ef68647SAndreas Gohr                    $media = cleanID($media);
1271ef68647SAndreas Gohr                    //check permissions (namespace only)
1281ef68647SAndreas Gohr                    if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ){
1291ef68647SAndreas Gohr                        $file = '';
1301ef68647SAndreas Gohr                    }
1311ef68647SAndreas Gohr                    $local  = mediaFN($media);
1321ef68647SAndreas Gohr                }
1331ef68647SAndreas Gohr
1341ef68647SAndreas Gohr                //handle image resizing/cropping
135ecaaf7b9SAndreas Gohr                if($w && file_exists($local)){
13608b99b84SAndreas Gohr                    if($h){
1371ef68647SAndreas Gohr                        $local = media_crop_image($local,$ext,$w,$h);
1381ef68647SAndreas Gohr                    }else{
1391ef68647SAndreas Gohr                        $local = media_resize_image($local,$ext,$w,$h);
1401ef68647SAndreas Gohr                    }
1411ef68647SAndreas Gohr                }
1422585efdfSKlap-in            }elseif(preg_match('/^(https?|ftp):\/\//',$file)){ // fixed external URLs
14308b99b84SAndreas Gohr                $local = media_get_from_URL($file,$ext,$conf['cachetime']);
1441ef68647SAndreas Gohr            }
1451ef68647SAndreas Gohr
1461ef68647SAndreas Gohr            if($local){
147a06728a6SAndreas Gohr                $file = $local;
1481ef68647SAndreas Gohr                $orig_srcpath = $local;
1491ef68647SAndreas Gohr            }
1501ef68647SAndreas Gohr        }
1511ef68647SAndreas Gohr
1521ef68647SAndreas Gohr        return parent::_getImage($file, $firsttime, $allowvector, $orig_srcpath);
1531ef68647SAndreas Gohr    }
1541ef68647SAndreas Gohr
1551ef68647SAndreas Gohr}
156