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
|
566 |
|
public function __construct(TimeZoneProvider $timeZoneProvider) { |
52
|
566 |
|
$this->timeZoneProvider = $timeZoneProvider; |
53
|
566 |
|
} |
54
|
|
|
|
55
|
40 |
|
private function getErrorCode($line) { |
56
|
40 |
|
$parts = explode(' ', $line); |
57
|
40 |
|
foreach ($parts as $part) { |
58
|
40 |
|
if (substr($part, 0, 9) === 'NT_STATUS') { |
59
|
40 |
|
return $part; |
60
|
|
|
} |
61
|
|
|
} |
62
|
4 |
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
40 |
|
public function checkForError($output, $path) { |
66
|
40 |
|
if (strpos($output[0], 'does not exist')) { |
67
|
|
|
throw new NotFoundException($path); |
68
|
|
|
} |
69
|
40 |
|
$error = $this->getErrorCode($output[0]); |
70
|
|
|
|
71
|
40 |
|
if (substr($output[0], 0, strlen(self::MSG_NOT_FOUND)) === self::MSG_NOT_FOUND) { |
72
|
2 |
|
$localPath = substr($output[0], strlen(self::MSG_NOT_FOUND)); |
73
|
2 |
|
throw new InvalidResourceException('Failed opening local file "' . $localPath . '" for writing'); |
74
|
|
|
} |
75
|
|
|
|
76
|
40 |
|
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
|
|
|
* @throws AccessDeniedException |
87
|
|
|
*/ |
88
|
512 |
|
public function checkConnectionError($line) { |
89
|
512 |
|
$line = rtrim($line, ')'); |
90
|
512 |
|
if (substr($line, -23) === ErrorCodes::LogonFailure) { |
91
|
|
|
throw new AuthenticationException('Invalid login'); |
92
|
|
|
} |
93
|
512 |
|
if (substr($line, -26) === ErrorCodes::BadHostName) { |
94
|
|
|
throw new InvalidHostException('Invalid hostname'); |
95
|
|
|
} |
96
|
512 |
|
if (substr($line, -22) === ErrorCodes::Unsuccessful) { |
97
|
2 |
|
throw new InvalidHostException('Connection unsuccessful'); |
98
|
|
|
} |
99
|
512 |
|
if (substr($line, -28) === ErrorCodes::ConnectionRefused) { |
100
|
|
|
throw new InvalidHostException('Connection refused'); |
101
|
|
|
} |
102
|
512 |
|
if (substr($line, -26) === ErrorCodes::NoLogonServers) { |
103
|
|
|
throw new NoLoginServerException('No login server'); |
104
|
|
|
} |
105
|
512 |
|
if (substr($line, -23) === ErrorCodes::AccessDenied) { |
106
|
|
|
throw new AccessDeniedException('Access denied'); |
107
|
|
|
} |
108
|
512 |
|
} |
109
|
|
|
|
110
|
228 |
|
public function parseMode($mode) { |
111
|
228 |
|
$result = 0; |
112
|
|
|
$modeStrings = array( |
113
|
228 |
|
'R' => FileInfo::MODE_READONLY, |
114
|
123 |
|
'H' => FileInfo::MODE_HIDDEN, |
115
|
123 |
|
'S' => FileInfo::MODE_SYSTEM, |
116
|
123 |
|
'D' => FileInfo::MODE_DIRECTORY, |
117
|
123 |
|
'A' => FileInfo::MODE_ARCHIVE, |
118
|
114 |
|
'N' => FileInfo::MODE_NORMAL |
119
|
9 |
|
); |
120
|
228 |
|
foreach ($modeStrings as $char => $val) { |
121
|
228 |
|
if (strpos($mode, $char) !== false) { |
122
|
228 |
|
$result |= $val; |
123
|
9 |
|
} |
124
|
9 |
|
} |
125
|
228 |
|
return $result; |
126
|
|
|
} |
127
|
|
|
|
128
|
58 |
|
public function parseStat($output) { |
129
|
58 |
|
$data = []; |
130
|
58 |
|
foreach ($output as $line) { |
131
|
|
|
// A line = explode statement may not fill all array elements |
132
|
|
|
// properly. May happen when accessing non Windows Fileservers |
133
|
58 |
|
$words = explode(':', $line, 2); |
134
|
58 |
|
$name = isset($words[0]) ? $words[0] : ''; |
135
|
58 |
|
$value = isset($words[1]) ? $words[1] : ''; |
136
|
58 |
|
$value = trim($value); |
137
|
58 |
|
$data[$name] = $value; |
138
|
3 |
|
} |
139
|
|
|
return [ |
140
|
58 |
|
'mtime' => strtotime($data['write_time']), |
141
|
58 |
|
'mode' => hexdec(substr($data['attributes'], strpos($data['attributes'], '('), -1)), |
142
|
58 |
|
'size' => isset($data['stream']) ? intval(explode(' ', $data['stream'])[1]) : 0 |
143
|
3 |
|
]; |
144
|
|
|
} |
145
|
|
|
|
146
|
504 |
|
public function parseDir($output, $basePath) { |
147
|
|
|
//last line is used space |
148
|
504 |
|
array_pop($output); |
149
|
504 |
|
$regex = '/^\s*(.*?)\s\s\s\s+(?:([NDHARS]*)\s+)?([0-9]+)\s+(.*)$/'; |
150
|
|
|
//2 spaces, filename, optional type, size, date |
151
|
504 |
|
$content = array(); |
152
|
504 |
|
foreach ($output as $line) { |
153
|
504 |
|
if (preg_match($regex, $line, $matches)) { |
154
|
504 |
|
list(, $name, $mode, $size, $time) = $matches; |
155
|
504 |
|
if ($name !== '.' and $name !== '..') { |
156
|
196 |
|
$mode = $this->parseMode($mode); |
157
|
196 |
|
$time = strtotime($time . ' ' . $this->timeZoneProvider->get()); |
158
|
504 |
|
$content[] = new FileInfo($basePath . '/' . $name, $name, $size, $time, $mode); |
159
|
1 |
|
} |
160
|
1 |
|
} |
161
|
1 |
|
} |
162
|
504 |
|
return $content; |
163
|
|
|
} |
164
|
|
|
|
165
|
10 |
|
public function parseListShares($output) { |
166
|
10 |
|
$shareNames = array(); |
167
|
10 |
|
foreach ($output as $line) { |
168
|
4 |
|
if (strpos($line, '|')) { |
169
|
|
|
list($type, $name, $description) = explode('|', $line); |
170
|
|
|
if (strtolower($type) === 'disk') { |
171
|
|
|
$shareNames[$name] = $description; |
172
|
|
|
} |
173
|
4 |
|
} else if (strpos($line, 'Disk')) { |
174
|
|
|
// new output format |
175
|
4 |
|
list($name, $description) = explode('Disk', $line); |
176
|
4 |
|
$shareNames[trim($name)] = trim($description); |
177
|
|
|
} |
178
|
|
|
} |
179
|
10 |
|
return $shareNames; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|