*
* PHP version 5
*
- * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
* @category Net
- * @package Net_SFTP_Stream
+ * @package SFTP
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2013 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
*/
+namespace phpseclib\Net\SFTP;
+
+use phpseclib\Crypt\RSA;
+use phpseclib\Net\SFTP;
+
/**
* SFTP Stream Wrapper
*
- * @package Net_SFTP_Stream
+ * @package SFTP
* @author Jim Wigginton <terrafrost@php.net>
* @access public
*/
-class Net_SFTP_Stream
+class Stream
{
/**
* SFTP instances
*
* Rather than re-create the connection we re-use instances if possible
*
- * @var Array
+ * @var array
*/
static $instances;
/**
* SFTP instance
*
- * @var Object
+ * @var object
* @access private
*/
var $sftp;
/**
* Path
*
- * @var String
+ * @var string
* @access private
*/
var $path;
/**
* Mode
*
- * @var String
+ * @var string
* @access private
*/
var $mode;
/**
* Position
*
- * @var Integer
+ * @var int
* @access private
*/
var $pos;
/**
* Size
*
- * @var Integer
+ * @var int
* @access private
*/
var $size;
/**
* Directory entries
*
- * @var Array
+ * @var array
* @access private
*/
var $entries;
/**
* EOF flag
*
- * @var Boolean
+ * @var bool
* @access private
*/
var $eof;
*
* Technically this needs to be publically accessible so PHP can set it directly
*
- * @var Resource
+ * @var resource
* @access public
*/
var $context;
/**
* Notification callback function
*
- * @var Callable
+ * @var callable
* @access public
*/
var $notification;
/**
* Registers this class as a URL wrapper.
*
- * @param optional String $protocol The wrapper name to be registered.
- * @return Boolean True on success, false otherwise.
+ * @param string $protocol The wrapper name to be registered.
+ * @return bool True on success, false otherwise.
* @access public
*/
static function register($protocol = 'sftp')
if (in_array($protocol, stream_get_wrappers(), true)) {
return false;
}
- $class = function_exists('get_called_class') ? get_called_class() : __CLASS__;
- return stream_wrapper_register($protocol, $class);
+ return stream_wrapper_register($protocol, get_called_class());
}
/**
*
* @access public
*/
- function Net_SFTP_Stream()
+ function __construct()
{
if (defined('NET_SFTP_STREAM_LOGGING')) {
echo "__construct()\r\n";
}
-
- if (!class_exists('Net_SFTP')) {
- include_once 'Net/SFTP.php';
- }
}
/**
* 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.
*
- * @param String $path
- * @return String
+ * @param string $path
+ * @return string
* @access private
*/
function _parse_path($path)
{
+ $orig = $path;
extract(parse_url($path) + array('port' => 22));
+ if (isset($query)) {
+ $path.= '?' . $query;
+ } elseif (preg_match('/(\?|\?#)$/', $orig)) {
+ $path.= '?';
+ }
+ if (isset($fragment)) {
+ $path.= '#' . $fragment;
+ } elseif ($orig[strlen($orig) - 1] == '#') {
+ $path.= '#';
+ }
if (!isset($host)) {
return false;
if ($host[0] == '$') {
$host = substr($host, 1);
global $$host;
- if (!is_object($$host) || get_class($$host) != 'Net_SFTP') {
+ if (($$host instanceof SFTP) === false) {
return false;
}
$this->sftp = $$host;
if (isset($context[$scheme]['sftp'])) {
$sftp = $context[$scheme]['sftp'];
}
- if (isset($sftp) && is_object($sftp) && get_class($sftp) == 'Net_SFTP') {
+ if (isset($sftp) && $sftp instanceof SFTP) {
$this->sftp = $sftp;
return $path;
}
if (isset($context[$scheme]['password'])) {
$pass = $context[$scheme]['password'];
}
- if (isset($context[$scheme]['privkey']) && is_object($context[$scheme]['privkey']) && get_Class($context[$scheme]['privkey']) == 'Crypt_RSA') {
+ if (isset($context[$scheme]['privkey']) && $context[$scheme]['privkey'] instanceof RSA) {
$pass = $context[$scheme]['privkey'];
}
return false;
}
- // casting $pass to a string is necessary in the event that it's a Crypt_RSA object
+ // casting $pass to a string is necessary in the event that it's a \phpseclib\Crypt\RSA object
if (isset(self::$instances[$host][$port][$user][(string) $pass])) {
$this->sftp = self::$instances[$host][$port][$user][(string) $pass];
} else {
- $this->sftp = new Net_SFTP($host, $port);
+ $this->sftp = new SFTP($host, $port);
$this->sftp->disableStatCache();
if (isset($this->notification) && is_callable($this->notification)) {
/* if !is_callable($this->notification) we could do this:
/**
* Opens file or URL
*
- * @param String $path
- * @param String $mode
- * @param Integer $options
- * @param String $opened_path
- * @return Boolean
+ * @param string $path
+ * @param string $mode
+ * @param int $options
+ * @param string $opened_path
+ * @return bool
* @access public
*/
function _stream_open($path, $mode, $options, &$opened_path)
if ($this->size === false) {
if ($this->mode[0] == 'r') {
return false;
+ } else {
+ $this->sftp->touch($path);
+ $this->size = 0;
}
} else {
switch ($this->mode[0]) {
case 'x':
return false;
case 'w':
- case 'c':
$this->sftp->truncate($path, 0);
+ $this->size = 0;
}
}
/**
* Read from stream
*
- * @param Integer $count
- * @return Mixed
+ * @param int $count
+ * @return mixed
* @access public
*/
function _stream_read($count)
/**
* Write to stream
*
- * @param String $data
- * @return Mixed
+ * @param string $data
+ * @return mixed
* @access public
*/
function _stream_write($data)
return false;
}
- $result = $this->sftp->put($this->path, $data, NET_SFTP_STRING, $this->pos);
+ $result = $this->sftp->put($this->path, $data, SFTP::SOURCE_STRING, $this->pos);
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);
/**
* Retrieve the current position of a stream
*
- * @return Integer
+ * @return int
* @access public
*/
function _stream_tell()
* 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.
*
- * @return Boolean
+ * @return bool
* @access public
*/
function _stream_eof()
/**
* Seeks to specific location in a stream
*
- * @param Integer $offset
- * @param Integer $whence
- * @return Boolean
+ * @param int $offset
+ * @param int $whence
+ * @return bool
* @access public
*/
function _stream_seek($offset, $whence)
/**
* Change stream options
*
- * @param String $path
- * @param Integer $option
- * @param Mixed $var
- * @return Boolean
+ * @param string $path
+ * @param int $option
+ * @param mixed $var
+ * @return bool
* @access public
*/
function _stream_metadata($path, $option, $var)
/**
* Retrieve the underlaying resource
*
- * @param Integer $cast_as
- * @return Resource
+ * @param int $cast_as
+ * @return resource
* @access public
*/
function _stream_cast($cast_as)
/**
* Advisory file locking
*
- * @param Integer $operation
- * @return Boolean
+ * @param int $operation
+ * @return bool
* @access public
*/
function _stream_lock($operation)
* Renames a file or directory
*
* Attempts to rename oldname to newname, moving it between directories if necessary.
- * If newname exists, it will be overwritten. This is a departure from what Net_SFTP
+ * If newname exists, it will be overwritten. This is a departure from what \phpseclib\Net\SFTP
* does.
*
- * @param String $path_from
- * @param String $path_to
- * @return Boolean
+ * @param string $path_from
+ * @param string $path_to
+ * @return bool
* @access public
*/
function _rename($path_from, $path_to)
$path_from = $this->_parse_path($path_from);
$path_to = parse_url($path_to);
- if ($path_from == false) {
+ if ($path_from === false) {
return false;
}
* string longname
* ATTRS attrs
*
- * @param String $path
- * @param Integer $options
- * @return Boolean
+ * @param string $path
+ * @param int $options
+ * @return bool
* @access public
*/
function _dir_opendir($path, $options)
/**
* Read entry from directory handle
*
- * @return Mixed
+ * @return mixed
* @access public
*/
function _dir_readdir()
/**
* Rewind directory handle
*
- * @return Boolean
+ * @return bool
* @access public
*/
function _dir_rewinddir()
/**
* Close directory handle
*
- * @return Boolean
+ * @return bool
* @access public
*/
function _dir_closedir()
*
* Only valid $options is STREAM_MKDIR_RECURSIVE
*
- * @param String $path
- * @param Integer $mode
- * @param Integer $options
- * @return Boolean
+ * @param string $path
+ * @param int $mode
+ * @param int $options
+ * @return bool
* @access public
*/
function _mkdir($path, $mode, $options)
* 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?
*
- * @param String $path
- * @param Integer $mode
- * @param Integer $options
- * @return Boolean
+ * @param string $path
+ * @param int $mode
+ * @param int $options
+ * @return bool
* @access public
*/
function _rmdir($path, $options)
/**
* Flushes the output
*
- * See <http://php.net/fflush>. Always returns true because Net_SFTP doesn't cache stuff before writing
+ * See <http://php.net/fflush>. Always returns true because \phpseclib\Net\SFTP doesn't cache stuff before writing
*
- * @return Boolean
+ * @return bool
* @access public
*/
function _stream_flush()
/**
* Retrieve information about a file resource
*
- * @return Mixed
+ * @return mixed
* @access public
*/
function _stream_stat()
/**
* Delete a file
*
- * @param String $path
- * @return Boolean
+ * @param string $path
+ * @return bool
* @access public
*/
function _unlink($path)
/**
* Retrieve information about a file
*
- * Ignores the STREAM_URL_STAT_QUIET flag because the entirety of Net_SFTP_Stream is quiet by default
+ * Ignores the STREAM_URL_STAT_QUIET flag because the entirety of \phpseclib\Net\SFTP\Stream is quiet by default
* 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
*
- * @param String $path
- * @param Integer $flags
- * @return Mixed
+ * @param string $path
+ * @param int $flags
+ * @return mixed
* @access public
*/
function _url_stat($path, $flags)
/**
* Truncate stream
*
- * @param Integer $new_size
- * @return Boolean
+ * @param int $new_size
+ * @return bool
* @access public
*/
function _stream_truncate($new_size)
* Change stream options
*
* STREAM_OPTION_WRITE_BUFFER isn't supported for the same reason stream_flush isn't.
- * The other two aren't supported because of limitations in Net_SFTP.
+ * The other two aren't supported because of limitations in \phpseclib\Net\SFTP.
*
- * @param Integer $option
- * @param Integer $arg1
- * @param Integer $arg2
- * @return Boolean
+ * @param int $option
+ * @param int $arg1
+ * @param int $arg2
+ * @return bool
* @access public
*/
function _stream_set_option($option, $arg1, $arg2)
* 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.
*
- * @param String
- * @param Array
- * @return Mixed
+ * @param string
+ * @param array
+ * @return mixed
* @access public
*/
function __call($name, $arguments)
return call_user_func_array(array($this, $name), $arguments);
}
}
-
-Net_SFTP_Stream::register();