PluginProbe
Assistant – Every Day Productivity Apps / trunk
Assistant – Every Day Productivity Apps vtrunk
1.5.5 trunk 0.1.0 0.2.0 0.2.1 0.2.2 0.3.0 0.3.1 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.6.0 0.7.0 0.7.0.2 0.7.0.3 0.7.0.4 0.7.0.5 0.7.0.6 0.7.0.7 0.7.0.8 0.7.0.9 0.7.1 1.0 All 71 releases
assistant / backend / src / Hooks / ImageProxy.php

ImageProxy.php in Assistant – Every Day Productivity Apps trunk, at backend/src/Hooks/ImageProxy.php

74 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // validate image
42 $filesystem = self::filesystem();
43 $tmpfile = tempnam( '/tmp', 'assistant' );
44 $filesystem->put_contents( $tmpfile, $body );
45 $validimage = wp_get_image_mime( $tmpfile );
46 $filesystem->delete( $tmpfile );
47
48 if ( ! $validimage ) {
49 return false;
50 }
51
52 if ( ! isset( $headers['content-type'] ) ) {
53 return;
54 } elseif ( 0 !== strpos( $headers['content-type'], 'image/' ) ) {
55 return;
56 }
57
58 header( 'Content-type: ' . $headers['content-type'] );
59 echo $body;
60 die();
61 }
62
63 private function filesystem() {
64 global $wp_filesystem;
65
66 if ( is_null( $wp_filesystem ) ) {
67 require_once ABSPATH . '/wp-admin/includes/file.php';
68 WP_Filesystem();
69 }
70
71 return $wp_filesystem;
72 }
73 }
74