This repository has been archived by the owner on Mar 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset.php
61 lines (50 loc) · 1.79 KB
/
asset.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
<?php
define('WP_USE_THEMES', false);
require(dirname(__FILE__).'/../../../wp/wp-blog-header.php');
// extract hash
$hashed = base64_decode($_REQUEST['asset']);
$params = explode('|', $hashed);
// bail out early
if (!is_numeric($params[0])) {
header('HTTP/1.0 404 Not found');
exit;
}
// what is what
$id = $params[0];
$size = (isset($params[1]) && !empty($params[1])) ? $params[1] : false;
$disp = (isset($params[2]) && !empty($params[2])) ? $params[2] : 'inline';
// check private
$private = get_post_meta($id, 'private', true);
if ($private === 'yes' && !is_user_logged_in()) {
header('HTTP/1.0 403 Forbidden');
exit;
}
// return file content
if ( $file = get_post_meta( $id, '_wp_attached_file', true ) ) {
if ( ($uploads = wp_get_upload_dir()) && false === $uploads['error'] ) {
$path = $uploads['basedir'].'/'.$file;
$name = basename($path);
if ($size) {
$image = image_downsize( $id, $size );
$url = $image[0];
$path = str_replace($uploads['baseurl'], $uploads['basedir'], $url);
}
if (is_file($path)) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $path);
$ext = pathinfo($path, PATHINFO_EXTENSION);
header('HTTP/1.0 200 OK');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('Content-Type: '.$mime);
header('Content-Disposition: '.$disp.'; filename='.$name.'.'.$ext);
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($path));
readfile($path);
exit;
}
}
}
// final header when no file was delivered
header('HTTP/1.0 404 Not Found');
exit;