11ef68647SAndreas Gohr<?php 25db42babSAndreas Gohr/** 35db42babSAndreas Gohr * Wrapper around the mpdf library class 45db42babSAndreas Gohr * 55db42babSAndreas Gohr * This class overrides some functions to make mpdf make use of DokuWiki' 65db42babSAndreas Gohr * standard tools instead of its own. 75db42babSAndreas Gohr * 85db42babSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 95db42babSAndreas Gohr */ 10c1acf1feSGerrit Uitslagglobal $conf; 111c9fae9eSAndreas Gohrif(!defined('_MPDF_TEMP_PATH')) define('_MPDF_TEMP_PATH', $conf['tmpdir'].'/dwpdf/'.rand(1,1000).'/'); 12b2853a2aSAndreas Gohrif(!defined('_MPDF_TTFONTDATAPATH')) define('_MPDF_TTFONTDATAPATH',$conf['cachedir'].'/mpdf_ttf/'); 13ca0441e7SGerrit Uitslag 141ef68647SAndreas Gohrrequire_once(dirname(__FILE__)."/mpdf/mpdf.php"); 151ef68647SAndreas Gohr 16ca0441e7SGerrit Uitslag/** 17ca0441e7SGerrit Uitslag * Class DokuPDF 18ca0441e7SGerrit Uitslag * Some DokuWiki specific extentions 19ca0441e7SGerrit Uitslag */ 201ef68647SAndreas Gohrclass DokuPDF extends mpdf { 211ef68647SAndreas Gohr 226ea88a05SAndreas Gohr function __construct($pagesize='A4', $orientation='portrait'){ 235bd9be9cSGerrit Uitslag global $conf; 245bd9be9cSGerrit Uitslag 25b2853a2aSAndreas Gohr io_mkdir_p(_MPDF_TTFONTDATAPATH); 261c9fae9eSAndreas Gohr io_mkdir_p(_MPDF_TEMP_PATH); 271ef68647SAndreas Gohr 286ea88a05SAndreas Gohr $format = $pagesize; 29*acf3824dSGerrit Uitslag if($orientation == 'landscape') { 30*acf3824dSGerrit Uitslag $format .= '-L'; 31*acf3824dSGerrit Uitslag } 326ea88a05SAndreas Gohr 335bd9be9cSGerrit Uitslag switch($conf['lang']) { 345bd9be9cSGerrit Uitslag case 'zh': 355bd9be9cSGerrit Uitslag case 'zh-tw': 365bd9be9cSGerrit Uitslag case 'ja': 375bd9be9cSGerrit Uitslag case 'ko': 385bd9be9cSGerrit Uitslag $mode = '+aCJK'; 395bd9be9cSGerrit Uitslag break; 405bd9be9cSGerrit Uitslag default: 415bd9be9cSGerrit Uitslag $mode = 'UTF-8-s'; 425bd9be9cSGerrit Uitslag 435bd9be9cSGerrit Uitslag } 445bd9be9cSGerrit Uitslag 451ef68647SAndreas Gohr // we're always UTF-8 465bd9be9cSGerrit Uitslag parent::__construct($mode, $format); 471ef68647SAndreas Gohr $this->SetAutoFont(AUTOFONT_ALL); 481ef68647SAndreas Gohr $this->ignore_invalid_utf8 = true; 492585efdfSKlap-in $this->tabSpaces = 4; 501c9fae9eSAndreas Gohr } 511ef68647SAndreas Gohr 521c9fae9eSAndreas Gohr /** 531c9fae9eSAndreas Gohr * Cleanup temp dir 541c9fae9eSAndreas Gohr */ 551c9fae9eSAndreas Gohr function __destruct(){ 56*acf3824dSGerrit Uitslag io_rmdir(_MPDF_TEMP_PATH, true); 571ef68647SAndreas Gohr } 581ef68647SAndreas Gohr 59a06728a6SAndreas Gohr /** 60a06728a6SAndreas Gohr * Decode all paths, since DokuWiki uses XHTML compliant URLs 61a06728a6SAndreas Gohr */ 62a06728a6SAndreas Gohr function GetFullPath(&$path,$basepath=''){ 63a06728a6SAndreas Gohr $path = htmlspecialchars_decode($path); 64a06728a6SAndreas Gohr parent::GetFullPath($path, $basepath); 65a06728a6SAndreas Gohr } 661ef68647SAndreas Gohr 671ef68647SAndreas Gohr /** 681ef68647SAndreas Gohr * Override the mpdf _getImage function 691ef68647SAndreas Gohr * 701ef68647SAndreas Gohr * This function takes care of gathering the image data from HTTP or 711ef68647SAndreas Gohr * local files before passing the data back to mpdf's original function 721ef68647SAndreas Gohr * making sure that only cached file paths are passed to mpdf. It also 731ef68647SAndreas Gohr * takes care of checking image ACls. 741ef68647SAndreas Gohr */ 751ef68647SAndreas Gohr function _getImage(&$file, $firsttime=true, $allowvector=true, $orig_srcpath=false){ 762585efdfSKlap-in global $conf; 77bfbc63f7SAndreas Gohr 781ef68647SAndreas Gohr // build regex to parse URL back to media info 79eac16b1dSAndreas Gohr $re = preg_quote(ml('xxx123yyy','',true,'&',true),'/'); 8008b99b84SAndreas Gohr $re = str_replace('xxx123yyy','([^&\?]*)',$re); 811ef68647SAndreas Gohr 82bfbc63f7SAndreas Gohr // extract the real media from a fetch.php uri and determine mime 83eac16b1dSAndreas Gohr if(preg_match("/^$re/",$file,$m) || 8408b99b84SAndreas Gohr preg_match('/[&\?]media=([^&\?]*)/',$file,$m)){ 851ef68647SAndreas Gohr $media = rawurldecode($m[1]); 86bfbc63f7SAndreas Gohr list($ext,$mime) = mimetype($media); 87bfbc63f7SAndreas Gohr }else{ 88bfbc63f7SAndreas Gohr list($ext,$mime) = mimetype($file); 89bfbc63f7SAndreas Gohr } 901ef68647SAndreas Gohr 915aa96d34SAndreas Gohr // local files 92ca0441e7SGerrit Uitslag $local = ''; 935aa96d34SAndreas Gohr if(substr($file,0,9) == 'dw2pdf://'){ 945aa96d34SAndreas Gohr // support local files passed from plugins 955aa96d34SAndreas Gohr $local = substr($file,9); 965aa96d34SAndreas Gohr }elseif(!preg_match('/(\.php|\?)/',$file)){ 975aa96d34SAndreas Gohr $re = preg_quote(DOKU_URL,'/'); 985aa96d34SAndreas Gohr // directly access local files instead of using HTTP, skip dynamic content 995aa96d34SAndreas Gohr $local = preg_replace("/^$re/i",DOKU_INC,$file); 1005aa96d34SAndreas Gohr } 1015aa96d34SAndreas Gohr 102bfbc63f7SAndreas Gohr if(substr($mime,0,6) == 'image/'){ 103bfbc63f7SAndreas Gohr if(!empty($media)){ 104bfbc63f7SAndreas Gohr // any size restrictions? 105ca0441e7SGerrit Uitslag $w = $h = 0; 10608b99b84SAndreas Gohr if(preg_match('/[\?&]w=(\d+)/',$file, $m)) $w = $m[1]; 10708b99b84SAndreas Gohr if(preg_match('/[\?&]h=(\d+)/',$file, $m)) $h = $m[1]; 10808b99b84SAndreas Gohr 109*acf3824dSGerrit Uitslag if(media_isexternal($media)){ 11008b99b84SAndreas Gohr $local = media_get_from_URL($media,$ext,-1); 11108b99b84SAndreas Gohr if(!$local) $local = $media; // let mpdf try again 1121ef68647SAndreas Gohr }else{ 1131ef68647SAndreas Gohr $media = cleanID($media); 1141ef68647SAndreas Gohr //check permissions (namespace only) 1151ef68647SAndreas Gohr if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ){ 1161ef68647SAndreas Gohr $file = ''; 1171ef68647SAndreas Gohr } 1181ef68647SAndreas Gohr $local = mediaFN($media); 1191ef68647SAndreas Gohr } 1201ef68647SAndreas Gohr 1211ef68647SAndreas Gohr //handle image resizing/cropping 122ecaaf7b9SAndreas Gohr if($w && file_exists($local)){ 12308b99b84SAndreas Gohr if($h){ 1241ef68647SAndreas Gohr $local = media_crop_image($local,$ext,$w,$h); 1251ef68647SAndreas Gohr }else{ 1261ef68647SAndreas Gohr $local = media_resize_image($local,$ext,$w,$h); 1271ef68647SAndreas Gohr } 1281ef68647SAndreas Gohr } 129*acf3824dSGerrit Uitslag }elseif(media_isexternal($file)){ // fixed external URLs 13008b99b84SAndreas Gohr $local = media_get_from_URL($file,$ext,$conf['cachetime']); 1311ef68647SAndreas Gohr } 1321ef68647SAndreas Gohr 1331ef68647SAndreas Gohr if($local){ 134a06728a6SAndreas Gohr $file = $local; 1351ef68647SAndreas Gohr $orig_srcpath = $local; 1361ef68647SAndreas Gohr } 1371ef68647SAndreas Gohr } 1381ef68647SAndreas Gohr 1391ef68647SAndreas Gohr return parent::_getImage($file, $firsttime, $allowvector, $orig_srcpath); 1401ef68647SAndreas Gohr } 1411ef68647SAndreas Gohr 1421ef68647SAndreas Gohr} 143