2012-12-26 20:59:08 +04:00
|
|
|
<?php
|
2012-12-27 22:47:46 +04:00
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Andreas Fischer <bantu@owncloud.com>
|
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author hkjolhede <hkjolhede@gmail.com>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Lennart Rosam <lennart.rosam@medien-systempartner.de>
|
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2015-10-05 21:54:56 +03:00
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Ross Nicoll <jrn@jrn.me.uk>
|
|
|
|
* @author SA <stephen@mthosting.net>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
2016-01-12 17:02:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2012-12-27 22:47:46 +04:00
|
|
|
*/
|
2013-02-09 18:57:33 +04:00
|
|
|
namespace OC\Files\Storage;
|
2015-03-10 18:30:13 +03:00
|
|
|
use Icewind\Streams\IteratorDirectory;
|
2012-12-27 22:47:46 +04:00
|
|
|
|
2015-08-03 07:39:53 +03:00
|
|
|
use phpseclib\Net\SFTP\Stream;
|
|
|
|
|
2014-07-12 15:58:49 +04:00
|
|
|
/**
|
2015-08-03 07:39:53 +03:00
|
|
|
* Uses phpseclib's Net\SFTP class and the Net\SFTP\Stream stream wrapper to
|
2014-07-12 15:58:49 +04:00
|
|
|
* provide access to SFTP servers.
|
|
|
|
*/
|
2013-02-10 15:35:49 +04:00
|
|
|
class SFTP extends \OC\Files\Storage\Common {
|
2012-12-27 02:38:17 +04:00
|
|
|
private $host;
|
2012-12-26 20:59:08 +04:00
|
|
|
private $user;
|
|
|
|
private $root;
|
2014-12-19 16:19:51 +03:00
|
|
|
private $port = 22;
|
2012-12-26 20:59:08 +04:00
|
|
|
|
2015-08-13 00:20:08 +03:00
|
|
|
private $auth;
|
|
|
|
|
2014-07-13 00:45:53 +04:00
|
|
|
/**
|
2015-08-03 07:39:53 +03:00
|
|
|
* @var SFTP
|
2014-07-13 00:45:53 +04:00
|
|
|
*/
|
2014-12-19 20:23:24 +03:00
|
|
|
protected $client;
|
2012-12-26 20:59:08 +04:00
|
|
|
|
2015-11-16 16:56:03 +03:00
|
|
|
/**
|
|
|
|
* @param string $host protocol://server:port
|
|
|
|
* @return array [$server, $port]
|
|
|
|
*/
|
|
|
|
private function splitHost($host) {
|
|
|
|
$input = $host;
|
|
|
|
if (strpos($host, '://') === false) {
|
|
|
|
// add a protocol to fix parse_url behavior with ipv6
|
|
|
|
$host = 'http://' . $host;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parsed = parse_url($host);
|
|
|
|
if(is_array($parsed) && isset($parsed['port'])) {
|
|
|
|
return [$parsed['host'], $parsed['port']];
|
|
|
|
} else if (is_array($parsed)) {
|
|
|
|
return [$parsed['host'], 22];
|
|
|
|
} else {
|
|
|
|
return [$input, 22];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 20:59:08 +04:00
|
|
|
public function __construct($params) {
|
2015-02-03 18:02:07 +03:00
|
|
|
// Register sftp://
|
2015-08-03 07:39:53 +03:00
|
|
|
Stream::register();
|
2014-07-12 15:57:27 +04:00
|
|
|
|
2015-11-16 16:56:03 +03:00
|
|
|
$parsedHost = $this->splitHost($params['host']);
|
2015-08-03 07:39:53 +03:00
|
|
|
|
2015-11-16 16:56:03 +03:00
|
|
|
$this->host = $parsedHost[0];
|
|
|
|
$this->port = $parsedHost[1];
|
2014-12-19 16:19:51 +03:00
|
|
|
|
2016-01-26 16:48:45 +03:00
|
|
|
if (!isset($params['user'])) {
|
|
|
|
throw new \UnexpectedValueException('no authentication parameters specified');
|
|
|
|
}
|
2012-12-26 20:59:08 +04:00
|
|
|
$this->user = $params['user'];
|
2015-08-13 00:20:08 +03:00
|
|
|
|
|
|
|
if (isset($params['public_key_auth'])) {
|
|
|
|
$this->auth = $params['public_key_auth'];
|
|
|
|
} elseif (isset($params['password'])) {
|
|
|
|
$this->auth = $params['password'];
|
|
|
|
} else {
|
|
|
|
throw new \UnexpectedValueException('no authentication parameters specified');
|
|
|
|
}
|
|
|
|
|
2013-08-30 16:14:38 +04:00
|
|
|
$this->root
|
|
|
|
= isset($params['root']) ? $this->cleanPath($params['root']) : '/';
|
2013-02-22 20:21:57 +04:00
|
|
|
|
2013-08-30 16:14:38 +04:00
|
|
|
if ($this->root[0] != '/') {
|
|
|
|
$this->root = '/' . $this->root;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (substr($this->root, -1, 1) != '/') {
|
|
|
|
$this->root .= '/';
|
|
|
|
}
|
2014-10-21 18:18:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the connection.
|
|
|
|
*
|
2015-08-05 11:28:11 +03:00
|
|
|
* @return \phpseclib\Net\SFTP connected client instance
|
2014-10-21 18:18:44 +04:00
|
|
|
* @throws \Exception when the connection failed
|
|
|
|
*/
|
|
|
|
public function getConnection() {
|
|
|
|
if (!is_null($this->client)) {
|
|
|
|
return $this->client;
|
|
|
|
}
|
2012-12-27 02:38:17 +04:00
|
|
|
|
2013-08-30 16:14:38 +04:00
|
|
|
$hostKeys = $this->readHostKeys();
|
2015-08-05 11:28:11 +03:00
|
|
|
$this->client = new \phpseclib\Net\SFTP($this->host, $this->port);
|
2013-08-30 16:14:38 +04:00
|
|
|
|
2014-07-12 22:41:35 +04:00
|
|
|
// The SSH Host Key MUST be verified before login().
|
2013-08-30 16:14:38 +04:00
|
|
|
$currentHostKey = $this->client->getServerPublicHostKey();
|
|
|
|
if (array_key_exists($this->host, $hostKeys)) {
|
|
|
|
if ($hostKeys[$this->host] != $currentHostKey) {
|
2013-02-10 15:35:49 +04:00
|
|
|
throw new \Exception('Host public key does not match known key');
|
2012-12-27 02:38:17 +04:00
|
|
|
}
|
|
|
|
} else {
|
2013-08-30 16:14:38 +04:00
|
|
|
$hostKeys[$this->host] = $currentHostKey;
|
|
|
|
$this->writeHostKeys($hostKeys);
|
2012-12-26 20:59:08 +04:00
|
|
|
}
|
2014-07-12 22:41:35 +04:00
|
|
|
|
2015-08-13 00:20:08 +03:00
|
|
|
if (!$this->client->login($this->user, $this->auth)) {
|
2014-07-12 22:41:35 +04:00
|
|
|
throw new \Exception('Login failed');
|
|
|
|
}
|
2014-10-21 18:18:44 +04:00
|
|
|
return $this->client;
|
2012-12-26 20:59:08 +04:00
|
|
|
}
|
2013-02-22 20:21:57 +04:00
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-29 01:22:04 +04:00
|
|
|
public function test() {
|
2013-08-30 16:14:38 +04:00
|
|
|
if (
|
|
|
|
!isset($this->host)
|
|
|
|
|| !isset($this->user)
|
|
|
|
) {
|
2013-08-30 14:10:10 +04:00
|
|
|
return false;
|
2013-02-22 20:21:57 +04:00
|
|
|
}
|
2014-10-21 18:18:44 +04:00
|
|
|
return $this->getConnection()->nlist() !== false;
|
2012-12-29 01:22:04 +04:00
|
|
|
}
|
2013-02-22 20:21:57 +04:00
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2013-02-10 16:11:29 +04:00
|
|
|
public function getId(){
|
2015-04-15 17:21:41 +03:00
|
|
|
$id = 'sftp::' . $this->user . '@' . $this->host;
|
|
|
|
if ($this->port !== 22) {
|
|
|
|
$id .= ':' . $this->port;
|
|
|
|
}
|
|
|
|
// note: this will double the root slash,
|
|
|
|
// we should not change it to keep compatible with
|
|
|
|
// old storage ids
|
|
|
|
$id .= '/' . $this->root;
|
|
|
|
return $id;
|
2013-02-10 16:11:29 +04:00
|
|
|
}
|
2012-12-26 20:59:08 +04:00
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-12-19 20:23:24 +03:00
|
|
|
public function getHost() {
|
|
|
|
return $this->host;
|
|
|
|
}
|
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-12-19 20:23:24 +03:00
|
|
|
public function getRoot() {
|
|
|
|
return $this->root;
|
|
|
|
}
|
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2014-12-19 20:23:24 +03:00
|
|
|
public function getUser() {
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $path
|
2015-02-24 15:30:28 +03:00
|
|
|
* @return string
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2013-08-30 16:14:38 +04:00
|
|
|
private function absPath($path) {
|
2013-02-10 02:49:21 +04:00
|
|
|
return $this->root . $this->cleanPath($path);
|
2012-12-26 20:59:08 +04:00
|
|
|
}
|
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
2015-11-30 13:29:06 +03:00
|
|
|
* @return string|false
|
2015-02-24 15:30:28 +03:00
|
|
|
*/
|
2013-08-30 16:14:38 +04:00
|
|
|
private function hostKeysPath() {
|
2012-12-27 02:49:53 +04:00
|
|
|
try {
|
|
|
|
$storage_view = \OCP\Files::getStorage('files_external');
|
|
|
|
if ($storage_view) {
|
2014-12-17 13:12:37 +03:00
|
|
|
return \OC::$server->getConfig()->getSystemValue('datadirectory') .
|
2012-12-27 02:49:53 +04:00
|
|
|
$storage_view->getAbsolutePath('') .
|
2013-08-30 16:14:38 +04:00
|
|
|
'ssh_hostKeys';
|
2012-12-27 02:49:53 +04:00
|
|
|
}
|
2013-02-10 15:35:49 +04:00
|
|
|
} catch (\Exception $e) {
|
2012-12-27 02:38:17 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* @param $keys
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-12-19 20:23:24 +03:00
|
|
|
protected function writeHostKeys($keys) {
|
2012-12-27 02:38:17 +04:00
|
|
|
try {
|
2013-08-30 16:14:38 +04:00
|
|
|
$keyPath = $this->hostKeysPath();
|
2013-11-16 00:10:09 +04:00
|
|
|
if ($keyPath && file_exists($keyPath)) {
|
|
|
|
$fp = fopen($keyPath, 'w');
|
|
|
|
foreach ($keys as $host => $key) {
|
|
|
|
fwrite($fp, $host . '::' . $key . "\n");
|
|
|
|
}
|
|
|
|
fclose($fp);
|
|
|
|
return true;
|
2012-12-27 02:38:17 +04:00
|
|
|
}
|
2013-02-10 15:35:49 +04:00
|
|
|
} catch (\Exception $e) {
|
2012-12-27 02:38:17 +04:00
|
|
|
}
|
2013-11-16 00:10:09 +04:00
|
|
|
return false;
|
2012-12-27 02:38:17 +04:00
|
|
|
}
|
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2014-12-19 20:23:24 +03:00
|
|
|
protected function readHostKeys() {
|
2012-12-27 02:49:53 +04:00
|
|
|
try {
|
2013-08-30 16:14:38 +04:00
|
|
|
$keyPath = $this->hostKeysPath();
|
|
|
|
if (file_exists($keyPath)) {
|
2012-12-27 02:49:53 +04:00
|
|
|
$hosts = array();
|
|
|
|
$keys = array();
|
2013-08-30 16:14:38 +04:00
|
|
|
$lines = file($keyPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
2012-12-27 02:49:53 +04:00
|
|
|
if ($lines) {
|
|
|
|
foreach ($lines as $line) {
|
2013-08-30 16:14:38 +04:00
|
|
|
$hostKeyArray = explode("::", $line, 2);
|
|
|
|
if (count($hostKeyArray) == 2) {
|
|
|
|
$hosts[] = $hostKeyArray[0];
|
|
|
|
$keys[] = $hostKeyArray[1];
|
2012-12-27 02:49:53 +04:00
|
|
|
}
|
2012-12-27 02:38:17 +04:00
|
|
|
}
|
2012-12-27 02:49:53 +04:00
|
|
|
return array_combine($hosts, $keys);
|
2012-12-27 02:38:17 +04:00
|
|
|
}
|
|
|
|
}
|
2013-02-10 15:35:49 +04:00
|
|
|
} catch (\Exception $e) {
|
2012-12-27 02:38:17 +04:00
|
|
|
}
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 20:59:08 +04:00
|
|
|
public function mkdir($path) {
|
2012-12-27 02:49:53 +04:00
|
|
|
try {
|
2014-10-21 18:18:44 +04:00
|
|
|
return $this->getConnection()->mkdir($this->absPath($path));
|
2013-02-10 15:35:49 +04:00
|
|
|
} catch (\Exception $e) {
|
2012-12-27 02:49:53 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-26 20:59:08 +04:00
|
|
|
}
|
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 20:59:08 +04:00
|
|
|
public function rmdir($path) {
|
2012-12-27 02:49:53 +04:00
|
|
|
try {
|
2015-06-18 18:40:38 +03:00
|
|
|
$result = $this->getConnection()->delete($this->absPath($path), true);
|
|
|
|
// workaround: stray stat cache entry when deleting empty folders
|
|
|
|
// see https://github.com/phpseclib/phpseclib/issues/706
|
|
|
|
$this->getConnection()->clearStatCache();
|
|
|
|
return $result;
|
2013-02-10 15:35:49 +04:00
|
|
|
} catch (\Exception $e) {
|
2012-12-27 02:49:53 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-28 00:00:20 +04:00
|
|
|
}
|
2012-12-26 20:59:08 +04:00
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 20:59:08 +04:00
|
|
|
public function opendir($path) {
|
|
|
|
try {
|
2014-10-21 18:18:44 +04:00
|
|
|
$list = $this->getConnection()->nlist($this->absPath($path));
|
2014-08-27 12:02:14 +04:00
|
|
|
if ($list === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-27 02:49:53 +04:00
|
|
|
|
2012-12-26 20:59:08 +04:00
|
|
|
$id = md5('sftp:' . $path);
|
2013-08-30 16:14:38 +04:00
|
|
|
$dirStream = array();
|
2012-12-26 20:59:08 +04:00
|
|
|
foreach($list as $file) {
|
|
|
|
if ($file != '.' && $file != '..') {
|
2013-08-30 16:14:38 +04:00
|
|
|
$dirStream[] = $file;
|
2012-12-26 20:59:08 +04:00
|
|
|
}
|
|
|
|
}
|
2015-03-10 18:30:13 +03:00
|
|
|
return IteratorDirectory::wrap($dirStream);
|
2013-02-10 15:35:49 +04:00
|
|
|
} catch(\Exception $e) {
|
2012-12-26 20:59:08 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 20:59:08 +04:00
|
|
|
public function filetype($path) {
|
2012-12-27 02:49:53 +04:00
|
|
|
try {
|
2014-10-21 18:18:44 +04:00
|
|
|
$stat = $this->getConnection()->stat($this->absPath($path));
|
2013-08-30 16:14:38 +04:00
|
|
|
if ($stat['type'] == NET_SFTP_TYPE_REGULAR) {
|
|
|
|
return 'file';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) {
|
|
|
|
return 'dir';
|
|
|
|
}
|
2015-02-24 15:26:23 +03:00
|
|
|
} catch (\Exception $e) {
|
2013-08-30 16:14:38 +04:00
|
|
|
|
2012-12-27 02:49:53 +04:00
|
|
|
}
|
2012-12-26 20:59:08 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 20:59:08 +04:00
|
|
|
public function file_exists($path) {
|
2012-12-27 02:49:53 +04:00
|
|
|
try {
|
2014-10-21 18:18:44 +04:00
|
|
|
return $this->getConnection()->stat($this->absPath($path)) !== false;
|
2013-02-10 15:35:49 +04:00
|
|
|
} catch (\Exception $e) {
|
2012-12-27 02:49:53 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-26 20:59:08 +04:00
|
|
|
}
|
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 20:59:08 +04:00
|
|
|
public function unlink($path) {
|
2012-12-27 02:49:53 +04:00
|
|
|
try {
|
2014-10-21 18:18:44 +04:00
|
|
|
return $this->getConnection()->delete($this->absPath($path), true);
|
2013-02-10 15:35:49 +04:00
|
|
|
} catch (\Exception $e) {
|
2012-12-27 02:49:53 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-26 20:59:08 +04:00
|
|
|
}
|
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 20:59:08 +04:00
|
|
|
public function fopen($path, $mode) {
|
2012-12-27 02:49:53 +04:00
|
|
|
try {
|
2013-08-30 16:14:38 +04:00
|
|
|
$absPath = $this->absPath($path);
|
2012-12-27 02:49:53 +04:00
|
|
|
switch($mode) {
|
|
|
|
case 'r':
|
|
|
|
case 'rb':
|
2013-08-30 16:14:38 +04:00
|
|
|
if ( !$this->file_exists($path)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-27 02:49:53 +04:00
|
|
|
case 'w':
|
|
|
|
case 'wb':
|
|
|
|
case 'a':
|
|
|
|
case 'ab':
|
|
|
|
case 'r+':
|
|
|
|
case 'w+':
|
|
|
|
case 'wb+':
|
|
|
|
case 'a+':
|
|
|
|
case 'x':
|
|
|
|
case 'x+':
|
|
|
|
case 'c':
|
|
|
|
case 'c+':
|
2014-10-21 18:18:44 +04:00
|
|
|
$context = stream_context_create(array('sftp' => array('session' => $this->getConnection())));
|
2014-07-13 00:45:53 +04:00
|
|
|
return fopen($this->constructUrl($path), $mode, false, $context);
|
2012-12-27 02:49:53 +04:00
|
|
|
}
|
2013-02-10 15:35:49 +04:00
|
|
|
} catch (\Exception $e) {
|
2012-12-26 20:59:08 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 20:59:08 +04:00
|
|
|
public function touch($path, $mtime=null) {
|
2012-12-27 02:49:53 +04:00
|
|
|
try {
|
2013-08-30 16:14:38 +04:00
|
|
|
if (!is_null($mtime)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-27 02:49:53 +04:00
|
|
|
if (!$this->file_exists($path)) {
|
2014-10-21 18:18:44 +04:00
|
|
|
$this->getConnection()->put($this->absPath($path), '');
|
2013-02-10 02:29:58 +04:00
|
|
|
} else {
|
|
|
|
return false;
|
2012-12-27 02:49:53 +04:00
|
|
|
}
|
2013-02-10 15:35:49 +04:00
|
|
|
} catch (\Exception $e) {
|
2013-02-10 02:29:58 +04:00
|
|
|
return false;
|
2012-12-26 20:59:08 +04:00
|
|
|
}
|
2013-02-10 02:29:58 +04:00
|
|
|
return true;
|
2012-12-26 20:59:08 +04:00
|
|
|
}
|
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param string $target
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2012-12-26 20:59:08 +04:00
|
|
|
public function getFile($path, $target) {
|
2014-10-21 18:18:44 +04:00
|
|
|
$this->getConnection()->get($path, $target);
|
2012-12-26 20:59:08 +04:00
|
|
|
}
|
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param string $target
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2012-12-26 20:59:08 +04:00
|
|
|
public function uploadFile($path, $target) {
|
2014-10-21 18:18:44 +04:00
|
|
|
$this->getConnection()->put($target, $path, NET_SFTP_LOCAL_FILE);
|
2012-12-26 20:59:08 +04:00
|
|
|
}
|
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 20:59:08 +04:00
|
|
|
public function rename($source, $target) {
|
2012-12-27 02:49:53 +04:00
|
|
|
try {
|
2013-11-26 19:32:44 +04:00
|
|
|
if (!$this->is_dir($target) && $this->file_exists($target)) {
|
|
|
|
$this->unlink($target);
|
|
|
|
}
|
2014-10-21 18:18:44 +04:00
|
|
|
return $this->getConnection()->rename(
|
2013-08-30 16:14:38 +04:00
|
|
|
$this->absPath($source),
|
|
|
|
$this->absPath($target)
|
|
|
|
);
|
2013-02-10 15:35:49 +04:00
|
|
|
} catch (\Exception $e) {
|
2012-12-27 02:49:53 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-28 00:00:20 +04:00
|
|
|
}
|
2012-12-26 20:59:08 +04:00
|
|
|
|
2015-02-24 15:30:28 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 20:59:08 +04:00
|
|
|
public function stat($path) {
|
2012-12-27 02:49:53 +04:00
|
|
|
try {
|
2014-10-21 18:18:44 +04:00
|
|
|
$stat = $this->getConnection()->stat($this->absPath($path));
|
2012-12-27 02:49:53 +04:00
|
|
|
|
|
|
|
$mtime = $stat ? $stat['mtime'] : -1;
|
|
|
|
$size = $stat ? $stat['size'] : 0;
|
2012-12-26 20:59:08 +04:00
|
|
|
|
2012-12-27 02:49:53 +04:00
|
|
|
return array('mtime' => $mtime, 'size' => $size, 'ctime' => -1);
|
2013-02-10 15:35:49 +04:00
|
|
|
} catch (\Exception $e) {
|
2012-12-27 02:49:53 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-26 20:59:08 +04:00
|
|
|
}
|
2013-11-20 02:16:20 +04:00
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $path
|
2015-02-24 15:30:28 +03:00
|
|
|
* @return string
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2013-11-20 02:16:20 +04:00
|
|
|
public function constructUrl($path) {
|
2014-07-13 00:45:53 +04:00
|
|
|
// Do not pass the password here. We want to use the Net_SFTP object
|
|
|
|
// supplied via stream context or fail. We only supply username and
|
|
|
|
// hostname because this might show up in logs (they are not used).
|
2014-12-19 16:19:51 +03:00
|
|
|
$url = 'sftp://'.$this->user.'@'.$this->host.':'.$this->port.$this->root.$path;
|
2013-11-20 02:16:20 +04:00
|
|
|
return $url;
|
|
|
|
}
|
2012-12-26 20:59:08 +04:00
|
|
|
}
|