Project

General

Profile

Download (44.4 KB) Statistics
| Branch: | Tag: | Revision:
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) 2010 Ermal Lu�i
12
 * Copyright (C) 2005-2006 Colin Smith (ethethlay@gmail.com)
13
 * All rights reserved.
14
 * Redistribution and use in source and binary forms, with or without
15
 * modification, are permitted provided that the following conditions are met:
16
 *
17
 * 1. Redistributions of source code must retain the above copyright notice,
18
 * this list of conditions and the following disclaimer.
19
 *
20
 * 2. Redistributions in binary form must reproduce the above copyright
21
 * notice, this list of conditions and the following disclaimer in the
22
 * documentation and/or other materials provided with the distribution.
23
 *
24
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
26
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
 * RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
 * POSSIBILITY OF SUCH DAMAGE.
34
 *
35
 */
36

    
37
/*
38
	pfSense_BUILDER_BINARIES:	/usr/bin/cd	/usr/bin/tar	/usr/sbin/fifolog_create	/bin/chmod
39
	pfSense_BUILDER_BINARIES:	/usr/sbin/pkg_add	/usr/sbin/pkg_info	/usr/sbin/pkg_delete	/bin/rm
40
	pfSense_MODULE:	pkg
41
*/
42

    
43
require_once("globals.inc");
44
require_once("xmlrpc.inc");
45
require_once("service-utils.inc");
46
if(file_exists("/cf/conf/use_xmlreader"))
47
	require_once("xmlreader.inc");
48
else
49
	require_once("xmlparse.inc");
50
require_once("service-utils.inc");
51
require_once("pfsense-utils.inc");
52

    
53
if(!function_exists("update_status")) {
54
	function update_status($status) {
55
		echo $status . "\n";
56
	}
57
}
58
if(!function_exists("update_output_window")) {
59
	function update_output_window($status) {
60
		echo $status . "\n";
61
	}
62
}
63

    
64
if (!function_exists("pkg_debug")) {
65
	/* set up logging if needed */
66
	function pkg_debug($msg) {
67
		global $g, $debug, $fd_log;
68

    
69
		if (!$debug)
70
			return;
71

    
72
		if (!$fd_log) {
73
			if (!$fd_log = fopen("{$g['tmp_path']}/pkg_mgr_{$package}.log", "w"))
74
				update_output_window("Warning, could not open log for writing.");
75
		}
76
		@fwrite($fd_log, $msg);
77
	}
78
}
79

    
80
$vardb = "/var/db/pkg";
81
safe_mkdir($vardb);
82
$g['platform'] = trim(file_get_contents("/etc/platform"));
83

    
84
conf_mount_rw();
85
if(!is_dir("/usr/local/pkg") or !is_dir("/usr/local/pkg/pf")) {
86
	safe_mkdir("/usr/local/pkg");
87
	safe_mkdir("/usr/local/pkg/pf");	
88
}
89
conf_mount_ro();
90

    
91
/****f* pkg-utils/remove_package
92
 * NAME
93
 *   remove_package - Removes package from FreeBSD if it exists
94
 * INPUTS
95
 *   $packagestring	- name/string to check for
96
 * RESULT
97
 *   none
98
 * NOTES
99
 *   
100
 ******/
101
function remove_freebsd_package($packagestring) {
102
	// The packagestring passed in must be the full PBI package name, 
103
	// as displayed by the pbi_info utility. e.g. "package-1.2.3_4-i386" 
104
	// It must NOT have ".pbi" on the end.
105
	exec("/usr/local/sbin/pbi_info {$packagestring} | /usr/bin/awk '/Prefix/ {print $2}'",$pbidir);
106
	$pbidir = $pbidir[0];
107
	if ($pbidir == "") {
108
		log_error("PBI dir for {$packagestring} was not found - cannot cleanup PBI files");
109
	}
110
	else {
111
		$linkdirs = array('bin','sbin');
112
		foreach($linkdirs as $dir) {
113
			if(is_dir("{$pbidir}/{$dir}")) {
114
				$files = scandir("{$pbidir}/{$dir}");
115
				foreach($files as $f) {
116
					if($f != '.' && $f != '..') {
117
						unlink("/usr/local/{$dir}/{$f}");
118
					}
119
				}
120
			}
121
		}
122

    
123
		exec("/usr/local/sbin/pbi_delete {$packagestring} 2>>/tmp/pbi_delete_errors.txt");
124
	}
125
}
126

    
127
/****f* pkg-utils/is_package_installed
128
 * NAME
129
 *   is_package_installed - Check whether a package is installed.
130
 * INPUTS
131
 *   $packagename	- name of the package to check
132
 * RESULT
133
 *   boolean	- true if the package is installed, false otherwise
134
 * NOTES
135
 *   This function is deprecated - get_pkg_id() can already check for installation.
136
 ******/
137
function is_package_installed($packagename) {
138
	$pkg = get_pkg_id($packagename);
139
	if($pkg == -1)
140
		return false;
141
	return true;
142
}
143

    
144
/****f* pkg-utils/get_pkg_id
145
 * NAME
146
 *   get_pkg_id - Find a package's numeric ID.
147
 * INPUTS
148
 *   $pkg_name	- name of the package to check
149
 * RESULT
150
 *   integer    - -1 if package is not found, >-1 otherwise
151
 ******/
152
function get_pkg_id($pkg_name) {
153
	global $config;
154

    
155
	if (is_array($config['installedpackages']['package'])) {
156
		foreach($config['installedpackages']['package'] as $idx => $pkg) {
157
			if($pkg['name'] == $pkg_name)
158
				return $idx;
159
		}
160
	}
161
	return -1;
162
}
163

    
164
/****f* pkg-utils/get_pkg_info
165
 * NAME
166
 *   get_pkg_info - Retrieve package information from pfsense.com.
167
 * INPUTS
168
 *   $pkgs - 'all' to retrieve all packages, an array containing package names otherwise
169
 *   $info - 'all' to retrieve all information, an array containing keys otherwise
170
 * RESULT
171
 *   $raw_versions - Array containing retrieved information, indexed by package name.
172
 ******/
