From bbf467babde690cf9f074a916d11313f677b2606 Mon Sep 17 00:00:00 2001 From: David Hedberg Date: Fri, 2 Jun 2017 09:58:28 +0200 Subject: [PATCH] SFTP: Add enablePathCanonicalization()/disablePathCanonicalization() --- phpseclib/Net/SFTP.php | 47 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/phpseclib/Net/SFTP.php b/phpseclib/Net/SFTP.php index 40c97daf..7665482d 100644 --- a/phpseclib/Net/SFTP.php +++ b/phpseclib/Net/SFTP.php @@ -199,7 +199,7 @@ class Net_SFTP extends Net_SSH2 * Current working directory * * @var string - * @see self::_realpath() + * @see self::realpath() * @see self::chdir() * @access private */ @@ -277,6 +277,20 @@ class Net_SFTP extends Net_SSH2 */ var $sortOptions = array(); + /** + * Canonicalization Flag + * + * Determines whether or not paths should be canonicalized before being + * passed on to the remote server. + * + * @see self::enablePathCanonicalization() + * @see self::disablePathCanonicalization() + * @see self::realpath() + * @var bool + * @access private + */ + var $canonicalize_paths = true; + /** * Default Constructor. * @@ -630,6 +644,26 @@ class Net_SFTP extends Net_SSH2 $this->stat_cache = array(); } + /** + * Enable path canonicalization + * + * @access public + */ + function enablePathCanonicalization() + { + $this->canonicalize_paths = true; + } + + /** + * Enable path canonicalization + * + * @access public + */ + function disablePathCanonicalization() + { + $this->canonicalize_paths = false; + } + /** * Returns the current directory name * @@ -688,13 +722,20 @@ class Net_SFTP extends Net_SSH2 * SFTP doesn't provide a mechanism by which the current working directory can be changed, so we'll emulate it. Returns * the absolute (canonicalized) path. * + * If canonicalize_paths has been disabled using disablePathCanonicalization(), $path is returned as-is. + * * @see self::chdir() + * @see self::disablePathCanonicalization() * @param string $path * @return mixed * @access private */ function _realpath($path) { + if (!$this->canonicalize_paths) { + return $path; + } + if ($this->pwd === false) { // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.9 if (!$this->_send_sftp_packet(NET_SFTP_REALPATH, pack('Na*', strlen($path), $path))) { @@ -1349,7 +1390,7 @@ class Net_SFTP extends Net_SSH2 /** * Returns general information about a file or symbolic link * - * Determines information without calling Net_SFTP::_realpath(). + * Determines information without calling Net_SFTP::realpath(). * The second parameter can be either NET_SFTP_STAT or NET_SFTP_LSTAT. * * @param string $filename @@ -1510,7 +1551,7 @@ class Net_SFTP extends Net_SSH2 return true; } - $filename = $this->_realPath($filename); + $filename = $this->realpath($filename); // rather than return what the permissions *should* be, we'll return what they actually are. this will also // tell us if the file actually exists. // incidentally, SFTPv4+ adds an additional 32-bit integer field - flags - to the following: