* This file is licensed under the Affero General Public License version 3 or * later. * See the COPYING-README file. */ namespace OC\Files\Storage; 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']; $this->root = isset($params['root']) ? $this->cleanPath($params['root']) : '/'; if ($this->root[0] != '/') { $this->root = '/' . $this->root; } if (substr($this->root, -1, 1) != '/') { $this->root .= '/'; } $hostKeys = $this->readHostKeys(); $this->client = new \Net_SFTP($this->host); if (!$this->client->login($this->user, $this->password)) { throw new \Exception('Login failed'); } $currentHostKey = $this->client->getServerPublicHostKey(); if (array_key_exists($this->host, $hostKeys)) { if ($hostKeys[$this->host] != $currentHostKey) { throw new \Exception('Host public key does not match known key'); } } else { $hostKeys[$this->host] = $currentHostKey; $this->writeHostKeys($hostKeys); } } public function test() { if ( !isset($this->host) || !isset($this->user) || !isset($this->password) ) { return false; } return $this->client->nlist() !== false; } public function getId(){ return 'sftp::' . $this->user . '@' . $this->host . '/' . $this->root; } private function absPath($path) { return $this->root . $this->cleanPath($path); } private function hostKeysPath() { try { $storage_view = \OCP\Files::getStorage('files_external'); if ($storage_view) { return \OCP\Config::getSystemValue('datadirectory') . $storage_view->getAbsolutePath('') . 'ssh_hostKeys'; } } catch (\Exception $e) { } return false; } private function writeHostKeys($keys) { try { $keyPath = $this->hostKeysPath(); 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) { } return false; } private function readHostKeys() { try { $keyPath = $this->hostKeysPath(); if (file_exists($keyPath)) { $hosts = array(); $keys = array(); $lines = file($keyPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); if ($lines) { foreach ($lines as $line) { $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 { return $this->client->mkdir($this->absPath($path)); } catch (\Exception $e) { return false; } } public function rmdir($path) { try { return $this->client->delete($this->absPath($path), true); } catch (\Exception $e) { return false; } } public function opendir($path) { try { $list = $this->client->nlist($this->absPath($path)); $id = md5('sftp:' . $path); $dirStream = array(); foreach($list as $file) { if ($file != '.' && $file != '..') { $dirStream[] = $file; } } \OC\Files\Stream\Dir::register($id, $dirStream); return opendir('fakedir://' . $id); } catch(\Exception $e) { return false; } } public function filetype($path) { try { $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) { } return false; } public function file_exists($path) { try { return $this->client->stat($this->absPath($path)) !== false; } catch (\Exception $e) { return false; } } public function unlink($path) { try { return $this->client->delete($this->absPath($path), true); } catch (\Exception $e) { return false; } } public function fopen($path, $mode) { try { $absPath = $this->absPath($path); switch($mode) { case 'r': case 'rb': if ( !$this->file_exists($path)) { return false; } if (strrpos($path, '.')!==false) { $ext=substr($path, strrpos($path, '.')); } else { $ext=''; } $tmp = \OC_Helper::tmpFile($ext); $this->getFile($absPath, $tmp); return fopen($tmp, $mode); 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=''; } $tmpFile=\OC_Helper::tmpFile($ext); \OC\Files\Stream\Close::registerCallback( $tmpFile, array($this, 'writeBack') ); if ($this->file_exists($path)) { $this->getFile($absPath, $tmpFile); } 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 { if (!is_null($mtime)) { return false; } if (!$this->file_exists($path)) { $this->client->put($this->absPath($path), ''); } else { return false; } } catch (\Exception $e) { return false; } 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 { return $this->client->rename( $this->absPath($source), $this->absPath($target) ); } catch (\Exception $e) { return false; } } public function stat($path) { try { $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; } } } orage_full_warning'>artonge/fix/storage_full_warning Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
summaryrefslogtreecommitdiffstats
path: root/lib/l10n/pl.js
blob: b59eedbf4b67c4c6d16e48c87031fd58cc55cc6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189