-
Notifications
You must be signed in to change notification settings - Fork 0
/
ALDPackage.php
60 lines (48 loc) · 1.37 KB
/
ALDPackage.php
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
<?php
require_once dirname(__FILE__) . '/lib/exceptions.php';
require_once dirname(__FILE__) . '/ALDPackageDefinition.php';
class ALDPackage {
private $archive;
public $definition;
public function __construct($path) {
if (!file_exists($path)) {
throw new FileNotFoundException();
}
$this->archive = new ZipArchive();
if (@$this->archive->open($path) !== true) {
@$this->archive->close();
throw new ArchiveOpenException();
}
$this->definition = new ALDPackageDefinition($this->archive->getFromName(ALDPackageDefinition::DefinitionFile));
$this->validate();
}
public function __destruct() {
$this->archive->close();
}
private function validate() {
$def_files = $this->definition->GetFiles();
$files = array_flip($this->GetFiles());
foreach ($def_files AS $def_file) {
if (!isset($files[$def_file])) {
throw new FileNotFoundException($def_file); # defined but missing
}
unset($files[$def_file]);
}
foreach ($files AS $file => $i) { # undefined files left
if ($file !== ALDPackageDefinition::DefinitionFile) {
throw new UndefinedFileException($file);
}
}
}
public function GetFiles() {
$files = array();
for ($i = 0; $i < $this->archive->numFiles; $i++) {
$stat = $this->archive->statIndex($i);
if ($stat['name'][strlen($stat['name'])-1] !== '/') {
$files[] = $stat['name'];
}
}
return $files;
}
}
?>