Project

General

Profile

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