Project

General

Profile

Download (40.2 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 0e16b9ca Scott Ullrich
 * Copyright (C) 2005-2006 Colin Smith (ethethlay@gmail.com)
12 8c6516d1 Colin Smith
 * All rights reserved.
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *
16
 * 1. Redistributions of source code must retain the above copyright notice,
17
 * this list of conditions and the following disclaimer.
18
 *
19
 * 2. Redistributions in binary form must reproduce the above copyright
20
 * notice, this list of conditions and the following disclaimer in the
21
 * documentation and/or other materials provided with the distribution.
22
 *
23
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
 * RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
 * POSSIBILITY OF SUCH DAMAGE.
33
 *
34
 */
35 523855b0 Scott Ullrich
36
/*
37
	pfSense_BUILDER_BINARIES:	/usr/bin/cd	/usr/bin/tar	/bin/cat	/usr/sbin/fifolog_create	/bin/chmod
38
	pfSense_BUILDER_BINARIES:	/usr/bin/killall	/usr/sbin/pkg_info	/usr/sbin/pkg_delete	/bin/rm	/bin/ls
39
	pfSense_BUILDER_BINARIES:	/sbin/pfctl
40
	pfSense_MODULE:	pkg
41
*/
42
43 33b7cc0d Colin Smith
require_once("xmlrpc.inc");
44 093bcebc Scott Ullrich
if(file_exists("/cf/conf/use_xmlreader"))
45
	require_once("xmlreader.inc");
46
else
47
	require_once("xmlparse.inc");
48 3c41c4ab Colin Smith
require_once("service-utils.inc");
49 7597c8e8 Colin Smith
require_once("pfsense-utils.inc");
50
require_once("globals.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 7597c8e8 Colin Smith
safe_mkdir("/var/db/pkg");
64 7955cde8 Scott Ullrich
65 5408fe23 Scott Ullrich
$g['platform'] = trim(file_get_contents("/etc/platform"));
66
if($g['platform'] == "pfSense") {
67 e7405fbf Scott Ullrich
	safe_mkdir("/usr/local/pkg");
68
	safe_mkdir("/usr/local/pkg/pf");
69 d3c02149 Scott Ullrich
} else {
70 7955cde8 Scott Ullrich
	if(!is_dir("/usr/local/pkg") or !is_dir("/usr/local/pkg/pf")) {
71 d3c02149 Scott Ullrich
	conf_mount_rw();
72
	safe_mkdir("/usr/local/pkg");
73 7955cde8 Scott Ullrich
	safe_mkdir("/usr/local/pkg/pf");	
74 6e2365bb Scott Ullrich
	conf_mount_ro();
75 7955cde8 Scott Ullrich
	}
76 e7405fbf Scott Ullrich
}
77 33b7cc0d Colin Smith
78 29b42b4a Ermal Luçi
$version = split("-", trim(file_get_contents("/etc/version")));
79
$ver = split("\.", $version[0]);
80
$g['version'] = intval($ver[1]);
81
82 31e7e1bc Scott Ullrich
/****f* pkg-utils/remove_package
83
 * NAME
84
 *   remove_package - Removes package from FreeBSD if it exists
85
 * INPUTS
86
 *   $packagestring	- name/string to check for
87
 * RESULT
88
 *   none
89
 * NOTES
90
 *   
91
 ******/
92
function remove_freebsd_package($packagestring) {
93 9eeef922 Scott Ullrich
	exec("cd /var/db/pkg && echo y | pkg_delete `ls | grep $packagestring`");
94 31e7e1bc Scott Ullrich
}
95
96 33b7cc0d Colin Smith
/****f* pkg-utils/is_package_installed
97
 * NAME
98
 *   is_package_installed - Check whether a package is installed.
99
 * INPUTS
100
 *   $packagename	- name of the package to check
101
 * RESULT
102
 *   boolean	- true if the package is installed, false otherwise
103
 * NOTES
104
 *   This function is deprecated - get_pkg_id() can already check for installation.
105
 ******/
106 8c6516d1 Colin Smith
function is_package_installed($packagename) {
107 33b7cc0d Colin Smith
	$pkg = get_pkg_id($packagename);
108
	if($pkg == -1) return false;
109
	return true;
110 8c6516d1 Colin Smith
}
111 43db85f8 Scott Ullrich
112 33b7cc0d Colin Smith
/****f* pkg-utils/get_pkg_id
113
 * NAME
114
 *   get_pkg_id - Find a package's numeric ID.
115
 * INPUTS
116
 *   $pkg_name	- name of the package to check
117
 * RESULT
118
 *   integer    - -1 if package is not found, >-1 otherwise
119
 ******/
120 8c6516d1 Colin Smith
function get_pkg_id($pkg_name) {
121 e65a287f Scott Ullrich
	global $config;
122
123
	if(is_array($config['installedpackages']['package'])) {
124
		$i = 0;
125
		foreach($config['installedpackages']['package'] as $pkg) {
126
			if($pkg['name'] == $pkg_name) return $i;
127
			$i++;
128
		}
129
	}
130
	return -1;
131 8c6516d1 Colin Smith
}
132
133 33b7cc0d Colin Smith
/****f* pkg-utils/get_pkg_info
134
 * NAME
135
 *   get_pkg_info - Retrive package information from pfsense.com.
136
 * INPUTS
137
 *   $pkgs - 'all' to retrive all packages, an array containing package names otherwise
138
 *   $info - 'all' to retrive all information, an array containing keys otherwise
139
 * RESULT
140
 *   $raw_versions - Array containing retrieved information, indexed by package name.
141
 ******/
142
function get_pkg_info($pkgs = 'all', $info = 'all') {
143 7597c8e8 Colin Smith
	global $g;
144 416c47c1 Scott Ullrich
	$freebsd_version = str_replace("\n", "", `uname -r | cut -d'-' -f1 | cut -d'.' -f1`);
145 340c0677 Scott Ullrich
	$params = array(
146
		"pkg" => $pkgs, 
147
		"info" => $info, 
148
		"freebsd_version" => $freebsd_version
149
		);
150 e65a287f Scott Ullrich
	$resp = call_pfsense_method('pfsense.get_pkgs', $params, 10);
151
	return $resp ? $resp : array();
152
}
153
154
function get_pkg_sizes($pkgs = 'all') {
155
	global $g;
156
	$params = array("pkg" => $pkgs);
157
	$msg = new XML_RPC_Message('pfsense.get_pkg_sizes', array(php_value_to_xmlrpc($params)));
158 ffba4976 jim-p
	$xmlrpc_base_url = isset($config['system']['altpkgrepo']['enable']) ? $config['system']['altpkgrepo']['xmlrpcbaseurl'] : $g['xmlrpcbaseurl'];
159
	$cli = new XML_RPC_Client($g['xmlrpcpath'], $xmlrpc_base_url);
160 43db85f8 Scott Ullrich
	$resp = $cli->send($msg, 10);
161 34da63c3 Colin Smith
	if($resp and !$resp->faultCode()) {
162 e65a287f Scott Ullrich
		$raw_versions = $resp->value();
163 34da63c3 Colin Smith
		return xmlrpc_value_to_php($raw_versions);
164
	} else {
165
		return array();
166
	}
167 8c6516d1 Colin Smith
}
168
169
/*
170
 * resync_all_package_configs() Force packages to setup their configuration and rc.d files.
171
 * This function may also print output to the terminal indicating progress.
172
 */
173
function resync_all_package_configs($show_message = false) {
174 6520d17b Scott Ullrich
	global $config, $restart_sync, $pkg_interface;
175 e65a287f Scott Ullrich
	$i = 0;
176
	log_error("Resyncing configuration for all packages.");
177
	if(!$config['installedpackages']['package']) return;
178
	if($show_message == true) print "Syncing packages:";
179
	foreach($config['installedpackages']['package'] as $package) {
180 29c7ac0d Ermal Lu?i
		if (empty($package['name']))
181
			continue;
182 e65a287f Scott Ullrich
		if($show_message == true) print " " . $package['name'];
183 597dd9b9 Scott Ullrich
		get_pkg_depends($package['name'], "all");
184 2f9a19df Scott Ullrich
		stop_service($package['name']);
185 e65a287f Scott Ullrich
		sync_package($i, true, true);
186 20e593fa Scott Ullrich
		if($restart_sync == true) {
187
			$restart_sync = false;
188
			if($pkg_interface == "console") 
189 5059da03 Scott Ullrich
				echo "\nSyncing packages:";
190 20e593fa Scott Ullrich
		}
191 e65a287f Scott Ullrich
		$i++;
192
	}
193
	if($show_message == true) print ".\n";
194 8c6516d1 Colin Smith
}
195
196 7597c8e8 Colin Smith
/*
197
 * is_freebsd_pkg_installed() - Check /var/db/pkg to determine whether or not a FreeBSD
198
 *				package is installed.
199
 */
200
function is_freebsd_pkg_installed($pkg) {
201
	global $g;
202
	if(in_array($pkg, return_dir_as_array("{$g['vardb_path']}/pkg"))) return true;
203
	return false;
204
}
205
206 8c6516d1 Colin Smith
/*
207
 * get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", return_nosync = 1):  Return a package's dependencies.
208
 *
209
 * $filetype = "all" || ".xml", ".tgz", etc.
210
 * $format = "files" (full filenames) || "names" (stripped / parsed depend names)
211
 * $return_nosync = 1 (return depends that have nosync set) | 0 (ignore packages with nosync)
212
 *
213
 */
214
function get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", $return_nosync = 1) {
215 e65a287f Scott Ullrich
	global $config;
216 91dc2ecf Scott Ullrich
	require_once("notices.inc");
217 e65a287f Scott Ullrich
	$pkg_id = get_pkg_id($pkg_name);
218
	if(!is_numeric($pkg_name)) {
219
		if($pkg_id == -1) return -1; // This package doesn't really exist - exit the function.
220
	} else {
221
		if(!isset($config['installedpackages']['package'][$pkg_id])) return; // No package belongs to the pkg_id passed to this function.
222
	}
223
	$package = $config['installedpackages']['package'][$pkg_id];
224
	if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
225 4bf3f3f1 Scott Ullrich
		log_error("The {$package['name']} package is missing required dependencies and must be reinstalled." . $package['configurationfile']);
226 f898cf33 Scott Ullrich
		install_package($package['name']);
227
		uninstall_package_from_name($package['name']);
228 c8433e58 Scott Ullrich
		install_package($package['name']);
229 093441f0 Colin Smith
		return;
230
	}
231 19a11678 Colin Smith
	$pkg_xml = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
232 e65a287f Scott Ullrich
	if($pkg_xml['additional_files_needed'] != "") {
233
		foreach($pkg_xml['additional_files_needed'] as $item) {
234
			if (($return_nosync == 0) && (isset($item['nosync']))) continue; // Do not return depends with nosync set if not required.
235
			$depend_file = substr(strrchr($item['item']['0'], '/'),1); // Strip URLs down to filenames.
236
			$depend_name = substr(substr($depend_file,0,strpos($depend_file,".")+1),0,-1); // Strip filename down to dependency name.
237 27d671c8 Scott Ullrich
			if (($filetype != "all") && (!preg_match("/{$filetype}/i", $depend_file))) continue;
238 e65a287f Scott Ullrich
			if ($item['prefix'] != "") {
239
				$prefix = $item['prefix'];
240
			} else {
241
				$prefix = "/usr/local/pkg/";
242
			}
243 3e155fab Scott Ullrich
			if(!file_exists($prefix . $depend_file))
244 3b371980 Scott Ullrich
				log_error("The {$package['name']} package is missing required dependencies and must be reinstalled.");
245 e65a287f Scott Ullrich
			switch ($format) {
246
				case "files":
247
				$depends[] = $depend_file;
248
			break;
249
            			case "names":
250
                		switch ($filetype) {
251
252
				case "all":
253
				if(preg_match("/\.xml/i", $depend_file)) {
254
					$depend_xml = parse_xml_config_pkg("/usr/local/pkg/" . $depend_file, "packagegui");
255
					$depends[] = $depend_xml['name'];
256
					break;
257
				} else {
258
					$depends[] = $depend_name; // If this dependency isn't package XML, use the stripped filename.
259
				break;
260
				}
261
				case ".xml":
262
				$depend_xml = parse_xml_config_pkg("/usr/local/pkg/" . $depend_file, "packagegui");
263
				$depends[] = $depend_xml['name'];
264
				break;
265
				default:
266
				$depends[] = $depend_name; // If we aren't looking for XML, use the stripped filename (it's all we have).
267
				break;
268
				}
269
			}
270
		}
271
		return $depends;
272
	}
273 8c6516d1 Colin Smith
}
274
275 f898cf33 Scott Ullrich
function uninstall_package_from_name($pkg_name) {
276 8797aa32 Scott Ullrich
	global $config;
277 f898cf33 Scott Ullrich
	$id = get_pkg_id($pkg_name);
278
	$todel = substr(reverse_strrchr($config['installedpackages']['package'][$id]['depends_on_package'], "."), 0, -1);
279
	delete_package($todel, $pkg_name);
280
	delete_package_xml($pkg_name);
281 9eeef922 Scott Ullrich
	remove_freebsd_package($pkg_name);
282 f898cf33 Scott Ullrich
}
283
284 7bbfe007 Scott Ullrich
function force_remove_package($pkg_name) {
285
	global $config;
286
	delete_package_xml($pkg_name);
287
}
288
289 8c6516d1 Colin Smith
/*
290
 * sync_package($pkg_name, $sync_depends = true, $show_message = false) Force a package to setup its configuration and rc.d files.
291
 */
