| 1 |
<?php |
| 2 |
|
| 3 |
namespace FL\Assistant\Hooks; |
| 4 |
|
| 5 |
class ImageProxy { |
| 6 |
|
| 7 |
public function __construct() { |
| 8 |
add_action( |
| 9 |
'init', function() { |
| 10 |
if ( ! current_user_can( 'edit_others_posts' ) ) { |
| 11 |
return; |
| 12 |
} |
| 13 |
if ( isset( $_GET['fl_asst_image_proxy'] ) ) { |
| 14 |
self::render_image(); |
| 15 |
} |
| 16 |
} |
| 17 |
); |
| 18 |
} |
| 19 |
|
| 20 |
public function render_image() { |
| 21 |
if ( ! isset( $_GET['fl_asst_image_proxy'] ) && ! isset( $_GET['url'] ) ) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
$url = esc_url_raw( $_GET['url'] ); |
| 26 |
$url = urldecode( $url ); |
| 27 |
|
| 28 |
if ( 0 !== strpos( $url, 'http' ) ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
$response = wp_safe_remote_get( $url ); |
| 33 |
|
| 34 |
if ( is_wp_error( $response ) ) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
$body = wp_remote_retrieve_body( $response ); |
| 39 |
$headers = wp_remote_retrieve_headers( $response ); |
| 40 |
|
| 41 |
if ( ! isset( $headers['content-type'] ) ) { |
| 42 |
return; |
| 43 |
} elseif ( 0 !== strpos( $headers['content-type'], 'image/' ) ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
header( 'Content-type: ' . $headers['content-type'] ); |
| 48 |
echo $body; |
| 49 |
die(); |
| 50 |
} |
| 51 |
} |
| 52 |
|