Project

General

Profile

Download (40.7 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
if(file_exists("/cf/conf/use_xmlreader"))
46
	require_once("xmlreader.inc");
47
else
48
	require_once("xmlparse.inc");
49
require_once("service-utils.inc");
50
require_once("pfsense-utils.inc");
51

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

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

    
68
		if (!$debug)
69
			return;
70

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

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

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

    
90
/****f* pkg-utils/remove_package
91
 * NAME
92
 *   remove_package - Removes package from FreeBSD if it exists
93
 * INPUTS
94
 *   $packagestring	- name/string to check for
95
 * RESULT
96
 *   none
97
 * NOTES
98
 *   
99
 ******/
100
function remove_freebsd_package($packagestring) {
101
	exec("/usr/sbin/pkg_delete -x {$packagestring} 2>>/tmp/pkg_delete_errors.txt");
102
}
103

    
104
/****f* pkg-utils/is_package_installed
105
 * NAME
106
 *   is_package_installed - Check whether a package is installed.
107
 * INPUTS
108
 *   $packagename	- name of the package to check
109
 * RESULT
110
 *   boolean	- true if the package is installed, false otherwise
111
 * NOTES
112
 *   This function is deprecated - get_pkg_id() can already check for installation.
113
 ******/
114
function is_package_installed($packagename) {
115
	$pkg = get_pkg_id($packagename);
116
	if($pkg == -1)
117
		return false;
118
	return true;
119
}
120

    
121
/****f* pkg-utils/get_pkg_id
122
 * NAME
123
 *   get_pkg_id - Find a package's numeric ID.
124
 * INPUTS
125
 *   $pkg_name	- name of the package to check
126
 * RESULT
127
 *   integer    - -1 if package is not found, >-1 otherwise
128
 ******/
129
function get_pkg_id($pkg_name) {
130
	global $config;
131

    
132
	if (is_array($config['installedpackages']['package'])) {
133
		foreach($config['installedpackages']['package'] as $idx => $pkg) {
134
			if($pkg['name'] == $pkg_name)
135
				return $idx;
136
		}
137
	}
138
	return -1;
139
}
140

    
141
/****f* pkg-utils/get_pkg_info
142
 * NAME
143
 *   get_pkg_info - Retrive package information from pfsense.com.
144
 * INPUTS
145
 *   $pkgs - 'all' to retrive all packages, an array containing package names otherwise
146
 *   $info - 'all' to retrive all information, an array containing keys otherwise
147
 * RESULT
148
 *   $raw_versions - Array containing retrieved information, indexed by package name.
149
 ******/
150
function get_pkg_info($pkgs = 'all', $info = 'all') {
151
	global $g;
152

    
153
	$freebsd_version = php_uname("r");
154
	$freebsd_machine = php_uname("m");
155
	$params = array(
156
		"pkg" => $pkgs, 
157
		"info" => $info, 
158
		"freebsd_version" => $freebsd_version[0],
159
		"freebsd_machine" => $freebsd_machine
160
	);
161
	$resp = call_pfsense_method('pfsense.get_pkgs', $params, 10);
162
	return $resp ? $resp : array();
163
}
164

    
165
function get_pkg_sizes($pkgs = 'all') {
166
	global $config, $g;
167

    
168
	$freebsd_version = php_uname("r");
169
	$freebsd_machine = php_uname("m");
170
	$params = array(
171
		"pkg" => $pkgs, 
172
		"freebsd_version" => $freebsd_version,
173
		"freebsd_machine" => $freebsd_machine
174
	);
175
	$msg = new XML_RPC_Message('pfsense.get_pkg_sizes', array(php_value_to_xmlrpc($params)));
176
	$xmlrpc_base_url = isset($config['system']['altpkgrepo']['enable']) ? $config['system']['altpkgrepo']['xmlrpcbaseurl'] : $g['xmlrpcbaseurl'];
177
	$cli = new XML_RPC_Client($g['xmlrpcpath'], $xmlrpc_base_url);
178
	$resp = $cli->send($msg, 10);
179
	if(!is_object($resp))
180
		log_error("Could not get response from XMLRPC server!");
181
 	else if (!$resp->faultCode()) {
182
		$raw_versions = $resp->value();
183
		return xmlrpc_value_to_php($raw_versions);
184
	}
185

    
186
	return array();
187
}
188

    
189
/*
190
 * resync_all_package_configs() Force packages to setup their configuration and rc.d files.
191
 * This function may also print output to the terminal indicating progress.
192
 */
193
function resync_all_package_configs($show_message = false) {
194
	global $config, $pkg_interface, $g;
195

    
196
	log_error("Resyncing configuration for all packages.");
197

    
198
	if (!is_array($config['installedpackages']['package']))
199
		return;
200

    
201
	if($show_message == true)
202
		echo "Syncing packages:";
203

    
204
	conf_mount_rw();
205

    
206
	foreach($config['installedpackages']['package'] as $idx => $package) {
207
		if (empty($package['name']))
208
			continue;
209
		if($show_message == true)
210
			echo " " . $package['name'];
211
		get_pkg_depends($package['name'], "all");
212
		if($g['booting'] != true)
213
			stop_service($package['name']);
214
		sync_package($idx, true, true);
215
		if($pkg_interface == "console") 
216
			echo "\nSyncing packages:";
217
	}
218

    
219
	if($show_message == true)
220
		echo " done.\n";
221

    
222
	@unlink("/conf/needs_package_sync");
223
	conf_mount_ro();
224
}
225

    
226
/*
227
 * is_freebsd_pkg_installed() - Check /var/db/pkg to determine whether or not a FreeBSD
228
 *				package is installed.
229
 */
230
function is_freebsd_pkg_installed($pkg) {
231
	if(!$pkg) 
232
		return;
233
	$output = "";
234
	exec("/usr/sbin/pkg_info -E \"{$pkg}*\"", $output, $retval);
235

    
236
	return (intval($retval) == 0);
237
}
238

    
239
/*
240
 * get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", return_nosync = 1):  Return a package's dependencies.
241
 *
242
 * $filetype = "all" || ".xml", ".tgz", etc.
243
 * $format = "files" (full filenames) || "names" (stripped / parsed depend names)
244
 * $return_nosync = 1 (return depends that have nosync set) | 0 (ignore packages with nosync)
245
 *
246
 */
247
function get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", $return_nosync = 1) {
248
	global $config;
249

    
250
	$pkg_id = get_pkg_id($pkg_name);
251
	if($pkg_id == -1)
252
		return -1; // This package doesn't really exist - exit the function.
253
	else if (!isset($config['installedpackages']['package'][$pkg_id]))
254
		return; // No package belongs to the pkg_id passed to this function.
255

    
256
	$package =& $config['installedpackages']['package'][$pkg_id];
257
	if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
258
		log_error("The {$package['name']} package is missing required dependencies and is being reinstalled." . $package['configurationfile']);
259
		uninstall_package($package['name']);
260
		if (install_package($package['name']) < 0) {
261
			log_error("Failed reinstalling package {$package['name']}.");
262
			return false;
263
		}
264
	}