292
function sync_package($pkg_name, $sync_depends = true, $show_message = false) {
293 669e1adb Bill Marquette
	global $config;
294 11d30033 Scott Ullrich
	require_once("notices.inc");
295 669e1adb Bill Marquette
	if(!$config['installedpackages']['package']) return;
296
	if(!is_numeric($pkg_name)) {
297
		$pkg_id = get_pkg_id($pkg_name);
298
		if($pkg_id == -1) return -1; // This package doesn't really exist - exit the function.
299
	} else {
300
		$pkg_id = $pkg_name;
301
		if(!isset($config['installedpackages']['package'][$pkg_id]))
302
		return;  // No package belongs to the pkg_id passed to this function.
303
	}
304 04d10bbc Scott Ullrich
        if (is_array($config['installedpackages']['package'][$pkg_id]))
305 7bbfe007 Scott Ullrich
			$package = $config['installedpackages']['package'][$pkg_id];
306 04d10bbc Scott Ullrich
        else
307 7bbfe007 Scott Ullrich
			return; /* empty package tag */
308 669e1adb Bill Marquette
	if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
309 7bbfe007 Scott Ullrich
		log_error("The {$package['name']} package is missing its configuration file and must be reinstalled.");
310
		force_remove_package($package['name']);
311 669e1adb Bill Marquette
	} else {
312
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
313 30e4c34a Scott Ullrich
314
		/* Bring in package include files */
315 9d3d1ac1 Ermal Lu?i
		if (!empty($pkg_config['include_file'])) {
316 30e4c34a Scott Ullrich
			$include_file = $pkg_config['include_file'];
317 3e155fab Scott Ullrich
			if (file_exists($include_file))
318
				require_once($include_file);
319 83cfae8d Ermal Lu?i
			else {
320
				/* XXX: What the heck is this?! */
321
				log_error("Could not locate {$include_file}.");
322
				install_package($package['name']);
323
				uninstall_package_from_name($package['name']);
324
				remove_freebsd_package($package['name']);
325
				install_package($package['name']);
326
			}
327 30e4c34a Scott Ullrich
		}
328
329 669e1adb Bill Marquette
		/* XXX: Zend complains about the next line "Wrong break depth"
330
		 * The code is obviously wrong, but I'm not sure what it's supposed to do?
331
		 */
332
		if(isset($pkg_config['nosync'])) continue;
333 32113e06 Ermal Lu?i
		if(!empty($pkg_config['custom_php_global_functions']))
334
			eval($pkg_config['custom_php_global_functions']);
335
		if(!empty($pkg_config['custom_php_resync_config_command']))
336
			eval($pkg_config['custom_php_resync_config_command']);
337 669e1adb Bill Marquette
		if($sync_depends == true) {
338
			$depends = get_pkg_depends($pkg_name, ".xml", "files", 1); // Call dependency handler and do a little more error checking.
339
			if(is_array($depends)) {
340
				foreach($depends as $item) {
341 26a26ef7 Scott Ullrich
					if(!file_exists("/usr/local/pkg/" . $item)) {
342 093441f0 Colin Smith
						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);
343 4bf3f3f1 Scott Ullrich
						log_error("Could not find {$item}. Reinstalling package.");
344 f898cf33 Scott Ullrich
						install_package($pkg_name);
345
						uninstall_package_from_name($pkg_name);
346 9eeef922 Scott Ullrich
						remove_freebsd_package($pkg_name);						
347 46c4f5d0 Scott Ullrich
						install_package($pkg_name);
348 093441f0 Colin Smith
					} else {
349 669e1adb Bill Marquette
						$item_config = parse_xml_config_pkg("/usr/local/pkg/" . $item, "packagegui");
350
						if(isset($item_config['nosync'])) continue;
351
						if($item_config['custom_php_command_before_form'] <> "") {
352
							eval($item_config['custom_php_command_before_form']);
353
						}
354
						if($item_config['custom_php_resync_config_command'] <> "") {
355
							eval($item_config['custom_php_resync_config_command']);
356
						}
357
						if($show_message == true) print " " . $item_config['name'];
358
					}
359
				}
360
			}
361
		}
