xref: /plugin/dw2pdf/DokuPDF.class.php (revision de172c8654355f6bace4d6ca2c4cb5e254fac7dd)
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);
47c00b769aSLarsDW223        $this->autoScriptToLang = true;
48c00b769aSLarsDW223        $this->baseScript = 1;
49c00b769aSLarsDW223        $this->autoVietnamese = true;
50c00b769aSLarsDW223        $this->autoArabic = true;
51c00b769aSLarsDW223        $this->autoLangToFont = true;
52c00b769aSLarsDW223
531ef68647SAndreas Gohr        $this->ignore_invalid_utf8 = true;
542585efdfSKlap-in        $this->tabSpaces = 4;
551c9fae9eSAndreas Gohr    }
561ef68647SAndreas Gohr
571c9fae9eSAndreas Gohr    /**
581c9fae9eSAndreas Gohr     * Cleanup temp dir
591c9fae9eSAndreas Gohr     */
601c9fae9eSAndreas Gohr    function __destruct(){
61acf3824dSGerrit Uitslag        io_rmdir(_MPDF_TEMP_PATH, true);
621ef68647SAndreas Gohr    }
631ef68647SAndreas Gohr
64a06728a6SAndreas Gohr    /**
65a06728a6SAndreas Gohr     * Decode all paths, since DokuWiki uses XHTML compliant URLs
66a06728a6SAndreas Gohr     */
67a06728a6SAndreas Gohr    function GetFullPath(&$path,$basepath=''){
68a06728a6SAndreas Gohr        $path = htmlspecialchars_decode($path);
69a06728a6SAndreas Gohr        parent::GetFullPath($path, $basepath);
70a06728a6SAndreas Gohr    }
711ef68647SAndreas Gohr
721ef68647SAndreas Gohr    /**
731ef68647SAndreas Gohr     * Override the mpdf _getImage function
741ef68647SAndreas Gohr     *
751ef68647SAndreas Gohr     * This function takes care of gathering the image data from HTTP or
761ef68647SAndreas Gohr     * local files before passing the data back to mpdf's original function
771ef68647SAndreas Gohr     * making sure that only cached file paths are passed to mpdf. It also
781ef68647SAndreas Gohr     * takes care of checking image ACls.
791ef68647SAndreas Gohr     */
80*de172c86SAnael Mobilia    function _getImage(&$file, $firsttime=true, $allowvector=true, $orig_srcpath=false, $interpolation = false){
812585efdfSKlap-in        global $conf;
82bfbc63f7SAndreas Gohr
831ef68647SAndreas Gohr        // build regex to parse URL back to media info
84eac16b1dSAndreas Gohr        $re = preg_quote(ml('xxx123yyy','',true,'&',true),'/');
8508b99b84SAndreas Gohr        $re = str_replace('xxx123yyy','([^&\?]*)',$re);
861ef68647SAndreas Gohr
87bfbc63f7SAndreas Gohr        // extract the real media from a fetch.php uri and determine mime
88eac16b1dSAndreas Gohr        if(preg_match("/^$re/",$file,$m) ||
8908b99b84SAndreas Gohr            preg_match('/[&\?]media=([^&\?]*)/',$file,$m)){
901ef68647SAndreas Gohr            $media = rawurldecode($m[1]);
91bfbc63f7SAndreas Gohr            list($ext,$mime) = mimetype($media);
92bfbc63f7SAndreas Gohr        }else{
93bfbc63f7SAndreas Gohr            list($ext,$mime) = mimetype($file);
94bfbc63f7SAndreas Gohr        }
951ef68647SAndreas Gohr
965aa96d34SAndreas Gohr        // local files
97ca0441e7SGerrit Uitslag        $local = '';
985aa96d34SAndreas Gohr        if(substr($file,0,9) == 'dw2pdf://'){
995aa96d34SAndreas Gohr            // support local files passed from plugins
1005aa96d34SAndreas Gohr            $local = substr($file,9);
1015aa96d34SAndreas Gohr        }elseif(!preg_match('/(\.php|\?)/',$file)){
1025aa96d34SAndreas Gohr            $re = preg_quote(DOKU_URL,'/');
1035aa96d34SAndreas Gohr            // directly access local files instead of using HTTP, skip dynamic content
1045aa96d34SAndreas Gohr            $local = preg_replace("/^$re/i",DOKU_INC,$file);
1055aa96d34SAndreas Gohr        }
1065aa96d34SAndreas Gohr
107bfbc63f7SAndreas Gohr        if(substr($mime,0,6) == 'image/'){
108bfbc63f7SAndreas Gohr            if(!empty($media)){
109bfbc63f7SAndreas Gohr                // any size restrictions?
110ca0441e7SGerrit Uitslag                $w = $h = 0;
11108b99b84SAndreas Gohr                if(preg_match('/[\?&]w=(\d+)/',$file, $m)) $w = $m[1];
11208b99b84SAndreas Gohr                if(preg_match('/[\?&]h=(\d+)/',$file, $m)) $h = $m[1];
11308b99b84SAndreas Gohr
114acf3824dSGerrit Uitslag                if(media_isexternal($media)){
11508b99b84SAndreas Gohr                    $local = media_get_from_URL($media,$ext,-1);
11608b99b84SAndreas Gohr                    if(!$local) $local = $media; // let mpdf try again
1171ef68647SAndreas Gohr                }else{
1181ef68647SAndreas Gohr                    $media = cleanID($media);
1191ef68647SAndreas Gohr                    //check permissions (namespace only)
1201ef68647SAndreas Gohr                    if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ){
1211ef68647SAndreas Gohr                        $file = '';
1221ef68647SAndreas Gohr                    }
1231ef68647SAndreas Gohr                    $local  = mediaFN($media);
1241ef68647SAndreas Gohr                }
1251ef68647SAndreas Gohr
1261ef68647SAndreas Gohr                //handle image resizing/cropping
127ecaaf7b9SAndreas Gohr                if($w && file_exists($local)){
12808b99b84SAndreas Gohr                    if($h){
1291ef68647SAndreas Gohr                        $local = media_crop_image($local,$ext,$w,$h);
1301ef68647SAndreas Gohr                    }else{
1311ef68647SAndreas Gohr                        $local = media_resize_image($local,$ext,$w,$h);
1321ef68647SAndreas Gohr                    }
1331ef68647SAndreas Gohr                }
134acf3824dSGerrit Uitslag            }elseif(media_isexternal($file)){ // fixed external URLs
13508b99b84SAndreas Gohr                $local = media_get_from_URL($file,$ext,$conf['cachetime']);
1361ef68647SAndreas Gohr            }
1371ef68647SAndreas Gohr
1381ef68647SAndreas Gohr            if($local){
139a06728a6SAndreas Gohr                $file = $local;
1401ef68647SAndreas Gohr                $orig_srcpath = $local;
1411ef68647SAndreas Gohr            }
1421ef68647SAndreas Gohr        }
1431ef68647SAndreas Gohr
144*de172c86SAnael Mobilia        return parent::_getImage($file, $firsttime, $allowvector, $orig_srcpath, $interpolation);
1451ef68647SAndreas Gohr    }
1461ef68647SAndreas Gohr
1471ef68647SAndreas Gohr}
148