173
function get_pkg_info($pkgs = 'all', $info = 'all') {
174
	global $g;
175

    
176
	$freebsd_version = php_uname("r");
177
	$freebsd_machine = php_uname("m");
178
	$params = array(
179
		"pkg" => $pkgs, 
180
		"info" => $info, 
181
		"freebsd_version" => $freebsd_version[0],
182
		"freebsd_machine" => $freebsd_machine
183
	);
184
	$resp = call_pfsense_method('pfsense.get_pkgs', $params, 10);
185
	return $resp ? $resp : array();
186
}
187

    
188
function get_pkg_sizes($pkgs = 'all') {
189
	global $config, $g;
190

    
191
	$freebsd_version = php_uname("r");
192
	$freebsd_machine = php_uname("m");
193
	$params = array(
194
		"pkg" => $pkgs, 
195
		"freebsd_version" => $freebsd_version,
196
		"freebsd_machine" => $freebsd_machine
197
	);
198
	$msg = new XML_RPC_Message('pfsense.get_pkg_sizes', array(php_value_to_xmlrpc($params)));
199
	$xmlrpc_base_url = isset($config['system']['altpkgrepo']['enable']) ? $config['system']['altpkgrepo']['xmlrpcbaseurl'] : $g['xmlrpcbaseurl'];
200
	$cli = new XML_RPC_Client($g['xmlrpcpath'], $xmlrpc_base_url);
201
	$resp = $cli->send($msg, 10);
202
	if(!is_object($resp))
203
		log_error("Could not get response from XMLRPC server!");
204
 	else if (!$resp->faultCode()) {
205
		$raw_versions = $resp->value();
206
		return xmlrpc_value_to_php($raw_versions);
207
	}
208

    
209
	return array();
210
}
211

    
212
/*
213
 * resync_all_package_configs() Force packages to setup their configuration and rc.d files.
214
 * This function may also print output to the terminal indicating progress.
215
 */
216
function resync_all_package_configs($show_message = false) {
217
	global $config, $pkg_interface, $g;
218

    
219
	log_error(gettext("Resyncing configuration for all packages."));
220

    
221
	if (!is_array($config['installedpackages']['package']))
222
		return;
223

    
224
	if($show_message == true)
225
		echo "Syncing packages:";
226

    
227
	conf_mount_rw();
228

    
229
	foreach($config['installedpackages']['package'] as $idx => $package) {
230
		if (empty($package['name']))
231
			continue;
232
		if($show_message == true)
233
			echo " " . $package['name'];
234
		get_pkg_depends($package['name'], "all");
235
		if($g['booting'] != true)
236
			stop_service($package['name']);
237
		sync_package($idx, true, true);
238
		if($pkg_interface == "console") 
239
			echo "\n" . gettext("Syncing packages:");
240
	}
241

    
242
	if($show_message == true)
243
		echo " done.\n";
244

    
245
	@unlink("/conf/needs_package_sync");
246
	conf_mount_ro();
247
}
248

    
249
/*
250
 * is_freebsd_pkg_installed() - Check /var/db/pkg to determine whether or not a FreeBSD
251
 *				package is installed.
252
 */
253
function is_freebsd_pkg_installed($pkg) {
254
	if(!$pkg) 
255
		return;
256
	$output = "";
257
	exec("/usr/local/sbin/pbi_info \"{$pkg}\"", $output, $retval);
258

    
259
	return (intval($retval) == 0);
260
}
261

    
262
/*
263
 * get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", return_nosync = 1):  Return a package's dependencies.
264
 *
265
 * $filetype = "all" || ".xml", ".tgz", etc.
266
 * $format = "files" (full filenames) || "names" (stripped / parsed depend names)
267
 * $return_nosync = 1 (return depends that have nosync set) | 0 (ignore packages with nosync)
268
 *
269
 */
270
function get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", $return_nosync = 1) {
271
	global $config;
272

    
273
	$pkg_id = get_pkg_id($pkg_name);
274
	if($pkg_id == -1)
275
		return -1; // This package doesn't really exist - exit the function.
276
	else if (!isset($config['installedpackages']['package'][$pkg_id]))
277
		return; // No package belongs to the pkg_id passed to this function.
278

    
279
	$package =& $config['installedpackages']['package'][$pkg_id];
280
	if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
281
		log_error(sprintf(gettext('The %1$s package is missing required dependencies and must be reinstalled. %2$s'), $package['name'], $package['configurationfile']));
282
		uninstall_package($package['name']);
283
		if (install_package($package['name']) < 0) {
284
			log_error("Failed reinstalling package {$package['name']}.");
285
			return false;
286
		}
287
	}
288
	$pkg_xml = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
289
	if (!empty($pkg_xml['additional_files_needed'])) {
290
		foreach($pkg_xml['additional_files_needed'] as $item) {
291
			if ($return_nosync == 0 && isset($item['nosync']))
292
				continue; // Do not return depends with nosync set if not required.
293
			$depend_file = substr(strrchr($item['item']['0'], '/'),1); // Strip URLs down to filenames.
294
			$depend_name = substr(substr($depend_file,0,strpos($depend_file,".")+1),0,-1); // Strip filename down to dependency name.
295
			if (($filetype != "all") && (!preg_match("/{$filetype}/i", $depend_file)))
296
					continue;
297
			if ($item['prefix'] != "")
298
				$prefix = $item['prefix'];
299
			else
300
				$prefix = "/usr/local/pkg/";
301
			// Ensure that the prefix exists to avoid installation errors.
302
			if(!is_dir($prefix)) 
303
				exec("/bin/mkdir -p {$prefix}");
304
			if(!file_exists($prefix . $depend_file))
305
				log_error(sprintf(gettext("The %s package is missing required dependencies and must be reinstalled."), $package['name']));
306
			switch ($format) {
307
			case "files":
308
				$depends[] = $prefix . $depend_file;
309
				break;
310
			case "names":
311
				switch ($filetype) {
312
				case "all":
313
					if(preg_match("/\.xml/i", $depend_file)) {
314
						$depend_xml = parse_xml_config_pkg("/usr/local/pkg/{$depend_file}", "packagegui");
315
						if (!empty($depend_xml))
316
							$depends[] = $depend_xml['name'];
317
					} else
318
						$depends[] = $depend_name; // If this dependency isn't package XML, use the stripped filename.
319
					break;
320
				case ".xml":
321
					$depend_xml = parse_xml_config_pkg("/usr/local/pkg/" . $depend_file, "packagegui");
322
					if (!empty($depend_xml))
323
						$depends[] = $depend_xml['name'];
324
					break;
325
				default:
326
					$depends[] = $depend_name; // If we aren't looking for XML, use the stripped filename (it's all we have).
327
					break;
328
				}
329
			}
330
		}
331
		return $depends;
332
	}
333
}
334

    
335
function uninstall_package($pkg_name) {
336
	global $config, $static_output;
337
	global $builder_package_install;
338

    
339
	// Back up /usr/local/lib libraries first if
340
	// not running from the builder code.
341
	// also take into account rrd binaries
342
	if(!$builder_package_install) {
343
		if(!file_exists("/tmp/pkg_libs.tgz")) {
344
			$static_output .= "Backing up libraries... ";
345
			update_output_window($static_output);
346
			exec("/usr/bin/tar czPf /tmp/pkg_libs.tgz `/bin/cat /etc/pfSense_md5.txt | /usr/bin/grep 'local/lib' | /usr/bin/awk '{ print $2 }' | /usr/bin/cut -d'(' -f2 | /usr/bin/cut -d')' -f1`");
347
			exec("/usr/bin/tar czPf /tmp/pkg_bins.tgz `/bin/cat /etc/pfSense_md5.txt | /usr/bin/grep 'rrd' | /usr/bin/awk '{ print $2 }' | /usr/bin/cut -d'(' -f2 | /usr/bin/cut -d')' -f1`");
348
			$static_output .= "\n";
349
		}
350
	}
351

    
352
	stop_service($pkg_name);
353

    
354
	$id = get_pkg_id($pkg_name);
355
	if ($id >= 0) {
356
		$pkg_depends =& $config['installedpackages']['package'][$id]['depends_on_package_pbi'];
357
		$static_output .= "Removing package...\n";
358
		update_output_window($static_output);
359
		if (is_array($pkg_depends)) {
360
			foreach ($pkg_depends as $pkg_depend)
361
				delete_package($pkg_depend);
362
		} else {
363
			// The packages (1 or more) are all in one long string.
364
			// We need to pass them 1 at a time to delete_package.
365
			// Compress any multiple whitespace (sp, tab, cr, lf...) into a single space char.
366
			$pkg_dep_str = preg_replace("'\s+'", ' ', $pkg_depends);
367
			// Get rid of any leading or trailing space.
368
			$pkg_dep_str = trim($pkg_dep_str);
369
			// Now we have a space-separated string. Make it into an array and process it.
370
			$pkg_dep_array = explode(" ", $pkg_dep_str);
371
			foreach ($pkg_dep_array as $pkg_depend) {
372
				delete_package($pkg_depend);
373
			}
374
		}
375
	}
376
	delete_package_xml($pkg_name);
377

    
378
	// Restore libraries that we backed up if not 
379
	// running from the builder code.
380
	if(!$builder_package_install) {
381
		$static_output .= "Cleaning up... ";
382
		update_output_window($static_output);
383
		exec("/usr/bin/tar xzPfU /tmp/pkg_libs.tgz -C /");
384
		exec("/usr/bin/tar xzPfU /tmp/pkg_bins.tgz -C /");
385
		@unlink("/tmp/pkg_libs.tgz");
386
		@unlink("/tmp/pkg_bins.tgz");
387
	}
388
}
389

    
390
function force_remove_package($pkg_name) {
391
	delete_package_xml($pkg_name);
392
}
393

    
394
/*
395
 * sync_package($pkg_name, $sync_depends = true, $show_message = false) Force a package to setup its configuration and rc.d files.
396
 */
397
function sync_package($pkg_name, $sync_depends = true, $show_message = false) {
398
	global $config, $config_parsed;
399
	global $builder_package_install;
400
	
401
	// If this code is being called by pfspkg_installer 
402
	// which the builder system uses then return (ignore).
403
	if($builder_package_install)
404
		return;
405
	
406
	if(empty($config['installedpackages']['package']))
407
		return;
408
	if(!is_numeric($pkg_name)) {
409
		$pkg_id = get_pkg_id($pkg_name);
410
		if($pkg_id == -1)
411
			return -1; // This package doesn't really exist - exit the function.
412
	} else {
413
		$pkg_id = $pkg_name;
414
		if(empty($config['installedpackages']['package'][$pkg_id]))
415
			return;  // No package belongs to the pkg_id passed to this function.
416
	}
417
        if (is_array($config['installedpackages']['package'][$pkg_id]))
418
		$package =& $config['installedpackages']['package'][$pkg_id];
419
        else
420
		return; /* empty package tag */
421
	if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
422
		log_error(sprintf(gettext("The %s package is missing its configuration file and must be reinstalled."), $package['name']));
423
		force_remove_package($package['name']);
424
		return -1;
425
	}
426
	$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
427
	if(isset($pkg_config['nosync']))
428
		return;
429
	/* Bring in package include files */
430
	if (!empty($pkg_config['include_file'])) {
431
		$include_file = $pkg_config['include_file'];
432
		if (file_exists($include_file))
433
			require_once($include_file);
434
		else {
435
			/* XXX: What the heck is this?! */
436
			log_error("Reinstalling package {$package['name']} because its include file({$include_file}) is missing!");
437
			uninstall_package($package['name']);
438
			if (install_package($package['name']) < 0) {
439
				log_error("Reinstalling package {$package['name']} failed. Take appropriate measures!!!");
440
				return -1;
441
			}
442
		}
443
	}
444

    
445
	if(!empty($pkg_config['custom_php_global_functions']))
446
		eval($pkg_config['custom_php_global_functions']);
447
	if(!empty($pkg_config['custom_php_resync_config_command']))
448
		eval($pkg_config['custom_php_resync_config_command']);
449
	if($sync_depends == true) {
450
		$depends = get_pkg_depends($pkg_name, ".xml", "files", 1); // Call dependency handler and do a little more error checking.
451
		if(is_array($depends)) {
452
			foreach($depends as $item) {
453
				if(!file_exists($item)) {
454
					require_once("notices.inc");
455
					file_notice($package['name'], sprintf(gettext("The %s package is missing required dependencies and must be reinstalled."), $package['name']), "Packages", "/pkg_mgr_install.php?mode=reinstallpkg&pkg={$package['name']}", 1);
456
					log_error("Could not find {$item}. Reinstalling package.");
457
					uninstall_package($pkg_name);
458
					if (install_package($pkg_name) < 0) {
459
						log_error("Reinstalling package {$package['name']} failed. Take appropriate measures!!!");
460
						return -1;
461
					}
462
				} else {
463
					$item_config = parse_xml_config_pkg($item, "packagegui");
464
					if (empty($item_config))
465
						continue;
466
					if(isset($item_config['nosync']))
467
						continue;
468
					if (!empty($item_config['include_file'])) {
469
						if (file_exists($item_config['include_file']))	
470
							require_once($item_config['include_file']);
471
						else {
472
							log_error("Not calling package sync code for dependency {$item_config['name']} of {$package['name']} because some include files are missing.");
473
							continue;
474
						}
475
					}
476
					if($item_config['custom_php_global_functions'] <> "")
477
						eval($item_config['custom_php_global_functions']);
478
					if($item_config['custom_php_resync_config_command'] <> "")
479
						eval($item_config['custom_php_resync_config_command']);
480
					if($show_message == true)
481
						print " " . $item_config['name'];
482
				}
483
			}
484
		}
485
	}
486
}
487

    
488
/*
489
 * pkg_fetch_recursive: Download and install a FreeBSD PBI package. This function provides output to
490
 * 			a progress bar and output window.
491
 */
