From 82ba539b2366cefadd54998571dc91dd74aa12f7 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Tue, 17 Sep 2013 22:47:09 -0500 Subject: [PATCH 1/8] Beginnings of multisite support --- class-404-template.php | 6 ++++++ class-helpers.php | 26 +++++++++++++++++++++++++- uploads-by-proxy.php | 9 ++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/class-404-template.php b/class-404-template.php index c6d7d82..13030b4 100644 --- a/class-404-template.php +++ b/class-404-template.php @@ -86,6 +86,7 @@ public function display_and_exit( $message=false ) { */ public function allow_path() { $path = $this->get_remote_path(); + if ( empty( $path ) ) { return false; } $allowed_paths = array( @@ -127,6 +128,11 @@ public function get_siteurl() { $url = parse_url( UBP_SITEURL ); $url = 'http://' . $url['host'] . @$url['path']; + }else if ( is_multisite() && ( $siteurl = get_option( 'ubp_site_url' ) ) ) { + + $url = parse_url( $siteurl ); + $url = 'http://' . $url['host'] . @$url['path']; + //die($url); }else if ( !is_multisite() ) { // Nothing set... Get original siteurl from database diff --git a/class-helpers.php b/class-helpers.php index e546bd3..2b46d2a 100644 --- a/class-helpers.php +++ b/class-helpers.php @@ -21,7 +21,8 @@ static public function requirements_check() { /** * Require single-site install before activating. */ - static public function require_no_multisite() { + static public function require_no_multisite() { + return true; if ( function_exists( 'is_multisite' ) && !is_multisite() ) { return true; } if ( is_admin() && (!defined('DOING_AJAX') || !DOING_AJAX) ) { @@ -64,4 +65,27 @@ static public function request_uploads_writable() { return false; } + static public function print_multisite_setting( $id ) { + switch_to_blog( $id ); + $ubp_site_url = get_option( 'ubp_site_url' ); + restore_current_blog(); + ?> + + UBP Site URL + + + '.print_r($_POST, true)); + if ( isset( $_POST['option']['ubp_site_url'] ) ) { + update_option( 'ubp_site_url', $_POST['option']['ubp_site_url'] ); + } + } + + static public function ubp_old_ms_path( $allowed_paths ) { + $allowed_paths[] = '/files/'; + return $allowed_paths; + } } \ No newline at end of file diff --git a/uploads-by-proxy.php b/uploads-by-proxy.php index ffd9ea8..7802c9e 100644 --- a/uploads-by-proxy.php +++ b/uploads-by-proxy.php @@ -75,4 +75,11 @@ if ( UBP_IS_LOCAL ) { add_action( 'admin_init', 'UBP_Helpers::requirements_check' ); add_filter( '404_template', 'UBP_Helpers::init_404_template' ); -} \ No newline at end of file +} + + +if ( function_exists( 'is_multisite' ) && is_multisite() ) { + add_action( 'wpmueditblogaction', 'UBP_Helpers::print_multisite_setting' ); + add_action( 'wpmu_update_blog_options', 'UBP_Helpers::wpmu_update_blog_options' ); + add_filter( 'ubp_allowed_paths', 'UBP_Helpers::ubp_old_ms_path' ); +} From 7a5f06446fdee61c6f3e65e87877497209e48373 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Wed, 18 Sep 2013 11:11:10 -0500 Subject: [PATCH 2/8] More multisite support (subdomains untested) --- class-404-template.php | 28 +++++++++++++++++++++------- class-helpers.php | 23 +++++++++++------------ uploads-by-proxy.php | 3 ++- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/class-404-template.php b/class-404-template.php index 13030b4..b755a81 100644 --- a/class-404-template.php +++ b/class-404-template.php @@ -123,17 +123,31 @@ public function get_siteurl() { $url = str_replace( array( 'http://', 'https://' ), '', UBP_LIVE_DOMAIN ); $url = 'http://' . $url; - }else if ( defined( 'UBP_SITEURL' ) && false !== UBP_SITEURL ) { - - $url = parse_url( UBP_SITEURL ); + }else if ( is_multisite() && ( $siteurl = get_option( '_ubp_site_url' ) ) ) { + + $url = parse_url( $siteurl ); $url = 'http://' . $url['host'] . @$url['path']; - }else if ( is_multisite() && ( $siteurl = get_option( 'ubp_site_url' ) ) ) { + }else if ( is_multisite() && defined( 'UBP_SITEURL' ) && false !== UBP_SITEURL ) { - $url = parse_url( $siteurl ); + + $details = get_blog_details(); + $url = parse_url( UBP_SITEURL ); + $ms_url = ''; + if ( SUBDOMAIN_INSTALL ) { + $parts = explode( '.', $details->domain ); + $ms_url = $parts[0] . '.' . $url['host']; + } else { + $ms_url = $url['host'] . $details->path; + } + $url = 'http://' . $ms_url . @$url['path']; + + } else if ( defined( 'UBP_SITEURL' ) && false !== UBP_SITEURL ) { + + $url = parse_url( UBP_SITEURL ); $url = 'http://' . $url['host'] . @$url['path']; - //die($url); - }else if ( !is_multisite() ) { + + } else { // Nothing set... Get original siteurl from database remove_filter( 'option_siteurl', '_config_wp_siteurl' ); diff --git a/class-helpers.php b/class-helpers.php index 2b46d2a..56555e6 100644 --- a/class-helpers.php +++ b/class-helpers.php @@ -13,7 +13,7 @@ static public function init_404_template( $template ) { } static public function requirements_check() { - add_action( 'admin_init', 'UBP_Helpers::require_no_multisite', 11 ); + //add_action( 'admin_init', 'UBP_Helpers::require_no_multisite', 11 ); add_action( 'admin_notices', 'UBP_Helpers::request_uploads_writable' ); add_action( 'admin_footer', 'UBP_Helpers::request_permalinks_enabled' ); } @@ -22,7 +22,6 @@ static public function requirements_check() { * Require single-site install before activating. */ static public function require_no_multisite() { - return true; if ( function_exists( 'is_multisite' ) && !is_multisite() ) { return true; } if ( is_admin() && (!defined('DOING_AJAX') || !DOING_AJAX) ) { @@ -67,25 +66,25 @@ static public function request_uploads_writable() { static public function print_multisite_setting( $id ) { switch_to_blog( $id ); - $ubp_site_url = get_option( 'ubp_site_url' ); + $ubp_site_url = get_option( '_ubp_site_url' ); restore_current_blog(); ?> UBP Site URL - + '.print_r($_POST, true)); - if ( isset( $_POST['option']['ubp_site_url'] ) ) { - update_option( 'ubp_site_url', $_POST['option']['ubp_site_url'] ); - } - } - static public function ubp_old_ms_path( $allowed_paths ) { - $allowed_paths[] = '/files/'; + $details = get_blog_details(); + $allowed_paths[] = $details->path . 'files/'; return $allowed_paths; } + + static function stop_ms_files_rewriting() { + $url = '/wp-content/uploads/sites/' . get_current_blog_id(); + define( 'BLOGUPLOADDIR', $url ); + } + } \ No newline at end of file diff --git a/uploads-by-proxy.php b/uploads-by-proxy.php index 7802c9e..22b6a6c 100644 --- a/uploads-by-proxy.php +++ b/uploads-by-proxy.php @@ -80,6 +80,7 @@ if ( function_exists( 'is_multisite' ) && is_multisite() ) { add_action( 'wpmueditblogaction', 'UBP_Helpers::print_multisite_setting' ); - add_action( 'wpmu_update_blog_options', 'UBP_Helpers::wpmu_update_blog_options' ); add_filter( 'ubp_allowed_paths', 'UBP_Helpers::ubp_old_ms_path' ); + add_action( 'init','UBP_Helpers::stop_ms_files_rewriting' ); } + From c51fd5294f91eaf4552df4a1bc18df531ff382f0 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Wed, 2 Oct 2013 09:52:02 -0500 Subject: [PATCH 3/8] More multi-site testing --- class-404-template.php | 29 ++++++++++++++++++++++++++--- class-helpers.php | 7 ++++--- uploads-by-proxy.php | 2 +- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/class-404-template.php b/class-404-template.php index b755a81..c4649dc 100644 --- a/class-404-template.php +++ b/class-404-template.php @@ -47,7 +47,7 @@ public function download() { $remove = str_replace( get_option( 'siteurl' ), '', $u['baseurl'] ); $basedir = str_replace( $remove, '', $basedir ); - $abspath = $basedir . $this->get_local_path(); + $abspath = $basedir . $this->get_dated_path(); $dir = dirname( $abspath ); if ( !is_dir( $dir ) && !wp_mkdir_p( $dir ) ) { @@ -57,7 +57,7 @@ public function download() { $saved_image = $wp_filesystem->put_contents( $abspath, $this->response['body'], FS_CHMOD_FILE ); // predefined mode settings for WP files if ( $saved_image ) { - wp_redirect( get_site_url( get_current_blog_id(), $this->get_local_path() ) ); + wp_redirect( get_site_url( get_current_blog_id(), $this->get_ms_adjusted_path() ) ); exit; }else { $this->display_and_exit( "Please check permissions. Could not write image $dir" ); @@ -169,6 +169,27 @@ public function get_domain() { return $this->domain; } + public function get_dated_path() { + $path = $this->get_local_path(); + + if ( function_exists( 'is_multisite' ) && is_multisite() && strpos( $path, '/files/' ) === 0 ) { + $path = str_replace( '/files', '', $path ); + } + + return $path; + } + + public function get_ms_adjusted_path() { + $path = $this->get_local_path(); + + //switch to the "new" multisite files location + if ( function_exists( 'is_multisite' ) && is_multisite() && strpos( $path, '/files/' ) === 0 ) { + $path = 'wp-content/uploads/sites/' . get_current_blog_id() . str_replace( '/files', '', $path ); + } + + return $path; + } + public function get_local_path() { if ( isset( $this->local_path ) ) { return $this->local_path; @@ -177,9 +198,10 @@ public function get_local_path() { // If local install is in a subdirectory, modify path to request from WordPress root $local_wordpress_path = parse_url( get_site_url(), PHP_URL_PATH ); $requested_path = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ); + if (substr($requested_path, 0, strlen($local_wordpress_path)) == $local_wordpress_path) { $requested_path = substr($requested_path, strlen($local_wordpress_path), strlen($requested_path)); - } + } $this->local_path = $requested_path; @@ -193,6 +215,7 @@ public function get_remote_path() { // If remote install is in a subdirectory, prepend the remote path $remote_path = parse_url( $this->get_siteurl(), PHP_URL_PATH ); + if ( !empty( $remote_path ) ) { $this->remote_path = $remote_path . $this->get_local_path(); }else { diff --git a/class-helpers.php b/class-helpers.php index 56555e6..1d24fe8 100644 --- a/class-helpers.php +++ b/class-helpers.php @@ -76,15 +76,16 @@ static public function print_multisite_setting( $id ) { path . 'files/'; + $allowed_paths[] = $details->path . 'wp-content/uploads/'; return $allowed_paths; } static function stop_ms_files_rewriting() { - $url = '/wp-content/uploads/sites/' . get_current_blog_id(); - define( 'BLOGUPLOADDIR', $url ); + $path = ABSPATH . 'wp-content/uploads/sites/' . get_current_blog_id(); + define( 'BLOGUPLOADDIR', $path ); } } \ No newline at end of file diff --git a/uploads-by-proxy.php b/uploads-by-proxy.php index 22b6a6c..3f17752 100644 --- a/uploads-by-proxy.php +++ b/uploads-by-proxy.php @@ -80,7 +80,7 @@ if ( function_exists( 'is_multisite' ) && is_multisite() ) { add_action( 'wpmueditblogaction', 'UBP_Helpers::print_multisite_setting' ); - add_filter( 'ubp_allowed_paths', 'UBP_Helpers::ubp_old_ms_path' ); + add_filter( 'ubp_allowed_paths', 'UBP_Helpers::ubp_extra_paths' ); add_action( 'init','UBP_Helpers::stop_ms_files_rewriting' ); } From e415e7d8c78592162f9747e54bb1d7dc3d9de21f Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Sat, 5 Oct 2013 12:00:01 -0500 Subject: [PATCH 4/8] Updated readme with multi-site instructions --- class-404-template.php | 4 +++- readme.txt | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/class-404-template.php b/class-404-template.php index c4649dc..194c1aa 100644 --- a/class-404-template.php +++ b/class-404-template.php @@ -57,7 +57,9 @@ public function download() { $saved_image = $wp_filesystem->put_contents( $abspath, $this->response['body'], FS_CHMOD_FILE ); // predefined mode settings for WP files if ( $saved_image ) { - wp_redirect( get_site_url( get_current_blog_id(), $this->get_ms_adjusted_path() ) ); + $path = $this->get_ms_adjusted_path(); + $redirect = get_site_url( get_current_blog_id(), $path ); + wp_redirect( $redirect ); exit; }else { $this->display_and_exit( "Please check permissions. Could not write image $dir" ); diff --git a/readme.txt b/readme.txt index 5f92e68..469aae2 100644 --- a/readme.txt +++ b/readme.txt @@ -1,5 +1,5 @@ === Uploads by Proxy === -Contributors: pdclark, brainstormmedia +Contributors: pdclark, brainstormmedia, jrfoell Author URI: http://brainstormmedia.com Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=FD4GKBGQFUZC8 License: GPLv2 or later @@ -23,6 +23,21 @@ 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! += Multi-Site = + +Multi-site support is available in WordPress 3.6+ but it requires additional configuration. + +In wp-config.php, in addition to the 'UBP_SITEURL' define you should also add this: + + define( 'UPLOADBLOGSDIR', 'wp-content/uploads/sites' ); + +In your .htaccess you should comment out the following line if present: + + #RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L] + +and add the following line directly above "RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]" + + RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-content/uploads.*) $2 [S=2] == Installation == From 1fa9b0640df7073b00537069d22bb0fb4e32b46a Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Sat, 5 Oct 2013 12:01:38 -0500 Subject: [PATCH 5/8] Removed unneeded .htaccess instructions (waiting on http://core.trac.wordpress.org/ticket/20746) --- readme.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/readme.txt b/readme.txt index 469aae2..c4c9944 100644 --- a/readme.txt +++ b/readme.txt @@ -35,10 +35,6 @@ In your .htaccess you should comment out the following line if present: #RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L] -and add the following line directly above "RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]" - - RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-content/uploads.*) $2 [S=2] - == Installation == 1. Upload the `uploads-by-proxy` folder to the `/wp-content/plugins/` directory From 52fa7580ab8f3901f6b8e1000c56dba431881726 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Sat, 22 Feb 2014 12:43:22 -0600 Subject: [PATCH 6/8] Updates MultiSite instructions with .htaccess fix --- readme.txt | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/readme.txt b/readme.txt index c4c9944..fa62a6a 100644 --- a/readme.txt +++ b/readme.txt @@ -31,9 +31,27 @@ In wp-config.php, in addition to the 'UBP_SITEURL' define you should also add th define( 'UPLOADBLOGSDIR', 'wp-content/uploads/sites' ); -In your .htaccess you should comment out the following line if present: +In your .htaccess you should comment out the "ms-files" line if present. It will look something like one of these two lines: #RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L] + #RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L] + + +Also add the following lines between # start UBP and # end UBP to .htaccess: + +RewriteCond %{REQUEST_FILENAME} -f [OR] +RewriteCond %{REQUEST_FILENAME} -d +RewriteRule ^ - [L] +# start UBP +RewriteCond %{REQUEST_URI} ^/wp-(content|admin|includes).*$ +RewriteRule ^ - [S=2] +# end UBP +RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L] +RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L] +RewriteRule . index.php [L]` + +This is due to a bug that either manifests itself in Apache or in the distributed Multi-Site .htaccess (https://core.trac.wordpress.org/ticket/20746) + == Installation == From 63000901fb11fe7037803542215b951c56ca5b75 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Sat, 1 Mar 2014 10:23:15 -0600 Subject: [PATCH 7/8] Fixed new uploads while UBP Multi-Site is enabled --- class-helpers.php | 3 +++ readme.txt | 19 ++++++------------- uploads-by-proxy.php | 1 + 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/class-helpers.php b/class-helpers.php index 1d24fe8..a95a7a9 100644 --- a/class-helpers.php +++ b/class-helpers.php @@ -88,4 +88,7 @@ static function stop_ms_files_rewriting() { define( 'BLOGUPLOADDIR', $path ); } + static function pre_site_option_ms_files_rewriting( $option ) { + return 0; //return 0, not FALSE + } } \ No newline at end of file diff --git a/readme.txt b/readme.txt index 035e00f..5f97b16 100644 --- a/readme.txt +++ b/readme.txt @@ -38,21 +38,14 @@ In your .htaccess, comment out the "ms-files" line if present. It will look some #RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L] #RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L] +Also in your .htaccess, replace the following 'Normal' rewrite rules with the UBP Multisite rule below: -Also add the following lines between # start UBP and # end UBP to .htaccess: +# Normal +#RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L] +# UBP Multisite +RewriteRule ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L] -RewriteCond %{REQUEST_FILENAME} -f [OR] -RewriteCond %{REQUEST_FILENAME} -d -RewriteRule ^ - [L] -# start UBP -RewriteCond %{REQUEST_URI} ^/wp-(content|admin|includes).*$ -RewriteRule ^ - [S=2] -# end UBP -RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L] -RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L] -RewriteRule . index.php [L]` - -These lines must be placed in that exact spot. They are needed due to a bug that either manifests itself in Apache or in the distributed Multi-Site .htaccess (https://core.trac.wordpress.org/ticket/20746) +This last .htaccess change is needed due to a bug that either manifests itself in Apache or in the default distributed WordPress Multi-Site .htaccess (https://core.trac.wordpress.org/ticket/20746) == Installation == diff --git a/uploads-by-proxy.php b/uploads-by-proxy.php index 6a349fa..cca1a39 100644 --- a/uploads-by-proxy.php +++ b/uploads-by-proxy.php @@ -59,4 +59,5 @@ add_action( 'wpmueditblogaction', 'UBP_Helpers::print_multisite_setting' ); add_filter( 'ubp_allowed_paths', 'UBP_Helpers::ubp_extra_paths' ); add_action( 'init','UBP_Helpers::stop_ms_files_rewriting' ); + add_filter( 'pre_site_option_ms_files_rewriting', 'UBP_Helpers::pre_site_option_ms_files_rewriting' ); } From d6c53bea348c1fc3a6365b28e6ffda86942b58ee Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Fri, 24 Jun 2016 17:25:57 -0500 Subject: [PATCH 8/8] Fixed errors in get public IP --- class-get-public-ip.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/class-get-public-ip.php b/class-get-public-ip.php index 1be63cc..69e19e8 100644 --- a/class-get-public-ip.php +++ b/class-get-public-ip.php @@ -45,7 +45,7 @@ function __toString() { public function get_ip( $url, $args=array() ) { $defaults = array( 'method' => 'GET', - 'referer'=> $domain, + 'referer'=> $this->domain, 'body' => '', 'index' => 0, ); @@ -59,7 +59,7 @@ public function get_ip( $url, $args=array() ) { ); $response = wp_remote_get( $url, $query_args ); - $body = strip_tags($response['body']); + $body = strip_tags($response->body); preg_match_all( $this->ip_pattern, $body, $matches );