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\Native; |
9
|
|
|
|
10
|
|
|
use Icewind\SMB\ACL; |
11
|
|
|
use Icewind\SMB\IFileInfo; |
12
|
|
|
|
13
|
|
|
class NativeFileInfo implements IFileInfo { |
14
|
|
|
const MODE_FILE = 0100000; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $path; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $name; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var NativeShare |
28
|
|
|
*/ |
29
|
|
|
protected $share; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array|null |
33
|
|
|
*/ |
34
|
|
|
protected $statCache = null; |
35
|
|
|
|
36
|
|
|
/** @var callable|null */ |
37
|
|
|
protected $statCallback = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
protected $modeCache; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param NativeShare $share |
46
|
|
|
* @param string $path |
47
|
|
|
* @param string $name |
48
|
|
|
* @param array|callable $stat |
49
|
|
|
*/ |
50
|
104 |
|
public function __construct($share, $path, $name, $stat) { |
51
|
104 |
|
$this->share = $share; |
52
|
104 |
|
$this->path = $path; |
53
|
104 |
|
$this->name = $name; |
54
|
|
|
|
55
|
104 |
|
if (is_array($stat)) { |
56
|
23 |
|
$this->statCache = $stat; |
57
|
102 |
|
} elseif (is_callable($stat)) { |
58
|
102 |
|
$this->statCallback = $stat; |
59
|
|
|
} else { |
60
|
|
|
throw new \InvalidArgumentException('$stat needs to be an array or callback'); |
61
|
|
|
} |
62
|
104 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
101 |
|
public function getPath() { |
68
|
101 |
|
return $this->path; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
49 |
|
public function getName() { |
75
|
49 |
|
return $this->name; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
102 |
|
protected function stat() { |
82
|
102 |
|
if (is_null($this->statCache)) { |
83
|
102 |
|
$this->statCache = call_user_func($this->statCallback); |
84
|
|
|
} |
85
|
102 |
|
return $this->statCache; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
29 |
|
public function getSize() { |
92
|
29 |
|
$stat = $this->stat(); |
93
|
29 |
|
return $stat['size']; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return int |
98
|
|
|
*/ |
99
|
1 |
|
public function getMTime() { |
100
|
1 |
|
$stat = $this->stat(); |
101
|
1 |
|
return $stat['mtime']; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
101 |
|
public function isDirectory() { |
108
|
101 |
|
$stat = $this->stat(); |
109
|
101 |
|
return !($stat['mode'] & self::MODE_FILE); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return int |
114
|
|
|
*/ |
115
|
14 |
|
protected function getMode() { |
116
|
14 |
|
if (!$this->modeCache) { |
117
|
14 |
|
$stat = $this->stat(); |
118
|
14 |
|
if (isset($stat['mode'])) { |
119
|
14 |
|
$this->modeCache = ($stat['mode']); |
120
|
|
|
} else { |
121
|
|
|
$attribute = $this->share->getAttribute($this->path, 'system.dos_attr.mode'); |
122
|
|
|
// parse hex string |
123
|
|
|
$this->modeCache = (int)hexdec(substr($attribute, 2)); |
124
|
|
|
} |
125
|
|
|
} |
126
|
14 |
|
return $this->modeCache; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
9 |
|
public function isReadOnly() { |
133
|
9 |
|
$mode = $this->getMode(); |
134
|
9 |
|
return (bool)($mode & IFileInfo::MODE_READONLY); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
5 |
|
public function isHidden() { |
141
|
5 |
|
$mode = $this->getMode(); |
142
|
5 |
|
return (bool)($mode & IFileInfo::MODE_HIDDEN); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
public function isSystem() { |
149
|
|
|
$mode = $this->getMode(); |
150
|
|
|
return (bool)($mode & IFileInfo::MODE_SYSTEM); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
8 |
|
public function isArchived() { |
157
|
8 |
|
$mode = $this->getMode(); |
158
|
8 |
|
return (bool)($mode & IFileInfo::MODE_ARCHIVE); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return ACL[] |
163
|
|
|
*/ |
164
|
|
|
public function getAcls(): array { |
165
|
|
|
$acls = []; |
166
|
|
|
$attribute = $this->share->getAttribute($this->path, 'system.nt_sec_desc.acl.*+'); |
167
|
|
|
|
168
|
|
|
foreach (explode(',', $attribute) as $acl) { |
169
|
|
|
[$user, $permissions] = explode(':', $acl, 2); |
|
|
|
|
170
|
|
|
[$type, $flags, $mask] = explode('/', $permissions); |
|
|
|
|
171
|
|
|
$mask = hexdec($mask); |
172
|
|
|
|
173
|
|
|
$acls[$user] = new ACL($type, $flags, $mask); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $acls; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.