From f767cdb73fc1f7a2be2914ec53b8af2168d1b174 Mon Sep 17 00:00:00 2001 From: Ramon Fincken Date: Thu, 30 Mar 2017 12:33:07 +0200 Subject: [PATCH 1/8] [TASK] Add redirect option, saves storage and ensures the latest version of the uploaded media file. Also: excludes the need for PHP ip/host lookup. --- class-404-template.php | 17 ++++++++++++++++- readme.txt | 16 ++++++++++++---- uploads-by-proxy.php | 2 +- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/class-404-template.php b/class-404-template.php index add91ef..d8a839d 100644 --- a/class-404-template.php +++ b/class-404-template.php @@ -22,6 +22,20 @@ function __construct() { * Stream files from publicly registered IP address through PHP */ public function stream() { + // Redirect to original url? + // https://www.mijnpress.nl/2013/uploads-by-proxy-302-redirect-instead-of-force-download-to-local-directory/ + if( defined('UBP_REDIRECT') && UBP_REDIRECT == TRUE ) + { + $redirect_url = UBP_SITEURL.$this->get_remote_path(); + $this->response = wp_remote_head( $redirect_url ); + if ( is_wp_error($this->response) || 200 != $this->response['response']['code'] ) { + $this->display_and_exit( "Remote url not readable. Path: ".$this->get_remote_path() ); + } + wp_redirect( $redirect_url, 302 ); // 302 as files _can_ change + exit; + } + // Falltrough to download file to local storage + require dirname(__FILE__).'/class-get-public-ip.php'; $ip = new UBP_Get_Public_IP( $this->get_domain() ); @@ -31,6 +45,7 @@ public function stream() { // 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'] ) { @@ -195,4 +210,4 @@ public function get_remote_path() { return $this->remote_path; } -} \ No newline at end of file +} diff --git a/readme.txt b/readme.txt index f330612..d1e7b9c 100644 --- a/readme.txt +++ b/readme.txt @@ -1,10 +1,10 @@ === Uploads by Proxy === -Contributors: pdclark +Contributors: pdclark, ramon fincken Author URI: http://pdclark.com -Tags: localhost, local, development, staging, uploads, media library, xampp, mamp, wamp, git, svn, subversion +Tags: localhost, local, development, staging, uploads, media library, xampp, mamp, wamp, git, svn, subversion, DTAP, OTAP, 302, redirect Requires at least: 3.1 Tested up to: 3.6 -Stable tag: 1.1.2 +Stable tag: 1.1.3 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -12,7 +12,7 @@ For local development: Automatically load images from the production version of == Description == -This plugin is meant to be used by developers who work on sites in a local development environment before deploying changes to a production (live) server. It allows you skip downloading the contents of `wp-content/uploads` to your local WordPress install. Instead, images missing from the uploads directory are loaded from the production server as needed. +This plugin is meant to be used by developers who work on sites in a local development environment before deploying changes to a production (live) server. It allows you skip downloading the contents of `wp-content/uploads` to your local WordPress install. Instead, images missing from the uploads directory are loaded from the production server as needed. You have the option to download the files or to redirect to the original url. = Setup = @@ -22,6 +22,10 @@ In most cases, you should be able to activate the plugin and go. If the plugin d If you are on a staging server (not a local development environment) you may need to force the plugin to run with `define( 'UBP_IS_LOCAL', true );` in `wp-config.php`. Do not set this on a live site! +If you are on a staging server you may also use this define to 302 redirect to the original URL instead of downloading. This saves storage and ensures the latest version of the uploaded media file. + define('UBP_REDIRECT', true); + + == Installation == @@ -82,6 +86,10 @@ function ubp_ip_url( $url, $domain ) { == Changelog == += 1.1.3 = + +* Add support to 302 redirect to the original URL instead of downloading. Saves storage and ensures the latest version of the uploaded media file. + = 1.1.2 = * Fix: Resolve a warning output in debug environments. (Static function not declared as static.) diff --git a/uploads-by-proxy.php b/uploads-by-proxy.php index a648e4c..345fc42 100644 --- a/uploads-by-proxy.php +++ b/uploads-by-proxy.php @@ -5,7 +5,7 @@ Author: Paul Clark Author URI: http://pdclark.com Description: Load images from production site if missing in development environment. Activate by using either define('WP_SITEURL', 'http://development-domain.com'); or define('UBP_SITEURL', 'http://live-domain.com/wordpress'); in wp-config.php. -Version: 1.1.2 +Version: 1.1.3 */ /** From 361a56a8e9567032bb6e26cf645bac7feebcf122 Mon Sep 17 00:00:00 2001 From: Ramon Fincken Date: Fri, 19 May 2017 12:02:12 +0200 Subject: [PATCH 2/8] [BUGFIX] comply with display_and_exit --- class-404-template.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/class-404-template.php b/class-404-template.php index d8a839d..8b1c961 100644 --- a/class-404-template.php +++ b/class-404-template.php @@ -29,6 +29,10 @@ public function stream() { $redirect_url = UBP_SITEURL.$this->get_remote_path(); $this->response = wp_remote_head( $redirect_url ); if ( is_wp_error($this->response) || 200 != $this->response['response']['code'] ) { + // Comply with display_and_exit() + if(is_wp_error($this->response)) { + $this->response = array( 'headers' => array(), 'body' => $this->response->get_error_message() ); + } $this->display_and_exit( "Remote url not readable. Path: ".$this->get_remote_path() ); } wp_redirect( $redirect_url, 302 ); // 302 as files _can_ change From b02b21376db8bd08dd06d2f3082d38b37ca65390 Mon Sep 17 00:00:00 2001 From: Ramon Fincken Date: Fri, 19 May 2017 12:14:36 +0200 Subject: [PATCH 3/8] [FEATURE] Bypass head check for proxy --- class-404-template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/class-404-template.php b/class-404-template.php index 8b1c961..8de36a1 100644 --- a/class-404-template.php +++ b/class-404-template.php @@ -28,7 +28,7 @@ public function stream() { { $redirect_url = UBP_SITEURL.$this->get_remote_path(); $this->response = wp_remote_head( $redirect_url ); - if ( is_wp_error($this->response) || 200 != $this->response['response']['code'] ) { + if ( (defined('UBP_BYPASS_HEAD_CHECK') && UBP_BYPASS_HEAD_CHECK == TRUE) || is_wp_error($this->response) || 200 != $this->response['response']['code'] ) { // Comply with display_and_exit() if(is_wp_error($this->response)) { $this->response = array( 'headers' => array(), 'body' => $this->response->get_error_message() ); From f6d5f3196a54d899534d722418ce9b2de5045541 Mon Sep 17 00:00:00 2001 From: Ramon Fincken Date: Fri, 19 May 2017 12:24:09 +0200 Subject: [PATCH 4/8] [FEATURE] Bypass head check for proxy --- class-404-template.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/class-404-template.php b/class-404-template.php index 8de36a1..671ae81 100644 --- a/class-404-template.php +++ b/class-404-template.php @@ -27,13 +27,15 @@ public function stream() { if( defined('UBP_REDIRECT') && UBP_REDIRECT == TRUE ) { $redirect_url = UBP_SITEURL.$this->get_remote_path(); - $this->response = wp_remote_head( $redirect_url ); - if ( (defined('UBP_BYPASS_HEAD_CHECK') && UBP_BYPASS_HEAD_CHECK == TRUE) || is_wp_error($this->response) || 200 != $this->response['response']['code'] ) { - // Comply with display_and_exit() - if(is_wp_error($this->response)) { - $this->response = array( 'headers' => array(), 'body' => $this->response->get_error_message() ); + if( !(defined('UBP_BYPASS_HEAD_CHECK') && UBP_BYPASS_HEAD_CHECK == TRUE) ) { + $this->response = wp_remote_head( $redirect_url ); + if ( is_wp_error($this->response) || 200 != $this->response['response']['code'] ) { + // Comply with display_and_exit() + if(is_wp_error($this->response)) { + $this->response = array( 'headers' => array(), 'body' => $this->response->get_error_message() ); + } + $this->display_and_exit( "Remote url not readable. Path: ".$this->get_remote_path() ); } - $this->display_and_exit( "Remote url not readable. Path: ".$this->get_remote_path() ); } wp_redirect( $redirect_url, 302 ); // 302 as files _can_ change exit; From 7bde98e70ba2640f5fff648a346ef6808c144ade Mon Sep 17 00:00:00 2001 From: Ramon Fincken Date: Fri, 10 Jul 2020 14:48:37 +0200 Subject: [PATCH 5/8] [FEATURE] search-replace --- class-404-template.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/class-404-template.php b/class-404-template.php index 671ae81..09d2988 100644 --- a/class-404-template.php +++ b/class-404-template.php @@ -27,6 +27,13 @@ public function stream() { if( defined('UBP_REDIRECT') && UBP_REDIRECT == TRUE ) { $redirect_url = UBP_SITEURL.$this->get_remote_path(); + if( defined( 'UBP_REPLACE' ) ) { + foreach( UBP_REPLACE as $search => $replace ) { + $redirect_url = str_replace( $search, $replace, $redirect_url ); + } + } + + if( !(defined('UBP_BYPASS_HEAD_CHECK') && UBP_BYPASS_HEAD_CHECK == TRUE) ) { $this->response = wp_remote_head( $redirect_url ); if ( is_wp_error($this->response) || 200 != $this->response['response']['code'] ) { From a5c02f162ac306279007da2e44b884596a20d37d Mon Sep 17 00:00:00 2001 From: Ramon Fincken Date: Fri, 10 Jul 2020 14:50:08 +0200 Subject: [PATCH 6/8] [TASK] version --- readme.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/readme.txt b/readme.txt index d1e7b9c..d52e745 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: pdclark, ramon fincken Author URI: http://pdclark.com Tags: localhost, local, development, staging, uploads, media library, xampp, mamp, wamp, git, svn, subversion, DTAP, OTAP, 302, redirect Requires at least: 3.1 -Tested up to: 3.6 +Tested up to: 5.4.2 Stable tag: 1.1.3 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -24,6 +24,7 @@ If you are on a staging server (not a local development environment) you may nee If you are on a staging server you may also use this define to 302 redirect to the original URL instead of downloading. This saves storage and ensures the latest version of the uploaded media file. define('UBP_REDIRECT', true); + define('UBP_REPLACE', [ '/app' => '/wp-content' ] ); // Enable vanilla wp-content instead of composer app directory From f39181f43a7f02b2f9e6d9f6efd2c6cb63a9730b Mon Sep 17 00:00:00 2001 From: Ramon Fincken Date: Fri, 10 Jul 2020 14:50:34 +0200 Subject: [PATCH 7/8] [TASK] version --- readme.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/readme.txt b/readme.txt index d52e745..e74fd2c 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Author URI: http://pdclark.com Tags: localhost, local, development, staging, uploads, media library, xampp, mamp, wamp, git, svn, subversion, DTAP, OTAP, 302, redirect Requires at least: 3.1 Tested up to: 5.4.2 -Stable tag: 1.1.3 +Stable tag: 1.1.4 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -87,6 +87,10 @@ function ubp_ip_url( $url, $domain ) { == Changelog == += 1.1.4 = + +* Add search replace + = 1.1.3 = * Add support to 302 redirect to the original URL instead of downloading. Saves storage and ensures the latest version of the uploaded media file. From 00320f4dc3e586db2cd6c354e1ad6ba17a26ce5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Mart=C3=ADnez?= Date: Tue, 20 Apr 2021 11:42:04 +0200 Subject: [PATCH 8/8] Check if $_SERVER indexes are set before calling them These trigger a notice every time you call WP-CLI --- uploads-by-proxy.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uploads-by-proxy.php b/uploads-by-proxy.php index 345fc42..d0d30a9 100644 --- a/uploads-by-proxy.php +++ b/uploads-by-proxy.php @@ -24,8 +24,8 @@ */ if ( !defined('UBP_IS_LOCAL') ) { define('UBP_IS_LOCAL', ( - ( '127.0.0.1' == $_SERVER['SERVER_ADDR'] && '127.0.0.1' == $_SERVER['REMOTE_ADDR'] ) // IPv4 - || ( '::1' == $_SERVER['SERVER_ADDR'] && '::1' == $_SERVER['REMOTE_ADDR'] ) // IPv6 + ( isset( $_SERVER['SERVER_ADDR'] ) && '127.0.0.1' == $_SERVER['SERVER_ADDR'] && isset( $_SERVER['REMOTE_ADDR'] ) && '127.0.0.1' == $_SERVER['REMOTE_ADDR'] ) // IPv4 + || ( isset( $_SERVER['SERVER_ADDR'] ) && '::1' == $_SERVER['SERVER_ADDR'] && isset( $_SERVER['REMOTE_ADDR'] ) && '::1' == $_SERVER['REMOTE_ADDR'] ) // IPv6 ) ); }