362
	}
363 8c6516d1 Colin Smith
}
364
365 7597c8e8 Colin Smith
/*
366
 * pkg_fetch_recursive: Download and install a FreeBSD package and its dependencies. This function provides output to
367
 * 			a progress bar and output window.
368
 *
369
 * XXX: This function needs to return where a pkg_add fails. Our current error messages aren't very descriptive.
370
 */
371 8c6516d1 Colin Smith
function pkg_fetch_recursive($pkgname, $filename, $dependlevel = 0, $base_url = 'http://ftp2.freebsd.org/pub/FreeBSD/ports/i386/packages-5.4-release/Latest') {
372 e65a287f Scott Ullrich
	global $pkgent, $static_output, $g, $fd_log;
373
	$pkg_extension = strrchr($filename, '.');
374
	$static_output .= "\n" . str_repeat(" ", $dependlevel * 2) . $pkgname . " ";
375
	$fetchto = "/tmp/apkg_" . $pkgname . $pkg_extension;
376 7597c8e8 Colin Smith
	download_file_with_progress_bar($base_url . '/' . $filename, $fetchto);
377 7df17e7c Scott Ullrich
	$static_output .= " (extracting)";
378 43db85f8 Scott Ullrich
	update_output_window($static_output);
379 e65a287f Scott Ullrich
		$slaveout = "";
380
	exec("/usr/bin/tar --fast-read -O -f {$fetchto} -x +CONTENTS 2>&1", $slaveout);
381
	$workingdir = preg_grep("/instmp/", $slaveout);
382
	$workingdir = $workingdir[0];
383
	$raw_depends_list = array_values(preg_grep("/\@pkgdep/", $slaveout));
384
	if($raw_depends_list != "") {
385
		if($pkgent['exclude_dependency'] != "")
386
			$raw_depends_list = array_values(preg_grep($pkgent['exclude_dependency'], PREG_GREP_INVERT));
387
		foreach($raw_depends_list as $adepend) {
388
			$working_depend = explode(" ", $adepend);
389
			//$working_depend = explode("-", $working_depend[1]);
390
			$depend_filename = $working_depend[1] . $pkg_extension;
391
			if(is_freebsd_pkg_installed($working_depend[1]) === false) {
392
				pkg_fetch_recursive($working_depend[1], $depend_filename, $dependlevel + 1, $base_url);
393
			} else {
394
//				$dependlevel++;
395
				$static_output .= "\n" . str_repeat(" ", $dependlevel * 2) . $working_depend[1] . " ";
396
				@fwrite($fd_log, $working_depend[1] . "\n");
397
			}
398
		}
399
	}
400
	$pkgaddout = "";
401
	exec("cat {$g['tmp_path']}/y | /usr/sbin/pkg_add -fv {$fetchto} 2>&1", $pkgaddout);
402
	@fwrite($fd_log, $pkgname . " " . print_r($pkgaddout, true) . "\n");
403
	return true;
404 8c6516d1 Colin Smith
}
405
406 7597c8e8 Colin Smith
function install_package($package, $pkg_info = "") {
407 20e593fa Scott Ullrich
	global $g, $config, $pkg_interface, $fd_log, $static_output, $pkg_interface, $restart_sync;
408 dbef849d Scott Ullrich
	if($pkg_interface == "console") 	
409
		echo "\n";
410 7597c8e8 Colin Smith
	/* open logfiles and begin installation */
411
	if(!$fd_log) {
412 669e1adb Bill Marquette
		if(!$fd_log = fopen("{$g['tmp_path']}/pkg_mgr_{$package}.log", "w")) {
413 7597c8e8 Colin Smith
			update_output_window("Warning, could not open log for writing.");
414
		}
415
	}
416
	/* fetch package information if needed */
417
	if(!$pkg_info or !is_array($pkg_info[$package])) {
418
		$pkg_info = get_pkg_info(array($package));
419
		$pkg_info = $pkg_info[$package]; // We're only dealing with one package, so we can strip away the extra array.
420
	}
421 761902b0 Scott Ullrich
	@fwrite($fd_log, "Beginning package installation.\n");
422
	log_error('Beginning package installation for ' . $pkg_info['name'] . '.');
423
	update_status("Beginning package installation for " . $pkg_info['name'] . "...");	
424 7597c8e8 Colin Smith
	/* fetch the package's configuration file */
425
	if($pkg_info['config_file'] != "") {
426
		$static_output .= "Downloading package configuration file... ";
427
		update_output_window($static_output);
428
		@fwrite($fd_log, "Downloading package configuration file...\n");
429
		$fetchto = substr(strrchr($pkg_info['config_file'], '/'), 1);
430
		download_file_with_progress_bar($pkg_info['config_file'], '/usr/local/pkg/' . $fetchto);
431
		if(!file_exists('/usr/local/pkg/' . $fetchto)) {
432
			@fwrite($fd_log, "ERROR! Unable to fetch package configuration file. Aborting installation.\n");
433
			if($pkg_interface == "console") {
434
				print "\nERROR! Unable to fetch package configuration file. Aborting package installation.\n";
435
				return;
436
			} else {
437
				$static_output .= "failed!\n\nInstallation aborted.";
438
				update_output_window($static_output);
439
				echo "<br>Show <a href=\"pkg_mgr_install.php?showlog=true\">install log</a></center>";
440 f9b205f3 Scott Ullrich
			 	return -1;
441 7597c8e8 Colin Smith
			}
442
		}
443
		$static_output .= "done.\n";
444
		update_output_window($static_output);
445
	}
446
	/* add package information to config.xml */
447
	$pkgid = get_pkg_id($pkg_info['name']);
448
	$static_output .= "Saving updated package information... ";
449
	update_output_window($static_output);
450
	if($pkgid == -1) {
451
		$config['installedpackages']['package'][] = $pkg_info;
452
		$changedesc = "Installed {$pkg_info['name']} package.";
453
		$to_output = "done.\n";
454
	} else {
455
		$config['installedpackages']['package'][$pkgid] = $pkg_info;
456
		$changedesc = "Overwrote previous installation of {$pkg_info['name']}.";
457
		$to_output = "overwrite!\n";
458
	}
459
	$static_output .= $to_output;
460
	update_output_window($static_output);
461
	/* install other package components */
462
	install_package_xml($package);
463 6ae2ac8d Colin Smith
	$static_output .= "Writing configuration... ";
464
	update_output_window($static_output);
465 7597c8e8 Colin Smith
	write_config($changedesc);
466 01fd7394 Scott Ullrich
	$static_output .= "done.\n";
467 6ae2ac8d Colin Smith
	update_output_window($static_output);
468 01fd7394 Scott Ullrich
	$static_output .= "Starting service.\n";
469 43db85f8 Scott Ullrich
	update_output_window($static_output);
470 02d6d72e Scott Ullrich
	if($pkg_info['after_install_info']) 
471
		update_output_window($pkg_info['after_install_info']);	
472 43db85f8 Scott Ullrich
	start_service($pkg_info['config_file']);
473 20e593fa Scott Ullrich
	$restart_sync = true;
474 7597c8e8 Colin Smith
}
475
476 cfde64b8 Scott Ullrich
function get_after_install_info($package) {
477
	global $pkg_info;
478
	/* fetch package information if needed */
479
	if(!$pkg_info or !is_array($pkg_info[$package])) {
480
		$pkg_info = get_pkg_info(array($package));
481
		$pkg_info = $pkg_info[$package]; // We're only dealing with one package, so we can strip away the extra array.
482
	}
483
	if($pkg_info['after_install_info'])
484
		return $pkg_info['after_install_info'];
485
}
486
487 2a0e6517 Colin Smith
function eval_once($toeval) {
488
	global $evaled;
489 57965588 Colin Smith
	if(!$evaled) $evaled = array();
490 2a0e6517 Colin Smith
	$evalmd5 = md5($toeval);
491
	if(!in_array($evalmd5, $evaled)) {
492
		eval($toeval);
493
		$evaled[] = $evalmd5;
494
	}
495
	return;
496
}
497
498 7597c8e8 Colin Smith
function install_package_xml($pkg) {
499 1a22ffcd Scott Ullrich
	global $g, $config, $fd_log, $static_output, $pkg_interface;
500 7597c8e8 Colin Smith
	if(($pkgid = get_pkg_id($pkg)) == -1) {
501
		$static_output .= "The {$pkg} package is not installed.\n\nInstallation aborted.";
502
		update_output_window($static_output);
503 1a22ffcd Scott Ullrich
		if($pkg_interface <> "console") {
504
			echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
505
			echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
506
		}
507 7597c8e8 Colin Smith
		sleep(1);
508 3f01fe47 Colin Smith
		return;
509 7597c8e8 Colin Smith
	} else {
510
		$pkg_info = $config['installedpackages']['package'][$pkgid];
511
	}
512
	/* set up logging if needed */
513
	if(!$fd_log) {
514 407bf67a Colin Smith
		if(!$fd_log = fopen("{$g['tmp_path']}/pkg_mgr_{$pkg}.log", "w")) {
515 7597c8e8 Colin Smith
			update_output_window("Warning, could not open log for writing.");
516
		}
517
	}
518
519 a6d0d461 Colin Smith
	/* set up package logging streams */
520 e65a287f Scott Ullrich
	if($pkg_info['logging']) {
521 57ecd9b6 Scott Ullrich
		mwexec("/usr/sbin/fifolog_create -s 32768 {$g['varlog_path']}/{$pkg_info['logging']['logfilename']}");
522 e65a287f Scott Ullrich
		chmod($g['varlog_path'] . '/' . $pkg_info['logging']['logfilename'], 0600);
523
		@fwrite($fd_log, "Adding text to file /etc/syslog.conf\n");
524 938ad5f2 Scott Ullrich
		mwexec("killall syslogd");
525 e65a287f Scott Ullrich
		system_syslogd_start();
526
	}
527 a6d0d461 Colin Smith
528
	/* make 'y' file */
529 e65a287f Scott Ullrich
	$fd = fopen("{$g['tmp_path']}/y", "w");
530
	for($line = 0; $line < 10; $line++) {
531
		fwrite($fd, "y\n");
532
	}
533
	fclose($fd);
534 a6d0d461 Colin Smith
535
	/* pkg_add the package and its dependencies */
536 e65a287f Scott Ullrich
	if($pkg_info['depends_on_package_base_url'] != "") {
537 dbef849d Scott Ullrich
		if($pkg_interface == "console") 
538
			echo "\n";
539 e65a287f Scott Ullrich
		update_status("Installing " . $pkg_info['name'] . " and its dependencies.");
540
		$static_output .= "Downloading " . $pkg_info['name'] . " and its dependencies... ";
541
		$static_orig = $static_output;
542
		$static_output .= "\n";
543
		update_output_window($static_output);
544
		foreach((array) $pkg_info['depends_on_package'] as $pkgdep) {
545
			$pkg_name = substr(reverse_strrchr($pkgdep, "."), 0, -1);
546
			if(isset($pkg_info['skip_install_checks'])) {
547
				$pkg_installed = true;
548
			} else {
549
				$pkg_installed = is_freebsd_pkg_installed($pkg_name);
550
			}
551
			if($pkg_installed == false) pkg_fetch_recursive($pkg_name, $pkgdep, 0, $pkg_info['depends_on_package_base_url']);
552
			$static_output = $static_orig . "done.\nChecking for successful package installation... ";
553
			update_output_window($static_output);
554
			/* make sure our package was successfully installed */
555
			if($pkg_installed == false) $pkg_installed = is_freebsd_pkg_installed($pkg_name);
556
			if($pkg_installed == true) {
557
				$static_output .= "done.\n";
558
				update_output_window($static_output);
559
				fwrite($fd_log, "pkg_add successfully completed.\n");
560
			} else {
561
				$static_output .= "failed!\n\nInstallation aborted.";
562
				update_output_window($static_output);
563
				fwrite($fd_log, "Package WAS NOT installed properly.\n");
564
				fclose($fd_log);
565 1a22ffcd Scott Ullrich
				if($pkg_interface <> "console") {
566
					echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
567
					echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
568
				}
569 e65a287f Scott Ullrich
				sleep(1);
570
				die;
571
			}
572
		}
573
	}
574 7597c8e8 Colin Smith
	$configfile = substr(strrchr($pkg_info['config_file'], '/'), 1);
575
	if(file_exists("/usr/local/pkg/" . $configfile)) {
576
		$static_output .= "Loading package configuration... ";
577
		update_output_window($static_output);
578 43db85f8 Scott Ullrich
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $configfile, "packagegui");
579 7597c8e8 Colin Smith
		$static_output .= "done.\n";
580
		update_output_window($static_output);
581
		$static_output .= "Configuring package components...\n";
582 8dee24a6 Ermal Lu?i
		if (!empty($pkg_config['filter_rules_needed']))
583 bc771948 Ermal Lu?i
			$config['installedpackages']['package'][$pkgid]['filter_rule_function'] = $pkg_config['filter_rules_needed'];
584 7597c8e8 Colin Smith
		update_output_window($static_output);
585
		/* modify system files */
586
		if($pkg_config['modify_system']['item'] <> "") {
587
			$static_output .= "\tSystem files... ";
588
			update_output_window($static_output);
589
			foreach($pkg_config['modify_system']['item'] as $ms) {
590
				if($ms['textneeded']) {
591
					add_text_to_file($ms['modifyfilename'], $ms['textneeded']);
592
				}
593
			}
594
			$static_output .= "done.\n";
595
			update_output_window($static_output);
596
		}
597
		/* download additional files */
598
		if($pkg_config['additional_files_needed'] <> "") {
599
			$static_output .= "\tAdditional files... ";
600
			$static_orig = $static_output;
601
			update_output_window($static_output);
602
			foreach($pkg_config['additional_files_needed'] as $afn) {
603
				$filename = get_filename_from_url($afn['item'][0]);
604
				if($afn['chmod'] <> "") {
605
					$pkg_chmod = $afn['chmod'];
606
				} else {
607
					$pkg_chmod = "";
608
				}
609
				if($afn['prefix'] <> "") {
610
					$prefix = $afn['prefix'];
611
				} else {
612
					$prefix = "/usr/local/pkg/";
613
				}
614 e6d436e8 Scott Ullrich
				if(!is_dir($prefix)) 
615
					safe_mkdir($prefix);
616
 				$static_output .= $filename . " ";
617 7597c8e8 Colin Smith
                                update_output_window($static_output);
618
				download_file_with_progress_bar($afn['item'][0], $prefix . $filename);
619
				if(stristr($filename, ".tgz") <> "") {
620
					fwrite($fd_log, "Extracting tarball to -C for " . $filename . "...\n");
621 e65a287f Scott Ullrich
					$tarout = "";
622 c1312033 Colin Smith
					exec("/usr/bin/tar xvzf " . $prefix . $filename . " -C / 2>&1", $tarout);
623 7597c8e8 Colin Smith
					fwrite($fd_log, print_r($tarout, true) . "\n");
624
				}
625
				if($pkg_chmod <> "") {
626
					fwrite($fd_log, "Changing file mode to {$pkg_chmod} for {$prefix}{$filename}\n");
627
					chmod($prefix . $filename, $pkg_chmod);
628
					system("/bin/chmod {$pkg_chmod} {$prefix}{$filename}");
629
				}
630
				$static_output = $static_orig;
631
                                update_output_window($static_output);
632
			}
633
			$static_output .= "done.\n";
634
			update_output_window($static_output);
635
		}
636 2df5cb99 Scott Ullrich
		/*   if a require exists, include it.  this will
637
		 *   show us where an error exists in a package
638
		 *   instead of making us blindly guess
639
		 */
640
		if($pkg_config['include_file'] <> "") {
641
			$static_output = "Loading package instructions...";
642
			update_output_window($static_output);
643
			fwrite($fd_log, "require_once('include_file')\n");
644
			require_once($pkg_config['include_file']);
645 43db85f8 Scott Ullrich
		}
646 7597c8e8 Colin Smith
		/* sidebar items */
647
		if($pkg_config['menu'] != "") {
648
			$static_output .= "\tMenu items... ";
649
			update_output_window($static_output);
650 0348a68a Scott Ullrich
			if(is_array($pkg_config['menu'])) {
651
				foreach($pkg_config['menu'] as $menu) {
652 b63f2e8b Matthew Grooms
					if(is_array($config['installedpackages']['menu']))
653
						foreach($config['installedpackages']['menu'] as $amenu)
654
							if($amenu['name'] == $menu['name'])
655
								continue 2;
656 0348a68a Scott Ullrich
					$config['installedpackages']['menu'][] = $menu;
657 740f2f90 Colin Smith
				}
658 7597c8e8 Colin Smith
			}
659
			$static_output .= "done.\n";
660
			update_output_window($static_output);
661
		}
662 b63f2e8b Matthew Grooms
		/* integrated tab items */
663
		if($pkg_config['tabs'] != "") {
664
			$static_output .= "\tIntegrated Tab items... ";
665
			update_output_window($static_output);
666
			if(is_array($pkg_config['tabs']['tab'])) {
667
				foreach($pkg_config['tabs']['tab'] as $tab) {
668
					if(is_array($config['installedpackages']['tab']))
669
						foreach($config['installedpackages']['tab'] as $atab)
670
							if($atab['name'] == $tab['name'])
671
								continue 2;
672
					$config['installedpackages']['tab'][] = $tab;
673
				}
674
			}
675
			$static_output .= "done.\n";
676
			update_output_window($static_output);
677
		}
678 2dc264a4 Colin Smith
		/* services */
679
		if($pkg_config['service'] != "") {
680
			$static_output .= "\tServices... ";
681
			update_output_window($static_output);
682
			foreach($pkg_config['service'] as $service) {
683
				$config['installedpackages']['service'][] = $service;
684
			}
685
			$static_output .= "done.\n";
686
			update_output_window($static_output);
687
		}
688 7597c8e8 Colin Smith
		/* custom commands */
689 997c3b7a Colin Smith
		$static_output .= "\tCustom commands... ";
690
		update_output_window($static_output);
691
		if($pkg_config['custom_php_global_functions'] <> "") {
692 219b8cc0 Scott Ullrich
			$static_output = "Executing custom_php_global_functions()...";
693 43db85f8 Scott Ullrich
			update_output_window($static_output);
694 997c3b7a Colin Smith
			eval_once($pkg_config['custom_php_global_functions']);
695 7597c8e8 Colin Smith
		}
696 997c3b7a Colin Smith
		if($pkg_config['custom_php_install_command']) {
697 219b8cc0 Scott Ullrich
			$static_output = "Executing custom_php_install_command()...";
698
			update_output_window($static_output);
699 997c3b7a Colin Smith
			eval_once($pkg_config['custom_php_install_command']);
700 33ef3f7c Scott Ullrich
		}
701 149dbafd Scott Ullrich
		if($pkg_config['custom_php_resync_config_command'] <> "") {
702 219b8cc0 Scott Ullrich
			$static_output = "Executing custom_php_resync_config_command()...";
703
			update_output_window($static_output);
704 e65a287f Scott Ullrich
			eval_once($pkg_config['custom_php_resync_config_command']);
705
		}
706 997c3b7a Colin Smith
		$static_output .= "done.\n";
707
		update_output_window($static_output);
708 7597c8e8 Colin Smith
	} else {
709
		$static_output .= "Loading package configuration... failed!\n\nInstallation aborted.";
710
		update_output_window($static_output);
711
		fwrite($fd_log, "Unable to load package configuration. Installation aborted.\n");
712 e65a287f Scott Ullrich
		fclose($fd_log);
713 1a22ffcd Scott Ullrich
		if($pkg_interface <> "console") {
714
			echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
715
			echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
716
		}
717 e65a287f Scott Ullrich
		sleep(1);
718 3f01fe47 Colin Smith
		return;
719 7597c8e8 Colin Smith
	}
