OldUnixExtraField.php
8.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
namespace PhpZip\Model\Extra\Fields;
use PhpZip\Model\Extra\ZipExtraField;
use PhpZip\Model\ZipEntry;
/**
* Info-ZIP Unix Extra Field (type 1):
* ==================================.
*
* The following is the layout of the old Info-ZIP extra block for
* Unix. It has been replaced by the extended-timestamp extra block
* (0x5455) and the Unix type 2 extra block (0x7855).
* (Last Revision 19970118)
*
* Local-header version:
*
* Value Size Description
* ----- ---- -----------
* (Unix1) 0x5855 Short tag for this extra block type ("UX")
* TSize Short total data size for this block
* AcTime Long time of last access (UTC/GMT)
* ModTime Long time of last modification (UTC/GMT)
* UID Short Unix user ID (optional)
* GID Short Unix group ID (optional)
*
* Central-header version:
*
* Value Size Description
* ----- ---- -----------
* (Unix1) 0x5855 Short tag for this extra block type ("UX")
* TSize Short total data size for this block
* AcTime Long time of last access (GMT/UTC)
* ModTime Long time of last modification (GMT/UTC)
*
* The file access and modification times are in standard Unix signed-
* long format, indicating the number of seconds since 1 January 1970
* 00:00:00. The times are relative to Coordinated Universal Time
* (UTC), also sometimes referred to as Greenwich Mean Time (GMT). To
* convert to local time, the software must know the local timezone
* offset from UTC/GMT. The modification time may be used by non-Unix
* systems to support inter-timezone freshening and updating of zip
* archives.
*
* The local-header extra block may optionally contain UID and GID
* info for the file. The local-header TSize value is the only
* indication of this. Note that Unix UIDs and GIDs are usually
* specific to a particular machine, and they generally require root
* access to restore.
*
* This extra field type is obsolete, but it has been in use since
* mid-1994. Therefore future archiving software should continue to
* support it.
*/
class OldUnixExtraField implements ZipExtraField
{
/** @var int Header id */
const HEADER_ID = 0x5855;
/** @var int|null Access timestamp */
private $accessTime;
/** @var int|null Modify timestamp */
private $modifyTime;
/** @var int|null User id */
private $uid;
/** @var int|null Group id */
private $gid;
/**
* @param int|null $accessTime
* @param int|null $modifyTime
* @param int|null $uid
* @param int|null $gid
*/
public function __construct($accessTime, $modifyTime, $uid, $gid)
{
$this->accessTime = $accessTime;
$this->modifyTime = $modifyTime;
$this->uid = $uid;
$this->gid = $gid;
}
/**
* Returns the Header ID (type) of this Extra Field.
* The Header ID is an unsigned short integer (two bytes)
* which must be constant during the life cycle of this object.
*
* @return int
*/
public function getHeaderId()
{
return self::HEADER_ID;
}
/**
* Populate data from this array as if it was in local file data.
*
* @param string $buffer the buffer to read data from
* @param ZipEntry|null $entry
*
* @return OldUnixExtraField
*/
public static function unpackLocalFileData($buffer, ZipEntry $entry = null)
{
$length = \strlen($buffer);
$accessTime = $modifyTime = $uid = $gid = null;
if ($length >= 4) {
$accessTime = unpack('V', $buffer)[1];
}
if ($length >= 8) {
$modifyTime = unpack('V', substr($buffer, 4, 4))[1];
}
if ($length >= 10) {
$uid = unpack('v', substr($buffer, 8, 2))[1];
}
if ($length >= 12) {
$gid = unpack('v', substr($buffer, 10, 2))[1];
}
return new self($accessTime, $modifyTime, $uid, $gid);
}
/**
* Populate data from this array as if it was in central directory data.
*
* @param string $buffer the buffer to read data from
* @param ZipEntry|null $entry
*
* @return OldUnixExtraField
*/
public static function unpackCentralDirData($buffer, ZipEntry $entry = null)
{
$length = \strlen($buffer);
$accessTime = $modifyTime = null;
if ($length >= 4) {
$accessTime = unpack('V', $buffer)[1];
}
if ($length >= 8) {
$modifyTime = unpack('V', substr($buffer, 4, 4))[1];
}
return new self($accessTime, $modifyTime, null, null);
}
/**
* The actual data to put into local file data - without Header-ID
* or length specifier.
*
* @return string the data
*/
public function packLocalFileData()
{
$data = '';
if ($this->accessTime !== null) {
$data .= pack('V', $this->accessTime);
if ($this->modifyTime !== null) {
$data .= pack('V', $this->modifyTime);
if ($this->uid !== null) {
$data .= pack('v', $this->uid);
if ($this->gid !== null) {
$data .= pack('v', $this->gid);
}
}
}
}
return $data;
}
/**
* The actual data to put into central directory - without Header-ID or
* length specifier.
*
* @return string the data
*/
public function packCentralDirData()
{
$data = '';
if ($this->accessTime !== null) {
$data .= pack('V', $this->accessTime);
if ($this->modifyTime !== null) {
$data .= pack('V', $this->modifyTime);
}
}
return $data;
}
/**
* @return int|null
*/
public function getAccessTime()
{
return $this->accessTime;
}
/**
* @param int|null $accessTime
*/
public function setAccessTime($accessTime)
{
$this->accessTime = $accessTime;
}
/**
* @return \DateTimeInterface|null
*/
public function getAccessDateTime()
{
try {
return $this->accessTime === null ? null :
new \DateTimeImmutable('@' . $this->accessTime);
} catch (\Exception $e) {
return null;
}
}
/**
* @return int|null
*/
public function getModifyTime()
{
return $this->modifyTime;
}
/**
* @param int|null $modifyTime
*/
public function setModifyTime($modifyTime)
{
$this->modifyTime = $modifyTime;
}
/**
* @return \DateTimeInterface|null
*/
public function getModifyDateTime()
{
try {
return $this->modifyTime === null ? null :
new \DateTimeImmutable('@' . $this->modifyTime);
} catch (\Exception $e) {
return null;
}
}
/**
* @return int|null
*/
public function getUid()
{
return $this->uid;
}
/**
* @param int|null $uid
*/
public function setUid($uid)
{
$this->uid = $uid;
}
/**
* @return int|null
*/
public function getGid()
{
return $this->gid;
}
/**
* @param int|null $gid
*/
public function setGid($gid)
{
$this->gid = $gid;
}
/**
* @return string
*/
public function __toString()
{
$args = [self::HEADER_ID];
$format = '0x%04x OldUnix:';
if (($modifyTime = $this->getModifyDateTime()) !== null) {
$format .= ' Modify:[%s]';
$args[] = $modifyTime->format(\DATE_ATOM);
}
if (($accessTime = $this->getAccessDateTime()) !== null) {
$format .= ' Access:[%s]';
$args[] = $accessTime->format(\DATE_ATOM);
}
if ($this->uid !== null) {
$format .= ' UID=%d';
$args[] = $this->uid;
}
if ($this->gid !== null) {
$format .= ' GID=%d';
$args[] = $this->gid;
}
return vsprintf($format, $args);
}
}