From fd011d95bf3450db0cff09791355d2d9b32f74ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dubravszky=20J=C3=B3zsef?= Date: Sat, 23 Dec 2017 16:34:34 +0100 Subject: [PATCH] Merging PR from sunhater/kcfinder #136 that fixes symlink problem See PR at https://github.com/sunhater/kcfinder/pull/136/files#diff-28ad25e2732bc428e58ddeb0df8f017b --- core/class/uploader.php | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/core/class/uploader.php b/core/class/uploader.php index 447d0ff..713fb11 100644 --- a/core/class/uploader.php +++ b/core/class/uploader.php @@ -169,18 +169,18 @@ public function __construct() { list($unused, $protocol, $domain, $unused, $port, $path) = $patt; $path = path::normalize($path); $this->config['uploadURL'] = "$protocol://$domain" . (strlen($port) ? ":$port" : "") . "/$path"; - $this->config['uploadDir'] = strlen($this->config['uploadDir']) + $this->config['uploadDir'] = $this->realpath(strlen($this->config['uploadDir']) ? path::normalize($this->config['uploadDir']) - : path::url2fullPath("/$path"); - $this->typeDir = "{$this->config['uploadDir']}/{$this->type}"; + : path::url2fullPath("/$path")); + $this->typeDir = $this->realpath("{$this->config['uploadDir']}/{$this->type}"); $this->typeURL = "{$this->config['uploadURL']}/{$this->type}"; // SITE ROOT } elseif ($this->config['uploadURL'] == "/") { - $this->config['uploadDir'] = strlen($this->config['uploadDir']) + $this->config['uploadDir'] = $this->realpath(strlen($this->config['uploadDir']) ? path::normalize($this->config['uploadDir']) - : path::normalize(realpath($_SERVER['DOCUMENT_ROOT'])); - $this->typeDir = "{$this->config['uploadDir']}/{$this->type}"; + : path::normalize(realpath($_SERVER['DOCUMENT_ROOT']))); + $this->typeDir = $this->realpath("{$this->config['uploadDir']}/{$this->type}"); $this->typeURL = "/{$this->type}"; // ABSOLUTE & RELATIVE @@ -188,10 +188,10 @@ public function __construct() { $this->config['uploadURL'] = (substr($this->config['uploadURL'], 0, 1) === "/") ? path::normalize($this->config['uploadURL']) : path::rel2abs_url($this->config['uploadURL']); - $this->config['uploadDir'] = strlen($this->config['uploadDir']) + $this->config['uploadDir'] = $this->realpath(strlen($this->config['uploadDir']) ? path::normalize($this->config['uploadDir']) - : path::url2fullPath($this->config['uploadURL']); - $this->typeDir = "{$this->config['uploadDir']}/{$this->type}"; + : path::url2fullPath($this->config['uploadURL'])); + $this->typeDir = $this->realpath("{$this->config['uploadDir']}/{$this->type}"); $this->typeURL = "{$this->config['uploadURL']}/{$this->type}"; } @@ -261,6 +261,20 @@ public function __construct() { } } + protected function realpath($path) { + // PHP's realpath() does not work on files that don't exist, but + // there might be a symlink somewhere in the path so we need to + // check it. + $existing_path = $path; + while (!file_exists($existing_path)) { + $existing_path = dirname($existing_path); + } + $rPath = realpath($existing_path) . substr($path, strlen($existing_path)); + if (strtoupper(substr(PHP_OS, 0, 3)) == "WIN") + $rPath = str_replace("\\", "/", $rPath); + return $rPath; + } + public function upload() { $config = &$this->config; $file = &$this->file; @@ -355,9 +369,7 @@ protected function normalizeDirname($dirname) { } protected function checkFilePath($file) { - $rPath = realpath($file); - if (strtoupper(substr(PHP_OS, 0, 3)) == "WIN") - $rPath = str_replace("\\", "/", $rPath); + $rPath = $this->realpath($file); return (substr($rPath, 0, strlen($this->typeDir)) === $this->typeDir); }