Project

General

Profile

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