forked from pdclark/uploads-by-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-404-template.php
245 lines (183 loc) · 6.45 KB
/
class-404-template.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
<?php
/**
* Handle redirection through WordPress 404 Template
*/
class UBP_404_Template {
var $siteurl;
var $domain;
var $remote_path;
var $local_path;
var $response;
function __construct() {
// Only run for whitelisted paths
if ( !$this->allow_path() ) { return; }
$this->stream();
}
/**
* Stream files from publicly registered IP address through PHP
*/
public function stream() {
require dirname(__FILE__).'/class-get-public-ip.php';
$ip = new UBP_Get_Public_IP( $this->get_domain() );
// Send domain name in request headers so vhosts resolve
$args = array( 'headers' => array( 'Host' => $this->get_domain() ) );
// Route around local DNS by requesting by IP directly
$url = 'http://' . $this->get_auth() . $ip . $this->get_remote_path();
$this->response = wp_remote_get( $url, $args);
if ( !is_wp_error($this->response) && 200 == $this->response['response']['code'] ) {
$this->download();
}
}
public function download() {
if ( !function_exists('WP_Filesystem')) { require ABSPATH.'wp-admin/includes/file.php'; }
global $wp_filesystem; WP_Filesystem();
$u = wp_upload_dir();
$remove = str_replace( get_option( 'siteurl' ), '', $u['baseurl'] );
$basedir = str_replace( $remove, '', $u['basedir'] );
$abspath = $basedir . $this->get_dated_path();
$dir = dirname( $abspath );
if ( !is_dir( $dir ) && !wp_mkdir_p( $dir ) ) {
$this->display_and_exit( "Please check permissions. Could not create directory $dir" );
}
$saved_image = $wp_filesystem->put_contents( $abspath, $this->response['body'], FS_CHMOD_FILE ); // predefined mode settings for WP files
if ( $saved_image ) {
$path = $this->get_ms_adjusted_path();
$redirect = get_site_url( get_current_blog_id(), $path );
wp_redirect( $redirect );
exit;
}else {
$this->display_and_exit( "Please check permissions. Could not write image $dir" );
}
}
public function display_and_exit( $message=false ) {
global $wp_query;
status_header( 200 );
$wp_query->is_404 = false;
// Send debug message in response headers.
if ( $message ) { header('*Uploads-by-Proxy: ' . $message ); }
foreach( $this->response['headers'] as $name => $value ){
header( "$name: $value" );
}
echo $this->response['body'];
exit;
}
/**
* Only redirect for whitelisted paths
*/
public function allow_path() {
$path = $this->get_remote_path();
if ( empty( $path ) ) { return false; }
$allowed_paths = array(
$this->uploads_basedir(),
);
$allowed_paths = apply_filters( 'ubp_allowed_paths', $allowed_paths );
foreach ( $allowed_paths as $value ){
if ( false !== @strpos( $path, $value) ) { return true; }
}
return false;
}
/**
* Return path to uploads folder, relative to WordPress root directory
* @var string
*/
public function uploads_basedir() {
$uploads = wp_upload_dir();
return parse_url( $uploads['baseurl'], PHP_URL_PATH );
}
public function get_siteurl() {
if ( isset( $this->siteurl ) ) {
return $this->siteurl;
}
if ( defined( 'UBP_LIVE_DOMAIN' ) && false !== UBP_LIVE_DOMAIN ) {
// Legacy support
// Strip schema, slashes, and whitespace
$url = str_replace( array( 'http://', 'https://' ), '', UBP_LIVE_DOMAIN );
$url = 'http://' . $url;
}else if ( is_multisite() && ( $siteurl = get_option( '_ubp_site_url' ) ) ) {
$url = parse_url( $siteurl );
$url = 'http://' . $url['host'] . @$url['path'];
}else if ( is_multisite() && defined( 'UBP_SITEURL' ) && false !== UBP_SITEURL ) {
$details = get_blog_details();
$url = parse_url( UBP_SITEURL );
$ms_url = '';
if ( SUBDOMAIN_INSTALL ) {
$parts = explode( '.', $details->domain );
$ms_url = $parts[0] . '.' . $url['host'];
} else {
$ms_url = $url['host'] . $details->path;
}
$url = 'http://' . $ms_url . @$url['path'];
} else if ( defined( 'UBP_SITEURL' ) && false !== UBP_SITEURL ) {
$url = parse_url( UBP_SITEURL );
$url = 'http://' . $url['host'] . @$url['path'];
} else {
// Nothing set... Get original siteurl from database
remove_filter( 'option_siteurl', '_config_wp_siteurl' );
$url = get_option( 'siteurl' );
add_filter( 'option_siteurl', '_config_wp_siteurl' );
}
$this->siteurl = untrailingslashit( $url );
return $this->siteurl;
}
public function get_domain() {
if ( !isset( $this->domain ) ) {
$this->domain = parse_url( $this->get_siteurl(), PHP_URL_HOST );
}
return $this->domain;
}
public function get_dated_path() {
$path = $this->get_local_path();
if ( function_exists( 'is_multisite' ) && is_multisite() && strpos( $path, '/files/' ) === 0 ) {
$path = str_replace( '/files', '', $path );
}
return $path;
}
public function get_ms_adjusted_path() {
$path = $this->get_local_path();
//switch to the "new" multisite files location
if ( function_exists( 'is_multisite' ) && is_multisite() && strpos( $path, '/files/' ) === 0 ) {
$path = 'wp-content/uploads/sites/' . get_current_blog_id() . str_replace( '/files', '', $path );
}
return $path;
}
public function get_auth() {
if ( !isset( $this->auth ) ) {
$user = parse_url( $this->get_siteurl(), PHP_URL_USER );
$pass = parse_url( $this->get_siteurl(), PHP_URL_PASS );
if ( $user && $pass )
$this->auth = $user . ':' . $pass . '@';
elseif ( $user )
$this->auth = $user . '@';
else
$this->auth = '';
}
return $this->auth;
}
public function get_local_path() {
if ( isset( $this->local_path ) ) {
return $this->local_path;
}
// If local install is in a subdirectory, modify path to request from WordPress root
$local_wordpress_path = parse_url( get_site_url(), PHP_URL_PATH ) . '/';
$requested_path = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
if (substr($requested_path, 0, strlen($local_wordpress_path)) == $local_wordpress_path) {
//$requested_path = substr($requested_path, strlen($local_wordpress_path), strlen($requested_path));
$requested_path = substr($requested_path, strlen($local_wordpress_path)-1, strlen($requested_path));
}
$this->local_path = $requested_path;
return $this->local_path;
}
public function get_remote_path() {
if ( isset( $this->remote_path ) ) {
return $this->remote_path;
}
// If remote install is in a subdirectory, prepend the remote path
$remote_path = parse_url( $this->get_siteurl(), PHP_URL_PATH );
if ( !empty( $remote_path ) ) {
$this->remote_path = $remote_path . $this->get_local_path();
}else {
$this->remote_path = $this->get_local_path();
}
return $this->remote_path;
}
}