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