1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace diecoding\flysystem; |
4
|
|
|
|
5
|
|
|
use diecoding\flysystem\adapter\FtpAdapter; |
6
|
|
|
use diecoding\flysystem\traits\UrlGeneratorComponentTrait; |
7
|
|
|
use League\Flysystem\Ftp\FtpConnectionOptions; |
8
|
|
|
use League\Flysystem\PathPrefixing\PathPrefixedAdapter; |
9
|
|
|
use Yii; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Interacting with an FTP filesystem |
13
|
|
|
* @see https://flysystem.thephpleague.com/docs/adapter/ftp/ |
14
|
|
|
* |
15
|
|
|
* ```php |
16
|
|
|
* 'components' => [ |
17
|
|
|
* 'fs' => [ |
18
|
|
|
* 'class' => \diecoding\flysystem\FtpComponent::class, |
19
|
|
|
* 'host' => 'hostname', |
20
|
|
|
* 'root' => '/root/path/', // or you can use @alias |
21
|
|
|
* 'username' => 'username', |
22
|
|
|
* 'password' => 'password', |
23
|
|
|
* // 'port' => 21, |
24
|
|
|
* // 'ssl' => false, |
25
|
|
|
* // 'timeout' => 90, |
26
|
|
|
* // 'utf8' => false, |
27
|
|
|
* // 'passive' => true, |
28
|
|
|
* // 'transferMode' => FTP_BINARY, |
29
|
|
|
* // 'systemType' => null, // 'windows' or 'unix' |
30
|
|
|
* // 'ignorePassiveAddress' => null, // true or false |
31
|
|
|
* // 'timestampsOnUnixListingsEnabled' => false, |
32
|
|
|
* // 'recurseManually' => true, |
33
|
|
|
* // 'useRawListOptions' => null, // true or false |
34
|
|
|
* // 'passphrase' => 'secret', // for secure route url |
35
|
|
|
* // 'action' => '/site/file', // action route |
36
|
|
|
* // 'prefix' => '', |
37
|
|
|
* ], |
38
|
|
|
* ], |
39
|
|
|
* ``` |
40
|
|
|
* |
41
|
|
|
* @link https://sugengsulistiyawan.my.id/ |
42
|
|
|
* @author Sugeng Sulistiyawan <[email protected]> |
43
|
|
|
* @copyright Copyright (c) 2023 |
44
|
|
|
*/ |
45
|
|
|
class FtpComponent extends AbstractComponent |
46
|
|
|
{ |
47
|
|
|
use UrlGeneratorComponentTrait; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
public $host; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
public $root; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
public $username; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
public $password; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var int |
71
|
|
|
*/ |
72
|
|
|
public $port; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var bool |
76
|
|
|
*/ |
77
|
|
|
public $ssl; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var int |
81
|
|
|
*/ |
82
|
|
|
public $timeout; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var bool |
86
|
|
|
*/ |
87
|
|
|
public $utf8; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var bool |
91
|
|
|
*/ |
92
|
|
|
public $passive; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var int |
96
|
|
|
*/ |
97
|
|
|
public $transferMode; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @var string `windows` or `unix` |
101
|
|
|
*/ |
102
|
|
|
public $systemType; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @var bool |
106
|
|
|
*/ |
107
|
|
|
public $ignorePassiveAddress; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @var bool |
111
|
|
|
*/ |
112
|
|
|
public $timestampsOnUnixListingsEnabled; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @var bool |
116
|
|
|
*/ |
117
|
|
|
public $recurseManually; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @var bool |
121
|
|
|
*/ |
122
|
|
|
public $useRawListOptions; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @var string |
126
|
|
|
*/ |
127
|
|
|
public $passphrase; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @var string[] |
131
|
|
|
*/ |
132
|
|
|
protected $_availableOptions = [ |
133
|
|
|
'host', |
134
|
|
|
'root', |
135
|
|
|
'username', |
136
|
|
|
'password', |
137
|
|
|
'port', |
138
|
|
|
'ssl', |
139
|
|
|
'timeout', |
140
|
|
|
'utf8', |
141
|
|
|
'passive', |
142
|
|
|
'transferMode', |
143
|
|
|
'systemType', |
144
|
|
|
'ignorePassiveAddress', |
145
|
|
|
'timestampsOnUnixListingsEnabled', |
146
|
|
|
'recurseManually', |
147
|
|
|
'useRawListOptions', |
148
|
|
|
]; |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @var FtpConnectionOptions |
152
|
|
|
*/ |
153
|
|
|
protected $_connectionOptions; |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @inheritdoc |
157
|
|
|
*/ |
158
|
|
|
public function init() |
159
|
|
|
{ |
160
|
|
|
$this->passphrase = $this->passphrase ?: ($this->password ?: ($this->username ?: Yii::$app->id)); |
161
|
|
|
$this->initEncrypter($this->passphrase); |
162
|
|
|
|
163
|
|
|
parent::init(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return FtpAdapter|PathPrefixedAdapter |
168
|
|
|
*/ |
169
|
|
|
protected function initAdapter() |
170
|
|
|
{ |
171
|
|
|
$this->root = (string) Yii::getAlias($this->root); |
172
|
|
|
|
173
|
|
|
$options = []; |
174
|
|
|
foreach ($this->_availableOptions as $property) { |
175
|
|
|
if ($this->$property !== null) { |
176
|
|
|
$options[$property] = $this->$property; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$this->_connectionOptions = FtpConnectionOptions::fromArray($options); |
181
|
|
|
|
182
|
|
|
$adapter = new FtpAdapter($this->_connectionOptions); |
183
|
|
|
// for UrlGeneratorAdapterTrait |
184
|
|
|
$adapter->component = $this; |
185
|
|
|
|
186
|
|
|
if ($this->prefix) { |
187
|
|
|
$adapter = new PathPrefixedAdapter($adapter, $this->prefix); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $adapter; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|