SFTP: Add enablePathCanonicalization()/disablePathCanonicalization()

This commit is contained in:
David Hedberg 2017-06-02 09:58:28 +02:00 committed by terrafrost
parent 0bb6c9b974
commit bbf467babd

View File

@ -199,7 +199,7 @@ class Net_SFTP extends Net_SSH2
* Current working directory * Current working directory
* *
* @var string * @var string
* @see self::_realpath() * @see self::realpath()
* @see self::chdir() * @see self::chdir()
* @access private * @access private
*/ */
@ -277,6 +277,20 @@ class Net_SFTP extends Net_SSH2
*/ */
var $sortOptions = array(); 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. * Default Constructor.
* *
@ -630,6 +644,26 @@ class Net_SFTP extends Net_SSH2
$this->stat_cache = array(); $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 * 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 * 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. * the absolute (canonicalized) path.
* *
* If canonicalize_paths has been disabled using disablePathCanonicalization(), $path is returned as-is.
*
* @see self::chdir() * @see self::chdir()
* @see self::disablePathCanonicalization()
* @param string $path * @param string $path
* @return mixed * @return mixed
* @access private * @access private
*/ */
function _realpath($path) function _realpath($path)
{ {
if (!$this->canonicalize_paths) {
return $path;
}
if ($this->pwd === false) { if ($this->pwd === false) {
// http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.9 // 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))) { 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 * 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. * The second parameter can be either NET_SFTP_STAT or NET_SFTP_LSTAT.
* *
* @param string $filename * @param string $filename
@ -1510,7 +1551,7 @@ class Net_SFTP extends Net_SSH2
return true; 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 // 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. // tell us if the file actually exists.
// incidentally, SFTPv4+ adds an additional 32-bit integer field - flags - to the following: // incidentally, SFTPv4+ adds an additional 32-bit integer field - flags - to the following: