xref: /plugin/dw2pdf/DokuPDF.class.php (revision 0119ca25672bca93bf0779d1cdcf04911dbee191)
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
14*0119ca25SAndreas Gohrrequire_once __DIR__ . '/vendor/autoload.php';
151ef68647SAndreas Gohr
16ca0441e7SGerrit Uitslag/**
17ca0441e7SGerrit Uitslag * Class DokuPDF
18ca0441e7SGerrit Uitslag * Some DokuWiki specific extentions
19ca0441e7SGerrit Uitslag */
20*0119ca25SAndreas Gohrclass DokuPDF extends \Mpdf\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
46*0119ca25SAndreas Gohr        parent::__construct(
47*0119ca25SAndreas Gohr            array(
48*0119ca25SAndreas Gohr                'mode' => $mode,
49*0119ca25SAndreas Gohr                'format' => $format
50*0119ca25SAndreas Gohr            )
51*0119ca25SAndreas Gohr        );
52c00b769aSLarsDW223        $this->autoScriptToLang = true;
53c00b769aSLarsDW223        $this->baseScript = 1;
54c00b769aSLarsDW223        $this->autoVietnamese = true;
55c00b769aSLarsDW223        $this->autoArabic = true;
56c00b769aSLarsDW223        $this->autoLangToFont = true;
57c00b769aSLarsDW223
581ef68647SAndreas Gohr        $this->ignore_invalid_utf8 = true;
592585efdfSKlap-in        $this->tabSpaces = 4;
601c9fae9eSAndreas Gohr    }
611ef68647SAndreas Gohr
621c9fae9eSAndreas Gohr    /**
631c9fae9eSAndreas Gohr     * Cleanup temp dir
641c9fae9eSAndreas Gohr     */
651c9fae9eSAndreas Gohr    function __destruct() {
66acf3824dSGerrit Uitslag        io_rmdir(_MPDF_TEMP_PATH, true);
671ef68647SAndreas Gohr    }
681ef68647SAndreas Gohr
69a06728a6SAndreas Gohr    /**
70a06728a6SAndreas Gohr     * Decode all paths, since DokuWiki uses XHTML compliant URLs
71a06728a6SAndreas Gohr     */
72a06728a6SAndreas Gohr    function GetFullPath(&$path, $basepath = '') {
73a06728a6SAndreas Gohr        $path = htmlspecialchars_decode($path);
74a06728a6SAndreas Gohr        parent::GetFullPath($path, $basepath);
75a06728a6SAndreas Gohr    }
761ef68647SAndreas Gohr
771ef68647SAndreas Gohr    /**
781ef68647SAndreas Gohr     * Override the mpdf _getImage function
791ef68647SAndreas Gohr     *
801ef68647SAndreas Gohr     * This function takes care of gathering the image data from HTTP or
811ef68647SAndreas Gohr     * local files before passing the data back to mpdf's original function
821ef68647SAndreas Gohr     * making sure that only cached file paths are passed to mpdf. It also
831ef68647SAndreas Gohr     * takes care of checking image ACls.
841ef68647SAndreas Gohr     */
85de172c86SAnael Mobilia    function _getImage(&$file, $firsttime = true, $allowvector = true, $orig_srcpath = false, $interpolation = false) {
862585efdfSKlap-in        global $conf;
87bfbc63f7SAndreas Gohr
881ef68647SAndreas Gohr        // build regex to parse URL back to media info
89eac16b1dSAndreas Gohr        $re = preg_quote(ml('xxx123yyy', '', true, '&', true), '/');
9008b99b84SAndreas Gohr        $re = str_replace('xxx123yyy', '([^&\?]*)', $re);
911ef68647SAndreas Gohr
92bfbc63f7SAndreas Gohr        // extract the real media from a fetch.php uri and determine mime
93eac16b1dSAndreas Gohr        if(preg_match("/^$re/", $file, $m) ||
94*0119ca25SAndreas Gohr            preg_match('/[&\?]media=([^&\?]*)/', $file, $m)
95*0119ca25SAndreas Gohr        ) {
961ef68647SAndreas Gohr            $media = rawurldecode($m[1]);
97bfbc63f7SAndreas Gohr            list($ext, $mime) = mimetype($media);
98bfbc63f7SAndreas Gohr        } else {
99bfbc63f7SAndreas Gohr            list($ext, $mime) = mimetype($file);
100bfbc63f7SAndreas Gohr        }
1011ef68647SAndreas Gohr
1025aa96d34SAndreas Gohr        // local files
103ca0441e7SGerrit Uitslag        $local = '';
1045aa96d34SAndreas Gohr        if(substr($file, 0, 9) == 'dw2pdf://') {
1055aa96d34SAndreas Gohr            // support local files passed from plugins
1065aa96d34SAndreas Gohr            $local = substr($file, 9);
1075aa96d34SAndreas Gohr        } elseif(!preg_match('/(\.php|\?)/', $file)) {
1085aa96d34SAndreas Gohr            $re = preg_quote(DOKU_URL, '/');
1095aa96d34SAndreas Gohr            // directly access local files instead of using HTTP, skip dynamic content
1105aa96d34SAndreas Gohr            $local = preg_replace("/^$re/i", DOKU_INC, $file);
1115aa96d34SAndreas Gohr        }
1125aa96d34SAndreas Gohr
113bfbc63f7SAndreas Gohr        if(substr($mime, 0, 6) == 'image/') {
114bfbc63f7SAndreas Gohr            if(!empty($media)) {
115bfbc63f7SAndreas Gohr                // any size restrictions?
116ca0441e7SGerrit Uitslag                $w = $h = 0;
11708b99b84SAndreas Gohr                if(preg_match('/[\?&]w=(\d+)/', $file, $m)) $w = $m[1];
11808b99b84SAndreas Gohr                if(preg_match('/[\?&]h=(\d+)/', $file, $m)) $h = $m[1];
11908b99b84SAndreas Gohr
120acf3824dSGerrit Uitslag                if(media_isexternal($media)) {
12108b99b84SAndreas Gohr                    $local = media_get_from_URL($media, $ext, -1);
12208b99b84SAndreas Gohr                    if(!$local) $local = $media; // let mpdf try again
1231ef68647SAndreas Gohr                } else {
1241ef68647SAndreas Gohr                    $media = cleanID($media);
1251ef68647SAndreas Gohr                    //check permissions (namespace only)
1261ef68647SAndreas Gohr                    if(auth_quickaclcheck(getNS($media) . ':X') < AUTH_READ) {
1271ef68647SAndreas Gohr                        $file = '';
1281ef68647SAndreas Gohr                    }
1291ef68647SAndreas Gohr                    $local = mediaFN($media);
1301ef68647SAndreas Gohr                }
1311ef68647SAndreas Gohr
1321ef68647SAndreas Gohr                //handle image resizing/cropping
133ecaaf7b9SAndreas Gohr                if($w && file_exists($local)) {
13408b99b84SAndreas Gohr                    if($h) {
1351ef68647SAndreas Gohr                        $local = media_crop_image($local, $ext, $w, $h);
1361ef68647SAndreas Gohr                    } else {
1371ef68647SAndreas Gohr                        $local = media_resize_image($local, $ext, $w, $h);
1381ef68647SAndreas Gohr                    }
1391ef68647SAndreas Gohr                }
140acf3824dSGerrit Uitslag            } elseif(media_isexternal($file)) { // fixed external URLs
14108b99b84SAndreas Gohr                $local = media_get_from_URL($file, $ext, $conf['cachetime']);
1421ef68647SAndreas Gohr            }
1431ef68647SAndreas Gohr
1441ef68647SAndreas Gohr            if($local) {
145a06728a6SAndreas Gohr                $file = $local;
1461ef68647SAndreas Gohr                $orig_srcpath = $local;
1471ef68647SAndreas Gohr            }
1481ef68647SAndreas Gohr        }
1491ef68647SAndreas Gohr
150de172c86SAnael Mobilia        return parent::_getImage($file, $firsttime, $allowvector, $orig_srcpath, $interpolation);
1511ef68647SAndreas Gohr    }
1521ef68647SAndreas Gohr
1531ef68647SAndreas Gohr}
154