-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxsendfile.class.php
391 lines (340 loc) · 9.78 KB
/
xsendfile.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
/*
sendfile emulator - emulates xsendfile with symlinks
2017 by Lukas Mueller
This program is free software: you can redistribute it and/or modify
it under the terms of the Lesser GNU General Public License as published by
the Free Software Foundation, either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Lesser GNU General Public License for more details.
You should have received a copy of the Lesser GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
*/
namespace mulu;
class xsendfile
{
protected static $_linkDir = '';
protected static $_linkDirUri = '';
protected static $_expireTime = 60; // default: 1 minute. After the download started it will be finished, wheter the file is "deleted" or not.
protected static $_salt = '';
protected static $_dirOk = false;
protected static $_disallowedExt = [];
protected static $_callbacks = [];
protected static $_headerWorker = [__CLASS__, 'headerWorker'];
protected static $_externalUrlSymlink = false; // used, when the sendfile is external
/**
* Defines the directory for symlinks
* @param string the directory name
* @param bool create directory if not exist
* @return bool directory successfully set
*/
public static function setLinkDir($dir, $create = true)
{
// non existing dir
if (!is_dir($dir))
{
if ($create)
{
// try to create given directory
if (!mkdir($dir))
{
trigger_error('Could not create directory', E_ERROR);
return false;
}
}
else
{
trigger_error('Directory does not exist', E_ERROR);
return false;
}
}
self::$_linkDir = $dir;
self::$_dirOk = is_writable($dir);
return self::$_dirOk;
}
/**
* Sets disallowed extensions
* @param array the extensions
*/
public static function setDisallowedExtensions($extensions)
{
self::$_disallowedExt = $extensions;
return true;
}
/**
* Add callback function
* @param string callback name
* @param callable a callable function
*/
public static function registerCallback($type, $function)
{
if (is_callable($function))
{
self::$_callbacks[$type] = $function;
}
return false;
}
/**
* Sets the uri to the symlinks
* So you can use CDN-plugins in Wordpress or so
* @param string the uri of the symlink-folder
*/
public static function setLinkDirUri($uri)
{
self::$_linkDirUri = $uri;
return true;
}
/**
* Sets the salt to use
* @param string the salt
*/
public static function setSalt($salt)
{
self::$_salt = $salt;
return true;
}
/**
* Sets the expire time of a symlink
* @param integer expire in seconds
* @return bool expire successfully set
*/
public static function setExpireTime($seconds)
{
if (is_numeric($seconds))
{
self::$_expireTime = $seconds;
return true;
}
return false;
}
/**
* Sets the symlink-path when external link
* @param string the path (variables: %host%, %path%)
* @return bool
*/
public static function setExtSymlinkPath($symlink)
{
self::$_externalUrlSymlink = $symlink;
}
/**
* returns true if is correct configured
* @return bool plugin ok
*/
public static function isActive()
{
$result = true;
// if dir is not set successfully
if (!self::$_dirOk)
{
$result = false;
}
// or the apache module is activated
elseif (php_sapi_name() == 'apache2handler')
{
$result = false;
}
return $result;
}
/**
* Catches header, creates symlinks an redirects
* @param callable a function that works with the header-data (for use with zend, shopware, etc)
*/
public static function rewrite($headerWorker = null)
{
if (is_null($headerWorker))
$headerWorker = self::$_headerWorker;
error_reporting(E_ALL);
// not active or header already sent
if (!self::isActive())
return false;
$headerWorker('del', 'content-disposition');
$headerWorker('set', 'Content-Type', 'text/plain');
$sendfile = false;
$filename = '';
$headers = $headerWorker('getall');
foreach($headers as $header)
{
// explode key and value
$data = explode(': ', $header);
switch($data[0])
{
case 'X-Sendfile': // Apache version
case 'X-Accel-Redirect': // nginx version
case 'X-Lighttpd-Sendfile': // lighty version
$sendfile = $data[1];
break;
case 'Content-Disposition': // includes the filename
if(preg_match('~filename=(.*)$~is', $data[1], $matchFn))
$filename = trim($matchFn[1], '";');
break;
}
}
// check for external link
$sendfileInfo = parse_url($sendfile);
if (!empty($sendfileInfo['scheme']))
{
// if not set quit
if (empty(self::$_externalUrlSymlink))
return false;
$sendfile = self::$_externalUrlSymlink;
$sendfile = str_replace('%host%', $sendfileInfo['host'], $sendfile);
$sendfile = str_replace('%path%', $sendfileInfo['path'], $sendfile);
}
if ($sendfile === false)
return;
// now remove no longer needed headers
$headerWorker('del', 'X-Sendfile');
$headerWorker('del', 'X-Accel-Redirect');
$headerWorker('del', 'X-Lighttpd-Sendfile');
$headerWorker('del', 'Content-Disposition');
// debug things, let it in, doesnt matter when we forward and makes debugging easier
$headerWorker('set', 'Content-Type', 'text/plain');
// break the loop if available secret dir found
while(true)
{
$secret = sha1($sendfile . rand() . self::$_salt); // secret hash for symlink directory
$secretDir = self::$_linkDir . $secret . '/'; // path to secret directory
if (!is_dir($secretDir))
{
$sendfile = self::makeAbsPath($sendfile);
$secretDir = self::makeAbsPath($secretDir);
// if no name, get filename
if (empty($filename))
$filename = basename($sendfile);
// check extension and forbid
if (in_array(pathinfo($filename, PATHINFO_EXTENSION), self::$_disallowedExt))
{
$noAuth = true;
// call callback to check if redirect is allowed
if (isset(self::$_callbacks['forbiddenExtension']))
{
$result = call_user_func(self::$_callbacks['forbiddenExtension'], pathinfo($filename, PATHINFO_EXTENSION));
if ($result === true)
$noAuth = false;
}
if ($noAuth)
{
$headerWorker('set', 'HTTP/1.0 403 Forbidden');
return;
}
}
mkdir($secretDir);
symlink(urldecode($sendfile), $secretDir . utf8_decode(urldecode($filename)));
// create web path
if (!empty(self::$_linkDirUri))
{
$weburi = self::$_linkDirUri . $secret . '/' . $filename;
}
$headerWorker('set', 'Location', $weburi, 1);
return; // end the loop
}
}
}
/**
* Makes a relative path absolute
* @param str relative path
* @return absolute path
*/
public static function makeAbsPath($path)
{
// On windows symlinks must have absolute paths
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
{
$matchWinAbsolute = '~^([a-z]{1}):(\\/|\\\\)~i'; // regex for valid absolute path on windows
// no unc-path and no absolute path
if (substr($path, 0, 2) != '\\\\' && !preg_match($matchWinAbsolute, $path, $arMatches))
{
// is a absolute path but without the drive letter
if (substr($path, 0, 1) == '/')
$path = substr(getenv('SCRIPT_FILENAME'), 0, 1) . ':' . $path;
else // relative path
$path = dirname(getenv('SCRIPT_FILENAME')) . '/' . $path;
}
}
// Linux, Mac
// without trailing slash -> relative
elseif (substr($path, 0, 1) !== '/')
{
$path = dirname(getenv('SCRIPT_FILENAME')) . '/' . $path;
}
return $path;
}
/*
* Implements the default-functions for header-handling
* @param str action to run
* @param str name of the header
* @param str value of the header
* @return list of headers of result of the action
*/
protected static function headerWorker($action = 'get', $name = '', $value = '')
{
if (headers_sent())
return ($action == 'getall') ? [] : '';
switch($action)
{
// returns all already set header
case 'getall':
return headers_list();
break;
// removes a header
case 'del';
return header_remove($name);
break;
// sets a header and overwrites it
case 'set':
if (substr($name, 0, 5) == 'HTTP/')
return header($name);
else
return header($name . ': ' . $value, true);
break;
}
}
/**
* Register the rewrite-method as shutdown function
*/
public static function register()
{
register_shutdown_function(function()
{
$class = __CLASS__;
$class::rewrite();
});
}
/**
* Runs the garbage collector
* @return bool Returns if directories have been deleted
**/
public static function runGBC()
{
if (!self::isActive())
return false;
// save old value and disable error reporting
$oldER = error_reporting();
error_reporting(E_ERROR);
$result = false;
$linkdirs = glob(self::$_linkDir . '*');
foreach($linkdirs as $linkdir)
{
// check creation date
if (filemtime($linkdir) > (time() - self::$_expireTime))
continue;
// delete all files in dir
array_walk(glob($linkdir . '/*'), array(__CLASS__, 'unlink'));
// and the dir itself
if (rmdir($linkdir))
$result = true;
}
// reset error reporting
error_reporting($oldER);
return $result;
}
/* Wrapper for unlink and array_walk */
protected static function unlink($file, $key = '')
{
return unlink($file);
}
}
?>