xref: /plugin/dw2pdf/DokuPDF.class.php (revision 5bd9be9c3a10252f74e49afa2e9ec7cd52ba705b)
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
176ea88a05SAndreas Gohr    function __construct($pagesize='A4', $orientation='portrait'){
18*5bd9be9cSGerrit Uitslag        global $conf;
19*5bd9be9cSGerrit Uitslag
20b2853a2aSAndreas Gohr        io_mkdir_p(_MPDF_TTFONTDATAPATH);
211c9fae9eSAndreas Gohr        io_mkdir_p(_MPDF_TEMP_PATH);
221ef68647SAndreas Gohr
236ea88a05SAndreas Gohr        $format = $pagesize;
246ea88a05SAndreas Gohr        if($orientation == 'landscape') $format .= '-L';
256ea88a05SAndreas Gohr
26*5bd9be9cSGerrit Uitslag        switch($conf['lang']) {
27*5bd9be9cSGerrit Uitslag            case 'zh':
28*5bd9be9cSGerrit Uitslag            case 'zh-tw':
29*5bd9be9cSGerrit Uitslag            case 'ja':
30*5bd9be9cSGerrit Uitslag            case 'ko':
31*5bd9be9cSGerrit Uitslag                $mode = '+aCJK';
32*5bd9be9cSGerrit Uitslag                break;
33*5bd9be9cSGerrit Uitslag            default:
34*5bd9be9cSGerrit Uitslag                $mode = 'UTF-8-s';
35*5bd9be9cSGerrit Uitslag
36*5bd9be9cSGerrit Uitslag        }
37*5bd9be9cSGerrit Uitslag
381ef68647SAndreas Gohr        // we're always UTF-8
39*5bd9be9cSGerrit Uitslag        parent::__construct($mode, $format);
401ef68647SAndreas Gohr        $this->SetAutoFont(AUTOFONT_ALL);
411ef68647SAndreas Gohr        $this->ignore_invalid_utf8 = true;
422585efdfSKlap-in        $this->tabSpaces = 4;
431c9fae9eSAndreas Gohr    }
441ef68647SAndreas Gohr
451c9fae9eSAndreas Gohr    /**
461c9fae9eSAndreas Gohr     * Cleanup temp dir
471c9fae9eSAndreas Gohr     */
481c9fae9eSAndreas Gohr    function __destruct(){
491c9fae9eSAndreas Gohr        $this->deletedir(_MPDF_TEMP_PATH);
501c9fae9eSAndreas Gohr    }
511c9fae9eSAndreas Gohr
521c9fae9eSAndreas Gohr    /**
531c9fae9eSAndreas Gohr     * Recursively delete a directory and its contents
541c9fae9eSAndreas Gohr     *
551c9fae9eSAndreas Gohr     * @link http://de3.php.net/manual/en/function.rmdir.php#108113
561c9fae9eSAndreas Gohr     */
571c9fae9eSAndreas Gohr    function deletedir($dir){
581c9fae9eSAndreas Gohr        foreach(glob($dir . '/*') as $file) {
591c9fae9eSAndreas Gohr            if(is_dir($file))
601c9fae9eSAndreas Gohr                $this->deletedir($file);
611c9fae9eSAndreas Gohr            else
621c9fae9eSAndreas Gohr                @unlink($file);
631c9fae9eSAndreas Gohr        }
641c9fae9eSAndreas Gohr        @rmdir($dir);
651ef68647SAndreas Gohr    }
661ef68647SAndreas Gohr
67a06728a6SAndreas Gohr    /**
68a06728a6SAndreas Gohr     * Decode all paths, since DokuWiki uses XHTML compliant URLs
69a06728a6SAndreas Gohr     */
70a06728a6SAndreas Gohr    function GetFullPath(&$path,$basepath=''){
71a06728a6SAndreas Gohr        $path = htmlspecialchars_decode($path);
72a06728a6SAndreas Gohr        parent::GetFullPath($path, $basepath);
73a06728a6SAndreas Gohr    }
741ef68647SAndreas Gohr
751ef68647SAndreas Gohr    /**
761ef68647SAndreas Gohr     * Override the mpdf _getImage function
771ef68647SAndreas Gohr     *
781ef68647SAndreas Gohr     * This function takes care of gathering the image data from HTTP or
791ef68647SAndreas Gohr     * local files before passing the data back to mpdf's original function
801ef68647SAndreas Gohr     * making sure that only cached file paths are passed to mpdf. It also
811ef68647SAndreas Gohr     * takes care of checking image ACls.
821ef68647SAndreas Gohr     */
831ef68647SAndreas Gohr    function _getImage(&$file, $firsttime=true, $allowvector=true, $orig_srcpath=false){
842585efdfSKlap-in        global $conf;
851ef68647SAndreas Gohr        list($ext,$mime) = mimetype($file);
86bfbc63f7SAndreas Gohr
871ef68647SAndreas Gohr        // build regex to parse URL back to media info
88eac16b1dSAndreas Gohr        $re = preg_quote(ml('xxx123yyy','',true,'&',true),'/');
8908b99b84SAndreas Gohr        $re = str_replace('xxx123yyy','([^&\?]*)',$re);
901ef68647SAndreas Gohr
91bfbc63f7SAndreas Gohr        // extract the real media from a fetch.php uri and determine mime
92eac16b1dSAndreas Gohr        if(preg_match("/^$re/",$file,$m) ||
9308b99b84SAndreas Gohr            preg_match('/[&\?]media=([^&\?]*)/',$file,$m)){
941ef68647SAndreas Gohr            $media = rawurldecode($m[1]);
95bfbc63f7SAndreas Gohr            list($ext,$mime) = mimetype($media);
96bfbc63f7SAndreas Gohr        }else{
97bfbc63f7SAndreas Gohr            list($ext,$mime) = mimetype($file);
98bfbc63f7SAndreas Gohr        }
991ef68647SAndreas Gohr
1005aa96d34SAndreas Gohr        // local files
1015aa96d34SAndreas Gohr        if(substr($file,0,9) == 'dw2pdf://'){
1025aa96d34SAndreas Gohr            // support local files passed from plugins
1035aa96d34SAndreas Gohr            $local = substr($file,9);
1045aa96d34SAndreas Gohr        }elseif(!preg_match('/(\.php|\?)/',$file)){
1055aa96d34SAndreas Gohr            $re = preg_quote(DOKU_URL,'/');
1065aa96d34SAndreas Gohr            // directly access local files instead of using HTTP, skip dynamic content
1075aa96d34SAndreas Gohr            $local = preg_replace("/^$re/i",DOKU_INC,$file);
1085aa96d34SAndreas Gohr        }
1095aa96d34SAndreas Gohr
110bfbc63f7SAndreas Gohr        if(substr($mime,0,6) == 'image/'){
111bfbc63f7SAndreas Gohr            if(!empty($media)){
112bfbc63f7SAndreas Gohr                // any size restrictions?
11308b99b84SAndreas Gohr                if(preg_match('/[\?&]w=(\d+)/',$file, $m)) $w = $m[1];
11408b99b84SAndreas Gohr                if(preg_match('/[\?&]h=(\d+)/',$file, $m)) $h = $m[1];
11508b99b84SAndreas Gohr
1162585efdfSKlap-in                if(preg_match('/^(https?|ftp):\/\//',$media)){
11708b99b84SAndreas Gohr                    $local = media_get_from_URL($media,$ext,-1);
11808b99b84SAndreas Gohr                    if(!$local) $local = $media; // let mpdf try again
1191ef68647SAndreas Gohr                }else{
1201ef68647SAndreas Gohr                    $media = cleanID($media);
1211ef68647SAndreas Gohr                    //check permissions (namespace only)
1221ef68647SAndreas Gohr                    if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ){
1231ef68647SAndreas Gohr                        $file = '';
1241ef68647SAndreas Gohr                    }
1251ef68647SAndreas Gohr                    $local  = mediaFN($media);
1261ef68647SAndreas Gohr                }
1271ef68647SAndreas Gohr
1281ef68647SAndreas Gohr                //handle image resizing/cropping
129ecaaf7b9SAndreas Gohr                if($w && file_exists($local)){
13008b99b84SAndreas Gohr                    if($h){
1311ef68647SAndreas Gohr                        $local = media_crop_image($local,$ext,$w,$h);
1321ef68647SAndreas Gohr                    }else{
1331ef68647SAndreas Gohr                        $local = media_resize_image($local,$ext,$w,$h);
1341ef68647SAndreas Gohr                    }
1351ef68647SAndreas Gohr                }
1362585efdfSKlap-in            }elseif(preg_match('/^(https?|ftp):\/\//',$file)){ // fixed external URLs
13708b99b84SAndreas Gohr                $local = media_get_from_URL($file,$ext,$conf['cachetime']);
1381ef68647SAndreas Gohr            }
1391ef68647SAndreas Gohr
1401ef68647SAndreas Gohr            if($local){
141a06728a6SAndreas Gohr                $file = $local;
1421ef68647SAndreas Gohr                $orig_srcpath = $local;
1431ef68647SAndreas Gohr            }
1441ef68647SAndreas Gohr        }
1451ef68647SAndreas Gohr
1461ef68647SAndreas Gohr        return parent::_getImage($file, $firsttime, $allowvector, $orig_srcpath);
1471ef68647SAndreas Gohr    }
1481ef68647SAndreas Gohr
1491ef68647SAndreas Gohr}
150