nextcloud/apps/files_external/lib/sftp.php

310 lines
6.6 KiB
PHP
Raw Normal View History

<?php
/**
* 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 18:57:33 +04:00
namespace OC\Files\Storage;
2013-02-14 20:57:19 +04:00
set_include_path(get_include_path() . PATH_SEPARATOR .
\OC_App::getAppPath('files_external') . '/3rdparty/phpseclib/phpseclib');
require 'Net/SFTP.php';
class SFTP extends \OC\Files\Storage\Common {
private $host;
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 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 .= '/';
}
2013-08-30 16:14:38 +04:00
$hostKeys = $this->readHostKeys();
$this->client = new \Net_SFTP($this->host);
2013-08-30 16:14:38 +04:00
if (!$this->client->login($this->user, $this->password)) {
throw new \Exception('Login failed');
}
2013-08-30 16:14:38 +04:00
$currentHostKey = $this->client->getServerPublicHostKey();
2013-08-30 16:14:38 +04:00
if (array_key_exists($this->host, $hostKeys)) {
if ($hostKeys[$this->host] != $currentHostKey) {
throw new \Exception('Host public key does not match known key');
}
} else {
2013-08-30 16:14:38 +04:00
$hostKeys[$this->host] = $currentHostKey;
$this->writeHostKeys($hostKeys);
}
}
2013-02-22 20:21:57 +04:00
public function test() {
2013-08-30 16:14:38 +04:00
if (
!isset($this->host)
|| !isset($this->user)
|| !isset($this->password)
) {
2013-08-30 14:10:10 +04:00
return false;
2013-02-22 20:21:57 +04:00
}
2013-08-30 16:14:38 +04:00
return $this->client->nlist() !== false;
}
2013-02-22 20:21:57 +04:00
2013-02-10 16:11:29 +04:00
public function getId(){
return 'sftp::' . $this->user . '@' . $this->host . '/' . $this->root;
}
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);
}
2013-08-30 16:14:38 +04:00
private function hostKeysPath() {
try {
$storage_view = \OCP\Files::getStorage('files_external');
if ($storage_view) {
return \OCP\Config::getSystemValue('datadirectory') .
$storage_view->getAbsolutePath('') .
2013-08-30 16:14:38 +04:00
'ssh_hostKeys';
}
} catch (\Exception $e) {
}
return false;
}
2013-08-30 16:14:38 +04:00
private function writeHostKeys($keys) {
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;
}
} catch (\Exception $e) {
}
2013-11-16 00:10:09 +04:00
return false;
}
2013-08-30 16:14:38 +04:00
private function readHostKeys() {
try {
2013-08-30 16:14:38 +04:00
$keyPath = $this->hostKeysPath();
if (file_exists($keyPath)) {
$hosts = array();
$keys = array();
2013-08-30 16:14:38 +04:00
$lines = file($keyPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
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];
}
}
return array_combine($hosts, $keys);
}
}
} catch (\Exception $e) {
}
return array();
}
public function mkdir($path) {
try {
2013-08-30 16:14:38 +04:00
return $this->client->mkdir($this->absPath($path));
} catch (\Exception $e) {
return false;
}
}
public function rmdir($path) {
try {
2013-08-30 16:14:38 +04:00
return $this->client->delete($this->absPath($path), true);
} catch (\Exception $e) {
return false;
}
}
public function opendir($path) {
try {
2013-08-30 16:14:38 +04:00
$list = $this->client->nlist($this->absPath($path));
$id = md5('sftp:' . $path);
2013-08-30 16:14:38 +04:00
$dirStream = array();
foreach($list as $file) {
if ($file != '.' && $file != '..') {
2013-08-30 16:14:38 +04:00
$dirStream[] = $file;
}
}
2013-08-30 16:14:38 +04:00
\OC\Files\Stream\Dir::register($id, $dirStream);
return opendir('fakedir://' . $id);
} catch(\Exception $e) {
return false;
}
}
public function filetype($path) {
try {
2013-08-30 16:14:38 +04: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';
}
} catch (\Exeption $e) {
2013-08-30 16:14:38 +04:00
}
return false;
}
public function file_exists($path) {
try {
2013-08-30 16:14:38 +04:00
return $this->client->stat($this->absPath($path)) !== false;
} catch (\Exception $e) {
return false;
}
}
public function unlink($path) {
try {
2013-08-30 16:14:38 +04:00
return $this->client->delete($this->absPath($path), true);
} catch (\Exception $e) {
return false;
}
}
public function fopen($path, $mode) {
try {
2013-08-30 16:14:38 +04:00
$absPath = $this->absPath($path);
switch($mode) {
case 'r':
case 'rb':
2013-08-30 16:14:38 +04:00
if ( !$this->file_exists($path)) {
return false;
}
if (strrpos($path, '.')!==false) {
$ext=substr($path, strrpos($path, '.'));
} else {
$ext='';
}
$tmp = \OC_Helper::tmpFile($ext);
2013-08-30 16:14:38 +04:00
$this->getFile($absPath, $tmp);
return fopen($tmp, $mode);
2013-02-22 20:21:57 +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+':
if (strrpos($path, '.')!==false) {
$ext=substr($path, strrpos($path, '.'));
} else {
$ext='';
}
2013-08-30 16:14:38 +04:00
$tmpFile=\OC_Helper::tmpFile($ext);
2013-08-30 16:14:38 +04:00
\OC\Files\Stream\Close::registerCallback(
$tmpFile,
array($this, 'writeBack')
);
if ($this->file_exists($path)) {
2013-08-30 16:14:38 +04:00
$this->getFile($absPath, $tmpFile);
}
2013-08-30 16:14:38 +04:00
self::$tempFiles[$tmpFile]=$absPath;
return fopen('close://'.$tmpFile, $mode);
}
} catch (\Exception $e) {
}
return false;
}
public function writeBack($tmpFile) {
if (array_key_exists($tmpFile, self::$tempFiles)) {
$this->uploadFile($tmpFile, self::$tempFiles[$tmpFile]);
unlink($tmpFile);
unset(self::$tempFiles[$tmpFile]);
}
}
public function touch($path, $mtime=null) {
try {
2013-08-30 16:14:38 +04:00
if (!is_null($mtime)) {
return false;
}
if (!$this->file_exists($path)) {
2013-08-30 16:14:38 +04:00
$this->client->put($this->absPath($path), '');
2013-02-10 02:29:58 +04:00
} else {
return false;
}
} catch (\Exception $e) {
2013-02-10 02:29:58 +04:00
return false;
}
2013-02-10 02:29:58 +04:00
return true;
}
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) {
try {
2013-08-30 16:14:38 +04:00
return $this->client->rename(
$this->absPath($source),
$this->absPath($target)
);
} catch (\Exception $e) {
return false;
}
}
public function stat($path) {
try {
2013-08-30 16:14:38 +04:00
$stat = $this->client->stat($this->absPath($path));
$mtime = $stat ? $stat['mtime'] : -1;
$size = $stat ? $stat['size'] : 0;
return array('mtime' => $mtime, 'size' => $size, 'ctime' => -1);
} catch (\Exception $e) {
return false;
}
}
}