]> git.mxchange.org Git - friendica.git/blobdiff - library/phpsec/Net/SFTP.php
bug #96 move libraries to library - better alignment of like rotator
[friendica.git] / library / phpsec / Net / SFTP.php
diff --git a/library/phpsec/Net/SFTP.php b/library/phpsec/Net/SFTP.php
new file mode 100644 (file)
index 0000000..06812ad
--- /dev/null
@@ -0,0 +1,1461 @@
+<?php\r
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */\r
+\r
+/**\r
+ * Pure-PHP implementation of SFTP.\r
+ *\r
+ * PHP versions 4 and 5\r
+ *\r
+ * Currently only supports SFTPv3, which, according to wikipedia.org, "is the most widely used version,\r
+ * implemented by the popular OpenSSH SFTP server".  If you want SFTPv4/5/6 support, provide me with access\r
+ * to an SFTPv4/5/6 server.\r
+ *\r
+ * The API for this library is modeled after the API from PHP's {@link http://php.net/book.ftp FTP extension}.\r
+ *\r
+ * Here's a short example of how to use this library:\r
+ * <code>\r
+ * <?php\r
+ *    include('Net/SFTP.php');\r
+ *\r
+ *    $sftp = new Net_SFTP('www.domain.tld');\r
+ *    if (!$sftp->login('username', 'password')) {\r
+ *        exit('Login Failed');\r
+ *    }\r
+ *\r
+ *    echo $sftp->pwd() . "\r\n";\r
+ *    $sftp->put('filename.ext', 'hello, world!');\r
+ *    print_r($sftp->nlist());\r
+ * ?>\r
+ * </code>\r
+ *\r
+ * LICENSE: This library is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU Lesser General Public\r
+ * License as published by the Free Software Foundation; either\r
+ * version 2.1 of the License, or (at your option) any later version.\r
+ *\r
+ * This library is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+ * Lesser General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU Lesser General Public\r
+ * License along with this library; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,\r
+ * MA  02111-1307  USA\r
+ *\r
+ * @category   Net\r
+ * @package    Net_SFTP\r
+ * @author     Jim Wigginton <terrafrost@php.net>\r
+ * @copyright  MMIX Jim Wigginton\r
+ * @license    http://www.gnu.org/licenses/lgpl.txt\r
+ * @version    $Id: SFTP.php,v 1.21 2010/04/09 02:31:34 terrafrost Exp $\r
+ * @link       http://phpseclib.sourceforge.net\r
+ */\r
+\r
+/**\r
+ * Include Net_SSH2\r
+ */\r
+require_once('Net/SSH2.php');\r
+\r
+/**#@+\r
+ * @access public\r
+ * @see Net_SFTP::getLog()\r
+ */\r
+/**\r
+ * Returns the message numbers\r
+ */\r
+define('NET_SFTP_LOG_SIMPLE',  NET_SSH2_LOG_SIMPLE);\r
+/**\r
+ * Returns the message content\r
+ */\r
+define('NET_SFTP_LOG_COMPLEX', NET_SSH2_LOG_COMPLEX);\r
+/**#@-*/\r
+\r
+/**\r
+ * SFTP channel constant\r
+ *\r
+ * Net_SSH2::exec() uses 0 and Net_SSH2::interactiveRead() / Net_SSH2::interactiveWrite() use 1.\r
+ *\r
+ * @see Net_SSH2::_send_channel_packet()\r
+ * @see Net_SSH2::_get_channel_packet()\r
+ * @access private\r
+ */\r
+define('NET_SFTP_CHANNEL', 2);\r
+\r
+/**#@+\r
+ * @access public\r
+ * @see Net_SFTP::put()\r
+ */\r
+/**\r
+ * Reads data from a local file.\r
+ */\r
+define('NET_SFTP_LOCAL_FILE', 1);\r
+/**\r
+ * Reads data from a string.\r
+ */\r
+define('NET_SFTP_STRING',  2);\r
+/**#@-*/\r
+\r
+/**\r
+ * Pure-PHP implementations of SFTP.\r
+ *\r
+ * @author  Jim Wigginton <terrafrost@php.net>\r
+ * @version 0.1.0\r
+ * @access  public\r
+ * @package Net_SFTP\r
+ */\r
+class Net_SFTP extends Net_SSH2 {\r
+    /**\r
+     * Packet Types\r
+     *\r
+     * @see Net_SFTP::Net_SFTP()\r
+     * @var Array\r
+     * @access private\r
+     */\r
+    var $packet_types = array();\r
+\r
+    /**\r
+     * Status Codes\r
+     *\r
+     * @see Net_SFTP::Net_SFTP()\r
+     * @var Array\r
+     * @access private\r
+     */\r
+    var $status_codes = array();\r
+\r
+    /**\r
+     * The Request ID\r
+     *\r
+     * The request ID exists in the off chance that a packet is sent out-of-order.  Of course, this library doesn't support\r
+     * concurrent actions, so it's somewhat academic, here.\r
+     *\r
+     * @var Integer\r
+     * @see Net_SFTP::_send_sftp_packet()\r
+     * @access private\r
+     */\r
+    var $request_id = false;\r
+\r
+    /**\r
+     * The Packet Type\r
+     *\r
+     * The request ID exists in the off chance that a packet is sent out-of-order.  Of course, this library doesn't support\r
+     * concurrent actions, so it's somewhat academic, here.\r
+     *\r
+     * @var Integer\r
+     * @see Net_SFTP::_get_sftp_packet()\r
+     * @access private\r
+     */\r
+    var $packet_type = -1;\r
+\r
+    /**\r
+     * Packet Buffer\r
+     *\r
+     * @var String\r
+     * @see Net_SFTP::_get_sftp_packet()\r
+     * @access private\r
+     */\r
+    var $packet_buffer = '';\r
+\r
+    /**\r
+     * Extensions supported by the server\r
+     *\r
+     * @var Array\r
+     * @see Net_SFTP::_initChannel()\r
+     * @access private\r
+     */\r
+    var $extensions = array();\r
+\r
+    /**\r
+     * Server SFTP version\r
+     *\r
+     * @var Integer\r
+     * @see Net_SFTP::_initChannel()\r
+     * @access private\r
+     */\r
+    var $version;\r
+\r
+    /**\r
+     * Current working directory\r
+     *\r
+     * @var String\r
+     * @see Net_SFTP::_realpath()\r
+     * @see Net_SFTP::chdir()\r
+     * @access private\r
+     */\r
+    var $pwd = false;\r
+\r
+    /**\r
+     * Packet Type Log\r
+     *\r
+     * @see Net_SFTP::getLog()\r
+     * @var Array\r
+     * @access private\r
+     */\r
+    var $packet_type_log = array();\r
+\r
+    /**\r
+     * Packet Log\r
+     *\r
+     * @see Net_SFTP::getLog()\r
+     * @var Array\r
+     * @access private\r
+     */\r
+    var $packet_log = array();\r
+\r
+    /**\r
+     * Error information\r
+     *\r
+     * @see Net_SFTP::getSFTPErrors()\r
+     * @see Net_SFTP::getLastSFTPError()\r
+     * @var String\r
+     * @access private\r
+     */\r
+    var $errors = array();\r
+\r
+    /**\r
+     * Default Constructor.\r
+     *\r
+     * Connects to an SFTP server\r
+     *\r
+     * @param String $host\r
+     * @param optional Integer $port\r
+     * @param optional Integer $timeout\r
+     * @return Net_SFTP\r
+     * @access public\r
+     */\r
+    function Net_SFTP($host, $port = 22, $timeout = 10)\r
+    {\r
+        parent::Net_SSH2($host, $port, $timeout);\r
+        $this->packet_types = array(\r
+            1  => 'NET_SFTP_INIT',\r
+            2  => 'NET_SFTP_VERSION',\r
+            /* the format of SSH_FXP_OPEN changed between SFTPv4 and SFTPv5+:\r
+                   SFTPv5+: http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.1.1\r
+               pre-SFTPv5 : http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-6.3 */\r
+            3  => 'NET_SFTP_OPEN',\r
+            4  => 'NET_SFTP_CLOSE',\r
+            5  => 'NET_SFTP_READ',\r
+            6  => 'NET_SFTP_WRITE',\r
+            8  => 'NET_SFTP_FSTAT',\r
+            9  => 'NET_SFTP_SETSTAT',\r
+            11 => 'NET_SFTP_OPENDIR',\r
+            12 => 'NET_SFTP_READDIR',\r
+            13 => 'NET_SFTP_REMOVE',\r
+            14 => 'NET_SFTP_MKDIR',\r
+            15 => 'NET_SFTP_RMDIR',\r
+            16 => 'NET_SFTP_REALPATH',\r
+            17 => 'NET_SFTP_STAT',\r
+            /* the format of SSH_FXP_RENAME changed between SFTPv4 and SFTPv5+:\r
+                   SFTPv5+: http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.3\r
+               pre-SFTPv5 : http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-6.5 */\r
+            18 => 'NET_SFTP_RENAME',\r
+\r
+            101=> 'NET_SFTP_STATUS',\r
+            102=> 'NET_SFTP_HANDLE',\r
+            /* the format of SSH_FXP_NAME changed between SFTPv3 and SFTPv4+:\r
+                   SFTPv4+: http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-9.4\r
+               pre-SFTPv4 : http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02#section-7 */\r
+            103=> 'NET_SFTP_DATA',\r
+            104=> 'NET_SFTP_NAME',\r
+            105=> 'NET_SFTP_ATTRS',\r
+\r
+            200=> 'NET_SFTP_EXTENDED'\r
+        );\r
+        $this->status_codes = array(\r
+            0 => 'NET_SFTP_STATUS_OK',\r
+            1 => 'NET_SFTP_STATUS_EOF',\r
+            2 => 'NET_SFTP_STATUS_NO_SUCH_FILE',\r
+            3 => 'NET_SFTP_STATUS_PERMISSION_DENIED',\r
+            4 => 'NET_SFTP_STATUS_FAILURE',\r
+            5 => 'NET_SFTP_STATUS_BAD_MESSAGE',\r
+            6 => 'NET_SFTP_STATUS_NO_CONNECTION',\r
+            7 => 'NET_SFTP_STATUS_CONNECTION_LOST',\r
+            8 => 'NET_SFTP_STATUS_OP_UNSUPPORTED'\r
+        );\r
+        // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-7.1\r
+        // the order, in this case, matters quite a lot - see Net_SFTP::_parseAttributes() to understand why\r
+        $this->attributes = array(\r
+            0x00000001 => 'NET_SFTP_ATTR_SIZE',\r
+            0x00000002 => 'NET_SFTP_ATTR_UIDGID', // defined in SFTPv3, removed in SFTPv4+\r
+            0x00000004 => 'NET_SFTP_ATTR_PERMISSIONS',\r
+            0x00000008 => 'NET_SFTP_ATTR_ACCESSTIME',\r
+                    -1 => 'NET_SFTP_ATTR_EXTENDED' // unpack('N', "\xFF\xFF\xFF\xFF") == array(1 => int(-1))\r
+        );\r
+        // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-6.3\r
+        // the flag definitions change somewhat in SFTPv5+.  if SFTPv5+ support is added to this library, maybe name\r
+        // the array for that $this->open5_flags and similarily alter the constant names.\r
+        $this->open_flags = array(\r
+            0x00000001 => 'NET_SFTP_OPEN_READ',\r
+            0x00000002 => 'NET_SFTP_OPEN_WRITE',\r
+            0x00000008 => 'NET_SFTP_OPEN_CREATE',\r
+            0x00000010 => 'NET_SFTP_OPEN_TRUNCATE'\r
+        );\r
+        $this->_define_array(\r
+            $this->packet_types,\r
+            $this->status_codes,\r
+            $this->attributes,\r
+            $this->open_flags\r
+        );\r
+    }\r
+\r
+    /**\r
+     * Login\r
+     *\r
+     * @param String $username\r
+     * @param optional String $password\r
+     * @return Boolean\r
+     * @access public\r
+     */\r
+    function login($username, $password = '')\r
+    {\r
+        if (!parent::login($username, $password)) {\r
+            return false;\r
+        }\r
+\r
+        $this->window_size_client_to_server[NET_SFTP_CHANNEL] = $this->window_size;\r
+\r
+        $packet = pack('CNa*N3',\r
+            NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', NET_SFTP_CHANNEL, $this->window_size, 0x4000);\r
+\r
+        if (!$this->_send_binary_packet($packet)) {\r
+            return false;\r
+        }\r
+\r
+        $this->channel_status[NET_SFTP_CHANNEL] = NET_SSH2_MSG_CHANNEL_OPEN;\r
+\r
+        $response = $this->_get_channel_packet(NET_SFTP_CHANNEL);\r
+        if ($response === false) {\r
+            return false;\r
+        }\r
+\r
+        $packet = pack('CNNa*CNa*',\r
+            NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SFTP_CHANNEL], strlen('subsystem'), 'subsystem', 1, strlen('sftp'), 'sftp');\r
+        if (!$this->_send_binary_packet($packet)) {\r
+            return false;\r
+        }\r
+\r
+        $this->channel_status[NET_SFTP_CHANNEL] = NET_SSH2_MSG_CHANNEL_REQUEST;\r
+\r
+        $response = $this->_get_channel_packet(NET_SFTP_CHANNEL);\r
+        if ($response === false) {\r
+            return false;\r
+        }\r
+\r
+        $this->channel_status[NET_SFTP_CHANNEL] = NET_SSH2_MSG_CHANNEL_DATA;\r
+\r
+        if (!$this->_send_sftp_packet(NET_SFTP_INIT, "\0\0\0\3")) {\r
+            return false;\r
+        }\r
+\r
+        $response = $this->_get_sftp_packet();\r
+        if ($this->packet_type != NET_SFTP_VERSION) {\r
+            user_error('Expected SSH_FXP_VERSION', E_USER_NOTICE);\r
+            return false;\r
+        }\r
+\r
+        extract(unpack('Nversion', $this->_string_shift($response, 4)));\r
+        $this->version = $version;\r
+        while (!empty($response)) {\r
+            extract(unpack('Nlength', $this->_string_shift($response, 4)));\r
+            $key = $this->_string_shift($response, $length);\r
+            extract(unpack('Nlength', $this->_string_shift($response, 4)));\r
+            $value = $this->_string_shift($response, $length);\r
+            $this->extensions[$key] = $value;\r
+        }\r
+\r
+        /*\r
+         SFTPv4+ defines a 'newline' extension.  SFTPv3 seems to have unofficial support for it via 'newline@vandyke.com',\r
+         however, I'm not sure what 'newline@vandyke.com' is supposed to do (the fact that it's unofficial means that it's\r
+         not in the official SFTPv3 specs) and 'newline@vandyke.com' / 'newline' are likely not drop-in substitutes for\r
+         one another due to the fact that 'newline' comes with a SSH_FXF_TEXT bitmask whereas it seems unlikely that\r
+         'newline@vandyke.com' would.\r
+        */\r
+        /*\r
+        if (isset($this->extensions['newline@vandyke.com'])) {\r
+            $this->extensions['newline'] = $this->extensions['newline@vandyke.com'];\r
+            unset($this->extensions['newline@vandyke.com']);\r
+        }\r
+        */\r
+\r
+        $this->request_id = 1;\r
+\r
+        /*\r
+         A Note on SFTPv4/5/6 support:\r
+         <http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-5.1> states the following:\r
+\r
+         "If the client wishes to interoperate with servers that support noncontiguous version\r
+          numbers it SHOULD send '3'"\r
+\r
+         Given that the server only sends its version number after the client has already done so, the above\r
+         seems to be suggesting that v3 should be the default version.  This makes sense given that v3 is the\r
+         most popular.\r
+\r
+         <http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-5.5> states the following;\r
+\r
+         "If the server did not send the "versions" extension, or the version-from-list was not included, the\r
+          server MAY send a status response describing the failure, but MUST then close the channel without\r
+          processing any further requests."\r
+\r
+         So what do you do if you have a client whose initial SSH_FXP_INIT packet says it implements v3 and\r
+         a server whose initial SSH_FXP_VERSION reply says it implements v4 and only v4?  If it only implements\r
+         v4, the "versions" extension is likely not going to have been sent so version re-negotiation as discussed\r
+         in draft-ietf-secsh-filexfer-13 would be quite impossible.  As such, what Net_SFTP would do is close the\r
+         channel and reopen it with a new and updated SSH_FXP_INIT packet.\r
+        */\r
+        if ($this->version != 3) {\r
+            return false;\r
+        }\r
+\r
+        $this->pwd = $this->_realpath('.');\r
+\r
+        return true;\r
+    }\r
+\r
+    /**\r
+     * Returns the current directory name\r
+     *\r
+     * @return Mixed\r
+     * @access public\r
+     */\r
+    function pwd()\r
+    {\r
+        return $this->pwd;\r
+    }\r
+\r
+    /**\r
+     * Canonicalize the Server-Side Path Name\r
+     *\r
+     * SFTP doesn't provide a mechanism by which the current working directory can be changed, so we'll emulate it.  Returns\r
+     * the absolute (canonicalized) path.  If $mode is set to NET_SFTP_CONFIRM_DIR (as opposed to NET_SFTP_CONFIRM_NONE,\r
+     * which is what it is set to by default), false is returned if $dir is not a valid directory.\r
+     *\r
+     * @see Net_SFTP::chdir()\r
+     * @param String $dir\r
+     * @param optional Integer $mode\r
+     * @return Mixed\r
+     * @access private\r
+     */\r
+    function _realpath($dir)\r
+    {\r
+        /*\r
+        "This protocol represents file names as strings.  File names are\r
+         assumed to use the slash ('/') character as a directory separator.\r
+\r
+         File names starting with a slash are "absolute", and are relative to\r
+         the root of the file system.  Names starting with any other character\r
+         are relative to the user's default directory (home directory).  Note\r
+         that identifying the user is assumed to take place outside of this\r
+         protocol."\r
+\r
+         -- http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-6\r
+        */\r
+        $file = '';\r
+        if ($this->pwd !== false) {\r
+            // if the SFTP server returned the canonicalized path even for non-existant files this wouldn't be necessary\r
+            // on OpenSSH it isn't necessary but on other SFTP servers it is.  that and since the specs say nothing on\r
+            // the subject, we'll go ahead and work around it with the following.\r
+            if ($dir[strlen($dir) - 1] != '/') {\r
+                $file = basename($dir);\r
+                $dir = dirname($dir);\r
+            }\r
+\r
+            if ($dir == '.' || $dir == $this->pwd) {\r
+                return $this->pwd . $file;\r
+            }\r
+\r
+            if ($dir[0] != '/') {\r
+                $dir = $this->pwd . '/' . $dir;\r
+            }\r
+            // on the surface it seems like maybe resolving a path beginning with / is unnecessary, but such paths\r
+            // can contain .'s and ..'s just like any other.  we could parse those out as appropriate or we can let\r
+            // the server do it.  we'll do the latter.\r
+        }\r
+\r
+        /*\r
+         that SSH_FXP_REALPATH returns SSH_FXP_NAME does not necessarily mean that anything actually exists at the\r
+         specified path.  generally speaking, no attributes are returned with this particular SSH_FXP_NAME packet\r
+         regardless of whether or not a file actually exists.  and in SFTPv3, the longname field and the filename\r
+         field match for this particular SSH_FXP_NAME packet.  for other SSH_FXP_NAME packets, this will likely\r
+         not be the case, but for this one, it is.\r
+        */\r
+        // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.9\r
+        if (!$this->_send_sftp_packet(NET_SFTP_REALPATH, pack('Na*', strlen($dir), $dir))) {\r
+            return false;\r
+        }\r
+\r
+        $response = $this->_get_sftp_packet();\r
+        switch ($this->packet_type) {\r
+            case NET_SFTP_NAME:\r
+                // although SSH_FXP_NAME is implemented differently in SFTPv3 than it is in SFTPv4+, the following\r
+                // should work on all SFTP versions since the only part of the SSH_FXP_NAME packet the following looks\r
+                // at is the first part and that part is defined the same in SFTP versions 3 through 6.\r
+                $this->_string_shift($response, 4); // skip over the count - it should be 1, anyway\r
+                extract(unpack('Nlength', $this->_string_shift($response, 4)));\r
+                $realpath = $this->_string_shift($response, $length);\r
+                break;\r
+            case NET_SFTP_STATUS:\r
+                extract(unpack('Nstatus/Nlength', $this->_string_shift($response, 8)));\r
+                $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+                return false;\r
+            default:\r
+                user_error('Expected SSH_FXP_NAME or SSH_FXP_STATUS', E_USER_NOTICE);\r
+                return false;\r
+        }\r
+\r
+        // if $this->pwd isn't set than the only thing $realpath could be is for '.', which is pretty much guaranteed to\r
+        // be a bonafide directory\r
+        return $realpath . '/' . $file;\r
+    }\r
+\r
+    /**\r
+     * Changes the current directory\r
+     *\r
+     * @param String $dir\r
+     * @return Boolean\r
+     * @access public\r
+     */\r
+    function chdir($dir)\r
+    {\r
+        if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {\r
+            return false;\r
+        }\r
+\r
+        if ($dir[strlen($dir) - 1] != '/') {\r
+            $dir.= '/';\r
+        }\r
+        $dir = $this->_realpath($dir);\r
+\r
+        // confirm that $dir is, in fact, a valid directory\r
+        if (!$this->_send_sftp_packet(NET_SFTP_OPENDIR, pack('Na*', strlen($dir), $dir))) {\r
+            return false;\r
+        }\r
+\r
+        // see Net_SFTP::nlist() for a more thorough explanation of the following\r
+        $response = $this->_get_sftp_packet();\r
+        switch ($this->packet_type) {\r
+            case NET_SFTP_HANDLE:\r
+                $handle = substr($response, 4);\r
+                break;\r
+            case NET_SFTP_STATUS:\r
+                extract(unpack('Nstatus/Nlength', $this->_string_shift($response, 8)));\r
+                $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+                return false;\r
+            default:\r
+                user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS', E_USER_NOTICE);\r
+                return false;\r
+        }\r
+\r
+        if (!$this->_send_sftp_packet(NET_SFTP_CLOSE, pack('Na*', strlen($handle), $handle))) {\r
+            return false;\r
+        }\r
+\r
+        $response = $this->_get_sftp_packet();\r
+        if ($this->packet_type != NET_SFTP_STATUS) {\r
+            user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);\r
+            return false;\r
+        }\r
+\r
+        extract(unpack('Nstatus', $this->_string_shift($response, 4)));\r
+        if ($status != NET_SFTP_STATUS_OK) {\r
+            extract(unpack('Nlength', $this->_string_shift($response, 4)));\r
+            $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+            return false;\r
+        }\r
+\r
+        $this->pwd = $dir;\r
+        return true;\r
+    }\r
+\r
+    /**\r
+     * Returns a list of files in the given directory\r
+     *\r
+     * @param optional String $dir\r
+     * @return Mixed\r
+     * @access public\r
+     */\r
+    function nlist($dir = '.')\r
+    {\r
+        return $this->_list($dir, false);\r
+    }\r
+\r
+    /**\r
+     * Returns a list of files in the given directory\r
+     *\r
+     * @param optional String $dir\r
+     * @return Mixed\r
+     * @access public\r
+     */\r
+    function rawlist($dir = '.')\r
+    {\r
+        return $this->_list($dir, true);\r
+    }\r
+\r
+    function _list($dir, $raw = true)\r
+    {\r
+        if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {\r
+            return false;\r
+        }\r
+\r
+        $dir = $this->_realpath($dir);\r
+        if ($dir === false) {\r
+            return false;\r
+        }\r
+\r
+        // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.1.2\r
+        if (!$this->_send_sftp_packet(NET_SFTP_OPENDIR, pack('Na*', strlen($dir), $dir))) {\r
+            return false;\r
+        }\r
+\r
+        $response = $this->_get_sftp_packet();\r
+        switch ($this->packet_type) {\r
+            case NET_SFTP_HANDLE:\r
+                // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-9.2\r
+                // since 'handle' is the last field in the SSH_FXP_HANDLE packet, we'll just remove the first four bytes that\r
+                // represent the length of the string and leave it at that\r
+                $handle = substr($response, 4);\r
+                break;\r
+            case NET_SFTP_STATUS:\r
+                // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED\r
+                extract(unpack('Nstatus/Nlength', $this->_string_shift($response, 8)));\r
+                $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+                return false;\r
+            default:\r
+                user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS', E_USER_NOTICE);\r
+                return false;\r
+        }\r
+\r
+        $contents = array();\r
+        while (true) {\r
+            // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.2.2\r
+            // why multiple SSH_FXP_READDIR packets would be sent when the response to a single one can span arbitrarily many\r
+            // SSH_MSG_CHANNEL_DATA messages is not known to me.\r
+            if (!$this->_send_sftp_packet(NET_SFTP_READDIR, pack('Na*', strlen($handle), $handle))) {\r
+                return false;\r
+            }\r
+\r
+            $response = $this->_get_sftp_packet();\r
+            switch ($this->packet_type) {\r
+                case NET_SFTP_NAME:\r
+                    extract(unpack('Ncount', $this->_string_shift($response, 4)));\r
+                    for ($i = 0; $i < $count; $i++) {\r
+                        extract(unpack('Nlength', $this->_string_shift($response, 4)));\r
+                        $shortname = $this->_string_shift($response, $length);\r
+                        extract(unpack('Nlength', $this->_string_shift($response, 4)));\r
+                        $this->_string_shift($response, $length); // SFTPv4+ drop this field - the "longname" field\r
+                        $attributes = $this->_parseAttributes($response); // we also don't care about the attributes\r
+                        if (!$raw) {\r
+                            $contents[] = $shortname;\r
+                        } else {\r
+                            $contents[$shortname] = $attributes;\r
+                        }\r
+                        // SFTPv6 has an optional boolean end-of-list field, but we'll ignore that, since the\r
+                        // final SSH_FXP_STATUS packet should tell us that, already.\r
+                    }\r
+                    break;\r
+                case NET_SFTP_STATUS:\r
+                    extract(unpack('Nstatus', $this->_string_shift($response, 4)));\r
+                    if ($status != NET_SFTP_STATUS_EOF) {\r
+                        extract(unpack('Nlength', $this->_string_shift($response, 4)));\r
+                        $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+                        return false;\r
+                    }\r
+                    break 2;\r
+                default:\r
+                    user_error('Expected SSH_FXP_NAME or SSH_FXP_STATUS', E_USER_NOTICE);\r
+                    return false;\r
+            }\r
+        }\r
+\r
+        if (!$this->_send_sftp_packet(NET_SFTP_CLOSE, pack('Na*', strlen($handle), $handle))) {\r
+            return false;\r
+        }\r
+\r
+        // "The client MUST release all resources associated with the handle regardless of the status."\r
+        //  -- http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.1.3\r
+        $response = $this->_get_sftp_packet();\r
+        if ($this->packet_type != NET_SFTP_STATUS) {\r
+            user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);\r
+            return false;\r
+        }\r
+\r
+        extract(unpack('Nstatus', $this->_string_shift($response, 4)));\r
+        if ($status != NET_SFTP_STATUS_OK) {\r
+            extract(unpack('Nlength', $this->_string_shift($response, 4)));\r
+            $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+            return false;\r
+        }\r
+\r
+        return $contents;\r
+    }\r
+\r
+    /**\r
+     * Returns the file size, in bytes, or false, on failure\r
+     *\r
+     * Files larger than 4GB will show up as being exactly 4GB.\r
+     *\r
+     * @param optional String $dir\r
+     * @return Mixed\r
+     * @access public\r
+     */\r
+    function size($filename)\r
+    {\r
+        if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {\r
+            return false;\r
+        }\r
+\r
+        $filename = $this->_realpath($filename);\r
+        if ($filename === false) {\r
+            return false;\r
+        }\r
+\r
+        // SFTPv4+ adds an additional 32-bit integer field - flags - to the following:\r
+        $packet = pack('Na*', strlen($filename), $filename);\r
+        if (!$this->_send_sftp_packet(NET_SFTP_STAT, $packet)) {\r
+            return false;\r
+        }\r
+\r
+        $response = $this->_get_sftp_packet();\r
+        switch ($this->packet_type) {\r
+            case NET_SFTP_ATTRS:\r
+                $attrs = $this->_parseAttributes($response);\r
+                return $attrs['size'];\r
+            case NET_SFTP_STATUS:\r
+                extract(unpack('Nstatus/Nlength', $this->_string_shift($response, 8)));\r
+                $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+                return false;\r
+        }\r
+\r
+        user_error('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS', E_USER_NOTICE);\r
+        return false;\r
+    }\r
+\r
+    /**\r
+     * Set permissions on a file.\r
+     *\r
+     * Returns the new file permissions on success or FALSE on error.\r
+     *\r
+     * @param Integer $mode\r
+     * @param String $filename\r
+     * @return Mixed\r
+     * @access public\r
+     */\r
+    function chmod($mode, $filename)\r
+    {\r
+        if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {\r
+            return false;\r
+        }\r
+\r
+        $filename = $this->_realpath($filename);\r
+        if ($filename === false) {\r
+            return false;\r
+        }\r
+\r
+        // SFTPv4+ has an additional byte field - type - that would need to be sent, as well. setting it to\r
+        // SSH_FILEXFER_TYPE_UNKNOWN might work. if not, we'd have to do an SSH_FXP_STAT before doing an SSH_FXP_SETSTAT.\r
+        $attr = pack('N2', NET_SFTP_ATTR_PERMISSIONS, $mode & 07777);\r
+        if (!$this->_send_sftp_packet(NET_SFTP_SETSTAT, pack('Na*a*', strlen($filename), $filename, $attr))) {\r
+            return false;\r
+        }\r
+\r
+        /*\r
+         "Because some systems must use separate system calls to set various attributes, it is possible that a failure \r
+          response will be returned, but yet some of the attributes may be have been successfully modified.  If possible,\r
+          servers SHOULD avoid this situation; however, clients MUST be aware that this is possible."\r
+\r
+          -- http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.6\r
+        */\r
+        $response = $this->_get_sftp_packet();\r
+        if ($this->packet_type != NET_SFTP_STATUS) {\r
+            user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);\r
+            return false;\r
+        }\r
+\r
+        extract(unpack('Nstatus', $this->_string_shift($response, 4)));\r
+        if ($status != NET_SFTP_STATUS_EOF) {\r
+            extract(unpack('Nlength', $this->_string_shift($response, 4)));\r
+            $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+        }\r
+\r
+        // rather than return what the permissions *should* be, we'll return what they actually are.  this will also\r
+        // tell us if the file actually exists.\r
+        // incidentally, SFTPv4+ adds an additional 32-bit integer field - flags - to the following:\r
+        $packet = pack('Na*', strlen($filename), $filename);\r
+        if (!$this->_send_sftp_packet(NET_SFTP_STAT, $packet)) {\r
+            return false;\r
+        }\r
+\r
+        $response = $this->_get_sftp_packet();\r
+        switch ($this->packet_type) {\r
+            case NET_SFTP_ATTRS:\r
+                $attrs = $this->_parseAttributes($response);\r
+                return $attrs['permissions'];\r
+            case NET_SFTP_STATUS:\r
+                extract(unpack('Nstatus/Nlength', $this->_string_shift($response, 8)));\r
+                $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+                return false;\r
+        }\r
+\r
+        user_error('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS', E_USER_NOTICE);\r
+        return false;\r
+    }\r
+\r
+    /**\r
+     * Creates a directory.\r
+     *\r
+     * @param String $dir\r
+     * @return Boolean\r
+     * @access public\r
+     */\r
+    function mkdir($dir)\r
+    {\r
+        if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {\r
+            return false;\r
+        }\r
+\r
+        $dir = $this->_realpath(rtrim($dir, '/'));\r
+        if ($dir === false) {\r
+            return false;\r
+        }\r
+\r
+        // by not providing any permissions, hopefully the server will use the logged in users umask - their \r
+        // default permissions.\r
+        if (!$this->_send_sftp_packet(NET_SFTP_MKDIR, pack('Na*N', strlen($dir), $dir, 0))) {\r
+            return false;\r
+        }\r
+\r
+        $response = $this->_get_sftp_packet();\r
+        if ($this->packet_type != NET_SFTP_STATUS) {\r
+            user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);\r
+            return false;\r
+        }\r
+\r
+        extract(unpack('Nstatus', $this->_string_shift($response, 4)));\r
+        if ($status != NET_SFTP_STATUS_OK) {\r
+            extract(unpack('Nlength', $this->_string_shift($response, 4)));\r
+            $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+            return false;\r
+        }\r
+\r
+        return true;\r
+    }\r
+\r
+    /**\r
+     * Removes a directory.\r
+     *\r
+     * @param String $dir\r
+     * @return Boolean\r
+     * @access public\r
+     */\r
+    function rmdir($dir)\r
+    {\r
+        if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {\r
+            return false;\r
+        }\r
+\r
+        $dir = $this->_realpath($dir);\r
+        if ($dir === false) {\r
+            return false;\r
+        }\r
+\r
+        if (!$this->_send_sftp_packet(NET_SFTP_RMDIR, pack('Na*', strlen($dir), $dir))) {\r
+            return false;\r
+        }\r
+\r
+        $response = $this->_get_sftp_packet();\r
+        if ($this->packet_type != NET_SFTP_STATUS) {\r
+            user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);\r
+            return false;\r
+        }\r
+\r
+        extract(unpack('Nstatus', $this->_string_shift($response, 4)));\r
+        if ($status != NET_SFTP_STATUS_OK) {\r
+            // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED?\r
+            extract(unpack('Nlength', $this->_string_shift($response, 4)));\r
+            $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+            return false;\r
+        }\r
+\r
+        return true;\r
+    }\r
+\r
+    /**\r
+     * Uploads a file to the SFTP server.\r
+     *\r
+     * By default, Net_SFTP::put() does not read from the local filesystem.  $data is dumped directly into $remote_file.\r
+     * So, for example, if you set $data to 'filename.ext' and then do Net_SFTP::get(), you will get a file, twelve bytes\r
+     * long, containing 'filename.ext' as its contents.\r
+     *\r
+     * Setting $mode to NET_SFTP_LOCAL_FILE will change the above behavior.  With NET_SFTP_LOCAL_FILE, $remote_file will \r
+     * contain as many bytes as filename.ext does on your local filesystem.  If your filename.ext is 1MB then that is how\r
+     * large $remote_file will be, as well.\r
+     *\r
+     * Currently, only binary mode is supported.  As such, if the line endings need to be adjusted, you will need to take\r
+     * care of that, yourself.\r
+     *\r
+     * @param String $remote_file\r
+     * @param String $data\r
+     * @param optional Integer $flags\r
+     * @return Boolean\r
+     * @access public\r
+     * @internal ASCII mode for SFTPv4/5/6 can be supported by adding a new function - Net_SFTP::setMode().\r
+     */\r
+    function put($remote_file, $data, $mode = NET_SFTP_STRING)\r
+    {\r
+        if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {\r
+            return false;\r
+        }\r
+\r
+        $remote_file = $this->_realpath($remote_file);\r
+        if ($remote_file === false) {\r
+            return false;\r
+        }\r
+\r
+        $packet = pack('Na*N2', strlen($remote_file), $remote_file, NET_SFTP_OPEN_WRITE | NET_SFTP_OPEN_CREATE | NET_SFTP_OPEN_TRUNCATE, 0);\r
+        if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {\r
+            return false;\r
+        }\r
+\r
+        $response = $this->_get_sftp_packet();\r
+        switch ($this->packet_type) {\r
+            case NET_SFTP_HANDLE:\r
+                $handle = substr($response, 4);\r
+                break;\r
+            case NET_SFTP_STATUS:\r
+                extract(unpack('Nstatus/Nlength', $this->_string_shift($response, 8)));\r
+                $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+                return false;\r
+            default:\r
+                user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS', E_USER_NOTICE);\r
+                return false;\r
+        }\r
+\r
+        $initialize = true;\r
+\r
+        // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.2.3\r
+        if ($mode == NET_SFTP_LOCAL_FILE) {\r
+            if (!is_file($data)) {\r
+                user_error("$data is not a valid file", E_USER_NOTICE);\r
+                return false;\r
+            }\r
+            $fp = fopen($data, 'rb');\r
+            if (!$fp) {\r
+                return false;\r
+            }\r
+            $sent = 0;\r
+            $size = filesize($data);\r
+        } else {\r
+            $sent = 0;\r
+            $size = strlen($data);\r
+        }\r
+\r
+        $size = $size < 0 ? ($size & 0x7FFFFFFF) + 0x80000000 : $size;\r
+\r
+        $sftp_packet_size = 34000; // PuTTY uses 4096\r
+        $i = 0;\r
+        while ($sent < $size) {\r
+            $temp = $mode == NET_SFTP_LOCAL_FILE ? fread($fp, $sftp_packet_size) : $this->_string_shift($data, $sftp_packet_size);\r
+            $packet = pack('Na*N3a*', strlen($handle), $handle, 0, $sent, strlen($temp), $temp);\r
+            if (!$this->_send_sftp_packet(NET_SFTP_WRITE, $packet)) {\r
+                fclose($fp);\r
+                return false;\r
+            }\r
+            $sent+= strlen($temp);\r
+\r
+            $i++;\r
+\r
+            if ($i == 50) {\r
+                if (!$this->_read_put_responses($i)) {\r
+                    $i = 0;\r
+                    break;\r
+                }\r
+                $i = 0;\r
+            }\r
+        }\r
+\r
+        $this->_read_put_responses($i);\r
+\r
+        if ($mode == NET_SFTP_LOCAL_FILE) {\r
+            fclose($fp);\r
+        }\r
+\r
+        if (!$this->_send_sftp_packet(NET_SFTP_CLOSE, pack('Na*', strlen($handle), $handle))) {\r
+            return false;\r
+        }\r
+\r
+        $response = $this->_get_sftp_packet();\r
+        if ($this->packet_type != NET_SFTP_STATUS) {\r
+            user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);\r
+            return false;\r
+        }\r
+\r
+        extract(unpack('Nstatus', $this->_string_shift($response, 4)));\r
+        if ($status != NET_SFTP_STATUS_OK) {\r
+            extract(unpack('Nlength', $this->_string_shift($response, 4)));\r
+            $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+            return false;\r
+        }\r
+\r
+        return true;\r
+    }\r
+\r
+    /**\r
+     * Reads multiple successive SSH_FXP_WRITE responses\r
+     *\r
+     * Sending an SSH_FXP_WRITE packet and immediately reading its response isn't as efficient as blindly sending out $i\r
+     * SSH_FXP_WRITEs, in succession, and then reading $i responses.\r
+     *\r
+     * @param Integer $i\r
+     * @return Boolean\r
+     * @access private\r
+     */\r
+    function _read_put_responses($i)\r
+    {\r
+        while ($i--) {\r
+            $response = $this->_get_sftp_packet();\r
+            if ($this->packet_type != NET_SFTP_STATUS) {\r
+                user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);\r
+                return false;\r
+            }\r
+\r
+            extract(unpack('Nstatus', $this->_string_shift($response, 4)));\r
+            if ($status != NET_SFTP_STATUS_OK) {\r
+                extract(unpack('Nlength', $this->_string_shift($response, 4)));\r
+                $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+                break;\r
+            }\r
+        }\r
+\r
+        return $i < 0;\r
+    }\r
+\r
+    /**\r
+     * Downloads a file from the SFTP server.\r
+     *\r
+     * Returns a string containing the contents of $remote_file if $local_file is left undefined or a boolean false if\r
+     * the operation was unsuccessful.  If $local_file is defined, returns true or false depending on the success of the\r
+     * operation\r
+     *\r
+     * @param String $remote_file\r
+     * @param optional String $local_file\r
+     * @return Mixed\r
+     * @access public\r
+     */\r
+    function get($remote_file, $local_file = false)\r
+    {\r
+        if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {\r
+            return false;\r
+        }\r
+\r
+        $remote_file = $this->_realpath($remote_file);\r
+        if ($remote_file === false) {\r
+            return false;\r
+        }\r
+\r
+        $packet = pack('Na*N2', strlen($remote_file), $remote_file, NET_SFTP_OPEN_READ, 0);\r
+        if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {\r
+            return false;\r
+        }\r
+\r
+        $response = $this->_get_sftp_packet();\r
+        switch ($this->packet_type) {\r
+            case NET_SFTP_HANDLE:\r
+                $handle = substr($response, 4);\r
+                break;\r
+            case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED\r
+                extract(unpack('Nstatus/Nlength', $this->_string_shift($response, 8)));\r
+                $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+                return false;\r
+            default:\r
+                user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS', E_USER_NOTICE);\r
+                return false;\r
+        }\r
+\r
+        $packet = pack('Na*', strlen($handle), $handle);\r
+        if (!$this->_send_sftp_packet(NET_SFTP_FSTAT, $packet)) {\r
+            return false;\r
+        }\r
+\r
+        $response = $this->_get_sftp_packet();\r
+        switch ($this->packet_type) {\r
+            case NET_SFTP_ATTRS:\r
+                $attrs = $this->_parseAttributes($response);\r
+                break;\r
+            case NET_SFTP_STATUS:\r
+                extract(unpack('Nstatus/Nlength', $this->_string_shift($response, 8)));\r
+                $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+                return false;\r
+            default:\r
+                user_error('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS', E_USER_NOTICE);\r
+                return false;\r
+        }\r
+\r
+        if ($local_file !== false) {\r
+            $fp = fopen($local_file, 'wb');\r
+            if (!$fp) {\r
+                return false;\r
+            }\r
+        } else {\r
+            $content = '';\r
+        }\r
+\r
+        $read = 0;\r
+        while ($read < $attrs['size']) {\r
+            $packet = pack('Na*N3', strlen($handle), $handle, 0, $read, 1 << 20);\r
+            if (!$this->_send_sftp_packet(NET_SFTP_READ, $packet)) {\r
+                return false;\r
+            }\r
+\r
+            $response = $this->_get_sftp_packet();\r
+            switch ($this->packet_type) {\r
+                case NET_SFTP_DATA:\r
+                    $temp = substr($response, 4);\r
+                    $read+= strlen($temp);\r
+                    if ($local_file === false) {\r
+                        $content.= $temp;\r
+                    } else {\r
+                        fputs($fp, $temp);\r
+                    }\r
+                    break;\r
+                case NET_SFTP_STATUS:\r
+                    extract(unpack('Nstatus/Nlength', $this->_string_shift($response, 8)));\r
+                    $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+                    break 2;\r
+                default:\r
+                    user_error('Expected SSH_FXP_DATA or SSH_FXP_STATUS', E_USER_NOTICE);\r
+                    return false;\r
+            }\r
+        }\r
+\r
+        if (!$this->_send_sftp_packet(NET_SFTP_CLOSE, pack('Na*', strlen($handle), $handle))) {\r
+            return false;\r
+        }\r
+\r
+        $response = $this->_get_sftp_packet();\r
+        if ($this->packet_type != NET_SFTP_STATUS) {\r
+            user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);\r
+            return false;\r
+        }\r
+\r
+        extract(unpack('Nstatus', $this->_string_shift($response, 4)));\r
+        if ($status != NET_SFTP_STATUS_OK) {\r
+            extract(unpack('Nlength', $this->_string_shift($response, 4)));\r
+            $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+            return false;\r
+        }\r
+\r
+        if (isset($content)) {\r
+            return $content;\r
+        }\r
+\r
+        fclose($fp);\r
+        return true;\r
+    }\r
+\r
+    /**\r
+     * Deletes a file on the SFTP server.\r
+     *\r
+     * @param String $path\r
+     * @return Boolean\r
+     * @access public\r
+     */\r
+    function delete($path)\r
+    {\r
+        if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {\r
+            return false;\r
+        }\r
+\r
+        $remote_file = $this->_realpath($path);\r
+        if ($path === false) {\r
+            return false;\r
+        }\r
+\r
+        // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.3\r
+        if (!$this->_send_sftp_packet(NET_SFTP_REMOVE, pack('Na*', strlen($path), $path))) {\r
+            return false;\r
+        }\r
+\r
+        $response = $this->_get_sftp_packet();\r
+        if ($this->packet_type != NET_SFTP_STATUS) {\r
+            user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);\r
+            return false;\r
+        }\r
+\r
+        // if $status isn't SSH_FX_OK it's probably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED\r
+        extract(unpack('Nstatus', $this->_string_shift($response, 4)));\r
+        if ($status != NET_SFTP_STATUS_OK) {\r
+            extract(unpack('Nlength', $this->_string_shift($response, 4)));\r
+            $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+            return false;\r
+        }\r
+\r
+        return true;\r
+    }\r
+\r
+    /**\r
+     * Renames a file or a directory on the SFTP server\r
+     *\r
+     * @param String $oldname\r
+     * @param String $newname\r
+     * @return Boolean\r
+     * @access public\r
+     */\r
+    function rename($oldname, $newname)\r
+    {\r
+        if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {\r
+            return false;\r
+        }\r
+\r
+        $oldname = $this->_realpath($oldname);\r
+        $newname = $this->_realpath($newname);\r
+        if ($oldname === false || $newname === false) {\r
+            return false;\r
+        }\r
+\r
+        // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.3\r
+        $packet = pack('Na*Na*', strlen($oldname), $oldname, strlen($newname), $newname);\r
+        if (!$this->_send_sftp_packet(NET_SFTP_RENAME, $packet)) {\r
+            return false;\r
+        }\r
+\r
+        $response = $this->_get_sftp_packet();\r
+        if ($this->packet_type != NET_SFTP_STATUS) {\r
+            user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);\r
+            return false;\r
+        }\r
+\r
+        // if $status isn't SSH_FX_OK it's probably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED\r
+        extract(unpack('Nstatus', $this->_string_shift($response, 4)));\r
+        if ($status != NET_SFTP_STATUS_OK) {\r
+            extract(unpack('Nlength', $this->_string_shift($response, 4)));\r
+            $this->sftp_errors[] = $this->status_codes[$status] . ': ' . $this->_string_shift($response, $length);\r
+            return false;\r
+        }\r
+\r
+        return true;\r
+    }\r
+\r
+    /**\r
+     * Parse Attributes\r
+     *\r
+     * See '7.  File Attributes' of draft-ietf-secsh-filexfer-13 for more info.\r
+     *\r
+     * @param String $response\r
+     * @return Array\r
+     * @access private\r
+     */\r
+    function _parseAttributes(&$response)\r
+    {\r
+        $attr = array();\r
+        extract(unpack('Nflags', $this->_string_shift($response, 4)));\r
+        // SFTPv4+ have a type field (a byte) that follows the above flag field\r
+        foreach ($this->attributes as $key => $value) {\r
+            switch ($flags & $key) {\r
+                case NET_SFTP_ATTR_SIZE: // 0x00000001\r
+                    // size is represented by a 64-bit integer, so we perhaps ought to be doing the following:\r
+                    // $attr['size'] = new Math_BigInteger($this->_string_shift($response, 8), 256);\r
+                    // of course, you shouldn't be using Net_SFTP to transfer files that are in excess of 4GB\r
+                    // (0xFFFFFFFF bytes), anyway.  as such, we'll just represent all file sizes that are bigger than\r
+                    // 4GB as being 4GB.\r
+                    extract(unpack('Nupper/Nsize', $this->_string_shift($response, 8)));\r
+                    if ($upper) {\r
+                        $attr['size'] = 0xFFFFFFFF;\r
+                    } else {\r
+                        $attr['size'] = $size < 0 ? ($size & 0x7FFFFFFF) + 0x80000000 : $size;\r
+                    }\r
+                    break;\r
+                case NET_SFTP_ATTR_UIDGID: // 0x00000002 (SFTPv3 only)\r
+                    $attr+= unpack('Nuid/Ngid', $this->_string_shift($response, 8));\r
+                    break;\r
+                case NET_SFTP_ATTR_PERMISSIONS: // 0x00000004\r
+                    $attr+= unpack('Npermissions', $this->_string_shift($response, 4));\r
+                    break;\r
+                case NET_SFTP_ATTR_ACCESSTIME: // 0x00000008\r
+                    $attr+= unpack('Natime/Nmtime', $this->_string_shift($response, 8));\r
+                    break;\r
+                case NET_SFTP_ATTR_EXTENDED: // 0x80000000\r
+                    extract(unpack('Ncount', $this->_string_shift($response, 4)));\r
+                    for ($i = 0; $i < $count; $i++) {\r
+                        extract(unpack('Nlength', $this->_string_shift($response, 4)));\r
+                        $key = $this->_string_shift($response, $length);\r
+                        extract(unpack('Nlength', $this->_string_shift($response, 4)));\r
+                        $attr[$key] = $this->_string_shift($response, $length);                        \r
+                    }\r
+            }\r
+        }\r
+        return $attr;\r
+    }\r
+\r
+    /**\r
+     * Sends SFTP Packets\r
+     *\r
+     * See '6. General Packet Format' of draft-ietf-secsh-filexfer-13 for more info.\r
+     *\r
+     * @param Integer $type\r
+     * @param String $data\r
+     * @see Net_SFTP::_get_sftp_packet()\r
+     * @see Net_SSH2::_send_channel_packet()\r
+     * @return Boolean\r
+     * @access private\r
+     */\r
+    function _send_sftp_packet($type, $data)\r
+    {\r
+        $packet = $this->request_id !== false ?\r
+            pack('NCNa*', strlen($data) + 5, $type, $this->request_id, $data) :\r
+            pack('NCa*',  strlen($data) + 1, $type, $data);\r
+\r
+        $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838\r
+        $result = $this->_send_channel_packet(NET_SFTP_CHANNEL, $packet);\r
+        $stop = strtok(microtime(), ' ') + strtok('');\r
+\r
+        if (defined('NET_SFTP_LOGGING')) {\r
+            $this->packet_type_log[] = '-> ' . $this->packet_types[$type] . \r
+                                       ' (' . round($stop - $start, 4) . 's)';\r
+            if (NET_SFTP_LOGGING == NET_SFTP_LOG_COMPLEX) {\r
+                $this->packet_log[] = $data;\r
+            }\r
+        }\r
+\r
+        return $result;\r
+    }\r
+\r
+    /**\r
+     * Receives SFTP Packets\r
+     *\r
+     * See '6. General Packet Format' of draft-ietf-secsh-filexfer-13 for more info.\r
+     *\r
+     * Incidentally, the number of SSH_MSG_CHANNEL_DATA messages has no bearing on the number of SFTP packets present.\r
+     * There can be one SSH_MSG_CHANNEL_DATA messages containing two SFTP packets or there can be two SSH_MSG_CHANNEL_DATA\r
+     * messages containing one SFTP packet.\r
+     *\r
+     * @see Net_SFTP::_send_sftp_packet()\r
+     * @return String\r
+     * @access private\r
+     */\r
+    function _get_sftp_packet()\r
+    {\r
+        $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838\r
+\r
+        // SFTP packet length\r
+        while (strlen($this->packet_buffer) < 4) {\r
+            $temp = $this->_get_channel_packet(NET_SFTP_CHANNEL);\r
+            if (is_bool($temp)) {\r
+                $this->packet_type = false;\r
+                $this->packet_buffer = '';\r
+                return false;\r
+            }\r
+            $this->packet_buffer.= $temp;\r
+        }\r
+        extract(unpack('Nlength', $this->_string_shift($this->packet_buffer, 4)));\r
+        $tempLength = $length;\r
+        $tempLength-= strlen($this->packet_buffer);\r
+\r
+        // SFTP packet type and data payload\r
+        while ($tempLength > 0) {\r
+            $temp = $this->_get_channel_packet(NET_SFTP_CHANNEL);\r
+            if (is_bool($temp)) {\r
+                $this->packet_type = false;\r
+                $this->packet_buffer = '';\r
+                return false;\r
+            }\r
+            $this->packet_buffer.= $temp;\r
+            $tempLength-= strlen($temp);\r
+        }\r
+\r
+        $stop = strtok(microtime(), ' ') + strtok('');\r
+\r
+        $this->packet_type = ord($this->_string_shift($this->packet_buffer));\r
+\r
+        if ($this->request_id !== false) {\r
+            $this->_string_shift($this->packet_buffer, 4); // remove the request id\r
+            $length-= 5; // account for the request id and the packet type\r
+        } else {\r
+            $length-= 1; // account for the packet type\r
+        }\r
+\r
+        $packet = $this->_string_shift($this->packet_buffer, $length);\r
+\r
+        if (defined('NET_SFTP_LOGGING')) {\r
+            $this->packet_type_log[] = '<- ' . $this->packet_types[$this->packet_type] . \r
+                                       ' (' . round($stop - $start, 4) . 's)';\r
+            if (NET_SFTP_LOGGING == NET_SFTP_LOG_COMPLEX) {\r
+                $this->packet_log[] = $packet;\r
+            }\r
+        }\r
+\r
+        return $packet;\r
+    }\r
+\r
+    /**\r
+     * Returns a log of the packets that have been sent and received.\r
+     *\r
+     * 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')\r
+     *\r
+     * @access public\r
+     * @return String or Array\r
+     */\r
+    function getSFTPLog()\r
+    {\r
+        if (!defined('NET_SFTP_LOGGING')) {\r
+            return false;\r
+        }\r
+\r
+        switch (NET_SFTP_LOGGING) {\r
+            case NET_SFTP_LOG_COMPLEX:\r
+                return $this->_format_log($this->packet_log, $this->packet_type_log);\r
+                break;\r
+            //case NET_SFTP_LOG_SIMPLE:\r
+            default:\r
+                return $this->packet_type_log;\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Returns all errors\r
+     *\r
+     * @return String\r
+     * @access public\r
+     */\r
+    function getSFTPErrors()\r
+    {\r
+        return $this->sftp_errors;\r
+    }\r
+\r
+    /**\r
+     * Returns the last error\r
+     *\r
+     * @return String\r
+     * @access public\r
+     */\r
+    function getLastSFTPError()\r
+    {\r
+        return $this->sftp_errors[count($this->sftp_errors) - 1];\r
+    }\r
+\r
+    /**\r
+     * Get supported SFTP versions\r
+     *\r
+     * @return Array\r
+     * @access public\r
+     */\r
+    function getSupportedVersions()\r
+    {\r
+        $temp = array('version' => $this->version);\r
+        if (isset($this->extensions['versions'])) {\r
+            $temp['extensions'] = $this->extensions['versions'];\r
+        }\r
+        return $temp;\r
+    }\r
+\r
+    /**\r
+     * Disconnect\r
+     *\r
+     * @param Integer $reason\r
+     * @return Boolean\r
+     * @access private\r
+     */\r
+    function _disconnect($reason)\r
+    {\r
+        $this->pwd = false;\r
+        parent::_disconnect($reason);\r
+    }\r
+}
\ No newline at end of file