-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-twicpics-urls-manager.php
467 lines (394 loc) · 12.2 KB
/
class-twicpics-urls-manager.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<?php
/**
* TwicPics URLs Manager
*
* @package TwicPics
*/
defined( 'ABSPATH' ) || die( 'ERROR !' );
/**
* URL constants as defined in the PHP Manual under "Constants usable with
* http_build_url()".
*
* @see http://us2.php.net/manual/en/http.constants.php#http.constants.url
*/
if ( ! defined( 'HTTP_URL_REPLACE' ) ) {
define( 'HTTP_URL_REPLACE', 1 );
}
if ( ! defined( 'HTTP_URL_JOIN_PATH' ) ) {
define( 'HTTP_URL_JOIN_PATH', 2 );
}
if ( ! defined( 'HTTP_URL_JOIN_QUERY' ) ) {
define( 'HTTP_URL_JOIN_QUERY', 4 );
}
if ( ! defined( 'HTTP_URL_STRIP_USER' ) ) {
define( 'HTTP_URL_STRIP_USER', 8 );
}
if ( ! defined( 'HTTP_URL_STRIP_PASS' ) ) {
define( 'HTTP_URL_STRIP_PASS', 16 );
}
if ( ! defined( 'HTTP_URL_STRIP_AUTH' ) ) {
define( 'HTTP_URL_STRIP_AUTH', 32 );
}
if ( ! defined( 'HTTP_URL_STRIP_PORT' ) ) {
define( 'HTTP_URL_STRIP_PORT', 64 );
}
if ( ! defined( 'HTTP_URL_STRIP_PATH' ) ) {
define( 'HTTP_URL_STRIP_PATH', 128 );
}
if ( ! defined( 'HTTP_URL_STRIP_QUERY' ) ) {
define( 'HTTP_URL_STRIP_QUERY', 256 );
}
if ( ! defined( 'HTTP_URL_STRIP_FRAGMENT' ) ) {
define( 'HTTP_URL_STRIP_FRAGMENT', 512 );
}
if ( ! defined( 'HTTP_URL_STRIP_ALL' ) ) {
define( 'HTTP_URL_STRIP_ALL', 1024 );
}
class TwicPicsUrlsManager {
public function __construct() {
$this->_website_url = ( isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? "https" : "http" ) . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
}
/**
* Provides the ability to write logs in JavaScript.
*
* @param string $log The log to write
*/
private function write_logs( $log ) {
echo '<script type="text/javascript">console.log( "twicpics",' . wp_json_encode( $log ) . ');</script>';
}
/**
* Gets the original size URL of the image by simply removing the -{width}x{height} added by WordPress
*
* @param string $img_url the URL of the originaly requested image (maybe cropped).
* @return string the full size URL of the image
*/
public function get_original_size_url( $img_url ) {
return preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $img_url );
// global $wpdb;
// $base_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $img_url );
// $image = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid=%s OR guid=%s;", $base_url, $img_url ) );
// if ( ! empty( $image ) ) {
// return wp_get_attachment_image_src( (int) $image[0], 'full' )[0];
// } else {
// return preg_replace( '/(.+)\-\d+x\d+.*(\..+)/', '$1$2', $img_url );
// }
}
/**
* Handle relative paths.
*
* @param $website_url The URL of the website
* @param $img_url The URL of the image
*/
public function get_absolute_url( $website_url, $img_url ) {
$url_parts = wp_parse_url( $img_url );
return $this->http_build_url( explode( '?', $website_url )[0], $url_parts );
}
/**
* Build a URL.
*
* The parts of the second URL will be merged into the first according to
* the flags argument.
*
* @param mixed $url (part(s) of) an URL in form of a string or
* associative array like parse_url() returns
* @param mixed $parts same as the first argument
* @param int $flags a bitmask of binary or'ed HTTP_URL constants;
* HTTP_URL_REPLACE is the default
* @param array $new_url if set, it will be filled with the parts of the
* composed url like parse_url() would return
* @return string
*/
private function http_build_url( $url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new_url = array() ) {
// Initialization
static $all_keys = array( 'scheme', 'user', 'pass', 'host', 'port', 'path', 'query', 'fragment' );
static $all_keys_flipped;
static $server_https;
static $default_host;
static $request_uri;
static $request_uri_no_query;
static $request_uri_path;
if ( ! isset( $all_keys_flipped ) ) {
$all_keys_flipped = array_flip( $all_keys );
}
if ( ! isset( $server_https ) ) {
$server_https = ! empty( $_SERVER['HTTPS'] ) && strtolower( $_SERVER['HTTPS'] ) === 'on';
}
if ( ! isset( $default_host ) ) {
// Avoid this autodetection, it is copy-exact from C code, but $_SERVER['HTTP_HOST'] is vulnerable.
$default_host =
isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST']
: ( isset( $_SERVER['SERVER_NAME'] ) ? $_SERVER['SERVER_NAME']: '' );
if ( '' === $default_host ) {
$default_host = function_exists( 'gethostname' ) ? gethostname() : php_uname( 'n' );
}
}
if ( ! isset( $request_uri ) ) {
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
$request_uri = $_SERVER['REQUEST_URI'];
} else {
$request_uri = '/';
}
}
if ( ! isset( $request_uri_no_query ) ) {
$request_uri_no_query = preg_replace( '~^([^\?]*).*$~', '$1', $request_uri );
}
if ( ! isset( $request_uri_path ) ) {
$request_uri_path = substr( $request_uri_no_query, 0, strrpos( $request_uri_no_query, '/' ) + 1 );
}
// Translate the flags from the single input parameter.
$JOIN_PATH = ( ( $flags | HTTP_URL_JOIN_PATH ) === $flags );
$JOIN_QUERY = ( ( $flags | HTTP_URL_JOIN_QUERY ) === $flags );
$STRIP_USER = ( ( $flags | HTTP_URL_STRIP_USER ) === $flags );
$STRIP_PASS = ( ( $flags | HTTP_URL_STRIP_PASS ) === $flags );
$STRIP_PORT = ( ( $flags | HTTP_URL_STRIP_PORT ) === $flags );
$STRIP_PATH = ( ( $flags | HTTP_URL_STRIP_PATH ) === $flags );
$STRIP_QUERY = ( ( $flags | HTTP_URL_STRIP_QUERY ) === $flags );
$STRIP_FRAGMENT = ( ( $flags | HTTP_URL_STRIP_FRAGMENT ) === $flags );
// Parse and validate the input URLs.
if ( ! is_array( $url ) ) {
$url = wp_parse_url( $url );
}
if ( ! is_array( $parts ) ) {
$parts = wp_parse_url( $parts );
}
$url = array_intersect_key( $url, $all_keys_flipped );
$parts = array_intersect_key( $parts, $all_keys_flipped );
// Unfortunately the 'query' part can not be an array or object type.
if ( isset( $url['query'] ) && ! is_string( $url['query'] ) ) {
unset( $url['query'] );
}
// Unfortunately the 'query' part can not be an array or object type.
if ( isset( $parts['query'] ) && ! is_string( $parts['query'] ) ) {
unset( $parts['query'] );
}
foreach ( $all_keys as $key ) {
if ( 'port' === $key ) {
if ( isset( $url[ $key ] ) ) {
$url[ $key ] = (int) $url[ $key ];
if ( $url[ $key ] <= 0 || $url[ $key ] >= 65535 ) {
unset( $url[ $key ] );
}
}
if ( isset( $parts[ $key ] ) ) {
$parts[ $key ] = (int) $parts[ $key ];
if ( $parts[ $key ] <= 0 || $parts[ $key ] >= 65535 ) {
unset( $parts[ $key ] );
}
}
} else {
if ( isset( $url[ $key ] ) ) {
if ( is_array( $url[ $key ] ) ) {
if ( empty( $url[ $key ] ) ) {
unset( $url[ $key ] );
}
} else {
$url[ $key ] = (string) $url[ $key ];
if ( $url[ $key ] == '' ) {
unset( $url[ $key ] );
}
}
}
if ( isset( $parts[ $key ] ) ) {
if ( is_array( $parts[ $key ] ) ) {
if ( empty( $parts[ $key ] ) ) {
unset( $parts[ $key ] );
}
} else {
$parts[ $key ] = (string) $parts[ $key ];
if ( '' === $parts[ $key ] ) {
unset( $parts[ $key ] );
}
}
}
}
}
// Start building the result.
// Port
if ( $STRIP_PORT ) {
if ( isset( $url['port'] ) ) {
unset( $url['port'] );
}
} else {
if ( isset( $parts['port'] ) ) {
$url['port'] = $parts['port'];
}
}
// User
if ( $STRIP_USER ) {
if ( isset( $url['user'] ) ) {
unset( $url['user'] );
}
} else {
if ( isset( $parts['user'] ) ) {
$url['user'] = $parts['user'];
}
}
// Password
if ( $STRIP_PASS || ! isset( $url['user'] ) ) {
if ( isset( $url['pass'] ) ) {
unset( $url['pass'] );
}
} else {
if ( isset( $parts['pass'] ) ) {
$url['pass'] = $parts['pass'];
}
}
// Scheme
if ( isset( $parts['scheme'] ) ) {
$url['scheme'] = $parts['scheme'];
}
// Host
if ( isset( $parts['host'] ) ) {
$url['host'] = $parts['host'];
}
// Path
if ( $STRIP_PATH ) {
if ( isset( $url['path'] ) ) {
unset( $url['path'] );
}
} else {
if ( $JOIN_PATH && isset( $parts['path'] ) && isset( $url['path'] ) && substr( $parts['path'], 0, 1 ) !== '/' ) {
if ( substr( $url['path'], -1, 1 ) != '/' ) {
$base_path = str_replace( '\\', '/', dirname( $url['path'] ) );
} else {
$base_path = $url['path'];
}
if ( substr( $base_path, -1, 1 ) != '/' ) {
$base_path .= '/';
}
$url['path'] = $base_path . $parts['path'];
} else {
if ( isset( $parts['path'] ) ) {
$url['path'] = $parts['path'];
}
}
}
// Query
if ( $STRIP_QUERY ) {
if ( isset( $url['query'] ) ) {
unset( $url['query'] );
}
} else {
if ( $JOIN_QUERY && isset( $url['query'] ) && isset( $parts['query'] ) ) {
$u_query = $url['query'];
$p_query = $parts['query'];
if ( ! is_array( $u_query ) ) {
parse_str( $u_query, $u_query );
}
if ( ! is_array( $p_query ) ) {
parse_str( $p_query, $p_query );
}
$u_query = http_build_str( $u_query );
$p_query = http_build_str( $p_query );
$u_query = str_replace( array( '[', '%5B' ), '{{{', $u_query );
$u_query = str_replace( array( ']', '%5D' ), '}}}', $u_query );
$p_query = str_replace( array( '[', '%5B' ), '{{{', $p_query );
$p_query = str_replace( array( ']', '%5D' ), '}}}', $p_query );
parse_str( $u_query, $u_query );
parse_str( $p_query, $p_query );
$query = http_build_str( array_merge( $u_query, $p_query ) );
$query = str_replace( array( '{{{', '%7B%7B%7B' ), '%5B', $query );
$query = str_replace( array( '}}}', '%7D%7D%7D' ), '%5D', $query );
parse_str( $query, $query );
} else {
if ( isset( $parts['query'] ) ) {
$query = $parts['query'];
}
}
if ( isset( $query ) ) {
if ( is_array( $query ) ) {
$query = http_build_str( $query );
}
$url['query'] = $query;
}
}
if ( isset( $url['query'] ) && is_array( $url['query'] ) ) {
$url['query'] = http_build_str( $url['query'] );
}
// Fragment
if ( $STRIP_FRAGMENT ) {
if ( isset( $url['fragment'] ) ) {
unset( $url['fragment'] );
}
} else {
if ( isset( $parts['fragment'] ) ) {
$url['fragment'] = $parts['fragment'];
}
}
// Ensure scheme presence.
if ( ! isset( $url['scheme'] ) ) {
if ( $server_https ) {
$url['scheme'] = 'https';
} elseif ( isset( $url['port'] ) ) {
if ( getservbyport( $url['port'], 'tcp' ) === $scheme ) {
$url['scheme'] = $scheme;
} else {
$url['scheme'] = 'http';
}
} else {
$url['scheme'] = 'http';
}
}
// Ensure host presence.
if ( ! isset( $url['host'] ) ) {
$url['host'] = $default_host;
}
// Hide standard ports.
// http://www.iana.org/assignments/port-numbers
if ( isset( $url['port'] ) ) {
if ( (int) getservbyname( $url['scheme'], 'tcp' ) === $url['port'] ) {
unset( $url['port'] );
}
}
// Ensure path presence.
if ( $STRIP_PATH ) {
$url['path'] = '';
} else {
if ( ! isset( $url['path'] ) ) {
$url['path'] = $request_uri_no_query;
} elseif ( substr( $url['path'], 0, 1 ) != '/' ) {
// A relative path, deal with it.
$url['path'] = $request_uri_path . $url['path'];
}
}
// Canonize the result path.
if ( strpos( $url['path'], './' ) !== false ) {
$path = explode( '/', $url['path'] );
$k_stack = array();
foreach ( $path as $k => $v ) {
if ( '..' === $v ) {
if ( $k_stack ) {
$k_parent = array_pop( $k_stack );
unset( $path[ $k_parent ] );
}
unset( $path[ $k ] );
} elseif ( '.' === $v ) {
unset( $path[ $k ] );
} else {
$k_stack[] = $k;
}
}
$url['path'] = implode( '/', $path );
}
$url['path'] = '/' . ltrim( $url['path'], '/' );
// The result as an array type is ready.
$new_url = $url;
// Build the result string.
$result = $url['scheme'] . '://';
if ( isset( $url['user'] ) ) {
$result .= $url['user'] . ( isset( $url['pass'] ) ? ':' . $url['pass'] : '' ) . '@';
}
$result .= $url['host'];
if ( isset( $url['port'] ) ) {
$result .= ':' . $url['port'];
}
$result .= $url['path'];
if ( isset( $url['query'] ) ) {
$result .= '?' . $url['query'];
}
if ( isset( $new_url['fragment'] ) ) {
$result .= '#' . $url['fragment'];
}
return $result;
}
}