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