265
	$pkg_xml = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
266
	if (!empty($pkg_xml['additional_files_needed'])) {
267
		foreach($pkg_xml['additional_files_needed'] as $item) {
268
			if ($return_nosync == 0 && isset($item['nosync']))
269
				continue; // Do not return depends with nosync set if not required.
270
			$depend_file = substr(strrchr($item['item']['0'], '/'),1); // Strip URLs down to filenames.
271
			$depend_name = substr(substr($depend_file,0,strpos($depend_file,".")+1),0,-1); // Strip filename down to dependency name.
272
			if (($filetype != "all") && (!preg_match("/{$filetype}/i", $depend_file)))
273
					continue;
274
			if ($item['prefix'] != "")
275
				$prefix = $item['prefix'];
276
			else
277
				$prefix = "/usr/local/pkg/";
278
			// Ensure that the prefix exists to avoid installation errors.
279
			if(!is_dir($prefix)) 
280
				exec("/bin/mkdir -p {$prefix}");
281
			if(!file_exists($prefix . $depend_file))
282
				log_error("The {$package['name']} package is missing required dependencies and must be reinstalled.");
283
			switch ($format) {
284
			case "files":
285
				$depends[] = $prefix . $depend_file;
286
				break;
287
			case "names":
288
				switch ($filetype) {
289
				case "all":
290
					if(preg_match("/\.xml/i", $depend_file)) {
291
						$depend_xml = parse_xml_config_pkg("/usr/local/pkg/{$depend_file}", "packagegui");
292
						if (!empty($depend_xml))
293
							$depends[] = $depend_xml['name'];
294
					} else
295
						$depends[] = $depend_name; // If this dependency isn't package XML, use the stripped filename.
296
					break;
297
				case ".xml":
298
					$depend_xml = parse_xml_config_pkg("/usr/local/pkg/" . $depend_file, "packagegui");
299
					if (!empty($depend_xml))
300
						$depends[] = $depend_xml['name'];
301
					break;
302
				default:
303
					$depends[] = $depend_name; // If we aren't looking for XML, use the stripped filename (it's all we have).
304
					break;
305
				}
306
			}
307
		}
308
		return $depends;
309
	}
