[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/vendor/phpseclib/phpseclib/phpseclib/Net/ -> SFTP.php (summary)

Pure-PHP implementation of SFTP. PHP version 5

Author: Jim Wigginton
Copyright: 2009 Jim Wigginton
License: http://www.opensource.org/licenses/mit-license.html MIT License
Link: http://phpseclib.sourceforge.net
File Size: 3169 lines (100 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

SFTP:: (72 methods):
  __construct()
  login()
  disableStatCache()
  enableStatCache()
  clearStatCache()
  enablePathCanonicalization()
  disablePathCanonicalization()
  pwd()
  _logError()
  realpath()
  _realpath()
  chdir()
  nlist()
  _nlist_helper()
  rawlist()
  _list()
  _comparator()
  setListOrder()
  size()
  _update_stat_cache()
  _remove_from_stat_cache()
  _query_stat_cache()
  stat()
  lstat()
  _stat()
  truncate()
  touch()
  chown()
  chgrp()
  chmod()
  _setstat()
  _setstat_recursive()
  readlink()
  symlink()
  mkdir()
  _mkdir_helper()
  rmdir()
  put()
  _read_put_responses()
  _close_handle()
  get()
  delete()
  _delete_recursive()
  file_exists()
  is_dir()
  is_file()
  is_link()
  is_readable()
  is_writable()
  is_writeable()
  fileatime()
  filemtime()
  fileperms()
  fileowner()
  filegroup()
  filesize()
  filetype()
  _get_stat_cache_prop()
  _get_lstat_cache_prop()
  _get_xstat_cache_prop()
  rename()
  _parseAttributes()
  _parseMode()
  _parseLongname()
  _send_sftp_packet()
  _reset_connection()
  _get_sftp_packet()
  getSFTPLog()
  getSFTPErrors()
  getLastSFTPError()
  getSupportedVersions()
  _disconnect()


Class: SFTP  - X-Ref

Pure-PHP implementations of SFTP.

__construct($host, $port = 22, $timeout = 10)   X-Ref
Default Constructor.

Connects to an SFTP server

param: string $host
param: int $port
param: int $timeout
return: \phpseclib\Net\SFTP

login($username)   X-Ref
Login

param: string $username
param: string $password
return: bool

disableStatCache()   X-Ref
Disable the stat cache


enableStatCache()   X-Ref
Enable the stat cache


clearStatCache()   X-Ref
Clear the stat cache


enablePathCanonicalization()   X-Ref
Enable path canonicalization


disablePathCanonicalization()   X-Ref
Enable path canonicalization


pwd()   X-Ref
Returns the current directory name

return: mixed

_logError($response, $status = -1)   X-Ref
Logs errors

param: string $response
param: int $status

realpath($path)   X-Ref
Returns canonicalized absolute pathname

realpath() expands all symbolic links and resolves references to '/./', '/../' and extra '/' characters in the input
path and returns the canonicalized absolute pathname.

param: string $path
return: mixed

_realpath($path)   X-Ref
Canonicalize the Server-Side Path Name

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.

param: string $path
return: mixed
see: self::chdir()
see: self::disablePathCanonicalization()

chdir($dir)   X-Ref
Changes the current directory

param: string $dir
return: bool

nlist($dir = '.', $recursive = false)   X-Ref
Returns a list of files in the given directory

param: string $dir
param: bool $recursive
return: mixed

_nlist_helper($dir, $recursive, $relativeDir)   X-Ref
Helper method for nlist

param: string $dir
param: bool $recursive
param: string $relativeDir
return: mixed

rawlist($dir = '.', $recursive = false)   X-Ref
Returns a detailed list of files in the given directory

param: string $dir
param: bool $recursive
return: mixed

_list($dir, $raw = true)   X-Ref
Reads a list, be it detailed or not, of files in the given directory

param: string $dir
param: bool $raw
return: mixed

_comparator($a, $b)   X-Ref
Compares two rawlist entries using parameters set by setListOrder()

Intended for use with uasort()

param: array $a
param: array $b
return: int

setListOrder()   X-Ref
Defines how nlist() and rawlist() will be sorted - if at all.

If sorting is enabled directories and files will be sorted independently with
directories appearing before files in the resultant array that is returned.

Any parameter returned by stat is a valid sort parameter for this function.
Filename comparisons are case insensitive.

Examples:

$sftp->setListOrder('filename', SORT_ASC);
$sftp->setListOrder('size', SORT_DESC, 'filename', SORT_ASC);
$sftp->setListOrder(true);
Separates directories from files but doesn't do any sorting beyond that
$sftp->setListOrder();
Don't do any sort of sorting


size($filename)   X-Ref
Returns the file size, in bytes, or false, on failure

Files larger than 4GB will show up as being exactly 4GB.

param: string $filename
return: mixed

_update_stat_cache($path, $value)   X-Ref
Save files / directories to cache

param: string $path
param: mixed $value

_remove_from_stat_cache($path)   X-Ref
Remove files / directories from cache

param: string $path
return: bool

_query_stat_cache($path)   X-Ref
Checks cache for path

Mainly used by file_exists

param: string $dir
return: mixed

stat($filename)   X-Ref
Returns general information about a file.

Returns an array on success and false otherwise.

param: string $filename
return: mixed

lstat($filename)   X-Ref
Returns general information about a file or symbolic link.

Returns an array on success and false otherwise.

param: string $filename
return: mixed

_stat($filename, $type)   X-Ref
Returns general information about a file or symbolic link

Determines information without calling \phpseclib\Net\SFTP::realpath().
The second parameter can be either NET_SFTP_STAT or NET_SFTP_LSTAT.

param: string $filename
param: int $type
return: mixed

truncate($filename, $new_size)   X-Ref
Truncates a file to a given length

param: string $filename
param: int $new_size
return: bool

touch($filename, $time = null, $atime = null)   X-Ref
Sets access and modification time of file.

If the file does not exist, it will be created.

param: string $filename
param: int $time
param: int $atime
return: bool

chown($filename, $uid, $recursive = false)   X-Ref
Changes file or directory owner

Returns true on success or false on error.

param: string $filename
param: int $uid
param: bool $recursive
return: bool

chgrp($filename, $gid, $recursive = false)   X-Ref
Changes file or directory group

Returns true on success or false on error.

param: string $filename
param: int $gid
param: bool $recursive
return: bool

chmod($mode, $filename, $recursive = false)   X-Ref
Set permissions on a file.

Returns the new file permissions on success or false on error.
If $recursive is true than this just returns true or false.

param: int $mode
param: string $filename
param: bool $recursive
return: mixed

_setstat($filename, $attr, $recursive)   X-Ref
Sets information about a file

param: string $filename
param: string $attr
param: bool $recursive
return: bool

_setstat_recursive($path, $attr, &$i)   X-Ref
Recursively sets information on directories on the SFTP server

Minimizes directory lookups and SSH_FXP_STATUS requests for speed.

param: string $path
param: string $attr
param: int $i
return: bool

readlink($link)   X-Ref
Return the target of a symbolic link

param: string $link
return: mixed

symlink($target, $link)   X-Ref
Create a symlink

symlink() creates a symbolic link to the existing target with the specified name link.

param: string $target
param: string $link
return: bool

mkdir($dir, $mode = -1, $recursive = false)   X-Ref
Creates a directory.

param: string $dir
return: bool

_mkdir_helper($dir, $attr)   X-Ref
Helper function for directory creation

param: string $dir
return: bool

rmdir($dir)   X-Ref
Removes a directory.

param: string $dir
return: bool

put($remote_file, $data, $mode = self::SOURCE_STRING, $start = -1, $local_start = -1, $progressCallback = null)   X-Ref
Uploads a file to the SFTP server.

By default, \phpseclib\Net\SFTP::put() does not read from the local filesystem.  $data is dumped directly into $remote_file.
So, for example, if you set $data to 'filename.ext' and then do \phpseclib\Net\SFTP::get(), you will get a file, twelve bytes
long, containing 'filename.ext' as its contents.

Setting $mode to self::SOURCE_LOCAL_FILE will change the above behavior.  With self::SOURCE_LOCAL_FILE, $remote_file will
contain as many bytes as filename.ext does on your local filesystem.  If your filename.ext is 1MB then that is how
large $remote_file will be, as well.

Setting $mode to self::SOURCE_CALLBACK will use $data as callback function, which gets only one parameter -- number of bytes to return, and returns a string if there is some data or null if there is no more data

If $data is a resource then it'll be used as a resource instead.

Currently, only binary mode is supported.  As such, if the line endings need to be adjusted, you will need to take
care of that, yourself.

$mode can take an additional two parameters - self::RESUME and self::RESUME_START. These are bitwise AND'd with
$mode. So if you want to resume upload of a 300mb file on the local file system you'd set $mode to the following:

self::SOURCE_LOCAL_FILE | self::RESUME

If you wanted to simply append the full contents of a local file to the full contents of a remote file you'd replace
self::RESUME with self::RESUME_START.

If $mode & (self::RESUME | self::RESUME_START) then self::RESUME_START will be assumed.

$start and $local_start give you more fine grained control over this process and take precident over self::RESUME
when they're non-negative. ie. $start could let you write at the end of a file (like self::RESUME) or in the middle
of one. $local_start could let you start your reading from the end of a file (like self::RESUME_START) or in the
middle of one.

Setting $local_start to > 0 or $mode | self::RESUME_START doesn't do anything unless $mode | self::SOURCE_LOCAL_FILE.

param: string $remote_file
param: string|resource $data
param: int $mode
param: int $start
param: int $local_start
param: callable|null $progressCallback
return: bool

_read_put_responses($i)   X-Ref
Reads multiple successive SSH_FXP_WRITE responses

Sending an SSH_FXP_WRITE packet and immediately reading its response isn't as efficient as blindly sending out $i
SSH_FXP_WRITEs, in succession, and then reading $i responses.

param: int $i
return: bool

_close_handle($handle)   X-Ref
Close handle

param: string $handle
return: bool

get($remote_file, $local_file = false, $offset = 0, $length = -1, $progressCallback = null)   X-Ref
Downloads a file from the SFTP server.

Returns a string containing the contents of $remote_file if $local_file is left undefined or a boolean false if
the operation was unsuccessful.  If $local_file is defined, returns true or false depending on the success of the
operation.

$offset and $length can be used to download files in chunks.

param: string $remote_file
param: string $local_file
param: int $offset
param: int $length
param: callable|null $progressCallback
return: mixed

delete($path, $recursive = true)   X-Ref
Deletes a file on the SFTP server.

param: string $path
param: bool $recursive
return: bool

_delete_recursive($path, &$i)   X-Ref
Recursively deletes directories on the SFTP server

Minimizes directory lookups and SSH_FXP_STATUS requests for speed.

param: string $path
param: int $i
return: bool

file_exists($path)   X-Ref
Checks whether a file or directory exists

param: string $path
return: bool

is_dir($path)   X-Ref
Tells whether the filename is a directory

param: string $path
return: bool

is_file($path)   X-Ref
Tells whether the filename is a regular file

param: string $path
return: bool

is_link($path)   X-Ref
Tells whether the filename is a symbolic link

param: string $path
return: bool

is_readable($path)   X-Ref
Tells whether a file exists and is readable

param: string $path
return: bool

is_writable($path)   X-Ref
Tells whether the filename is writable

param: string $path
return: bool

is_writeable($path)   X-Ref
Tells whether the filename is writeable

Alias of is_writable

param: string $path
return: bool

fileatime($path)   X-Ref
Gets last access time of file

param: string $path
return: mixed

filemtime($path)   X-Ref
Gets file modification time

param: string $path
return: mixed

fileperms($path)   X-Ref
Gets file permissions

param: string $path
return: mixed

fileowner($path)   X-Ref
Gets file owner

param: string $path
return: mixed

filegroup($path)   X-Ref
Gets file group

param: string $path
return: mixed

filesize($path)   X-Ref
Gets file size

param: string $path
return: mixed

filetype($path)   X-Ref
Gets file type

param: string $path
return: mixed

_get_stat_cache_prop($path, $prop)   X-Ref
Return a stat properity

Uses cache if appropriate.

param: string $path
param: string $prop
return: mixed

_get_lstat_cache_prop($path, $prop)   X-Ref
Return an lstat properity

Uses cache if appropriate.

param: string $path
param: string $prop
return: mixed

_get_xstat_cache_prop($path, $prop, $type)   X-Ref
Return a stat or lstat properity

Uses cache if appropriate.

param: string $path
param: string $prop
return: mixed

rename($oldname, $newname)   X-Ref
Renames a file or a directory on the SFTP server

param: string $oldname
param: string $newname
return: bool

_parseAttributes(&$response)   X-Ref
Parse Attributes

See '7.  File Attributes' of draft-ietf-secsh-filexfer-13 for more info.

param: string $response
return: array

_parseMode($mode)   X-Ref
Attempt to identify the file type

Quoting the SFTP RFC, "Implementations MUST NOT send bits that are not defined" but they seem to anyway

param: int $mode
return: int

_parseLongname($longname)   X-Ref
Parse Longname

SFTPv3 doesn't provide any easy way of identifying a file type.  You could try to open
a file as a directory and see if an error is returned or you could try to parse the
SFTPv3-specific longname field of the SSH_FXP_NAME packet.  That's what this function does.
The result is returned using the
{@link http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-5.2 SFTPv4 type constants}.

If the longname is in an unrecognized format bool(false) is returned.

param: string $longname
return: mixed

_send_sftp_packet($type, $data, $request_id = 1)   X-Ref
Sends SFTP Packets

See '6. General Packet Format' of draft-ietf-secsh-filexfer-13 for more info.

param: int $type
param: string $data
return: bool
see: self::_get_sftp_packet()
see: self::_send_channel_packet()

_reset_connection($reason)   X-Ref
Resets a connection for re-use

param: int $reason

_get_sftp_packet($request_id = null)   X-Ref
Receives SFTP Packets

See '6. General Packet Format' of draft-ietf-secsh-filexfer-13 for more info.

Incidentally, the number of SSH_MSG_CHANNEL_DATA messages has no bearing on the number of SFTP packets present.
There can be one SSH_MSG_CHANNEL_DATA messages containing two SFTP packets or there can be two SSH_MSG_CHANNEL_DATA
messages containing one SFTP packet.

return: string
see: self::_send_sftp_packet()

getSFTPLog()   X-Ref
Returns a log of the packets that have been sent and received.

Returns a string if NET_SFTP_LOGGING == NET_SFTP_LOG_COMPLEX, an array if NET_SFTP_LOGGING == NET_SFTP_LOG_SIMPLE and false if !defined('NET_SFTP_LOGGING')

return: string or Array

getSFTPErrors()   X-Ref
Returns all errors

return: array

getLastSFTPError()   X-Ref
Returns the last error

return: string

getSupportedVersions()   X-Ref
Get supported SFTP versions

return: array

_disconnect($reason)   X-Ref
Disconnect

param: int $reason
return: bool