1<?php 2/** 3 * Wrapper around the mpdf library class 4 * 5 * This class overrides some functions to make mpdf make use of DokuWiki' 6 * standard tools instead of its own. 7 * 8 * @author Andreas Gohr <andi@splitbrain.org> 9 */ 10global $conf; 11if(!defined('_MPDF_TEMP_PATH')) define('_MPDF_TEMP_PATH', $conf['tmpdir'] . '/dwpdf/' . rand(1, 1000) . '/'); 12if(!defined('_MPDF_TTFONTDATAPATH')) define('_MPDF_TTFONTDATAPATH', $conf['cachedir'] . '/mpdf_ttf/'); 13 14require_once __DIR__ . '/vendor/autoload.php'; 15 16/** 17 * Class DokuPDF 18 * Some DokuWiki specific extentions 19 */ 20class DokuPDF extends \Mpdf\Mpdf { 21 22 function __construct($pagesize = 'A4', $orientation = 'portrait') { 23 global $conf; 24 25 io_mkdir_p(_MPDF_TTFONTDATAPATH); 26 io_mkdir_p(_MPDF_TEMP_PATH); 27 28 $format = $pagesize; 29 if($orientation == 'landscape') { 30 $format .= '-L'; 31 } 32 33 switch($conf['lang']) { 34 case 'zh': 35 case 'zh-tw': 36 case 'ja': 37 case 'ko': 38 $mode = '+aCJK'; 39 break; 40 default: 41 $mode = 'UTF-8-s'; 42 43 } 44 45 // we're always UTF-8 46 parent::__construct( 47 array( 48 'mode' => $mode, 49 'format' => $format 50 ) 51 ); 52 $this->autoScriptToLang = true; 53 $this->baseScript = 1; 54 $this->autoVietnamese = true; 55 $this->autoArabic = true; 56 $this->autoLangToFont = true; 57 58 $this->ignore_invalid_utf8 = true; 59 $this->tabSpaces = 4; 60 } 61 62 /** 63 * Cleanup temp dir 64 */ 65 function __destruct() { 66 io_rmdir(_MPDF_TEMP_PATH, true); 67 } 68 69 /** 70 * Decode all paths, since DokuWiki uses XHTML compliant URLs 71 */ 72 function GetFullPath(&$path, $basepath = '') { 73 $path = htmlspecialchars_decode($path); 74 parent::GetFullPath($path, $basepath); 75 } 76 77 /** 78 * Override the mpdf _getImage function 79 * 80 * This function takes care of gathering the image data from HTTP or 81 * local files before passing the data back to mpdf's original function 82 * making sure that only cached file paths are passed to mpdf. It also 83 * takes care of checking image ACls. 84 */ 85 function _getImage(&$file, $firsttime = true, $allowvector = true, $orig_srcpath = false, $interpolation = false) { 86 global $conf; 87 88 // build regex to parse URL back to media info 89 $re = preg_quote(ml('xxx123yyy', '', true, '&', true), '/'); 90 $re = str_replace('xxx123yyy', '([^&\?]*)', $re); 91 92 // extract the real media from a fetch.php uri and determine mime 93 if(preg_match("/^$re/", $file, $m) || 94 preg_match('/[&\?]media=([^&\?]*)/', $file, $m) 95 ) { 96 $media = rawurldecode($m[1]); 97 list($ext, $mime) = mimetype($media); 98 } else { 99 list($ext, $mime) = mimetype($file); 100 } 101 102 // local files 103 $local = ''; 104 if(substr($file, 0, 9) == 'dw2pdf://') { 105 // support local files passed from plugins 106 $local = substr($file, 9); 107 } elseif(!preg_match('/(\.php|\?)/', $file)) { 108 $re = preg_quote(DOKU_URL, '/'); 109 // directly access local files instead of using HTTP, skip dynamic content 110 $local = preg_replace("/^$re/i", DOKU_INC, $file); 111 } 112 113 if(substr($mime, 0, 6) == 'image/') { 114 if(!empty($media)) { 115 // any size restrictions? 116 $w = $h = 0; 117 if(preg_match('/[\?&]w=(\d+)/', $file, $m)) $w = $m[1]; 118 if(preg_match('/[\?&]h=(\d+)/', $file, $m)) $h = $m[1]; 119 120 if(media_isexternal($media)) { 121 $local = media_get_from_URL($media, $ext, -1); 122 if(!$local) $local = $media; // let mpdf try again 123 } else { 124 $media = cleanID($media); 125 //check permissions (namespace only) 126 if(auth_quickaclcheck(getNS($media) . ':X') < AUTH_READ) { 127 $file = ''; 128 } 129 $local = mediaFN($media); 130 } 131 132 //handle image resizing/cropping 133 if($w && file_exists($local)) { 134 if($h) { 135 $local = media_crop_image($local, $ext, $w, $h); 136 } else { 137 $local = media_resize_image($local, $ext, $w, $h); 138 } 139 } 140 } elseif(media_isexternal($file)) { // fixed external URLs 141 $local = media_get_from_URL($file, $ext, $conf['cachetime']); 142 } 143 144 if($local) { 145 $file = $local; 146 $orig_srcpath = $local; 147 } 148 } 149 150 return parent::_getImage($file, $firsttime, $allowvector, $orig_srcpath, $interpolation); 151 } 152 153} 154