2012-12-26 16:59:08 +00:00
|
|
|
<?php
|
2012-12-27 18:47:46 +00:00
|
|
|
/**
|
2016-07-21 14:49:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Andreas Fischer <bantu@owncloud.com>
|
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author hkjolhede <hkjolhede@gmail.com>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Lennart Rosam <lennart.rosam@medien-systempartner.de>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 16:13:36 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 14:02:16 +00:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Ross Nicoll <jrn@jrn.me.uk>
|
|
|
|
* @author SA <stephen@mthosting.net>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Senorsen <senorsen.zhang@gmail.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @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 18:47:46 +00:00
|
|
|
*/
|
2016-04-13 22:18:07 +00:00
|
|
|
namespace OCA\Files_External\Lib\Storage;
|
2015-03-10 15:30:13 +00:00
|
|
|
use Icewind\Streams\IteratorDirectory;
|
2016-03-30 20:45:09 +00:00
|
|
|
use Icewind\Streams\RetryWrapper;
|
2015-08-03 04:39:53 +00:00
|
|
|
use phpseclib\Net\SFTP\Stream;
|
|
|
|
|
2014-07-12 11:58:49 +00:00
|
|
|
/**
|
2015-08-03 04:39:53 +00:00
|
|
|
* Uses phpseclib's Net\SFTP class and the Net\SFTP\Stream stream wrapper to
|
2014-07-12 11:58:49 +00:00
|
|
|
* provide access to SFTP servers.
|
|
|
|
*/
|
2013-02-10 11:35:49 +00:00
|
|
|
class SFTP extends \OC\Files\Storage\Common {
|
2012-12-26 22:38:17 +00:00
|
|
|
private $host;
|
2012-12-26 16:59:08 +00:00
|
|
|
private $user;
|
|
|
|
private $root;
|
2014-12-19 13:19:51 +00:00
|
|
|
private $port = 22;
|
2012-12-26 16:59:08 +00:00
|
|
|
|
2015-08-12 21:20:08 +00:00
|
|
|
private $auth;
|
|
|
|
|
2014-07-12 20:45:53 +00:00
|
|
|
/**
|
2016-07-05 21:18:41 +00:00
|
|
|
* @var \phpseclib\Net\SFTP
|
|
|
|
*/
|
2014-12-19 17:23:24 +00:00
|
|
|
protected $client;
|
2012-12-26 16:59:08 +00:00
|
|
|
|
2015-11-16 13:56:03 +00: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 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 16:59:08 +00:00
|
|
|
public function __construct($params) {
|
2015-02-03 15:02:07 +00:00
|
|
|
// Register sftp://
|
2015-08-03 04:39:53 +00:00
|
|
|
Stream::register();
|
2014-07-12 11:57:27 +00:00
|
|
|
|
2015-11-16 13:56:03 +00:00
|
|
|
$parsedHost = $this->splitHost($params['host']);
|
2015-08-03 04:39:53 +00:00
|
|
|
|
2015-11-16 13:56:03 +00:00
|
|
|
$this->host = $parsedHost[0];
|
|
|
|
$this->port = $parsedHost[1];
|
2014-12-19 13:19:51 +00:00
|
|
|
|
2016-01-26 13:48:45 +00:00
|
|
|
if (!isset($params['user'])) {
|
|
|
|
throw new \UnexpectedValueException('no authentication parameters specified');
|
|
|
|
}
|
2012-12-26 16:59:08 +00:00
|
|
|
$this->user = $params['user'];
|
2015-08-12 21:20:08 +00: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 12:14:38 +00:00
|
|
|
$this->root
|
|
|
|
= isset($params['root']) ? $this->cleanPath($params['root']) : '/';
|
2013-02-22 16:21:57 +00:00
|
|
|
|
2018-01-25 21:26:47 +00:00
|
|
|
$this->root = '/' . ltrim($this->root, '/');
|
|
|
|
$this->root = rtrim($this->root, '/') . '/';
|
2014-10-21 14:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the connection.
|
|
|
|
*
|
2015-08-05 08:28:11 +00:00
|
|
|
* @return \phpseclib\Net\SFTP connected client instance
|
2014-10-21 14:18:44 +00:00
|
|
|
* @throws \Exception when the connection failed
|
|
|
|
*/
|
|
|
|
public function getConnection() {
|
|
|
|
if (!is_null($this->client)) {
|
|
|
|
return $this->client;
|
|
|
|
}
|
2012-12-26 22:38:17 +00:00
|
|
|
|
2013-08-30 12:14:38 +00:00
|
|
|
$hostKeys = $this->readHostKeys();
|
2015-08-05 08:28:11 +00:00
|
|
|
$this->client = new \phpseclib\Net\SFTP($this->host, $this->port);
|
2013-08-30 12:14:38 +00:00
|
|
|
|
2014-07-12 18:41:35 +00:00
|
|
|
// The SSH Host Key MUST be verified before login().
|
2013-08-30 12:14:38 +00:00
|
|
|
$currentHostKey = $this->client->getServerPublicHostKey();
|
|
|
|
if (array_key_exists($this->host, $hostKeys)) {
|
2017-05-10 12:12:58 +00:00
|
|
|
if ($hostKeys[$this->host] !== $currentHostKey) {
|
2013-02-10 11:35:49 +00:00
|
|
|
throw new \Exception('Host public key does not match known key');
|
2012-12-26 22:38:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-08-30 12:14:38 +00:00
|
|
|
$hostKeys[$this->host] = $currentHostKey;
|
|
|
|
$this->writeHostKeys($hostKeys);
|
2012-12-26 16:59:08 +00:00
|
|
|
}
|
2014-07-12 18:41:35 +00:00
|
|
|
|
2015-08-12 21:20:08 +00:00
|
|
|
if (!$this->client->login($this->user, $this->auth)) {
|
2014-07-12 18:41:35 +00:00
|
|
|
throw new \Exception('Login failed');
|
|
|
|
}
|
2014-10-21 14:18:44 +00:00
|
|
|
return $this->client;
|
2012-12-26 16:59:08 +00:00
|
|
|
}
|
2013-02-22 16:21:57 +00:00
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-28 21:22:04 +00:00
|
|
|
public function test() {
|
2013-08-30 12:14:38 +00:00
|
|
|
if (
|
|
|
|
!isset($this->host)
|
|
|
|
|| !isset($this->user)
|
|
|
|
) {
|
2013-08-30 10:10:10 +00:00
|
|
|
return false;
|
2013-02-22 16:21:57 +00:00
|
|
|
}
|
2014-10-21 14:18:44 +00:00
|
|
|
return $this->getConnection()->nlist() !== false;
|
2012-12-28 21:22:04 +00:00
|
|
|
}
|
2013-02-22 16:21:57 +00:00
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2013-02-10 12:11:29 +00:00
|
|
|
public function getId(){
|
2015-04-15 14:21:41 +00: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 12:11:29 +00:00
|
|
|
}
|
2012-12-26 16:59:08 +00:00
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-12-19 17:23:24 +00:00
|
|
|
public function getHost() {
|
|
|
|
return $this->host;
|
|
|
|
}
|
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-12-19 17:23:24 +00:00
|
|
|
public function getRoot() {
|
|
|
|
return $this->root;
|
|
|
|
}
|
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2014-12-19 17:23:24 +00:00
|
|
|
public function getUser() {
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
|
|
|
* @param string $path
|
2015-02-24 12:30:28 +00:00
|
|
|
* @return string
|
2014-02-06 15:30:58 +00:00
|
|
|
*/
|
2013-08-30 12:14:38 +00:00
|
|
|
private function absPath($path) {
|
2013-02-09 22:49:21 +00:00
|
|
|
return $this->root . $this->cleanPath($path);
|
2012-12-26 16:59:08 +00:00
|
|
|
}
|
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
2015-11-30 10:29:06 +00:00
|
|
|
* @return string|false
|
2015-02-24 12:30:28 +00:00
|
|
|
*/
|
2013-08-30 12:14:38 +00:00
|
|
|
private function hostKeysPath() {
|
2012-12-26 22:49:53 +00:00
|
|
|
try {
|
|
|
|
$storage_view = \OCP\Files::getStorage('files_external');
|
|
|
|
if ($storage_view) {
|
2017-01-20 01:49:41 +00:00
|
|
|
return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') .
|
2012-12-26 22:49:53 +00:00
|
|
|
$storage_view->getAbsolutePath('') .
|
2013-08-30 12:14:38 +00:00
|
|
|
'ssh_hostKeys';
|
2012-12-26 22:49:53 +00:00
|
|
|
}
|
2013-02-10 11:35:49 +00:00
|
|
|
} catch (\Exception $e) {
|
2012-12-26 22:38:17 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* @param $keys
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-12-19 17:23:24 +00:00
|
|
|
protected function writeHostKeys($keys) {
|
2012-12-26 22:38:17 +00:00
|
|
|
try {
|
2013-08-30 12:14:38 +00:00
|
|
|
$keyPath = $this->hostKeysPath();
|
2013-11-15 20:10:09 +00: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-26 22:38:17 +00:00
|
|
|
}
|
2013-02-10 11:35:49 +00:00
|
|
|
} catch (\Exception $e) {
|
2012-12-26 22:38:17 +00:00
|
|
|
}
|
2013-11-15 20:10:09 +00:00
|
|
|
return false;
|
2012-12-26 22:38:17 +00:00
|
|
|
}
|
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2014-12-19 17:23:24 +00:00
|
|
|
protected function readHostKeys() {
|
2012-12-26 22:49:53 +00:00
|
|
|
try {
|
2013-08-30 12:14:38 +00:00
|
|
|
$keyPath = $this->hostKeysPath();
|
|
|
|
if (file_exists($keyPath)) {
|
2012-12-26 22:49:53 +00:00
|
|
|
$hosts = array();
|
|
|
|
$keys = array();
|
2013-08-30 12:14:38 +00:00
|
|
|
$lines = file($keyPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
2012-12-26 22:49:53 +00:00
|
|
|
if ($lines) {
|
|
|
|
foreach ($lines as $line) {
|
2013-08-30 12:14:38 +00:00
|
|
|
$hostKeyArray = explode("::", $line, 2);
|
2017-05-10 12:12:58 +00:00
|
|
|
if (count($hostKeyArray) === 2) {
|
2013-08-30 12:14:38 +00:00
|
|
|
$hosts[] = $hostKeyArray[0];
|
|
|
|
$keys[] = $hostKeyArray[1];
|
2012-12-26 22:49:53 +00:00
|
|
|
}
|
2012-12-26 22:38:17 +00:00
|
|
|
}
|
2012-12-26 22:49:53 +00:00
|
|
|
return array_combine($hosts, $keys);
|
2012-12-26 22:38:17 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-10 11:35:49 +00:00
|
|
|
} catch (\Exception $e) {
|
2012-12-26 22:38:17 +00:00
|
|
|
}
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 16:59:08 +00:00
|
|
|
public function mkdir($path) {
|
2012-12-26 22:49:53 +00:00
|
|
|
try {
|
2014-10-21 14:18:44 +00:00
|
|
|
return $this->getConnection()->mkdir($this->absPath($path));
|
2013-02-10 11:35:49 +00:00
|
|
|
} catch (\Exception $e) {
|
2012-12-26 22:49:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-26 16:59:08 +00:00
|
|
|
}
|
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 16:59:08 +00:00
|
|
|
public function rmdir($path) {
|
2012-12-26 22:49:53 +00:00
|
|
|
try {
|
2015-06-18 15:40:38 +00: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 11:35:49 +00:00
|
|
|
} catch (\Exception $e) {
|
2012-12-26 22:49:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-27 20:00:20 +00:00
|
|
|
}
|
2012-12-26 16:59:08 +00:00
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 16:59:08 +00:00
|
|
|
public function opendir($path) {
|
|
|
|
try {
|
2014-10-21 14:18:44 +00:00
|
|
|
$list = $this->getConnection()->nlist($this->absPath($path));
|
2014-08-27 08:02:14 +00:00
|
|
|
if ($list === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-26 22:49:53 +00:00
|
|
|
|
2012-12-26 16:59:08 +00:00
|
|
|
$id = md5('sftp:' . $path);
|
2013-08-30 12:14:38 +00:00
|
|
|
$dirStream = array();
|
2012-12-26 16:59:08 +00:00
|
|
|
foreach($list as $file) {
|
2017-05-10 12:12:58 +00:00
|
|
|
if ($file !== '.' && $file !== '..') {
|
2013-08-30 12:14:38 +00:00
|
|
|
$dirStream[] = $file;
|
2012-12-26 16:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-10 15:30:13 +00:00
|
|
|
return IteratorDirectory::wrap($dirStream);
|
2013-02-10 11:35:49 +00:00
|
|
|
} catch(\Exception $e) {
|
2012-12-26 16:59:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 16:59:08 +00:00
|
|
|
public function filetype($path) {
|
2012-12-26 22:49:53 +00:00
|
|
|
try {
|
2014-10-21 14:18:44 +00:00
|
|
|
$stat = $this->getConnection()->stat($this->absPath($path));
|
2017-05-10 12:12:58 +00:00
|
|
|
if ((int) $stat['type'] === NET_SFTP_TYPE_REGULAR) {
|
2013-08-30 12:14:38 +00:00
|
|
|
return 'file';
|
|
|
|
}
|
|
|
|
|
2017-05-10 12:12:58 +00:00
|
|
|
if ((int) $stat['type'] === NET_SFTP_TYPE_DIRECTORY) {
|
2013-08-30 12:14:38 +00:00
|
|
|
return 'dir';
|
|
|
|
}
|
2015-02-24 12:26:23 +00:00
|
|
|
} catch (\Exception $e) {
|
2013-08-30 12:14:38 +00:00
|
|
|
|
2012-12-26 22:49:53 +00:00
|
|
|
}
|
2012-12-26 16:59:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 16:59:08 +00:00
|
|
|
public function file_exists($path) {
|
2012-12-26 22:49:53 +00:00
|
|
|
try {
|
2014-10-21 14:18:44 +00:00
|
|
|
return $this->getConnection()->stat($this->absPath($path)) !== false;
|
2013-02-10 11:35:49 +00:00
|
|
|
} catch (\Exception $e) {
|
2012-12-26 22:49:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-26 16:59:08 +00:00
|
|
|
}
|
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 16:59:08 +00:00
|
|
|
public function unlink($path) {
|
2012-12-26 22:49:53 +00:00
|
|
|
try {
|
2014-10-21 14:18:44 +00:00
|
|
|
return $this->getConnection()->delete($this->absPath($path), true);
|
2013-02-10 11:35:49 +00:00
|
|
|
} catch (\Exception $e) {
|
2012-12-26 22:49:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-26 16:59:08 +00:00
|
|
|
}
|
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 16:59:08 +00:00
|
|
|
public function fopen($path, $mode) {
|
2012-12-26 22:49:53 +00:00
|
|
|
try {
|
2013-08-30 12:14:38 +00:00
|
|
|
$absPath = $this->absPath($path);
|
2012-12-26 22:49:53 +00:00
|
|
|
switch($mode) {
|
|
|
|
case 'r':
|
|
|
|
case 'rb':
|
2013-08-30 12:14:38 +00:00
|
|
|
if ( !$this->file_exists($path)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-26 22:49:53 +00: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 14:18:44 +00:00
|
|
|
$context = stream_context_create(array('sftp' => array('session' => $this->getConnection())));
|
2016-03-30 20:45:09 +00:00
|
|
|
$handle = fopen($this->constructUrl($path), $mode, false, $context);
|
|
|
|
return RetryWrapper::wrap($handle);
|
2012-12-26 22:49:53 +00:00
|
|
|
}
|
2013-02-10 11:35:49 +00:00
|
|
|
} catch (\Exception $e) {
|
2012-12-26 16:59:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 16:59:08 +00:00
|
|
|
public function touch($path, $mtime=null) {
|
2012-12-26 22:49:53 +00:00
|
|
|
try {
|
2013-08-30 12:14:38 +00:00
|
|
|
if (!is_null($mtime)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-26 22:49:53 +00:00
|
|
|
if (!$this->file_exists($path)) {
|
2014-10-21 14:18:44 +00:00
|
|
|
$this->getConnection()->put($this->absPath($path), '');
|
2013-02-09 22:29:58 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
2012-12-26 22:49:53 +00:00
|
|
|
}
|
2013-02-10 11:35:49 +00:00
|
|
|
} catch (\Exception $e) {
|
2013-02-09 22:29:58 +00:00
|
|
|
return false;
|
2012-12-26 16:59:08 +00:00
|
|
|
}
|
2013-02-09 22:29:58 +00:00
|
|
|
return true;
|
2012-12-26 16:59:08 +00:00
|
|
|
}
|
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param string $target
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2012-12-26 16:59:08 +00:00
|
|
|
public function getFile($path, $target) {
|
2014-10-21 14:18:44 +00:00
|
|
|
$this->getConnection()->get($path, $target);
|
2012-12-26 16:59:08 +00:00
|
|
|
}
|
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param string $target
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2012-12-26 16:59:08 +00:00
|
|
|
public function uploadFile($path, $target) {
|
2014-10-21 14:18:44 +00:00
|
|
|
$this->getConnection()->put($target, $path, NET_SFTP_LOCAL_FILE);
|
2012-12-26 16:59:08 +00:00
|
|
|
}
|
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 16:59:08 +00:00
|
|
|
public function rename($source, $target) {
|
2012-12-26 22:49:53 +00:00
|
|
|
try {
|
2016-07-01 15:24:57 +00:00
|
|
|
if ($this->file_exists($target)) {
|
2013-11-26 15:32:44 +00:00
|
|
|
$this->unlink($target);
|
|
|
|
}
|
2014-10-21 14:18:44 +00:00
|
|
|
return $this->getConnection()->rename(
|
2013-08-30 12:14:38 +00:00
|
|
|
$this->absPath($source),
|
|
|
|
$this->absPath($target)
|
|
|
|
);
|
2013-02-10 11:35:49 +00:00
|
|
|
} catch (\Exception $e) {
|
2012-12-26 22:49:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-27 20:00:20 +00:00
|
|
|
}
|
2012-12-26 16:59:08 +00:00
|
|
|
|
2015-02-24 12:30:28 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-26 16:59:08 +00:00
|
|
|
public function stat($path) {
|
2012-12-26 22:49:53 +00:00
|
|
|
try {
|
2014-10-21 14:18:44 +00:00
|
|
|
$stat = $this->getConnection()->stat($this->absPath($path));
|
2012-12-26 22:49:53 +00:00
|
|
|
|
|
|
|
$mtime = $stat ? $stat['mtime'] : -1;
|
|
|
|
$size = $stat ? $stat['size'] : 0;
|
2012-12-26 16:59:08 +00:00
|
|
|
|
2012-12-26 22:49:53 +00:00
|
|
|
return array('mtime' => $mtime, 'size' => $size, 'ctime' => -1);
|
2013-02-10 11:35:49 +00:00
|
|
|
} catch (\Exception $e) {
|
2012-12-26 22:49:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-26 16:59:08 +00:00
|
|
|
}
|
2013-11-19 22:16:20 +00:00
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
|
|
|
* @param string $path
|
2015-02-24 12:30:28 +00:00
|
|
|
* @return string
|
2014-02-06 15:30:58 +00:00
|
|
|
*/
|
2013-11-19 22:16:20 +00:00
|
|
|
public function constructUrl($path) {
|
2014-07-12 20:45:53 +00: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).
|
2016-03-15 17:49:40 +00:00
|
|
|
$url = 'sftp://' . urlencode($this->user) . '@' . $this->host . ':' . $this->port . $this->root . $path;
|
2013-11-19 22:16:20 +00:00
|
|
|
return $url;
|
|
|
|
}
|
2012-12-26 16:59:08 +00:00
|
|
|
}
|