310
}
311

    
312
function uninstall_package($pkg_name) {
313
	global $config, $static_output;
314
	global $builder_package_install;
315

    
316
	// Back up /usr/local/lib libraries first if
317
	// not running from the builder code.
318
	if(!$builder_package_install) {
319
		if(!file_exists("/tmp/pkg_libs.tgz")) {
320
			$static_output .= "Backing up libraries... ";
321
			update_output_window($static_output);
322
			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`");
323
			$static_output .= "\n";
324
		}
325
	}
326

    
327
	$id = get_pkg_id($pkg_name);
328
	if ($id >= 0) {
329
		$pkg_depends =& $config['installedpackages']['package'][$id]['depends_on_package'];
330
		$static_output .= "Removing package...\n";
331
		update_output_window($static_output);
332
		if (is_array($pkg_depends)) {
333
			foreach ($pkg_depends as $pkg_depend)
334
				delete_package($pkg_depend);
335
		}
336
	}
337
	delete_package_xml($pkg_name);
338

    
339
	// Restore libraries that we backed up if not 
340
	// running from the builder code.
341
	if(!$builder_package_install) {
342
		$static_output .= "Cleaning up... ";
343
		update_output_window($static_output);
344
		exec("/usr/bin/tar xzPfU /tmp/pkg_libs.tgz -C /");
345
		@unlink("/tmp/pkg_libs.tgz");	
346
	}
347
}
348

    
349
function force_remove_package($pkg_name) {
350
	delete_package_xml($pkg_name);
351
}
352

    
353
/*
354
 * sync_package($pkg_name, $sync_depends = true, $show_message = false) Force a package to setup its configuration and rc.d files.
355
 */
356
function sync_package($pkg_name, $sync_depends = true, $show_message = false) {
357
	global $config, $config_parsed;
358
	global $builder_package_install;
359
	
360
	// If this code is being called by pfspkg_installer 
361
	// which the builder system uses then return (ignore).
362
	if($builder_package_install)
363
		return;
364
	
365
	if(empty($config['installedpackages']['package']))
366
		return;
367
	if(!is_numeric($pkg_name)) {
368
		$pkg_id = get_pkg_id($pkg_name);
369
		if($pkg_id == -1)
370
			return -1; // This package doesn't really exist - exit the function.
371
	} else {
372
		$pkg_id = $pkg_name;
373
		if(empty($config['installedpackages']['package'][$pkg_id]))
374
			return;  // No package belongs to the pkg_id passed to this function.
375
	}
376
        if (is_array($config['installedpackages']['package'][$pkg_id]))
377
		$package =& $config['installedpackages']['package'][$pkg_id];
378
        else
379
		return; /* empty package tag */
380
	if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
381
		log_error("The {$package['name']} package is missing its configuration file and must be reinstalled.");
382
		force_remove_package($package['name']);
383
		return -1;
384
	}
385
	$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
386
	if(isset($pkg_config['nosync']))
387
		return;
388
	/* Bring in package include files */
389
	if (!empty($pkg_config['include_file'])) {
390
		$include_file = $pkg_config['include_file'];
391
		if (file_exists($include_file))
392
			require_once($include_file);
393
		else {
394
			/* XXX: What the heck is this?! */
395
			log_error("Reinstalling package {$package['name']} because its include file({$include_file}) is missing!");
396
			uninstall_package($package['name']);
397
			if (install_package($package['name']) < 0) {
398
				log_error("Reinstalling package {$package['name']} failed. Take appropriate measures!!!");
399
				return -1;
400
			}
401
		}
402
	}
403

    
404
	if(!empty($pkg_config['custom_php_global_functions']))
405
		eval($pkg_config['custom_php_global_functions']);
406
	if(!empty($pkg_config['custom_php_resync_config_command']))
407
		eval($pkg_config['custom_php_resync_config_command']);
408
	if($sync_depends == true) {
409
		$depends = get_pkg_depends($pkg_name, ".xml", "files", 1); // Call dependency handler and do a little more error checking.
410
		if(is_array($depends)) {
411
			foreach($depends as $item) {
412
				if(!file_exists($item)) {
413
					require_once("notices.inc");
414
					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);
415
					log_error("Could not find {$item}. Reinstalling package.");
416
					uninstall_package($pkg_name);
417
					if (install_package($pkg_name) < 0) {
418
						log_error("Reinstalling package {$package['name']} failed. Take appropriate measures!!!");
419
						return -1;
420
					}
421
				} else {
422
					$item_config = parse_xml_config_pkg($item, "packagegui");
423
					if (empty($item_config))
424
						continue;
425
					if(isset($item_config['nosync']))
426
						continue;
427
					if (!empty($item_config['include_file'])) {
428
						if (file_exists($item_config['include_file']))	
429
							require_once($item_config['include_file']);
430
						else {
431
							log_error("Not calling package sync code for dependency {$item_config['name']} of {$package['name']} because some include files are missing.");
432
							continue;
433
						}
434
					}
435
					if($item_config['custom_php_global_functions'] <> "")
436
						eval($item_config['custom_php_global_functions']);
437
					if($item_config['custom_php_resync_config_command'] <> "")
438
						eval($item_config['custom_php_resync_config_command']);
439
					if($show_message == true)
440
						print " " . $item_config['name'];
441
				}
442
			}
443
		}
444
	}
445
}
446

    
447
/*
448
 * pkg_fetch_recursive: Download and install a FreeBSD package and its dependencies. This function provides output to
449
 * 			a progress bar and output window.
450
 */
451
function pkg_fetch_recursive($pkgname, $filename, $dependlevel = 0, $base_url = "") {
452
	global $static_output, $g;
453

    
454
	if (($g['platform'] == "nanobsd") || ($g['platform'] == "embedded")) {
455
		$pkgtmpdir = "/usr/bin/env PKG_TMPDIR=/root/ ";
456
		$pkgstagingdir = "/root/tmp";
457
		if (!is_dir($pkgstagingdir))
458
			mkdir($pkgstagingdir);
459
		$pkgstaging = "-t {$pkgstagingdir}/instmp.XXXXXX";
460
		$fetchdir = $pkgstagingdir;
461
	} else {
462
		$fetchdir = $g['tmp_path'];
463
	}
464

    
465
	$osname = php_uname("s");
466
	$arch =  php_uname("m");
467
	$rel = strtolower(php_uname("r"));
468
	if (substr_count($rel, '-') > 1)
469
		$rel = substr($rel, 0, strrpos($rel, "-"));
470
	$priv_url = "http://ftp2.{$osname}.org/pub/{$osname}/ports/{$arch}/packages-{$rel}/All";
471
	if (empty($base_url))
472
		$base_url = $priv_url;
473
	if (substr($base_url, -1) == "/")
474
		$base_url = substr($base_url, 0, -1);
475
	$fetchto = "{$fetchdir}/apkg_{$filename}";
476
	$static_output .= "\n" . str_repeat(" ", $dependlevel * 2 + 1) . "Downloading {$base_url}/{$filename} ... ";
477
	if (download_file_with_progress_bar("{$base_url}/{$filename}", $fetchto) !== true) {
478
		if ($base_url != $priv_url && download_file_with_progress_bar("{$priv_url}/{$filename}", $fetchto) !== true) {
479
			$static_output .= " could not download from there or {$priv_url}/{$filename}.\n";
480
			update_output_window($static_output);
481
			return false;
482
		} else if ($base_url == $priv_url) {
483
			$static_output .= " failed to download.\n";
484
			update_output_window($static_output);
485
			return false;
486
		} else {
487
			$static_output .= " [{$osname} repository]\n";
488
			update_output_window($static_output);
489
		}
490
	}
491
	$static_output .= " (extracting)";
492
	update_output_window($static_output);
493
	$slaveout = "";
494
	exec("/usr/bin/tar --fast-read -O -f {$fetchto} -x +CONTENTS 2>&1", $slaveout);
495
	$raw_depends_list = array_values(preg_grep("/\@pkgdep/", $slaveout));
496
	if ($raw_depends_list != "") {
497
		$pkg_extension = ".tbz";
498
		foreach($raw_depends_list as $adepend) {
499
			$working_depend = explode(" ", trim($adepend, "\n"));
500
			if (substr($working_depend[1], -4) != ".tbz")
501
				$depend_filename = $working_depend[1] . $pkg_extension;
502
			else
503
				$depend_filename = $working_depend[1];
504
			if (!is_freebsd_pkg_installed($working_depend[1])) {
505
				if (pkg_fetch_recursive($working_depend[1], $depend_filename, $dependlevel + 1, $base_url) == false)
506
					return false;
507
			} else {
508
				pkg_debug($working_depend[1] . "\n");
509
			}
510
		}
511
	}
512

    
513
	$pkgaddout = "";
514
	exec("{$pkgtmpdir}/usr/sbin/pkg_add {$pkgstaging} -fv {$fetchto} 2>&1", $pkgaddout);
515
	pkg_debug($pkgname . " " . print_r($pkgaddout, true) . "\npkg_add successfully completed.\n");
516

    
517
	return true;
518
}
519

    
520
function install_package($package, $pkg_info = "") {
521
	global $g, $config, $static_output, $pkg_interface;
522

    
523
	/* safe side. Write config below will send to ro again. */
524
	conf_mount_rw();
525

    
526
	if($pkg_interface == "console") 	
527
		echo "\n";
528
	/* fetch package information if needed */
529
	if(empty($pkg_info) or !is_array($pkg_info[$package])) {
530
		$pkg_info = get_pkg_info(array($package));
531
		$pkg_info = $pkg_info[$package]; // We're only dealing with one package, so we can strip away the extra array.
532
		if (empty($pkg_info)) {
533
			conf_mount_ro();
534
			return -1;
535
		}
536
	}
537
	pkg_debug("Beginning package installation.\n");
538
	log_error('Beginning package installation for ' . $pkg_info['name'] . '.');
539
	$static_output .= "Beginning package installation for " . $pkg_info['name'] . "...";
540
	update_status($static_output);
541
	/* fetch the package's configuration file */
542
	if($pkg_info['config_file'] != "") {
543
		$static_output .= "\nDownloading package configuration file... ";
544
		update_output_window($static_output);
545
		pkg_debug("Downloading package configuration file...\n");
546
		$fetchto = substr(strrchr($pkg_info['config_file'], '/'), 1);
547
		download_file_with_progress_bar($pkg_info['config_file'], '/usr/local/pkg/' . $fetchto);
548
		if(!file_exists('/usr/local/pkg/' . $fetchto)) {
549
			pkg_debug("ERROR! Unable to fetch package configuration file. Aborting installation.\n");
550
			if($pkg_interface == "console")
551
				print "\nERROR! Unable to fetch package configuration file. Aborting package installation.\n";
552
			else {
553
				$static_output .= "failed!\n\nInstallation aborted.\n";
554
				update_output_window($static_output);
555
				echo "<br>Show <a href=\"pkg_mgr_install.php?showlog=true\">install log</a></center>";
556
			}
557
			conf_mount_ro();
558
			return -1;
559
		}
560
		$static_output .= "done.\n";
561
		update_output_window($static_output);
562
	}
563
	/* add package information to config.xml */
564
	$pkgid = get_pkg_id($pkg_info['name']);
565
	$static_output .= "Saving updated package information... ";
566
	update_output_window($static_output);
567
	if($pkgid == -1) {
568
		$config['installedpackages']['package'][] = $pkg_info;
569
		$changedesc = "Installed {$pkg_info['name']} package.";
570
		$to_output = "done.\n";
571
	} else {
572
		$config['installedpackages']['package'][$pkgid] = $pkg_info;
573
		$changedesc = "Overwrote previous installation of {$pkg_info['name']}.";
574
		$to_output = "overwrite!\n";
575
	}
576
	if(file_exists('/conf/needs_package_sync'))
577
		@unlink('/conf/needs_package_sync');
578
	conf_mount_ro();
579
	write_config("Intermediate config write during package install for {$pkg_info['name']}.");
580
	$static_output .= $to_output;
581
	update_output_window($static_output);
582
	/* install other package components */
583
	if (!install_package_xml($package)) {
584
		uninstall_package($package);
585
		write_config($changedesc);
586
		$static_output .= "Failed to install package.\n";
587
		update_output_window($static_output);
588
		return -1;
589
	} else {
590
		$static_output .= "Writing configuration... ";
591
		update_output_window($static_output);
592
		write_config($changedesc);
593
		$static_output .= "done.\n";
594
		update_output_window($static_output);
595
		$static_output .= "Starting service.\n";
596
		update_output_window($static_output);
597
		if($pkg_info['after_install_info']) 
598
			update_output_window($pkg_info['after_install_info']);	
599
	}
600
}
601

    
602
function get_after_install_info($package) {
603
	global $pkg_info;
604
	/* fetch package information if needed */
605
	if(!$pkg_info or !is_array($pkg_info[$package])) {
606
		$pkg_info = get_pkg_info(array($package));
607
		$pkg_info = $pkg_info[$package]; // We're only dealing with one package, so we can strip away the extra array.
608
	}
609
	if($pkg_info['after_install_info'])
610
		return $pkg_info['after_install_info'];
611
}
612

    
613
function eval_once($toeval) {
614
	global $evaled;
615
	if(!$evaled) $evaled = array();
616
	$evalmd5 = md5($toeval);
617
	if(!in_array($evalmd5, $evaled)) {
618
		@eval($toeval);
619
		$evaled[] = $evalmd5;
620
	}
621
	return;
622
}
623

    
624
function install_package_xml($pkg) {
625
	global $g, $config, $static_output, $pkg_interface, $config_parsed;
626

    
627
	if(($pkgid = get_pkg_id($pkg)) == -1) {
628
		$static_output .= "The {$pkg} package is not installed.\n\nInstallation aborted.";
629
		update_output_window($static_output);
630
		if($pkg_interface <> "console") {
631
			echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
632
			echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
633
		}
634
		sleep(1);
635
		return false;
636
	} else
637
		$pkg_info = $config['installedpackages']['package'][$pkgid];
638

    
639
	/* pkg_add the package and its dependencies */
640
	if($pkg_info['depends_on_package_base_url'] != "") {
641
		if($pkg_interface == "console") 
642
			echo "\n";
643
		update_status("Installing " . $pkg_info['name'] . " and its dependencies.");
644
		$static_output .= "Downloading " . $pkg_info['name'] . " and its dependencies... ";
645
		$static_orig = $static_output;
646
		$static_output .= "\n";
647
		update_output_window($static_output);
648
		foreach((array) $pkg_info['depends_on_package'] as $pkgdep) {
649
			$pkg_name = substr(reverse_strrchr($pkgdep, "."), 0, -1);
650
			$static_output = $static_orig . "\nChecking for package installation... ";
651
			update_output_window($static_output);
652
			if (!is_freebsd_pkg_installed($pkg_name)) {
653
				if (!pkg_fetch_recursive($pkg_name, $pkgdep, 0, $pkg_info['depends_on_package_base_url'])) {
654
					$static_output .= "of {$pkg_name} failed!\n\nInstallation aborted.";
655
					update_output_window($static_output);
656
					pkg_debug("Package WAS NOT installed properly.\n");
657
					if($pkg_interface <> "console") {
658
						echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
659
						echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
660
					}
661
					sleep(1);
662
					return false;
663
				}
664
			}
665
		}
666
	}
667
	$configfile = substr(strrchr($pkg_info['config_file'], '/'), 1);
668
	if(file_exists("/usr/local/pkg/" . $configfile)) {
669
		$static_output .= "Loading package configuration... ";
670
		update_output_window($static_output);
671
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $configfile, "packagegui");
672
		$static_output .= "done.\n";
673
		update_output_window($static_output);
674
		$static_output .= "Configuring package components...\n";
675
		if (!empty($pkg_config['filter_rules_needed']))
676
			$config['installedpackages']['package'][$pkgid]['filter_rule_function'] = $pkg_config['filter_rules_needed'];
677
		update_output_window($static_output);
678
		/* modify system files */
679
		if(is_array($pkg_config['modify_system']) && is_array($pkg_config['modify_system']['item'])) {
680
			$static_output .= "System files... ";
681
			update_output_window($static_output);
682
			foreach($pkg_config['modify_system']['item'] as $ms) {
683
				if($ms['textneeded']) {
684
					add_text_to_file($ms['modifyfilename'], $ms['textneeded']);
685
				}
686
			}
687
			$static_output .= "done.\n";
688
			update_output_window($static_output);
689
		}
690
		/* download additional files */
691
		if(is_array($pkg_config['additional_files_needed'])) {
692
			$static_output .= "Additional files... ";
693
			$static_orig = $static_output;
694
			update_output_window($static_output);
695
			foreach($pkg_config['additional_files_needed'] as $afn) {
696
				$filename = get_filename_from_url($afn['item'][0]);
697
				if($afn['chmod'] <> "")
698
					$pkg_chmod = $afn['chmod'];
699
				else
700
					$pkg_chmod = "";
701

    
702
				if($afn['prefix'] <> "")
703
					$prefix = $afn['prefix'];
704
				else
705
					$prefix = "/usr/local/pkg/";
706

    
707
				if(!is_dir($prefix)) 
708
					safe_mkdir($prefix);
709
 				$static_output .= $filename . " ";
710
				update_output_window($static_output);
711
				if (download_file_with_progress_bar($afn['item'][0], $prefix . $filename) !== true) {
712
					$static_output .= "failed.\n";
713
					update_output_window($static_output);
714
					return false;
715
				}
716
				if(stristr($filename, ".tgz") <> "") {
717
					pkg_debug("Extracting tarball to -C for " . $filename . "...\n");
718
					$tarout = "";
719
					exec("/usr/bin/tar xvzf " . $prefix . $filename . " -C / 2>&1", $tarout);
720
					pkg_debug(print_r($tarout, true) . "\n");
721
				}
722
				if($pkg_chmod <> "") {
723
					pkg_debug("Changing file mode to {$pkg_chmod} for {$prefix}{$filename}\n");
724
					@chmod($prefix . $filename, $pkg_chmod);
725
					system("/bin/chmod {$pkg_chmod} {$prefix}{$filename}");
726
				}
727
				$static_output = $static_orig;
728
                                update_output_window($static_output);
729
			}
730
			$static_output .= "done.\n";
731
			update_output_window($static_output);
732
		}
733
		/*   if a require exists, include it.  this will
734
		 *   show us where an error exists in a package
735
		 *   instead of making us blindly guess
736
		 */
737
		$missing_include = false;
738
		if($pkg_config['include_file'] <> "") {
739
			$static_output .= "Loading package instructions...\n";
740
			update_output_window($static_output);
741
			pkg_debug("require_once('{$pkg_config['include_file']}')\n");
742
			if (file_exists($pkg_config['include_file']))
743
				require_once($pkg_config['include_file']);
744
			else {
745
				$missing_include = true;
746
				$static_output .= "Include " . basename($pkg_config['include_file']) . " is missing!\n";
747
				update_output_window($static_output);
748
				/* XXX: Should undo the steps before this?! */
749
				return false;
750
			}
751
		}
752
		/* sidebar items */
753
		if(is_array($pkg_config['menu'])) {
754
			$static_output .= "Menu items... ";
755
			update_output_window($static_output);
756
			foreach($pkg_config['menu'] as $menu) {
757
				if(is_array($config['installedpackages']['menu']))
758
					foreach($config['installedpackages']['menu'] as $amenu)
759
						if($amenu['name'] == $menu['name'])
760
							continue 2;
761
				$config['installedpackages']['menu'][] = $menu;
762
			}
763
			$static_output .= "done.\n";
764
			update_output_window($static_output);
765
		}
766
		/* integrated tab items */
767
		if(is_array($pkg_config['tabs']['tab'])) {
768
			$static_output .= "Integrated Tab items... ";
769
			update_output_window($static_output);
770
			foreach($pkg_config['tabs']['tab'] as $tab) {
771
				if(is_array($config['installedpackages']['tab']))
772
					foreach($config['installedpackages']['tab'] as $atab)
773
						if($atab['name'] == $tab['name'])
774
							continue 2;
775
				$config['installedpackages']['tab'][] = $tab;
776
			}
777
			$static_output .= "done.\n";
778
			update_output_window($static_output);
779
		}
780
		/* services */
781
		if(is_array($pkg_config['service'])) {
782
			$static_output .= "Services... ";
783
			update_output_window($static_output);
784
			foreach($pkg_config['service'] as $service) {
785
				if(is_array($config['installedpackages']['service']))
786
					foreach($config['installedpackages']['service'] as $aservice)
787
						if($aservice['name'] == $service['name'])
788
							continue 2;
789
				$config['installedpackages']['service'][] = $service;
790
			}
791
			$static_output .= "done.\n";
792
			update_output_window($static_output);
793
		}
794
		/* custom commands */
795
		$static_output .= "Custom commands...\n";
796
		update_output_window($static_output);
797
		if ($missing_include == false) {
798
			if($pkg_config['custom_php_global_functions'] <> "") {
799
				$static_output .= "Executing custom_php_global_functions()...";
800
				update_output_window($static_output);
801
				eval_once($pkg_config['custom_php_global_functions']);
802
				$static_output .= "done.\n";
803
				update_output_window($static_output);
804
			}
805
			if($pkg_config['custom_php_install_command']) {
806
				$static_output .= "Executing custom_php_install_command()...";
807
				update_output_window($static_output);
808
				eval_once($pkg_config['custom_php_install_command']);
809
				$static_output .= "done.\n";
810
				update_output_window($static_output);
811
			}
812
			if($pkg_config['custom_php_resync_config_command'] <> "") {
813
				$static_output .= "Executing custom_php_resync_config_command()...";
814
				update_output_window($static_output);
815
				eval_once($pkg_config['custom_php_resync_config_command']);
816
				$static_output .= "done.\n";
817
				update_output_window($static_output);
818
			}
819
		}
820
	} else {
821
		$static_output .= "Loading package configuration... failed!\n\nInstallation aborted.";
822
		update_output_window($static_output);
823
		pkg_debug("Unable to load package configuration. Installation aborted.\n");
824
		if($pkg_interface <> "console") {
825
			echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
826
			echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
827
		}
828
		sleep(1);
829
		return false;
830
	}
831

    
832
	/* set up package logging streams */
833
	if($pkg_info['logging']) {
834
		mwexec("/usr/sbin/fifolog_create -s 32768 {$g['varlog_path']}/{$pkg_info['logging']['logfilename']}");
835
		@chmod($g['varlog_path'] . '/' . $pkg_info['logging']['logfilename'], 0600);
836
		add_text_to_file("/etc/syslog.conf", $pkg_info['logging']['facilityname'] . "\t\t\t\t" . $pkg_info['logging']['logfilename']);
837
		pkg_debug("Adding text to file /etc/syslog.conf\n");
838
		system_syslogd_start();
839
	}
840

    
841
	return true;
842
}
843

    
844
function does_package_depend($pkg) {
845
	// Should not happen, but just in case.
846
	if(!$pkg)
847
		return;
848
	$pkg_var_db_dir = glob("/var/db/pkg/{$pkg}*");
849
	// If this package has dependency then return true
850
	foreach($pkg_var_db_dir as $pvdd) {
851
		if (file_exists("{$vardb}/{$pvdd}/+REQUIRED_BY") && count(file("{$vardb}/{$pvdd}/+REQUIRED_BY")) > 0) 
852
			return true;
853
	}	
854
	// Did not find a record of dependencies, so return false.
855
	return false;
856
}
857

    
858
function delete_package($pkg) {
859
	global $config, $g, $static_output, $vardb;
860

    
861
	if(!$pkg) 
862
		return;
863

    
864
	$pkg = substr(reverse_strrchr($pkg, "."), 0, -1);
865

    
866
	// If package has dependencies then skip it
867
	if(does_package_depend($pkg)) {
868
		$static_output .= "Skipping package deletion for {$pkg} because it is a dependency.\n";
869
		update_output_window($static_output);
870
		return;		
871
	} else {
872
		if($pkg)
873
			$static_output .= "Starting package deletion for {$pkg}...";
874
		update_output_window($static_output);		
875
	}
876

    
877
	$info = "";
878
	exec("/usr/sbin/pkg_info -qrx {$pkg}", $info);
879
	remove_freebsd_package($pkg);
880
	$static_output .= "done.\n";
881
	update_output_window($static_output);
882
	foreach($info as $line) {
883
		$depend = trim(str_replace("@pkgdep ", "", $line), " \n");
884
		// If package has dependencies then skip it
885
		if(!does_package_depend($depend)) 			
886
			delete_package($depend);
887
	}
888

    
889
	/* Rescan directories for what has been left and avoid fooling other programs. */
890
	mwexec("/sbin/ldconfig");
891

    
892
	return;
893
}
894

    
895
function delete_package_xml($pkg) {
896
	global $g, $config, $static_output, $pkg_interface;
897

    
898
	conf_mount_rw();
899

    
900
	$pkgid = get_pkg_id($pkg);
901
	if ($pkgid == -1) {
902
		$static_output .= "The {$pkg} package is not installed.\n\nDeletion aborted.";
903
		update_output_window($static_output);
904
		if($pkg_interface <> "console") {
905
			echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
906
			echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
907
		}
908
		ob_flush();
909
		sleep(1);
910
		conf_mount_ro();
911
		return;
912
	}
913
	pkg_debug("Removing {$pkg} package... ");
914
	$static_output .= "Removing {$pkg} components...\n";
915
	update_output_window($static_output);
916
	/* parse package configuration */
917
	$packages = &$config['installedpackages']['package'];
918
	$tabs =& $config['installedpackages']['tab'];
919
	$menus =& $config['installedpackages']['menu'];
920
	$services = &$config['installedpackages']['service'];
921
	$pkg_info =& $packages[$pkgid];
922
	if(file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
923
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $packages[$pkgid]['configurationfile'], "packagegui");
924
		/* remove tab items */
925
		if(is_array($pkg_config['tabs'])) {
926
			$static_output .= "Tabs items... ";
927
			update_output_window($static_output);
928
			if(is_array($pkg_config['tabs']['tab']) && is_array($tabs)) {
929
				foreach($pkg_config['tabs']['tab'] as $tab) {
930
					foreach($tabs as $key => $insttab) {
931
						if($insttab['name'] == $tab['name']) {
932
							unset($tabs[$key]);
933
							break;
934
						}
935
					}
936
				}
937
			}
938
			$static_output .= "done.\n";
939
			update_output_window($static_output);
940
		}
941
		/* remove menu items */
942
		if(is_array($pkg_config['menu'])) {
943
			$static_output .= "Menu items... ";
944
			update_output_window($static_output);
945
			if (is_array($pkg_config['menu']) && is_array($menus)) {
946
				foreach($pkg_config['menu'] as $menu) {
947
					foreach($menus as $key => $instmenu) {
948
						if($instmenu['name'] == $menu['name']) {
949
							unset($menus[$key]);
950
							break;
951
						}
952
					}
953
				}
954
			}
955
			$static_output .= "done.\n";
956
			update_output_window($static_output);
957
		}
958
		/* remove services */
959
		if(is_array($pkg_config['service'])) {
960
			$static_output .= "Services... ";
961
			update_output_window($static_output);
962
			if (is_array($pkg_config['service']) && is_array($services)) {
963
				foreach($pkg_config['service'] as $service) {
964
					foreach($services as $key => $instservice) {
965
						if($instservice['name'] == $service['name']) {
966
							if($g['booting'] != true)
967
								stop_service($service['name']);
968
							unset($services[$key]);
969
						}
970
					}
971
				}
972
			}
973
			$static_output .= "done.\n";
974
			update_output_window($static_output);
975
		}
976
		/*
977
		 * XXX: Otherwise inclusion of config.inc again invalidates actions taken.
978
		 * 	Same is done during installation.
979
		 */
980
		write_config("Intermediate config write during package removal for {$pkg}.");
981

    
982
		/*
983
		 * If a require exists, include it.  this will
984
		 * show us where an error exists in a package
985
		 * instead of making us blindly guess
986
		 */
987
		$missing_include = false;
988
		if($pkg_config['include_file'] <> "") {
989
			$static_output .= "Loading package instructions...\n";
990
			update_output_window($static_output);
991
			pkg_debug("require_once(\"{$pkg_config['include_file']}\")\n");
992
			if (file_exists($pkg_config['include_file']))
993
				require_once($pkg_config['include_file']);
994
			else {
995
				$missing_include = true;
996
				update_output_window($static_output);
997
				$static_output .= "Include file " . basename($pkg_config['include_file']) . " could not be found for inclusion.\n";
998
			}
999
		}
1000
		/* ermal
1001
		 * NOTE: It is not possible to handle parse errors on eval.
1002
		 * So we prevent it from being run at all to not interrupt all the other code.
1003
		 */
1004
		if ($missing_include == false) {
1005
			/* evalate this package's global functions and pre deinstall commands */
1006
			if($pkg_config['custom_php_global_functions'] <> "")
1007
				eval_once($pkg_config['custom_php_global_functions']);
1008
			if($pkg_config['custom_php_pre_deinstall_command'] <> "")
1009
				eval_once($pkg_config['custom_php_pre_deinstall_command']);
1010
		}
1011
		/* system files */
1012
		if(is_array($pkg_config['modify_system']) && is_array($pkg_config['modify_system']['item'])) {
1013
			$static_output .= "System files... ";
1014
			update_output_window($static_output);
1015
			foreach($pkg_config['modify_system']['item'] as $ms)
1016
				if($ms['textneeded']) remove_text_from_file($ms['modifyfilename'], $ms['textneeded']);
1017

    
1018
			$static_output .= "done.\n";
1019
			update_output_window($static_output);
1020
		}
1021
		/* deinstall commands */
1022
		if($pkg_config['custom_php_deinstall_command'] <> "") {
1023
			$static_output .= "Deinstall commands... ";
1024
			update_output_window($static_output);
1025
			if ($missing_include == false) {
1026
				eval_once($pkg_config['custom_php_deinstall_command']);
1027
				$static_output .= "done.\n";
1028
			} else
1029
				$static_output .= "\nNot executing custom deinstall hook because an include is missing.\n";
1030
			update_output_window($static_output);
1031
		}
1032
		if($pkg_config['include_file'] <> "") {
1033
			$static_output .= "Removing package instructions...";
1034
			update_output_window($static_output);
1035
			pkg_debug("Remove '{$pkg_config['include_file']}'\n");
1036
			unlink_if_exists("/usr/local/pkg/" . $pkg_config['include_file']);
1037
			$static_output .= "done.\n";
1038
			update_output_window($static_output);
1039
		}
1040
		/* remove all additional files */
1041
		if(is_array($pkg_config['additional_files_needed'])) {
1042
			$static_output .= "Auxiliary files... ";
1043
			update_output_window($static_output);
1044
			foreach($pkg_config['additional_files_needed'] as $afn) {
1045
				$filename = get_filename_from_url($afn['item'][0]);
1046
				if($afn['prefix'] <> "")
1047
					$prefix = $afn['prefix'];
1048
				else
1049
					$prefix = "/usr/local/pkg/";
1050
				unlink_if_exists($prefix . $filename);
1051
			}
1052
			$static_output .= "done.\n";
1053
			update_output_window($static_output);
1054
		}
1055
		/* package XML file */
1056
		$static_output .= "Package XML... ";
1057
		update_output_window($static_output);
1058
		unlink_if_exists("/usr/local/pkg/" . $packages[$pkgid]['configurationfile']);
1059
		$static_output .= "done.\n";
1060
		update_output_window($static_output);
1061
	}
1062
	/* syslog */
1063
	if(is_array($pkg_info['logging']) && $pkg_info['logging']['logfile_name'] <> "") {
1064
		$static_output .= "Syslog entries... ";
1065
		update_output_window($static_output);
1066
		remove_text_from_file("/etc/syslog.conf", $pkg_info['logging']['facilityname'] . "\t\t\t\t" . $pkg_info['logging']['logfilename']);
1067
		system_syslogd_start();
1068
		@unlink("{$g['varlog_path']}/{$pkg_info['logging']['logfilename']}");
1069
		$static_output .= "done.\n";
1070
		update_output_window($static_output);
1071
	}
1072
	
1073
	conf_mount_ro();
1074
	/* remove config.xml entries */
1075
	$static_output .= "Configuration... ";
1076
	update_output_window($static_output);
1077
	unset($config['installedpackages']['package'][$pkgid]);
1078
	$static_output .= "done.\n";
1079
	update_output_window($static_output);
1080
	write_config("Removed {$pkg} package.\n");
1081
}
1082

    
1083
function expand_to_bytes($size) {
1084
	$conv = array(
1085
			"G" =>	"3",
1086
			"M" =>  "2",
1087
			"K" =>  "1",
1088
			"B" =>  "0"
1089
		);
1090
	$suffix = substr($size, -1);
1091
	if(!in_array($suffix, array_keys($conv))) return $size;
1092
	$size = substr($size, 0, -1);
1093
	for($i = 0; $i < $conv[$suffix]; $i++) {
1094
		$size *= 1024;
1095
	}
1096
	return $size;
1097
}
1098

    
1099
function get_pkg_db() {
1100
	global $g;
1101
	return return_dir_as_array($g['vardb_path'] . '/pkg');
1102
}
1103

    
1104
function walk_depend($depend, $pkgdb = "", $alreadyseen = "") {
1105
	if(!$pkgdb)
1106
		$pkgdb = get_pkg_db();
1107
	if(!is_array($alreadyseen))
1108
		$alreadyseen = array();
1109
	if (!is_array($depend))
1110
		$depend = array();
1111
	foreach($depend as $adepend) {
1112
		$pkgname = reverse_strrchr($adepend['name'], '.');
1113
		if(in_array($pkgname, $alreadyseen)) {
1114
			continue;
1115
		} elseif(!in_array($pkgname, $pkgdb)) {
1116
			$size += expand_to_bytes($adepend['size']);
1117
			$alreadyseen[] = $pkgname;
1118
			if(is_array($adepend['depend'])) $size += walk_depend($adepend['depend'], $pkgdb, $alreadyseen);
1119
		}
1120
	}
1121
	return $size;
1122
}
1123

    
1124
function get_package_install_size($pkg = 'all', $pkg_info = "") {
1125
	global $config, $g;
1126
	if((!is_array($pkg)) and ($pkg != 'all'))
1127
		$pkg = array($pkg);
1128
	$pkgdb = get_pkg_db();
1129
	if(!$pkg_info)
1130
		$pkg_info = get_pkg_sizes($pkg);
1131
	foreach($pkg as $apkg) {
1132
		if(!$pkg_info[$apkg])
1133
			continue;
1134
		$toreturn[$apkg] = expand_to_bytes(walk_depend(array($pkg_info[$apkg]), $pkgdb));
1135
	}
1136
	return $toreturn;
1137
}
1138

    
1139
function squash_from_bytes($size, $round = "") {
1140
	$conv = array(1 => "B", "K", "M", "G");
1141
	foreach($conv as $div => $suffix) {
1142
		$sizeorig = $size;
1143
		if(($size /= 1024) < 1) {
1144
			if($round) {
1145
				$sizeorig = round($sizeorig, $round);
1146
			}
1147
			return $sizeorig . $suffix;
1148
		}
1149
	}
1150
	return;
1151
}
1152

    
1153
function pkg_reinstall_all() {
1154
	global $g, $config;
1155
	$pkg_id = 0;
1156
	$todo = array();
1157
	if (is_array($config['installedpackages']['package']))
1158
		foreach($config['installedpackages']['package'] as $package)
1159
			$todo[] = array('name' => $package['name'], 'version' => $package['version']);
1160
	echo "One moment please, reinstalling packages...\n";
1161
	echo " >>> Trying to fetch package info...";
1162
	$pkg_info = get_pkg_info();
1163
	if ($pkg_info) {
1164
		echo " Done.\n";
1165
	} else {
1166
		$xmlrpc_base_url = isset($config['system']['altpkgrepo']['enable']) ? $config['system']['altpkgrepo']['xmlrpcbaseurl'] : $g['xmlrpcbaseurl'];
1167
		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";
1168
		return;
1169
	}
1170
	if(is_array($todo)) {
1171
		foreach($todo as $pkgtodo) {
1172
			$static_output = "";
1173
			if($pkgtodo['name']) {
1174
				uninstall_package($pkgtodo['name']);
1175
				install_package($pkgtodo['name']);
1176
				$pkg_id++;
1177
			}
1178
		}
1179
	}
1180
}
1181

    
1182
?>
(36-36/61)