Project

General

Profile

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