1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2014 Robin Appelman <[email protected]> |
4
|
|
|
* This file is licensed under the Licensed under the MIT license: |
5
|
|
|
* http://opensource.org/licenses/MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Icewind\SMB\Wrapped; |
9
|
|
|
|
10
|
|
|
use Icewind\SMB\Exception\AuthenticationException; |
11
|
|
|
use Icewind\SMB\Exception\ConnectException; |
12
|
|
|
use Icewind\SMB\Exception\ConnectionException; |
13
|
|
|
use Icewind\SMB\Exception\InvalidHostException; |
14
|
|
|
use Icewind\SMB\Exception\NoLoginServerException; |
15
|
|
|
|
16
|
|
|
class Connection extends RawConnection { |
17
|
|
|
const DELIMITER = 'smb:'; |
18
|
|
|
const DELIMITER_LENGTH = 4; |
19
|
|
|
|
20
|
|
|
/** @var Parser */ |
21
|
|
|
private $parser; |
22
|
|
|
|
23
|
765 |
|
public function __construct($command, Parser $parser, $env = array()) { |
24
|
765 |
|
parent::__construct($command, $env); |
25
|
765 |
|
$this->parser = $parser; |
26
|
765 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* send input to smbclient |
30
|
|
|
* |
31
|
|
|
* @param string $input |
32
|
|
|
*/ |
33
|
765 |
|
public function write($input) { |
34
|
765 |
|
parent::write($input . PHP_EOL); |
35
|
765 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @throws ConnectException |
39
|
|
|
*/ |
40
|
765 |
|
public function clearTillPrompt() { |
41
|
765 |
|
$this->write(''); |
42
|
|
|
do { |
43
|
765 |
|
$promptLine = $this->readLine(); |
44
|
765 |
|
$this->parser->checkConnectionError($promptLine); |
45
|
765 |
|
} while (!$this->isPrompt($promptLine)); |
46
|
765 |
|
$this->write(''); |
47
|
765 |
|
$this->readLine(); |
48
|
765 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* get all unprocessed output from smbclient until the next prompt |
52
|
|
|
* |
53
|
|
|
* @param callable $callback (optional) callback to call for every line read |
54
|
|
|
* @return string[] |
55
|
|
|
* @throws AuthenticationException |
56
|
|
|
* @throws ConnectException |
57
|
|
|
* @throws ConnectionException |
58
|
|
|
* @throws InvalidHostException |
59
|
|
|
* @throws NoLoginServerException |
60
|
|
|
*/ |
61
|
762 |
|
public function read(callable $callback = null) { |
62
|
762 |
|
if (!$this->isValid()) { |
63
|
|
|
throw new ConnectionException('Connection not valid'); |
64
|
|
|
} |
65
|
762 |
|
$promptLine = $this->readLine(); //first line is prompt |
66
|
762 |
|
$this->parser->checkConnectionError($promptLine); |
67
|
|
|
|
68
|
762 |
|
$output = array(); |
69
|
762 |
|
$line = $this->readLine(); |
70
|
762 |
|
if ($line === false) { |
71
|
|
|
$this->unknownError($promptLine); |
|
|
|
|
72
|
|
|
} |
73
|
762 |
|
while (!$this->isPrompt($line)) { //next prompt functions as delimiter |
74
|
756 |
|
if (is_callable($callback)) { |
75
|
6 |
|
$result = $callback($line); |
76
|
6 |
|
if ($result === false) { // allow the callback to close the connection for infinite running commands |
77
|
6 |
|
$this->close(true); |
78
|
6 |
|
break; |
79
|
|
|
} |
80
|
|
|
} else { |
81
|
750 |
|
$output[] .= $line; |
82
|
|
|
} |
83
|
750 |
|
$line = $this->readLine(); |
84
|
|
|
} |
85
|
762 |
|
return $output; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check |
90
|
|
|
* |
91
|
|
|
* @param $line |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
765 |
|
private function isPrompt($line) { |
95
|
765 |
|
return mb_substr($line, 0, self::DELIMITER_LENGTH) === self::DELIMITER || $line === false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $promptLine (optional) prompt line that might contain some info about the error |
100
|
|
|
* @throws ConnectException |
101
|
|
|
*/ |
102
|
|
|
private function unknownError($promptLine = '') { |
103
|
|
|
if ($promptLine) { //maybe we have some error we missed on the previous line |
104
|
|
|
throw new ConnectException('Unknown error (' . $promptLine . ')'); |
105
|
|
|
} else { |
106
|
|
|
$error = $this->readError(); // maybe something on stderr |
107
|
|
|
if ($error) { |
108
|
|
|
throw new ConnectException('Unknown error (' . $error . ')'); |
109
|
|
|
} else { |
110
|
|
|
throw new ConnectException('Unknown error'); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
765 |
|
public function close($terminate = true) { |
116
|
765 |
|
if (is_resource($this->getInputStream())) { |
117
|
765 |
|
$this->write('close' . PHP_EOL); |
118
|
|
|
} |
119
|
765 |
|
parent::close($terminate); |
120
|
765 |
|
} |
121
|
|
|
} |
122
|
|
|
|
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.