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
		update_output_window($static_output);
596
		if($pkg_info['after_install_info']) 
597
			update_output_window($pkg_info['after_install_info']);	
598
	}
599
}
600

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

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

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

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

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

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

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

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

    
840
	return true;
841
}
842

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

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

    
860
	if(!$pkg) 
861
		return;
862

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

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

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

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

    
891
	return;
892
}
893

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

    
897
	conf_mount_rw();
898

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

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

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

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

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

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

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

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

    
1152
function pkg_reinstall_all() {
1153
	global $g, $config;
1154

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

    
1183
?>
(36-36/61)