492
function pkg_fetch_recursive($pkgname, $filename, $dependlevel = 0, $base_url = "") {
493
	global $static_output, $g;
494

    
495
	// Clean up incoming filenames
496
	$filename = str_replace("  ", " ", $filename);
497
	$filename = str_replace("\n", " ", $filename);
498
	$filename = str_replace("  ", " ", $filename);
499

    
500
	$pkgs = explode(" ", $filename);
501
	foreach($pkgs as $filename) {
502
		$filename = trim($filename);
503
		if (($g['platform'] == "nanobsd") || ($g['platform'] == "embedded")) {
504
			$pkgtmpdir = "/usr/bin/env PKG_TMPDIR=/root/ ";
505
			$pkgstagingdir = "/root/tmp";
506
			if (!is_dir($pkgstagingdir))
507
				mkdir($pkgstagingdir);
508
			$pkgstaging = "-o {$pkgstagingdir}/instmp.XXXXXX";
509
			$fetchdir = $pkgstagingdir;
510
		} else {
511
			$fetchdir = $g['tmp_path'];
512
		}
513

    
514
		$osname = php_uname("s");
515
		$arch =  php_uname("m");
516
		$rel = strtolower(php_uname("r"));
517
		if (substr_count($rel, '-') > 1)
518
			$rel = substr($rel, 0, strrpos($rel, "-"));
519
		$priv_url = "http://ftp2.{$osname}.org/pub/{$osname}/ports/{$arch}/packages-{$rel}/All";
520
		if (empty($base_url))
521
			$base_url = $priv_url;
522
		if (substr($base_url, -1) == "/")
523
			$base_url = substr($base_url, 0, -1);
524
		$fetchto = "{$fetchdir}/apkg_{$filename}";
525
		$static_output .= "\n" . str_repeat(" ", $dependlevel * 2 + 1) . "Downloading {$base_url}/{$filename} ... ";
526
		if (download_file_with_progress_bar("{$base_url}/{$filename}", $fetchto) !== true) {
527
			if ($base_url != $priv_url && download_file_with_progress_bar("{$priv_url}/{$filename}", $fetchto) !== true) {
528
				$static_output .= " could not download from there or {$priv_url}/{$filename}.\n";
529
				update_output_window($static_output);
530
				return false;
531
			} else if ($base_url == $priv_url) {
532
				$static_output .= " failed to download.\n";
533
				update_output_window($static_output);
534
				return false;
535
			} else {
536
				$static_output .= " [{$osname} repository]\n";
537
				update_output_window($static_output);
538
			}
539
		}
540
		$static_output .= " (extracting)\n";
541
		update_output_window($static_output);
542

    
543
		$pkgaddout = "";
544

    
545
		exec("/usr/local/sbin/pbi_add {$pkgstaging} -f -v --no-checksig {$fetchto} 2>&1", $pkgaddout);
546
		pkg_debug($pkgname . " " . print_r($pkgaddout, true) . "\npbi_add successfully completed.\n");
547

    
548
		exec("/usr/local/sbin/pbi_info " . preg_replace('/\.pbi$/','',$filename) . " | /usr/bin/awk '/Prefix/ {print $2}'",$pbidir);
549
		$pbidir = $pbidir[0];
550
		$linkdirs = array('bin','sbin');
551
		foreach($linkdirs as $dir) {
552
			if(is_dir("{$pbidir}/{$dir}")) {
553
				$files = scandir("{$pbidir}/{$dir}");
554
				foreach($files as $f) {
555
					if(!file_exists("/usr/local/{$dir}/{$f}")) {
556
						symlink("{$pbidir}/{$dir}/${$f}","/usr/local/{$dir}/{$f}");
557
					}
558
				}
559
			}
560
		}
561
	}
562
	return true;
563
}
564

    
565
function install_package($package, $pkg_info = "") {
566
	global $g, $config, $static_output, $pkg_interface;
567

    
568
	/* safe side. Write config below will send to ro again. */
569
	conf_mount_rw();
570

    
571
	if($pkg_interface == "console") 	
572
		echo "\n";
573
	/* fetch package information if needed */
574
	if(empty($pkg_info) or !is_array($pkg_info[$package])) {
575
		$pkg_info = get_pkg_info(array($package));
576
		$pkg_info = $pkg_info[$package]; // We're only dealing with one package, so we can strip away the extra array.
577
		if (empty($pkg_info)) {
578
			conf_mount_ro();
579
			return -1;
580
		}
581
	}
582
	pkg_debug(gettext("Beginning package installation.") . "\n");
583
	log_error(sprintf(gettext('Beginning package installation for %s .'), $pkg_info['name']));
584
	$static_output .= sprintf(gettext("Beginning package installation for %s ."), $pkg_info['name']);
585
	update_status($static_output);
586
	/* fetch the package's configuration file */
587
	if($pkg_info['config_file'] != "") {
588
		$static_output .= "\n" . gettext("Downloading package configuration file... ");
589
		update_output_window($static_output);
590
		pkg_debug(gettext("Downloading package configuration file...") . "\n");
591
		$fetchto = substr(strrchr($pkg_info['config_file'], '/'), 1);
592
		download_file_with_progress_bar($pkg_info['config_file'], '/usr/local/pkg/' . $fetchto);
593
		if(!file_exists('/usr/local/pkg/' . $fetchto)) {
594
			pkg_debug(gettext("ERROR! Unable to fetch package configuration file. Aborting installation.") . "\n");
595
			if($pkg_interface == "console")
596
				print "\n" . gettext("ERROR! Unable to fetch package configuration file. Aborting package installation.") . "\n";
597
			else {
598
				$static_output .= gettext("failed!\n\nInstallation aborted.\n");
599
				update_output_window($static_output);
600
				echo "<br>Show <a href=\"pkg_mgr_install.php?showlog=true\">install log</a></center>";
601
			}
602
			conf_mount_ro();
603
			return -1;
604
		}
605
		$static_output .= gettext("done.") . "\n";
606
		update_output_window($static_output);
607
	}
608
	/* add package information to config.xml */
609
	$pkgid = get_pkg_id($pkg_info['name']);
610
	$static_output .= gettext("Saving updated package information...") . " ";
611
	update_output_window($static_output);
612
	if($pkgid == -1) {
613
		$config['installedpackages']['package'][] = $pkg_info;
614
		$changedesc = sprintf(gettext("Installed %s package."),$pkg_info['name']);
615
		$to_output = gettext("done.") . "\n";
616
	} else {
617
		$config['installedpackages']['package'][$pkgid] = $pkg_info;
618
		$changedesc = sprintf(gettext("Overwrote previous installation of %s."), $pkg_info['name']);
619
		$to_output = gettext("overwrite!") . "\n";
620
	}
621
	if(file_exists('/conf/needs_package_sync'))
622
		@unlink('/conf/needs_package_sync');
623
	conf_mount_ro();
624
	write_config("Intermediate config write during package install for {$pkg_info['name']}.");
625
	$static_output .= $to_output;
626
	update_output_window($static_output);
627
	/* install other package components */
628
	if (!install_package_xml($package)) {
629
		uninstall_package($package);
630
		write_config($changedesc);
631
		$static_output .= gettext("Failed to install package.") . "\n";
632
		update_output_window($static_output);
633
		return -1;
634
	} else {
635
		$static_output .= gettext("Writing configuration... ");
636
		update_output_window($static_output);
637
		write_config($changedesc);
638
		$static_output .= gettext("done.") . "\n";
639
		update_output_window($static_output);
640
		if($pkg_info['after_install_info']) 
641
			update_output_window($pkg_info['after_install_info']);	
642
	}
643
}
644

    
645
function get_after_install_info($package) {
646
	global $pkg_info;
647
	/* fetch package information if needed */
648
	if(!$pkg_info or !is_array($pkg_info[$package])) {
649
		$pkg_info = get_pkg_info(array($package));
650
		$pkg_info = $pkg_info[$package]; // We're only dealing with one package, so we can strip away the extra array.
651
	}
652
	if($pkg_info['after_install_info'])
653
		return $pkg_info['after_install_info'];
654
}
655

    
656
function eval_once($toeval) {
657
	global $evaled;
658
	if(!$evaled) $evaled = array();
659
	$evalmd5 = md5($toeval);
660
	if(!in_array($evalmd5, $evaled)) {
661
		@eval($toeval);
662
		$evaled[] = $evalmd5;
663
	}
664
	return;
665
}
666

    
667
function install_package_xml($pkg) {
668
	global $g, $config, $static_output, $pkg_interface, $config_parsed;
669

    
670
	if(($pkgid = get_pkg_id($pkg)) == -1) {
671
		$static_output .= sprintf(gettext("The %s package is not installed.%sInstallation aborted."), $pkg, "\n\n");
672
		update_output_window($static_output);
673
		if($pkg_interface <> "console") {
674
			echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
675
			echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
676
		}
677
		sleep(1);
678
		return false;
679
	} else
680
		$pkg_info = $config['installedpackages']['package'][$pkgid];
681

    
682
	/* pkg_add the package and its dependencies */
683
	if($pkg_info['depends_on_package_base_url'] != "") {
684
		if($pkg_interface == "console") 
685
			echo "\n";
686
		update_status(gettext("Installing") . " " . $pkg_info['name'] . " " . gettext("and its dependencies."));
687
		$static_output .= gettext("Downloading") . " " . $pkg_info['name'] . " " . gettext("and its dependencies... ");
688
		$static_orig = $static_output;
689
		$static_output .= "\n";
690
		update_output_window($static_output);
691
		foreach((array) $pkg_info['depends_on_package_pbi'] as $pkgdep) {
692
			$pkg_name = substr(reverse_strrchr($pkgdep, "."), 0, -1);
693
			$static_output = $static_orig . "\nChecking for package installation... ";
694
			update_output_window($static_output);
695
			if (!is_freebsd_pkg_installed($pkg_name)) {
696
				if (!pkg_fetch_recursive($pkg_name, $pkgdep, 0, $pkg_info['depends_on_package_base_url'])) {
697
					$static_output .= "of {$pkg_name} failed!\n\nInstallation aborted.";
698
					update_output_window($static_output);
699
					pkg_debug(gettext("Package WAS NOT installed properly.") . "\n");
700
					if($pkg_interface <> "console") {
701
						echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
702
						echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
703
					}
704
					sleep(1);
705
					return false;
706
				}
707
			}
708
		}
709
	}
710
	$configfile = substr(strrchr($pkg_info['config_file'], '/'), 1);
711
	if(file_exists("/usr/local/pkg/" . $configfile)) {
712
		$static_output .= gettext("Loading package configuration... ");
713
		update_output_window($static_output);
714
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $configfile, "packagegui");
715
		$static_output .= gettext("done.") . "\n";
716
		update_output_window($static_output);
717
		$static_output .= gettext("Configuring package components...\n");
718
		if (!empty($pkg_config['filter_rules_needed']))
719
			$config['installedpackages']['package'][$pkgid]['filter_rule_function'] = $pkg_config['filter_rules_needed'];
720
		update_output_window($static_output);
721
		/* modify system files */
722
		if(is_array($pkg_config['modify_system']) && is_array($pkg_config['modify_system']['item'])) {
723
			$static_output .= gettext("System files... ");
724
			update_output_window($static_output);
725
			foreach($pkg_config['modify_system']['item'] as $ms) {
726
				if($ms['textneeded']) {
727
					add_text_to_file($ms['modifyfilename'], $ms['textneeded']);
728
				}
729
			}
730
			$static_output .= gettext("done.") . "\n";
731
			update_output_window($static_output);
732
		}
733
		/* download additional files */
734
		if(is_array($pkg_config['additional_files_needed'])) {
735
			$static_output .= gettext("Additional files... ");
736
			$static_orig = $static_output;
737
			update_output_window($static_output);
738
			foreach($pkg_config['additional_files_needed'] as $afn) {
739
				$filename = get_filename_from_url($afn['item'][0]);
740
				if($afn['chmod'] <> "")
741
					$pkg_chmod = $afn['chmod'];
742
				else
743
					$pkg_chmod = "";
744

    
745
				if($afn['prefix'] <> "")
746
					$prefix = $afn['prefix'];
747
				else
748
					$prefix = "/usr/local/pkg/";
749

    
750
				if(!is_dir($prefix)) 
751
					safe_mkdir($prefix);
752
 				$static_output .= $filename . " ";
753
				update_output_window($static_output);
754
				if (download_file_with_progress_bar($afn['item'][0], $prefix . $filename) !== true) {
755
					$static_output .= "failed.\n";
756
					@unlink($prefix . $filename);
757
					update_output_window($static_output);
758
					return false;
759
				}
760
				if(stristr($filename, ".tgz") <> "") {
761
					pkg_debug(gettext("Extracting tarball to -C for ") . $filename . "...\n");
762
					$tarout = "";
763
					exec("/usr/bin/tar xvzf " . $prefix . $filename . " -C / 2>&1", $tarout);
764
					pkg_debug(print_r($tarout, true) . "\n");
765
				}
766
				if($pkg_chmod <> "") {
767
					pkg_debug(sprintf(gettext('Changing file mode to %1$s for %2$s%3$s%4$s'), $pkg_chmod, $prefix, $filename, "\n"));
768
					@chmod($prefix . $filename, $pkg_chmod);
769
					system("/bin/chmod {$pkg_chmod} {$prefix}{$filename}");
770
				}
771
				$static_output = $static_orig;
772
                                update_output_window($static_output);
773
			}
774
			$static_output .= gettext("done.") . "\n";
775
			update_output_window($static_output);
776
		}
777
		/*   if a require exists, include it.  this will
778
		 *   show us where an error exists in a package
779
		 *   instead of making us blindly guess
780
		 */
781
		$missing_include = false;
782
		if($pkg_config['include_file'] <> "") {
783
			$static_output = gettext("Loading package instructions...") . "\n";
784
			update_output_window($static_output);
785
			pkg_debug("require_once('{$pkg_config['include_file']}')\n");
786
			if (file_exists($pkg_config['include_file']))
787
				require_once($pkg_config['include_file']);
788
			else {
789
				$missing_include = true;
790
				$static_output .= "Include " . basename($pkg_config['include_file']) . " is missing!\n";
791
				update_output_window($static_output);
792
				/* XXX: Should undo the steps before this?! */
793
				return false;
794
			}
795
		}
796

    
797
		/* custom commands */
798
		$static_output .= gettext("Custom commands...") . "\n";
799
		update_output_window($static_output);
800
		if ($missing_include == false) {
801
			if($pkg_config['custom_php_global_functions'] <> "") {
802
				$static_output .= gettext("Executing custom_php_global_functions()...");
803
				update_output_window($static_output);
804
				eval_once($pkg_config['custom_php_global_functions']);
805
				$static_output .= gettext("done.") . "\n";
806
				update_output_window($static_output);
807
			}
808
			if($pkg_config['custom_php_install_command']) {
809
				$static_output .= gettext("Executing custom_php_install_command()...");
810
				update_output_window($static_output);
811
				/* XXX: create symlinks for conf files into the PBI directories.
812
				 *	change packages to store configs at /usr/pbi/pkg/etc and remove this
813
				 */
814
				eval_once($pkg_config['custom_php_install_command']);
815
				// Note: pkg may be mixed-case, e.g. "squidGuard" but the PBI names are lowercase.
816
				// e.g. "squidguard-1.4_4-i386" so feed lowercase to pbi_info below.
817
				// Also add the "-" so that examples like "squid-" do not match "squidguard-".
818
				$pkg_name_for_pbi_match = strtolower($pkg) . "-";
819
				exec("/usr/local/sbin/pbi_info | grep {$pkg_name_for_pbi_match}- | xargs /usr/local/sbin/pbi_info | awk '/Prefix/ {print $2}'",$pbidirarray);
820
				$pbidir0 = $pbidirarray[0];
821
				exec("find /usr/local/etc/ -name *.conf | grep {$pkg}",$files);
822
				foreach($files as $f) {
823
					$pbiconf = str_replace('/usr/local',$pbidir0,$f);
824
					if (is_file($pbiconf) || is_link($pbiconf)) {
825
						unlink($pbiconf);
826
					}
827
					symlink($f,$pbiconf);
828
				}
829
				eval_once($pkg_config['custom_php_install_command']);
830
				$static_output .= gettext("done.") . "\n";
831
				update_output_window($static_output);
832
			}
833
			if($pkg_config['custom_php_resync_config_command'] <> "") {
834
				$static_output .= gettext("Executing custom_php_resync_config_command()...");
835
				update_output_window($static_output);
836
				eval_once($pkg_config['custom_php_resync_config_command']);
837
				$static_output .= gettext("done.") . "\n";
838
				update_output_window($static_output);
839
			}
840
		}
841
		/* sidebar items */
842
		if(is_array($pkg_config['menu'])) {
843
			$static_output .= gettext("Menu items... ");
844
			update_output_window($static_output);
845
			foreach($pkg_config['menu'] as $menu) {
846
				if(is_array($config['installedpackages']['menu'])) {
847
					foreach($config['installedpackages']['menu'] as $amenu)
848
						if($amenu['name'] == $menu['name'])
849
							continue 2;
850
				} else
851
					$config['installedpackages']['menu'] = array();
852
				$config['installedpackages']['menu'][] = $menu;
853
			}
854
			$static_output .= gettext("done.") . "\n";
855
			update_output_window($static_output);
856
		}
857
		/* integrated tab items */
858
		if(is_array($pkg_config['tabs']['tab'])) {
859
			$static_output .= gettext("Integrated Tab items... ");
860
			update_output_window($static_output);
861
			foreach($pkg_config['tabs']['tab'] as $tab) {
862
				if(is_array($config['installedpackages']['tab'])) {
863
					foreach($config['installedpackages']['tab'] as $atab)
864
						if($atab['name'] == $tab['name'])
865
							continue 2;
866
				} else
867
					$config['installedpackages']['tab'] = array();
868
				$config['installedpackages']['tab'][] = $tab;
869
			}
870
			$static_output .= gettext("done.") . "\n";
871
			update_output_window($static_output);
872
		}
873
		/* services */
874
		if(is_array($pkg_config['service'])) {
875
			$static_output .= gettext("Services... ");
876
			update_output_window($static_output);
877
			foreach($pkg_config['service'] as $service) {
878
				if(is_array($config['installedpackages']['service'])) {
879
					foreach($config['installedpackages']['service'] as $aservice)
880
						if($aservice['name'] == $service['name'])
881
							continue 2;
882
				} else
883
					$config['installedpackages']['service'] = array();
884
				$config['installedpackages']['service'][] = $service;
885
			}
886
			$static_output .= gettext("done.") . "\n";
887
			update_output_window($static_output);
888
		}
889
	} else {
890
		$static_output .= gettext("Loading package configuration... failed!") . "\n\n" . gettext("Installation aborted.");
891
		update_output_window($static_output);
892
		pkg_debug(gettext("Unable to load package configuration. Installation aborted.") ."\n");
893
		if($pkg_interface <> "console") {
894
			echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
895
			echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
896
		}
897
		sleep(1);
898
		return false;
899
	}
