123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766 |
- <?php
- class Net_SFTP_Stream {
-
- static $instances;
-
- var $sftp;
-
- var $path;
-
- var $mode;
-
- var $pos;
-
- var $size;
-
- var $entries;
-
- var $eof;
-
- var $context;
-
- var $notification;
-
- function Net_SFTP_Stream()
- {
- if (!class_exists('Net_SFTP')) {
- require_once('Net/SFTP.php');
- }
- }
-
- function _parse_path($path)
- {
- extract(parse_url($path));
- if (!isset($host)) {
- return false;
- }
- $context = stream_context_get_params($this->context);
- if (isset($context['notification'])) {
- $this->notification = $context['notification'];
- }
- if ($host[0] == '$') {
- $host = substr($host, 1);
- global $$host;
- if (!is_object($$host) || get_class($$host) != 'Net_sFTP') {
- return false;
- }
- $this->sftp = $$host;
- } else {
- $context = stream_context_get_options($this->context);
- if (isset($context['sftp']['session'])) {
- $sftp = $context['sftp']['session'];
- }
- if (isset($context['sftp']['sftp'])) {
- $sftp = $context['sftp']['sftp'];
- }
- if (isset($sftp) && is_object($sftp) && get_class($sftp) == 'Net_SFTP') {
- $this->sftp = $sftp;
- return $path;
- }
- if (isset($context['sftp']['username'])) {
- $user = $context['sftp']['username'];
- }
- if (isset($context['sftp']['password'])) {
- $pass = $context['sftp']['password'];
- }
- if (isset($context['sftp']['privkey']) && is_object($context['sftp']['privkey']) && get_Class($context['sftp']['privkey']) == 'Crypt_RSA') {
- $pass = $context['sftp']['privkey'];
- }
- if (!isset($user) || !isset($pass)) {
- return false;
- }
-
- 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, isset($port) ? $port : 22);
- if (isset($this->notification) && is_callable($this->notification)) {
-
- 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;
- }
- }
- self::$instances[$host][$port][$user][(string) $pass] = $this->sftp;
- }
- }
- return $path;
- }
-
- function _stream_open($path, $mode, $options, &$opened_path)
- {
- $path = $this->_parse_path($path);
- if ($path === false) {
- return false;
- }
- $this->path = $path;
- $this->size = $this->sftp->size($path);
- $this->mode = preg_replace('#[bt]$#', '', $mode);
- if ($this->size === false) {
- if ($this->mode[0] == 'r') {
- return false;
- }
- } else {
- switch ($this->mode[0]) {
- case 'x':
- return false;
- case 'w':
- case 'c':
- $this->sftp->truncate($path, 0);
- }
- }
- $this->pos = $this->mode[0] != 'a' ? 0 : $this->size;
- return true;
- }
-
- function _stream_read($count)
- {
- switch ($this->mode) {
- case 'w':
- case 'a':
- case 'x':
- case 'c':
- return false;
- }
-
-
-
-
-
- $result = $this->sftp->get($this->path, false, $this->pos, $count);
- 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;
- }
-
- call_user_func($this->notification, STREAM_NOTIFY_PROGRESS, STREAM_NOTIFY_SEVERITY_INFO, '', 0, strlen($result), $this->size);
- }
- if (empty($result)) {
- $this->eof = true;
- return false;
- }
- $this->pos+= strlen($result);
- return $result;
- }
-
- function _stream_write($data)
- {
- switch ($this->mode) {
- case 'r':
- return false;
- }
- $result = $this->sftp->put($this->path, $data, NET_SFTP_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);
- return 0;
- }
-
- call_user_func($this->notification, STREAM_NOTIFY_PROGRESS, STREAM_NOTIFY_SEVERITY_INFO, '', 0, strlen($data), strlen($data));
- }
- if ($result === false) {
- return false;
- }
- $this->pos+= strlen($data);
- if ($this->pos > $this->size) {
- $this->size = $this->pos;
- }
- $this->eof = false;
- return strlen($data);
- }
-
- function _stream_tell()
- {
- return $this->pos;
- }
-
- function _stream_eof()
- {
- return $this->eof;
- }
-
- function _stream_seek($offset, $whence)
- {
- 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;
- }
-
- function _stream_metadata($path, $option, $var)
- {
- $path = $this->_parse_path($path);
- if ($path === false) {
- return false;
- }
-
-
-
- switch ($option) {
- case 1:
- return $this->sftp->touch($path, $var[0], $var[1]);
- case 2:
- case 3:
- return false;
- case 4:
- return $this->sftp->chown($path, $var);
- case 5:
- return $this->sftp->chgrp($path, $var);
- case 6:
- return $this->sftp->chmod($path, $var) !== false;
- }
- }
-
- function _stream_cast($cast_as)
- {
- return $this->sftp->fsock;
- }
-
- function _stream_lock($operation)
- {
- return false;
- }
-
- function _rename($path_from, $path_to)
- {
- $path1 = parse_url($path_from);
- $path2 = parse_url($path_to);
- unset($path1['path'], $path2['path']);
- if ($path1 != $path2) {
- return false;
- }
- $path_from = $this->_parse_path($path_from);
- $path_to = parse_url($path_to);
- if ($path_from == false) {
- return false;
- }
- $path_to = $path_to['path'];
-
-
- 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;
- }
-
- function _dir_opendir($path, $options)
- {
- $path = $this->_parse_path($path);
- if ($path === false) {
- return false;
- }
- $this->pos = 0;
- $this->entries = $this->sftp->nlist($path);
- return $this->entries !== false;
- }
-
- function _dir_readdir()
- {
- if (isset($this->entries[$this->pos])) {
- return $this->entries[$this->pos++];
- }
- return false;
- }
-
- function _dir_rewinddir()
- {
- $this->pos = 0;
- return true;
- }
-
- function _dir_closedir()
- {
- return true;
- }
-
- function _mkdir($path, $mode, $options)
- {
- $path = $this->_parse_path($path);
- if ($path === false) {
- return false;
- }
- return $this->sftp->mkdir($path, $mode, $options & STREAM_MKDIR_RECURSIVE);
- }
-
- function _rmdir($path, $options)
- {
- $path = $this->_parse_path($path);
- if ($path === false) {
- return false;
- }
- return $this->sftp->rmdir($path);
- }
-
- function _stream_flush()
- {
- return true;
- }
-
- function _stream_stat()
- {
- $results = $this->sftp->stat($this->path);
- if ($results === false) {
- return false;
- }
- return $results;
- }
-
- function _unlink($path)
- {
- $path = $this->_parse_path($path);
- if ($path === false) {
- return false;
- }
- return $this->sftp->delete($path, false);
- }
-
- function _url_stat($path, $flags)
- {
- $path = $this->_parse_path($path);
- 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;
- }
-
- function _stream_truncate($new_size)
- {
- if (!$this->sftp->truncate($this->path, $new_size)) {
- return false;
- }
- $this->eof = false;
- $this->size = $new_size;
- return true;
- }
-
- function _stream_set_option($option, $arg1, $arg2)
- {
- return false;
- }
-
- function _stream_close()
- {
- }
-
- function __call($name, $arguments)
- {
- 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;
- }
- return call_user_func_array(array($this, $name), $arguments);
- }
- }
- if (function_exists('stream_wrapper_register')) {
- stream_wrapper_register('sftp', 'Net_SFTP_Stream');
- }
|