forked from kazmiya/dokuwiki-plugin-preservefilenames
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.php
97 lines (88 loc) · 3.05 KB
/
common.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
94
95
96
97
<?php
/**
* DokuWiki Plugin PreserveFilenames / common.php
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Kazutaka Miyasaka <kazmiya@gmail.com>
*/
// must be run within DokuWiki
if (!defined('DOKU_INC')) {
die();
}
class PreserveFilenames_Common
{
/**
* Returns a correct basename
* (fixes PHP Bug #37738: basename() bug in handling multibyte filenames)
*
* @see http://bugs.php.net/37738
*/
function _correctBasename($path)
{
return rawurldecode(basename(preg_replace_callback(
'/%(?:[013-7][0-9a-fA-F]|2[0-46-9a-fA-F])/', // ASCII except for '%'
array(self, '_correctBasename_callback'),
rawurlencode($path)
)));
}
/**
* Callback function for _correctBasename() (only does rawurldecode)
*/
static function _correctBasename_callback($matches)
{
return rawurldecode($matches[0]);
}
/**
* Returns a sanitized safe filename
*
* @see http://en.wikipedia.org/wiki/Filename
*/
function _sanitizeFileName($filename)
{
$filename = preg_replace('/[\x00-\x1F\x7F]/', '', $filename); // control
$filename = preg_replace('/["*:<>?|\/\\\\]/', '_', $filename); // graphic
$filename = preg_replace('/[#&]/', '_', $filename); // dw technical issues
return $filename;
}
/**
* Builds appropriate "Content-Disposition" header strings
*
* @see http://greenbytes.de/tech/tc2231/
*/
function _buildContentDispositionHeader($download, $filename, $no_pathinfo = false)
{
global $conf;
$ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$type = $download ? 'attachment' : 'inline';
$ret = "Content-Disposition: $type;";
if (!preg_match('/[\x00-\x1F\x7F-\xFF"*:<>?|\/\\\\]/', $filename)) {
// no problem with filename-safe ascii characters
$ret .= ' filename="' . $filename . '";';
} elseif (preg_match('/(?:Gecko\/|Opera\/| Opera )/', $ua)) {
// use RFC2231 if accessed via RFC2231-compliant browser
$ret .= " filename*=UTF-8''" . rawurlencode($filename) . ';';
} elseif (
strpos($filename, '"') === false
&& strpos($ua, 'Safari/') !== false
&& strpos($ua, 'Chrome/') === false
&& preg_match('/Version\/[4-9]/', $ua)
&& !preg_match('/Mac OS X 10_[1-4]_/', $ua)
) {
// raw UTF-8 quoted-string
$ret .= ' filename="' . $filename . '"';
} elseif (
!$no_pathinfo
&& $conf['useslash']
&& $conf['userewrite']
&& strpos($ua, 'Safari/') !== false
&& strpos($ua, 'Chrome/') === false
) {
// leave filename-parm field empty
// (browsers can retrieve a filename from pathinfo of its url)
} else {
// fallback to the DokuWiki default
$ret .= ' filename="' . rawurlencode($filename) . '";';
}
return $ret;
}
}