720
}
721 407bf67a Colin Smith
722 219b8cc0 Scott Ullrich
function delete_package($pkg, $pkgid) {
723 62c55268 Colin Smith
	global $g, $config, $fd_log, $static_output;
724
	update_status("Removing package...");
725
	$static_output .= "Removing package... ";
726
	update_output_window($static_output);
727 219b8cc0 Scott Ullrich
	$pkgid = get_pkg_id($pkgid);
728
	$pkg_info = $config['installedpackages']['package'][$pkgid];
729 ca9b25aa Scott Ullrich
730 219b8cc0 Scott Ullrich
	$configfile = $pkg_info['configurationfile'];
731
	if(file_exists("/usr/local/pkg/" . $configfile)) {
732
		$static_output .= "\nLoading package configuration $configfile... ";
733
		update_output_window($static_output);
734
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $configfile, "packagegui");
735
		/*   if a require exists, include it.  this will
736
		 *   show us where an error exists in a package
737
		 *   instead of making us blindly guess
738
		 */
739
		if($pkg_config['include_file'] <> "") {
740
			$static_output .= "\nLoading package instructions...\n";
741
			update_output_window($static_output);
742
			require_once($pkg_config['include_file']);
743
		}
744
	}
745 9eeef922 Scott Ullrich
	$static_output .= "\nStarting package deletion for {$pkg_info['name']}...\n";
