Completed
Push — notify ( e1b943...11023c )
by Robin
02:07
created

Connection::unknownError()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
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;
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
19
	/**
20
	 * send input to smbclient
21
	 *
22
	 * @param string $input
23
	 */
24
	public function write($input) {
25
		parent::write($input . PHP_EOL);
26
	}
27
28
	/**
29
	 * get all unprocessed output from smbclient until the next prompt
30
	 *
31
	 * @param callable $callback (optional) callback to call for every line read
32
	 * @return string
33
	 * @throws AuthenticationException
34
	 * @throws ConnectException
35
	 * @throws ConnectionException
36
	 * @throws InvalidHostException
37
	 * @throws NoLoginServerException
38
	 */
39
	public function read(callable $callback = null) {
40
		if (!$this->isValid()) {
41
			throw new ConnectionException('Connection not valid');
42
		}
43
		$promptLine = $this->readLine(); //first line is prompt
44
		$this->checkConnectionError($promptLine);
45
46
		$output = array();
47
		$line = $this->readLine();
48
		if ($line === false) {
49
			$this->unknownError($promptLine);
50
		}
51
		$length = mb_strlen(self::DELIMITER);
52
		while (mb_substr($line, 0, $length) !== self::DELIMITER && $line !== false) { //next prompt functions as delimiter
53
			if (is_callable($callback)) {
54
				$result = $callback($line);
55
				if ($result === false) { // allow the callback to close the connection for infinite running commands
56
					$this->close(true);
57
				}
58
			} else {
59
				$output[] .= $line;
60
			}
61
			$line = $this->readLine();
62
		}
63
		return $output;
64
	}
65
66
	/**
67
	 * @param string $promptLine (optional) prompt line that might contain some info about the error
68
	 * @throws ConnectException
69
	 */
70
	private function unknownError($promptLine = '') {
71
		if ($promptLine) { //maybe we have some error we missed on the previous line
72
			throw new ConnectException('Unknown error (' . $promptLine . ')');
73
		} else {
74
			$error = $this->readError(); // maybe something on stderr
75
			if ($error) {
76
				throw new ConnectException('Unknown error (' . $error . ')');
77
			} else {
78
				throw new ConnectException('Unknown error');
79
			}
80
		}
81
	}
82
83
	/**
84
	 * check if the first line holds a connection failure
85
	 *
86
	 * @param $line
87
	 * @throws AuthenticationException
88
	 * @throws InvalidHostException
89
	 * @throws NoLoginServerException
90
	 */
91
	private function checkConnectionError($line) {
92
		$line = rtrim($line, ')');
93
		if (substr($line, -23) === ErrorCodes::LogonFailure) {
94
			throw new AuthenticationException('Invalid login');
95
		}
96
		if (substr($line, -26) === ErrorCodes::BadHostName) {
97
			throw new InvalidHostException('Invalid hostname');
98
		}
99
		if (substr($line, -22) === ErrorCodes::Unsuccessful) {
100
			throw new InvalidHostException('Connection unsuccessful');
101
		}
102
		if (substr($line, -28) === ErrorCodes::ConnectionRefused) {
103
			throw new InvalidHostException('Connection refused');
104
		}
105
		if (substr($line, -26) === ErrorCodes::NoLogonServers) {
106
			throw new NoLoginServerException('No login server');
107
		}
108
	}
109
110
	public function close($terminate = true) {
111
		if (is_resource($this->getInputStream())) {
112
			$this->write('close' . PHP_EOL);
113
		}
114
		parent::close($terminate);
115
	}
116
}
117