diff --git a/class-404-template.php b/class-404-template.php index add91ef..09d2988 100644 --- a/class-404-template.php +++ b/class-404-template.php @@ -22,6 +22,33 @@ 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(); + 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'] ) { + // 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 + 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 +58,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 +223,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..e74fd2c 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 +Tested up to: 5.4.2 +Stable tag: 1.1.4 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,11 @@ 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); + define('UBP_REPLACE', [ '/app' => '/wp-content' ] ); // Enable vanilla wp-content instead of composer app directory + + == Installation == @@ -82,6 +87,14 @@ 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. + = 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..d0d30a9 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 */ /** @@ -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 ) ); }