forked from cosmocode/dokuwiki-plugin-dw2pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DokuImageProcessorDecorator.class.php
93 lines (79 loc) · 3.46 KB
/
DokuImageProcessorDecorator.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace dokuwiki\plugin\dw2pdf;
class DokuImageProcessorDecorator extends \Mpdf\Image\ImageProcessor {
/**
* Override the mpdf _getImage function
*
* This function takes care of gathering the image data from HTTP or
* local files before passing the data back to mpdf's original function
* making sure that only cached file paths are passed to mpdf. It also
* takes care of checking image ACls.
*/
public function getImage (&$file, $firsttime = true, $allowvector = true, $orig_srcpath = false, $interpolation = false) {
list($file, $orig_srcpath) = self::adjustGetImageLinks($file, $orig_srcpath);
return parent::getImage($file, $firsttime, $allowvector, $orig_srcpath, $interpolation);
}
public static function adjustGetImageLinks($file, $orig_srcpath) {
global $conf;
// build regex to parse URL back to media info
$re = preg_quote(ml('xxx123yyy', '', true, '&', true), '/');
$re = str_replace('xxx123yyy', '([^&\?]*)', $re);
// extract the real media from a fetch.php uri and determine mime
if(preg_match("/^$re/", $file, $m) ||
preg_match('/[&\?]media=([^&\?]*)/', $file, $m)
) {
$media = rawurldecode($m[1]);
list($ext, $mime) = mimetype($media);
} else {
list($ext, $mime) = mimetype($file);
}
// local files
$local = '';
if(substr($file, 0, 9) == 'dw2pdf://') {
// support local files passed from plugins
$local = substr($file, 9);
} elseif(!preg_match('/(\.php|\?)/', $file)) {
$re = preg_quote(DOKU_URL, '/');
// directly access local files instead of using HTTP, skip dynamic content
$local = preg_replace("/^$re/i", DOKU_INC, $file);
}
if(substr($mime, 0, 6) == 'image/') {
if(!empty($media)) {
// any size restrictions?
$w = $h = 0;
$rev = '';
if(preg_match('/[\?&]w=(\d+)/', $file, $m)) $w = $m[1];
if(preg_match('/[\?&]h=(\d+)/', $file, $m)) $h = $m[1];
if(preg_match('/[&\?]rev=(\d+)/', $file, $m)) $rev = $m[1];
if(media_isexternal($media)) {
$local = media_get_from_URL($media, $ext, -1);
if(!$local) $local = $media; // let mpdf try again
} else {
$media = cleanID($media);
//check permissions (namespace only)
if(auth_quickaclcheck(getNS($media) . ':X') < AUTH_READ) {
$file = '';
$local = '';
} else {
$local = mediaFN($media, $rev);
}
}
//handle image resizing/cropping
if($w && file_exists($local)) {
if($h) {
$local = media_crop_image($local, $ext, $w, $h);
} else {
$local = media_resize_image($local, $ext, $w, $h);
}
}
} elseif(!file_exists($local) && media_isexternal($file)) { // fixed external URLs
$local = media_get_from_URL($file, $ext, $conf['cachetime']);
}
if($local) {
$file = $local;
$orig_srcpath = $local;
}
}
return [$file, $orig_srcpath];
}
}