Completed
Pull Request — master (#47)
by Robin
02:41 queued 32s
created

Connection   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 10
Bugs 1 Features 3
Metric Value
wmc 18
c 10
b 1
f 3
lcom 1
cbo 6
dl 0
loc 93
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 3 1
D read() 0 35 9
B checkConnectionError() 0 18 6
A close() 0 6 2
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 !== false) { //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
			} else {
68
				$output[] .= $line;
69
			}
70
			$line = $this->readLine();
71
		}
72
		return $output;
73
	}
74
75
	/**
76
	 * check if the first line holds a connection failure
77
	 *
78
	 * @param $line
79
	 * @throws AuthenticationException
80
	 * @throws InvalidHostException
81
	 * @throws NoLoginServerException
82
	 */
83
	private function checkConnectionError($line) {
84
		$line = rtrim($line, ')');
85
		if (substr($line, -23) === ErrorCodes::LogonFailure) {
86
			throw new AuthenticationException('Invalid login');
87
		}
88
		if (substr($line, -26) === ErrorCodes::BadHostName) {
89
			throw new InvalidHostException('Invalid hostname');
90
		}
91
		if (substr($line, -22) === ErrorCodes::Unsuccessful) {
92
			throw new InvalidHostException('Connection unsuccessful');
93
		}
94
		if (substr($line, -28) === ErrorCodes::ConnectionRefused) {
95
			throw new InvalidHostException('Connection refused');
96
		}
97
		if (substr($line, -26) === ErrorCodes::NoLogonServers) {
98
			throw new NoLoginServerException('No login server');
99
		}
100
	}
101
102
	public function close($terminate = true) {
103
		if (is_resource($this->getInputStream())) {
104
			$this->write('close' . PHP_EOL);
105
		}
106
		parent::close($terminate);
107
	}
108
}
109