From f4cb59e423e8752ce91f98c77519a8894ffd6b12 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 31 Aug 2014 04:42:38 +0200 Subject: [PATCH 1/8] Improve the re-usability of the addQueryArg() function - Support SSL protocol - Make the serverUrl code re-usable --- includes/Wpup/UpdateServer.php | 55 ++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/includes/Wpup/UpdateServer.php b/includes/Wpup/UpdateServer.php index 2cb56eb..d0755c0 100644 --- a/includes/Wpup/UpdateServer.php +++ b/includes/Wpup/UpdateServer.php @@ -11,13 +11,7 @@ public function __construct($serverUrl = null, $serverDirectory = null) { $serverDirectory = realpath(__DIR__ . '/../..'); } if ( $serverUrl === null ) { - //Default to the current URL minus the query and "index.php". - $serverUrl = 'http://' . $_SERVER['HTTP_HOST']; - $path = $_SERVER['SCRIPT_NAME']; - if ( basename($path) === 'index.php' ) { - $path = dirname($path) . '/'; - } - $serverUrl .= $path; + $serverUrl = self::determineServerUrl(); } $this->serverUrl = $serverUrl; @@ -26,6 +20,46 @@ public function __construct($serverUrl = null, $serverDirectory = null) { $this->cache = new Wpup_FileCache($serverDirectory . '/cache'); } + /** + * Determine the Server Url based on the current request. + * + * Defaults to the current URL minus the query and "index.php". + * + * @static + * + * @return string Url + */ + public static function determineServerUrl() { + $serverUrl = ( self::isSsl() ? 'https' : 'http' ); + $serverUrl .= '://' . $_SERVER['HTTP_HOST']; + $path = $_SERVER['SCRIPT_NAME']; + if ( basename($path) === 'index.php' ) { + $path = dirname($path) . '/'; + } + $serverUrl .= $path; + return $serverUrl; + } + + /** + * Determine if ssl is used. + * + * @return bool True if SSL, false if not used. + */ + public static function isSsl() { + if ( isset($_SERVER['HTTPS']) ) { + if ( 'on' === strtolower($_SERVER['HTTPS']) ) { + return true; + } + if ( '1' == $_SERVER['HTTPS'] ) { + return true; + } + } + elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) { + return true; + } + return false; + } + /** * Process an update API request. * @@ -312,10 +346,13 @@ protected function exitWithError($message, $httpStatus = 500) { * You can also set an argument to NULL to remove it. * * @param array $args An associative array of query arguments. - * @param string $url The old URL. + * @param string $url The old URL. Optional, defaults to the request url without query arguments. * @return string New URL. */ - protected static function addQueryArg($args, $url) { + protected static function addQueryArg($args, $url = null ) { + if ( !isset($url) ) { + $url = self::determineServerUrl(); + } if ( strpos($url, '?') !== false ) { $parts = explode('?', $url, 2); $base = $parts[0] . '?'; From 8312dd5d672a2c6faca8884840408519670c49e7 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 31 Aug 2014 07:33:35 +0200 Subject: [PATCH 2/8] Fix issues with dirname() on Windows --- includes/Wpup/UpdateServer.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/includes/Wpup/UpdateServer.php b/includes/Wpup/UpdateServer.php index d0755c0..aee6e9f 100644 --- a/includes/Wpup/UpdateServer.php +++ b/includes/Wpup/UpdateServer.php @@ -33,13 +33,25 @@ public static function determineServerUrl() { $serverUrl = ( self::isSsl() ? 'https' : 'http' ); $serverUrl .= '://' . $_SERVER['HTTP_HOST']; $path = $_SERVER['SCRIPT_NAME']; + if ( basename($path) === 'index.php' ) { - $path = dirname($path) . '/'; + if ( DIRECTORY_SEPARATOR === '/' ) { + $path = dirname($path) . '/'; + } + else { + // Fix Windows + if ( dirname($path) === '\\' ) { + $path = '/'; + } + else { + $path = str_replace('\\', '/', dirname($path)) . '/'; + } + } } + $serverUrl .= $path; return $serverUrl; } - /** * Determine if ssl is used. * From 5f8a332a27cfda98536bfe9565d061b5adf1b5d8 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 31 Aug 2014 07:35:29 +0200 Subject: [PATCH 3/8] Whitespace --- includes/Wpup/UpdateServer.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/Wpup/UpdateServer.php b/includes/Wpup/UpdateServer.php index aee6e9f..a21a8e6 100644 --- a/includes/Wpup/UpdateServer.php +++ b/includes/Wpup/UpdateServer.php @@ -52,6 +52,7 @@ public static function determineServerUrl() { $serverUrl .= $path; return $serverUrl; } + /** * Determine if ssl is used. * From 57ed71fdb728ac649e45cebba2d1ab4d0a17577a Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 3 Sep 2014 00:20:01 +0200 Subject: [PATCH 4/8] Code style --- includes/Wpup/UpdateServer.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/includes/Wpup/UpdateServer.php b/includes/Wpup/UpdateServer.php index a21a8e6..de8beb9 100644 --- a/includes/Wpup/UpdateServer.php +++ b/includes/Wpup/UpdateServer.php @@ -58,19 +58,19 @@ public static function determineServerUrl() { * * @return bool True if SSL, false if not used. */ - public static function isSsl() { - if ( isset($_SERVER['HTTPS']) ) { - if ( 'on' === strtolower($_SERVER['HTTPS']) ) { - return true; + public static function isSsl() { + if ( isset($_SERVER['HTTPS']) ) { + if ( 'on' === strtolower($_SERVER['HTTPS']) ) { + return true; } - if ( '1' == $_SERVER['HTTPS'] ) { - return true; - } - } + if ( '1' == $_SERVER['HTTPS'] ) { + return true; + } + } elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) { - return true; + return true; } - return false; + return false; } /** @@ -262,7 +262,7 @@ protected function logRequest($query) { $columns = array( isset($_SERVER['REMOTE_ADDR']) ? str_pad($_SERVER['REMOTE_ADDR'], 15, ' ') : '-', isset($query['action']) ? $query['action'] : '-', - isset($query['slug']) ? $query['slug'] : '-', + isset($query['slug']) ? $query['slug'] : '-', isset($query['installed_version']) ? $query['installed_version'] : '-', isset($wpVersion) ? $wpVersion : '-', isset($wpSiteUrl) ? $wpSiteUrl : '-', From 12b8dc16c7d5fb7ce539e9ef4b7ee44940ad9a51 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 3 Sep 2014 18:12:05 +0200 Subject: [PATCH 5/8] Small fixes --- includes/Wpup/UpdateServer.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/includes/Wpup/UpdateServer.php b/includes/Wpup/UpdateServer.php index de8beb9..4c46e3e 100644 --- a/includes/Wpup/UpdateServer.php +++ b/includes/Wpup/UpdateServer.php @@ -42,8 +42,7 @@ public static function determineServerUrl() { // Fix Windows if ( dirname($path) === '\\' ) { $path = '/'; - } - else { + } else { $path = str_replace('\\', '/', dirname($path)) . '/'; } } @@ -56,14 +55,13 @@ public static function determineServerUrl() { /** * Determine if ssl is used. * + * @see WP core - wp-includes/functions.php + * * @return bool True if SSL, false if not used. */ public static function isSsl() { if ( isset($_SERVER['HTTPS']) ) { - if ( 'on' === strtolower($_SERVER['HTTPS']) ) { - return true; - } - if ( '1' == $_SERVER['HTTPS'] ) { + if ( $_SERVER['HTTPS'] == '1' || strtolower($_SERVER['HTTPS']) === 'on' ) { return true; } } @@ -261,8 +259,8 @@ protected function logRequest($query) { $columns = array( isset($_SERVER['REMOTE_ADDR']) ? str_pad($_SERVER['REMOTE_ADDR'], 15, ' ') : '-', - isset($query['action']) ? $query['action'] : '-', - isset($query['slug']) ? $query['slug'] : '-', + isset($query['action']) ? $query['action'] : '-', + isset($query['slug']) ? $query['slug'] : '-', isset($query['installed_version']) ? $query['installed_version'] : '-', isset($wpVersion) ? $wpVersion : '-', isset($wpSiteUrl) ? $wpSiteUrl : '-', From c13099ace4d5f7f8ab01d263803792e8ee2dd4d0 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 3 Sep 2014 18:15:50 +0200 Subject: [PATCH 6/8] Missed one else --- includes/Wpup/UpdateServer.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/includes/Wpup/UpdateServer.php b/includes/Wpup/UpdateServer.php index 4c46e3e..2f6bc35 100644 --- a/includes/Wpup/UpdateServer.php +++ b/includes/Wpup/UpdateServer.php @@ -37,8 +37,7 @@ public static function determineServerUrl() { if ( basename($path) === 'index.php' ) { if ( DIRECTORY_SEPARATOR === '/' ) { $path = dirname($path) . '/'; - } - else { + } else { // Fix Windows if ( dirname($path) === '\\' ) { $path = '/'; From d7c244f7c88cf195ecb3362b50a19f268e70e717 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 3 Sep 2014 20:03:20 +0200 Subject: [PATCH 7/8] Slight simplification --- includes/Wpup/UpdateServer.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/includes/Wpup/UpdateServer.php b/includes/Wpup/UpdateServer.php index 2f6bc35..4376379 100644 --- a/includes/Wpup/UpdateServer.php +++ b/includes/Wpup/UpdateServer.php @@ -35,14 +35,15 @@ public static function determineServerUrl() { $path = $_SERVER['SCRIPT_NAME']; if ( basename($path) === 'index.php' ) { + $dir = dirname($path); if ( DIRECTORY_SEPARATOR === '/' ) { - $path = dirname($path) . '/'; + $path = $dir . '/'; } else { // Fix Windows - if ( dirname($path) === '\\' ) { - $path = '/'; - } else { - $path = str_replace('\\', '/', dirname($path)) . '/'; + $path = str_replace('\\', '/', $dir); + //Make sure there's a trailing slash. + if ( substr($path, -1) !== '/' ) { + $path .= '/'; } } } From 595b05825be0a517d646b14cde9b7ca4dc8f16e5 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 11 Sep 2014 21:37:17 +0200 Subject: [PATCH 8/8] Minor changes / code style, method naming --- includes/Wpup/UpdateServer.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/includes/Wpup/UpdateServer.php b/includes/Wpup/UpdateServer.php index 70612df..ea6d333 100644 --- a/includes/Wpup/UpdateServer.php +++ b/includes/Wpup/UpdateServer.php @@ -11,7 +11,7 @@ public function __construct($serverUrl = null, $serverDirectory = null) { $serverDirectory = realpath(__DIR__ . '/../..'); } if ( $serverUrl === null ) { - $serverUrl = self::determineServerUrl(); + $serverUrl = self::guessServerUrl(); } $this->serverUrl = $serverUrl; @@ -21,7 +21,7 @@ public function __construct($serverUrl = null, $serverDirectory = null) { } /** - * Determine the Server Url based on the current request. + * Guess the Server Url based on the current request. * * Defaults to the current URL minus the query and "index.php". * @@ -29,7 +29,7 @@ public function __construct($serverUrl = null, $serverDirectory = null) { * * @return string Url */ - public static function determineServerUrl() { + public static function guessServerUrl() { $serverUrl = ( self::isSsl() ? 'https' : 'http' ); $serverUrl .= '://' . $_SERVER['HTTP_HOST']; $path = $_SERVER['SCRIPT_NAME']; @@ -64,8 +64,7 @@ public static function isSsl() { if ( $_SERVER['HTTPS'] == '1' || strtolower($_SERVER['HTTPS']) === 'on' ) { return true; } - } - elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) { + } elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) { return true; } return false; @@ -389,7 +388,7 @@ protected function exitWithError($message = '', $httpStatus = 500) { */ protected static function addQueryArg($args, $url = null ) { if ( !isset($url) ) { - $url = self::determineServerUrl(); + $url = self::guessServerUrl(); } if ( strpos($url, '?') !== false ) { $parts = explode('?', $url, 2);