Project

General

Profile

Download (40.5 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
	$osname = php_uname("s");
455
	$arch =  php_uname("m");
456
	$rel = strtolower(php_uname("r"));
457
	if (substr_count($rel, '-') > 1)
458
		$rel = substr($rel, 0, strrpos($rel, "-"));
459
	$priv_url = "http://ftp2.{$osname}.org/pub/{$osname}/ports/{$arch}/packages-{$rel}/All";
460
	if (empty($base_url))
461
		$base_url = $priv_url;
462
	if (substr($base_url, -1) == "/")
463
		$base_url = substr($base_url, 0, -1);
464
	$fetchto = "{$g['tmp_path']}/apkg_{$filename}";
465
	$static_output .= "\n" . str_repeat(" ", $dependlevel * 2 + 1) . "Downloading {$base_url}/{$filename} ... ";
466
	if (download_file_with_progress_bar("{$base_url}/{$filename}", $fetchto) !== true) {
467
		if ($base_url != $priv_url && download_file_with_progress_bar("{$priv_url}/{$filename}", $fetchto) !== true) {
468
			$static_output .= " could not download from there or {$priv_url}/{$filename}.\n";
469
			update_output_window($static_output);
470
			return false;
471
		} else if ($base_url == $priv_url) {
472
			$static_output .= " failed to download.\n";
473
			update_output_window($static_output);
474
			return false;
475
		} else {
476
			$static_output .= " [{$osname} repository]\n";
477
			update_output_window($static_output);
478
		}
479
	}
480
	$static_output .= " (extracting)";
481
	update_output_window($static_output);
482
	$slaveout = "";
483
	exec("/usr/bin/tar --fast-read -O -f {$fetchto} -x +CONTENTS 2>&1", $slaveout);
484
	$raw_depends_list = array_values(preg_grep("/\@pkgdep/", $slaveout));
485
	if ($raw_depends_list != "") {
486
		$pkg_extension = ".tbz";
487
		foreach($raw_depends_list as $adepend) {
488
			$working_depend = explode(" ", trim($adepend, "\n"));
489
			if (substr($working_depend[1], -4) != ".tbz")
490
				$depend_filename = $working_depend[1] . $pkg_extension;
491
			else
492
				$depend_filename = $working_depend[1];
493
			if (!is_freebsd_pkg_installed($working_depend[1])) {
494
				if (pkg_fetch_recursive($working_depend[1], $depend_filename, $dependlevel + 1, $base_url) == false)
495
					return false;
496
			} else {
497
				pkg_debug($working_depend[1] . "\n");
498
			}
499
		}
500
	}
501
	if (($g['platform'] == "nanobsd") || ($g['platform'] == "embedded"))
502
		$pkgtmpdir = "/usr/bin/env PKG_TMPDIR=/root/ ";
503
	$pkgaddout = "";
504
	exec("{$pkgtmpdir}/usr/sbin/pkg_add -fv {$fetchto} 2>&1", $pkgaddout);
505
	pkg_debug($pkgname . " " . print_r($pkgaddout, true) . "\npkg_add successfully completed.\n");
506

    
507
	return true;
508
}
509

    
510
function install_package($package, $pkg_info = "") {
511
	global $g, $config, $static_output, $pkg_interface;
512

    
513
	/* safe side. Write config below will send to ro again. */
514
	conf_mount_rw();
515

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

    
592
function get_after_install_info($package) {
593
	global $pkg_info;
594
	/* fetch package information if needed */
595
	if(!$pkg_info or !is_array($pkg_info[$package])) {
596
		$pkg_info = get_pkg_info(array($package));
597
		$pkg_info = $pkg_info[$package]; // We're only dealing with one package, so we can strip away the extra array.
598
	}
599
	if($pkg_info['after_install_info'])
600
		return $pkg_info['after_install_info'];
601
}
602

    
603
function eval_once($toeval) {
604
	global $evaled;
605
	if(!$evaled) $evaled = array();
606
	$evalmd5 = md5($toeval);
607
	if(!in_array($evalmd5, $evaled)) {
608
		@eval($toeval);
609
		$evaled[] = $evalmd5;
610
	}
611
	return;
612
}
613

    
614
function install_package_xml($pkg) {
615
	global $g, $config, $static_output, $pkg_interface, $config_parsed;
616

    
617
	if(($pkgid = get_pkg_id($pkg)) == -1) {
618
		$static_output .= "The {$pkg} package is not installed.\n\nInstallation aborted.";
619
		update_output_window($static_output);
620
		if($pkg_interface <> "console") {
621
			echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
622
			echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
623
		}
624
		sleep(1);
625
		return false;
626
	} else
627
		$pkg_info = $config['installedpackages']['package'][$pkgid];
628

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

    
692
				if($afn['prefix'] <> "")
693
					$prefix = $afn['prefix'];
694
				else
695
					$prefix = "/usr/local/pkg/";
696

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

    
822
	/* set up package logging streams */
823
	if($pkg_info['logging']) {
824
		mwexec("/usr/sbin/fifolog_create -s 32768 {$g['varlog_path']}/{$pkg_info['logging']['logfilename']}");
825
		@chmod($g['varlog_path'] . '/' . $pkg_info['logging']['logfilename'], 0600);
826
		add_text_to_file("/etc/syslog.conf", $pkg_info['logging']['facilityname'] . "\t\t\t\t" . $pkg_info['logging']['logfilename']);
827
		pkg_debug("Adding text to file /etc/syslog.conf\n");
828
		system_syslogd_start();
829
	}
830

    
831
	return true;
832
}
833

    
834
function does_package_depend($pkg) {
835
	// Should not happen, but just in case.
836
	if(!$pkg)
837
		return;
838
	$pkg_var_db_dir = glob("/var/db/pkg/{$pkg}*");
839
	// If this package has dependency then return true
840
	foreach($pkg_var_db_dir as $pvdd) {
841
		if (file_exists("{$vardb}/{$pvdd}/+REQUIRED_BY") && count(file("{$vardb}/{$pvdd}/+REQUIRED_BY")) > 0) 
842
			return true;
843
	}	
844
	// Did not find a record of dependencies, so return false.
845
	return false;
846
}
847

    
848
function delete_package($pkg) {
849
	global $config, $g, $static_output, $vardb;
850

    
851
	if(!$pkg) 
852
		return;
853

    
854
	$pkg = substr(reverse_strrchr($pkg, "."), 0, -1);
855

    
856
	// If package has dependencies then skip it
857
	if(does_package_depend($pkg)) {
858
		$static_output .= "Skipping package deletion for {$pkg} because it is a dependency.\n";
859
		update_output_window($static_output);
860
		return;		
861
	} else {
862
		if($pkg)
863
			$static_output .= "Starting package deletion for {$pkg}...";
864
		update_output_window($static_output);		
865
	}
866

    
867
	$info = "";
868
	exec("/usr/sbin/pkg_info -qrx {$pkg}", $info);
869
	remove_freebsd_package($pkg);
870
	$static_output .= "done.\n";
871
	update_output_window($static_output);
872
	foreach($info as $line) {
873
		$depend = trim(str_replace("@pkgdep ", "", $line), " \n");
874
		// If package has dependencies then skip it
875
		if(!does_package_depend($depend)) 			
876
			delete_package($depend);
877
	}
878

    
879
	/* Rescan directories for what has been left and avoid fooling other programs. */
880
	mwexec("/sbin/ldconfig");
881

    
882
	return;
883
}
884

    
885
function delete_package_xml($pkg) {
886
	global $g, $config, $static_output, $pkg_interface;
887

    
888
	conf_mount_rw();
889

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

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

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

    
1073
function expand_to_bytes($size) {
1074
	$conv = array(
1075
			"G" =>	"3",
1076
			"M" =>  "2",
1077
			"K" =>  "1",
1078
			"B" =>  "0"
1079
		);
1080
	$suffix = substr($size, -1);
1081
	if(!in_array($suffix, array_keys($conv))) return $size;
1082
	$size = substr($size, 0, -1);
1083
	for($i = 0; $i < $conv[$suffix]; $i++) {
1084
		$size *= 1024;
1085
	}
1086
	return $size;
1087
}
1088

    
1089
function get_pkg_db() {
1090
	global $g;
1091
	return return_dir_as_array($g['vardb_path'] . '/pkg');
1092
}
1093

    
1094
function walk_depend($depend, $pkgdb = "", $alreadyseen = "") {
1095
	if(!$pkgdb)
1096
		$pkgdb = get_pkg_db();
1097
	if(!is_array($alreadyseen))
1098
		$alreadyseen = array();
1099
	if (!is_array($depend))
1100
		$depend = array();
1101
	foreach($depend as $adepend) {
1102
		$pkgname = reverse_strrchr($adepend['name'], '.');
1103
		if(in_array($pkgname, $alreadyseen)) {
1104
			continue;
1105
		} elseif(!in_array($pkgname, $pkgdb)) {
1106
			$size += expand_to_bytes($adepend['size']);
1107
			$alreadyseen[] = $pkgname;
1108
			if(is_array($adepend['depend'])) $size += walk_depend($adepend['depend'], $pkgdb, $alreadyseen);
1109
		}
1110
	}
1111
	return $size;
1112
}
1113

    
1114
function get_package_install_size($pkg = 'all', $pkg_info = "") {
1115
	global $config, $g;
1116
	if((!is_array($pkg)) and ($pkg != 'all'))
1117
		$pkg = array($pkg);
1118
	$pkgdb = get_pkg_db();
1119
	if(!$pkg_info)
1120
		$pkg_info = get_pkg_sizes($pkg);
1121
	foreach($pkg as $apkg) {
1122
		if(!$pkg_info[$apkg])
1123
			continue;
1124
		$toreturn[$apkg] = expand_to_bytes(walk_depend(array($pkg_info[$apkg]), $pkgdb));
1125
	}
1126
	return $toreturn;
1127
}
1128

    
1129
function squash_from_bytes($size, $round = "") {
1130
	$conv = array(1 => "B", "K", "M", "G");
1131
	foreach($conv as $div => $suffix) {
1132
		$sizeorig = $size;
1133
		if(($size /= 1024) < 1) {
1134
			if($round) {
1135
				$sizeorig = round($sizeorig, $round);
1136
			}
1137
			return $sizeorig . $suffix;
1138
		}
1139
	}
1140
	return;
1141
}
1142

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

    
1172
?>
(36-36/61)