xref: /plugin/siteexport/inc/mpdf.php (revision 569232624e458180ad19e2514de3411c88df6fe8)
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/DokuPDF.class.php') ) {
15
16	global $conf;
17	// if(!defined('_MPDF_TEMP_PATH')) define('_MPDF_TEMP_PATH', $conf['tmpdir'].'/dwpdf/'.rand(1,1000).'/');
18
19    require_once(DOKU_PLUGIN . 'dw2pdf/DokuPDF.class.php');
20
21    class siteexportPDF extends DokuPDF {
22
23        var $debugObj = true;
24
25		function __construct($encoding, $debug=false) {
26
27            parent::__construct($encoding);
28            $this->debugObj = $debug;
29            $this->debug = true;
30            $this->shrink_tables_to_fit = 1; // Does not shrink tables by default, only in emergency
31            $this->use_kwt = true; // avoids page-breaking in H1-H6 if a table follows directly
32        }
33
34        function message($msg, $vars=null, $lvl=1)
35        {
36            if ( $this->debugObj !== false ) {
37                $this->debugObj->message($msg, $vars, $lvl);
38            }
39        }
40
41        function Error($msg)
42        {
43            if ( $this->debug !== false && $lvl == null && method_exists($this->debug, 'runtimeException') ) {
44                $this->debug->runtimeException($msg);
45            } else {
46                parent::Error($msg);
47            }
48        }
49
50        function GetFullPath(&$path,$basepath='') {
51
52        	// Full Path might return a doubled path like /~gamma/documentation/lib//~gamma/documentation/lib/tpl/clearreports/./_print-images/background-bottom.jpg
53
54			$path = str_replace("\\","/",$path); //If on Windows
55			$path = preg_replace('/^\/\//','http://',$path);	// mPDF 5.6.27
56			$regexp = '|^./|';	// Inadvertently corrects "./path/etc" and "//www.domain.com/etc"
57			$path = preg_replace($regexp,'',$path);
58
59        	if ( preg_match("/^.+\/\.\.\//", $path) ) {
60        		// ../ not at the beginning
61	        	$newpath = array();
62	        	$oldpath = explode('/', $path);
63
64	        	foreach( $oldpath as $slice ) {
65		        	if ( $slice == ".." && count($newpath) > 0 ) {
66			        	array_pop($newpath);
67			        	continue;
68		        	}
69
70		        	$newpath[] = $slice;
71	        	}
72
73	        	$path = implode('/', $newpath);
74        	}
75
76        	parent::GetFullPath($path, $basepath);
77
78        	$regex = "/^(". preg_quote(DOKU_BASE, '/') .".+)\\1/";
79        	if ( preg_match($regex, $path, $matches) ) {
80        		$path = preg_replace($regex, "\\1", $path);
81        	}
82
83        }
84
85        function OpenTag($tag, $attr) {
86            switch($tag) {
87                case 'BOOKMARK':
88                case 'TOCENTRY':
89                    if ( $attr['CONTENT'] ) {
90                        // resolve double encoding
91                        $attr['CONTENT'] = htmlspecialchars_decode($attr['CONTENT'], ENT_QUOTES);
92                    }
93                    break;
94            }
95            return parent::OpenTag($tag, $attr);
96        }
97    }
98}