/pdf/DokuPDF.class.php

https://github.com/Lemelisk/examPlugin · PHP · 93 lines · 53 code · 16 blank · 24 comment · 12 complexity · ec0221db5a8f514beabedf22726eb22c MD5 · raw file

  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. if(!defined('_MPDF_TEMP_PATH')) define('_MPDF_TEMP_PATH', $conf['tmpdir']);
  11. if(!defined('_MPDF_TTFONTDATAPATH')) define('_MPDF_TTFONTDATAPATH',$conf['cachedir'].'/mpdf_ttf/');
  12. require_once(dirname(__FILE__)."/mpdf/mpdf.php");
  13. class DokuPDF extends mpdf {
  14. function __construct(){
  15. io_mkdir_p(_MPDF_TTFONTDATAPATH);
  16. // we're always UTF-8
  17. parent::__construct('UTF-8-s');
  18. $this->SetAutoFont(AUTOFONT_ALL);
  19. $this->ignore_invalid_utf8 = true;
  20. }
  21. /**
  22. * Decode all paths, since DokuWiki uses XHTML compliant URLs
  23. */
  24. function GetFullPath(&$path,$basepath=''){
  25. $path = htmlspecialchars_decode($path);
  26. parent::GetFullPath($path, $basepath);
  27. }
  28. /**
  29. * Override the mpdf _getImage function
  30. *
  31. * This function takes care of gathering the image data from HTTP or
  32. * local files before passing the data back to mpdf's original function
  33. * making sure that only cached file paths are passed to mpdf. It also
  34. * takes care of checking image ACls.
  35. */
  36. function _getImage(&$file, $firsttime=true, $allowvector=true, $orig_srcpath=false){
  37. list($ext,$mime) = mimetype($file);
  38. //if(substr($mime,0,6) == 'image/'){
  39. if(true) {
  40. // build regex to parse URL back to media info
  41. $re = preg_quote(ml('xxx123yyy'),'/');
  42. $re = str_replace('xxx123yyy','([^&\?]*)',$re);
  43. if(preg_match("/$re/",$file,$m) ||
  44. preg_match('/[&\?]media=([^&\?]*)/',$file,$m)){
  45. $media = rawurldecode($m[1]);
  46. if(preg_match('/[\?&]w=(\d+)/',$file, $m)) $w = $m[1];
  47. if(preg_match('/[\?&]h=(\d+)/',$file, $m)) $h = $m[1];
  48. if(preg_match('/^https?:\/\//',$media)){
  49. $local = media_get_from_URL($media,$ext,-1);
  50. if(!$local) $local = $media; // let mpdf try again
  51. }else{
  52. $media = cleanID($media);
  53. //check permissions (namespace only)
  54. if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ){
  55. $file = '';
  56. }
  57. $local = mediaFN($media);
  58. }
  59. //handle image resizing/cropping
  60. if($w){
  61. if($h){
  62. $local = media_crop_image($local,$ext,$w,$h);
  63. }else{
  64. $local = media_resize_image($local,$ext,$w,$h);
  65. }
  66. }
  67. }elseif(preg_match('/^https?:\/\//',$file)){ // fixed external URLs
  68. $local = media_get_from_URL($file,$ext,$conf['cachetime']);
  69. }
  70. if($local){
  71. $file = $local;
  72. $orig_srcpath = $local;
  73. }
  74. }
  75. return parent::_getImage($file, $firsttime, $allowvector, $orig_srcpath);
  76. }
  77. }