Project

General

Profile

Download (45.9 KB) Statistics
| Branch: | Tag: | Revision:
1 7597c8e8 Colin Smith
<?php
2 8c6516d1 Colin Smith
/****h* pfSense/pkg-utils
3
 * NAME
4
 *   pkg-utils.inc - Package subsystem
5
 * DESCRIPTION
6 33b7cc0d Colin Smith
 *   This file contains various functions used by the pfSense package system.
7 8c6516d1 Colin Smith
 * HISTORY
8
 *   $Id$
9
 ******
10
 *
11 84d50fe7 Phil Davis
 * Copyright (C) 2010 Ermal Lu�i
12 0e16b9ca Scott Ullrich
 * Copyright (C) 2005-2006 Colin Smith (ethethlay@gmail.com)
13 8c6516d1 Colin Smith
 * 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 523855b0 Scott Ullrich
37
/*
38 b098343a Ermal
	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 523855b0 Scott Ullrich
	pfSense_MODULE:	pkg
41
*/
42
43 01a6e665 Ermal
require_once("globals.inc");
44 33b7cc0d Colin Smith
require_once("xmlrpc.inc");
45 7eea4407 Ermal
require_once("service-utils.inc");
46 093bcebc Scott Ullrich
if(file_exists("/cf/conf/use_xmlreader"))
47
	require_once("xmlreader.inc");
48
else
49
	require_once("xmlparse.inc");
50 3c41c4ab Colin Smith
require_once("service-utils.inc");
51 7597c8e8 Colin Smith
require_once("pfsense-utils.inc");
52 33b7cc0d Colin Smith
53 f041d58b Scott Ullrich
if(!function_exists("update_status")) {
54
	function update_status($status) {
55
		echo $status . "\n";
56 b47833cc Scott Ullrich
	}
57
}
58 762cb660 Scott Ullrich
if(!function_exists("update_output_window")) {
59
	function update_output_window($status) {
60
		echo $status . "\n";
61
	}
62
}
63 b47833cc Scott Ullrich
64 eab543ed Ermal
if (!function_exists("pkg_debug")) {
65
	/* set up logging if needed */
66
	function pkg_debug($msg) {
67
		global $g, $debug, $fd_log;
68
69
		if (!$debug)
70
			return;
71
72
		if (!$fd_log) {
73
			if (!$fd_log = fopen("{$g['tmp_path']}/pkg_mgr_{$package}.log", "w"))
74
				update_output_window("Warning, could not open log for writing.");
75
		}
76
		@fwrite($fd_log, $msg);
77
	}
78
}
79
80 1b28121f Ermal
$vardb = "/var/db/pkg";
81
safe_mkdir($vardb);
82 eab543ed Ermal
$g['platform'] = trim(file_get_contents("/etc/platform"));
83 7955cde8 Scott Ullrich
84 43ad432c Ermal Lu?i
conf_mount_rw();
85
if(!is_dir("/usr/local/pkg") or !is_dir("/usr/local/pkg/pf")) {
86 d3c02149 Scott Ullrich
	safe_mkdir("/usr/local/pkg");
87 7955cde8 Scott Ullrich
	safe_mkdir("/usr/local/pkg/pf");	
88 e7405fbf Scott Ullrich
}
89 3339fac0 Ermal Lu?i
conf_mount_ro();
90 33b7cc0d Colin Smith
91 31e7e1bc Scott Ullrich
/****f* pkg-utils/remove_package
92
 * NAME
93
 *   remove_package - Removes package from FreeBSD if it exists
94
 * INPUTS
95
 *   $packagestring	- name/string to check for
96
 * RESULT
97
 *   none
98
 * NOTES
99
 *   
100
 ******/
101
function remove_freebsd_package($packagestring) {
102 8059acb5 Phil Davis
	// The packagestring passed in must be the full PBI package name, 
103
	// as displayed by the pbi_info utility. e.g. "package-1.2.3_4-i386" 
104
	// It must NOT have ".pbi" on the end.
105 53107064 Vinicius Coque
	exec("/usr/local/sbin/pbi_info {$packagestring} | /usr/bin/awk '/Prefix/ {print $2}'",$pbidir);
106
	$pbidir = $pbidir[0];
107 8059acb5 Phil Davis
	if ($pbidir == "") {
108
		log_error("PBI dir for {$packagestring} was not found - cannot cleanup PBI files");
109
	}
110
	else {
111
		$linkdirs = array('bin','sbin');
112
		foreach($linkdirs as $dir) {
113 a1d4a048 Phil Davis
			$target_dir = $pbidir . "/" . $dir;
114
			if(is_dir($target_dir)) {
115
				$files = scandir($target_dir);
116 8059acb5 Phil Davis
				foreach($files as $f) {
117
					if($f != '.' && $f != '..') {
118 a1d4a048 Phil Davis
						// Only try to unlink the file if it is a link to the expected pbi dir.
119
						$local_name = "/usr/local/{$dir}/{$f}";
120
						if(is_link($local_name)) {
121
							if(substr(readlink($local_name),0,strlen($target_dir)) == $target_dir) {
122
								unlink($local_name);
123
							}
124
						}
125 8059acb5 Phil Davis
					}
126 53107064 Vinicius Coque
				}
127
			}
128
		}
129
130 8059acb5 Phil Davis
		exec("/usr/local/sbin/pbi_delete {$packagestring} 2>>/tmp/pbi_delete_errors.txt");
131
	}
132 31e7e1bc Scott Ullrich
}
133
134 33b7cc0d Colin Smith
/****f* pkg-utils/is_package_installed
135
 * NAME
136
 *   is_package_installed - Check whether a package is installed.
137
 * INPUTS
138
 *   $packagename	- name of the package to check
139
 * RESULT
140
 *   boolean	- true if the package is installed, false otherwise
141
 * NOTES
142
 *   This function is deprecated - get_pkg_id() can already check for installation.
143
 ******/
144 8c6516d1 Colin Smith
function is_package_installed($packagename) {
145 33b7cc0d Colin Smith
	$pkg = get_pkg_id($packagename);
146 b2a66231 Ermal
	if($pkg == -1)
147
		return false;
148 33b7cc0d Colin Smith
	return true;
149 8c6516d1 Colin Smith
}
150 43db85f8 Scott Ullrich
151 33b7cc0d Colin Smith
/****f* pkg-utils/get_pkg_id
152
 * NAME
153
 *   get_pkg_id - Find a package's numeric ID.
154
 * INPUTS
155
 *   $pkg_name	- name of the package to check
156
 * RESULT
157
 *   integer    - -1 if package is not found, >-1 otherwise
158
 ******/
159 8c6516d1 Colin Smith
function get_pkg_id($pkg_name) {
160 e65a287f Scott Ullrich
	global $config;
161
162 2addd5b2 Ermal
	if (is_array($config['installedpackages']['package'])) {
163
		foreach($config['installedpackages']['package'] as $idx => $pkg) {
164 b2a66231 Ermal
			if($pkg['name'] == $pkg_name)
165 2addd5b2 Ermal
				return $idx;
166 e65a287f Scott Ullrich
		}
167
	}
168
	return -1;
169 8c6516d1 Colin Smith
}
170
171 33b7cc0d Colin Smith
/****f* pkg-utils/get_pkg_info
172
 * NAME
173 5b542ae5 Bill Marquette
 *   get_pkg_info - Retrieve package information from pfsense.com.
174 33b7cc0d Colin Smith
 * INPUTS
175 5b542ae5 Bill Marquette
 *   $pkgs - 'all' to retrieve all packages, an array containing package names otherwise
176
 *   $info - 'all' to retrieve all information, an array containing keys otherwise
177 33b7cc0d Colin Smith
 * RESULT
178
 *   $raw_versions - Array containing retrieved information, indexed by package name.
179
 ******/
180
function get_pkg_info($pkgs = 'all', $info = 'all') {
181 7597c8e8 Colin Smith
	global $g;
182 b2a66231 Ermal
183 2addd5b2 Ermal
	$freebsd_version = php_uname("r");
184
	$freebsd_machine = php_uname("m");
185 340c0677 Scott Ullrich
	$params = array(
186
		"pkg" => $pkgs, 
187
		"info" => $info, 
188 d465a277 Ermal
		"freebsd_version" => $freebsd_version[0],
189 e4c3d767 sullrich
		"freebsd_machine" => $freebsd_machine
190
	);
191 e65a287f Scott Ullrich
	$resp = call_pfsense_method('pfsense.get_pkgs', $params, 10);
192
	return $resp ? $resp : array();
193
}
194
195
function get_pkg_sizes($pkgs = 'all') {
196 2addd5b2 Ermal
	global $config, $g;
197 b2a66231 Ermal
198 2addd5b2 Ermal
	$freebsd_version = php_uname("r");
199
	$freebsd_machine = php_uname("m");
200
	$params = array(
201
		"pkg" => $pkgs, 
202
		"freebsd_version" => $freebsd_version,
203
		"freebsd_machine" => $freebsd_machine
204
	);
205 e65a287f Scott Ullrich
	$msg = new XML_RPC_Message('pfsense.get_pkg_sizes', array(php_value_to_xmlrpc($params)));
206 ffba4976 jim-p
	$xmlrpc_base_url = isset($config['system']['altpkgrepo']['enable']) ? $config['system']['altpkgrepo']['xmlrpcbaseurl'] : $g['xmlrpcbaseurl'];
207
	$cli = new XML_RPC_Client($g['xmlrpcpath'], $xmlrpc_base_url);
208 43db85f8 Scott Ullrich
	$resp = $cli->send($msg, 10);
209 2addd5b2 Ermal
	if(!is_object($resp))
210
		log_error("Could not get response from XMLRPC server!");
211
 	else if (!$resp->faultCode()) {
212 e65a287f Scott Ullrich
		$raw_versions = $resp->value();
213 34da63c3 Colin Smith
		return xmlrpc_value_to_php($raw_versions);
214
	}
215 b2a66231 Ermal
216
	return array();
217 8c6516d1 Colin Smith
}
218
219
/*
220
 * resync_all_package_configs() Force packages to setup their configuration and rc.d files.
221
 * This function may also print output to the terminal indicating progress.
222
 */
