|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Lazzard/ftp-bridge package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) El Amrani Chakir <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Lazzard\FtpBridge\Stream; |
|
13
|
|
|
|
|
14
|
|
|
use Lazzard\FtpBridge\Response\Response; |
|
15
|
|
|
use Lazzard\FtpBridge\Error\ErrorTrigger; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @since 1.0 |
|
19
|
|
|
* @author El Amrani Chakir <[email protected]> |
|
20
|
|
|
* |
|
21
|
|
|
* @internal |
|
22
|
|
|
*/ |
|
23
|
|
|
class PassiveDataStream extends DataStream |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Opens the data connection stream to the server port sent via the FTP server after |
|
27
|
|
|
* sending the PASV command. |
|
28
|
|
|
* |
|
29
|
|
|
* {@inheritDoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function open() |
|
32
|
|
|
{ |
|
33
|
|
|
$this->commandStream->write('PASV'); |
|
34
|
|
|
$response = new Response($this->commandStream->read()); |
|
|
|
|
|
|
35
|
|
|
if ($response->getCode() !== 227) { |
|
36
|
|
|
return !ErrorTrigger::raise($response->getMessage()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if (!preg_match('/(\d+,){4}+/', $response->getMessage(), $ipMatches) |
|
40
|
|
|
|| !preg_match('/\d+,\d+\)/', $response->getMessage(), $portMatches)) { |
|
41
|
|
|
return !ErrorTrigger::raise("Unable to get the passive IP & PORT from the reply message."); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$ip = rtrim(str_replace(",", ".", $ipMatches[0]), "."); |
|
45
|
|
|
$ports = explode(",", rtrim($portMatches[0], ")")); |
|
46
|
|
|
$port = ($ports[0] * 256) + $ports[1]; |
|
47
|
|
|
|
|
48
|
|
|
return $this->openStreamSocket($ip, $port, $this->commandStream->timeout, $this->commandStream->blocking); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritDoc |
|
53
|
|
|
*/ |
|
54
|
|
|
public function read() |
|
55
|
|
|
{ |
|
56
|
|
|
$data = ''; |
|
57
|
|
|
while (!feof($this->stream)) { |
|
58
|
|
|
$data .= fread($this->stream, 8192); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$this->log($data); |
|
62
|
|
|
|
|
63
|
|
|
return $data; |
|
64
|
|
|
} |
|
65
|
|
|
} |