|
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\Logger\LoggerInterface; |
|
15
|
|
|
use Lazzard\FtpBridge\Response\Response; |
|
16
|
|
|
use Lazzard\FtpBridge\Error\ErrorTrigger; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Abstracts shared implementation of an FTP stream. |
|
20
|
|
|
* Holds the common logic between FTP streams. |
|
21
|
|
|
* |
|
22
|
|
|
* @since 1.0 |
|
23
|
|
|
* @author El Amrani Chakir <[email protected]> |
|
24
|
|
|
* |
|
25
|
|
|
* @internal |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class Stream implements StreamInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var resource */ |
|
30
|
|
|
public $stream; |
|
31
|
|
|
|
|
32
|
|
|
/** @var LoggerInterface */ |
|
33
|
|
|
public $logger; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Stream constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param LoggerInterface|null $logger |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(LoggerInterface $logger = null) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->logger = $logger; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritDoc |
|
47
|
|
|
*/ |
|
48
|
|
|
final public function write($command) |
|
49
|
|
|
{ |
|
50
|
|
|
if (($write = fwrite($this->stream, trim($command).self::CRLF)) !== false && $this->logger) { |
|
|
|
|
|
|
51
|
|
|
$this->logger->command($command.self::CRLF); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return true; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @inheritDoc |
|
59
|
|
|
*/ |
|
60
|
|
|
final public function close() |
|
61
|
|
|
{ |
|
62
|
|
|
return fclose($this->stream); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Internal logging method. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $message |
|
69
|
|
|
* |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
final protected function log($message) |
|
73
|
|
|
{ |
|
74
|
|
|
if (!$this->logger) { |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$response = new Response($message); |
|
79
|
|
|
call_user_func_array( |
|
80
|
|
|
array($this->logger, $response->getCode() < 400 ? 'info' : 'error'), |
|
81
|
|
|
array($message) |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Opens an FTP stream socket connection. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $host |
|
89
|
|
|
* @param int $port |
|
90
|
|
|
* @param int $timeout |
|
91
|
|
|
* @param bool $blocking |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
|
|
final protected function openStreamSocket($host, $port, $timeout, $blocking) |
|
96
|
|
|
{ |
|
97
|
|
|
if (!($this->stream = stream_socket_client("tcp://$host:$port", $errno, $errMsg, $timeout, |
|
98
|
|
|
$blocking ? STREAM_CLIENT_CONNECT : STREAM_CLIENT_ASYNC_CONNECT))) { |
|
99
|
|
|
return !ErrorTrigger::raise($errMsg); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @inheritDoc |
|
107
|
|
|
*/ |
|
108
|
|
|
abstract public function open(); |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @inheritDoc |
|
112
|
|
|
*/ |
|
113
|
|
|
abstract public function read(); |
|
114
|
|
|
} |