xref: /plugin/dw2pdf/DokuPDF.class.php (revision c00b769a66764f8c476eb1366a6b778adf00a269)
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 */
10c1acf1feSGerrit 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;
29acf3824dSGerrit Uitslag        if($orientation == 'landscape') {
30acf3824dSGerrit Uitslag            $format .= '-L';
31acf3824dSGerrit Uitslag        }
326ea88a05SAndreas Gohr
335bd9be9cSGerrit Uitslag        switch($conf['lang']) {
345bd9be9cSGerrit Uitslag            case 'zh':
355bd9be9cSGerrit Uitslag            case 'zh-tw':
365bd9be9cSGerrit Uitslag            case 'ja':
375bd9be9cSGerrit Uitslag            case 'ko':
385bd9be9cSGerrit Uitslag                $mode = '+aCJK';
395bd9be9cSGerrit Uitslag                break;
405bd9be9cSGerrit Uitslag            default:
415bd9be9cSGerrit Uitslag                $mode = 'UTF-8-s';
425bd9be9cSGerrit Uitslag
435bd9be9cSGerrit Uitslag        }
445bd9be9cSGerrit Uitslag
451ef68647SAndreas Gohr        // we're always UTF-8
465bd9be9cSGerrit Uitslag        parent::__construct($mode, $format);
47*c00b769aSLarsDW223        // The following replaces $this->SetAutoFont(AUTOFONT_ALL);
48*c00b769aSLarsDW223        // Taken from mpdf/examples/example29_multilingual_autofont.php
49*c00b769aSLarsDW223        $this->autoScriptToLang = true;
50*c00b769aSLarsDW223        $this->baseScript = 1;
51*c00b769aSLarsDW223        $this->autoVietnamese = true;
52*c00b769aSLarsDW223        $this->autoArabic = true;
53*c00b769aSLarsDW223        $this->autoLangToFont = true;
54*c00b769aSLarsDW223
551ef68647SAndreas Gohr        $this->ignore_invalid_utf8 = true;
562585efdfSKlap-in        $this->tabSpaces = 4;
571c9fae9eSAndreas Gohr    }
581ef68647SAndreas Gohr
591c9fae9eSAndreas Gohr    /**
601c9fae9eSAndreas Gohr     * Cleanup temp dir
611c9fae9eSAndreas Gohr     */
621c9fae9eSAndreas Gohr    function __destruct(){
63acf3824dSGerrit Uitslag        io_rmdir(_MPDF_TEMP_PATH, true);
641ef68647SAndreas Gohr    }
651ef68647SAndreas Gohr
66a06728a6SAndreas Gohr    /**
67a06728a6SAndreas Gohr     * Decode all paths, since DokuWiki uses XHTML compliant URLs
68a06728a6SAndreas Gohr     */
69a06728a6SAndreas Gohr    function GetFullPath(&$path,$basepath=''){
70a06728a6SAndreas Gohr        $path = htmlspecialchars_decode($path);
71a06728a6SAndreas Gohr        parent::GetFullPath($path, $basepath);
72a06728a6SAndreas Gohr    }
731ef68647SAndreas Gohr
741ef68647SAndreas Gohr    /**
751ef68647SAndreas Gohr     * Override the mpdf _getImage function
761ef68647SAndreas Gohr     *
771ef68647SAndreas Gohr     * This function takes care of gathering the image data from HTTP or
781ef68647SAndreas Gohr     * local files before passing the data back to mpdf's original function
791ef68647SAndreas Gohr     * making sure that only cached file paths are passed to mpdf. It also
801ef68647SAndreas Gohr     * takes care of checking image ACls.
811ef68647SAndreas Gohr     */
821ef68647SAndreas Gohr    function _getImage(&$file, $firsttime=true, $allowvector=true, $orig_srcpath=false){
832585efdfSKlap-in        global $conf;
84bfbc63f7SAndreas Gohr
851ef68647SAndreas Gohr        // build regex to parse URL back to media info
86eac16b1dSAndreas Gohr        $re = preg_quote(ml('xxx123yyy','',true,'&',true),'/');
8708b99b84SAndreas Gohr        $re = str_replace('xxx123yyy','([^&\?]*)',$re);
881ef68647SAndreas Gohr
89bfbc63f7SAndreas Gohr        // extract the real media from a fetch.php uri and determine mime
90eac16b1dSAndreas Gohr        if(preg_match("/^$re/",$file,$m) ||
9108b99b84SAndreas Gohr            preg_match('/[&\?]media=([^&\?]*)/',$file,$m)){
921ef68647SAndreas Gohr            $media = rawurldecode($m[1]);
93bfbc63f7SAndreas Gohr            list($ext,$mime) = mimetype($media);
94bfbc63f7SAndreas Gohr        }else{
95bfbc63f7SAndreas Gohr            list($ext,$mime) = mimetype($file);
96bfbc63f7SAndreas Gohr        }
971ef68647SAndreas Gohr
985aa96d34SAndreas Gohr        // local files
99ca0441e7SGerrit Uitslag        $local = '';
1005aa96d34SAndreas Gohr        if(substr($file,0,9) == 'dw2pdf://'){
1015aa96d34SAndreas Gohr            // support local files passed from plugins
1025aa96d34SAndreas Gohr            $local = substr($file,9);
1035aa96d34SAndreas Gohr        }elseif(!preg_match('/(\.php|\?)/',$file)){
1045aa96d34SAndreas Gohr            $re = preg_quote(DOKU_URL,'/');
1055aa96d34SAndreas Gohr            // directly access local files instead of using HTTP, skip dynamic content
1065aa96d34SAndreas Gohr            $local = preg_replace("/^$re/i",DOKU_INC,$file);
1075aa96d34SAndreas Gohr        }
1085aa96d34SAndreas Gohr
109bfbc63f7SAndreas Gohr        if(substr($mime,0,6) == 'image/'){
110bfbc63f7SAndreas Gohr            if(!empty($media)){
111bfbc63f7SAndreas Gohr                // any size restrictions?
112ca0441e7SGerrit Uitslag                $w = $h = 0;
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
116acf3824dSGerrit Uitslag                if(media_isexternal($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                }
136acf3824dSGerrit Uitslag            }elseif(media_isexternal($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