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
|
#!/usr/local/bin/php -f
<?php // -*- C++ -*-
error_reporting(1);
// config section, should be read from php-config
$debug = true;
$pear_phpdir = "@PEAR_INSTALLDIR@";
$pear_extdir = "@EXTENSION_DIR@";
//$pear_extdir = ini_get("extension_dir");
$pear_docdir = ""; // empty means don't install docs
$pkgfile = $argv[0];
if (!$pkgfile) {
die("Usage: pear <packagefile>\n");
}
if (!file_exists($pkgfile)) {
die("No such file: $pkgfile\n");
}
$fp = popen("gzip -dc $pkgfile | tar -tf -", "r");
if (!$fp) {
die("Unable to examine $pkgfile (gzip or tar failed)\n");
}
while ($line = fgets($fp, 4096)) {
$line = rtrim($line);
if (preg_match('!^[^/]+/package.xml$!', $line)) {
if ($descfile) {
die("Invalid package: multiple package.xml files at depth one!\n");
}
$descfile = $line;
}
}
pclose($fp);
if (!$descfile) {
die("Invalid package: no package.xml file found!\n");
}
$tmpdir = tempnam("/tmp", "pear");
if (!mkdir($tmpdir, 0755)) {
die("Unable to create temporary directory $tmpdir.\n");
}
register_shutdown_function("cleanup");
$pwd = trim(`pwd`);
if (substr($pkgfile, 0, 1) == "/") {
$pkgfilepath = $pkgfile;
} else {
$pkgfilepath = $pwd.'/'.$pkgfile;
}
if (!chdir($tmpdir)) {
die("Unable to chdir to $tmpdir.\n");
}
system("gzip -dc $pkgfilepath | tar -xf -");
if (!file_exists($descfile)) {
die("Huh? No package.xml file after extracting the archive.\n");
}
$pkgdir = dirname($descfile);
$fp = fopen($descfile, "r");
$xp = xml_parser_create();
if (!$xp) {
die("Unable to create XML parser.\n");
}
xml_set_element_handler($xp, "start_handler", "end_handler");
xml_set_character_data_handler($xp, "char_handler");
xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, false);
$element_stack = array();
$current_element = false;
$destdir = '';
$pkginfo = array();
while ($data = fread($fp, 2048)) {
if (!xml_parse($xp, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xp)),
xml_get_current_line_number($xp)));
}
}
if ($pkginfo['pkgtype'] != "binary") {
die("Invalid package: only binary packages supported yet.\n");
}
print "ok so far\n";
exit;
function start_handler($xp, $name, $attribs) {
global $element_stack, $current_element, $pkginfo;
array_push($element_stack, $name);
$current_element = $name;
switch ($name) {
case "Package":
$pkginfo['pkgtype'] = strtolower($attribs["Type"]);
break;
}
}
function end_handler($xp, $name) {
global $element_stack, $current_element;
array_pop($element_stack);
$current_element = $element_stack[sizeof($element_stack)-1];
}
function char_handler($xp, $data) {
global $element_stack, $current_element, $pkginfo;
global $phpfiles, $extfiles, $docfiles, $pkgdir;
global $pear_phpdir, $pear_extdir, $pear_docdir;
global $destdir, $debug;
switch ($current_element) {
case "DestDir":
$destdir = trim($data);
if (substr($destdir, 0, 1) == "/") {
$destdir = substr($destdir, 1);
}
break;
case "Dir":
if (!$pear_phpdir) {
break;
}
$dir = trim($data);
$d = "$pear_phpdir/$destdir/$dir";
if (is_file($d)) {
print "Error: wanted to create the directory $d\n";
print " but it was already a file.\n";
continue 2;
}
if (is_dir($d)) {
continue 2;
}
if (!mkdir($d, 0755)) {
print "Error: could not mkdir $d\n";
continue 2;
}
if ($debug) print "[debug] created directory $d\n";
break;
case "File":
if (!$pear_phpdir) {
break;
}
$file = trim($data);
$d = "$pear_phpdir/$destdir";
if (!copy("$pkgdir/$file", "$d/$file")) {
print "Error: failed to copy $pkgdir/$file to $d\n";
continue 2;
}
if ($debug) print "[debug] installed $d/$file\n";
break;
case "ExtDir":
if (!$pear_extdir) {
break;
}
$dir = trim($data);
$d = "$pear_extdir/$destdir/$dir";
if (is_file($d)) {
print "Error: wanted to create the directory $d\n";
print " but it was already a file.\n";
continue 2;
}
if (is_dir($d)) {
continue 2;
}
if (!mkdir($d, 0755)) {
print "Error: could not mkdir $d\n";
continue 2;
}
if ($debug) print "[debug] created directory $d\n";
break;
case "ExtFile":
if (!$pear_extdir) {
break;
}
$file = trim($data);
$d = "$pear_extdir/$destdir";
if (!copy("$pkgdir/$file", "$d/$file")) {
print "Error: failed to copy $pkgdir/$file to $d\n";
continue 2;
}
if ($debug) print "[debug] installed $d/$file\n";
break;
case "DocDir":
if (!$pear_docdir) {
break;
}
$dir = trim($data);
$d = "$pear_docdir/$destdir/$dir";
if (is_file($d)) {
print "Error: wanted to create the directory $d\n";
print " but it was already a file.\n";
continue 2;
}
if (is_dir($d)) {
continue 2;
}
if (!mkdir($d, 0755)) {
print "Error: could not mkdir $d\n";
continue 2;
}
if ($debug) print "[debug] created directory $d\n";
break;
case "DocFile":
if (!$pear_docdir) {
break;
}
$file = trim($data);
$d = "$pear_docdir/$destdir";
if (!copy("$pkgdir/$file", "$d/$file")) {
print "Error: failed to copy $pkgdir/$file to $d\n";
continue 2;
}
if ($debug) print "[debug] installed $d/$file\n";
break;
}
}
function cleanup() {
global $tmpdir;
if ($tmpdir && is_dir($tmpdir)) {
system("rm -rf $tmpdir");
}
$tmpdir = null;
}
?>
|