Completed
Push — master ( f25894...228057 )
by Robin
03:02
created

Connection::write()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 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
	const DELIMITER_LENGTH = 4;
19
20
	/** @var Parser */
21
	private $parser;
22
23
	public function __construct($command, Parser $parser, $env = array()) {
24
		parent::__construct($command, $env);
25
		$this->parser = $parser;
26
	}
27
28
	/**
29
	 * send input to smbclient
30
	 *
31
	 * @param string $input
32
	 */
33
	public function write($input) {
34
		parent::write($input . PHP_EOL);
35
	}
36
37
	public function clearTillPrompt() {
38
		$this->write('');
39
		do {
40
			$promptLine = $this->readLine();
41
		} while (!$this->isPrompt($promptLine));
42
		$this->write('');
43
		$this->readLine();
44
	}
45
46
	/**
47
	 * get all unprocessed output from smbclient until the next prompt
48
	 *
49
	 * @param callable $callback (optional) callback to call for every line read
50
	 * @return string[]
51
	 * @throws AuthenticationException
52
	 * @throws ConnectException
53
	 * @throws ConnectionException
54
	 * @throws InvalidHostException
55
	 * @throws NoLoginServerException
56
	 */
57
	public function read(callable $callback = null) {
58
		if (!$this->isValid()) {
59
			throw new ConnectionException('Connection not valid');
60
		}
61
		$promptLine = $this->readLine(); //first line is prompt
62
		$this->parser->checkConnectionError($promptLine);
63
64
		$output = array();
65
		$line = $this->readLine();
66
		if ($line === false) {
67
			$this->unknownError($promptLine);
0 ignored issues
show
Security Bug introduced by
It seems like $promptLine defined by $this->readLine() on line 61 can also be of type false; however, Icewind\SMB\Connection::unknownError() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

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 returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
68
		}
69
		while (!$this->isPrompt($line)) { //next prompt functions as delimiter
70
			if (is_callable($callback)) {
71
				$result = $callback($line);
72
				if ($result === false) { // allow the callback to close the connection for infinite running commands
73
					$this->close(true);
74
					break;
75
				}
76
			} else {
77
				$output[] .= $line;
78
			}
79
			$line = $this->readLine();
80
		}
81
		return $output;
82
	}
83
84
	/**
85
	 * Check
86
	 *
87
	 * @param $line
88
	 * @return bool
89
	 */
90
	private function isPrompt($line) {
91
		return mb_substr($line, 0, self::DELIMITER_LENGTH) === self::DELIMITER || $line === false;
92
	}
93
94
	/**
95
	 * @param string $promptLine (optional) prompt line that might contain some info about the error
96
	 * @throws ConnectException
97
	 */
98
	private function unknownError($promptLine = '') {
99
		if ($promptLine) { //maybe we have some error we missed on the previous line
100
			throw new ConnectException('Unknown error (' . $promptLine . ')');
101
		} else {
102
			$error = $this->readError(); // maybe something on stderr
103
			if ($error) {
104
				throw new ConnectException('Unknown error (' . $error . ')');
105
			} else {
106
				throw new ConnectException('Unknown error');
107
			}
108
		}
109
	}
110
111
	public function close($terminate = true) {
112
		if (is_resource($this->getInputStream())) {
113
			$this->write('close' . PHP_EOL);
114
		}
115
		parent::close($terminate);
116
	}
117
}
118