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\AccessDeniedException; |
11
|
|
|
use Icewind\SMB\Exception\AlreadyExistsException; |
12
|
|
|
use Icewind\SMB\Exception\AuthenticationException; |
13
|
|
|
use Icewind\SMB\Exception\Exception; |
14
|
|
|
use Icewind\SMB\Exception\FileInUseException; |
15
|
|
|
use Icewind\SMB\Exception\InvalidHostException; |
16
|
|
|
use Icewind\SMB\Exception\InvalidParameterException; |
17
|
|
|
use Icewind\SMB\Exception\InvalidResourceException; |
18
|
|
|
use Icewind\SMB\Exception\InvalidTypeException; |
19
|
|
|
use Icewind\SMB\Exception\NoLoginServerException; |
20
|
|
|
use Icewind\SMB\Exception\NotEmptyException; |
21
|
|
|
use Icewind\SMB\Exception\NotFoundException; |
22
|
|
|
use Icewind\SMB\TimeZoneProvider; |
23
|
|
|
|
24
|
|
|
class Parser { |
25
|
|
|
const MSG_NOT_FOUND = 'Error opening local file '; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Icewind\SMB\TimeZoneProvider |
29
|
|
|
*/ |
30
|
|
|
protected $timeZoneProvider; |
31
|
|
|
|
32
|
|
|
// todo replace with static once <5.6 support is dropped |
33
|
|
|
// see error.h |
34
|
|
|
const EXCEPTION_MAP = [ |
35
|
|
|
ErrorCodes::LogonFailure => AuthenticationException::class, |
36
|
|
|
ErrorCodes::PathNotFound => NotFoundException::class, |
37
|
|
|
ErrorCodes::ObjectNotFound => NotFoundException::class, |
38
|
|
|
ErrorCodes::NoSuchFile => NotFoundException::class, |
39
|
|
|
ErrorCodes::NameCollision => AlreadyExistsException::class, |
40
|
|
|
ErrorCodes::AccessDenied => AccessDeniedException::class, |
41
|
|
|
ErrorCodes::DirectoryNotEmpty => NotEmptyException::class, |
42
|
|
|
ErrorCodes::FileIsADirectory => InvalidTypeException::class, |
43
|
|
|
ErrorCodes::NotADirectory => InvalidTypeException::class, |
44
|
|
|
ErrorCodes::SharingViolation => FileInUseException::class, |
45
|
|
|
ErrorCodes::InvalidParameter => InvalidParameterException::class |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param TimeZoneProvider $timeZoneProvider |
50
|
|
|
*/ |
51
|
825 |
|
public function __construct(TimeZoneProvider $timeZoneProvider) { |
52
|
825 |
|
$this->timeZoneProvider = $timeZoneProvider; |
53
|
825 |
|
} |
54
|
|
|
|
55
|
60 |
|
private function getErrorCode($line) { |
56
|
60 |
|
$parts = explode(' ', $line); |
57
|
60 |
|
foreach ($parts as $part) { |
58
|
60 |
|
if (substr($part, 0, 9) === 'NT_STATUS') { |
59
|
60 |
|
return $part; |
60
|
|
|
} |
61
|
3 |
|
} |
62
|
6 |
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
60 |
|
public function checkForError($output, $path) { |
66
|
60 |
|
if (strpos($output[0], 'does not exist')) { |
67
|
|
|
throw new NotFoundException($path); |
68
|
|
|
} |
69
|
60 |
|
$error = $this->getErrorCode($output[0]); |
70
|
|
|
|
71
|
60 |
|
if (substr($output[0], 0, strlen(self::MSG_NOT_FOUND)) === self::MSG_NOT_FOUND) { |
72
|
3 |
|
$localPath = substr($output[0], strlen(self::MSG_NOT_FOUND)); |
73
|
3 |
|
throw new InvalidResourceException('Failed opening local file "' . $localPath . '" for writing'); |
74
|
|
|
} |
75
|
|
|
|
76
|
60 |
|
throw Exception::fromMap(self::EXCEPTION_MAP, $error, $path); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* check if the first line holds a connection failure |
81
|
|
|
* |
82
|
|
|
* @param $line |
83
|
|
|
* @throws AuthenticationException |
84
|
|
|
* @throws InvalidHostException |
85
|
|
|
* @throws NoLoginServerException |
86
|
|
|
*/ |
87
|
774 |
|
public function checkConnectionError($line) { |
88
|
774 |
|
$line = rtrim($line, ')'); |
89
|
774 |
|
if (substr($line, -23) === ErrorCodes::LogonFailure) { |
90
|
3 |
|
throw new AuthenticationException('Invalid login'); |
91
|
|
|
} |
92
|
771 |
|
if (substr($line, -26) === ErrorCodes::BadHostName) { |
93
|
|
|
throw new InvalidHostException('Invalid hostname'); |
94
|
|
|
} |
95
|
771 |
|
if (substr($line, -22) === ErrorCodes::Unsuccessful) { |
96
|
6 |
|
throw new InvalidHostException('Connection unsuccessful'); |
97
|
|
|
} |
98
|
765 |
|
if (substr($line, -28) === ErrorCodes::ConnectionRefused) { |
99
|
|
|
throw new InvalidHostException('Connection refused'); |
100
|
|
|
} |
101
|
765 |
|
if (substr($line, -26) === ErrorCodes::NoLogonServers) { |
102
|
|
|
throw new NoLoginServerException('No login server'); |
103
|
|
|
} |
104
|
765 |
|
} |
105
|
|
|
|
106
|
324 |
|
public function parseMode($mode) { |
107
|
324 |
|
$result = 0; |
108
|
|
|
$modeStrings = array( |
109
|
324 |
|
'R' => FileInfo::MODE_READONLY, |
110
|
219 |
|
'H' => FileInfo::MODE_HIDDEN, |
111
|
219 |
|
'S' => FileInfo::MODE_SYSTEM, |
112
|
219 |
|
'D' => FileInfo::MODE_DIRECTORY, |
113
|
219 |
|
'A' => FileInfo::MODE_ARCHIVE, |
114
|
114 |
|
'N' => FileInfo::MODE_NORMAL |
115
|
105 |
|
); |
116
|
324 |
|
foreach ($modeStrings as $char => $val) { |
117
|
324 |
|
if (strpos($mode, $char) !== false) { |
118
|
324 |
|
$result |= $val; |
119
|
105 |
|
} |
120
|
105 |
|
} |
121
|
324 |
|
return $result; |
122
|
|
|
} |
123
|
|
|
|
124
|
81 |
|
public function parseStat($output) { |
125
|
81 |
|
$data = []; |
126
|
81 |
|
foreach ($output as $line) { |
127
|
|
|
// A line = explode statement may not fill all array elements |
128
|
|
|
// properly. May happen when accessing non Windows Fileservers |
129
|
81 |
|
$words = explode(':', $line, 2); |
130
|
81 |
|
$name = isset($words[0]) ? $words[0] : ''; |
131
|
81 |
|
$value = isset($words[1]) ? $words[1] : ''; |
132
|
81 |
|
$value = trim($value); |
133
|
81 |
|
$data[$name] = $value; |
134
|
26 |
|
} |
135
|
|
|
return [ |
136
|
81 |
|
'mtime' => strtotime($data['write_time']), |
137
|
81 |
|
'mode' => hexdec(substr($data['attributes'], strpos($data['attributes'], '('), -1)), |
138
|
81 |
|
'size' => isset($data['stream']) ? intval(explode(' ', $data['stream'])[1]) : 0 |
139
|
26 |
|
]; |
140
|
|
|
} |
141
|
|
|
|
142
|
754 |
|
public function parseDir($output, $basePath) { |
143
|
|
|
//last line is used space |
144
|
754 |
|
array_pop($output); |
145
|
754 |
|
$regex = '/^\s*(.*?)\s\s\s\s+(?:([NDHARS]*)\s+)?([0-9]+)\s+(.*)$/'; |
146
|
|
|
//2 spaces, filename, optional type, size, date |
147
|
754 |
|
$content = array(); |
148
|
754 |
|
foreach ($output as $line) { |
149
|
754 |
|
if (preg_match($regex, $line, $matches)) { |
150
|
754 |
|
list(, $name, $mode, $size, $time) = $matches; |
151
|
754 |
|
if ($name !== '.' and $name !== '..') { |
152
|
292 |
|
$mode = $this->parseMode($mode); |
153
|
292 |
|
$time = strtotime($time . ' ' . $this->timeZoneProvider->get()); |
154
|
600 |
|
$content[] = new FileInfo($basePath . '/' . $name, $name, $size, $time, $mode); |
155
|
97 |
|
} |
156
|
251 |
|
} |
157
|
251 |
|
} |
158
|
754 |
|
return $content; |
159
|
|
|
} |
160
|
|
|
|
161
|
6 |
|
public function parseListShares($output) { |
162
|
6 |
|
$shareNames = array(); |
163
|
6 |
|
foreach ($output as $line) { |
164
|
6 |
|
if (strpos($line, '|')) { |
165
|
|
|
list($type, $name, $description) = explode('|', $line); |
166
|
|
|
if (strtolower($type) === 'disk') { |
167
|
|
|
$shareNames[$name] = $description; |
168
|
|
|
} |
169
|
6 |
|
} else if (strpos($line, 'Disk')) { |
170
|
|
|
// new output format |
171
|
6 |
|
list($name, $description) = explode('Disk', $line); |
172
|
6 |
|
$shareNames[trim($name)] = trim($description); |
173
|
2 |
|
} |
174
|
2 |
|
} |
175
|
6 |
|
return $shareNames; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|