746 43db85f8 Scott Ullrich
	update_output_window($static_output);
747 62c55268 Colin Smith
	delete_package_recursive($pkg);
748 9eeef922 Scott Ullrich
	remove_freebsd_package($pkg);
749 62c55268 Colin Smith
	$static_output .= "done.\n";
750
	update_output_window($static_output);
751
	return;
752
}
753
754 249a6b95 Colin Smith
function delete_package_recursive($pkg) {
755 5daf1708 Scott Ullrich
	global $config, $g;
756 1af187a1 Scott Ullrich
	$fd = fopen("{$g['tmp_path']}/y", "w");
757
	for($line = 0; $line < 10; $line++) {
758
		fwrite($fd, "y\n");
759
	}
760
	fclose($fd);
761 e65a287f Scott Ullrich
	$info = "";
762 62c55268 Colin Smith
	exec("/usr/sbin/pkg_info -r " . $pkg . " 2>&1", $info);
763 249a6b95 Colin Smith
	exec("cat {$g['tmp_path']}/y | /usr/sbin/pkg_delete " . $pkg ." > /dev/null 2>&1");
764 9eeef922 Scott Ullrich
	remove_freebsd_package($pkg);
765 e65a287f Scott Ullrich
	$pkgdb = "";
766 249a6b95 Colin Smith
	exec("/bin/ls /var/db/pkg", $pkgdb);
767 407bf67a Colin Smith
	foreach($info as $line) {
768 249a6b95 Colin Smith
		$depend = trim(array_pop(explode(":", $line)));
769 cc3087bd Scott Ullrich
		if(in_array($depend, $pkgdb)) 
770
			delete_package_recursive($depend);
771 407bf67a Colin Smith
	}
772
	return;
773
}
774
775
function delete_package_xml($pkg) {
776 1a22ffcd Scott Ullrich
	global $g, $config, $fd_log, $static_output, $pkg_interface;
777 407bf67a Colin Smith
	if(($pkgid = get_pkg_id($pkg)) == -1) {
778 e65a287f Scott Ullrich
		$static_output .= "The {$pkg} package is not installed.\n\nDeletion aborted.";
779
		update_output_window($static_output);
780 1a22ffcd Scott Ullrich
		if($pkg_interface <> "console") {
781
			echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
782
			echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
783
		}
784 e65a287f Scott Ullrich
		ob_flush();
785
		sleep(1);
786
		return;
787
	}
788 407bf67a Colin Smith
	/* set up logging if needed */
789 e65a287f Scott Ullrich
	if(!$fd_log) {
790
		if(!$fd_log = fopen("{$g['tmp_path']}/pkg_mgr_{$pkg}.log", "w")) {
791
			update_output_window("Warning, could not open log for writing.");
792
		}
793
	}
794 62c55268 Colin Smith
	update_status("Removing {$pkg} components...");
795 407bf67a Colin Smith
	fwrite($fd_log, "Removing {$pkg} package... ");
796 62c55268 Colin Smith
	$static_output .= "Removing {$pkg} components...\n";
797 407bf67a Colin Smith
	update_output_window($static_output);
798
	/* parse package configuration */
799
	$packages = &$config['installedpackages']['package'];
800 b63f2e8b Matthew Grooms
	$tabs =& $config['installedpackages']['tab'];
801
	$menus =& $config['installedpackages']['menu'];
802 3c41c4ab Colin Smith
	$services = &$config['installedpackages']['service'];
803 b783468f Colin Smith
	if(file_exists("/usr/local/pkg/" . $packages[$pkgid]['configurationfile'])) {
804 19a11678 Colin Smith
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $packages[$pkgid]['configurationfile'], "packagegui");
805 b63f2e8b Matthew Grooms
		/* remove tab items */
806
		if(is_array($pkg_config['tabs'])) {
807
			$static_output .= "\tMenu items... ";
808
			update_output_window($static_output);
809 5274feb0 Scott Ullrich
			if(is_array($tabs))
810 8d582d67 Scott Ullrich
				foreach($tabs as $tab)
811 5274feb0 Scott Ullrich
					$insttabs[] = $tab['name'];
812
			if($pkg_config['tabs']['tab'])
813
				foreach($pkg_config['tabs']['tab'] as $tab)
814
					foreach($insttabs as $key => $insttab)
815
						if($insttab == $tab['name'])
816
							unset($tabs[$key]);
817 b63f2e8b Matthew Grooms
			$static_output .= "done.\n";
818
			update_output_window($static_output);
819
		}
820 3f01fe47 Colin Smith
		/* remove menu items */
821
		if(is_array($pkg_config['menu'])) {
822
			$static_output .= "\tMenu items... ";
823
			update_output_window($static_output);
824 b63f2e8b Matthew Grooms
			foreach($menus as $menu)
825
				$instmenus[] = $menu['name'];
826
			foreach($pkg_config['menu'] as $menu)
827
				foreach($instmenus as $key => $instmenu)
828
					if($instmenu == $menu['name'])
829
						unset($menus[$key]);
830 3f01fe47 Colin Smith
			$static_output .= "done.\n";
831
			update_output_window($static_output);
832 407bf67a Colin Smith
		}
833 3c41c4ab Colin Smith
		/* remove services */
834
		if(is_array($pkg_config['service'])) {
835
			$static_output .= "\tServices... ";
836
			update_output_window($static_output);
837
			foreach($services as $service) $instservices[] = $service['name'];
838
			foreach($pkg_config['service'] as $service) {
839
				foreach($instservices as $key => $instservice) {
840 0cab7cad Colin Smith
					if($instservice == $service['name']) {
841
						stop_service($service['name']);
842
						unset($services[$key]);
843
					}
844 3c41c4ab Colin Smith
				}
845
			}
846
			$static_output .= "done.\n";
847
			update_output_window($static_output);
848
		}
849 892aef15 Scott Ullrich
		/*   if a require exists, include it.  this will
850
		 *   show us where an error exists in a package
851
		 *   instead of making us blindly guess
852
		 */
853
		if($pkg_config['include_file'] <> "") {
854
			$static_output = "Loading package instructions...";
855
			update_output_window($static_output);
856
			fwrite($fd_log, "require_once('include_file')\n");
857 2c21de57 Scott Ullrich
			if(file_exists($pkg_config['include_file']))
858
				require_once($pkg_config['include_file']);
859 892aef15 Scott Ullrich
			fwrite($fd_log, "require_once('include_file') included\n");
860 43db85f8 Scott Ullrich
		}
861 3f01fe47 Colin Smith
		/* evalate this package's global functions and pre deinstall commands */
862
		if($pkg_config['custom_php_global_functions'] <> "")
863 2a0e6517 Colin Smith
			eval_once($pkg_config['custom_php_global_functions']);
864 43db85f8 Scott Ullrich
		if($pkg_config['custom_php_pre_deinstall_command'] <> "")
865 2a0e6517 Colin Smith
			eval_once($pkg_config['custom_php_pre_deinstall_command']);
866 3f01fe47 Colin Smith
		/* remove all additional files */
867
		if($pkg_config['additional_files_needed'] <> "") {
868
			$static_output .= "\tAuxiliary files... ";
869
			update_output_window($static_output);
870
			foreach($pkg_config['additional_files_needed'] as $afn) {
871
				$filename = get_filename_from_url($afn['item'][0]);
872
				if($afn['prefix'] <> "") {
873
					$prefix = $afn['prefix'];
874
				} else {
875
					$prefix = "/usr/local/pkg/";
876
				}
877
				unlink_if_exists($prefix . $filename);
878 05ac89d8 Scott Ullrich
				if(file_exists($prefix . $filename))
879
				    mwexec("rm -rf {$prefix}{$filename}");
880 3f01fe47 Colin Smith
			}
881
			$static_output .= "done.\n";
882
			update_output_window($static_output);
883
		}
884
		/* system files */
885
		if($pkg_config['modify_system']['item'] <> "") {
886
			$static_output .= "\tSystem files... ";
887
			update_output_window($static_output);
888
			foreach($pkg_config['modify_system']['item'] as $ms) {
889
				if($ms['textneeded']) remove_text_from_file($ms['modifyfilename'], $ms['textneeded']);
890 407bf67a Colin Smith
			}
891 3f01fe47 Colin Smith
			$static_output .= "done.\n";
892
			update_output_window($static_output);
893 407bf67a Colin Smith
		}
894 3f01fe47 Colin Smith
		/* syslog */
895
		if($pkg_config['logging']['logfile_name'] <> "") {
896
			$static_output .= "\tSyslog entries... ";
897
			update_output_window($static_output);
898
			remove_text_from_file("/etc/syslog.conf", $pkg_config['logging']['facilityname'] . "\t\t\t\t" . $pkg_config['logging']['logfilename']);
899
			$static_output .= "done.\n";
900
			update_output_window($static_output);
901 407bf67a Colin Smith
		}
902 644d2d59 Colin Smith
		/* deinstall commands */
903
		if($pkg_config['custom_php_deinstall_command'] <> "") {
904
			$static_output .= "\tDeinstall commands... ";
905
			update_output_window($static_output);
906 2a0e6517 Colin Smith
			eval_once($pkg_config['custom_php_deinstall_command']);
907 644d2d59 Colin Smith
			$static_output .= "done.\n";
908
			update_output_window($static_output);
909
		}
910 047c40c4 Colin Smith
		/* package XML file */
911
		$static_output .= "\tPackage XML... ";
912
		update_output_window($static_output);
913
		unlink_if_exists("/usr/local/pkg/" . $packages[$pkgid]['configurationfile']);
914
		$static_output .= "done.\n";
915
		update_output_window($static_output);
916 407bf67a Colin Smith
	}
