1<?php
2/**
3 * Site Export Plugin - mPDF Extension
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     i-net software <tools@inetsoftware.de>
7 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8 */
9
10if (file_exists(DOKU_PLUGIN . 'dw2pdf/mpdf/mpdf.php')) {
11
12    global $conf;
13    if (!defined('_MPDF_TEMP_PATH')) {
14        define('_MPDF_TEMP_PATH', $conf['tmpdir'] . '/dwpdf/' . rand(1, 1000) . '/');
15    }
16    if (!defined('_MPDF_TTFONTDATAPATH')) {
17        define('_MPDF_TTFONTDATAPATH', $conf['cachedir'] . '/mpdf_ttf/');
18    }
19
20    require_once(DOKU_PLUGIN . 'dw2pdf/mpdf/mpdf.php');
21
22    class siteexportPDF extends mpdf {
23
24        private $debugObj = null;
25
26        public function __construct($debug) {
27            global $INPUT;
28            global $conf;
29
30            $dw2pdf = plugin_load('action', 'dw2pdf');
31
32            // decide on the paper setup from param or config
33            $pagesize    = $INPUT->str('pagesize', $dw2pdf->getConf('pagesize'), true);
34            $orientation = $INPUT->str('orientation', $dw2pdf->getConf('orientation'), true);
35
36            io_mkdir_p(_MPDF_TTFONTDATAPATH);
37            io_mkdir_p(_MPDF_TEMP_PATH);
38
39            $format = $pagesize;
40            if ($orientation == 'landscape') {
41                $format .= '-L';
42            }
43
44            switch ($conf['lang']) {
45                case 'zh':
46                case 'zh-tw':
47                case 'ja':
48                case 'ko':
49                    $mode = '+aCJK';
50                    break;
51                default:
52                    $mode = 'UTF-8-s';
53
54            }
55
56            // we're always UTF-8
57            parent::__construct($mode, $format);
58            $this->ignore_invalid_utf8 = true;
59            $this->tabSpaces = 4;
60            $this->debugObj = $debug;
61            $this->debug = $debug !== false;
62            $this->shrink_tables_to_fit = 1; // Does not shrink tables by default, only in emergency
63            $this->use_kwt = true; // avoids page-breaking in H1-H6 if a table follows directly
64            $this->useSubstitutions = true;
65        }
66
67        public function message($msg, $vars = null, $lvl = 1)
68        {
69            if ($this->debugObj !== null) {
70                $this->debugObj->message($msg, $vars, $lvl);
71            }
72        }
73
74        public function Error($msg)
75        {
76            if ($this->debugObj !== null && method_exists($this->debugObj, 'runtimeException')) {
77                $this->debugObj->runtimeException($msg);
78            } else {
79                parent::Error($msg);
80            }
81        }
82
83        public function GetFullPath(&$path,$basepath='') {
84
85            // Full Path might return a doubled path like /~gamma/documentation/lib//~gamma/documentation/lib/tpl/clearreports/./_print-images/background-bottom.jpg
86
87            $path = str_replace("\\","/",$path); //If on Windows
88            $path = preg_replace('/^\/\//','http://',$path);    // mPDF 5.6.27
89            $regexp = '|^./|';    // Inadvertently corrects "./path/etc" and "//www.domain.com/etc"
90            $path = preg_replace($regexp,'',$path);
91
92            if ( preg_match("/^.+\/\.\.\//", $path) ) {
93                // ../ not at the beginning
94                $newpath = array();
95                $oldpath = explode('/', $path);
96
97                foreach( $oldpath as $slice ) {
98                    if ( $slice == ".." && count($newpath) > 0 ) {
99                        array_pop($newpath);
100                        continue;
101                    }
102
103                    $newpath[] = $slice;
104                }
105
106                $path = implode('/', $newpath);
107            }
108
109            parent::GetFullPath($path, $basepath);
110
111            $regex = "/^(". preg_quote(DOKU_BASE, '/') .".+)\\1/";
112            if ( preg_match($regex, $path, $matches) ) {
113                $path = preg_replace($regex, "\\1", $path);
114            }
115
116        }
117
118        /*
119          Only when the toc is being generated
120        */
121        public function MovePages($target_page, $start_page, $end_page = -1) {
122            parent::MovePages($target_page, $start_page, $end_page);
123        }
124
125        public function OpenTag($tag, $attr, &$ahtml, &$ihtml) {
126            switch ($tag) {
127                case 'BOOKMARK':
128                case 'TOCENTRY':
129                    if ($attr['CONTENT']) {
130                        // resolve double encoding
131                        $attr['CONTENT'] = htmlspecialchars_decode($attr['CONTENT'], ENT_QUOTES);
132                    }
133                    break;
134            }
135            return parent::OpenTag($tag, $attr, $ahtml, $ihtml);
136        }
137    }
138
139    if (file_exists(DOKU_PLUGIN . 'dw2pdf/mpdf/classes/cssmgr.php') && !class_exists('cssmgr', false)) {
140//*
141        require_once(DOKU_PLUGIN . 'siteexport/inc/patchCSSmgr.php');
142        $objPatch = new CSSMgrPatch(DOKU_PLUGIN . 'dw2pdf/mpdf/classes/cssmgr.php');
143        if ($objPatch->redefineFunction(file_get_contents(DOKU_PLUGIN . 'siteexport/inc/readCSS.patch'))) {
144            eval($objPatch->getCode());
145        }
146/*/
147//*/
148    }
149
150}
151