Improved support for paths with backslashes on Unix filesystems (#3501)

Fixes #3497
This commit is contained in:
SOFe 2020-05-19 17:34:51 +08:00 committed by GitHub
parent a67d2ae978
commit 786f416f2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 5 deletions

View File

@ -78,8 +78,8 @@ abstract class PluginBase implements Plugin{
$this->loader = $loader;
$this->server = $server;
$this->description = $description;
$this->dataFolder = rtrim($dataFolder, "\\/") . "/";
$this->file = rtrim($file, "\\/") . "/";
$this->dataFolder = rtrim($dataFolder, "/" . DIRECTORY_SEPARATOR) . "/";
$this->file = rtrim($file, "/" . DIRECTORY_SEPARATOR) . "/";
$this->configFile = $this->dataFolder . "config.yml";
$this->logger = new PluginLogger($this);
$this->scheduler = new TaskScheduler($this->logger, $this->getFullName());
@ -169,7 +169,7 @@ abstract class PluginBase implements Plugin{
* @return null|resource Resource data, or null
*/
public function getResource(string $filename){
$filename = rtrim(str_replace("\\", "/", $filename), "/");
$filename = rtrim(str_replace(DIRECTORY_SEPARATOR, "/", $filename), "/");
if(file_exists($this->file . "resources/" . $filename)){
return fopen($this->file . "resources/" . $filename, "rb");
}

View File

@ -89,6 +89,7 @@ use function substr;
use function sys_get_temp_dir;
use function trim;
use function xdebug_get_function_stack;
use const DIRECTORY_SEPARATOR;
use const PHP_EOL;
use const PHP_INT_MAX;
use const PHP_INT_SIZE;
@ -639,7 +640,7 @@ class Utils{
* @return string
*/
public static function cleanPath($path){
$result = str_replace(["\\", ".php", "phar://"], ["/", "", ""], $path);
$result = str_replace([DIRECTORY_SEPARATOR, ".php", "phar://"], ["/", "", ""], $path);
//remove relative paths
//TODO: make these paths dynamic so they can be unit-tested against
@ -648,7 +649,7 @@ class Utils{
\pocketmine\PATH => ""
];
foreach($cleanPaths as $cleanPath => $replacement){
$cleanPath = rtrim(str_replace(["\\", "phar://"], ["/", ""], $cleanPath), "/");
$cleanPath = rtrim(str_replace([DIRECTORY_SEPARATOR, "phar://"], ["/", ""], $cleanPath), "/");
if(strpos($result, $cleanPath) === 0){
$result = ltrim(str_replace($cleanPath, $replacement, $result), "/");
}