xref: /plugin/dw2pdf/DokuPDF.class.php (revision f8f1ee5a484f7358fea428ce9eca45b25e991d22)
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
140119ca25SAndreas Gohrrequire_once __DIR__ . '/vendor/autoload.php';
151ef68647SAndreas Gohr
16ca0441e7SGerrit Uitslag/**
17ca0441e7SGerrit Uitslag * Class DokuPDF
18ca0441e7SGerrit Uitslag * Some DokuWiki specific extentions
19ca0441e7SGerrit Uitslag */
200119ca25SAndreas Gohrclass DokuPDF extends \Mpdf\Mpdf {
211ef68647SAndreas Gohr
22*f8f1ee5aSAndreas Gohr    function __construct($pagesize = 'A4', $orientation = 'portrait', $fontsize = 11) {
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
460119ca25SAndreas Gohr        parent::__construct(
470119ca25SAndreas Gohr            array(
480119ca25SAndreas Gohr                'mode' => $mode,
49*f8f1ee5aSAndreas Gohr                'format' => $format,
50*f8f1ee5aSAndreas Gohr                'fontsize' => $fontsize
510119ca25SAndreas Gohr            )
520119ca25SAndreas Gohr        );
53c00b769aSLarsDW223        $this->autoScriptToLang = true;
54c00b769aSLarsDW223        $this->baseScript = 1;
55c00b769aSLarsDW223        $this->autoVietnamese = true;
56c00b769aSLarsDW223        $this->autoArabic = true;
57c00b769aSLarsDW223        $this->autoLangToFont = true;
58c00b769aSLarsDW223
591ef68647SAndreas Gohr        $this->ignore_invalid_utf8 = true;
602585efdfSKlap-in        $this->tabSpaces = 4;
611c9fae9eSAndreas Gohr    }
621ef68647SAndreas Gohr
631c9fae9eSAndreas Gohr    /**
641c9fae9eSAndreas Gohr     * Cleanup temp dir
651c9fae9eSAndreas Gohr     */
661c9fae9eSAndreas Gohr    function __destruct() {
67acf3824dSGerrit Uitslag        io_rmdir(_MPDF_TEMP_PATH, true);
681ef68647SAndreas Gohr    }
691ef68647SAndreas Gohr
70a06728a6SAndreas Gohr    /**
71a06728a6SAndreas Gohr     * Decode all paths, since DokuWiki uses XHTML compliant URLs
72a06728a6SAndreas Gohr     */
73a06728a6SAndreas Gohr    function GetFullPath(&$path, $basepath = '') {
74a06728a6SAndreas Gohr        $path = htmlspecialchars_decode($path);
75a06728a6SAndreas Gohr        parent::GetFullPath($path, $basepath);
76a06728a6SAndreas Gohr    }
771ef68647SAndreas Gohr
781ef68647SAndreas Gohr    /**
791ef68647SAndreas Gohr     * Override the mpdf _getImage function
801ef68647SAndreas Gohr     *
811ef68647SAndreas Gohr     * This function takes care of gathering the image data from HTTP or
821ef68647SAndreas Gohr     * local files before passing the data back to mpdf's original function
831ef68647SAndreas Gohr     * making sure that only cached file paths are passed to mpdf. It also
841ef68647SAndreas Gohr     * takes care of checking image ACls.
851ef68647SAndreas Gohr     */
86de172c86SAnael Mobilia    function _getImage(&$file, $firsttime = true, $allowvector = true, $orig_srcpath = false, $interpolation = false) {
872585efdfSKlap-in        global $conf;
88bfbc63f7SAndreas Gohr
891ef68647SAndreas Gohr        // build regex to parse URL back to media info
90eac16b1dSAndreas Gohr        $re = preg_quote(ml('xxx123yyy', '', true, '&', true), '/');
9108b99b84SAndreas Gohr        $re = str_replace('xxx123yyy', '([^&\?]*)', $re);
921ef68647SAndreas Gohr
93bfbc63f7SAndreas Gohr        // extract the real media from a fetch.php uri and determine mime
94eac16b1dSAndreas Gohr        if(preg_match("/^$re/", $file, $m) ||
950119ca25SAndreas Gohr            preg_match('/[&\?]media=([^&\?]*)/', $file, $m)
960119ca25SAndreas Gohr        ) {
971ef68647SAndreas Gohr            $media = rawurldecode($m[1]);
98bfbc63f7SAndreas Gohr            list($ext, $mime) = mimetype($media);
99bfbc63f7SAndreas Gohr        } else {
100bfbc63f7SAndreas Gohr            list($ext, $mime) = mimetype($file);
101bfbc63f7SAndreas Gohr        }
1021ef68647SAndreas Gohr
1035aa96d34SAndreas Gohr        // local files
104ca0441e7SGerrit Uitslag        $local = '';
1055aa96d34SAndreas Gohr        if(substr($file, 0, 9) == 'dw2pdf://') {
1065aa96d34SAndreas Gohr            // support local files passed from plugins
1075aa96d34SAndreas Gohr            $local = substr($file, 9);
1085aa96d34SAndreas Gohr        } elseif(!preg_match('/(\.php|\?)/', $file)) {
1095aa96d34SAndreas Gohr            $re = preg_quote(DOKU_URL, '/');
1105aa96d34SAndreas Gohr            // directly access local files instead of using HTTP, skip dynamic content
1115aa96d34SAndreas Gohr            $local = preg_replace("/^$re/i", DOKU_INC, $file);
1125aa96d34SAndreas Gohr        }
1135aa96d34SAndreas Gohr
114bfbc63f7SAndreas Gohr        if(substr($mime, 0, 6) == 'image/') {
115bfbc63f7SAndreas Gohr            if(!empty($media)) {
116bfbc63f7SAndreas Gohr                // any size restrictions?
117ca0441e7SGerrit Uitslag                $w = $h = 0;
11808b99b84SAndreas Gohr                if(preg_match('/[\?&]w=(\d+)/', $file, $m)) $w = $m[1];
11908b99b84SAndreas Gohr                if(preg_match('/[\?&]h=(\d+)/', $file, $m)) $h = $m[1];
12008b99b84SAndreas Gohr
121acf3824dSGerrit Uitslag                if(media_isexternal($media)) {
12208b99b84SAndreas Gohr                    $local = media_get_from_URL($media, $ext, -1);
12308b99b84SAndreas Gohr                    if(!$local) $local = $media; // let mpdf try again
1241ef68647SAndreas Gohr                } else {
1251ef68647SAndreas Gohr                    $media = cleanID($media);
1261ef68647SAndreas Gohr                    //check permissions (namespace only)
1271ef68647SAndreas Gohr                    if(auth_quickaclcheck(getNS($media) . ':X') < AUTH_READ) {
1281ef68647SAndreas Gohr                        $file = '';
1291ef68647SAndreas Gohr                    }
1301ef68647SAndreas Gohr                    $local = mediaFN($media);
1311ef68647SAndreas Gohr                }
1321ef68647SAndreas Gohr
1331ef68647SAndreas Gohr                //handle image resizing/cropping
134ecaaf7b9SAndreas Gohr                if($w && file_exists($local)) {
13508b99b84SAndreas Gohr                    if($h) {
1361ef68647SAndreas Gohr                        $local = media_crop_image($local, $ext, $w, $h);
1371ef68647SAndreas Gohr                    } else {
1381ef68647SAndreas Gohr                        $local = media_resize_image($local, $ext, $w, $h);
1391ef68647SAndreas Gohr                    }
1401ef68647SAndreas Gohr                }
141acf3824dSGerrit Uitslag            } elseif(media_isexternal($file)) { // fixed external URLs
14208b99b84SAndreas Gohr                $local = media_get_from_URL($file, $ext, $conf['cachetime']);
1431ef68647SAndreas Gohr            }
1441ef68647SAndreas Gohr
1451ef68647SAndreas Gohr            if($local) {
146a06728a6SAndreas Gohr                $file = $local;
1471ef68647SAndreas Gohr                $orig_srcpath = $local;
1481ef68647SAndreas Gohr            }
1491ef68647SAndreas Gohr        }
1501ef68647SAndreas Gohr
151de172c86SAnael Mobilia        return parent::_getImage($file, $firsttime, $allowvector, $orig_srcpath, $interpolation);
1521ef68647SAndreas Gohr    }
1531ef68647SAndreas Gohr
1541ef68647SAndreas Gohr}
155