|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class SftpFile |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://www.icy2003.com/ |
|
6
|
|
|
* @author icy2003 <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2019, icy2003 |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace icy2003\php\icomponents\file; |
|
10
|
|
|
|
|
11
|
|
|
use icy2003\php\C; |
|
12
|
|
|
use icy2003\php\I; |
|
13
|
|
|
use icy2003\php\ihelpers\Arrays; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* sftp |
|
17
|
|
|
* |
|
18
|
|
|
* - 由于 PHP 里 sftp 提供的函数相对较少,为了让代码不那么诡异,这里用了很多 shell 命令 |
|
19
|
|
|
* - Windows 平台自求多福 |
|
20
|
|
|
*/ |
|
21
|
|
|
class SftpFile extends Base |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* ssh 连接 |
|
25
|
|
|
* |
|
26
|
|
|
* @var resource |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $_conn; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* sftp 对象 |
|
32
|
|
|
* |
|
33
|
|
|
* @var resource |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $_sftp; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* 构造函数 |
|
39
|
|
|
* |
|
40
|
|
|
* @param array $config sftp 连接配置。至少包括:host、username、password,可选:port、methods、callback |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct($config) |
|
43
|
|
|
{ |
|
44
|
|
|
C::assertFunction('ssh2_connect', '请开启 ssh2 扩展'); |
|
45
|
|
|
C::assertTrue(Arrays::keyExistsAll(['host', 'username', 'password'], $config, $diff), '缺少 ' . implode(',', $diff) . ' 参数'); |
|
46
|
|
|
$this->_conn = ssh2_connect( |
|
47
|
|
|
I::get($config, 'host'), |
|
|
|
|
|
|
48
|
|
|
I::get($config, 'port', 22), |
|
|
|
|
|
|
49
|
|
|
I::get($config, 'methods', []), |
|
|
|
|
|
|
50
|
|
|
I::get($config, 'callback', []) |
|
|
|
|
|
|
51
|
|
|
); |
|
52
|
|
|
C::assertNotFalse($this->_conn, '连接失败'); |
|
53
|
|
|
C::assertNotFalse(ssh2_auth_password($this->_conn, I::get($config, 'username'), I::get($config, 'password')), '账号密码错误'); |
|
|
|
|
|
|
54
|
|
|
$this->_sftp = ssh2_sftp($this->_conn); |
|
55
|
|
|
C::assertNotFalse($this->_sftp, '初始化 sftp 失败'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @ignore |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getCommandResult($command) |
|
62
|
|
|
{ |
|
63
|
|
|
$stream = ssh2_exec($this->_conn, $command); |
|
64
|
|
|
stream_set_blocking($stream, true); |
|
65
|
|
|
$result = stream_get_contents($stream); |
|
66
|
|
|
fclose($stream); |
|
67
|
|
|
return $result; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* 获取 ssh2 协议格式的文件路径 |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $file 文件(目录)的路径 |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
|
|
private function __getFilepath($file) |
|
78
|
|
|
{ |
|
79
|
|
|
return rtrim('ssh2.sftp://' . intval($this->_sftp), '/') . $file; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @ignore |
|
84
|
|
|
*/ |
|
85
|
|
|
public function isFile($file) |
|
86
|
|
|
{ |
|
87
|
|
|
return file_exists($this->__getFilepath($file)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @ignore |
|
92
|
|
|
*/ |
|
93
|
|
|
public function isDir($dir) |
|
94
|
|
|
{ |
|
95
|
|
|
return is_dir($this->__getFilepath($dir)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @ignore |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getRealpath($path) |
|
102
|
|
|
{ |
|
103
|
|
|
return ssh2_sftp_realpath($this->_sftp, $path); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @ignore |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getLists($dir = null, $flags = FileConstants::COMPLETE_PATH) |
|
110
|
|
|
{ |
|
111
|
|
|
$list = []; |
|
112
|
|
|
null === $dir && $dir = rtrim($this->getCommandResult('pwd'), PHP_EOL); |
|
113
|
|
|
$dir = rtrim($dir, '/') . '/'; |
|
114
|
|
|
$command = 'ls -m "' . $dir . '"'; |
|
115
|
|
|
$listString = rtrim($this->getCommandResult($command), PHP_EOL); |
|
116
|
|
|
$files = explode(', ', $listString); |
|
117
|
|
|
foreach ($files as $file) { |
|
118
|
|
|
$file = $this->getBasename($file); |
|
119
|
|
|
if (I::hasFlag($flags, FileConstants::RECURSIVE) && $this->isDir($dir . $file)) { |
|
120
|
|
|
$list = array_merge($list, $this->getLists($dir . $file, $flags)); |
|
121
|
|
|
} |
|
122
|
|
|
$list[] = I::hasFlag($flags, FileConstants::COMPLETE_PATH) ? $dir . $file : $file; |
|
123
|
|
|
} |
|
124
|
|
|
return $list; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @ignore |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getFilesize($file) |
|
131
|
|
|
{ |
|
132
|
|
|
return filesize($this->__getFilepath($file)); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @ignore |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getFileContent($file) |
|
139
|
|
|
{ |
|
140
|
|
|
return file_get_contents($this->__getFilepath($file)); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @ignore |
|
145
|
|
|
*/ |
|
146
|
|
|
public function putFileContent($file, $string, $mode = 0777) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->createDir($this->getDirname($file)); |
|
149
|
|
|
$isCreated = false !== file_put_contents($this->__getFilepath($file), $string); |
|
150
|
|
|
$this->chmod($file, $mode, FileConstants::RECURSIVE_DISABLED); |
|
151
|
|
|
return $isCreated; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @ignore |
|
156
|
|
|
*/ |
|
157
|
|
|
public function createDir($dir, $mode = 0777) |
|
158
|
|
|
{ |
|
159
|
|
|
return ssh2_sftp_mkdir($this->_sftp, $dir, $mode, true); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @ignore |
|
164
|
|
|
*/ |
|
165
|
|
|
public function deleteFile($file) |
|
166
|
|
|
{ |
|
167
|
|
|
if ($this->isFile($file)) { |
|
168
|
|
|
return ssh2_sftp_unlink($this->_sftp, $file); |
|
169
|
|
|
} |
|
170
|
|
|
return true; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @ignore |
|
175
|
|
|
*/ |
|
176
|
|
|
public function uploadFile($toFile, $fromFile = null, $overwrite = true) |
|
177
|
|
|
{ |
|
178
|
|
|
null === $fromFile && $fromFile = './' . $this->getBasename($toFile); |
|
179
|
|
|
if (false === $overwrite && $this->isFile($toFile)) { |
|
180
|
|
|
return false; |
|
181
|
|
|
} |
|
182
|
|
|
$this->createDir($this->getDirname($toFile)); |
|
183
|
|
|
return copy($fromFile, $this->__getFilepath($toFile)); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @ignore |
|
188
|
|
|
*/ |
|
189
|
|
|
public function downloadFile($fromFile, $toFile = null, $overwrite = true) |
|
190
|
|
|
{ |
|
191
|
|
|
null === $toFile && $toFile = './' . $this->getBasename($fromFile); |
|
192
|
|
|
if (false === $overwrite && $this->isFile($toFile)) { |
|
193
|
|
|
return false; |
|
194
|
|
|
} |
|
195
|
|
|
return copy($this->__getFilepath($fromFile), $toFile); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @ignore |
|
200
|
|
|
*/ |
|
201
|
|
|
public function chown($file, $user, $flags = FileConstants::RECURSIVE_DISABLED) |
|
202
|
|
|
{ |
|
203
|
|
|
return false; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @ignore |
|
208
|
|
|
*/ |
|
209
|
|
|
public function chgrp($file, $group, $flags = FileConstants::RECURSIVE_DISABLED) |
|
210
|
|
|
{ |
|
211
|
|
|
return false; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @ignore |
|
216
|
|
|
*/ |
|
217
|
|
|
public function chmod($file, $mode = 0777, $flags = FileConstants::RECURSIVE_DISABLED) |
|
218
|
|
|
{ |
|
219
|
|
|
if ($this->isDir($file) && I::hasFlag($flags, FileConstants::RECURSIVE)) { |
|
220
|
|
|
$files = $this->getLists($file, FileConstants::COMPLETE_PATH | FileConstants::RECURSIVE); |
|
221
|
|
|
foreach ($files as $subFile) { |
|
222
|
|
|
@ssh2_sftp_chmod($this->_sftp, $subFile, $mode); |
|
|
|
|
|
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
return @ssh2_sftp_chmod($this->_sftp, $subFile, $mode); |
|
|
|
|
|
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @ignore |
|
230
|
|
|
*/ |
|
231
|
|
|
public function symlink($from, $to) |
|
232
|
|
|
{ |
|
233
|
|
|
return ssh2_sftp_symlink($this->_sftp, $from, $to); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @ignore |
|
238
|
|
|
*/ |
|
239
|
|
|
public function close() |
|
240
|
|
|
{ |
|
241
|
|
|
return ssh2_disconnect($this->_conn); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @ignore |
|
246
|
|
|
*/ |
|
247
|
|
|
protected function _copy($fromFile, $toFile) |
|
248
|
|
|
{ |
|
249
|
|
|
return copy($this->__getFilepath($fromFile), $this->__getFilepath($toFile)); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @ignore |
|
254
|
|
|
*/ |
|
255
|
|
|
protected function _move($fromFile, $toFile) |
|
256
|
|
|
{ |
|
257
|
|
|
return ssh2_sftp_rename($this->_sftp, $fromFile, $toFile); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* @ignore |
|
262
|
|
|
*/ |
|
263
|
|
|
protected function _mkdir($dir, $mode = 0777) |
|
264
|
|
|
{ |
|
265
|
|
|
return ssh2_sftp_mkdir($this->_sftp, $dir, $mode); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @ignore |
|
270
|
|
|
*/ |
|
271
|
|
|
protected function _rmdir($dir) |
|
272
|
|
|
{ |
|
273
|
|
|
return ssh2_sftp_rmdir($this->_sftp, $dir); |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|