917
	/* remove config.xml entries */
918
	$static_output .= "\tConfiguration... ";
919
	update_output_window($static_output);
920
	unset($config['installedpackages']['package'][$pkgid]);
921
	$static_output .= "done.\n";
922
	update_output_window($static_output);
923 62c55268 Colin Smith
	write_config("Removed {$pkg} package.");
924 407bf67a Colin Smith
	/* file cleanup */
925
	$ctag = file("/etc/crontab");
926
	foreach($ctag as $line) {
927
		if(trim($line) != "") $towrite[] = $line;
928
	}
929
	$tmptab = fopen("/tmp/crontab", "w");
930
	foreach($towrite as $line) {
931
		fwrite($tmptab, $line);
932
	}
933
	fclose($tmptab);
934
	rename("/tmp/crontab", "/etc/crontab");
935
}
936 566181ea Colin Smith
937
function expand_to_bytes($size) {
938
	$conv = array(
939
			"G" =>	"3",
940
			"M" =>  "2",
941
			"K" =>  "1",
942
			"B" =>  "0"
943
		);
944
	$suffix = substr($size, -1);
945
	if(!in_array($suffix, array_keys($conv))) return $size;
946
	$size = substr($size, 0, -1);
947
	for($i = 0; $i < $conv[$suffix]; $i++) {
948
		$size *= 1024;
949
	}
950
	return $size;
951
}
952
953
function get_pkg_db() {
954
	global $g;
955
	return return_dir_as_array($g['vardb_path'] . '/pkg');
956
}
957
958 b8a1c2a3 Colin Smith
function walk_depend($depend, $pkgdb = "", $alreadyseen = "") {
959 566181ea Colin Smith
	if(!$pkgdb) $pkgdb = get_pkg_db();
960 b8a1c2a3 Colin Smith
	if(!$alreadyseen) $alreadyseen = array();
961 566181ea Colin Smith
	foreach($depend as $adepend) {
962
		$pkgname = reverse_strrchr($adepend['name'], '.');
963 b8a1c2a3 Colin Smith
		if(in_array($pkgname, $alreadyseen)) {
964
			continue;
965
		} elseif(!in_array($pkgname, $pkgdb)) {
966
			$size += expand_to_bytes($adepend['size']);
967
			$alreadyseen[] = $pkgname;
968
			if(is_array($adepend['depend'])) $size += walk_depend($adepend['depend'], $pkgdb, $alreadyseen);
969
		} else {
970
			continue;
971
		}
972 566181ea Colin Smith
	}
973
	return $size;
974
}
975
976
function get_package_install_size($pkg = 'all', $pkg_info = "") {
977
	global $config, $g;
978
	if((!is_array($pkg)) and ($pkg != 'all')) $pkg = array($pkg);
979
	$pkgdb = get_pkg_db();
980
	if(!$pkg_info) $pkg_info = get_pkg_sizes($pkg);
981
	foreach($pkg as $apkg) {
982
		if(!$pkg_info[$apkg]) continue;
983 b8a1c2a3 Colin Smith
		$toreturn[$apkg] = expand_to_bytes(walk_depend(array($pkg_info[$apkg]), $pkgdb));
984 566181ea Colin Smith
	}
985 b8a1c2a3 Colin Smith
	return $toreturn;
986 566181ea Colin Smith
}
987 f0a550fd Colin Smith
988 e43ba9ad Colin Smith
function squash_from_bytes($size, $round = "") {
989 f0a550fd Colin Smith
	$conv = array(1 => "B", "K", "M", "G");
990
	foreach($conv as $div => $suffix) {
991
		$sizeorig = $size;
992
		if(($size /= 1024) < 1) {
993 e43ba9ad Colin Smith
			if($round) {
994
				$sizeorig = round($sizeorig, $round);
995
			}
996 f0a550fd Colin Smith
			return $sizeorig . $suffix;
997
		}
998
	}
999
	return;
1000
}
1001 5025a56c Scott Ullrich
1002 a9b2e638 Ermal Lu?i
function pkg_build_filter_rules() {
1003 07b73e3d Ermal Lu?i
	global $config, $g;
1004 a9b2e638 Ermal Lu?i
1005 b0ec358d Ermal Lu?i
	
1006 a9b2e638 Ermal Lu?i
	$pkgrules = "";
1007 07b73e3d Ermal Lu?i
        $pkgrulesearly = "";
1008
        $pkgnatrules = "";
1009
        $pkgnatrulesearly = "";
1010
        $pkgrdrrules = "";
1011
        $pkgrdrrulesearly = "";
1012 b0ec358d Ermal Lu?i
1013 a9b2e638 Ermal Lu?i
	if (is_array($config['installedpackages']['package'])) {
1014
		run_plugins("/usr/local/pkg");
1015
                foreach($config['installedpackages']['package'] as $pkg) {
1016
                        if (!isset($pkg['filter_rule_function']))
1017
				continue;
1018 a6d821bc Ermal Lu?i
			$tmpresult = call_user_func($pkg['filter_rule_function'], 'natearly');
1019
                        if (!empty($tmpresult))
1020
                                $pkgnatrulesearly .= $tmpresult . " \n";
1021 a9b2e638 Ermal Lu?i
			$tmpresult = call_user_func($pkg['filter_rule_function'], 'nat');
1022
			if (!empty($tmpresult))
1023
				$pkgnatrules .= $tmpresult . " \n";
1024 a6d821bc Ermal Lu?i
			$tmpresult = call_user_func($pkg['filter_rule_function'], 'ruleearly');
1025
                        if (!empty($tmpresult))
1026
                                $pkgrulesearly .= $tmpresult . " \n";
1027 a9b2e638 Ermal Lu?i
			$tmpresult = call_user_func($pkg['filter_rule_function'], 'rule');
1028
			if (!empty($tmpresult))
1029
				$pkgrules .= $tmpresult . " \n";
1030 a6d821bc Ermal Lu?i
			$tmpresult = call_user_func($pkg['filter_rule_function'], 'rdrearly');
1031
                        if (!empty($tmpresult))
1032
                                $pkgrdrrulesearly .= $tmpresult . " \n";
1033
			$tmpresult = call_user_func($pkg['filter_rule_function'], 'rdr');
1034
                        if (!empty($tmpresult))
1035
                                $pkgrdrrules .= $tmpresult . " \n";
1036 a9b2e638 Ermal Lu?i
			
1037
                }
1038
        }
1039 ada51839 Ermal Lu?i
        file_put_contents("{$g['tmp_path']}/rules.natearly.packages", $pkgnatrulesearly);
1040
	file_put_contents("{$g['tmp_path']}/rules.nat.packages", $pkgnatrules);
1041
	file_put_contents("{$g['tmp_path']}/rules.rulesearly.packages", $pkgrulesearly);
1042
        file_put_contents("{$g['tmp_path']}/rules.packages", $pkgrules);
1043
        file_put_contents("{$g['tmp_path']}/rules.rdr.packages", $pkgrdrrules);
1044
        file_put_contents("{$g['tmp_path']}/rules.rdrearly.packages", $pkgrdrrulesearly);
1045 a9b2e638 Ermal Lu?i
	$error = "";
1046 a6d821bc Ermal Lu?i
	$status = mwexec("/sbin/pfctl -nf {$g['tmp_path']}/rules.natearly.packages");
1047
        if ($status <> 0) {
1048 6bb91286 Ermal Lu?i
                $errornatearly = "There was an error while parsing the NAT early package rules.\n";
1049
		$error .= $errornatearly;
1050
                log_error($errornatearly);
1051
		file_put_contents("{$g['tmp_path']}/rules.natearly.packages", "#{$errornatearly}\n");
1052 a6d821bc Ermal Lu?i
        }
1053 a9b2e638 Ermal Lu?i
	$status = mwexec("/sbin/pfctl -nf {$g['tmp_path']}/rules.nat.packages");
1054 a6d821bc Ermal Lu?i
        if ($status <> 0) {
1055 6bb91286 Ermal Lu?i
                $errornat = "There was an error while parsing the NAT package rules.\n";
1056
		$error .= $errornat;
1057
		log_error($errornat);
1058
                file_put_contents("{$g['tmp_path']}/rules.nat.packages", "#{$errornat}\n");
1059 a6d821bc Ermal Lu?i
        }
1060
	$status = mwexec("/sbin/pfctl -nf {$g['tmp_path']}/rules.rulesearly.packages");
1061 a9b2e638 Ermal Lu?i
	if ($status <> 0) {
1062 6bb91286 Ermal Lu?i
		$errorrulesearly = "There was an error while parsing the package filter early rules.\n";
1063
		$error .= $errorrulesearly;
1064
		log_error($errorrulesearly);
1065
                file_put_contents("{$g['tmp_path']}/rules.rulesearly.packages", "#{$errorrulesearly}\n");
1066 a9b2e638 Ermal Lu?i
	}
1067
	$status = mwexec("/sbin/pfctl -nf {$g['tmp_path']}/rules.packages");
1068
	if ($status <> 0) {
1069 6bb91286 Ermal Lu?i
		$errorrules = "There was an error while parsing the package filter rules.\n";
1070
		$error .= $errorrules;
1071
		log_error($errorrules);
1072
                file_put_contents("{$g['tmp_path']}/rules.packages", "#{$errorrules}\n");
1073 a6d821bc Ermal Lu?i
        }
1074
	$status = mwexec("/sbin/pfctl -nf {$g['tmp_path']}/rules.rdrearly.packages");
1075
        if ($status <> 0) {
1076 6bb91286 Ermal Lu?i
                $errorrdrearly = "There was an error while parsing the RDR early package rules.\n";
1077
		$error .= $errorrdrearly;
1078
		log_error($errorrdrearly);
1079
                file_put_contents("{$g['tmp_path']}/rules.rdrearly.packages", "#{$errorrdrearly}\n");
1080 a6d821bc Ermal Lu?i
        }
1081
	$status = mwexec("/sbin/pfctl -nf {$g['tmp_path']}/rules.rdr.packages");
1082
        if ($status <> 0) {
1083 6bb91286 Ermal Lu?i
		$errorrdr = "There was an error while parsing the RDR package rules.\n";
1084
		$error .= $errorrdr;
1085
		log_error($errorrdr);
1086
                file_put_contents("{$g['tmp_path']}/rules.rdr.packages", "#{$errorrdr}\n");
1087 a9b2e638 Ermal Lu?i
        }
1088
	if ($error <> "")
1089
		file_notice($error);
1090
}
1091
1092 29c7ac0d Ermal Lu?i
?>