2012-12-26 16:59:08 +00:00
|
|
|
<?php
|
2012-12-27 18:47:46 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Henrik Kjölhede <hkjolhede@gmail.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
2013-02-09 14:57:33 +00:00
|
|
|
namespace OC\Files\Storage;
|
2012-12-27 18:47:46 +00:00
|
|
|
|
2013-02-14 16:57:19 +00:00
|
|
|
set_include_path(get_include_path() . PATH_SEPARATOR .
|
|
|
|
\OC_App::getAppPath('files_external') . '/3rdparty/phpseclib/phpseclib');
|
|
|
|
require 'Net/SFTP.php';
|
2013-11-19 22:16:20 +00:00
|
|
|
require 'Net/SFTP/Stream.php';
|
2012-12-26 16:59:08 +00:00
|
|
|
|
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 $password;
|
|
|
|
private $root;
|
|
|
|
|
|
|
|
private $client;
|
|
|
|
|
|
|
|
private static $tempFiles = array();
|
|
|
|
|
|
|
|
public function __construct($params) {
|
|
|
|
$this->host = $params['host'];
|
|
|
|
$proto = strpos($this->host, '://');
|
|
|
|
if ($proto != false) {
|
|
|
|
$this->host = substr($this->host, $proto+3);
|
|
|
|
}
|
|
|
|
$this->user = $params['user'];
|
|
|
|
$this->password = $params['password'];
|
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
|
|
|
|
2013-08-30 12:14:38 +00:00
|
|
|
if ($this->root[0] != '/') {
|
|
|
|
$this->root = '/' . $this->root;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (substr($this->root, -1, 1) != '/') {
|
|
|
|
$this->root .= '/';
|
|
|
|
}
|
2012-12-26 22:38:17 +00:00
|
|
|
|
2013-08-30 12:14:38 +00:00
|
|
|
$hostKeys = $this->readHostKeys();
|
2013-02-10 11:35:49 +00:00
|
|
|
$this->client = new \Net_SFTP($this->host);
|
2013-08-30 12:14:38 +00:00
|
|
|
|
2012-12-26 16:59:08 +00:00
|
|
|
if (!$this->client->login($this->user, $this->password)) {
|
2013-02-10 11:35:49 +00:00
|
|
|
throw new \Exception('Login failed');
|
2012-12-26 22:38:17 +00:00
|
|
|
}
|
|
|
|
|
2013-08-30 12:14:38 +00:00
|
|
|
$currentHostKey = $this->client->getServerPublicHostKey();
|
2012-12-26 22:38:17 +00:00
|
|
|
|
2013-08-30 12:14:38 +00:00
|
|
|
if (array_key_exists($this->host, $hostKeys)) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2013-02-22 16:21:57 +00:00
|
|
|
|
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)
|
|
|
|
|| !isset($this->password)
|
|
|
|
) {
|
2013-08-30 10:10:10 +00:00
|
|
|
return false;
|
2013-02-22 16:21:57 +00:00
|
|
|
}
|
2013-08-30 12:14:38 +00:00
|
|
|
return $this->client->nlist() !== false;
|
2012-12-28 21:22:04 +00:00
|
|
|
}
|
2013-02-22 16:21:57 +00:00
|
|
|
|
2013-02-10 12:11:29 +00:00
|
|
|
public function getId(){
|
|
|
|
return 'sftp::' . $this->user . '@' . $this->host . '/' . $this->root;
|
|
|
|
}
|
2012-12-26 16:59:08 +00:00
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return \OCP\Config::getSystemValue('datadirectory') .
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2013-08-30 12:14:38 +00:00
|
|
|
private 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
|
|
|
}
|
|
|
|
|
2013-08-30 12:14:38 +00:00
|
|
|
private 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);
|
|
|
|
if (count($hostKeyArray) == 2) {
|
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
2012-12-26 16:59:08 +00:00
|
|
|
public function mkdir($path) {
|
2012-12-26 22:49:53 +00:00
|
|
|
try {
|
2013-08-30 12:14:38 +00:00
|
|
|
return $this->client->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
|
|
|
}
|
|
|
|
|
|
|
|
public function rmdir($path) {
|
2012-12-26 22:49:53 +00:00
|
|
|
try {
|
2013-08-30 12:14:38 +00:00
|
|
|
return $this->client->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-27 20:00:20 +00:00
|
|
|
}
|
2012-12-26 16:59:08 +00:00
|
|
|
|
|
|
|
public function opendir($path) {
|
|
|
|
try {
|
2013-08-30 12:14:38 +00:00
|
|
|
$list = $this->client->nlist($this->absPath($path));
|
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) {
|
|
|
|
if ($file != '.' && $file != '..') {
|
2013-08-30 12:14:38 +00:00
|
|
|
$dirStream[] = $file;
|
2012-12-26 16:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-30 12:14:38 +00:00
|
|
|
\OC\Files\Stream\Dir::register($id, $dirStream);
|
2012-12-26 16:59:08 +00:00
|
|
|
return opendir('fakedir://' . $id);
|
2013-02-10 11:35:49 +00:00
|
|
|
} catch(\Exception $e) {
|
2012-12-26 16:59:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function filetype($path) {
|
2012-12-26 22:49:53 +00:00
|
|
|
try {
|
2013-08-30 12:14:38 +00:00
|
|
|
$stat = $this->client->stat($this->absPath($path));
|
|
|
|
if ($stat['type'] == NET_SFTP_TYPE_REGULAR) {
|
|
|
|
return 'file';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) {
|
|
|
|
return 'dir';
|
|
|
|
}
|
2013-02-10 11:35:49 +00:00
|
|
|
} catch (\Exeption $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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function file_exists($path) {
|
2012-12-26 22:49:53 +00:00
|
|
|
try {
|
2013-08-30 12:14:38 +00:00
|
|
|
return $this->client->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
|
|
|
}
|
|
|
|
|
|
|
|
public function unlink($path) {
|
2012-12-26 22:49:53 +00:00
|
|
|
try {
|
2013-08-30 12:14:38 +00:00
|
|
|
return $this->client->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
|
|
|
}
|
|
|
|
|
|
|
|
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+':
|
2013-11-19 22:16:20 +00:00
|
|
|
// FIXME: make client login lazy to prevent it when using fopen()
|
|
|
|
return fopen($this->constructUrl($path), $mode);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
2013-08-30 12:14:38 +00:00
|
|
|
$this->client->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
|
|
|
}
|
|
|
|
|
|
|
|
public function getFile($path, $target) {
|
|
|
|
$this->client->get($path, $target);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function uploadFile($path, $target) {
|
|
|
|
$this->client->put($target, $path, NET_SFTP_LOCAL_FILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rename($source, $target) {
|
2012-12-26 22:49:53 +00:00
|
|
|
try {
|
2013-11-26 15:32:44 +00:00
|
|
|
if (!$this->is_dir($target) && $this->file_exists($target)) {
|
|
|
|
$this->unlink($target);
|
|
|
|
}
|
2013-08-30 12:14:38 +00:00
|
|
|
return $this->client->rename(
|
|
|
|
$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
|
|
|
|
|
|
|
public function stat($path) {
|
2012-12-26 22:49:53 +00:00
|
|
|
try {
|
2013-08-30 12:14:38 +00:00
|
|
|
$stat = $this->client->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
|
|
|
|
*/
|
2013-11-19 22:16:20 +00:00
|
|
|
public function constructUrl($path) {
|
|
|
|
$url = 'sftp://'.$this->user.':'.$this->password.'@'.$this->host.$this->root.$path;
|
|
|
|
return $url;
|
|
|
|
}
|
2012-12-26 16:59:08 +00:00
|
|
|
}
|