xref: /plugin/dw2pdf/DokuPDF.class.php (revision ecaaf7b9e686166d92c38015e8bf6ddaffc40794)
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
11b2853a2aSAndreas Gohrif(!defined('_MPDF_TEMP_PATH')) define('_MPDF_TEMP_PATH', $conf['tmpdir']);
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
171ef68647SAndreas Gohr    function __construct(){
18b2853a2aSAndreas Gohr        io_mkdir_p(_MPDF_TTFONTDATAPATH);
191ef68647SAndreas Gohr
201ef68647SAndreas Gohr        // we're always UTF-8
211ef68647SAndreas Gohr        parent::__construct('UTF-8-s');
221ef68647SAndreas Gohr        $this->SetAutoFont(AUTOFONT_ALL);
231ef68647SAndreas Gohr        $this->ignore_invalid_utf8 = true;
241ef68647SAndreas Gohr
251ef68647SAndreas Gohr    }
261ef68647SAndreas Gohr
27a06728a6SAndreas Gohr    /**
28a06728a6SAndreas Gohr     * Decode all paths, since DokuWiki uses XHTML compliant URLs
29a06728a6SAndreas Gohr     */
30a06728a6SAndreas Gohr    function GetFullPath(&$path,$basepath=''){
31a06728a6SAndreas Gohr        $path = htmlspecialchars_decode($path);
32a06728a6SAndreas Gohr        parent::GetFullPath($path, $basepath);
33a06728a6SAndreas Gohr    }
341ef68647SAndreas Gohr
351ef68647SAndreas Gohr    /**
361ef68647SAndreas Gohr     * Override the mpdf _getImage function
371ef68647SAndreas Gohr     *
381ef68647SAndreas Gohr     * This function takes care of gathering the image data from HTTP or
391ef68647SAndreas Gohr     * local files before passing the data back to mpdf's original function
401ef68647SAndreas Gohr     * making sure that only cached file paths are passed to mpdf. It also
411ef68647SAndreas Gohr     * takes care of checking image ACls.
421ef68647SAndreas Gohr     */
431ef68647SAndreas Gohr    function _getImage(&$file, $firsttime=true, $allowvector=true, $orig_srcpath=false){
441ef68647SAndreas Gohr        list($ext,$mime) = mimetype($file);
45bfbc63f7SAndreas Gohr
461ef68647SAndreas Gohr        // build regex to parse URL back to media info
47eac16b1dSAndreas Gohr        $re = preg_quote(ml('xxx123yyy','',true,'&',true),'/');
4808b99b84SAndreas Gohr        $re = str_replace('xxx123yyy','([^&\?]*)',$re);
491ef68647SAndreas Gohr
50bfbc63f7SAndreas Gohr        // extract the real media from a fetch.php uri and determine mime
51eac16b1dSAndreas Gohr        if(preg_match("/^$re/",$file,$m) ||
5208b99b84SAndreas Gohr            preg_match('/[&\?]media=([^&\?]*)/',$file,$m)){
531ef68647SAndreas Gohr            $media = rawurldecode($m[1]);
54bfbc63f7SAndreas Gohr            list($ext,$mime) = mimetype($media);
55bfbc63f7SAndreas Gohr        }else{
56bfbc63f7SAndreas Gohr            list($ext,$mime) = mimetype($file);
57bfbc63f7SAndreas Gohr        }
581ef68647SAndreas Gohr
595aa96d34SAndreas Gohr        // local files
605aa96d34SAndreas Gohr        if(substr($file,0,9) == 'dw2pdf://'){
615aa96d34SAndreas Gohr            // support local files passed from plugins
625aa96d34SAndreas Gohr            $local = substr($file,9);
635aa96d34SAndreas Gohr        }elseif(!preg_match('/(\.php|\?)/',$file)){
645aa96d34SAndreas Gohr            $re = preg_quote(DOKU_URL,'/');
655aa96d34SAndreas Gohr            // directly access local files instead of using HTTP, skip dynamic content
665aa96d34SAndreas Gohr            $local = preg_replace("/^$re/i",DOKU_INC,$file);
675aa96d34SAndreas Gohr        }
685aa96d34SAndreas Gohr
69bfbc63f7SAndreas Gohr        if(substr($mime,0,6) == 'image/'){
70bfbc63f7SAndreas Gohr            if(!empty($media)){
71bfbc63f7SAndreas Gohr                // any size restrictions?
7208b99b84SAndreas Gohr                if(preg_match('/[\?&]w=(\d+)/',$file, $m)) $w = $m[1];
7308b99b84SAndreas Gohr                if(preg_match('/[\?&]h=(\d+)/',$file, $m)) $h = $m[1];
7408b99b84SAndreas Gohr
7508b99b84SAndreas Gohr                if(preg_match('/^https?:\/\//',$media)){
7608b99b84SAndreas Gohr                    $local = media_get_from_URL($media,$ext,-1);
7708b99b84SAndreas Gohr                    if(!$local) $local = $media; // let mpdf try again
781ef68647SAndreas Gohr                }else{
791ef68647SAndreas Gohr                    $media = cleanID($media);
801ef68647SAndreas Gohr                    //check permissions (namespace only)
811ef68647SAndreas Gohr                    if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ){
821ef68647SAndreas Gohr                        $file = '';
831ef68647SAndreas Gohr                    }
841ef68647SAndreas Gohr                    $local  = mediaFN($media);
851ef68647SAndreas Gohr                }
861ef68647SAndreas Gohr
871ef68647SAndreas Gohr                //handle image resizing/cropping
88*ecaaf7b9SAndreas Gohr                if($w && file_exists($local)){
8908b99b84SAndreas Gohr                    if($h){
901ef68647SAndreas Gohr                        $local = media_crop_image($local,$ext,$w,$h);
911ef68647SAndreas Gohr                    }else{
921ef68647SAndreas Gohr                        $local = media_resize_image($local,$ext,$w,$h);
931ef68647SAndreas Gohr                    }
941ef68647SAndreas Gohr                }
9508b99b84SAndreas Gohr            }elseif(preg_match('/^https?:\/\//',$file)){ // fixed external URLs
9608b99b84SAndreas Gohr                $local = media_get_from_URL($file,$ext,$conf['cachetime']);
971ef68647SAndreas Gohr            }
981ef68647SAndreas Gohr
991ef68647SAndreas Gohr            if($local){
100a06728a6SAndreas Gohr                $file = $local;
1011ef68647SAndreas Gohr                $orig_srcpath = $local;
1021ef68647SAndreas Gohr            }
1031ef68647SAndreas Gohr        }
1041ef68647SAndreas Gohr
1051ef68647SAndreas Gohr        return parent::_getImage($file, $firsttime, $allowvector, $orig_srcpath);
1061ef68647SAndreas Gohr    }
1071ef68647SAndreas Gohr
1081ef68647SAndreas Gohr}
109