2013-03-13 02:57:40 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SFTP Stream Wrapper
|
|
|
|
*
|
|
|
|
* Creates an sftp:// protocol handler that can be used with, for example, fopen(), dir(), etc.
|
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
2013-12-10 20:10:37 +01:00
|
|
|
* @category Net
|
2014-12-10 02:31:41 +01:00
|
|
|
* @package SFTP
|
2013-12-10 20:10:37 +01:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
2014-12-10 00:02:44 +01:00
|
|
|
* @copyright 2013 Jim Wigginton
|
2013-12-10 20:10:37 +01:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link http://phpseclib.sourceforge.net
|
2013-03-13 02:57:40 +01:00
|
|
|
*/
|
|
|
|
|
2014-12-10 02:31:41 +01:00
|
|
|
namespace phpseclib\Net\SFTP;
|
|
|
|
|
2014-12-24 22:05:56 +01:00
|
|
|
use phpseclib\Crypt\RSA;
|
2014-12-10 02:31:41 +01:00
|
|
|
use phpseclib\Net\SFTP;
|
2016-04-30 23:23:35 +02:00
|
|
|
use phpseclib\Net\SSH2;
|
2014-12-10 02:31:41 +01:00
|
|
|
|
2013-03-13 02:57:40 +01:00
|
|
|
/**
|
|
|
|
* SFTP Stream Wrapper
|
|
|
|
*
|
2014-12-10 02:31:41 +01:00
|
|
|
* @package SFTP
|
2013-03-13 02:57:40 +01:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access public
|
|
|
|
*/
|
2014-12-10 02:31:41 +01:00
|
|
|
class Stream
|
2013-12-03 19:34:41 +01:00
|
|
|
{
|
2013-03-13 02:57:40 +01:00
|
|
|
/**
|
|
|
|
* SFTP instances
|
|
|
|
*
|
|
|
|
* Rather than re-create the connection we re-use instances if possible
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var array
|
2013-03-13 02:57:40 +01:00
|
|
|
*/
|
|
|
|
static $instances;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SFTP instance
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var object
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private $sftp;
|
2013-03-13 02:57:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Path
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var string
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private $path;
|
2013-03-13 02:57:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mode
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var string
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private $mode;
|
2013-03-13 02:57:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Position
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var int
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private $pos;
|
2013-03-13 02:57:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Size
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var int
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private $size;
|
2013-03-13 02:57:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Directory entries
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var array
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private $entries;
|
2013-03-13 02:57:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* EOF flag
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var bool
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private $eof;
|
2013-03-13 02:57:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Context resource
|
|
|
|
*
|
2018-03-03 13:07:14 +01:00
|
|
|
* Technically this needs to be publicly accessible so PHP can set it directly
|
2013-03-13 02:57:40 +01:00
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var resource
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
public $context;
|
2013-03-13 02:57:40 +01:00
|
|
|
|
2013-04-16 06:58:13 +02:00
|
|
|
/**
|
|
|
|
* Notification callback function
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var callable
|
2013-04-16 06:58:13 +02:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private $notification;
|
2013-04-16 06:58:13 +02:00
|
|
|
|
2014-06-27 00:22:41 +02:00
|
|
|
/**
|
|
|
|
* Registers this class as a URL wrapper.
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $protocol The wrapper name to be registered.
|
|
|
|
* @return bool True on success, false otherwise.
|
2014-06-27 00:22:41 +02:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
public static function register($protocol = 'sftp')
|
2014-06-27 00:22:41 +02:00
|
|
|
{
|
|
|
|
if (in_array($protocol, stream_get_wrappers(), true)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-27 14:22:47 +02:00
|
|
|
return stream_wrapper_register($protocol, get_called_class());
|
2014-06-27 00:22:41 +02:00
|
|
|
}
|
|
|
|
|
2013-05-13 07:41:52 +02:00
|
|
|
/**
|
|
|
|
* The Constructor
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
public function __construct()
|
2013-05-13 07:41:52 +02:00
|
|
|
{
|
2013-11-20 16:11:14 +01:00
|
|
|
if (defined('NET_SFTP_STREAM_LOGGING')) {
|
|
|
|
echo "__construct()\r\n";
|
|
|
|
}
|
2013-05-13 07:41:52 +02:00
|
|
|
}
|
|
|
|
|
2013-03-13 02:57:40 +01:00
|
|
|
/**
|
|
|
|
* Path Parser
|
|
|
|
*
|
|
|
|
* Extract a path from a URI and actually connect to an SSH server if appropriate
|
|
|
|
*
|
2013-04-16 06:58:13 +02:00
|
|
|
* If "notification" is set as a context parameter the message code for successful login is
|
|
|
|
* NET_SSH2_MSG_USERAUTH_SUCCESS. For a failed login it's NET_SSH2_MSG_USERAUTH_FAILURE.
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $path
|
|
|
|
* @return string
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function parse_path($path)
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
2016-04-10 18:30:59 +02:00
|
|
|
$orig = $path;
|
2016-11-30 22:19:23 +01:00
|
|
|
extract(parse_url($path) + ['port' => 22]);
|
2016-04-10 18:30:59 +02:00
|
|
|
if (isset($query)) {
|
|
|
|
$path.= '?' . $query;
|
|
|
|
} elseif (preg_match('/(\?|\?#)$/', $orig)) {
|
|
|
|
$path.= '?';
|
|
|
|
}
|
|
|
|
if (isset($fragment)) {
|
|
|
|
$path.= '#' . $fragment;
|
|
|
|
} elseif ($orig[strlen($orig) - 1] == '#') {
|
|
|
|
$path.= '#';
|
|
|
|
}
|
2013-03-14 05:30:52 +01:00
|
|
|
|
2013-03-13 02:57:40 +01:00
|
|
|
if (!isset($host)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-28 20:55:14 +02:00
|
|
|
if (isset($this->context)) {
|
|
|
|
$context = stream_context_get_params($this->context);
|
|
|
|
if (isset($context['notification'])) {
|
|
|
|
$this->notification = $context['notification'];
|
|
|
|
}
|
2013-04-16 06:58:13 +02:00
|
|
|
}
|
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
if (preg_match('/^{[a-z0-9]+}$/i', $host)) {
|
|
|
|
$host = SSH2::getConnectionByResourceId($host);
|
|
|
|
if ($host === false) {
|
2013-03-13 02:57:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-30 23:23:35 +02:00
|
|
|
$this->sftp = $host;
|
2013-03-13 02:57:40 +01:00
|
|
|
} else {
|
2013-11-19 20:23:54 +01:00
|
|
|
if (isset($this->context)) {
|
2013-08-28 20:55:14 +02:00
|
|
|
$context = stream_context_get_options($this->context);
|
|
|
|
}
|
2014-05-29 23:14:03 +02:00
|
|
|
if (isset($context[$scheme]['session'])) {
|
|
|
|
$sftp = $context[$scheme]['session'];
|
2013-03-13 02:57:40 +01:00
|
|
|
}
|
2014-05-29 23:14:03 +02:00
|
|
|
if (isset($context[$scheme]['sftp'])) {
|
|
|
|
$sftp = $context[$scheme]['sftp'];
|
2013-03-13 02:57:40 +01:00
|
|
|
}
|
2014-12-24 22:05:56 +01:00
|
|
|
if (isset($sftp) && $sftp instanceof SFTP) {
|
2013-03-13 02:57:40 +01:00
|
|
|
$this->sftp = $sftp;
|
|
|
|
return $path;
|
|
|
|
}
|
2014-05-29 23:14:03 +02:00
|
|
|
if (isset($context[$scheme]['username'])) {
|
|
|
|
$user = $context[$scheme]['username'];
|
2013-03-13 02:57:40 +01:00
|
|
|
}
|
2014-05-29 23:14:03 +02:00
|
|
|
if (isset($context[$scheme]['password'])) {
|
|
|
|
$pass = $context[$scheme]['password'];
|
2013-03-13 02:57:40 +01:00
|
|
|
}
|
2014-12-24 22:05:56 +01:00
|
|
|
if (isset($context[$scheme]['privkey']) && $context[$scheme]['privkey'] instanceof RSA) {
|
2014-05-29 23:14:03 +02:00
|
|
|
$pass = $context[$scheme]['privkey'];
|
2013-03-13 02:57:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($user) || !isset($pass)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-17 01:16:54 +01:00
|
|
|
// casting $pass to a string is necessary in the event that it's a \phpseclib\Crypt\RSA object
|
2013-03-14 05:30:52 +01:00
|
|
|
if (isset(self::$instances[$host][$port][$user][(string) $pass])) {
|
|
|
|
$this->sftp = self::$instances[$host][$port][$user][(string) $pass];
|
|
|
|
} else {
|
2014-12-10 02:31:41 +01:00
|
|
|
$this->sftp = new SFTP($host, $port);
|
2014-05-26 22:42:14 +02:00
|
|
|
$this->sftp->disableStatCache();
|
2013-04-16 06:58:13 +02:00
|
|
|
if (isset($this->notification) && is_callable($this->notification)) {
|
|
|
|
/* if !is_callable($this->notification) we could do this:
|
|
|
|
|
|
|
|
user_error('fopen(): failed to call user notifier', E_USER_WARNING);
|
|
|
|
|
|
|
|
the ftp wrapper gives errors like that when the notifier isn't callable.
|
|
|
|
i've opted not to do that, however, since the ftp wrapper gives the line
|
|
|
|
on which the fopen occurred as the line number - not the line that the
|
|
|
|
user_error is on.
|
|
|
|
*/
|
|
|
|
call_user_func($this->notification, STREAM_NOTIFY_CONNECT, STREAM_NOTIFY_SEVERITY_INFO, '', 0, 0, 0);
|
|
|
|
call_user_func($this->notification, STREAM_NOTIFY_AUTH_REQUIRED, STREAM_NOTIFY_SEVERITY_INFO, '', 0, 0, 0);
|
|
|
|
if (!$this->sftp->login($user, $pass)) {
|
|
|
|
call_user_func($this->notification, STREAM_NOTIFY_AUTH_RESULT, STREAM_NOTIFY_SEVERITY_ERR, 'Login Failure', NET_SSH2_MSG_USERAUTH_FAILURE, 0, 0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
call_user_func($this->notification, STREAM_NOTIFY_AUTH_RESULT, STREAM_NOTIFY_SEVERITY_INFO, 'Login Success', NET_SSH2_MSG_USERAUTH_SUCCESS, 0, 0);
|
|
|
|
} else {
|
|
|
|
if (!$this->sftp->login($user, $pass)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-03-14 05:30:52 +01:00
|
|
|
}
|
|
|
|
self::$instances[$host][$port][$user][(string) $pass] = $this->sftp;
|
2013-03-13 02:57:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens file or URL
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $path
|
|
|
|
* @param string $mode
|
|
|
|
* @param int $options
|
|
|
|
* @param string $opened_path
|
|
|
|
* @return bool
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _stream_open($path, $mode, $options, &$opened_path)
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
2017-01-08 02:51:56 +01:00
|
|
|
$path = $this->parse_path($path);
|
2013-03-13 02:57:40 +01:00
|
|
|
|
|
|
|
if ($path === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->path = $path;
|
|
|
|
|
|
|
|
$this->size = $this->sftp->size($path);
|
|
|
|
$this->mode = preg_replace('#[bt]$#', '', $mode);
|
2013-11-19 23:13:10 +01:00
|
|
|
$this->eof = false;
|
2013-03-13 02:57:40 +01:00
|
|
|
|
|
|
|
if ($this->size === false) {
|
|
|
|
if ($this->mode[0] == 'r') {
|
|
|
|
return false;
|
2015-06-25 14:24:18 +02:00
|
|
|
} else {
|
|
|
|
$this->sftp->touch($path);
|
2015-06-25 14:26:35 +02:00
|
|
|
$this->size = 0;
|
2013-03-13 02:57:40 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch ($this->mode[0]) {
|
|
|
|
case 'x':
|
|
|
|
return false;
|
|
|
|
case 'w':
|
|
|
|
$this->sftp->truncate($path, 0);
|
2015-06-25 14:26:35 +02:00
|
|
|
$this->size = 0;
|
2013-03-13 02:57:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-08 08:21:53 +02:00
|
|
|
$this->pos = $this->mode[0] != 'a' ? 0 : $this->size;
|
2013-03-13 02:57:40 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read from stream
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param int $count
|
|
|
|
* @return mixed
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _stream_read($count)
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
|
|
|
switch ($this->mode) {
|
|
|
|
case 'w':
|
|
|
|
case 'a':
|
|
|
|
case 'x':
|
|
|
|
case 'c':
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// commented out because some files - eg. /dev/urandom - will say their size is 0 when in fact it's kinda infinite
|
|
|
|
//if ($this->pos >= $this->size) {
|
|
|
|
// $this->eof = true;
|
|
|
|
// return false;
|
|
|
|
//}
|
|
|
|
|
|
|
|
$result = $this->sftp->get($this->path, false, $this->pos, $count);
|
2013-04-16 06:58:13 +02:00
|
|
|
if (isset($this->notification) && is_callable($this->notification)) {
|
|
|
|
if ($result === false) {
|
|
|
|
call_user_func($this->notification, STREAM_NOTIFY_FAILURE, STREAM_NOTIFY_SEVERITY_ERR, $this->sftp->getLastSFTPError(), NET_SFTP_OPEN, 0, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// seems that PHP calls stream_read in 8k chunks
|
2013-05-08 08:21:53 +02:00
|
|
|
call_user_func($this->notification, STREAM_NOTIFY_PROGRESS, STREAM_NOTIFY_SEVERITY_INFO, '', 0, strlen($result), $this->size);
|
2013-04-16 06:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($result)) { // ie. false or empty string
|
2013-03-13 02:57:40 +01:00
|
|
|
$this->eof = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->pos+= strlen($result);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write to stream
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $data
|
|
|
|
* @return mixed
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _stream_write($data)
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
|
|
|
switch ($this->mode) {
|
|
|
|
case 'r':
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-10 02:31:41 +01:00
|
|
|
$result = $this->sftp->put($this->path, $data, SFTP::SOURCE_STRING, $this->pos);
|
2013-04-16 06:58:13 +02:00
|
|
|
if (isset($this->notification) && is_callable($this->notification)) {
|
|
|
|
if (!$result) {
|
|
|
|
call_user_func($this->notification, STREAM_NOTIFY_FAILURE, STREAM_NOTIFY_SEVERITY_ERR, $this->sftp->getLastSFTPError(), NET_SFTP_OPEN, 0, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// seems that PHP splits up strings into 8k blocks before calling stream_write
|
|
|
|
call_user_func($this->notification, STREAM_NOTIFY_PROGRESS, STREAM_NOTIFY_SEVERITY_INFO, '', 0, strlen($data), strlen($data));
|
|
|
|
}
|
|
|
|
|
2013-03-13 02:57:40 +01:00
|
|
|
if ($result === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->pos+= strlen($data);
|
|
|
|
if ($this->pos > $this->size) {
|
|
|
|
$this->size = $this->pos;
|
|
|
|
}
|
|
|
|
$this->eof = false;
|
|
|
|
return strlen($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the current position of a stream
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return int
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _stream_tell()
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
|
|
|
return $this->pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for end-of-file on a file pointer
|
|
|
|
*
|
|
|
|
* In my testing there are four classes functions that normally effect the pointer:
|
|
|
|
* fseek, fputs / fwrite, fgets / fread and ftruncate.
|
|
|
|
*
|
|
|
|
* Only fgets / fread, however, results in feof() returning true. do fputs($fp, 'aaa') on a blank file and feof()
|
|
|
|
* will return false. do fread($fp, 1) and feof() will then return true. do fseek($fp, 10) on ablank file and feof()
|
|
|
|
* will return false. do fread($fp, 1) and feof() will then return true.
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return bool
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _stream_eof()
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
|
|
|
return $this->eof;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Seeks to specific location in a stream
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param int $offset
|
|
|
|
* @param int $whence
|
|
|
|
* @return bool
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _stream_seek($offset, $whence)
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
|
|
|
switch ($whence) {
|
|
|
|
case SEEK_SET:
|
|
|
|
if ($offset >= $this->size || $offset < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
|
|
$offset+= $this->pos;
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
|
|
|
$offset+= $this->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->pos = $offset;
|
|
|
|
$this->eof = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change stream options
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $path
|
|
|
|
* @param int $option
|
|
|
|
* @param mixed $var
|
|
|
|
* @return bool
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _stream_metadata($path, $option, $var)
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
2017-01-08 02:51:56 +01:00
|
|
|
$path = $this->parse_path($path);
|
2013-03-13 02:57:40 +01:00
|
|
|
if ($path === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// stream_metadata was introduced in PHP 5.4.0 but as of 5.4.11 the constants haven't been defined
|
|
|
|
// see http://www.php.net/streamwrapper.stream-metadata and https://bugs.php.net/64246
|
|
|
|
// and https://github.com/php/php-src/blob/master/main/php_streams.h#L592
|
|
|
|
switch ($option) {
|
|
|
|
case 1: // PHP_STREAM_META_TOUCH
|
|
|
|
return $this->sftp->touch($path, $var[0], $var[1]);
|
|
|
|
case 2: // PHP_STREAM_OWNER_NAME
|
|
|
|
case 3: // PHP_STREAM_GROUP_NAME
|
|
|
|
return false;
|
|
|
|
case 4: // PHP_STREAM_META_OWNER
|
|
|
|
return $this->sftp->chown($path, $var);
|
|
|
|
case 5: // PHP_STREAM_META_GROUP
|
|
|
|
return $this->sftp->chgrp($path, $var);
|
|
|
|
case 6: // PHP_STREAM_META_ACCESS
|
|
|
|
return $this->sftp->chmod($path, $var) !== false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the underlaying resource
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param int $cast_as
|
|
|
|
* @return resource
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _stream_cast($cast_as)
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
|
|
|
return $this->sftp->fsock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Advisory file locking
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param int $operation
|
|
|
|
* @return bool
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _stream_lock($operation)
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renames a file or directory
|
|
|
|
*
|
|
|
|
* Attempts to rename oldname to newname, moving it between directories if necessary.
|
2014-12-10 02:31:41 +01:00
|
|
|
* If newname exists, it will be overwritten. This is a departure from what \phpseclib\Net\SFTP
|
2013-03-13 02:57:40 +01:00
|
|
|
* does.
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $path_from
|
|
|
|
* @param string $path_to
|
|
|
|
* @return bool
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _rename($path_from, $path_to)
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
|
|
|
$path1 = parse_url($path_from);
|
|
|
|
$path2 = parse_url($path_to);
|
|
|
|
unset($path1['path'], $path2['path']);
|
|
|
|
if ($path1 != $path2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-08 02:51:56 +01:00
|
|
|
$path_from = $this->parse_path($path_from);
|
2013-03-13 02:57:40 +01:00
|
|
|
$path_to = parse_url($path_to);
|
2015-05-07 07:26:39 +02:00
|
|
|
if ($path_from === false) {
|
2013-03-13 02:57:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$path_to = $path_to['path']; // the $component part of parse_url() was added in PHP 5.1.2
|
|
|
|
// "It is an error if there already exists a file with the name specified by newpath."
|
|
|
|
// -- http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02#section-6.5
|
|
|
|
if (!$this->sftp->rename($path_from, $path_to)) {
|
|
|
|
if ($this->sftp->stat($path_to)) {
|
|
|
|
return $this->sftp->delete($path_to, true) && $this->sftp->rename($path_from, $path_to);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open directory handle
|
|
|
|
*
|
|
|
|
* The only $options is "whether or not to enforce safe_mode (0x04)". Since safe mode was deprecated in 5.3 and
|
2014-05-29 23:23:28 +02:00
|
|
|
* removed in 5.4 I'm just going to ignore it.
|
|
|
|
*
|
|
|
|
* Also, nlist() is the best that this function is realistically going to be able to do. When an SFTP client
|
2014-05-29 23:49:04 +02:00
|
|
|
* sends a SSH_FXP_READDIR packet you don't generally get info on just one file but on multiple files. Quoting
|
2014-05-29 23:23:28 +02:00
|
|
|
* the SFTP specs:
|
|
|
|
*
|
|
|
|
* The SSH_FXP_NAME response has the following format:
|
|
|
|
*
|
|
|
|
* uint32 id
|
|
|
|
* uint32 count
|
|
|
|
* repeats count times:
|
|
|
|
* string filename
|
|
|
|
* string longname
|
|
|
|
* ATTRS attrs
|
2013-03-13 02:57:40 +01:00
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $path
|
|
|
|
* @param int $options
|
|
|
|
* @return bool
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _dir_opendir($path, $options)
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
2017-01-08 02:51:56 +01:00
|
|
|
$path = $this->parse_path($path);
|
2013-03-13 02:57:40 +01:00
|
|
|
if ($path === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->pos = 0;
|
|
|
|
$this->entries = $this->sftp->nlist($path);
|
|
|
|
return $this->entries !== false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read entry from directory handle
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return mixed
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _dir_readdir()
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
|
|
|
if (isset($this->entries[$this->pos])) {
|
|
|
|
return $this->entries[$this->pos++];
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rewind directory handle
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return bool
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _dir_rewinddir()
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
|
|
|
$this->pos = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close directory handle
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return bool
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _dir_closedir()
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a directory
|
|
|
|
*
|
|
|
|
* Only valid $options is STREAM_MKDIR_RECURSIVE
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $path
|
|
|
|
* @param int $mode
|
|
|
|
* @param int $options
|
|
|
|
* @return bool
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _mkdir($path, $mode, $options)
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
2017-01-08 02:51:56 +01:00
|
|
|
$path = $this->parse_path($path);
|
2013-03-13 02:57:40 +01:00
|
|
|
if ($path === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->sftp->mkdir($path, $mode, $options & STREAM_MKDIR_RECURSIVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a directory
|
|
|
|
*
|
|
|
|
* Only valid $options is STREAM_MKDIR_RECURSIVE per <http://php.net/streamwrapper.rmdir>, however,
|
|
|
|
* <http://php.net/rmdir> does not have a $recursive parameter as mkdir() does so I don't know how
|
|
|
|
* STREAM_MKDIR_RECURSIVE is supposed to be set. Also, when I try it out with rmdir() I get 8 as
|
|
|
|
* $options. What does 8 correspond to?
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $path
|
|
|
|
* @param int $options
|
|
|
|
* @return bool
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _rmdir($path, $options)
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
2017-01-08 02:51:56 +01:00
|
|
|
$path = $this->parse_path($path);
|
2013-03-13 02:57:40 +01:00
|
|
|
if ($path === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->sftp->rmdir($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flushes the output
|
|
|
|
*
|
2014-12-10 02:31:41 +01:00
|
|
|
* See <http://php.net/fflush>. Always returns true because \phpseclib\Net\SFTP doesn't cache stuff before writing
|
2013-03-13 02:57:40 +01:00
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return bool
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _stream_flush()
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve information about a file resource
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return mixed
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _stream_stat()
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
|
|
|
$results = $this->sftp->stat($this->path);
|
|
|
|
if ($results === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a file
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $path
|
|
|
|
* @return bool
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _unlink($path)
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
2017-01-08 02:51:56 +01:00
|
|
|
$path = $this->parse_path($path);
|
2013-03-13 02:57:40 +01:00
|
|
|
if ($path === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->sftp->delete($path, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve information about a file
|
|
|
|
*
|
2014-12-10 02:31:41 +01:00
|
|
|
* Ignores the STREAM_URL_STAT_QUIET flag because the entirety of \phpseclib\Net\SFTP\Stream is quiet by default
|
2013-03-13 02:57:40 +01:00
|
|
|
* might be worthwhile to reconstruct bits 12-16 (ie. the file type) if mode doesn't have them but we'll
|
|
|
|
* cross that bridge when and if it's reached
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $path
|
|
|
|
* @param int $flags
|
|
|
|
* @return mixed
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _url_stat($path, $flags)
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
2017-01-08 02:51:56 +01:00
|
|
|
$path = $this->parse_path($path);
|
2013-03-13 02:57:40 +01:00
|
|
|
if ($path === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$results = $flags & STREAM_URL_STAT_LINK ? $this->sftp->lstat($path) : $this->sftp->stat($path);
|
|
|
|
if ($results === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Truncate stream
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param int $new_size
|
|
|
|
* @return bool
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _stream_truncate($new_size)
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
|
|
|
if (!$this->sftp->truncate($this->path, $new_size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->eof = false;
|
|
|
|
$this->size = $new_size;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change stream options
|
|
|
|
*
|
|
|
|
* STREAM_OPTION_WRITE_BUFFER isn't supported for the same reason stream_flush isn't.
|
2014-12-10 02:31:41 +01:00
|
|
|
* The other two aren't supported because of limitations in \phpseclib\Net\SFTP.
|
2013-03-13 02:57:40 +01:00
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param int $option
|
|
|
|
* @param int $arg1
|
|
|
|
* @param int $arg2
|
|
|
|
* @return bool
|
2013-03-13 02:57:40 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _stream_set_option($option, $arg1, $arg2)
|
2013-03-13 02:57:40 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-16 06:58:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Close an resource
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
private function _stream_close()
|
2013-04-16 06:58:13 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __call Magic Method
|
|
|
|
*
|
|
|
|
* When you're utilizing an SFTP stream you're not calling the methods in this class directly - PHP is calling them for you.
|
|
|
|
* Which kinda begs the question... what methods is PHP calling and what parameters is it passing to them? This function
|
|
|
|
* lets you figure that out.
|
|
|
|
*
|
|
|
|
* If NET_SFTP_STREAM_LOGGING is defined all calls will be output on the screen and then (regardless of whether or not
|
|
|
|
* NET_SFTP_STREAM_LOGGING is enabled) the parameters will be passed through to the appropriate method.
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string
|
|
|
|
* @param array
|
|
|
|
* @return mixed
|
2013-04-16 06:58:13 +02:00
|
|
|
* @access public
|
|
|
|
*/
|
2017-01-08 02:51:56 +01:00
|
|
|
public function __call($name, $arguments)
|
2013-04-16 06:58:13 +02:00
|
|
|
{
|
|
|
|
if (defined('NET_SFTP_STREAM_LOGGING')) {
|
|
|
|
echo $name . '(';
|
|
|
|
$last = count($arguments) - 1;
|
|
|
|
foreach ($arguments as $i => $argument) {
|
|
|
|
var_export($argument);
|
|
|
|
if ($i != $last) {
|
|
|
|
echo ',';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
echo ")\r\n";
|
|
|
|
}
|
|
|
|
$name = '_' . $name;
|
|
|
|
if (!method_exists($this, $name)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-30 22:19:23 +01:00
|
|
|
return call_user_func_array([$this, $name], $arguments);
|
2013-04-16 06:58:13 +02:00
|
|
|
}
|
2013-03-13 02:57:40 +01:00
|
|
|
}
|