Completed
Push — notify ( cf23de )
by Robin
02:11
created

Connection::read()   D

Complexity

Conditions 9
Paths 8

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 1
Metric Value
c 7
b 1
f 1
dl 0
loc 34
rs 4.909
cc 9
eloc 25
nc 8
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
			if ($promptLine) { //maybe we have some error we missed on the previous line
50
				throw new ConnectException('Unknown error (' . $promptLine . ')');
51
			} else {
52
				$error = $this->readError(); // maybe something on stderr
53
				if ($error) {
54
					throw new ConnectException('Unknown error (' . $error . ')');
55
				} else {
56
					throw new ConnectException('Unknown error');
57
				}
58
			}
59
		}
60
		$length = mb_strlen(self::DELIMITER);
61
		while (mb_substr($line, 0, $length) !== self::DELIMITER && $line) { //next prompt functions as delimiter
62
			if (is_callable($callback)) {
63
				$result = $callback($line);
64
				if ($result === false) { // allow the callback to close the connection for infinite running commands
65
					$this->close(true);
66
				}
67
			}
68
			$output[] .= $line;
69
			$line = $this->readLine();
70
		}
71
		return $output;
72
	}
73
74
	/**
75
	 * check if the first line holds a connection failure
76
	 *
77
	 * @param $line
78
	 * @throws AuthenticationException
79
	 * @throws InvalidHostException
80
	 * @throws NoLoginServerException
81
	 */
82
	private function checkConnectionError($line) {
83
		$line = rtrim($line, ')');
84
		if (substr($line, -23) === ErrorCodes::LogonFailure) {
85
			throw new AuthenticationException('Invalid login');
86
		}
87
		if (substr($line, -26) === ErrorCodes::BadHostName) {
88
			throw new InvalidHostException('Invalid hostname');
89
		}
90
		if (substr($line, -22) === ErrorCodes::Unsuccessful) {
91
			throw new InvalidHostException('Connection unsuccessful');
92
		}
93
		if (substr($line, -28) === ErrorCodes::ConnectionRefused) {
94
			throw new InvalidHostException('Connection refused');
95
		}
96
		if (substr($line, -26) === ErrorCodes::NoLogonServers) {
97
			throw new NoLoginServerException('No login server');
98
		}
99
	}
100
101
	public function close($terminate = true) {
102
		if (is_resource($this->getInputStream())) {
103
			$this->write('close' . PHP_EOL);
104
		}
105
		parent::close($terminate);
106
	}
107
}
108