1
|
<?php
|
2
|
/****h* pfSense/pkg-utils
|
3
|
* NAME
|
4
|
* pkg-utils.inc - Package subsystem
|
5
|
* DESCRIPTION
|
6
|
* This file contains various functions used by the pfSense package system.
|
7
|
* HISTORY
|
8
|
* $Id$
|
9
|
******
|
10
|
*
|
11
|
* Copyright (C) 2005-2006 Colin Smith (ethethlay@gmail.com)
|
12
|
* All rights reserved.
|
13
|
* Redistribution and use in source and binary forms, with or without
|
14
|
* modification, are permitted provided that the following conditions are met:
|
15
|
*
|
16
|
* 1. Redistributions of source code must retain the above copyright notice,
|
17
|
* this list of conditions and the following disclaimer.
|
18
|
*
|
19
|
* 2. Redistributions in binary form must reproduce the above copyright
|
20
|
* notice, this list of conditions and the following disclaimer in the
|
21
|
* documentation and/or other materials provided with the distribution.
|
22
|
*
|
23
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
24
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
25
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
26
|
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
27
|
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
* RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
* POSSIBILITY OF SUCH DAMAGE.
|
33
|
*
|
34
|
*/
|
35
|
require_once("xmlrpc.inc");
|
36
|
require_once("xmlparse.inc");
|
37
|
require_once("service-utils.inc");
|
38
|
require_once("pfsense-utils.inc");
|
39
|
require_once("globals.inc");
|
40
|
|
41
|
safe_mkdir("/var/db/pkg");
|
42
|
$g['platform'] = trim(file_get_contents("/etc/platform"));
|
43
|
if($g['platform'] == "pfSense") {
|
44
|
safe_mkdir("/usr/local/pkg");
|
45
|
safe_mkdir("/usr/local/pkg/pf");
|
46
|
}
|
47
|
|
48
|
/****f* pkg-utils/is_package_installed
|
49
|
* NAME
|
50
|
* is_package_installed - Check whether a package is installed.
|
51
|
* INPUTS
|
52
|
* $packagename - name of the package to check
|
53
|
* RESULT
|
54
|
* boolean - true if the package is installed, false otherwise
|
55
|
* NOTES
|
56
|
* This function is deprecated - get_pkg_id() can already check for installation.
|
57
|
******/
|
58
|
function is_package_installed($packagename) {
|
59
|
$pkg = get_pkg_id($packagename);
|
60
|
if($pkg == -1) return false;
|
61
|
return true;
|
62
|
}
|
63
|
|
64
|
/****f* pkg-utils/get_pkg_id
|
65
|
* NAME
|
66
|
* get_pkg_id - Find a package's numeric ID.
|
67
|
* INPUTS
|
68
|
* $pkg_name - name of the package to check
|
69
|
* RESULT
|
70
|
* integer - -1 if package is not found, >-1 otherwise
|
71
|
******/
|
72
|
function get_pkg_id($pkg_name) {
|
73
|
global $config;
|
74
|
|
75
|
if(is_array($config['installedpackages']['package'])) {
|
76
|
$i = 0;
|
77
|
foreach($config['installedpackages']['package'] as $pkg) {
|
78
|
if($pkg['name'] == $pkg_name) return $i;
|
79
|
$i++;
|
80
|
}
|
81
|
}
|
82
|
return -1;
|
83
|
}
|
84
|
|
85
|
/****f* pkg-utils/get_pkg_info
|
86
|
* NAME
|
87
|
* get_pkg_info - Retrive package information from pfsense.com.
|
88
|
* INPUTS
|
89
|
* $pkgs - 'all' to retrive all packages, an array containing package names otherwise
|
90
|
* $info - 'all' to retrive all information, an array containing keys otherwise
|
91
|
* RESULT
|
92
|
* $raw_versions - Array containing retrieved information, indexed by package name.
|
93
|
******/
|
94
|
function get_pkg_info($pkgs = 'all', $info = 'all') {
|
95
|
global $g;
|
96
|
$params = array("pkg" => $pkgs, "info" => $info);
|
97
|
$resp = call_pfsense_method('pfsense.get_pkgs', $params, 10);
|
98
|
return $resp ? $resp : array();
|
99
|
}
|
100
|
|
101
|
function get_pkg_sizes($pkgs = 'all') {
|
102
|
global $g;
|
103
|
$params = array("pkg" => $pkgs);
|
104
|
$msg = new XML_RPC_Message('pfsense.get_pkg_sizes', array(php_value_to_xmlrpc($params)));
|
105
|
$cli = new XML_RPC_Client($g['xmlrpcpath'], $g['xmlrpcbaseurl']);
|
106
|
$resp = $cli->send($msg, 10);
|
107
|
if($resp and !$resp->faultCode()) {
|
108
|
$raw_versions = $resp->value();
|
109
|
return xmlrpc_value_to_php($raw_versions);
|
110
|
} else {
|
111
|
return array();
|
112
|
}
|
113
|
}
|
114
|
|
115
|
/*
|
116
|
* resync_all_package_configs() Force packages to setup their configuration and rc.d files.
|
117
|
* This function may also print output to the terminal indicating progress.
|
118
|
*/
|
119
|
function resync_all_package_configs($show_message = false) {
|
120
|
global $config;
|
121
|
$i = 0;
|
122
|
log_error("Resyncing configuration for all packages.");
|
123
|
if(!$config['installedpackages']['package']) return;
|
124
|
if($show_message == true) print "Syncing packages:";
|
125
|
foreach($config['installedpackages']['package'] as $package) {
|
126
|
if($show_message == true) print " " . $package['name'];
|
127
|
sync_package($i, true, true);
|
128
|
$i++;
|
129
|
}
|
130
|
if($show_message == true) print ".\n";
|
131
|
}
|
132
|
|
133
|
/*
|
134
|
* is_freebsd_pkg_installed() - Check /var/db/pkg to determine whether or not a FreeBSD
|
135
|
* package is installed.
|
136
|
*/
|
137
|
function is_freebsd_pkg_installed($pkg) {
|
138
|
global $g;
|
139
|
if(in_array($pkg, return_dir_as_array("{$g['vardb_path']}/pkg"))) return true;
|
140
|
return false;
|
141
|
}
|
142
|
|
143
|
/*
|
144
|
* get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", return_nosync = 1): Return a package's dependencies.
|
145
|
*
|
146
|
* $filetype = "all" || ".xml", ".tgz", etc.
|
147
|
* $format = "files" (full filenames) || "names" (stripped / parsed depend names)
|
148
|
* $return_nosync = 1 (return depends that have nosync set) | 0 (ignore packages with nosync)
|
149
|
*
|
150
|
*/
|
151
|
function get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", $return_nosync = 1) {
|
152
|
global $config;
|
153
|
$pkg_id = get_pkg_id($pkg_name);
|
154
|
if(!is_numeric($pkg_name)) {
|
155
|
if($pkg_id == -1) return -1; // This package doesn't really exist - exit the function.
|
156
|
} else {
|
157
|
if(!isset($config['installedpackages']['package'][$pkg_id])) return; // No package belongs to the pkg_id passed to this function.
|
158
|
}
|
159
|
$package = $config['installedpackages']['package'][$pkg_id];
|
160
|
if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
|
161
|
file_notice($package['name'], "The {$package['name']} package is missing its configuration file and must be reinstalled.", "Packages", "/pkg_mgr_install.php?mode=reinstallpkg&pkg={$package['name']}", 1);
|
162
|
return;
|
163
|
}
|
164
|
$pkg_xml = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
|
165
|
if($pkg_xml['additional_files_needed'] != "") {
|
166
|
foreach($pkg_xml['additional_files_needed'] as $item) {
|
167
|
if (($return_nosync == 0) && (isset($item['nosync']))) continue; // Do not return depends with nosync set if not required.
|
168
|
$depend_file = substr(strrchr($item['item']['0'], '/'),1); // Strip URLs down to filenames.
|
169
|
$depend_name = substr(substr($depend_file,0,strpos($depend_file,".")+1),0,-1); // Strip filename down to dependency name.
|
170
|
if (($filetype != "all") && (!preg_match("/${filetype}/i", $depend_file))) continue;
|
171
|
if ($item['prefix'] != "") {
|
172
|
$prefix = $item['prefix'];
|
173
|
} else {
|
174
|
$prefix = "/usr/local/pkg/";
|
175
|
}
|
176
|
if(!file_exists($prefix . $pkg_name)) {
|
177
|
file_notice($package['name'], "The {$package['name']} package is missing required dependencies and must be reinstalled.", "Packages", "/pkg_mgr_install.php?mode=reinstallpkg&pkg={$package['name']}", 1);
|
178
|
}
|
179
|
switch ($format) {
|
180
|
case "files":
|
181
|
$depends[] = $depend_file;
|
182
|
break;
|
183
|
case "names":
|
184
|
switch ($filetype) {
|
185
|
|
186
|
case "all":
|
187
|
if(preg_match("/\.xml/i", $depend_file)) {
|
188
|
$depend_xml = parse_xml_config_pkg("/usr/local/pkg/" . $depend_file, "packagegui");
|
189
|
$depends[] = $depend_xml['name'];
|
190
|
break;
|
191
|
} else {
|
192
|
$depends[] = $depend_name; // If this dependency isn't package XML, use the stripped filename.
|
193
|
break;
|
194
|
}
|
195
|
case ".xml":
|
196
|
$depend_xml = parse_xml_config_pkg("/usr/local/pkg/" . $depend_file, "packagegui");
|
197
|
$depends[] = $depend_xml['name'];
|
198
|
break;
|
199
|
default:
|
200
|
$depends[] = $depend_name; // If we aren't looking for XML, use the stripped filename (it's all we have).
|
201
|
break;
|
202
|
}
|
203
|
}
|
204
|
}
|
205
|
return $depends;
|
206
|
}
|
207
|
}
|
208
|
|
209
|
/*
|
210
|
* sync_package($pkg_name, $sync_depends = true, $show_message = false) Force a package to setup its configuration and rc.d files.
|
211
|
*/
|
212
|
function sync_package($pkg_name, $sync_depends = true, $show_message = false) {
|
213
|
global $config;
|
214
|
require_once("notices.inc");
|
215
|
if(!$config['installedpackages']['package']) return;
|
216
|
if(!is_numeric($pkg_name)) {
|
217
|
$pkg_id = get_pkg_id($pkg_name);
|
218
|
if($pkg_id == -1) return -1; // This package doesn't really exist - exit the function.
|
219
|
} else {
|
220
|
$pkg_id = $pkg_name;
|
221
|
if(!isset($config['installedpackages']['package'][$pkg_id]))
|
222
|
return; // No package belongs to the pkg_id passed to this function.
|
223
|
}
|
224
|
if (is_array($config['installedpackages']['package'][$pkg_id]))
|
225
|
$package = $config['installedpackages']['package'][$pkg_id];
|
226
|
else
|
227
|
return; /* empty package tag */
|
228
|
if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
|
229
|
file_notice($package['name'], "The {$package['name']} package is missing its configuration file and must be reinstalled.", "Packages", "/pkg_mgr_install.php?mode=reinstallpkg&pkg={$package['name']}", 1);
|
230
|
} else {
|
231
|
$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
|
232
|
|
233
|
/* Bring in package include files */
|
234
|
if (isset($pkg_config['include_file']) && $pkg_config['include_file'] != ""
|
235
|
) {
|
236
|
$include_file = $pkg_config['include_file'];
|
237
|
if (file_exists('/usr/local/pkg/' . $include_file))
|
238
|
require_once('/usr/local/pkg/' . $include_file);
|
239
|
else
|
240
|
require_once($include_file);
|
241
|
}
|
242
|
|
243
|
/* XXX: Zend complains about the next line "Wrong break depth"
|
244
|
* The code is obviously wrong, but I'm not sure what it's supposed to do?
|
245
|
*/
|
246
|
if(isset($pkg_config['nosync'])) continue;
|
247
|
if($pkg_config['custom_php_global_functions'] <> "")
|
248
|
eval($pkg_config['custom_php_global_functions']);
|
249
|
if($pkg_config['custom_php_resync_config_command'] <> "")
|
250
|
eval($pkg_config['custom_php_resync_config_command']);
|
251
|
if($sync_depends == true) {
|
252
|
$depends = get_pkg_depends($pkg_name, ".xml", "files", 1); // Call dependency handler and do a little more error checking.
|
253
|
if(is_array($depends)) {
|
254
|
foreach($depends as $item) {
|
255
|
if(!file_exists("/usr/local/pkg" . $item)) {
|
256
|
file_notice($package['name'], "The {$package['name']} package is missing required dependencies and must be reinstalled.", "Packages", "/pkg_mgr_install.php?mode=reinstallpkg&pkg={$package['name']}", 1);
|
257
|
} else {
|
258
|
$item_config = parse_xml_config_pkg("/usr/local/pkg/" . $item, "packagegui");
|
259
|
if(isset($item_config['nosync'])) continue;
|
260
|
if($item_config['custom_php_command_before_form'] <> "") {
|
261
|
eval($item_config['custom_php_command_before_form']);
|
262
|
}
|
263
|
if($item_config['custom_php_resync_config_command'] <> "") {
|
264
|
eval($item_config['custom_php_resync_config_command']);
|
265
|
}
|
266
|
if($show_message == true) print " " . $item_config['name'];
|
267
|
}
|
268
|
}
|
269
|
}
|
270
|
}
|
271
|
}
|
272
|
}
|
273
|
|
274
|
/*
|
275
|
* pkg_fetch_recursive: Download and install a FreeBSD package and its dependencies. This function provides output to
|
276
|
* a progress bar and output window.
|
277
|
*
|
278
|
* XXX: This function needs to return where a pkg_add fails. Our current error messages aren't very descriptive.
|
279
|
*/
|
280
|
function pkg_fetch_recursive($pkgname, $filename, $dependlevel = 0, $base_url = 'http://ftp2.freebsd.org/pub/FreeBSD/ports/i386/packages-5.4-release/Latest') {
|
281
|
global $pkgent, $static_output, $g, $fd_log;
|
282
|
$pkg_extension = strrchr($filename, '.');
|
283
|
$static_output .= "\n" . str_repeat(" ", $dependlevel * 2) . $pkgname . " ";
|
284
|
$fetchto = "/tmp/apkg_" . $pkgname . $pkg_extension;
|
285
|
download_file_with_progress_bar($base_url . '/' . $filename, $fetchto);
|
286
|
$static_output .= " (extracting)";
|
287
|
update_output_window($static_output);
|
288
|
$slaveout = "";
|
289
|
exec("/usr/bin/tar --fast-read -O -f {$fetchto} -x +CONTENTS 2>&1", $slaveout);
|
290
|
$workingdir = preg_grep("/instmp/", $slaveout);
|
291
|
$workingdir = $workingdir[0];
|
292
|
$raw_depends_list = array_values(preg_grep("/\@pkgdep/", $slaveout));
|
293
|
if($raw_depends_list != "") {
|
294
|
if($pkgent['exclude_dependency'] != "")
|
295
|
$raw_depends_list = array_values(preg_grep($pkgent['exclude_dependency'], PREG_GREP_INVERT));
|
296
|
foreach($raw_depends_list as $adepend) {
|
297
|
$working_depend = explode(" ", $adepend);
|
298
|
//$working_depend = explode("-", $working_depend[1]);
|
299
|
$depend_filename = $working_depend[1] . $pkg_extension;
|
300
|
if(is_freebsd_pkg_installed($working_depend[1]) === false) {
|
301
|
pkg_fetch_recursive($working_depend[1], $depend_filename, $dependlevel + 1, $base_url);
|
302
|
} else {
|
303
|
// $dependlevel++;
|
304
|
$static_output .= "\n" . str_repeat(" ", $dependlevel * 2) . $working_depend[1] . " ";
|
305
|
@fwrite($fd_log, $working_depend[1] . "\n");
|
306
|
}
|
307
|
}
|
308
|
}
|
309
|
$pkgaddout = "";
|
310
|
exec("cat {$g['tmp_path']}/y | /usr/sbin/pkg_add -fv {$fetchto} 2>&1", $pkgaddout);
|
311
|
@fwrite($fd_log, $pkgname . " " . print_r($pkgaddout, true) . "\n");
|
312
|
return true;
|
313
|
}
|
314
|
|
315
|
function download_file_with_progress_bar($url_file, $destination_file) {
|
316
|
global $ch, $fout, $file_size, $downloaded, $pkg_interface;
|
317
|
$file_size = 1;
|
318
|
$downloaded = 1;
|
319
|
/* open destination file */
|
320
|
$fout = fopen($destination_file, "wb");
|
321
|
|
322
|
/*
|
323
|
* Originally by Author: Keyvan Minoukadeh
|
324
|
* Modified by Scott Ullrich to return Content-Length size
|
325
|
*/
|
326
|
|
327
|
$ch = curl_init();
|
328
|
curl_setopt($ch, CURLOPT_URL, $url_file);
|
329
|
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
|
330
|
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'read_body');
|
331
|
curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
|
332
|
|
333
|
curl_exec($ch);
|
334
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
335
|
fclose($fout);
|
336
|
curl_close($ch);
|
337
|
return ($http_code == 200) ? true : $http_code;
|
338
|
}
|
339
|
|
340
|
function read_header($ch, $string) {
|
341
|
global $file_size, $fout;
|
342
|
$length = strlen($string);
|
343
|
$regs = "";
|
344
|
ereg("(Content-Length:) (.*)", $string, $regs);
|
345
|
if($regs[2] <> "") {
|
346
|
$file_size = intval($regs[2]);
|
347
|
}
|
348
|
ob_flush();
|
349
|
return $length;
|
350
|
}
|
351
|
|
352
|
function read_body($ch, $string) {
|
353
|
global $fout, $file_size, $downloaded, $sendto, $static_status, $static_output, $lastseen, $pkg_interface;
|
354
|
$length = strlen($string);
|
355
|
$downloaded += intval($length);
|
356
|
$downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
|
357
|
$downloadProgress = 100 - $downloadProgress;
|
358
|
if($lastseen <> $downloadProgress and $downloadProgress < 101) {
|
359
|
if($sendto == "status") {
|
360
|
$tostatus = $static_status . $downloadProgress . "%";
|
361
|
update_status($tostatus);
|
362
|
} else {
|
363
|
$tooutput = $static_output . $downloadProgress . "%";
|
364
|
update_output_window($tooutput);
|
365
|
}
|
366
|
update_progress_bar($downloadProgress);
|
367
|
$lastseen = $downloadProgress;
|
368
|
}
|
369
|
fwrite($fout, $string);
|
370
|
ob_flush();
|
371
|
return $length;
|
372
|
}
|
373
|
|
374
|
function install_package($package, $pkg_info = "") {
|
375
|
global $g, $config, $pkg_interface, $fd_log, $static_output;
|
376
|
/* open logfiles and begin installation */
|
377
|
if(!$fd_log) {
|
378
|
if(!$fd_log = fopen("{$g['tmp_path']}/pkg_mgr_{$package}.log", "w")) {
|
379
|
update_output_window("Warning, could not open log for writing.");
|
380
|
}
|
381
|
}
|
382
|
@fwrite($fd_log, "Beginning package installation.\n");
|
383
|
log_error('Beginning package installation for ' . $package . '.');
|
384
|
update_status("Beginning package installation for " . $package . "...");
|
385
|
/* fetch package information if needed */
|
386
|
if(!$pkg_info or !is_array($pkg_info[$package])) {
|
387
|
$pkg_info = get_pkg_info(array($package));
|
388
|
$pkg_info = $pkg_info[$package]; // We're only dealing with one package, so we can strip away the extra array.
|
389
|
}
|
390
|
/* fetch the package's configuration file */
|
391
|
if($pkg_info['config_file'] != "") {
|
392
|
$static_output .= "Downloading package configuration file... ";
|
393
|
update_output_window($static_output);
|
394
|
@fwrite($fd_log, "Downloading package configuration file...\n");
|
395
|
$fetchto = substr(strrchr($pkg_info['config_file'], '/'), 1);
|
396
|
download_file_with_progress_bar($pkg_info['config_file'], '/usr/local/pkg/' . $fetchto);
|
397
|
if(!file_exists('/usr/local/pkg/' . $fetchto)) {
|
398
|
@fwrite($fd_log, "ERROR! Unable to fetch package configuration file. Aborting installation.\n");
|
399
|
if($pkg_interface == "console") {
|
400
|
print "\nERROR! Unable to fetch package configuration file. Aborting package installation.\n";
|
401
|
return;
|
402
|
} else {
|
403
|
$static_output .= "failed!\n\nInstallation aborted.";
|
404
|
update_output_window($static_output);
|
405
|
echo "<br>Show <a href=\"pkg_mgr_install.php?showlog=true\">install log</a></center>";
|
406
|
return -1;
|
407
|
}
|
408
|
}
|
409
|
$static_output .= "done.\n";
|
410
|
update_output_window($static_output);
|
411
|
}
|
412
|
/* add package information to config.xml */
|
413
|
$pkgid = get_pkg_id($pkg_info['name']);
|
414
|
$static_output .= "Saving updated package information... ";
|
415
|
update_output_window($static_output);
|
416
|
if($pkgid == -1) {
|
417
|
$config['installedpackages']['package'][] = $pkg_info;
|
418
|
$changedesc = "Installed {$pkg_info['name']} package.";
|
419
|
$to_output = "done.\n";
|
420
|
} else {
|
421
|
$config['installedpackages']['package'][$pkgid] = $pkg_info;
|
422
|
$changedesc = "Overwrote previous installation of {$pkg_info['name']}.";
|
423
|
$to_output = "overwrite!\n";
|
424
|
}
|
425
|
$static_output .= $to_output;
|
426
|
update_output_window($static_output);
|
427
|
/* install other package components */
|
428
|
install_package_xml($package);
|
429
|
$static_output .= "Writing configuration... ";
|
430
|
update_output_window($static_output);
|
431
|
write_config($changedesc);
|
432
|
$static_output .= "done.\n";
|
433
|
update_output_window($static_output);
|
434
|
$static_output .= "Starting service.\n";
|
435
|
update_output_window($static_output);
|
436
|
start_service($pkg_info['config_file']);
|
437
|
}
|
438
|
|
439
|
function eval_once($toeval) {
|
440
|
global $evaled;
|
441
|
if(!$evaled) $evaled = array();
|
442
|
$evalmd5 = md5($toeval);
|
443
|
if(!in_array($evalmd5, $evaled)) {
|
444
|
eval($toeval);
|
445
|
$evaled[] = $evalmd5;
|
446
|
}
|
447
|
return;
|
448
|
}
|
449
|
|
450
|
function install_package_xml($pkg) {
|
451
|
global $g, $config, $fd_log, $static_output;
|
452
|
if(($pkgid = get_pkg_id($pkg)) == -1) {
|
453
|
$static_output .= "The {$pkg} package is not installed.\n\nInstallation aborted.";
|
454
|
update_output_window($static_output);
|
455
|
echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
|
456
|
echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
|
457
|
sleep(1);
|
458
|
return;
|
459
|
} else {
|
460
|
$pkg_info = $config['installedpackages']['package'][$pkgid];
|
461
|
}
|
462
|
/* set up logging if needed */
|
463
|
if(!$fd_log) {
|
464
|
if(!$fd_log = fopen("{$g['tmp_path']}/pkg_mgr_{$pkg}.log", "w")) {
|
465
|
update_output_window("Warning, could not open log for writing.");
|
466
|
}
|
467
|
}
|
468
|
|
469
|
/* set up package logging streams */
|
470
|
if($pkg_info['logging']) {
|
471
|
mwexec("/usr/sbin/clog -i -s 32768 {$g['varlog_path']}/{$pkg_info['logging']['logfilename']}");
|
472
|
chmod($g['varlog_path'] . '/' . $pkg_info['logging']['logfilename'], 0600);
|
473
|
@fwrite($fd_log, "Adding text to file /etc/syslog.conf\n");
|
474
|
mwexec("killall syslogd");
|
475
|
system_syslogd_start();
|
476
|
}
|
477
|
|
478
|
/* make 'y' file */
|
479
|
$fd = fopen("{$g['tmp_path']}/y", "w");
|
480
|
for($line = 0; $line < 10; $line++) {
|
481
|
fwrite($fd, "y\n");
|
482
|
}
|
483
|
fclose($fd);
|
484
|
|
485
|
/* pkg_add the package and its dependencies */
|
486
|
if($pkg_info['depends_on_package_base_url'] != "") {
|
487
|
update_status("Installing " . $pkg_info['name'] . " and its dependencies.");
|
488
|
$static_output .= "Downloading " . $pkg_info['name'] . " and its dependencies... ";
|
489
|
$static_orig = $static_output;
|
490
|
$static_output .= "\n";
|
491
|
update_output_window($static_output);
|
492
|
foreach((array) $pkg_info['depends_on_package'] as $pkgdep) {
|
493
|
$pkg_name = substr(reverse_strrchr($pkgdep, "."), 0, -1);
|
494
|
if(isset($pkg_info['skip_install_checks'])) {
|
495
|
$pkg_installed = true;
|
496
|
} else {
|
497
|
$pkg_installed = is_freebsd_pkg_installed($pkg_name);
|
498
|
}
|
499
|
if($pkg_installed == false) pkg_fetch_recursive($pkg_name, $pkgdep, 0, $pkg_info['depends_on_package_base_url']);
|
500
|
$static_output = $static_orig . "done.\nChecking for successful package installation... ";
|
501
|
update_output_window($static_output);
|
502
|
/* make sure our package was successfully installed */
|
503
|
if($pkg_installed == false) $pkg_installed = is_freebsd_pkg_installed($pkg_name);
|
504
|
if($pkg_installed == true) {
|
505
|
$static_output .= "done.\n";
|
506
|
update_output_window($static_output);
|
507
|
fwrite($fd_log, "pkg_add successfully completed.\n");
|
508
|
} else {
|
509
|
$static_output .= "failed!\n\nInstallation aborted.";
|
510
|
update_output_window($static_output);
|
511
|
fwrite($fd_log, "Package WAS NOT installed properly.\n");
|
512
|
fclose($fd_log);
|
513
|
echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
|
514
|
echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
|
515
|
sleep(1);
|
516
|
die;
|
517
|
}
|
518
|
}
|
519
|
}
|
520
|
$configfile = substr(strrchr($pkg_info['config_file'], '/'), 1);
|
521
|
if(file_exists("/usr/local/pkg/" . $configfile)) {
|
522
|
$static_output .= "Loading package configuration... ";
|
523
|
update_output_window($static_output);
|
524
|
$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $configfile, "packagegui");
|
525
|
$static_output .= "done.\n";
|
526
|
update_output_window($static_output);
|
527
|
$static_output .= "Configuring package components...\n";
|
528
|
update_output_window($static_output);
|
529
|
/* modify system files */
|
530
|
if($pkg_config['modify_system']['item'] <> "") {
|
531
|
$static_output .= "\tSystem files... ";
|
532
|
update_output_window($static_output);
|
533
|
foreach($pkg_config['modify_system']['item'] as $ms) {
|
534
|
if($ms['textneeded']) {
|
535
|
add_text_to_file($ms['modifyfilename'], $ms['textneeded']);
|
536
|
}
|
537
|
}
|
538
|
$static_output .= "done.\n";
|
539
|
update_output_window($static_output);
|
540
|
}
|
541
|
/* download additional files */
|
542
|
if($pkg_config['additional_files_needed'] <> "") {
|
543
|
$static_output .= "\tAdditional files... ";
|
544
|
$static_orig = $static_output;
|
545
|
update_output_window($static_output);
|
546
|
foreach($pkg_config['additional_files_needed'] as $afn) {
|
547
|
$filename = get_filename_from_url($afn['item'][0]);
|
548
|
if($afn['chmod'] <> "") {
|
549
|
$pkg_chmod = $afn['chmod'];
|
550
|
} else {
|
551
|
$pkg_chmod = "";
|
552
|
}
|
553
|
if($afn['prefix'] <> "") {
|
554
|
$prefix = $afn['prefix'];
|
555
|
} else {
|
556
|
$prefix = "/usr/local/pkg/";
|
557
|
}
|
558
|
$static_output .= $filename . " ";
|
559
|
update_output_window($static_output);
|
560
|
download_file_with_progress_bar($afn['item'][0], $prefix . $filename);
|
561
|
if(stristr($filename, ".tgz") <> "") {
|
562
|
fwrite($fd_log, "Extracting tarball to -C for " . $filename . "...\n");
|
563
|
$tarout = "";
|
564
|
exec("/usr/bin/tar xvzf " . $prefix . $filename . " -C / 2>&1", $tarout);
|
565
|
fwrite($fd_log, print_r($tarout, true) . "\n");
|
566
|
}
|
567
|
if($pkg_chmod <> "") {
|
568
|
fwrite($fd_log, "Changing file mode to {$pkg_chmod} for {$prefix}{$filename}\n");
|
569
|
chmod($prefix . $filename, $pkg_chmod);
|
570
|
system("/bin/chmod {$pkg_chmod} {$prefix}{$filename}");
|
571
|
}
|
572
|
$static_output = $static_orig;
|
573
|
update_output_window($static_output);
|
574
|
}
|
575
|
$static_output .= "done.\n";
|
576
|
update_output_window($static_output);
|
577
|
}
|
578
|
/* if a require exists, include it. this will
|
579
|
* show us where an error exists in a package
|
580
|
* instead of making us blindly guess
|
581
|
*/
|
582
|
if($pkg_config['include_file'] <> "") {
|
583
|
$static_output = "Loading package instructions...";
|
584
|
update_output_window($static_output);
|
585
|
fwrite($fd_log, "require_once('include_file')\n");
|
586
|
require_once($pkg_config['include_file']);
|
587
|
}
|
588
|
/* sidebar items */
|
589
|
if($pkg_config['menu'] != "") {
|
590
|
$static_output .= "\tMenu items... ";
|
591
|
update_output_window($static_output);
|
592
|
if(is_array($pkg_config['menu'])) {
|
593
|
foreach($pkg_config['menu'] as $menu) {
|
594
|
if(is_array($config['installedpackages']['menu'])) {
|
595
|
foreach($config['installedpackages']['menu'] as $amenu) {
|
596
|
if($amenu['name'] == $menu['name']) continue 2;
|
597
|
}
|
598
|
}
|
599
|
$config['installedpackages']['menu'][] = $menu;
|
600
|
}
|
601
|
}
|
602
|
$static_output .= "done.\n";
|
603
|
update_output_window($static_output);
|
604
|
}
|
605
|
/* services */
|
606
|
if($pkg_config['service'] != "") {
|
607
|
$static_output .= "\tServices... ";
|
608
|
update_output_window($static_output);
|
609
|
foreach($pkg_config['service'] as $service) {
|
610
|
$config['installedpackages']['service'][] = $service;
|
611
|
}
|
612
|
$static_output .= "done.\n";
|
613
|
update_output_window($static_output);
|
614
|
}
|
615
|
/* custom commands */
|
616
|
$static_output .= "\tCustom commands... ";
|
617
|
update_output_window($static_output);
|
618
|
if($pkg_config['custom_php_global_functions'] <> "") {
|
619
|
$static_output = "Executing custom_php_global_functions()...";
|
620
|
update_output_window($static_output);
|
621
|
eval_once($pkg_config['custom_php_global_functions']);
|
622
|
}
|
623
|
if($pkg_config['custom_php_install_command']) {
|
624
|
$static_output = "Executing custom_php_install_command()...";
|
625
|
update_output_window($static_output);
|
626
|
eval_once($pkg_config['custom_php_install_command']);
|
627
|
}
|
628
|
if($pkg_config['custom_php_resync_config_command'] <> "") {
|
629
|
$static_output = "Executing custom_php_resync_config_command()...";
|
630
|
update_output_window($static_output);
|
631
|
eval_once($pkg_config['custom_php_resync_config_command']);
|
632
|
}
|
633
|
$static_output .= "done.\n";
|
634
|
update_output_window($static_output);
|
635
|
} else {
|
636
|
$static_output .= "Loading package configuration... failed!\n\nInstallation aborted.";
|
637
|
update_output_window($static_output);
|
638
|
fwrite($fd_log, "Unable to load package configuration. Installation aborted.\n");
|
639
|
fclose($fd_log);
|
640
|
echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
|
641
|
echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
|
642
|
sleep(1);
|
643
|
return;
|
644
|
}
|
645
|
}
|
646
|
|
647
|
function delete_package($pkg, $pkgid) {
|
648
|
global $g, $config, $fd_log, $static_output;
|
649
|
update_status("Removing package...");
|
650
|
$static_output .= "Removing package... ";
|
651
|
update_output_window($static_output);
|
652
|
$pkgid = get_pkg_id($pkgid);
|
653
|
$pkg_info = $config['installedpackages']['package'][$pkgid];
|
654
|
|
655
|
$configfile = $pkg_info['configurationfile'];
|
656
|
if(file_exists("/usr/local/pkg/" . $configfile)) {
|
657
|
$static_output .= "\nLoading package configuration $configfile... ";
|
658
|
update_output_window($static_output);
|
659
|
$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $configfile, "packagegui");
|
660
|
/* if a require exists, include it. this will
|
661
|
* show us where an error exists in a package
|
662
|
* instead of making us blindly guess
|
663
|
*/
|
664
|
if($pkg_config['include_file'] <> "") {
|
665
|
$static_output .= "\nLoading package instructions...\n";
|
666
|
update_output_window($static_output);
|
667
|
require_once($pkg_config['include_file']);
|
668
|
}
|
669
|
}
|
670
|
$static_output .= "\Starting package deletion...\n";
|
671
|
update_output_window($static_output);
|
672
|
delete_package_recursive($pkg);
|
673
|
$static_output .= "done.\n";
|
674
|
update_output_window($static_output);
|
675
|
return;
|
676
|
}
|
677
|
|
678
|
function delete_package_recursive($pkg) {
|
679
|
$info = "";
|
680
|
exec("/usr/sbin/pkg_info -r " . $pkg . " 2>&1", $info);
|
681
|
exec("cat {$g['tmp_path']}/y | /usr/sbin/pkg_delete " . $pkg ." > /dev/null 2>&1");
|
682
|
$pkgdb = "";
|
683
|
exec("/bin/ls /var/db/pkg", $pkgdb);
|
684
|
if(stristr($info[0], "can't find package") != false) return;
|
685
|
foreach($info as $line) {
|
686
|
$depend = trim(array_pop(explode(":", $line)));
|
687
|
if(in_array($depend, $pkgdb)) delete_package_recursive($depend);
|
688
|
}
|
689
|
$fd = fopen("{$g['tmp_path']}/y", "w");
|
690
|
for($line = 0; $line < 10; $line++) {
|
691
|
fwrite($fd, "y\n");
|
692
|
}
|
693
|
fclose($fd);
|
694
|
return;
|
695
|
}
|
696
|
|
697
|
function delete_package_xml($pkg) {
|
698
|
global $g, $config, $fd_log, $static_output;
|
699
|
if(($pkgid = get_pkg_id($pkg)) == -1) {
|
700
|
$static_output .= "The {$pkg} package is not installed.\n\nDeletion aborted.";
|
701
|
update_output_window($static_output);
|
702
|
echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
|
703
|
echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
|
704
|
ob_flush();
|
705
|
sleep(1);
|
706
|
return;
|
707
|
}
|
708
|
/* set up logging if needed */
|
709
|
if(!$fd_log) {
|
710
|
if(!$fd_log = fopen("{$g['tmp_path']}/pkg_mgr_{$pkg}.log", "w")) {
|
711
|
update_output_window("Warning, could not open log for writing.");
|
712
|
}
|
713
|
}
|
714
|
update_status("Removing {$pkg} components...");
|
715
|
fwrite($fd_log, "Removing {$pkg} package... ");
|
716
|
$static_output .= "Removing {$pkg} components...\n";
|
717
|
update_output_window($static_output);
|
718
|
/* parse package configuration */
|
719
|
$packages = &$config['installedpackages']['package'];
|
720
|
$menus = &$config['installedpackages']['menu'];
|
721
|
$services = &$config['installedpackages']['service'];
|
722
|
if(file_exists("/usr/local/pkg/" . $packages[$pkgid]['configurationfile'])) {
|
723
|
$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $packages[$pkgid]['configurationfile'], "packagegui");
|
724
|
/* remove menu items */
|
725
|
if(is_array($pkg_config['menu'])) {
|
726
|
$static_output .= "\tMenu items... ";
|
727
|
update_output_window($static_output);
|
728
|
foreach($menus as $menu) $instmenus[] = $menu['name'];
|
729
|
foreach($pkg_config['menu'] as $menu) {
|
730
|
foreach($instmenus as $key => $instmenu) {
|
731
|
if($instmenu == $menu['name']) unset($menus[$key]);
|
732
|
}
|
733
|
}
|
734
|
$static_output .= "done.\n";
|
735
|
update_output_window($static_output);
|
736
|
}
|
737
|
/* remove services */
|
738
|
if(is_array($pkg_config['service'])) {
|
739
|
$static_output .= "\tServices... ";
|
740
|
update_output_window($static_output);
|
741
|
foreach($services as $service) $instservices[] = $service['name'];
|
742
|
foreach($pkg_config['service'] as $service) {
|
743
|
foreach($instservices as $key => $instservice) {
|
744
|
if($instservice == $service['name']) {
|
745
|
stop_service($service['name']);
|
746
|
unset($services[$key]);
|
747
|
}
|
748
|
}
|
749
|
}
|
750
|
$static_output .= "done.\n";
|
751
|
update_output_window($static_output);
|
752
|
}
|
753
|
/* if a require exists, include it. this will
|
754
|
* show us where an error exists in a package
|
755
|
* instead of making us blindly guess
|
756
|
*/
|
757
|
if($pkg_config['include_file'] <> "") {
|
758
|
$static_output = "Loading package instructions...";
|
759
|
update_output_window($static_output);
|
760
|
fwrite($fd_log, "require_once('include_file')\n");
|
761
|
if(file_exists($pkg_config['include_file']))
|
762
|
require_once($pkg_config['include_file']);
|
763
|
fwrite($fd_log, "require_once('include_file') included\n");
|
764
|
}
|
765
|
/* evalate this package's global functions and pre deinstall commands */
|
766
|
if($pkg_config['custom_php_global_functions'] <> "")
|
767
|
eval_once($pkg_config['custom_php_global_functions']);
|
768
|
if($pkg_config['custom_php_pre_deinstall_command'] <> "")
|
769
|
eval_once($pkg_config['custom_php_pre_deinstall_command']);
|
770
|
/* remove all additional files */
|
771
|
if($pkg_config['additional_files_needed'] <> "") {
|
772
|
$static_output .= "\tAuxiliary files... ";
|
773
|
update_output_window($static_output);
|
774
|
foreach($pkg_config['additional_files_needed'] as $afn) {
|
775
|
$filename = get_filename_from_url($afn['item'][0]);
|
776
|
if($afn['prefix'] <> "") {
|
777
|
$prefix = $afn['prefix'];
|
778
|
} else {
|
779
|
$prefix = "/usr/local/pkg/";
|
780
|
}
|
781
|
unlink_if_exists($prefix . $filename);
|
782
|
if(file_exists($prefix . $filename))
|
783
|
mwexec("rm -rf {$prefix}{$filename}");
|
784
|
}
|
785
|
$static_output .= "done.\n";
|
786
|
update_output_window($static_output);
|
787
|
}
|
788
|
/* system files */
|
789
|
if($pkg_config['modify_system']['item'] <> "") {
|
790
|
$static_output .= "\tSystem files... ";
|
791
|
update_output_window($static_output);
|
792
|
foreach($pkg_config['modify_system']['item'] as $ms) {
|
793
|
if($ms['textneeded']) remove_text_from_file($ms['modifyfilename'], $ms['textneeded']);
|
794
|
}
|
795
|
$static_output .= "done.\n";
|
796
|
update_output_window($static_output);
|
797
|
}
|
798
|
/* syslog */
|
799
|
if($pkg_config['logging']['logfile_name'] <> "") {
|
800
|
$static_output .= "\tSyslog entries... ";
|
801
|
update_output_window($static_output);
|
802
|
remove_text_from_file("/etc/syslog.conf", $pkg_config['logging']['facilityname'] . "\t\t\t\t" . $pkg_config['logging']['logfilename']);
|
803
|
$static_output .= "done.\n";
|
804
|
update_output_window($static_output);
|
805
|
}
|
806
|
/* deinstall commands */
|
807
|
if($pkg_config['custom_php_deinstall_command'] <> "") {
|
808
|
$static_output .= "\tDeinstall commands... ";
|
809
|
update_output_window($static_output);
|
810
|
eval_once($pkg_config['custom_php_deinstall_command']);
|
811
|
$static_output .= "done.\n";
|
812
|
update_output_window($static_output);
|
813
|
}
|
814
|
/* package XML file */
|
815
|
$static_output .= "\tPackage XML... ";
|
816
|
update_output_window($static_output);
|
817
|
unlink_if_exists("/usr/local/pkg/" . $packages[$pkgid]['configurationfile']);
|
818
|
$static_output .= "done.\n";
|
819
|
update_output_window($static_output);
|
820
|
}
|
821
|
/* remove config.xml entries */
|
822
|
$static_output .= "\tConfiguration... ";
|
823
|
update_output_window($static_output);
|
824
|
unset($config['installedpackages']['package'][$pkgid]);
|
825
|
$static_output .= "done.\n";
|
826
|
update_output_window($static_output);
|
827
|
write_config("Removed {$pkg} package.");
|
828
|
/* file cleanup */
|
829
|
$ctag = file("/etc/crontab");
|
830
|
foreach($ctag as $line) {
|
831
|
if(trim($line) != "") $towrite[] = $line;
|
832
|
}
|
833
|
$tmptab = fopen("/tmp/crontab", "w");
|
834
|
foreach($towrite as $line) {
|
835
|
fwrite($tmptab, $line);
|
836
|
}
|
837
|
fclose($tmptab);
|
838
|
rename("/tmp/crontab", "/etc/crontab");
|
839
|
}
|
840
|
|
841
|
function expand_to_bytes($size) {
|
842
|
$conv = array(
|
843
|
"G" => "3",
|
844
|
"M" => "2",
|
845
|
"K" => "1",
|
846
|
"B" => "0"
|
847
|
);
|
848
|
$suffix = substr($size, -1);
|
849
|
if(!in_array($suffix, array_keys($conv))) return $size;
|
850
|
$size = substr($size, 0, -1);
|
851
|
for($i = 0; $i < $conv[$suffix]; $i++) {
|
852
|
$size *= 1024;
|
853
|
}
|
854
|
return $size;
|
855
|
}
|
856
|
|
857
|
function get_pkg_db() {
|
858
|
global $g;
|
859
|
return return_dir_as_array($g['vardb_path'] . '/pkg');
|
860
|
}
|
861
|
|
862
|
function walk_depend($depend, $pkgdb = "", $alreadyseen = "") {
|
863
|
if(!$pkgdb) $pkgdb = get_pkg_db();
|
864
|
if(!$alreadyseen) $alreadyseen = array();
|
865
|
foreach($depend as $adepend) {
|
866
|
$pkgname = reverse_strrchr($adepend['name'], '.');
|
867
|
if(in_array($pkgname, $alreadyseen)) {
|
868
|
continue;
|
869
|
} elseif(!in_array($pkgname, $pkgdb)) {
|
870
|
$size += expand_to_bytes($adepend['size']);
|
871
|
$alreadyseen[] = $pkgname;
|
872
|
if(is_array($adepend['depend'])) $size += walk_depend($adepend['depend'], $pkgdb, $alreadyseen);
|
873
|
} else {
|
874
|
continue;
|
875
|
}
|
876
|
}
|
877
|
return $size;
|
878
|
}
|
879
|
|
880
|
function get_package_install_size($pkg = 'all', $pkg_info = "") {
|
881
|
global $config, $g;
|
882
|
if((!is_array($pkg)) and ($pkg != 'all')) $pkg = array($pkg);
|
883
|
$pkgdb = get_pkg_db();
|
884
|
if(!$pkg_info) $pkg_info = get_pkg_sizes($pkg);
|
885
|
foreach($pkg as $apkg) {
|
886
|
if(!$pkg_info[$apkg]) continue;
|
887
|
$toreturn[$apkg] = expand_to_bytes(walk_depend(array($pkg_info[$apkg]), $pkgdb));
|
888
|
}
|
889
|
return $toreturn;
|
890
|
}
|
891
|
|
892
|
function squash_from_bytes($size, $round = "") {
|
893
|
$conv = array(1 => "B", "K", "M", "G");
|
894
|
foreach($conv as $div => $suffix) {
|
895
|
$sizeorig = $size;
|
896
|
if(($size /= 1024) < 1) {
|
897
|
if($round) {
|
898
|
$sizeorig = round($sizeorig, $round);
|
899
|
}
|
900
|
return $sizeorig . $suffix;
|
901
|
}
|
902
|
}
|
903
|
return;
|
904
|
}
|
905
|
?>
|