900

    
901
	/* set up package logging streams */
902
	if($pkg_info['logging']) {
903
		mwexec("/usr/sbin/fifolog_create -s 32768 {$g['varlog_path']}/{$pkg_info['logging']['logfilename']}");
904
		@chmod($g['varlog_path'] . '/' . $pkg_info['logging']['logfilename'], 0600);
905
		add_text_to_file("/etc/syslog.conf", $pkg_info['logging']['facilityname'] . "\t\t\t\t" . $pkg_info['logging']['logfilename']);
906
		pkg_debug("Adding text to file /etc/syslog.conf\n");
907
		system_syslogd_start();
908
	}
909

    
910
	return true;
911
}
912

    
913
function does_package_depend($pkg) {
914
	// Should not happen, but just in case.
915
	if(!$pkg)
916
		return;
917
	$pkg_var_db_dir = glob("/var/db/pkg/{$pkg}*");
918
	// If this package has dependency then return true
919
	foreach($pkg_var_db_dir as $pvdd) {
920
		if (file_exists("{$vardb}/{$pvdd}/+REQUIRED_BY") && count(file("{$vardb}/{$pvdd}/+REQUIRED_BY")) > 0) 
921
			return true;
922
	}	
923
	// Did not find a record of dependencies, so return false.
924
	return false;
925
}
926

    
927
function delete_package($pkg) {
928
	global $config, $g, $static_output, $vardb;
929

    
930
	if(!$pkg) 
931
		return;
932

    
933
	// Note: $pkg has the full PBI package name followed by ".pbi". Strip off ".pbi".
934
	$pkg = substr(reverse_strrchr($pkg, "."), 0, -1);
935

    
936
	if($pkg)
937
		$static_output .= sprintf(gettext("Starting package deletion for %s..."),$pkg);
938
	update_output_window($static_output);
939

    
940
	remove_freebsd_package($pkg);
941
	$static_output .= "done.\n";
942
	update_output_window($static_output);
943

    
944
	/* Rescan directories for what has been left and avoid fooling other programs. */
945
	mwexec("/sbin/ldconfig");
946

    
947
	return;
948
}
949

    
950
function delete_package_xml($pkg) {
951
	global $g, $config, $static_output, $pkg_interface, $rcfileprefix;
952

    
953
	conf_mount_rw();
954

    
955
	$pkgid = get_pkg_id($pkg);
956
	if ($pkgid == -1) {
957
		$static_output .= sprintf(gettext("The %s package is not installed.%sDeletion aborted."), $pkg, "\n\n");
958
		update_output_window($static_output);
959
		if($pkg_interface <> "console") {
960
			echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
961
			echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
962
		}
963
		ob_flush();
964
		sleep(1);
965
		conf_mount_ro();
966
		return;
967
	}
968
	pkg_debug(sprintf(gettext("Removing %s package... "),$pkg));
969
	$static_output .= sprintf(gettext("Removing %s components..."),$pkg) . "\n";
970
	update_output_window($static_output);
971
	/* parse package configuration */
972
	$packages = &$config['installedpackages']['package'];
973
	$tabs =& $config['installedpackages']['tab'];
974
	$menus =& $config['installedpackages']['menu'];
975
	$services = &$config['installedpackages']['service'];
976
	$pkg_info =& $packages[$pkgid];
977
	if(file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
978
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $packages[$pkgid]['configurationfile'], "packagegui");
979
		/* remove tab items */
980
		if(is_array($pkg_config['tabs'])) {
981
			$static_output .= gettext("Tabs items... ");
982
			update_output_window($static_output);
983
			if(is_array($pkg_config['tabs']['tab']) && is_array($tabs)) {
984
				foreach($pkg_config['tabs']['tab'] as $tab) {
985
					foreach($tabs as $key => $insttab) {
986
						if($insttab['name'] == $tab['name']) {
987
							unset($tabs[$key]);
988
							break;
989
						}
990
					}
991
				}
992
			}
993
			$static_output .= gettext("done.") . "\n";
994
			update_output_window($static_output);
995
		}
996
		/* remove menu items */
997
		if(is_array($pkg_config['menu'])) {
998
			$static_output .= gettext("Menu items... ");
999
			update_output_window($static_output);
1000
			if (is_array($pkg_config['menu']) && is_array($menus)) {
1001
				foreach($pkg_config['menu'] as $menu) {
1002
					foreach($menus as $key => $instmenu) {
1003
						if($instmenu['name'] == $menu['name']) {
1004
							unset($menus[$key]);
1005
							break;
1006
						}
1007
					}
1008
				}
1009
			}
1010
			$static_output .= gettext("done.") . "\n";
1011
			update_output_window($static_output);
1012
		}
1013
		/* remove services */
1014
		if(is_array($pkg_config['service'])) {
1015
			$static_output .= gettext("Services... ");
1016
			update_output_window($static_output);
1017
			if (is_array($pkg_config['service']) && is_array($services)) {
1018
				foreach($pkg_config['service'] as $service) {
1019
					foreach($services as $key => $instservice) {
1020
						if($instservice['name'] == $service['name']) {
1021
							if($g['booting'] != true)
1022
								stop_service($service['name']);
1023
							if($service['rcfile']) {
1024
								$prefix = $rcfileprefix;
1025
								if (!empty($service['prefix']))
1026
									$prefix = $service['prefix'];
1027
								if (file_exists("{$prefix}{$service['rcfile']}"))
1028
									@unlink("{$prefix}{$service['rcfile']}");
1029
							}
1030
							unset($services[$key]);
1031
						}
1032
					}
1033
				}
1034
			}
1035
			$static_output .= gettext("done.") . "\n";
1036
			update_output_window($static_output);
1037
		}
1038
		/*
1039
		 * XXX: Otherwise inclusion of config.inc again invalidates actions taken.
1040
		 * 	Same is done during installation.
1041
		 */
1042
		write_config("Intermediate config write during package removal for {$pkg}.");
1043

    
1044
		/*
1045
		 * If a require exists, include it.  this will
1046
		 * show us where an error exists in a package
1047
		 * instead of making us blindly guess
1048
		 */
1049
		$missing_include = false;
1050
		if($pkg_config['include_file'] <> "") {
1051
			$static_output .= gettext("Loading package instructions...") . "\n";
1052
			update_output_window($static_output);
1053
			pkg_debug("require_once(\"{$pkg_config['include_file']}\")\n");
1054
			if (file_exists($pkg_config['include_file']))
1055
				require_once($pkg_config['include_file']);
1056
			else {
1057
				$missing_include = true;
1058
				update_output_window($static_output);
1059
				$static_output .= "Include file " . basename($pkg_config['include_file']) . " could not be found for inclusion.\n";
1060
			}
1061
		}
1062
		/* ermal
1063
		 * NOTE: It is not possible to handle parse errors on eval.
1064
		 * So we prevent it from being run at all to not interrupt all the other code.
1065
		 */
1066
		if ($missing_include == false) {
1067
			/* evalate this package's global functions and pre deinstall commands */
1068
			if($pkg_config['custom_php_global_functions'] <> "")
1069
				eval_once($pkg_config['custom_php_global_functions']);
1070
			if($pkg_config['custom_php_pre_deinstall_command'] <> "")
1071
				eval_once($pkg_config['custom_php_pre_deinstall_command']);
1072
		}
1073
		/* system files */
1074
		if(is_array($pkg_config['modify_system']) && is_array($pkg_config['modify_system']['item'])) {
1075
			$static_output .= gettext("System files... ");
1076
			update_output_window($static_output);
1077
			foreach($pkg_config['modify_system']['item'] as $ms)
1078
				if($ms['textneeded']) remove_text_from_file($ms['modifyfilename'], $ms['textneeded']);
1079

    
1080
			$static_output .= gettext("done.") . "\n";
1081
			update_output_window($static_output);
1082
		}
1083
		/* deinstall commands */
1084
		if($pkg_config['custom_php_deinstall_command'] <> "") {
1085
			$static_output .= gettext("Deinstall commands... ");
1086
			update_output_window($static_output);
1087
			if ($missing_include == false) {
1088
				eval_once($pkg_config['custom_php_deinstall_command']);
1089
				$static_output .= gettext("done.") . "\n";
1090
			} else
1091
				$static_output .= "\nNot executing custom deinstall hook because an include is missing.\n";
1092
			update_output_window($static_output);
1093
		}
1094
		if($pkg_config['include_file'] <> "") {
1095
			$static_output .= gettext("Removing package instructions...");
1096
			update_output_window($static_output);
1097
                        pkg_debug(sprintf(gettext("Remove '%s'"), $pkg_config['include_file']) . "\n");
1098
			unlink_if_exists("/usr/local/pkg/" . $pkg_config['include_file']);
1099
			$static_output .= gettext("done.") . "\n";
1100
			update_output_window($static_output);
1101
		}
1102
		/* remove all additional files */
1103
		if(is_array($pkg_config['additional_files_needed'])) {
1104
			$static_output .= gettext("Auxiliary files... ");
1105
			update_output_window($static_output);
1106
			foreach($pkg_config['additional_files_needed'] as $afn) {
1107
				$filename = get_filename_from_url($afn['item'][0]);
1108
				if($afn['prefix'] <> "")
1109
					$prefix = $afn['prefix'];
1110
				else
1111
					$prefix = "/usr/local/pkg/";
1112
				unlink_if_exists($prefix . $filename);
1113
			}
1114
			$static_output .= gettext("done.") . "\n";
1115
			update_output_window($static_output);
1116
		}
1117
		/* package XML file */
1118
		$static_output .= gettext("Package XML... ");
1119
		update_output_window($static_output);
1120
		unlink_if_exists("/usr/local/pkg/" . $packages[$pkgid]['configurationfile']);
1121
		$static_output .= gettext("done.") . "\n";
1122
		update_output_window($static_output);
1123
	}
