1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\RemoteRequest\Protocols\Fsp\Answer\GetDir; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\RemoteRequest\Protocols\Fsp; |
7
|
|
|
use SplFileInfo; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class FileInfo |
12
|
|
|
* @package kalanis\RemoteRequest\Protocols\Fsp\Answer\GetDir |
13
|
|
|
* Process Get file info |
14
|
|
|
* @link https://www.php.net/manual/en/class.splfileobject.php |
15
|
|
|
*/ |
16
|
|
|
class FileInfo extends SplFileInfo |
17
|
|
|
{ |
18
|
|
|
/** @var array<int, string> */ |
19
|
|
|
protected static array $types = [ |
20
|
|
|
Fsp::RDTYPE_DIR => 'dir', |
21
|
|
|
Fsp::RDTYPE_FILE => 'file', |
22
|
|
|
Fsp::RDTYPE_LINK => 'link', |
23
|
|
|
Fsp::RDTYPE_SKIP => 'skip', |
24
|
|
|
Fsp::RDTYPE_END => 'end', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
protected string $path = ''; |
28
|
|
|
protected string $file_name = ''; |
29
|
|
|
protected string $link_name = ''; |
30
|
|
|
protected int $size = 0; |
31
|
|
|
protected int $time = 0; |
32
|
|
|
protected int $type = 0; |
33
|
|
|
|
34
|
2 |
|
public function setData(string $data, string $path = ''): self |
35
|
|
|
{ |
36
|
2 |
|
$this->path = $path; |
37
|
2 |
|
$this->time = Fsp\Strings::mb_ord(substr($data, 0, 4)); |
38
|
2 |
|
$this->size = Fsp\Strings::mb_ord(substr($data, 4, 4)); |
39
|
2 |
|
$this->type = Fsp\Strings::mb_ord($data[8]); |
40
|
2 |
|
if (Fsp::RDTYPE_END != $this->type && Fsp::RDTYPE_SKIP != $this->type) { |
41
|
2 |
|
$this->file_name = rtrim(substr($data, 9), "\t\n\r\0\x0B"); // spaces must stay |
42
|
2 |
|
if ($this->isLink()) { |
43
|
1 |
|
$this->parseLinkName(); |
44
|
|
|
} |
45
|
|
|
} |
46
|
2 |
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
protected function parseLinkName(): void |
50
|
|
|
{ |
51
|
1 |
|
$nlpos = strpos($this->file_name, "\n"); |
52
|
1 |
|
$this->link_name = $nlpos ? substr($this->file_name, $nlpos + 1) : '' ; |
53
|
1 |
|
$this->file_name = $this->link_name && $nlpos ? substr($this->file_name, 0, $nlpos) : $this->file_name ; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public function setPath(string $path): self |
57
|
|
|
{ |
58
|
1 |
|
$this->path = $path; |
59
|
1 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
public function getPath(): string |
63
|
|
|
{ |
64
|
1 |
|
return $this->path; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
public function getFilename(): string |
68
|
|
|
{ |
69
|
2 |
|
return $this->file_name; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
public function getExtension(): string |
73
|
|
|
{ |
74
|
1 |
|
$dot = strrpos($this->file_name, '.'); |
75
|
1 |
|
return $dot ? substr($this->file_name, $dot + 1) : ''; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
public function getPathname(): string |
79
|
|
|
{ |
80
|
1 |
|
return $this->path . DIRECTORY_SEPARATOR . $this->file_name; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
public function getPerms(): int |
84
|
|
|
{ |
85
|
|
|
// need server info |
86
|
1 |
|
return 0666; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
public function getInode(): int |
90
|
|
|
{ |
91
|
1 |
|
return 0; |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
public function getSize(): int |
95
|
|
|
{ |
96
|
2 |
|
return $this->size; |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
public function getOwner(): int |
100
|
|
|
{ |
101
|
1 |
|
return 0; |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
public function getGroup(): int |
105
|
|
|
{ |
106
|
1 |
|
return 0; |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
public function getATime(): int |
110
|
|
|
{ |
111
|
1 |
|
return $this->time; |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
public function getMTime(): int |
115
|
|
|
{ |
116
|
2 |
|
return $this->time; |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
public function getCTime(): int |
120
|
|
|
{ |
121
|
1 |
|
return $this->time; |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
public function getType(): string |
125
|
|
|
{ |
126
|
1 |
|
return static::$types[$this->type]; |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
public function getOrigType(): int |
130
|
|
|
{ |
131
|
1 |
|
return $this->type; |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
public function isWritable(): bool |
135
|
|
|
{ |
136
|
|
|
/// need server info |
137
|
1 |
|
return true; |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
public function isReadable(): bool |
141
|
|
|
{ |
142
|
|
|
/// need server info |
143
|
1 |
|
return true; |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
public function isExecutable(): bool |
147
|
|
|
{ |
148
|
1 |
|
return $this->isDir(); |
149
|
|
|
} |
150
|
|
|
|
151
|
2 |
|
public function isFile(): bool |
152
|
|
|
{ |
153
|
2 |
|
return Fsp::RDTYPE_FILE == $this->type; |
154
|
|
|
} |
155
|
|
|
|
156
|
2 |
|
public function isDir(): bool |
157
|
|
|
{ |
158
|
2 |
|
return Fsp::RDTYPE_DIR == $this->type; |
159
|
|
|
} |
160
|
|
|
|
161
|
2 |
|
public function isLink(): bool |
162
|
|
|
{ |
163
|
2 |
|
return Fsp::RDTYPE_LINK == $this->type; |
164
|
|
|
} |
165
|
|
|
|
166
|
1 |
|
public function getLinkTarget(): string |
167
|
|
|
{ |
168
|
1 |
|
return $this->isLink() ? $this->link_name : '' ; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
#[\ReturnTypeWillChange] |
172
|
1 |
|
public function getRealPath() |
173
|
|
|
{ |
174
|
1 |
|
return $this->path . DIRECTORY_SEPARATOR . $this->file_name; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|