223
function resync_all_package_configs($show_message = false) {
224 b1224cdc jim-p
	global $config, $pkg_interface, $g;
225 b2a66231 Ermal
226 6acdf659 Carlos Eduardo Ramos
	log_error(gettext("Resyncing configuration for all packages."));
227 06e57df8 Scott Ullrich
228 2addd5b2 Ermal
	if (!is_array($config['installedpackages']['package']))
229 3a9eb3c9 Ermal
		return;
230 06e57df8 Scott Ullrich
231 3a9eb3c9 Ermal
	if($show_message == true)
232
		echo "Syncing packages:";
233 b2a66231 Ermal
234 78b94214 Ermal
	conf_mount_rw();
235 06e57df8 Scott Ullrich
236 2addd5b2 Ermal
	foreach($config['installedpackages']['package'] as $idx => $package) {
237
		if (empty($package['name']))
238
			continue;
239
		if($show_message == true)
240
			echo " " . $package['name'];
241
		get_pkg_depends($package['name'], "all");
242 b1224cdc jim-p
		if($g['booting'] != true)
243 06e57df8 Scott Ullrich
			stop_service($package['name']);
244 2addd5b2 Ermal
		sync_package($idx, true, true);
245
		if($pkg_interface == "console") 
246 c92ccac7 Vinicius Coque
			echo "\n" . gettext("Syncing packages:");
247 e65a287f Scott Ullrich
	}
248 06e57df8 Scott Ullrich
249 b2a66231 Ermal
	if($show_message == true)
250 08452bff Warren Baker
		echo " done.\n";
251 06e57df8 Scott Ullrich
252 3a9eb3c9 Ermal
	@unlink("/conf/needs_package_sync");
253 78b94214 Ermal
	conf_mount_ro();
254 8c6516d1 Colin Smith
}
255
256 7597c8e8 Colin Smith
/*
257
 * is_freebsd_pkg_installed() - Check /var/db/pkg to determine whether or not a FreeBSD
258
 *				package is installed.
259
 */
260
function is_freebsd_pkg_installed($pkg) {
261 86af45ec Scott Ullrich
	if(!$pkg) 
262
		return;
263 fcf92dae Ermal
	$output = "";
264 a5566aa8 Vinicius Coque
	exec("/usr/local/sbin/pbi_info \"{$pkg}\"", $output, $retval);
265 b2a66231 Ermal
266 fcf92dae Ermal
	return (intval($retval) == 0);
267 7597c8e8 Colin Smith
}
268
269 8c6516d1 Colin Smith
/*
270
 * get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", return_nosync = 1):  Return a package's dependencies.
271
 *
272
 * $filetype = "all" || ".xml", ".tgz", etc.
273
 * $format = "files" (full filenames) || "names" (stripped / parsed depend names)
274
 * $return_nosync = 1 (return depends that have nosync set) | 0 (ignore packages with nosync)
275
 *
276
 */
277
function get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", $return_nosync = 1) {
278 e65a287f Scott Ullrich
	global $config;
279 b2a66231 Ermal
280 e65a287f Scott Ullrich
	$pkg_id = get_pkg_id($pkg_name);
281 b2a66231 Ermal
	if($pkg_id == -1)
282
		return -1; // This package doesn't really exist - exit the function.
283
	else if (!isset($config['installedpackages']['package'][$pkg_id]))
284
		return; // No package belongs to the pkg_id passed to this function.
285
286
	$package =& $config['installedpackages']['package'][$pkg_id];
287 e65a287f Scott Ullrich
	if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
288 addc0439 Renato Botelho
		log_error(sprintf(gettext('The %1$s package is missing required dependencies and must be reinstalled. %2$s'), $package['name'], $package['configurationfile']));
289 2c794549 Ermal
		uninstall_package($package['name']);
290 2addd5b2 Ermal
		if (install_package($package['name']) < 0) {
291
			log_error("Failed reinstalling package {$package['name']}.");
292 2c794549 Ermal
			return false;
293 2addd5b2 Ermal
		}
294 093441f0 Colin Smith
	}
295 19a11678 Colin Smith
	$pkg_xml = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
296 b2a66231 Ermal
	if (!empty($pkg_xml['additional_files_needed'])) {
297 e65a287f Scott Ullrich
		foreach($pkg_xml['additional_files_needed'] as $item) {
298 b2a66231 Ermal
			if ($return_nosync == 0 && isset($item['nosync']))
299
				continue; // Do not return depends with nosync set if not required.
300 e65a287f Scott Ullrich
			$depend_file = substr(strrchr($item['item']['0'], '/'),1); // Strip URLs down to filenames.
301
			$depend_name = substr(substr($depend_file,0,strpos($depend_file,".")+1),0,-1); // Strip filename down to dependency name.
302 b2a66231 Ermal
			if (($filetype != "all") && (!preg_match("/{$filetype}/i", $depend_file)))
303
					continue;
304 2c794549 Ermal
			if ($item['prefix'] != "")
305 e65a287f Scott Ullrich
				$prefix = $item['prefix'];
306 2c794549 Ermal
			else
307 e65a287f Scott Ullrich
				$prefix = "/usr/local/pkg/";
308 017d381c Scott Ullrich
			// Ensure that the prefix exists to avoid installation errors.
309
			if(!is_dir($prefix)) 
310 b2a66231 Ermal
				exec("/bin/mkdir -p {$prefix}");
311 3e155fab Scott Ullrich
			if(!file_exists($prefix . $depend_file))
312 6acdf659 Carlos Eduardo Ramos
				log_error(sprintf(gettext("The %s package is missing required dependencies and must be reinstalled."), $package['name']));
313 e65a287f Scott Ullrich
			switch ($format) {
314 b2a66231 Ermal
			case "files":
315
				$depends[] = $prefix . $depend_file;
316
				break;
317
			case "names":
318
				switch ($filetype) {
319
				case "all":
320
					if(preg_match("/\.xml/i", $depend_file)) {
321
						$depend_xml = parse_xml_config_pkg("/usr/local/pkg/{$depend_file}", "packagegui");
322
						if (!empty($depend_xml))
323 017d381c Scott Ullrich
							$depends[] = $depend_xml['name'];
324 b2a66231 Ermal
					} else
325
						$depends[] = $depend_name; // If this dependency isn't package XML, use the stripped filename.
326
					break;
327
				case ".xml":
328
					$depend_xml = parse_xml_config_pkg("/usr/local/pkg/" . $depend_file, "packagegui");
329
					if (!empty($depend_xml))
330
						$depends[] = $depend_xml['name'];
331
					break;
332
				default:
333
					$depends[] = $depend_name; // If we aren't looking for XML, use the stripped filename (it's all we have).
334
					break;
335
				}
336 e65a287f Scott Ullrich
			}
337 b2a66231 Ermal
		}
338 e65a287f Scott Ullrich
		return $depends;
339
	}