1124
	/* syslog */
1125
	if(is_array($pkg_info['logging']) && $pkg_info['logging']['logfile_name'] <> "") {
1126
		$static_output .= "Syslog entries... ";
1127
		update_output_window($static_output);
1128
		remove_text_from_file("/etc/syslog.conf", $pkg_info['logging']['facilityname'] . "\t\t\t\t" . $pkg_info['logging']['logfilename']);
1129
		system_syslogd_start();
1130
		@unlink("{$g['varlog_path']}/{$pkg_info['logging']['logfilename']}");
1131
		$static_output .= "done.\n";
1132
		update_output_window($static_output);
1133
	}
1134
	
1135
	conf_mount_ro();
1136
	/* remove config.xml entries */
1137
	$static_output .= gettext("Configuration... ");
1138
	update_output_window($static_output);
1139
	unset($config['installedpackages']['package'][$pkgid]);
1140
	$static_output .= gettext("done.") . "\n";
1141
	update_output_window($static_output);
1142
	write_config("Removed {$pkg} package.\n");
1143
}
1144

    
1145
function expand_to_bytes($size) {
1146
	$conv = array(
1147
			"G" =>	"3",
1148
			"M" =>  "2",
1149
			"K" =>  "1",
1150
			"B" =>  "0"
1151
		);
1152
	$suffix = substr($size, -1);
1153
	if(!in_array($suffix, array_keys($conv))) return $size;
1154
	$size = substr($size, 0, -1);
1155
	for($i = 0; $i < $conv[$suffix]; $i++) {
1156
		$size *= 1024;
1157
	}
1158
	return $size;
1159
}
1160

    
1161
function get_pkg_db() {
1162
	global $g;
1163
	return return_dir_as_array($g['vardb_path'] . '/pkg');
1164
}
1165

    
1166
function walk_depend($depend, $pkgdb = "", $alreadyseen = "") {
1167
	if(!$pkgdb)
1168
		$pkgdb = get_pkg_db();
1169
	if(!is_array($alreadyseen))
1170
		$alreadyseen = array();
1171
	if (!is_array($depend))
1172
		$depend = array();
1173
	foreach($depend as $adepend) {
1174
		$pkgname = reverse_strrchr($adepend['name'], '.');
1175
		if(in_array($pkgname, $alreadyseen)) {
1176
			continue;
1177
		} elseif(!in_array($pkgname, $pkgdb)) {
1178
			$size += expand_to_bytes($adepend['size']);
1179
			$alreadyseen[] = $pkgname;
1180
			if(is_array($adepend['depend'])) $size += walk_depend($adepend['depend'], $pkgdb, $alreadyseen);
1181
		}
1182
	}
1183
	return $size;
1184
}
1185

    
1186
function get_package_install_size($pkg = 'all', $pkg_info = "") {
1187
	global $config, $g;
1188
	if((!is_array($pkg)) and ($pkg != 'all'))
1189
		$pkg = array($pkg);
1190
	$pkgdb = get_pkg_db();
1191
	if(!$pkg_info)
1192
		$pkg_info = get_pkg_sizes($pkg);
1193
	foreach($pkg as $apkg) {
1194
		if(!$pkg_info[$apkg])
1195
			continue;
1196
		$toreturn[$apkg] = expand_to_bytes(walk_depend(array($pkg_info[$apkg]), $pkgdb));
1197
	}
1198
	return $toreturn;
1199
}
1200

    
1201
function squash_from_bytes($size, $round = "") {
1202
	$conv = array(1 => "B", "K", "M", "G");
1203
	foreach($conv as $div => $suffix) {
1204
		$sizeorig = $size;
1205
		if(($size /= 1024) < 1) {
1206
			if($round) {
1207
				$sizeorig = round($sizeorig, $round);
1208
			}
1209
			return $sizeorig . $suffix;
1210
		}
1211
	}
1212
	return;
1213
}
1214

    
1215
function pkg_reinstall_all() {
1216
	global $g, $config;
1217

    
1218
	@unlink('/conf/needs_package_sync');
1219
	$pkg_id = 0;
1220
	$todo = array();
1221
	if (is_array($config['installedpackages']['package']))
1222
		foreach($config['installedpackages']['package'] as $package)
1223
			$todo[] = array('name' => $package['name'], 'version' => $package['version']);
1224
	echo "One moment please, reinstalling packages...\n";
1225
	echo " >>> Trying to fetch package info...";
1226
	$pkg_info = get_pkg_info();
1227
	if ($pkg_info) {
1228
		echo " Done.\n";
1229
	} else {
1230
		$xmlrpc_base_url = isset($config['system']['altpkgrepo']['enable']) ? $config['system']['altpkgrepo']['xmlrpcbaseurl'] : $g['xmlrpcbaseurl'];
1231
		echo "\n" . sprintf(gettext(' >>> Unable to communicate with %1$s. Please verify DNS and interface configuration, and that %2$s has functional Internet connectivity.'), $xmlrpc_base_url, $g['product_name']) . "\n";
1232
		return;
1233
	}
1234
	if(is_array($todo)) {
1235
		foreach($todo as $pkgtodo) {
1236
			$static_output = "";
1237
			if($pkgtodo['name']) {
1238
				uninstall_package($pkgtodo['name']);
1239
				install_package($pkgtodo['name']);
1240
				$pkg_id++;
1241
			}
1242
		}
1243
	}
1244
}
1245

    
1246
?>
(38-38/64)