xref: /plugin/siteexport/inc/mpdf.php (revision ed50730582ed6baa071dc0d2c327c7b4a387d58b)
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
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13
14if ( file_exists(DOKU_PLUGIN . 'dw2pdf/mpdf/mpdf.php') ) {
15
16    global $conf;
17    if(!defined('_MPDF_TEMP_PATH')) define('_MPDF_TEMP_PATH', $conf['tmpdir'].'/dwpdf/'.rand(1,1000).'/');
18    if(!defined('_MPDF_TTFONTDATAPATH')) define('_MPDF_TTFONTDATAPATH',$conf['cachedir'].'/mpdf_ttf/');
19
20    require_once(DOKU_PLUGIN . 'dw2pdf/mpdf/mpdf.php');
21
22    class siteexportPDF extends mpdf {
23
24        private $debugObj = false;
25
26		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') $format .= '-L';
41
42            switch($conf['lang']) {
43                case 'zh':
44                case 'zh-tw':
45                case 'ja':
46                case 'ko':
47                    $mode = '+aCJK';
48                    break;
49                default:
50                    $mode = 'UTF-8-s';
51
52            }
53
54            // we're always UTF-8
55            parent::__construct($mode, $format);
56            $this->SetAutoFont(AUTOFONT_ALL);
57            $this->ignore_invalid_utf8 = true;
58            $this->tabSpaces = 4;
59            $this->debugObj = $debug;
60            $this->debug = $debug !== false;
61            $this->shrink_tables_to_fit = 1; // Does not shrink tables by default, only in emergency
62            $this->use_kwt = true; // avoids page-breaking in H1-H6 if a table follows directly
63        }
64
65        function message($msg, $vars=null, $lvl=1)
66        {
67            if ( $this->debugObj !== false ) {
68                $this->debugObj->message($msg, $vars, $lvl);
69            }
70        }
71
72        function Error($msg)
73        {
74            if ( $this->debugObj !== false && method_exists($this->debugObj, 'runtimeException') ) {
75                $this->debugObj->runtimeException($msg);
76            } else {
77                parent::Error($msg);
78            }
79        }
80
81        function GetFullPath(&$path,$basepath='') {
82
83        	// Full Path might return a doubled path like /~gamma/documentation/lib//~gamma/documentation/lib/tpl/clearreports/./_print-images/background-bottom.jpg
84
85			$path = str_replace("\\","/",$path); //If on Windows
86			$path = preg_replace('/^\/\//','http://',$path);	// mPDF 5.6.27
87			$regexp = '|^./|';	// Inadvertently corrects "./path/etc" and "//www.domain.com/etc"
88			$path = preg_replace($regexp,'',$path);
89
90        	if ( preg_match("/^.+\/\.\.\//", $path) ) {
91        		// ../ not at the beginning
92	        	$newpath = array();
93	        	$oldpath = explode('/', $path);
94
95	        	foreach( $oldpath as $slice ) {
96		        	if ( $slice == ".." && count($newpath) > 0 ) {
97			        	array_pop($newpath);
98			        	continue;
99		        	}
100
101		        	$newpath[] = $slice;
102	        	}
103
104	        	$path = implode('/', $newpath);
105        	}
106
107        	parent::GetFullPath($path, $basepath);
108
109        	$regex = "/^(". preg_quote(DOKU_BASE, '/') .".+)\\1/";
110        	if ( preg_match($regex, $path, $matches) ) {
111        		$path = preg_replace($regex, "\\1", $path);
112        	}
113
114        }
115
116        function OpenTag($tag, $attr) {
117            switch($tag) {
118                case 'BOOKMARK':
119                case 'TOCENTRY':
120                    if ( $attr['CONTENT'] ) {
121                        // resolve double encoding
122                        $attr['CONTENT'] = htmlspecialchars_decode($attr['CONTENT'], ENT_QUOTES);
123                    }
124                    break;
125            }
126            return parent::OpenTag($tag, $attr);
127        }
128    }
129
130    if ( file_exists(DOKU_PLUGIN . 'dw2pdf/mpdf/classes/cssmgr.php') && !class_exists('cssmgr', false)) {
131//*
132        require_once(DOKU_PLUGIN . 'siteexport/inc/patchCSSmgr.php');
133        $objPatch = new CSSMgrPatch(DOKU_PLUGIN . 'dw2pdf/mpdf/classes/cssmgr.php');
134        if ( $objPatch->redefineFunction(file_get_contents(DOKU_PLUGIN . 'siteexport/inc/readCSS.patch')) ) {
135            eval($objPatch->getCode());
136        }
137/*/
138//*/
139    }
140
141}
142