340 8c6516d1 Colin Smith
}
341
342 2c794549 Ermal
function uninstall_package($pkg_name) {
343 fcf92dae Ermal
	global $config, $static_output;
344 f0695975 Scott Ullrich
	global $builder_package_install;
345 b2a66231 Ermal
346 09e11b69 Scott Ullrich
	// Back up /usr/local/lib libraries first if
347
	// not running from the builder code.
348 d2a08a06 Scott Ullrich
	// also take into account rrd binaries
349 f0695975 Scott Ullrich
	if(!$builder_package_install) {
350
		if(!file_exists("/tmp/pkg_libs.tgz")) {
351
			$static_output .= "Backing up libraries... ";
352
			update_output_window($static_output);
353
			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`");
354 cb6630e5 Scott Ullrich
			exec("/usr/bin/tar czPf /tmp/pkg_bins.tgz `/bin/cat /etc/pfSense_md5.txt | /usr/bin/grep 'rrd' | /usr/bin/awk '{ print $2 }' | /usr/bin/cut -d'(' -f2 | /usr/bin/cut -d')' -f1`");
355 f0695975 Scott Ullrich
			$static_output .= "\n";
356
		}
357 4c6a49d7 Scott Ullrich
	}
358 b2a66231 Ermal
359 7eea4407 Ermal
	stop_service($pkg_name);
360
361 f898cf33 Scott Ullrich
	$id = get_pkg_id($pkg_name);
362 df5da531 Ermal
	if ($id >= 0) {
363 a5566aa8 Vinicius Coque
		$pkg_depends =& $config['installedpackages']['package'][$id]['depends_on_package_pbi'];
364 fcf92dae Ermal
		$static_output .= "Removing package...\n";
365
		update_output_window($static_output);
366 b7729cee Ermal
		if (is_array($pkg_depends)) {
367
			foreach ($pkg_depends as $pkg_depend)
368 fcf92dae Ermal
				delete_package($pkg_depend);
369 569aeae7 Vinicius Coque
		} else {
370 8059acb5 Phil Davis
			// The packages (1 or more) are all in one long string.
371
			// We need to pass them 1 at a time to delete_package.
372
			// Compress any multiple whitespace (sp, tab, cr, lf...) into a single space char.
373
			$pkg_dep_str = preg_replace("'\s+'", ' ', $pkg_depends);
374
			// Get rid of any leading or trailing space.
375
			$pkg_dep_str = trim($pkg_dep_str);
376
			// Now we have a space-separated string. Make it into an array and process it.
377
			$pkg_dep_array = explode(" ", $pkg_dep_str);
378
			foreach ($pkg_dep_array as $pkg_depend) {
379
				delete_package($pkg_depend);
380
			}
381 b7729cee Ermal
		}
382 1570d27a Ermal Lu?i
	}
383 f898cf33 Scott Ullrich
	delete_package_xml($pkg_name);
384 4c6a49d7 Scott Ullrich
385 09e11b69 Scott Ullrich
	// Restore libraries that we backed up if not 
386
	// running from the builder code.
387 f0695975 Scott Ullrich
	if(!$builder_package_install) {
388
		$static_output .= "Cleaning up... ";
389
		update_output_window($static_output);
390
		exec("/usr/bin/tar xzPfU /tmp/pkg_libs.tgz -C /");
391 efea7969 Scott Ullrich
		exec("/usr/bin/tar xzPfU /tmp/pkg_bins.tgz -C /");
392
		@unlink("/tmp/pkg_libs.tgz");
393
		@unlink("/tmp/pkg_bins.tgz");
394 f0695975 Scott Ullrich
	}
395 f898cf33 Scott Ullrich
}
396
397 7bbfe007 Scott Ullrich
function force_remove_package($pkg_name) {
398
	delete_package_xml($pkg_name);
399
}
400
401 8c6516d1 Colin Smith
/*
402
 * sync_package($pkg_name, $sync_depends = true, $show_message = false) Force a package to setup its configuration and rc.d files.
403
 */
404
function sync_package($pkg_name, $sync_depends = true, $show_message = false) {
405 c132bdb0 Ermal
	global $config, $config_parsed;
406 f0695975 Scott Ullrich
	global $builder_package_install;
407
	
408 09e11b69 Scott Ullrich
	// If this code is being called by pfspkg_installer 
409
	// which the builder system uses then return (ignore).
410 f0695975 Scott Ullrich
	if($builder_package_install)
411
		return;
412 b2a66231 Ermal
	
413
	if(empty($config['installedpackages']['package']))
414
		return;
415 669e1adb Bill Marquette
	if(!is_numeric($pkg_name)) {
416
		$pkg_id = get_pkg_id($pkg_name);
417 b2a66231 Ermal
		if($pkg_id == -1)
418
			return -1; // This package doesn't really exist - exit the function.
419 669e1adb Bill Marquette
	} else {
420
		$pkg_id = $pkg_name;
421 b2a66231 Ermal
		if(empty($config['installedpackages']['package'][$pkg_id]))
422
			return;  // No package belongs to the pkg_id passed to this function.
423 669e1adb Bill Marquette
	}
424 04d10bbc Scott Ullrich
        if (is_array($config['installedpackages']['package'][$pkg_id]))
425 b2a66231 Ermal
		$package =& $config['installedpackages']['package'][$pkg_id];
426 04d10bbc Scott Ullrich
        else
427 b2a66231 Ermal
		return; /* empty package tag */
428 669e1adb Bill Marquette
	if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
429 6acdf659 Carlos Eduardo Ramos
		log_error(sprintf(gettext("The %s package is missing its configuration file and must be reinstalled."), $package['name']));
430 7bbfe007 Scott Ullrich
		force_remove_package($package['name']);
431 2c794549 Ermal
		return -1;
432
	}
433
	$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
434 2addd5b2 Ermal
	if(isset($pkg_config['nosync']))
435
		return;
436 2c794549 Ermal
	/* Bring in package include files */
437
	if (!empty($pkg_config['include_file'])) {
438
		$include_file = $pkg_config['include_file'];
439
		if (file_exists($include_file))
440
			require_once($include_file);
441
		else {
442
			/* XXX: What the heck is this?! */
443
			log_error("Reinstalling package {$package['name']} because its include file({$include_file}) is missing!");
444
			uninstall_package($package['name']);
445
			if (install_package($package['name']) < 0) {
446
				log_error("Reinstalling package {$package['name']} failed. Take appropriate measures!!!");
447
				return -1;
448 83cfae8d Ermal Lu?i
			}
449 30e4c34a Scott Ullrich
		}
450 2c794549 Ermal
	}
451 30e4c34a Scott Ullrich
452 2c794549 Ermal
	if(!empty($pkg_config['custom_php_global_functions']))
453
		eval($pkg_config['custom_php_global_functions']);
454
	if(!empty($pkg_config['custom_php_resync_config_command']))
455
		eval($pkg_config['custom_php_resync_config_command']);
456
	if($sync_depends == true) {
457
		$depends = get_pkg_depends($pkg_name, ".xml", "files", 1); // Call dependency handler and do a little more error checking.
458
		if(is_array($depends)) {
459
			foreach($depends as $item) {
460
				if(!file_exists($item)) {
461 2addd5b2 Ermal
					require_once("notices.inc");
462 b96f6496 Renato Botelho
					file_notice($package['name'], sprintf(gettext("The %s package is missing required dependencies and must be reinstalled."), $package['name']), "Packages", "/pkg_mgr_install.php?mode=reinstallpkg&pkg={$package['name']}", 1);
463 2c794549 Ermal
					log_error("Could not find {$item}. Reinstalling package.");
464
					uninstall_package($pkg_name);
465 2addd5b2 Ermal
					if (install_package($pkg_name) < 0) {
466
						log_error("Reinstalling package {$package['name']} failed. Take appropriate measures!!!");
467
						return -1;
468
					}
469 2c794549 Ermal
				} else {
470
					$item_config = parse_xml_config_pkg($item, "packagegui");
471
					if (empty($item_config))
472
						continue;
473
					if(isset($item_config['nosync']))
474
						continue;
475 2addd5b2 Ermal
					if (!empty($item_config['include_file'])) {
476
						if (file_exists($item_config['include_file']))	
477
							require_once($item_config['include_file']);
478
						else {
479
							log_error("Not calling package sync code for dependency {$item_config['name']} of {$package['name']} because some include files are missing.");
480
							continue;
481
						}
482
					}
483
					if($item_config['custom_php_global_functions'] <> "")
484
						eval($item_config['custom_php_global_functions']);
485 2c794549 Ermal
					if($item_config['custom_php_resync_config_command'] <> "")
486
						eval($item_config['custom_php_resync_config_command']);
487
					if($show_message == true)
488
						print " " . $item_config['name'];
489 669e1adb Bill Marquette
				}
490
			}
491
		}
492
	}
493 8c6516d1 Colin Smith
}
494
495 7597c8e8 Colin Smith
/*
496 43dad535 Vinicius Coque
 * pkg_fetch_recursive: Download and install a FreeBSD PBI package. This function provides output to
497 7597c8e8 Colin Smith
 * 			a progress bar and output window.
498
 */
499 0cdf4e87 Ermal
function pkg_fetch_recursive($pkgname, $filename, $dependlevel = 0, $base_url = "") {
500 2addd5b2 Ermal
	global $static_output, $g;
501 b2a66231 Ermal
502 19f576fd Scott Ullrich
	// Clean up incoming filenames
503
	$filename = str_replace("  ", " ", $filename);
504
	$filename = str_replace("\n", " ", $filename);
505
	$filename = str_replace("  ", " ", $filename);
506
507 cfbfd941 smos
	$pkgs = explode(" ", $filename);
508 764bd0ac Scott Ullrich
	foreach($pkgs as $filename) {
509 21762198 jim-p
		$filename = trim($filename);
510 05ff388a Scott Ullrich
		if (($g['platform'] == "nanobsd") || ($g['platform'] == "embedded")) {
511
			$pkgtmpdir = "/usr/bin/env PKG_TMPDIR=/root/ ";
512
			$pkgstagingdir = "/root/tmp";
513
			if (!is_dir($pkgstagingdir))
514
				mkdir($pkgstagingdir);
515
			$pkgstaging = "-o {$pkgstagingdir}/instmp.XXXXXX";
516
			$fetchdir = $pkgstagingdir;
517 aeaa7358 Ermal
		} else {
518 05ff388a Scott Ullrich
			$fetchdir = $g['tmp_path'];
519 aeaa7358 Ermal
		}
520 22beab88 jim-p
521 05ff388a Scott Ullrich
		$osname = php_uname("s");
522
		$arch =  php_uname("m");
523
		$rel = strtolower(php_uname("r"));
524
		if (substr_count($rel, '-') > 1)
525
			$rel = substr($rel, 0, strrpos($rel, "-"));
526
		$priv_url = "http://ftp2.{$osname}.org/pub/{$osname}/ports/{$arch}/packages-{$rel}/All";
527
		if (empty($base_url))
528
			$base_url = $priv_url;
529
		if (substr($base_url, -1) == "/")
530
			$base_url = substr($base_url, 0, -1);
531
		$fetchto = "{$fetchdir}/apkg_{$filename}";
532
		$static_output .= "\n" . str_repeat(" ", $dependlevel * 2 + 1) . "Downloading {$base_url}/{$filename} ... ";
533
		if (download_file_with_progress_bar("{$base_url}/{$filename}", $fetchto) !== true) {
534
			if ($base_url != $priv_url && download_file_with_progress_bar("{$priv_url}/{$filename}", $fetchto) !== true) {
535
				$static_output .= " could not download from there or {$priv_url}/{$filename}.\n";
536
				update_output_window($static_output);
537
				return false;
538
			} else if ($base_url == $priv_url) {
539
				$static_output .= " failed to download.\n";
540
				update_output_window($static_output);
541
				return false;
542
			} else {
543
				$static_output .= " [{$osname} repository]\n";
544
				update_output_window($static_output);
545
			}
546
		}
547
		$static_output .= " (extracting)\n";
548
		update_output_window($static_output);
549 43dad535 Vinicius Coque
550 05ff388a Scott Ullrich
		$pkgaddout = "";
551 b2a66231 Ermal
552 05ff388a Scott Ullrich
		exec("/usr/local/sbin/pbi_add {$pkgstaging} -f -v --no-checksig {$fetchto} 2>&1", $pkgaddout);
553
		pkg_debug($pkgname . " " . print_r($pkgaddout, true) . "\npbi_add successfully completed.\n");
554
555
		exec("/usr/local/sbin/pbi_info " . preg_replace('/\.pbi$/','',$filename) . " | /usr/bin/awk '/Prefix/ {print $2}'",$pbidir);
556
		$pbidir = $pbidir[0];
557
		$linkdirs = array('bin','sbin');
558
		foreach($linkdirs as $dir) {
559
			if(is_dir("{$pbidir}/{$dir}")) {
560
				$files = scandir("{$pbidir}/{$dir}");
561
				foreach($files as $f) {
562 44d55df6 Scott Ullrich
					if(!file_exists("/usr/local/{$dir}/{$f}")) {
563 a1d4a048 Phil Davis
						symlink("{$pbidir}/{$dir}/{$f}","/usr/local/{$dir}/{$f}");
564 44d55df6 Scott Ullrich
					}
565 05ff388a Scott Ullrich
				}
566 43dad535 Vinicius Coque
			}
567
		}
568
	}
569 e65a287f Scott Ullrich
	return true;
570 8c6516d1 Colin Smith
}
571
572 7597c8e8 Colin Smith
function install_package($package, $pkg_info = "") {
573 2addd5b2 Ermal
	global $g, $config, $static_output, $pkg_interface;
574 b2a66231 Ermal
575 43ad432c Ermal Lu?i
	/* safe side. Write config below will send to ro again. */
576
	conf_mount_rw();
577
578 dbef849d Scott Ullrich
	if($pkg_interface == "console") 	
579
		echo "\n";
580 7597c8e8 Colin Smith
	/* fetch package information if needed */
581 b2a66231 Ermal
	if(empty($pkg_info) or !is_array($pkg_info[$package])) {
582 7597c8e8 Colin Smith
		$pkg_info = get_pkg_info(array($package));
583
		$pkg_info = $pkg_info[$package]; // We're only dealing with one package, so we can strip away the extra array.
584 1b28121f Ermal
		if (empty($pkg_info)) {
585
			conf_mount_ro();
586
			return -1;
587
		}
588 7597c8e8 Colin Smith
	}
589 c92ccac7 Vinicius Coque
	pkg_debug(gettext("Beginning package installation.") . "\n");
590
	log_error(sprintf(gettext('Beginning package installation for %s .'), $pkg_info['name']));
591
	$static_output .= sprintf(gettext("Beginning package installation for %s ."), $pkg_info['name']);
592 1b28121f Ermal
	update_status($static_output);
593 7597c8e8 Colin Smith
	/* fetch the package's configuration file */
594
	if($pkg_info['config_file'] != "") {
595 0b07c763 Chris Buechler
		$static_output .= "\n" . gettext("Downloading package configuration file... ");
596 7597c8e8 Colin Smith
		update_output_window($static_output);
597 c92ccac7 Vinicius Coque
		pkg_debug(gettext("Downloading package configuration file...") . "\n");
598 7597c8e8 Colin Smith
		$fetchto = substr(strrchr($pkg_info['config_file'], '/'), 1);
599
		download_file_with_progress_bar($pkg_info['config_file'], '/usr/local/pkg/' . $fetchto);
600
		if(!file_exists('/usr/local/pkg/' . $fetchto)) {
601 84bc8eb7 jim-p
			pkg_debug(gettext("ERROR! Unable to fetch package configuration file. Aborting installation.") . "\n");
602 fcf92dae Ermal
			if($pkg_interface == "console")
603 3ec86ca8 Renato Botelho
				print "\n" . gettext("ERROR! Unable to fetch package configuration file. Aborting package installation.") . "\n";
604 fcf92dae Ermal
			else {
605 9d3d8d00 Vinicius Coque
				$static_output .= gettext("failed!\n\nInstallation aborted.\n");
606 7597c8e8 Colin Smith
				update_output_window($static_output);
607
				echo "<br>Show <a href=\"pkg_mgr_install.php?showlog=true\">install log</a></center>";
608
			}
609 fcf92dae Ermal
			conf_mount_ro();
610
			return -1;
611 7597c8e8 Colin Smith
		}
612 6acdf659 Carlos Eduardo Ramos
		$static_output .= gettext("done.") . "\n";
613 7597c8e8 Colin Smith
		update_output_window($static_output);
614
	}
615
	/* add package information to config.xml */
616
	$pkgid = get_pkg_id($pkg_info['name']);
617 6acdf659 Carlos Eduardo Ramos
	$static_output .= gettext("Saving updated package information...") . " ";
618 7597c8e8 Colin Smith
	update_output_window($static_output);
619
	if($pkgid == -1) {
620
		$config['installedpackages']['package'][] = $pkg_info;
621 b1e4005f Vinicius Coque
		$changedesc = sprintf(gettext("Installed %s package."),$pkg_info['name']);
622 6acdf659 Carlos Eduardo Ramos
		$to_output = gettext("done.") . "\n";
623 7597c8e8 Colin Smith
	} else {
624
		$config['installedpackages']['package'][$pkgid] = $pkg_info;
625 6acdf659 Carlos Eduardo Ramos
		$changedesc = sprintf(gettext("Overwrote previous installation of %s."), $pkg_info['name']);
626
		$to_output = gettext("overwrite!") . "\n";
627 7597c8e8 Colin Smith
	}
628 6622e126 Scott Ullrich
	if(file_exists('/conf/needs_package_sync'))
629
		@unlink('/conf/needs_package_sync');
630 3339fac0 Ermal Lu?i
	conf_mount_ro();
631 f9c8e64c jim-p
	write_config("Intermediate config write during package install for {$pkg_info['name']}.");
632 7597c8e8 Colin Smith
	$static_output .= $to_output;
633
	update_output_window($static_output);
634
	/* install other package components */
635 2c794549 Ermal
	if (!install_package_xml($package)) {
636
		uninstall_package($package);
637
		write_config($changedesc);
638 b96f6496 Renato Botelho
		$static_output .= gettext("Failed to install package.") . "\n";
639 2c794549 Ermal
		update_output_window($static_output);
640
		return -1;
641
	} else {
642 b96f6496 Renato Botelho
		$static_output .= gettext("Writing configuration... ");
643 2c794549 Ermal
		update_output_window($static_output);
644
		write_config($changedesc);
645 b96f6496 Renato Botelho
		$static_output .= gettext("done.") . "\n";
646 2c794549 Ermal
		update_output_window($static_output);
647
		if($pkg_info['after_install_info']) 
648
			update_output_window($pkg_info['after_install_info']);	
649
	}
650 7597c8e8 Colin Smith
}
651
652 cfde64b8 Scott Ullrich
function get_after_install_info($package) {
653
	global $pkg_info;
654
	/* fetch package information if needed */
655
	if(!$pkg_info or !is_array($pkg_info[$package])) {
656
		$pkg_info = get_pkg_info(array($package));
657
		$pkg_info = $pkg_info[$package]; // We're only dealing with one package, so we can strip away the extra array.
658
	}
659
	if($pkg_info['after_install_info'])
660
		return $pkg_info['after_install_info'];
661
}
662
663 2a0e6517 Colin Smith
function eval_once($toeval) {
664
	global $evaled;
665 57965588 Colin Smith
	if(!$evaled) $evaled = array();
666 2a0e6517 Colin Smith
	$evalmd5 = md5($toeval);
667
	if(!in_array($evalmd5, $evaled)) {
668 8604523b Ermal Lu?i
		@eval($toeval);
669 2a0e6517 Colin Smith
		$evaled[] = $evalmd5;
670
	}
671
	return;
672
}
673
674 7597c8e8 Colin Smith
function install_package_xml($pkg) {
675 c132bdb0 Ermal
	global $g, $config, $static_output, $pkg_interface, $config_parsed;
676 b2a66231 Ermal
677 7597c8e8 Colin Smith
	if(($pkgid = get_pkg_id($pkg)) == -1) {
678 6acdf659 Carlos Eduardo Ramos
		$static_output .= sprintf(gettext("The %s package is not installed.%sInstallation aborted."), $pkg, "\n\n");
679 7597c8e8 Colin Smith
		update_output_window($static_output);
680 1a22ffcd Scott Ullrich
		if($pkg_interface <> "console") {
681
			echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
682
			echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
683
		}
684 7597c8e8 Colin Smith
		sleep(1);
685 2c794549 Ermal
		return false;
686 b2a66231 Ermal
	} else
687 7597c8e8 Colin Smith
		$pkg_info = $config['installedpackages']['package'][$pkgid];
688 b2a66231 Ermal
689 a6d0d461 Colin Smith
	/* pkg_add the package and its dependencies */
690 e65a287f Scott Ullrich
	if($pkg_info['depends_on_package_base_url'] != "") {
691 dbef849d Scott Ullrich
		if($pkg_interface == "console") 
692
			echo "\n";
693 6acdf659 Carlos Eduardo Ramos
		update_status(gettext("Installing") . " " . $pkg_info['name'] . " " . gettext("and its dependencies."));
694
		$static_output .= gettext("Downloading") . " " . $pkg_info['name'] . " " . gettext("and its dependencies... ");
695 e65a287f Scott Ullrich
		$static_orig = $static_output;
696
		$static_output .= "\n";
697
		update_output_window($static_output);
698 a5566aa8 Vinicius Coque
		foreach((array) $pkg_info['depends_on_package_pbi'] as $pkgdep) {
699 e65a287f Scott Ullrich
			$pkg_name = substr(reverse_strrchr($pkgdep, "."), 0, -1);
700 b3a4ff7c Scott Ullrich
			$static_output = $static_orig . "\nChecking for package installation... ";
701 1b28121f Ermal
			update_output_window($static_output);
702 ad88ff3f Ermal
			if (!is_freebsd_pkg_installed($pkg_name)) {
703 a0b205f0 Ermal
				if (!pkg_fetch_recursive($pkg_name, $pkgdep, 0, $pkg_info['depends_on_package_base_url'])) {
704 ad88ff3f Ermal
					$static_output .= "of {$pkg_name} failed!\n\nInstallation aborted.";
705
					update_output_window($static_output);
706 c92ccac7 Vinicius Coque
					pkg_debug(gettext("Package WAS NOT installed properly.") . "\n");
707 ad88ff3f Ermal
					if($pkg_interface <> "console") {
708
						echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
709
						echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
710
					}
711
					sleep(1);
712
					return false;
713 1a22ffcd Scott Ullrich
				}
714 e65a287f Scott Ullrich
			}
715
		}
716
	}
717 7597c8e8 Colin Smith
	$configfile = substr(strrchr($pkg_info['config_file'], '/'), 1);
718
	if(file_exists("/usr/local/pkg/" . $configfile)) {
719 9d3d8d00 Vinicius Coque
		$static_output .= gettext("Loading package configuration... ");
720 7597c8e8 Colin Smith
		update_output_window($static_output);
721 43db85f8 Scott Ullrich
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $configfile, "packagegui");
722 6acdf659 Carlos Eduardo Ramos
		$static_output .= gettext("done.") . "\n";
723 7597c8e8 Colin Smith
		update_output_window($static_output);
724 9d3d8d00 Vinicius Coque
		$static_output .= gettext("Configuring package components...\n");
725 8dee24a6 Ermal Lu?i
		if (!empty($pkg_config['filter_rules_needed']))
726 bc771948 Ermal Lu?i
			$config['installedpackages']['package'][$pkgid]['filter_rule_function'] = $pkg_config['filter_rules_needed'];
727 7597c8e8 Colin Smith
		update_output_window($static_output);
728
		/* modify system files */
729 fcf92dae Ermal
		if(is_array($pkg_config['modify_system']) && is_array($pkg_config['modify_system']['item'])) {
730 9d3d8d00 Vinicius Coque
			$static_output .= gettext("System files... ");
731 7597c8e8 Colin Smith
			update_output_window($static_output);
732
			foreach($pkg_config['modify_system']['item'] as $ms) {
733
				if($ms['textneeded']) {
734
					add_text_to_file($ms['modifyfilename'], $ms['textneeded']);
735
				}
736
			}
737 6acdf659 Carlos Eduardo Ramos
			$static_output .= gettext("done.") . "\n";
738 7597c8e8 Colin Smith
			update_output_window($static_output);
739
		}
740
		/* download additional files */
741 1570d27a Ermal Lu?i
		if(is_array($pkg_config['additional_files_needed'])) {
742 9d3d8d00 Vinicius Coque
			$static_output .= gettext("Additional files... ");
743 7597c8e8 Colin Smith
			$static_orig = $static_output;
744
			update_output_window($static_output);
745
			foreach($pkg_config['additional_files_needed'] as $afn) {
746
				$filename = get_filename_from_url($afn['item'][0]);
747 b2a66231 Ermal
				if($afn['chmod'] <> "")
748 7597c8e8 Colin Smith
					$pkg_chmod = $afn['chmod'];
749 b2a66231 Ermal
				else
750 7597c8e8 Colin Smith
					$pkg_chmod = "";
751 b2a66231 Ermal
752
				if($afn['prefix'] <> "")
753 7597c8e8 Colin Smith
					$prefix = $afn['prefix'];
754 b2a66231 Ermal
				else
755 7597c8e8 Colin Smith
					$prefix = "/usr/local/pkg/";
756 b2a66231 Ermal
757 e6d436e8 Scott Ullrich
				if(!is_dir($prefix)) 
758
					safe_mkdir($prefix);
759
 				$static_output .= $filename . " ";
760 b2b15543 Scott Ullrich
				update_output_window($static_output);
761 fcf92dae Ermal
				if (download_file_with_progress_bar($afn['item'][0], $prefix . $filename) !== true) {
762
					$static_output .= "failed.\n";
763 6f14b34a Ermal
					@unlink($prefix . $filename);
764 fcf92dae Ermal
					update_output_window($static_output);
765
					return false;
766
				}
767 7597c8e8 Colin Smith
				if(stristr($filename, ".tgz") <> "") {
768 c92ccac7 Vinicius Coque
					pkg_debug(gettext("Extracting tarball to -C for ") . $filename . "...\n");
769 e65a287f Scott Ullrich
					$tarout = "";
770 c1312033 Colin Smith
					exec("/usr/bin/tar xvzf " . $prefix . $filename . " -C / 2>&1", $tarout);
771 eab543ed Ermal
					pkg_debug(print_r($tarout, true) . "\n");
772 7597c8e8 Colin Smith
				}
773
				if($pkg_chmod <> "") {
774 c92ccac7 Vinicius Coque
					pkg_debug(sprintf(gettext('Changing file mode to %1$s for %2$s%3$s%4$s'), $pkg_chmod, $prefix, $filename, "\n"));
775 6ee34f4d Ermal Lu?i
					@chmod($prefix . $filename, $pkg_chmod);
776 7597c8e8 Colin Smith
					system("/bin/chmod {$pkg_chmod} {$prefix}{$filename}");
777
				}
778
				$static_output = $static_orig;
779
                                update_output_window($static_output);
780
			}
781 6acdf659 Carlos Eduardo Ramos
			$static_output .= gettext("done.") . "\n";
782 7597c8e8 Colin Smith
			update_output_window($static_output);
783
		}
784 2df5cb99 Scott Ullrich
		/*   if a require exists, include it.  this will
785
		 *   show us where an error exists in a package
786
		 *   instead of making us blindly guess
787
		 */
788 fcf92dae Ermal
		$missing_include = false;
789 2df5cb99 Scott Ullrich
		if($pkg_config['include_file'] <> "") {
790 3ec86ca8 Renato Botelho
			$static_output = gettext("Loading package instructions...") . "\n";
791 2df5cb99 Scott Ullrich
			update_output_window($static_output);
792 eab543ed Ermal
			pkg_debug("require_once('{$pkg_config['include_file']}')\n");
793 43ad432c Ermal Lu?i
			if (file_exists($pkg_config['include_file']))
794
				require_once($pkg_config['include_file']);
795 fcf92dae Ermal
			else {
796
				$missing_include = true;
797 7dbbaecd Scott Ullrich
				$static_output .= "Include " . basename($pkg_config['include_file']) . " is missing!\n";
798 fcf92dae Ermal
				update_output_window($static_output);
799
				/* XXX: Should undo the steps before this?! */
800
				return false;
801
			}
802 43db85f8 Scott Ullrich
		}
803 57811192 Ermal
804
		/* custom commands */
805
		$static_output .= gettext("Custom commands...") . "\n";
806
		update_output_window($static_output);
807
		if ($missing_include == false) {
808
			if($pkg_config['custom_php_global_functions'] <> "") {
809
				$static_output .= gettext("Executing custom_php_global_functions()...");
810
				update_output_window($static_output);
811
				eval_once($pkg_config['custom_php_global_functions']);
812
				$static_output .= gettext("done.") . "\n";
813
				update_output_window($static_output);
814
			}
815
			if($pkg_config['custom_php_install_command']) {
816
				$static_output .= gettext("Executing custom_php_install_command()...");
817
				update_output_window($static_output);
818 169f1574 Vinicius Coque
				/* XXX: create symlinks for conf files into the PBI directories.
819
				 *	change packages to store configs at /usr/pbi/pkg/etc and remove this
820
				 */
821
				eval_once($pkg_config['custom_php_install_command']);
822 8059acb5 Phil Davis
				// Note: pkg may be mixed-case, e.g. "squidGuard" but the PBI names are lowercase.
823
				// e.g. "squidguard-1.4_4-i386" so feed lowercase to pbi_info below.
824
				// Also add the "-" so that examples like "squid-" do not match "squidguard-".
825
				$pkg_name_for_pbi_match = strtolower($pkg) . "-";
826 a1d4a048 Phil Davis
				exec("/usr/local/sbin/pbi_info | grep '^{$pkg_name_for_pbi_match}' | xargs /usr/local/sbin/pbi_info | awk '/Prefix/ {print $2}'",$pbidirarray);
827 84d50fe7 Phil Davis
				$pbidir0 = $pbidirarray[0];
828 169f1574 Vinicius Coque
				exec("find /usr/local/etc/ -name *.conf | grep {$pkg}",$files);
829
				foreach($files as $f) {
830 84d50fe7 Phil Davis
					$pbiconf = str_replace('/usr/local',$pbidir0,$f);
831 a1d4a048 Phil Davis
					if(is_file($pbiconf) || is_link($pbiconf)) {
832 8059acb5 Phil Davis
						unlink($pbiconf);
833
					}
834 a1d4a048 Phil Davis
					if(is_dir(dirname($pbiconf))) {
835
						symlink($f,$pbiconf);
836
					} else {
837
						log_error("The dir for {$pbiconf} does not exist. Cannot add symlink to {$f}.");
838
					}
839 169f1574 Vinicius Coque
				}
840 57811192 Ermal
				eval_once($pkg_config['custom_php_install_command']);
841
				$static_output .= gettext("done.") . "\n";
842
				update_output_window($static_output);
843
			}
844
			if($pkg_config['custom_php_resync_config_command'] <> "") {
845
				$static_output .= gettext("Executing custom_php_resync_config_command()...");
846
				update_output_window($static_output);
847
				eval_once($pkg_config['custom_php_resync_config_command']);
848
				$static_output .= gettext("done.") . "\n";
849
				update_output_window($static_output);
850
			}
851
		}
852 7597c8e8 Colin Smith
		/* sidebar items */
853 1570d27a Ermal Lu?i
		if(is_array($pkg_config['menu'])) {
854 9d3d8d00 Vinicius Coque
			$static_output .= gettext("Menu items... ");
855 7597c8e8 Colin Smith
			update_output_window($static_output);
856 1570d27a Ermal Lu?i
			foreach($pkg_config['menu'] as $menu) {
857 f3a274b7 Ermal
				if(is_array($config['installedpackages']['menu'])) {
858 1570d27a Ermal Lu?i
					foreach($config['installedpackages']['menu'] as $amenu)
859
						if($amenu['name'] == $menu['name'])
860
							continue 2;
861 f3a274b7 Ermal
				} else
862 27018d3c Ermal
					$config['installedpackages']['menu'] = array();
863 1570d27a Ermal Lu?i
				$config['installedpackages']['menu'][] = $menu;
864 7597c8e8 Colin Smith
			}
865 6acdf659 Carlos Eduardo Ramos
			$static_output .= gettext("done.") . "\n";
866 7597c8e8 Colin Smith
			update_output_window($static_output);
867
		}
868 b63f2e8b Matthew Grooms
		/* integrated tab items */
869 1570d27a Ermal Lu?i
		if(is_array($pkg_config['tabs']['tab'])) {
870 9d3d8d00 Vinicius Coque
			$static_output .= gettext("Integrated Tab items... ");
871 b63f2e8b Matthew Grooms
			update_output_window($static_output);
872 1570d27a Ermal Lu?i
			foreach($pkg_config['tabs']['tab'] as $tab) {
873 f3a274b7 Ermal
				if(is_array($config['installedpackages']['tab'])) {
874 1570d27a Ermal Lu?i
					foreach($config['installedpackages']['tab'] as $atab)
875
						if($atab['name'] == $tab['name'])
876
							continue 2;
877 f3a274b7 Ermal
				} else
878 27018d3c Ermal
					$config['installedpackages']['tab'] = array();
879 1570d27a Ermal Lu?i
				$config['installedpackages']['tab'][] = $tab;
880 b63f2e8b Matthew Grooms
			}
881 6acdf659 Carlos Eduardo Ramos
			$static_output .= gettext("done.") . "\n";
882 b63f2e8b Matthew Grooms
			update_output_window($static_output);
883
		}
884 2dc264a4 Colin Smith
		/* services */
885 1570d27a Ermal Lu?i
		if(is_array($pkg_config['service'])) {
886 9d3d8d00 Vinicius Coque
			$static_output .= gettext("Services... ");
887 2dc264a4 Colin Smith
			update_output_window($static_output);
888
			foreach($pkg_config['service'] as $service) {
889 f3a274b7 Ermal
				if(is_array($config['installedpackages']['service'])) {
890 d282095a Renato Botelho
					foreach($config['installedpackages']['service'] as $aservice)
891
						if($aservice['name'] == $service['name'])
892
							continue 2;
893 f3a274b7 Ermal
				} else
894 27018d3c Ermal
					$config['installedpackages']['service'] = array();
895 2dc264a4 Colin Smith
				$config['installedpackages']['service'][] = $service;
896
			}
897 6acdf659 Carlos Eduardo Ramos
			$static_output .= gettext("done.") . "\n";
898 2dc264a4 Colin Smith
			update_output_window($static_output);
899
		}
900 7597c8e8 Colin Smith
	} else {
901 6acdf659 Carlos Eduardo Ramos
		$static_output .= gettext("Loading package configuration... failed!") . "\n\n" . gettext("Installation aborted.");
902 7597c8e8 Colin Smith
		update_output_window($static_output);
903 c92ccac7 Vinicius Coque
		pkg_debug(gettext("Unable to load package configuration. Installation aborted.") ."\n");
904 1a22ffcd Scott Ullrich
		if($pkg_interface <> "console") {
905
			echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
906
			echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
907
		}
908 e65a287f Scott Ullrich
		sleep(1);
909 2c794549 Ermal
		return false;
910 7597c8e8 Colin Smith
	}
911 2c794549 Ermal
912
	/* set up package logging streams */
913
	if($pkg_info['logging']) {
914
		mwexec("/usr/sbin/fifolog_create -s 32768 {$g['varlog_path']}/{$pkg_info['logging']['logfilename']}");
915
		@chmod($g['varlog_path'] . '/' . $pkg_info['logging']['logfilename'], 0600);
916 2addd5b2 Ermal
		add_text_to_file("/etc/syslog.conf", $pkg_info['logging']['facilityname'] . "\t\t\t\t" . $pkg_info['logging']['logfilename']);
917 eab543ed Ermal
		pkg_debug("Adding text to file /etc/syslog.conf\n");
918 2c794549 Ermal
		system_syslogd_start();
919
	}
920
921
	return true;
922 7597c8e8 Colin Smith
}
923 407bf67a Colin Smith
924 64974db7 Scott Ullrich
function does_package_depend($pkg) {
925
	// Should not happen, but just in case.
926
	if(!$pkg)
927
		return;
928
	$pkg_var_db_dir = glob("/var/db/pkg/{$pkg}*");
929
	// If this package has dependency then return true
930
	foreach($pkg_var_db_dir as $pvdd) {
931
		if (file_exists("{$vardb}/{$pvdd}/+REQUIRED_BY") && count(file("{$vardb}/{$pvdd}/+REQUIRED_BY")) > 0) 
932
			return true;
933
	}	
934
	// Did not find a record of dependencies, so return false.
935
	return false;
936
}
937
938 fcf92dae Ermal
function delete_package($pkg) {
939 1b28121f Ermal
	global $config, $g, $static_output, $vardb;
940 b2a66231 Ermal
941 86af45ec Scott Ullrich
	if(!$pkg) 
942
		return;
943 62c55268 Colin Smith
944 8059acb5 Phil Davis
	// Note: $pkg has the full PBI package name followed by ".pbi". Strip off ".pbi".
945 fcf92dae Ermal
	$pkg = substr(reverse_strrchr($pkg, "."), 0, -1);
946 b2a66231 Ermal
947 a5566aa8 Vinicius Coque
	if($pkg)
948
		$static_output .= sprintf(gettext("Starting package deletion for %s..."),$pkg);
949
	update_output_window($static_output);
950 64974db7 Scott Ullrich
951 9eeef922 Scott Ullrich
	remove_freebsd_package($pkg);
952 d9426abc Ermal
	$static_output .= "done.\n";
953
	update_output_window($static_output);
954 fcf92dae Ermal
955 b7ff3186 Ermal
	/* Rescan directories for what has been left and avoid fooling other programs. */
956
	mwexec("/sbin/ldconfig");
957
958 407bf67a Colin Smith
	return;
959
}
960
961
function delete_package_xml($pkg) {
962 941baf1e Ermal
	global $g, $config, $static_output, $pkg_interface, $rcfileprefix;
963 b2a66231 Ermal
964 232b01db jim-p
	conf_mount_rw();
965 6955830f Ermal Lu?i
966 b2a66231 Ermal
	$pkgid = get_pkg_id($pkg);
967
	if ($pkgid == -1) {
968 6acdf659 Carlos Eduardo Ramos
		$static_output .= sprintf(gettext("The %s package is not installed.%sDeletion aborted."), $pkg, "\n\n");
969 e65a287f Scott Ullrich
		update_output_window($static_output);
970 1a22ffcd Scott Ullrich
		if($pkg_interface <> "console") {
971
			echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
972
			echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
973
		}
974 e65a287f Scott Ullrich
		ob_flush();
975
		sleep(1);
976 3339fac0 Ermal Lu?i
		conf_mount_ro();
977 e65a287f Scott Ullrich
		return;
978
	}
979 c92ccac7 Vinicius Coque
	pkg_debug(sprintf(gettext("Removing %s package... "),$pkg));
980
	$static_output .= sprintf(gettext("Removing %s components..."),$pkg) . "\n";
981 407bf67a Colin Smith
	update_output_window($static_output);
982
	/* parse package configuration */
983
	$packages = &$config['installedpackages']['package'];
984 b63f2e8b Matthew Grooms
	$tabs =& $config['installedpackages']['tab'];
985
	$menus =& $config['installedpackages']['menu'];
986 3c41c4ab Colin Smith
	$services = &$config['installedpackages']['service'];
987 2addd5b2 Ermal
	$pkg_info =& $packages[$pkgid];
988
	if(file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
989 19a11678 Colin Smith
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $packages[$pkgid]['configurationfile'], "packagegui");
990 b63f2e8b Matthew Grooms
		/* remove tab items */
991
		if(is_array($pkg_config['tabs'])) {
992 9d3d8d00 Vinicius Coque
			$static_output .= gettext("Tabs items... ");
993 b63f2e8b Matthew Grooms
			update_output_window($static_output);
994 8604523b Ermal Lu?i
			if(is_array($pkg_config['tabs']['tab']) && is_array($tabs)) {
995
				foreach($pkg_config['tabs']['tab'] as $tab) {
996 b2a66231 Ermal
					foreach($tabs as $key => $insttab) {
997
						if($insttab['name'] == $tab['name']) {
998 5274feb0 Scott Ullrich
							unset($tabs[$key]);
999 b2a66231 Ermal
							break;
1000
						}
1001
					}
1002 8604523b Ermal Lu?i
				}
1003
			}
1004 6acdf659 Carlos Eduardo Ramos
			$static_output .= gettext("done.") . "\n";
1005 b63f2e8b Matthew Grooms
			update_output_window($static_output);
1006
		}
1007 3f01fe47 Colin Smith
		/* remove menu items */
1008
		if(is_array($pkg_config['menu'])) {
1009 9d3d8d00 Vinicius Coque
			$static_output .= gettext("Menu items... ");
1010 3f01fe47 Colin Smith
			update_output_window($static_output);
1011 8604523b Ermal Lu?i
			if (is_array($pkg_config['menu']) && is_array($menus)) {
1012
				foreach($pkg_config['menu'] as $menu) {
1013 b2a66231 Ermal
					foreach($menus as $key => $instmenu) {
1014
						if($instmenu['name'] == $menu['name']) {
1015 8604523b Ermal Lu?i
							unset($menus[$key]);
1016 b2a66231 Ermal
							break;
1017
						}
1018
					}
1019 8604523b Ermal Lu?i
				}
1020
			}
1021 6acdf659 Carlos Eduardo Ramos
			$static_output .= gettext("done.") . "\n";
1022 3f01fe47 Colin Smith
			update_output_window($static_output);
1023 407bf67a Colin Smith
		}
1024 3c41c4ab Colin Smith
		/* remove services */
1025
		if(is_array($pkg_config['service'])) {
1026 9d3d8d00 Vinicius Coque
			$static_output .= gettext("Services... ");
1027 3c41c4ab Colin Smith
			update_output_window($static_output);
1028 8604523b Ermal Lu?i
			if (is_array($pkg_config['service']) && is_array($services)) {
1029
				foreach($pkg_config['service'] as $service) {
1030
					foreach($services as $key => $instservice) {
1031
						if($instservice['name'] == $service['name']) {
1032 b1224cdc jim-p
							if($g['booting'] != true)
1033 06e57df8 Scott Ullrich
								stop_service($service['name']);
1034 941baf1e Ermal
							if($service['rcfile']) {
1035
								$prefix = $rcfileprefix;
1036
								if (!empty($service['prefix']))
1037
									$prefix = $service['prefix'];
1038
								if (file_exists("{$prefix}{$service['rcfile']}"))
1039
									@unlink("{$prefix}{$service['rcfile']}");
1040
							}
1041 8604523b Ermal Lu?i
							unset($services[$key]);
1042
						}
1043 0cab7cad Colin Smith
					}
1044 3c41c4ab Colin Smith
				}
1045
			}
1046 6acdf659 Carlos Eduardo Ramos
			$static_output .= gettext("done.") . "\n";
1047 3c41c4ab Colin Smith
			update_output_window($static_output);
1048
		}
1049 b2a66231 Ermal
		/*
1050
		 * XXX: Otherwise inclusion of config.inc again invalidates actions taken.
1051
		 * 	Same is done during installation.
1052
		 */
1053 f9c8e64c jim-p
		write_config("Intermediate config write during package removal for {$pkg}.");
1054 b2a66231 Ermal
1055
		/*
1056
		 * If a require exists, include it.  this will
1057
		 * show us where an error exists in a package
1058
		 * instead of making us blindly guess
1059 892aef15 Scott Ullrich
		 */
1060 fcf92dae Ermal
		$missing_include = false;
1061 892aef15 Scott Ullrich
		if($pkg_config['include_file'] <> "") {
1062 3ec86ca8 Renato Botelho
			$static_output .= gettext("Loading package instructions...") . "\n";
1063 892aef15 Scott Ullrich
			update_output_window($static_output);
1064 eab543ed Ermal
			pkg_debug("require_once(\"{$pkg_config['include_file']}\")\n");
1065 fcf92dae Ermal
			if (file_exists($pkg_config['include_file']))
1066 1570d27a Ermal Lu?i
				require_once($pkg_config['include_file']);
1067 fcf92dae Ermal
			else {
1068
				$missing_include = true;
1069
				update_output_window($static_output);
1070 7dbbaecd Scott Ullrich
				$static_output .= "Include file " . basename($pkg_config['include_file']) . " could not be found for inclusion.\n";
1071 fcf92dae Ermal
			}
1072
		}
1073
		/* ermal
1074
		 * NOTE: It is not possible to handle parse errors on eval.
1075
		 * So we prevent it from being run at all to not interrupt all the other code.
1076
		 */
1077
		if ($missing_include == false) {
1078
			/* evalate this package's global functions and pre deinstall commands */
1079
			if($pkg_config['custom_php_global_functions'] <> "")
1080
				eval_once($pkg_config['custom_php_global_functions']);
1081
			if($pkg_config['custom_php_pre_deinstall_command'] <> "")
1082
				eval_once($pkg_config['custom_php_pre_deinstall_command']);
1083 43db85f8 Scott Ullrich
		}
1084 3f01fe47 Colin Smith
		/* system files */
1085 fcf92dae Ermal
		if(is_array($pkg_config['modify_system']) && is_array($pkg_config['modify_system']['item'])) {
1086 9d3d8d00 Vinicius Coque
			$static_output .= gettext("System files... ");
1087 3f01fe47 Colin Smith
			update_output_window($static_output);
1088 b2a66231 Ermal
			foreach($pkg_config['modify_system']['item'] as $ms)
1089 3f01fe47 Colin Smith
				if($ms['textneeded']) remove_text_from_file($ms['modifyfilename'], $ms['textneeded']);
1090 b2a66231 Ermal
1091 6acdf659 Carlos Eduardo Ramos
			$static_output .= gettext("done.") . "\n";
1092 3f01fe47 Colin Smith
			update_output_window($static_output);
1093 407bf67a Colin Smith
		}
1094 644d2d59 Colin Smith
		/* deinstall commands */
1095
		if($pkg_config['custom_php_deinstall_command'] <> "") {
1096 9d3d8d00 Vinicius Coque
			$static_output .= gettext("Deinstall commands... ");
1097 644d2d59 Colin Smith
			update_output_window($static_output);
1098 fcf92dae Ermal
			if ($missing_include == false) {
1099
				eval_once($pkg_config['custom_php_deinstall_command']);
1100 3ec86ca8 Renato Botelho
				$static_output .= gettext("done.") . "\n";
1101 fcf92dae Ermal
			} else
1102 7dbbaecd Scott Ullrich
				$static_output .= "\nNot executing custom deinstall hook because an include is missing.\n";
1103 644d2d59 Colin Smith
			update_output_window($static_output);
1104
		}
1105 1570d27a Ermal Lu?i
		if($pkg_config['include_file'] <> "") {
1106 9d3d8d00 Vinicius Coque
			$static_output .= gettext("Removing package instructions...");
1107 b2b15543 Scott Ullrich
			update_output_window($static_output);
1108 c92ccac7 Vinicius Coque
                        pkg_debug(sprintf(gettext("Remove '%s'"), $pkg_config['include_file']) . "\n");
1109 b2b15543 Scott Ullrich
			unlink_if_exists("/usr/local/pkg/" . $pkg_config['include_file']);
1110 9d3d8d00 Vinicius Coque
			$static_output .= gettext("done.") . "\n";
1111 b2b15543 Scott Ullrich
			update_output_window($static_output);
1112
		}
1113 af8fca8f jim-p
		/* remove all additional files */
1114
		if(is_array($pkg_config['additional_files_needed'])) {
1115 9d3d8d00 Vinicius Coque
			$static_output .= gettext("Auxiliary files... ");
1116 af8fca8f jim-p
			update_output_window($static_output);
1117
			foreach($pkg_config['additional_files_needed'] as $afn) {
1118
				$filename = get_filename_from_url($afn['item'][0]);
1119 b2a66231 Ermal
				if($afn['prefix'] <> "")
1120 af8fca8f jim-p
					$prefix = $afn['prefix'];
1121 b2a66231 Ermal
				else
1122 af8fca8f jim-p
					$prefix = "/usr/local/pkg/";
1123
				unlink_if_exists($prefix . $filename);
1124
			}
1125 6acdf659 Carlos Eduardo Ramos
			$static_output .= gettext("done.") . "\n";
1126 af8fca8f jim-p
			update_output_window($static_output);
1127
		}
1128 047c40c4 Colin Smith
		/* package XML file */
1129 9d3d8d00 Vinicius Coque
		$static_output .= gettext("Package XML... ");
1130 047c40c4 Colin Smith
		update_output_window($static_output);
1131
		unlink_if_exists("/usr/local/pkg/" . $packages[$pkgid]['configurationfile']);
1132 6acdf659 Carlos Eduardo Ramos
		$static_output .= gettext("done.") . "\n";
1133 047c40c4 Colin Smith
		update_output_window($static_output);
1134 407bf67a Colin Smith
	}
1135 2addd5b2 Ermal
	/* syslog */
1136
	if(is_array($pkg_info['logging']) && $pkg_info['logging']['logfile_name'] <> "") {
1137 7dbbaecd Scott Ullrich
		$static_output .= "Syslog entries... ";
1138 2addd5b2 Ermal
		update_output_window($static_output);
1139
		remove_text_from_file("/etc/syslog.conf", $pkg_info['logging']['facilityname'] . "\t\t\t\t" . $pkg_info['logging']['logfilename']);
1140
		system_syslogd_start();
1141
		@unlink("{$g['varlog_path']}/{$pkg_info['logging']['logfilename']}");
1142
		$static_output .= "done.\n";
1143
		update_output_window($static_output);
1144
	}
1145 e9c7a334 Scott Ullrich
	
1146 3339fac0 Ermal Lu?i
	conf_mount_ro();
1147 2addd5b2 Ermal
	/* remove config.xml entries */
1148 9d3d8d00 Vinicius Coque
	$static_output .= gettext("Configuration... ");
1149 407bf67a Colin Smith
	update_output_window($static_output);
1150
	unset($config['installedpackages']['package'][$pkgid]);
1151 6acdf659 Carlos Eduardo Ramos
	$static_output .= gettext("done.") . "\n";
1152 407bf67a Colin Smith
	update_output_window($static_output);
1153 fcf92dae Ermal
	write_config("Removed {$pkg} package.\n");
1154 407bf67a Colin Smith
}
1155 566181ea Colin Smith
1156
function expand_to_bytes($size) {
1157
	$conv = array(
1158
			"G" =>	"3",
1159
			"M" =>  "2",
1160
			"K" =>  "1",
1161
			"B" =>  "0"
1162
		);
1163
	$suffix = substr($size, -1);
1164
	if(!in_array($suffix, array_keys($conv))) return $size;
1165
	$size = substr($size, 0, -1);
1166
	for($i = 0; $i < $conv[$suffix]; $i++) {
1167
		$size *= 1024;
1168
	}
1169
	return $size;
1170
}
1171
1172
function get_pkg_db() {
1173
	global $g;
1174
	return return_dir_as_array($g['vardb_path'] . '/pkg');
1175
}
1176
1177 b8a1c2a3 Colin Smith
function walk_depend($depend, $pkgdb = "", $alreadyseen = "") {
1178 1570d27a Ermal Lu?i
	if(!$pkgdb)
1179
		$pkgdb = get_pkg_db();
1180
	if(!is_array($alreadyseen))
1181
		$alreadyseen = array();
1182
	if (!is_array($depend))
1183
		$depend = array();
1184 566181ea Colin Smith
	foreach($depend as $adepend) {
1185
		$pkgname = reverse_strrchr($adepend['name'], '.');
1186 b8a1c2a3 Colin Smith
		if(in_array($pkgname, $alreadyseen)) {
1187
			continue;
1188
		} elseif(!in_array($pkgname, $pkgdb)) {
1189
			$size += expand_to_bytes($adepend['size']);
1190
			$alreadyseen[] = $pkgname;
1191
			if(is_array($adepend['depend'])) $size += walk_depend($adepend['depend'], $pkgdb, $alreadyseen);
1192
		}
1193 566181ea Colin Smith
	}
1194
	return $size;
1195
}
1196
1197
function get_package_install_size($pkg = 'all', $pkg_info = "") {
1198
	global $config, $g;
1199 1570d27a Ermal Lu?i
	if((!is_array($pkg)) and ($pkg != 'all'))
1200
		$pkg = array($pkg);
1201 566181ea Colin Smith
	$pkgdb = get_pkg_db();
1202 1570d27a Ermal Lu?i
	if(!$pkg_info)
1203
		$pkg_info = get_pkg_sizes($pkg);
1204 566181ea Colin Smith
	foreach($pkg as $apkg) {
1205 2addd5b2 Ermal
		if(!$pkg_info[$apkg])
1206
			continue;
1207 b8a1c2a3 Colin Smith
		$toreturn[$apkg] = expand_to_bytes(walk_depend(array($pkg_info[$apkg]), $pkgdb));
1208 566181ea Colin Smith
	}
1209 b8a1c2a3 Colin Smith
	return $toreturn;
1210 566181ea Colin Smith
}
1211 f0a550fd Colin Smith
1212 e43ba9ad Colin Smith
function squash_from_bytes($size, $round = "") {
1213 f0a550fd Colin Smith
	$conv = array(1 => "B", "K", "M", "G");
1214
	foreach($conv as $div => $suffix) {
1215
		$sizeorig = $size;
1216
		if(($size /= 1024) < 1) {
1217 e43ba9ad Colin Smith
			if($round) {
1218
				$sizeorig = round($sizeorig, $round);
1219
			}
1220 f0a550fd Colin Smith
			return $sizeorig . $suffix;
1221
		}
1222
	}
1223
	return;
1224
}
1225 5025a56c Scott Ullrich
1226 9b193619 Scott Ullrich
function pkg_reinstall_all() {
1227
	global $g, $config;
1228 c53eb903 Ermal
1229
	@unlink('/conf/needs_package_sync');
1230 9b193619 Scott Ullrich
	$pkg_id = 0;
1231
	$todo = array();
1232
	if (is_array($config['installedpackages']['package']))
1233
		foreach($config['installedpackages']['package'] as $package)
1234
			$todo[] = array('name' => $package['name'], 'version' => $package['version']);
1235
	echo "One moment please, reinstalling packages...\n";
1236 ad0d6389 jim-p
	echo " >>> Trying to fetch package info...";
1237
	$pkg_info = get_pkg_info();
1238
	if ($pkg_info) {
1239
		echo " Done.\n";
1240
	} else {
1241
		$xmlrpc_base_url = isset($config['system']['altpkgrepo']['enable']) ? $config['system']['altpkgrepo']['xmlrpcbaseurl'] : $g['xmlrpcbaseurl'];
1242
		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";
1243
		return;
1244
	}
1245 9b193619 Scott Ullrich
	if(is_array($todo)) {
1246
		foreach($todo as $pkgtodo) {
1247
			$static_output = "";
1248
			if($pkgtodo['name']) {
1249
				uninstall_package($pkgtodo['name']);
1250
				install_package($pkgtodo['name']);
1251
				$pkg_id++;
1252
			}
1253
		}
1254
	}
1255
}
1256
1257 60dd7649 jim-p
function stop_packages() {
1258
	require_once("config.inc");
1259
	require_once("functions.inc");
1260
	require_once("filter.inc");
1261
	require_once("shaper.inc");
1262
	require_once("captiveportal.inc");
1263
	require_once("pkg-utils.inc");
1264
	require_once("pfsense-utils.inc");
1265
	require_once("service-utils.inc");
1266
1267
	global $config, $g, $rcfileprefix;
1268
1269
	log_error("Stopping all packages.");
1270
1271
	$rcfiles = glob("{$rcfileprefix}*.sh");
1272
	if (!$rcfiles)
1273
		$rcfiles = array();
1274
	else {
1275
		$rcfiles = array_flip($rcfiles);
1276
		if (!$rcfiles)
1277
			$rcfiles = array();
1278
	}
1279
1280
	if (is_array($config['installedpackages']['package'])) {
1281
		foreach($config['installedpackages']['package'] as $package) {
1282
			echo " Stopping package {$package['name']}...";
1283
			stop_service($package['name']);
1284
			unset($rcfiles["{$rcfileprefix}{$package['name']}.sh"]);
1285
			echo "done.\n";
1286
		}
1287
	}
1288
1289
	$shell = @popen("/bin/sh", "w");
1290
	if ($shell) {
1291
		foreach ($rcfiles as $rcfile => $number) {
1292
			echo " Stopping {$rcfile}...";
1293
			fwrite($shell, "{$rcfile} stop >>/tmp/bootup_messages 2>&1");
1294
			echo "done.\n";
1295
		}
1296
1297
		pclose($shell);
1298
	}
1299
}
1300
1301 6955830f Ermal Lu?i
?>