Skip to content

Commit

Permalink
Fix incorrect banner & icon URLs on Windows in certain configurations.
Browse files Browse the repository at this point in the history
Probably related to #78
  • Loading branch information
YahnisElsts committed Jan 11, 2021
1 parent 2e9e974 commit d33c389
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions includes/Wpup/UpdateServer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

class Wpup_UpdateServer {
const FILE_PER_DAY = 'Y-m-d';
const FILE_PER_MONTH = 'Y-m';
Expand Down Expand Up @@ -26,7 +27,7 @@ public function __construct($serverUrl = null, $serverDirectory = null) {
if ( $serverDirectory === null ) {
$serverDirectory = realpath(__DIR__ . '/../..');
}
$this->serverDirectory = $serverDirectory;
$this->serverDirectory = $this->normalizeFilePath($serverDirectory);
if ( $serverUrl === null ) {
$serverUrl = self::guessServerUrl();
}
Expand Down Expand Up @@ -60,7 +61,7 @@ public function __construct($serverUrl = null, $serverDirectory = null) {
* @return string Url
*/
public static function guessServerUrl() {
$serverUrl = ( self::isSsl() ? 'https' : 'http' );
$serverUrl = (self::isSsl() ? 'https' : 'http');
$serverUrl .= '://' . $_SERVER['HTTP_HOST'];
$path = $_SERVER['SCRIPT_NAME'];

Expand All @@ -83,16 +84,16 @@ public static function guessServerUrl() {
/**
* Determine if ssl is used.
*
* @return bool True if SSL, false if not 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 ( $_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;
Expand Down Expand Up @@ -240,7 +241,7 @@ protected function filterMetadata($meta, /** @noinspection PhpUnusedParameterIns
*/
protected function actionDownload(Wpup_Request $request) {
//Required for IE, otherwise Content-Disposition may be ignored.
if(ini_get('zlib.output_compression')) {
if ( ini_get('zlib.output_compression') ) {
@ini_set('zlib.output_compression', 'Off');
}

Expand Down Expand Up @@ -289,7 +290,7 @@ protected function checkAuthorization($request) {
protected function generateDownloadUrl(Wpup_Package $package) {
$query = array(
'action' => 'download',
'slug' => $package->slug,
'slug' => $package->slug,
);
return self::addQueryArg($query, $this->serverUrl);
}
Expand Down Expand Up @@ -324,9 +325,9 @@ protected function getBanners(Wpup_Package $package) {
/**
* Get a publicly accessible URL for a plugin banner.
*
* @deprecated Use generateAssetUrl() instead.
* @param string $relativeFileName Banner file name relative to the "banners" subdirectory.
* @return string
* @deprecated Use generateAssetUrl() instead.
*/
protected function generateBannerUrl($relativeFileName) {
return $this->generateAssetUrl('banners', $relativeFileName);
Expand Down Expand Up @@ -393,7 +394,7 @@ protected function findFirstAsset(
protected function generateAssetUrl($assetType, $relativeFileName) {
//The current implementation is trivially simple, but you could override this method
//to (for example) create URLs that don't rely on the directory being public.
$directory = $this->assetDirectories[$assetType];
$directory = $this->normalizeFilePath($this->assetDirectories[$assetType]);
if ( strpos($directory, $this->serverDirectory) === 0 ) {
$subDirectory = substr($directory, strlen($this->serverDirectory) + 1);
} else {
Expand All @@ -403,6 +404,19 @@ protected function generateAssetUrl($assetType, $relativeFileName) {
return $this->serverUrl . $subDirectory . '/' . $relativeFileName;
}

/**
* Convert all directory separators to forward slashes.
*
* @param string $path
* @return string
*/
protected function normalizeFilePath($path) {
if ( !is_string($path) ) {
return $path;
}
return str_replace(array(DIRECTORY_SEPARATOR, '\\'), '/', $path);
}

/**
* Log an API request.
*
Expand Down Expand Up @@ -479,7 +493,7 @@ protected function getLogFileName() {
* @param Wpup_Request|null $request
* @return array
*/
protected function filterLogInfo($columns, /** @noinspection PhpUnusedParameterInspection */$request = null) {
protected function filterLogInfo($columns, /** @noinspection PhpUnusedParameterInspection */ $request = null) {
return $columns;
}

Expand All @@ -501,7 +515,7 @@ protected function escapeLogInfo(array $columns) {
*/
protected function escapeLogValue($value) {

if (!isset($value)) {
if ( !isset($value) ) {
return null;
}

Expand All @@ -519,10 +533,10 @@ protected function escapeLogValue($value) {
$value = str_replace('\\', '\\\\', $value);
$value = preg_replace_callback(
$regex,
function(array $matches) {
function (array $matches) {
$length = strlen($matches[0]);
$escaped = '';
for($i = 0; $i < $length; $i++) {
for ($i = 0; $i < $length; $i++) {
//Convert the character to a hexadecimal escape sequence.
$hexCode = dechex(ord($matches[0][$i]));
$escaped .= '\x' . strtoupper(str_pad($hexCode, 2, '0', STR_PAD_LEFT));
Expand Down Expand Up @@ -573,7 +587,7 @@ protected function rotateLogs() {
$logFiles = array_reverse($logFiles);

//Keep the most recent $logBackupCount files, delete the rest.
foreach(array_slice($logFiles, $this->logBackupCount) as $fileName) {
foreach (array_slice($logFiles, $this->logBackupCount) as $fileName) {
@unlink($fileName);
}
}
Expand Down Expand Up @@ -657,7 +671,7 @@ protected function exitWithError($message = '', $httpStatus = 500) {
502 => '502 Bad Gateway',
503 => '503 Service Unavailable',
504 => '504 Gateway Timeout',
505 => '505 HTTP Version Not Supported'
505 => '505 HTTP Version Not Supported',
);

if ( !isset($_SERVER['SERVER_PROTOCOL']) || $_SERVER['SERVER_PROTOCOL'] === '' ) {
Expand Down Expand Up @@ -698,7 +712,7 @@ protected function exitWithError($message = '', $httpStatus = 500) {
* @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 = null ) {
protected static function addQueryArg($args, $url = null) {
if ( !isset($url) ) {
$url = self::guessServerUrl();
}
Expand All @@ -714,7 +728,7 @@ protected static function addQueryArg($args, $url = null ) {
$query = array_merge($query, $args);

//Remove null/false arguments.
$query = array_filter($query, function($value) {
$query = array_filter($query, function ($value) {
return ($value !== null) && ($value !== false);
});

Expand Down

0 comments on commit d33c389

Please sign in to comment.