Project

General

Profile

Download (86 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/****h* pfSense/pfsense-utils
3
 * NAME
4
 *   pfsense-utils.inc - Utilities specific to pfSense
5
 * DESCRIPTION
6
 *   This include contains various pfSense specific functions.
7
 * HISTORY
8
 *   $Id$
9
 ******
10
 *
11
 * Copyright (C) 2004-2007 Scott Ullrich (sullrich@gmail.com)
12
 * 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

    
36
/*
37
	pfSense_BUILDER_BINARIES:	/sbin/ifconfig	/sbin/pfctl	/usr/local/bin/php /usr/bin/netstat
38
	pfSense_BUILDER_BINARIES:	/bin/df	/usr/bin/grep	/usr/bin/awk	/bin/rm	/usr/sbin/pwd_mkdb	/usr/bin/host
39
	pfSense_BUILDER_BINARIES:	/sbin/kldload
40
	pfSense_MODULE:	utils
41
*/
42

    
43
/****f* pfsense-utils/have_natpfruleint_access
44
 * NAME
45
 *   have_natpfruleint_access
46
 * INPUTS
47
 *	none
48
 * RESULT
49
 *   returns true if user has access to edit a specific firewall nat port forward interface
50
 ******/
51
function have_natpfruleint_access($if) {
52
	$security_url = "firewall_nat_edit.php?if=". strtolower($if);
53
	if(isAllowedPage($security_url, $allowed))
54
		return true;
55
	return false;
56
}
57

    
58
/****f* pfsense-utils/have_ruleint_access
59
 * NAME
60
 *   have_ruleint_access
61
 * INPUTS
62
 *	none
63
 * RESULT
64
 *   returns true if user has access to edit a specific firewall interface
65
 ******/
66
function have_ruleint_access($if) {
67
	$security_url = "firewall_rules.php?if=". strtolower($if);
68
	if(isAllowedPage($security_url))
69
		return true;
70
	return false;
71
}
72

    
73
/****f* pfsense-utils/does_url_exist
74
 * NAME
75
 *   does_url_exist
76
 * INPUTS
77
 *	none
78
 * RESULT
79
 *   returns true if a url is available
80
 ******/
81
function does_url_exist($url) {
82
	$fd = fopen("$url","r");
83
	if($fd) {
84
		fclose($fd);
85
		return true;
86
	} else {
87
		return false;
88
	}
89
}
90

    
91
/****f* pfsense-utils/is_private_ip
92
 * NAME
93
 *   is_private_ip
94
 * INPUTS
95
 *	none
96
 * RESULT
97
 *   returns true if an ip address is in a private range
98
 ******/
99
function is_private_ip($iptocheck) {
100
	$isprivate = false;
101
	$ip_private_list=array(
102
		"10.0.0.0/8",
103
		"100.64.0.0/10",
104
		"172.16.0.0/12",
105
		"192.168.0.0/16",
106
	);
107
	foreach($ip_private_list as $private) {
108
		if(ip_in_subnet($iptocheck,$private)==true)
109
			$isprivate = true;
110
	}
111
	return $isprivate;
112
}
113

    
114
/****f* pfsense-utils/get_tmp_file
115
 * NAME
116
 *   get_tmp_file
117
 * INPUTS
118
 *	none
119
 * RESULT
120
 *   returns a temporary filename
121
 ******/
122
function get_tmp_file() {
123
	global $g;
124
	return "{$g['tmp_path']}/tmp-" . time();
125
}
126

    
127
/****f* pfsense-utils/get_dns_servers
128
 * NAME
129
 *   get_dns_servres - get system dns servers
130
 * INPUTS
131
 *   $dns_servers - an array of the dns servers
132
 * RESULT
133
 *   null
134
 ******/
135
function get_dns_servers() {
136
	$dns_servers = array();
137
	$dns_s = file("/etc/resolv.conf", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
138
	foreach($dns_s as $dns) {
139
		$matches = "";
140
		if (preg_match("/nameserver (.*)/", $dns, $matches))
141
			$dns_servers[] = $matches[1];
142
	}
143
	return array_unique($dns_servers);
144
}
145

    
146
function hardware_offloading_applyflags($iface) {
147
	global $config;
148

    
149
	$flags_on = 0;
150
	$flags_off = 0;
151
	$options = pfSense_get_interface_addresses($iface);
152

    
153
	if(isset($config['system']['disablechecksumoffloading'])) {
154
		if (isset($options['encaps']['txcsum']))
155
			$flags_off |= IFCAP_TXCSUM;
156
		if (isset($options['encaps']['rxcsum']))
157
			$flags_off |= IFCAP_RXCSUM;
158
	} else {
159
		if (isset($options['caps']['txcsum']))
160
			$flags_on |= IFCAP_TXCSUM;
161
		if (isset($options['caps']['rxcsum']))
162
			$flags_on |= IFCAP_RXCSUM;
163
	}
164

    
165
	if(isset($config['system']['disablesegmentationoffloading']))
166
		$flags_off |= IFCAP_TSO;
167
	else if (isset($options['caps']['tso']) || isset($options['caps']['tso4']) || isset($options['caps']['tso6']))
168
		$flags_on |= IFCAP_TSO;
169

    
170
	if(isset($config['system']['disablelargereceiveoffloading']))
171
		$flags_off |= IFCAP_LRO;
172
	else if (isset($options['caps']['lro']))
173
		$flags_on |= IFCAP_LRO;
174

    
175
	/* if the NIC supports polling *AND* it is enabled in the GUI */
176
	if (!isset($config['system']['polling']))
177
		$flags_off |= IFCAP_POLLING;
178
	else if (isset($options['caps']['polling']))
179
		$flags_on |= IFCAP_POLLING;
180

    
181
	pfSense_interface_capabilities($iface, -$flags_off);
182
	pfSense_interface_capabilities($iface, $flags_on);
183
}
184

    
185
/****f* pfsense-utils/enable_hardware_offloading
186
 * NAME
187
 *   enable_hardware_offloading - Enable a NIC's supported hardware features.
188
 * INPUTS
189
 *   $interface	- string containing the physical interface to work on.
190
 * RESULT
191
 *   null
192
 * NOTES
193
 *   This function only supports the fxp driver's loadable microcode.
194
 ******/
195
function enable_hardware_offloading($interface) {
196
	global $g, $config;
197

    
198
	$int = get_real_interface($interface);
199
	if(empty($int))
200
		return;
201

    
202
	if (!isset($config['system']['do_not_use_nic_microcode'])) {
203
		/* translate wan, lan, opt -> real interface if needed */
204
		$int_family = preg_split("/[0-9]+/", $int);
205
		$supported_ints = array('fxp');
206
		if (in_array($int_family, $supported_ints)) {
207
			if(does_interface_exist($int))
208
				pfSense_interface_flags($int, IFF_LINK0);
209
		}
210
	}
211

    
212
	/* This is mostly for vlans and ppp types */
213
	$realhwif = get_parent_interface($interface);
214
	if ($realhwif[0] == $int)
215
		hardware_offloading_applyflags($int);
216
	else {
217
		hardware_offloading_applyflags($realhwif[0]);
218
		hardware_offloading_applyflags($int);
219
	}
220
}
221

    
222
/****f* pfsense-utils/interface_supports_polling
223
 * NAME
224
 *   checks to see if an interface supports polling according to man polling
225
 * INPUTS
226
 *
227
 * RESULT
228
 *   true or false
229
 * NOTES
230
 *
231
 ******/
232
function interface_supports_polling($iface) {
233
	$opts = pfSense_get_interface_addresses($iface);
234
	if (is_array($opts) && isset($opts['caps']['polling']))
235
		return true;
236

    
237
	return false;
238
}
239

    
240
/****f* pfsense-utils/is_alias_inuse
241
 * NAME
242
 *   checks to see if an alias is currently in use by a rule
243
 * INPUTS
244
 *
245
 * RESULT
246
 *   true or false
247
 * NOTES
248
 *
249
 ******/
250
function is_alias_inuse($alias) {
251
	global $g, $config;
252

    
253
	if($alias == "") return false;
254
	/* loop through firewall rules looking for alias in use */
255
	if(is_array($config['filter']['rule']))
256
		foreach($config['filter']['rule'] as $rule) {
257
			if($rule['source']['address'])
258
				if($rule['source']['address'] == $alias)
259
					return true;
260
			if($rule['destination']['address'])
261
				if($rule['destination']['address'] == $alias)
262
					return true;
263
		}
264
	/* loop through nat rules looking for alias in use */
265
	if(is_array($config['nat']['rule']))
266
		foreach($config['nat']['rule'] as $rule) {
267
			if($rule['target'] && $rule['target'] == $alias)
268
				return true;
269
			if($rule['source']['address'] && $rule['source']['address'] == $alias)
270
				return true;
271
			if($rule['destination']['address'] && $rule['destination']['address'] == $alias)
272
				return true;
273
		}
274
	return false;
275
}
276

    
277
/****f* pfsense-utils/is_schedule_inuse
278
 * NAME
279
 *   checks to see if a schedule is currently in use by a rule
280
 * INPUTS
281
 *
282
 * RESULT
283
 *   true or false
284
 * NOTES
285
 *
286
 ******/
287
function is_schedule_inuse($schedule) {
288
	global $g, $config;
289

    
290
	if($schedule == "") return false;
291
	/* loop through firewall rules looking for schedule in use */
292
	if(is_array($config['filter']['rule']))
293
		foreach($config['filter']['rule'] as $rule) {
294
			if($rule['sched'] == $schedule)
295
				return true;
296
		}
297
	return false;
298
}
299

    
300
/****f* pfsense-utils/setup_polling
301
 * NAME
302
 *   sets up polling
303
 * INPUTS
304
 *
305
 * RESULT
306
 *   null
307
 * NOTES
308
 *
309
 ******/
310
function setup_polling() {
311
	global $g, $config;
312

    
313
	if (isset($config['system']['polling']))
314
		set_single_sysctl("kern.polling.idle_poll", "1");
315
	else
316
		set_single_sysctl("kern.polling.idle_poll", "0");
317

    
318
	if($config['system']['polling_each_burst'])
319
		set_single_sysctl("kern.polling.each_burst", $config['system']['polling_each_burst']);
320
	if($config['system']['polling_burst_max'])
321
		set_single_sysctl("kern.polling.burst_max", $config['system']['polling_burst_max']);
322
	if($config['system']['polling_user_frac'])
323
		set_single_sysctl("kern.polling.user_frac", $config['system']['polling_user_frac']);
324
}
325

    
326
/****f* pfsense-utils/setup_microcode
327
 * NAME
328
 *   enumerates all interfaces and calls enable_hardware_offloading which
329
 *   enables a NIC's supported hardware features.
330
 * INPUTS
331
 *
332
 * RESULT
333
 *   null
334
 * NOTES
335
 *   This function only supports the fxp driver's loadable microcode.
336
 ******/
337
function setup_microcode() {
338

    
339
	/* if list */
340
	$iflist = get_configured_interface_list(false, true);
341
	foreach($iflist as $if => $ifdescr)
342
		enable_hardware_offloading($if);
343
	unset($iflist);
344
}
345

    
346
/****f* pfsense-utils/get_carp_status
347
 * NAME
348
 *   get_carp_status - Return whether CARP is enabled or disabled.
349
 * RESULT
350
 *   boolean	- true if CARP is enabled, false if otherwise.
351
 ******/
352
function get_carp_status() {
353
	/* grab the current status of carp */
354
	$status = get_single_sysctl('net.inet.carp.allow');
355
	return (intval($status) > 0);
356
}
357

    
358
/*
359
 * convert_ip_to_network_format($ip, $subnet): converts an ip address to network form
360

    
361
 */
362
function convert_ip_to_network_format($ip, $subnet) {
363
	$ipsplit = explode('.', $ip);
364
	$string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet;
365
	return $string;
366
}
367

    
368
/*
369
 * get_carp_interface_status($carpinterface): returns the status of a carp ip
370
 */
371
function get_carp_interface_status($carpinterface) {
372
	$carp_query = "";
373

    
374
	/* XXX: Need to find a better way for this! */
375
	list ($interface, $vhid) = explode("_vip", $carpinterface);
376
	$interface = get_real_interface($interface);
377
	exec("/sbin/ifconfig $interface | /usr/bin/grep -v grep | /usr/bin/grep carp: | /usr/bin/grep 'vhid {$vhid}'", $carp_query);
378
	foreach($carp_query as $int) {
379
		if(stristr($int, "MASTER"))
380
			return "MASTER";
381
		if(stristr($int, "BACKUP"))
382
			return "BACKUP";
383
		if(stristr($int, "INIT"))
384
			return "INIT";
385
	}
386
	return;
387
}
388

    
389
/*
390
 * get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync
391
 */
392
function get_pfsync_interface_status($pfsyncinterface) {
393
	if (!does_interface_exist($pfsyncinterface))
394
		return;
395

    
396
	return exec_command("/sbin/ifconfig {$pfsyncinterface} | /usr/bin/awk '/pfsync:/ {print \$5}'");
397
}
398

    
399
/*
400
 * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
401
 */
402
function add_rule_to_anchor($anchor, $rule, $label) {
403
	mwexec("echo " . escapeshellarg($rule) . " | /sbin/pfctl -a " . escapeshellarg($anchor) . ":" . escapeshellarg($label) . " -f -");
404
}
405

    
406
/*
407
 * remove_text_from_file
408
 * remove $text from file $file
409
 */
410
function remove_text_from_file($file, $text) {
411
	if(!file_exists($file) && !is_writable($file))
412
		return;
413
	$filecontents = file_get_contents($file);
414
	$text = str_replace($text, "", $filecontents);
415
	@file_put_contents($file, $text);
416
}
417

    
418
/*
419
 * add_text_to_file($file, $text): adds $text to $file.
420
 * replaces the text if it already exists.
421
 */
422
function add_text_to_file($file, $text, $replace = false) {
423
	if(file_exists($file) and is_writable($file)) {
424
		$filecontents = file($file);
425
		$filecontents = array_map('rtrim', $filecontents);
426
		array_push($filecontents, $text);
427
		if ($replace)
428
			$filecontents = array_unique($filecontents);
429

    
430
		$file_text = implode("\n", $filecontents);
431

    
432
		@file_put_contents($file, $file_text);
433
		return true;
434
	}
435
	return false;
436
}
437

    
438
/*
439
 *   after_sync_bump_adv_skew(): create skew values by 1S
440
 */
441
function after_sync_bump_adv_skew() {
442
	global $config, $g;
443
	$processed_skew = 1;
444
	$a_vip = &$config['virtualip']['vip'];
445
	foreach ($a_vip as $vipent) {
446
		if($vipent['advskew'] <> "") {
447
			$processed_skew = 1;
448
			$vipent['advskew'] = $vipent['advskew']+1;
449
		}
450
	}
451
	if($processed_skew == 1)
452
		write_config(gettext("After synch increase advertising skew"));
453
}
454

    
455
/*
456
 * get_filename_from_url($url): converts a url to its filename.
457
 */
458
function get_filename_from_url($url) {
459
	return basename($url);
460
}
461

    
462
/*
463
 *   get_dir: return an array of $dir
464
 */
465
function get_dir($dir) {
466
	$dir_array = array();
467
	$d = dir($dir);
468
	while (false !== ($entry = $d->read())) {
469
		array_push($dir_array, $entry);
470
	}
471
	$d->close();
472
	return $dir_array;
473
}
474

    
475
/****f* pfsense-utils/WakeOnLan
476
 * NAME
477
 *   WakeOnLan - Wake a machine up using the wake on lan format/protocol
478
 * RESULT
479
 *   true/false - true if the operation was successful
480
 ******/
481
function WakeOnLan($addr, $mac)
482
{
483
	$addr_byte = explode(':', $mac);
484
	$hw_addr = '';
485

    
486
	for ($a=0; $a < 6; $a++)
487
		$hw_addr .= chr(hexdec($addr_byte[$a]));
488

    
489
	$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
490

    
491
	for ($a = 1; $a <= 16; $a++)
492
		$msg .= $hw_addr;
493

    
494
	// send it to the broadcast address using UDP
495
	$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
496
	if ($s == false) {
497
		log_error(gettext("Error creating socket!"));
498
		log_error(sprintf(gettext("Error code is '%1\$s' - %2\$s"), socket_last_error($s), socket_strerror(socket_last_error($s))));
499
	} else {
500
		// setting a broadcast option to socket:
501
		$opt_ret =  socket_set_option($s, 1, 6, TRUE);
502
		if($opt_ret < 0)
503
			log_error(sprintf(gettext("setsockopt() failed, error: %s"), strerror($opt_ret)));
504
		$e = socket_sendto($s, $msg, strlen($msg), 0, $addr, 2050);
505
		socket_close($s);
506
		log_error(sprintf(gettext('Magic Packet sent (%1$s) to {%2$s} MAC=%3$s'), $e, $addr, $mac));
507
		return true;
508
	}
509

    
510
	return false;
511
}
512

    
513
/*
514
 * reverse_strrchr($haystack, $needle):  Return everything in $haystack up to the *last* instance of $needle.
515
 *					 Useful for finding paths and stripping file extensions.
516
 */
517
function reverse_strrchr($haystack, $needle) {
518
	if (!is_string($haystack))
519
		return;
520
	return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
521
}
522

    
523
/*
524
 *  backup_config_section($section): returns as an xml file string of
525
 *                                   the configuration section
526
 */
527
function backup_config_section($section_name) {
528
	global $config;
529
	$new_section = &$config[$section_name];
530
	/* generate configuration XML */
531
	$xmlconfig = dump_xml_config($new_section, $section_name);
532
	$xmlconfig = str_replace("<?xml version=\"1.0\"?>", "", $xmlconfig);
533
	return $xmlconfig;
534
}
535

    
536
/*
537
 *  restore_config_section($section_name, new_contents): restore a configuration section,
538
 *                                                  and write the configuration out
539
 *                                                  to disk/cf.
540
 */
541
function restore_config_section($section_name, $new_contents) {
542
	global $config, $g;
543
	conf_mount_rw();
544
	$fout = fopen("{$g['tmp_path']}/tmpxml","w");
545
	fwrite($fout, $new_contents);
546
	fclose($fout);
547

    
548
	$xml = parse_xml_config($g['tmp_path'] . "/tmpxml", null);
549
	if ($xml['pfsense']) {
550
		$xml = $xml['pfsense'];
551
	}
552
	else if ($xml['m0n0wall']) {
553
		$xml = $xml['m0n0wall'];
554
	}
555
	if ($xml[$section_name]) {
556
		$section_xml = $xml[$section_name];
557
	} else {
558
		$section_xml = -1;
559
	}
560

    
561
	@unlink($g['tmp_path'] . "/tmpxml");
562
	if ($section_xml === -1) {
563
		return false;
564
	}
565
	$config[$section_name] = &$section_xml;
566
	if(file_exists("{$g['tmp_path']}/config.cache"))
567
		unlink("{$g['tmp_path']}/config.cache");
568
	write_config(sprintf(gettext("Restored %s of config file (maybe from CARP partner)"), $section_name));
569
	disable_security_checks();
570
	conf_mount_ro();
571
	return true;
572
}
573

    
574
/*
575
 *  merge_config_section($section_name, new_contents):   restore a configuration section,
576
 *                                                  and write the configuration out
577
 *                                                  to disk/cf.  But preserve the prior
578
 * 													structure if needed
579
 */
580
function merge_config_section($section_name, $new_contents) {
581
	global $config;
582
	conf_mount_rw();
583
	$fname = get_tmp_filename();
584
	$fout = fopen($fname, "w");
585
	fwrite($fout, $new_contents);
586
	fclose($fout);
587
	$section_xml = parse_xml_config($fname, $section_name);
588
	$config[$section_name] = $section_xml;
589
	unlink($fname);
590
	write_config(sprintf(gettext("Restored %s of config file (maybe from CARP partner)"), $section_name));
591
	disable_security_checks();
592
	conf_mount_ro();
593
	return;
594
}
595

    
596
/*
597
 * http_post($server, $port, $url, $vars): does an http post to a web server
598
 *                                         posting the vars array.
599
 * written by nf@bigpond.net.au
600
 */
601
function http_post($server, $port, $url, $vars) {
602
	$user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
603
	$urlencoded = "";
604
	while (list($key,$value) = each($vars))
605
		$urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
606
	$urlencoded = substr($urlencoded,0,-1);
607
	$content_length = strlen($urlencoded);
608
	$headers = "POST $url HTTP/1.1
609
Accept: */*
610
Accept-Language: en-au
611
Content-Type: application/x-www-form-urlencoded
612
User-Agent: $user_agent
613
Host: $server
614
Connection: Keep-Alive
615
Cache-Control: no-cache
616
Content-Length: $content_length
617

    
618
";
619

    
620
	$errno = "";
621
	$errstr = "";
622
	$fp = fsockopen($server, $port, $errno, $errstr);
623
	if (!$fp) {
624
		return false;
625
	}
626

    
627
	fputs($fp, $headers);
628
	fputs($fp, $urlencoded);
629

    
630
	$ret = "";
631
	while (!feof($fp))
632
		$ret.= fgets($fp, 1024);
633
	fclose($fp);
634

    
635
	return $ret;
636
}
637

    
638
/*
639
 *  php_check_syntax($code_tocheck, $errormessage): checks $code_to_check for errors
640
 */
641
if (!function_exists('php_check_syntax')){
642
	global $g;
643
	function php_check_syntax($code_to_check, &$errormessage){
644
		return false;
645
		$fout = fopen("{$g['tmp_path']}/codetocheck.php","w");
646
		$code = $_POST['content'];
647
		$code = str_replace("<?php", "", $code);
648
		$code = str_replace("?>", "", $code);
649
		fwrite($fout, "<?php\n\n");
650
		fwrite($fout, $code_to_check);
651
		fwrite($fout, "\n\n?>\n");
652
		fclose($fout);
653
		$command = "/usr/local/bin/php -l {$g['tmp_path']}/codetocheck.php";
654
		$output = exec_command($command);
655
		if (stristr($output, "Errors parsing") == false) {
656
			echo "false\n";
657
			$errormessage = '';
658
			return(false);
659
		} else {
660
			$errormessage = $output;
661
			return(true);
662
		}
663
	}
664
}
665

    
666
/*
667
 *  php_check_filename_syntax($filename, $errormessage): checks the file $filename for errors
668
 */
669
if (!function_exists('php_check_syntax')){
670
	function php_check_syntax($code_to_check, &$errormessage){
671
		return false;
672
		$command = "/usr/local/bin/php -l " . escapeshellarg($code_to_check);
673
		$output = exec_command($command);
674
		if (stristr($output, "Errors parsing") == false) {
675
			echo "false\n";
676
			$errormessage = '';
677
			return(false);
678
		} else {
679
			$errormessage = $output;
680
			return(true);
681
		}
682
	}
683
}
684

    
685
/*
686
 * rmdir_recursive($path,$follow_links=false)
687
 * Recursively remove a directory tree (rm -rf path)
688
 * This is for directories _only_
689
 */
690
function rmdir_recursive($path,$follow_links=false) {
691
	$to_do = glob($path);
692
	if(!is_array($to_do)) $to_do = array($to_do);
693
	foreach($to_do as $workingdir) { // Handle wildcards by foreaching.
694
		if(file_exists($workingdir)) {
695
			if(is_dir($workingdir)) {
696
				$dir = opendir($workingdir);
697
				while ($entry = readdir($dir)) {
698
					if (is_file("$workingdir/$entry") || ((!$follow_links) && is_link("$workingdir/$entry")))
699
						unlink("$workingdir/$entry");
700
					elseif (is_dir("$workingdir/$entry") && $entry!='.' && $entry!='..')
701
						rmdir_recursive("$workingdir/$entry");
702
				}
703
				closedir($dir);
704
				rmdir($workingdir);
705
			} elseif (is_file($workingdir)) {
706
				unlink($workingdir);
707
			}
708
		}
709
	}
710
	return;
711
}
712

    
713
/*
714
 * call_pfsense_method(): Call a method exposed by the pfsense.org XMLRPC server.
715
 */
716
function call_pfsense_method($method, $params, $timeout = 0) {
717
	global $g, $config;
718

    
719
	$xmlrpc_base_url = get_active_xml_rpc_base_url();
720
	$xmlrpc_path = $g['xmlrpcpath'];
721
	
722
	$xmlrpcfqdn = preg_replace("(https?://)", "", $xmlrpc_base_url);
723
	$ip = gethostbyname($xmlrpcfqdn);
724
	if($ip == $xmlrpcfqdn)
725
		return false;
726

    
727
	$msg = new XML_RPC_Message($method, array(XML_RPC_Encode($params)));
728
	$port = 0;
729
	$proxyurl = "";
730
	$proxyport = 0;
731
	$proxyuser = "";
732
	$proxypass = "";
733
	if (!empty($config['system']['proxyurl']))
734
		$proxyurl = $config['system']['proxyurl'];
735
	if (!empty($config['system']['proxyport']) && is_numeric($config['system']['proxyport']))
736
		$proxyport = $config['system']['proxyport'];
737
	if (!empty($config['system']['proxyuser']))
738
		$proxyuser = $config['system']['proxyuser'];
739
	if (!empty($config['system']['proxypass']))
740
		$proxypass = $config['system']['proxypass'];
741
	$cli = new XML_RPC_Client($xmlrpc_path, $xmlrpc_base_url, $port, $proxyurl, $proxyport, $proxyuser, $proxypass);
742
	// If the ALT PKG Repo has a username/password set, use it.
743
	if($config['system']['altpkgrepo']['username'] &&
744
	   $config['system']['altpkgrepo']['password']) {
745
		$username = $config['system']['altpkgrepo']['username'];
746
		$password = $config['system']['altpkgrepo']['password'];
747
		$cli->setCredentials($username, $password);
748
	}
749
	$resp = $cli->send($msg, $timeout);
750
	if(!is_object($resp)) {
751
		log_error(sprintf(gettext("XMLRPC communication error: %s"), $cli->errstr));
752
		return false;
753
	} elseif($resp->faultCode()) {
754
		log_error(sprintf(gettext('XMLRPC request failed with error %1$s: %2$s'), $resp->faultCode(), $resp->faultString()));
755
		return false;
756
	} else {
757
		return XML_RPC_Decode($resp->value());
758
	}
759
}
760

    
761
/*
762
 * check_firmware_version(): Check whether the current firmware installed is the most recently released.
763
 */
764
function check_firmware_version($tocheck = "all", $return_php = true) {
765
	global $g, $config;
766
	
767
	$xmlrpc_base_url = get_active_xml_rpc_base_url();
768
	$xmlrpcfqdn = preg_replace("(https?://)", "", $xmlrpc_base_url);
769
	$ip = gethostbyname($xmlrpcfqdn);
770
	if($ip == $xmlrpcfqdn)
771
		return false;
772
	$version = php_uname('r');
773
	$version = explode('-', $version);
774
	$rawparams = array("firmware" => array("version" => trim(file_get_contents('/etc/version'))),
775
		"kernel"   => array("version" => $version[0]),
776
		"base"     => array("version" => $version[0]),
777
		"platform" => trim(file_get_contents('/etc/platform')),
778
		"config_version" => $config['version']
779
		);
780
	unset($version);
781

    
782
	if($tocheck == "all") {
783
		$params = $rawparams;
784
	} else {
785
		foreach($tocheck as $check) {
786
			$params['check'] = $rawparams['check'];
787
			$params['platform'] = $rawparams['platform'];
788
		}
789
	}
790
	if($config['system']['firmware']['branch'])
791
		$params['branch'] = $config['system']['firmware']['branch'];
792

    
793
	/* XXX: What is this method? */
794
	if(!($versions = call_pfsense_method('pfsense.get_firmware_version', $params))) {
795
		return false;
796
	} else {
797
		$versions["current"] = $params;
798
	}
799

    
800
	return $versions;
801
}
802

    
803
/*
804
 * host_firmware_version(): Return the versions used in this install
805
 */
806
function host_firmware_version($tocheck = "") {
807
	global $g, $config;
808

    
809
	$os_version = trim(substr(php_uname("r"), 0, strpos(php_uname("r"), '-')));
810

    
811
	return array(
812
		"firmware" => array("version" => trim(file_get_contents('/etc/version', " \n"))),
813
		"kernel"   => array("version" => $os_version),
814
		"base"     => array("version" => $os_version),
815
		"platform" => trim(file_get_contents('/etc/platform', " \n")),
816
		"config_version" => $config['version']
817
	);
818
}
819

    
820
function get_disk_info() {
821
	$diskout = "";
822
	exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $2, $3, $4, $5 }'", $diskout);
823
	return explode(' ', $diskout[0]);
824
}
825

    
826
/****f* pfsense-utils/strncpy
827
 * NAME
828
 *   strncpy - copy strings
829
 * INPUTS
830
 *   &$dst, $src, $length
831
 * RESULT
832
 *   none
833
 ******/
834
function strncpy(&$dst, $src, $length) {
835
	if (strlen($src) > $length) {
836
		$dst = substr($src, 0, $length);
837
	} else {
838
		$dst = $src;
839
	}
840
}
841

    
842
/****f* pfsense-utils/reload_interfaces_sync
843
 * NAME
844
 *   reload_interfaces - reload all interfaces
845
 * INPUTS
846
 *   none
847
 * RESULT
848
 *   none
849
 ******/
850
function reload_interfaces_sync() {
851
	global $config, $g;
852

    
853
	if($g['debug'])
854
		log_error(gettext("reload_interfaces_sync() is starting."));
855

    
856
	/* parse config.xml again */
857
	$config = parse_config(true);
858

    
859
	/* enable routing */
860
	system_routing_enable();
861
	if($g['debug'])
862
		log_error(gettext("Enabling system routing"));
863

    
864
	if($g['debug'])
865
		log_error(gettext("Cleaning up Interfaces"));
866

    
867
	/* set up interfaces */
868
	interfaces_configure();
869
}
870

    
871
/****f* pfsense-utils/reload_all
872
 * NAME
873
 *   reload_all - triggers a reload of all settings
874
 *   * INPUTS
875
 *   none
876
 * RESULT
877
 *   none
878
 ******/
879
function reload_all() {
880
	send_event("service reload all");
881
}
882

    
883
/****f* pfsense-utils/reload_interfaces
884
 * NAME
885
 *   reload_interfaces - triggers a reload of all interfaces
886
 * INPUTS
887
 *   none
888
 * RESULT
889
 *   none
890
 ******/
891
function reload_interfaces() {
892
	send_event("interface all reload");
893
}
894

    
895
/****f* pfsense-utils/reload_all_sync
896
 * NAME
897
 *   reload_all - reload all settings
898
 *   * INPUTS
899
 *   none
900
 * RESULT
901
 *   none
902
 ******/
903
function reload_all_sync() {
904
	global $config, $g;
905

    
906
	/* parse config.xml again */
907
	$config = parse_config(true);
908

    
909
	/* set up our timezone */
910
	system_timezone_configure();
911

    
912
	/* set up our hostname */
913
	system_hostname_configure();
914

    
915
	/* make hosts file */
916
	system_hosts_generate();
917

    
918
	/* generate resolv.conf */
919
	system_resolvconf_generate();
920

    
921
	/* enable routing */
922
	system_routing_enable();
923

    
924
	/* set up interfaces */
925
	interfaces_configure();
926

    
927
	/* start dyndns service */
928
	services_dyndns_configure();
929

    
930
	/* configure cron service */
931
	configure_cron();
932

    
933
	/* start the NTP client */
934
	system_ntp_configure();
935

    
936
	/* sync pw database */
937
	conf_mount_rw();
938
	unlink_if_exists("/etc/spwd.db.tmp");
939
	mwexec("/usr/sbin/pwd_mkdb -d /etc/ /etc/master.passwd");
940
	conf_mount_ro();
941

    
942
	/* restart sshd */
943
	send_event("service restart sshd");
944

    
945
	/* restart webConfigurator if needed */
946
	send_event("service restart webgui");
947
}
948

    
949
function setup_serial_port($when="save", $path="") {
950
	global $g, $config;
951
	conf_mount_rw();
952
	$ttys_file = "{$path}/etc/ttys";
953
	$boot_config_file = "{$path}/boot.config";
954
	$loader_conf_file = "{$path}/boot/loader.conf";
955
	/* serial console - write out /boot.config */
956
	if(file_exists($boot_config_file))
957
		$boot_config = file_get_contents($boot_config_file);
958
	else
959
		$boot_config = "";
960

    
961
	$serialspeed = (is_numeric($config['system']['serialspeed'])) ? $config['system']['serialspeed'] : "115200";
962
	if ($g['platform'] != "cdrom") {
963
		$serial_only = false;
964

    
965
		if (($g['platform'] == "nanobsd") && !file_exists("/etc/nano_use_vga.txt")) {
966
			$serial_only = true;
967
		} else {
968
			$specific_platform = system_identify_specific_platform();
969
			if ($specific_platform['name'] == 'RCC-VE') {
970
				$serial_only = true;
971
			}
972
		}
973

    
974
		$boot_config_split = explode("\n", $boot_config);
975
		$fd = fopen($boot_config_file,"w");
976
		if($fd) {
977
			foreach($boot_config_split as $bcs) {
978
				if(stristr($bcs, "-D") || stristr($bcs, "-h")) {
979
					/* DONT WRITE OUT, WE'LL DO IT LATER */
980
				} else {
981
					if($bcs <> "")
982
						fwrite($fd, "{$bcs}\n");
983
				}
984
			}
985
			if ($serial_only === true)
986
				fwrite($fd, "-S{$serialspeed} -h");
987
			else if (is_serial_enabled())
988
				fwrite($fd, "-S{$serialspeed} -D");
989
			fclose($fd);
990
		}
991

    
992
		/* serial console - write out /boot/loader.conf */
993
		if ($when == "upgrade")
994
			system("echo \"Reading {$loader_conf_file}...\" >> /conf/upgrade_log.txt");
995
		$boot_config = file_get_contents($loader_conf_file);
996
		$boot_config_split = explode("\n", $boot_config);
997
		if(count($boot_config_split) > 0) {
998
			$new_boot_config = array();
999
			// Loop through and only add lines that are not empty, and which
1000
			//  do not contain a console directive.
1001
			foreach($boot_config_split as $bcs)
1002
				if(!empty($bcs)
1003
					&& (stripos($bcs, "console") === false)
1004
					&& (stripos($bcs, "boot_multicons") === false)
1005
					&& (stripos($bcs, "boot_serial") === false)
1006
					&& (stripos($bcs, "hw.usb.no_pf") === false))
1007
					$new_boot_config[] = $bcs;
1008

    
1009
			if ($serial_only === true) {
1010
				$new_boot_config[] = 'boot_serial="YES"';
1011
				$new_boot_config[] = 'console="comconsole"';
1012
			} else if (is_serial_enabled()) {
1013
				$new_boot_config[] = 'boot_multicons="YES"';
1014
				$new_boot_config[] = 'boot_serial="YES"';
1015
				$primaryconsole = isset($g['primaryconsole_force']) ? $g['primaryconsole_force'] : $config['system']['primaryconsole'];
1016
				switch ($primaryconsole) {
1017
					case "video":
1018
						$new_boot_config[] = 'console="vidconsole,comconsole"';
1019
						break;
1020
					case "serial":
1021
					default:
1022
						$new_boot_config[] = 'console="comconsole,vidconsole"';
1023
				}
1024
			}
1025
			$new_boot_config[] = 'comconsole_speed="' . $serialspeed . '"';
1026
			$new_boot_config[] = 'hw.usb.no_pf="1"';
1027

    
1028
			file_put_contents($loader_conf_file, implode("\n", $new_boot_config) . "\n");
1029
		}
1030
	}
1031
	$ttys = file_get_contents($ttys_file);
1032
	$ttys_split = explode("\n", $ttys);
1033
	$fd = fopen($ttys_file, "w");
1034

    
1035
	$on_off = (is_serial_enabled() ? 'onifconsole' : 'off');
1036

    
1037
	if (isset($config['system']['disableconsolemenu'])) {
1038
		$console_type = 'Pc';
1039
		$serial_type = 'std.' . $serialspeed;
1040
	} else {
1041
		$console_type = 'al.Pc';
1042
		$serial_type = 'al.' . $serialspeed;
1043
	}
1044
	foreach($ttys_split as $tty) {
1045
		if (stristr($tty, "ttyv0"))
1046
			fwrite($fd, "ttyv0	\"/usr/libexec/getty {$console_type}\"	cons25	on	secure\n");
1047
		else if (stristr($tty, "ttyu")) {
1048
			$ttyn = substr($tty, 0, 5);
1049
			fwrite($fd, "{$ttyn}	\"/usr/libexec/getty {$serial_type}\"	cons25	{$on_off}	secure\n");
1050
		} else
1051
			fwrite($fd, $tty . "\n");
1052
	}
1053
	unset($on_off, $console_type, $serial_type);
1054
	fclose($fd);
1055
	if ($when != "upgrade")
1056
		reload_ttys();
1057

    
1058
	conf_mount_ro();
1059
	return;
1060
}
1061

    
1062
function is_serial_enabled() {
1063
	global $g, $config;
1064

    
1065
	if (!isset($g['enableserial_force']) &&
1066
	    !isset($config['system']['enableserial']) &&
1067
	    ($g['platform'] == "pfSense" || $g['platform'] == "cdrom" || file_exists("/etc/nano_use_vga.txt")))
1068
		return false;
1069

    
1070
	return true;
1071
}
1072

    
1073
function reload_ttys() {
1074
	// Send a HUP signal to init will make it reload /etc/ttys
1075
	posix_kill(1, SIGHUP);
1076
}
1077

    
1078
function print_value_list($list, $count = 10, $separator = ",") {
1079
	$list = implode($separator, array_slice($list, 0, $count));
1080
	if(count($list) < $count) {
1081
		$list .= ".";
1082
	} else {
1083
		$list .= "...";
1084
	}
1085
	return $list;
1086
}
1087

    
1088
/* DHCP enabled on any interfaces? */
1089
function is_dhcp_server_enabled() {
1090
	global $config;
1091

    
1092
	if (!is_array($config['dhcpd']))
1093
		return false;
1094

    
1095
	foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
1096
		if (isset($dhcpifconf['enable']) && !empty($config['interfaces'][$dhcpif]))
1097
			return true;
1098
	}
1099

    
1100
	return false;
1101
}
1102

    
1103
/* DHCP enabled on any interfaces? */
1104
function is_dhcpv6_server_enabled() {
1105
	global $config;
1106

    
1107
	if (is_array($config['interfaces'])) {
1108
		foreach ($config['interfaces'] as $ifcfg) {
1109
			if (isset($ifcfg['enable']) && !empty($ifcfg['track6-interface']))
1110
				return true;
1111
		}
1112
	}
1113

    
1114
	if (!is_array($config['dhcpdv6']))
1115
		return false;
1116

    
1117
	foreach ($config['dhcpdv6'] as $dhcpv6if => $dhcpv6ifconf) {
1118
		if (isset($dhcpv6ifconf['enable']) && !empty($config['interfaces'][$dhcpv6if]))
1119
			return true;
1120
	}
1121

    
1122
	return false;
1123
}
1124

    
1125
/* radvd enabled on any interfaces? */
1126
function is_radvd_enabled() {
1127
	global $config;
1128

    
1129
	if (!is_array($config['dhcpdv6']))
1130
		$config['dhcpdv6'] = array();
1131

    
1132
	$dhcpdv6cfg = $config['dhcpdv6'];
1133
	$Iflist = get_configured_interface_list();
1134

    
1135
	/* handle manually configured DHCP6 server settings first */
1136
	foreach ($dhcpdv6cfg as $dhcpv6if => $dhcpv6ifconf) {
1137
		if(!isset($config['interfaces'][$dhcpv6if]['enable']))
1138
			continue;
1139

    
1140
		if(!isset($dhcpv6ifconf['ramode']))
1141
			$dhcpv6ifconf['ramode'] = $dhcpv6ifconf['mode'];
1142

    
1143
		if($dhcpv6ifconf['ramode'] == "disabled")
1144
			continue;
1145

    
1146
		$ifcfgipv6 = get_interface_ipv6($dhcpv6if);
1147
		if(!is_ipaddrv6($ifcfgipv6))
1148
			continue;
1149

    
1150
		return true;
1151
	}
1152

    
1153
	/* handle DHCP-PD prefixes and 6RD dynamic interfaces */
1154
	foreach ($Iflist as $if => $ifdescr) {
1155
		if(!isset($config['interfaces'][$if]['track6-interface']))
1156
			continue;
1157
		if(!isset($config['interfaces'][$if]['enable']))
1158
			continue;
1159

    
1160
		$ifcfgipv6 = get_interface_ipv6($if);
1161
		if(!is_ipaddrv6($ifcfgipv6))
1162
			continue;
1163

    
1164
		$ifcfgsnv6 = get_interface_subnetv6($if);
1165
		$subnetv6 = gen_subnetv6($ifcfgipv6, $ifcfgsnv6);
1166

    
1167
		if(!is_ipaddrv6($subnetv6))
1168
			continue;
1169

    
1170
		return true;
1171
	}
1172

    
1173
	return false;
1174
}
1175

    
1176
/* Any PPPoE servers enabled? */
1177
function is_pppoe_server_enabled() {
1178
	global $config;
1179

    
1180
	$pppoeenable = false;
1181

    
1182
	if (!is_array($config['pppoes']) || !is_array($config['pppoes']['pppoe']))
1183
		return false;
1184

    
1185
	foreach ($config['pppoes']['pppoe'] as $pppoes)
1186
		if ($pppoes['mode'] == 'server')
1187
			$pppoeenable = true;
1188

    
1189
	return $pppoeenable;
1190
}
1191

    
1192
function convert_seconds_to_hms($sec){
1193
	$min=$hrs=0;
1194
	if ($sec != 0){
1195
		$min = floor($sec/60);
1196
		$sec %= 60;
1197
	}
1198
	if ($min != 0){
1199
		$hrs = floor($min/60);
1200
		$min %= 60;
1201
	}
1202
	if ($sec < 10)
1203
		$sec = "0".$sec;
1204
	if ($min < 10)
1205
		$min = "0".$min;
1206
	if ($hrs < 10)
1207
		$hrs = "0".$hrs;
1208
	$result = $hrs.":".$min.":".$sec;
1209
	return $result;
1210
}
1211

    
1212
/* Compute the total uptime from the ppp uptime log file in the conf directory */
1213

    
1214
function get_ppp_uptime($port){
1215
	if (file_exists("/conf/{$port}.log")){
1216
		$saved_time = file_get_contents("/conf/{$port}.log");
1217
		$uptime_data = explode("\n",$saved_time);
1218
		$sec=0;
1219
		foreach($uptime_data as $upt) {
1220
			$sec += substr($upt, 1 + strpos($upt, " "));
1221
		}
1222
		return convert_seconds_to_hms($sec);
1223
	} else {
1224
		$total_time = gettext("No history data found!");
1225
		return $total_time;
1226
	}
1227
}
1228

    
1229
//returns interface information
1230
function get_interface_info($ifdescr) {
1231
	global $config, $g;
1232

    
1233
	$ifinfo = array();
1234
	if (empty($config['interfaces'][$ifdescr]))
1235
		return;
1236
	$ifinfo['hwif'] = $config['interfaces'][$ifdescr]['if'];
1237
	$ifinfo['if'] = get_real_interface($ifdescr);
1238

    
1239
	$chkif = $ifinfo['if'];
1240
	$ifinfotmp = pfSense_get_interface_addresses($chkif);
1241
	$ifinfo['status'] = $ifinfotmp['status'];
1242
	if (empty($ifinfo['status']))
1243
		$ifinfo['status'] = "down";
1244
	$ifinfo['macaddr'] = $ifinfotmp['macaddr'];
1245
	$ifinfo['mtu'] = $ifinfotmp['mtu'];
1246
	$ifinfo['ipaddr'] = $ifinfotmp['ipaddr'];
1247
	$ifinfo['subnet'] = $ifinfotmp['subnet'];
1248
	$ifinfo['linklocal'] = get_interface_linklocal($ifdescr);
1249
	$ifinfo['ipaddrv6'] = get_interface_ipv6($ifdescr);
1250
	$ifinfo['subnetv6'] = get_interface_subnetv6($ifdescr);
1251
	if (isset($ifinfotmp['link0']))
1252
		$link0 = "down";
1253
	$ifinfotmp = pfSense_get_interface_stats($chkif);
1254
	// $ifinfo['inpkts'] = $ifinfotmp['inpkts'];
1255
	// $ifinfo['outpkts'] = $ifinfotmp['outpkts'];
1256
	$ifinfo['inerrs'] = $ifinfotmp['inerrs'];
1257
	$ifinfo['outerrs'] = $ifinfotmp['outerrs'];
1258
	$ifinfo['collisions'] = $ifinfotmp['collisions'];
1259

    
1260
	/* Use pfctl for non wrapping 64 bit counters */
1261
	/* Pass */
1262
	exec("/sbin/pfctl -vvsI -i {$chkif}", $pfctlstats);
1263
	$pf_in4_pass = preg_split("/ +/ ", $pfctlstats[3]);
1264
	$pf_out4_pass = preg_split("/ +/", $pfctlstats[5]);
1265
	$pf_in6_pass = preg_split("/ +/ ", $pfctlstats[7]);
1266
	$pf_out6_pass = preg_split("/ +/", $pfctlstats[9]);
1267
	$in4_pass = $pf_in4_pass[5];
1268
	$out4_pass = $pf_out4_pass[5];
1269
	$in4_pass_packets = $pf_in4_pass[3];
1270
	$out4_pass_packets = $pf_out4_pass[3];
1271
	$in6_pass = $pf_in6_pass[5];
1272
	$out6_pass = $pf_out6_pass[5];
1273
	$in6_pass_packets = $pf_in6_pass[3];
1274
	$out6_pass_packets = $pf_out6_pass[3];
1275
	$ifinfo['inbytespass'] = $in4_pass + $in6_pass;
1276
	$ifinfo['outbytespass'] = $out4_pass + $out6_pass;
1277
	$ifinfo['inpktspass'] = $in4_pass_packets + $in6_pass_packets;
1278
	$ifinfo['outpktspass'] = $out4_pass_packets + $out6_pass_packets;
1279

    
1280
	/* Block */
1281
	$pf_in4_block = preg_split("/ +/", $pfctlstats[4]);
1282
	$pf_out4_block = preg_split("/ +/", $pfctlstats[6]);
1283
	$pf_in6_block = preg_split("/ +/", $pfctlstats[8]);
1284
	$pf_out6_block = preg_split("/ +/", $pfctlstats[10]);
1285
	$in4_block = $pf_in4_block[5];
1286
	$out4_block = $pf_out4_block[5];
1287
	$in4_block_packets = $pf_in4_block[3];
1288
	$out4_block_packets = $pf_out4_block[3];
1289
	$in6_block = $pf_in6_block[5];
1290
	$out6_block = $pf_out6_block[5];
1291
	$in6_block_packets = $pf_in6_block[3];
1292
	$out6_block_packets = $pf_out6_block[3];
1293
	$ifinfo['inbytesblock'] = $in4_block + $in6_block;
1294
	$ifinfo['outbytesblock'] = $out4_block + $out6_block;
1295
	$ifinfo['inpktsblock'] = $in4_block_packets + $in6_block_packets;
1296
	$ifinfo['outpktsblock'] = $out4_block_packets + $out6_block_packets;
1297

    
1298
	$ifinfo['inbytes'] = $in4_pass + $in6_pass;
1299
	$ifinfo['outbytes'] = $out4_pass + $out6_pass;
1300
	$ifinfo['inpkts'] = $in4_pass_packets + $in6_pass_packets;
1301
	$ifinfo['outpkts'] = $out4_pass_packets + $out6_pass_packets;
1302

    
1303
	$ifconfiginfo = "";
1304
	$link_type = $config['interfaces'][$ifdescr]['ipaddr'];
1305
	switch ($link_type) {
1306
	/* DHCP? -> see if dhclient is up */
1307
	case "dhcp":
1308
		/* see if dhclient is up */
1309
		if (find_dhclient_process($ifinfo['if']) != 0)
1310
			$ifinfo['dhcplink'] = "up";
1311
		else
1312
			$ifinfo['dhcplink'] = "down";
1313

    
1314
		break;
1315
	/* PPPoE/PPTP/L2TP interface? -> get status from virtual interface */
1316
	case "pppoe":
1317
	case "pptp":
1318
	case "l2tp":
1319
		if ($ifinfo['status'] == "up" && !isset($link0))
1320
			/* get PPPoE link status for dial on demand */
1321
			$ifinfo["{$link_type}link"] = "up";
1322
		else
1323
			$ifinfo["{$link_type}link"] = "down";
1324

    
1325
		break;
1326
	/* PPP interface? -> get uptime for this session and cumulative uptime from the persistant log file in conf */
1327
	case "ppp":
1328
		if ($ifinfo['status'] == "up")
1329
			$ifinfo['ppplink'] = "up";
1330
		else
1331
			$ifinfo['ppplink'] = "down" ;
1332

    
1333
		if (empty($ifinfo['status']))
1334
			$ifinfo['status'] = "down";
1335

    
1336
		if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
1337
			foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
1338
				if ($config['interfaces'][$ifdescr]['if'] == $ppp['if'])
1339
					break;
1340
			}
1341
		}
1342
		$dev = $ppp['ports'];
1343
		if ($config['interfaces'][$ifdescr]['if'] != $ppp['if'] || empty($dev))
1344
			break;
1345
		if (!file_exists($dev)) {
1346
			$ifinfo['nodevice'] = 1;
1347
			$ifinfo['pppinfo'] = $dev . " " . gettext("device not present! Is the modem attached to the system?");
1348
		}
1349

    
1350
		$usbmodemoutput = array();
1351
		exec("usbconfig", $usbmodemoutput);
1352
		$mondev = "{$g['tmp_path']}/3gstats.{$ifdescr}";
1353
		if(file_exists($mondev)) {
1354
			$cellstats = file($mondev);
1355
			/* skip header */
1356
			$a_cellstats = explode(",", $cellstats[1]);
1357
			if(preg_match("/huawei/i", implode("\n", $usbmodemoutput))) {
1358
				$ifinfo['cell_rssi'] = huawei_rssi_to_string($a_cellstats[1]);
1359
				$ifinfo['cell_mode'] = huawei_mode_to_string($a_cellstats[2], $a_cellstats[3]);
1360
				$ifinfo['cell_simstate'] = huawei_simstate_to_string($a_cellstats[10]);
1361
				$ifinfo['cell_service'] = huawei_service_to_string(trim($a_cellstats[11]));
1362
			}
1363
			if(preg_match("/zte/i", implode("\n", $usbmodemoutput))) {
1364
				$ifinfo['cell_rssi'] = zte_rssi_to_string($a_cellstats[1]);
1365
				$ifinfo['cell_mode'] = zte_mode_to_string($a_cellstats[2], $a_cellstats[3]);
1366
				$ifinfo['cell_simstate'] = zte_simstate_to_string($a_cellstats[10]);
1367
				$ifinfo['cell_service'] = zte_service_to_string(trim($a_cellstats[11]));
1368
			}
1369
			$ifinfo['cell_upstream'] = $a_cellstats[4];
1370
			$ifinfo['cell_downstream'] = trim($a_cellstats[5]);
1371
			$ifinfo['cell_sent'] = $a_cellstats[6];
1372
			$ifinfo['cell_received'] = trim($a_cellstats[7]);
1373
			$ifinfo['cell_bwupstream'] = $a_cellstats[8];
1374
			$ifinfo['cell_bwdownstream'] = trim($a_cellstats[9]);
1375
		}
1376
		// Calculate cumulative uptime for PPP link. Useful for connections that have per minute/hour contracts so you don't go over!
1377
		if (isset($ppp['uptime']))
1378
			$ifinfo['ppp_uptime_accumulated'] = "(".get_ppp_uptime($ifinfo['if']).")";
1379
		break;
1380
	default:
1381
		break;
1382
	}
1383

    
1384
	if (file_exists("{$g['varrun_path']}/{$link_type}_{$ifdescr}.pid")) {
1385
		$sec = trim(`/usr/local/sbin/ppp-uptime.sh {$ifinfo['if']}`);
1386
		$ifinfo['ppp_uptime'] = convert_seconds_to_hms($sec);
1387
	}
1388

    
1389
	if ($ifinfo['status'] == "up") {
1390
		/* try to determine media with ifconfig */
1391
		unset($ifconfiginfo);
1392
		exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
1393
		$wifconfiginfo = array();
1394
		if(is_interface_wireless($ifdescr)) {
1395
			exec("/sbin/ifconfig {$ifinfo['if']} list sta", $wifconfiginfo);
1396
			array_shift($wifconfiginfo);
1397
		}
1398
		$matches = "";
1399
		foreach ($ifconfiginfo as $ici) {
1400

    
1401
			/* don't list media/speed for wireless cards, as it always
1402
			   displays 2 Mbps even though clients can connect at 11 Mbps */
1403
			if (preg_match("/media: .*? \((.*?)\)/", $ici, $matches)) {
1404
				$ifinfo['media'] = $matches[1];
1405
			} else if (preg_match("/media: Ethernet (.*)/", $ici, $matches)) {
1406
				$ifinfo['media'] = $matches[1];
1407
			} else if (preg_match("/media: IEEE 802.11 Wireless Ethernet (.*)/", $ici, $matches)) {
1408
				$ifinfo['media'] = $matches[1];
1409
			}
1410

    
1411
			if (preg_match("/status: (.*)$/", $ici, $matches)) {
1412
				if ($matches[1] != "active")
1413
					$ifinfo['status'] = $matches[1];
1414
				if($ifinfo['status'] == gettext("running"))
1415
					$ifinfo['status'] = gettext("up");
1416
			}
1417
			if (preg_match("/channel (\S*)/", $ici, $matches)) {
1418
				$ifinfo['channel'] = $matches[1];
1419
			}
1420
			if (preg_match("/ssid (\".*?\"|\S*)/", $ici, $matches)) {
1421
				if ($matches[1][0] == '"')
1422
					$ifinfo['ssid'] = substr($matches[1], 1, -1);
1423
				else
1424
					$ifinfo['ssid'] = $matches[1];
1425
			}
1426
			if (preg_match("/laggproto (.*)$/", $ici, $matches)) {
1427
				$ifinfo['laggproto'] = $matches[1];
1428
			}
1429
			if (preg_match("/laggport: (.*)$/", $ici, $matches)) {
1430
				$ifinfo['laggport'][] = $matches[1];
1431
			}
1432
		}
1433
		foreach($wifconfiginfo as $ici) {
1434
			$elements = preg_split("/[ ]+/i", $ici);
1435
			if ($elements[0] != "") {
1436
				$ifinfo['bssid'] = $elements[0];
1437
			}
1438
			if ($elements[3] != "") {
1439
				$ifinfo['rate'] = $elements[3];
1440
			}
1441
			if ($elements[4] != "") {
1442
				$ifinfo['rssi'] = $elements[4];
1443
			}
1444

    
1445
		}
1446
		/* lookup the gateway */
1447
		if (interface_has_gateway($ifdescr)) {
1448
			$ifinfo['gateway'] = get_interface_gateway($ifdescr);
1449
			$ifinfo['gatewayv6'] = get_interface_gateway_v6($ifdescr);
1450
		}
1451
	}
1452

    
1453
	$bridge = "";
1454
	$bridge = link_interface_to_bridge($ifdescr);
1455
	if($bridge) {
1456
		$bridge_text = `/sbin/ifconfig {$bridge}`;
1457
		if(stristr($bridge_text, "blocking") <> false) {
1458
			$ifinfo['bridge'] = "<b><font color='red'>" . gettext("blocking") . "</font></b> - " . gettext("check for ethernet loops");
1459
			$ifinfo['bridgeint'] = $bridge;
1460
		} else if(stristr($bridge_text, "learning") <> false) {
1461
			$ifinfo['bridge'] = gettext("learning");
1462
			$ifinfo['bridgeint'] = $bridge;
1463
		} else if(stristr($bridge_text, "forwarding") <> false) {
1464
			$ifinfo['bridge'] = gettext("forwarding");
1465
			$ifinfo['bridgeint'] = $bridge;
1466
		}
1467
	}
1468

    
1469
	return $ifinfo;
1470
}
1471

    
1472
//returns cpu speed of processor. Good for determining capabilities of machine
1473
function get_cpu_speed() {
1474
	return get_single_sysctl("hw.clockrate");
1475
}
1476

    
1477
function get_uptime_sec() {
1478
	$boottime = "";
1479
	$matches = "";
1480
	$boottime = get_single_sysctl("kern.boottime");
1481
	preg_match("/sec = (\d+)/", $boottime, $matches);
1482
	$boottime = $matches[1];
1483
	if(intval($boottime) == 0)
1484
		return 0;
1485

    
1486
	$uptime = time() - $boottime;
1487
	return $uptime;
1488
}
1489

    
1490
function add_hostname_to_watch($hostname) {
1491
	if(!is_dir("/var/db/dnscache")) {
1492
		mkdir("/var/db/dnscache");
1493
	}
1494
	$result = array();
1495
	if((is_fqdn($hostname)) && (!is_ipaddr($hostname))) {
1496
		$domrecords = array();
1497
		$domips = array();
1498
		exec("host -t A " . escapeshellarg($hostname), $domrecords, $rethost);
1499
		if($rethost == 0) {
1500
			foreach($domrecords as $domr) {
1501
				$doml = explode(" ", $domr);
1502
				$domip = $doml[3];
1503
				/* fill array with domain ip addresses */
1504
				if(is_ipaddr($domip)) {
1505
					$domips[] = $domip;
1506
				}
1507
			}
1508
		}
1509
		sort($domips);
1510
		$contents = "";
1511
		if(! empty($domips)) {
1512
			foreach($domips as $ip) {
1513
				$contents .= "$ip\n";
1514
			}
1515
		}
1516
		file_put_contents("/var/db/dnscache/$hostname", $contents);
1517
		/* Remove empty elements */
1518
		$result = array_filter(explode("\n", $contents), 'strlen');
1519
	}
1520
	return $result;
1521
}
1522

    
1523
function is_fqdn($fqdn) {
1524
	$hostname = false;
1525
	if(preg_match("/[-A-Z0-9\.]+\.[-A-Z0-9\.]+/i", $fqdn)) {
1526
		$hostname = true;
1527
	}
1528
	if(preg_match("/\.\./", $fqdn)) {
1529
		$hostname = false;
1530
	}
1531
	if(preg_match("/^\./i", $fqdn)) {
1532
		$hostname = false;
1533
	}
1534
	if(preg_match("/\//i", $fqdn)) {
1535
		$hostname = false;
1536
	}
1537
	return($hostname);
1538
}
1539

    
1540
function pfsense_default_state_size() {
1541
	/* get system memory amount */
1542
	$memory = get_memory();
1543
	$physmem = $memory[0];
1544
	/* Be cautious and only allocate 10% of system memory to the state table */
1545
	$max_states = (int) ($physmem/10)*1000;
1546
	return $max_states;
1547
}
1548

    
1549
function pfsense_default_tables_size() {
1550
	$current = `pfctl -sm | grep ^tables | awk '{print $4};'`;
1551
	return $current;
1552
}
1553

    
1554
function pfsense_default_table_entries_size() {
1555
	$current = `pfctl -sm | grep table-entries | awk '{print $4};'`;
1556
	return $current;
1557
}
1558

    
1559
/* Compare the current hostname DNS to the DNS cache we made
1560
 * if it has changed we return the old records
1561
 * if no change we return false */
1562
function compare_hostname_to_dnscache($hostname) {
1563
	if(!is_dir("/var/db/dnscache")) {
1564
		mkdir("/var/db/dnscache");
1565
	}
1566
	$hostname = trim($hostname);
1567
	if(is_readable("/var/db/dnscache/{$hostname}")) {
1568
		$oldcontents = file_get_contents("/var/db/dnscache/{$hostname}");
1569
	} else {
1570
		$oldcontents = "";
1571
	}
1572
	if((is_fqdn($hostname)) && (!is_ipaddr($hostname))) {
1573
		$domrecords = array();
1574
		$domips = array();
1575
		exec("host -t A " . escapeshellarg($hostname), $domrecords, $rethost);
1576
		if($rethost == 0) {
1577
			foreach($domrecords as $domr) {
1578
				$doml = explode(" ", $domr);
1579
				$domip = $doml[3];
1580
				/* fill array with domain ip addresses */
1581
				if(is_ipaddr($domip)) {
1582
					$domips[] = $domip;
1583
				}
1584
			}
1585
		}
1586
		sort($domips);
1587
		$contents = "";
1588
		if(! empty($domips)) {
1589
			foreach($domips as $ip) {
1590
				$contents .= "$ip\n";
1591
			}
1592
		}
1593
	}
1594

    
1595
	if(trim($oldcontents) != trim($contents)) {
1596
		if($g['debug']) {
1597
			log_error(sprintf(gettext('DNSCACHE: Found old IP %1$s and new IP %2$s'), $oldcontents, $contents));
1598
		}
1599
		return ($oldcontents);
1600
	} else {
1601
		return false;
1602
	}
1603
}
1604

    
1605
/*
1606
 * load_crypto() - Load crypto modules if enabled in config.
1607
 */
1608
function load_crypto() {
1609
	global $config, $g;
1610
	$crypto_modules = array('glxsb', 'aesni');
1611

    
1612
	if (!in_array($config['system']['crypto_hardware'], $crypto_modules))
1613
		return false;
1614

    
1615
	if (!empty($config['system']['crypto_hardware']) && !is_module_loaded($config['system']['crypto_hardware'])) {
1616
		log_error("Loading {$config['system']['crypto_hardware']} cryptographic accelerator module.");
1617
		mwexec("/sbin/kldload {$config['system']['crypto_hardware']}");
1618
	}
1619
}
1620

    
1621
/*
1622
 * load_thermal_hardware() - Load temperature monitor kernel module
1623
 */
1624
function load_thermal_hardware() {
1625
	global $config, $g;
1626
	$thermal_hardware_modules = array('coretemp', 'amdtemp');
1627

    
1628
	if (!in_array($config['system']['thermal_hardware'], $thermal_hardware_modules))
1629
		return false;
1630

    
1631
	if (!empty($config['system']['thermal_hardware']) && !is_module_loaded($config['system']['thermal_hardware'])) {
1632
		log_error("Loading {$config['system']['thermal_hardware']} thermal monitor module.");
1633
		mwexec("/sbin/kldload {$config['system']['thermal_hardware']}");
1634
	}
1635
}
1636

    
1637
/****f* pfsense-utils/isvm
1638
 * NAME
1639
 *   isvm
1640
 * INPUTS
1641
 *	none
1642
 * RESULT
1643
 *   returns true if machine is running under a virtual environment
1644
 ******/
1645
function isvm() {
1646
	$virtualenvs = array("vmware", "parallels", "qemu", "bochs", "plex86", "VirtualBox");
1647
	$_gb = exec('/bin/kenv smbios.system.product 2>/dev/null', $output, $rc);
1648

    
1649
	if ($rc != 0 || !isset($output[0]))
1650
		return false;
1651

    
1652
	foreach ($virtualenvs as $virtualenv)
1653
		if (stripos($output[0], $virtualenv) !== false)
1654
			return true;
1655

    
1656
	return false;
1657
}
1658

    
1659
function get_freebsd_version() {
1660
	$version = explode(".", php_uname("r"));
1661
	return $version[0];
1662
}
1663

    
1664
function download_file($url, $destination, $verify_ssl = false, $connect_timeout = 60, $timeout = 0) {
1665
	global $config, $g;
1666

    
1667
	$fp = fopen($destination, "wb");
1668

    
1669
	if (!$fp)
1670
		return false;
1671

    
1672
	$ch = curl_init();
1673
	curl_setopt($ch, CURLOPT_URL, $url);
1674
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify_ssl);
1675
	curl_setopt($ch, CURLOPT_FILE, $fp);
1676
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connect_timeout);
1677
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
1678
	curl_setopt($ch, CURLOPT_HEADER, false);
1679
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
1680
	curl_setopt($ch, CURLOPT_USERAGENT, $g['product_name'] . '/' . rtrim(file_get_contents("/etc/version")));
1681

    
1682
	if (!empty($config['system']['proxyurl'])) {
1683
		curl_setopt($ch, CURLOPT_PROXY, $config['system']['proxyurl']);
1684
		if (!empty($config['system']['proxyport']))
1685
			curl_setopt($ch, CURLOPT_PROXYPORT, $config['system']['proxyport']);
1686
		if (!empty($config['system']['proxyuser']) && !empty($config['system']['proxypass'])) {
1687
			@curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_ANY | CURLAUTH_ANYSAFE);
1688
			curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$config['system']['proxyuser']}:{$config['system']['proxypass']}");
1689
		}
1690
	}
1691

    
1692
	@curl_exec($ch);
1693
	$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
1694
	fclose($fp);
1695
	curl_close($ch);
1696
	return ($http_code == 200) ? true : $http_code;
1697
}
1698

    
1699
function download_file_with_progress_bar($url_file, $destination_file, $readbody = 'read_body', $connect_timeout=60, $timeout=0) {
1700
	global $ch, $fout, $file_size, $downloaded, $config, $first_progress_update, $g;
1701
	$file_size  = 1;
1702
	$downloaded = 1;
1703
	$first_progress_update = TRUE;
1704
	/* open destination file */
1705
	$fout = fopen($destination_file, "wb");
1706

    
1707
	/*
1708
	 *      Originally by Author: Keyvan Minoukadeh
1709
	 *      Modified by Scott Ullrich to return Content-Length size
1710
	 */
1711

    
1712
	$ch = curl_init();
1713
	curl_setopt($ch, CURLOPT_URL, $url_file);
1714
	curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
1715
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
1716
	/* Don't verify SSL peers since we don't have the certificates to do so. */
1717
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
1718
	curl_setopt($ch, CURLOPT_WRITEFUNCTION, $readbody);
1719
	curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
1720
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connect_timeout);
1721
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
1722
	curl_setopt($ch, CURLOPT_USERAGENT, $g['product_name'] . '/' . rtrim(file_get_contents("/etc/version")));
1723

    
1724
	if (!empty($config['system']['proxyurl'])) {
1725
		curl_setopt($ch, CURLOPT_PROXY, $config['system']['proxyurl']);
1726
		if (!empty($config['system']['proxyport']))
1727
			curl_setopt($ch, CURLOPT_PROXYPORT, $config['system']['proxyport']);
1728
		if (!empty($config['system']['proxyuser']) && !empty($config['system']['proxypass'])) {
1729
			@curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_ANY | CURLAUTH_ANYSAFE);
1730
			curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$config['system']['proxyuser']}:{$config['system']['proxypass']}");
1731
		}
1732
	}
1733

    
1734
	@curl_exec($ch);
1735
	$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
1736
	if($fout)
1737
		fclose($fout);
1738
	curl_close($ch);
1739
	return ($http_code == 200) ? true : $http_code;
1740
}
1741

    
1742
function read_header($ch, $string) {
1743
	global $file_size, $fout;
1744
	$length = strlen($string);
1745
	$regs = "";
1746
	preg_match("/(Content-Length:) (.*)/", $string, $regs);
1747
	if($regs[2] <> "") {
1748
		$file_size = intval($regs[2]);
1749
	}
1750
	ob_flush();
1751
	return $length;
1752
}
1753

    
1754
function read_body($ch, $string) {
1755
	global $fout, $file_size, $downloaded, $sendto, $static_status, $static_output, $lastseen, $first_progress_update;
1756
	global $pkg_interface;
1757
	$length = strlen($string);
1758
	$downloaded += intval($length);
1759
	if($file_size > 0) {
1760
		$downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
1761
		$downloadProgress = 100 - $downloadProgress;
1762
	} else
1763
		$downloadProgress = 0;
1764
	if($lastseen <> $downloadProgress and $downloadProgress < 101) {
1765
		if($sendto == "status") {
1766
			if($pkg_interface == "console") {
1767
				if(($downloadProgress % 10) == 0 || $downloadProgress < 10) {
1768
					$tostatus = $static_status . $downloadProgress . "%";
1769
					if ($downloadProgress == 100) {
1770
						$tostatus = $tostatus . "\r";
1771
					}
1772
					update_status($tostatus);
1773
				}
1774
			} else {
1775
				$tostatus = $static_status . $downloadProgress . "%";
1776
				update_status($tostatus);
1777
			}
1778
		} else {
1779
			if($pkg_interface == "console") {
1780
				if(($downloadProgress % 10) == 0 || $downloadProgress < 10) {
1781
					$tooutput = $static_output . $downloadProgress . "%";
1782
					if ($downloadProgress == 100) {
1783
						$tooutput = $tooutput . "\r";
1784
					}
1785
					update_output_window($tooutput);
1786
				}
1787
			} else {
1788
				$tooutput = $static_output . $downloadProgress . "%";
1789
				update_output_window($tooutput);
1790
			}
1791
		}
1792
				if(($pkg_interface != "console") || (($downloadProgress % 10) == 0) || ($downloadProgress < 10)) {
1793
					update_progress_bar($downloadProgress, $first_progress_update);
1794
					$first_progress_update = FALSE;
1795
				}
1796
		$lastseen = $downloadProgress;
1797
	}
1798
	if($fout)
1799
		fwrite($fout, $string);
1800
	ob_flush();
1801
	return $length;
1802
}
1803

    
1804
/*
1805
 *   update_output_window: update bottom textarea dynamically.
1806
 */
1807
function update_output_window($text) {
1808
	global $pkg_interface;
1809
	$log = preg_replace("/\n/", "\\n", $text);
1810
	if($pkg_interface != "console") {
1811
		echo "\n<script type=\"text/javascript\">";
1812
		echo "\n//<![CDATA[";
1813
		echo "\nthis.document.forms[0].output.value = \"" . $log . "\";";
1814
		echo "\nthis.document.forms[0].output.scrollTop = this.document.forms[0].output.scrollHeight;";
1815
		echo "\n//]]>";
1816
		echo "\n</script>";
1817
	}
1818
	/* ensure that contents are written out */
1819
	ob_flush();
1820
}
1821

    
1822
/*
1823
 *   update_status: update top textarea dynamically.
1824
 */
1825
function update_status($status) {
1826
	global $pkg_interface;
1827
	if($pkg_interface == "console") {
1828
		echo "\r{$status}";
1829
	} else {
1830
		echo "\n<script type=\"text/javascript\">";
1831
		echo "\n//<![CDATA[";
1832
		echo "\nthis.document.forms[0].status.value=\"" . $status . "\";";
1833
		echo "\n//]]>";
1834
		echo "\n</script>";
1835
	}
1836
	/* ensure that contents are written out */
1837
	ob_flush();
1838
}
1839

    
1840
/*
1841
 * update_progress_bar($percent, $first_time): updates the javascript driven progress bar.
1842
 */
1843
function update_progress_bar($percent, $first_time) {
1844
	global $pkg_interface;
1845
	if($percent > 100) $percent = 1;
1846
	if($pkg_interface <> "console") {
1847
		echo "\n<script type=\"text/javascript\">";
1848
		echo "\n//<![CDATA[";
1849
		echo "\ndocument.progressbar.style.width='" . $percent . "%';";
1850
		echo "\n//]]>";
1851
		echo "\n</script>";
1852
	} else {
1853
		if(!($first_time))
1854
			echo "\x08\x08\x08\x08\x08";
1855
		echo sprintf("%4d%%", $percent);
1856
	}
1857
}
1858

    
1859
/* Split() is being DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged. */
1860
if(!function_exists("split")) {
1861
	function split($separator, $haystack, $limit = null) {
1862
		log_error("deprecated split() call with separator '{$separator}'");
1863
		return preg_split($separator, $haystack, $limit);
1864
	}
1865
}
1866

    
1867
function update_alias_names_upon_change($section, $field, $new_alias_name, $origname) {
1868
	global $g, $config, $pconfig, $debug;
1869
	if(!$origname)
1870
		return;
1871

    
1872
	$sectionref = &$config;
1873
	foreach($section as $sectionname) {
1874
		if(is_array($sectionref) && isset($sectionref[$sectionname]))
1875
			$sectionref = &$sectionref[$sectionname];
1876
		else
1877
			return;
1878
	}
1879

    
1880
	if($debug) $fd = fopen("{$g['tmp_path']}/print_r", "a");
1881
	if($debug) fwrite($fd, print_r($pconfig, true));
1882

    
1883
	if(is_array($sectionref)) {
1884
		foreach($sectionref as $itemkey => $item) {
1885
			if($debug) fwrite($fd, "$itemkey\n");
1886

    
1887
			$fieldfound = true;
1888
			$fieldref = &$sectionref[$itemkey];
1889
			foreach($field as $fieldname) {
1890
				if(is_array($fieldref) && isset($fieldref[$fieldname]))
1891
					$fieldref = &$fieldref[$fieldname];
1892
				else {
1893
					$fieldfound = false;
1894
					break;
1895
				}
1896
			}
1897
			if($fieldfound && $fieldref == $origname) {
1898
				if($debug) fwrite($fd, "Setting old alias value $origname to $new_alias_name\n");
1899
				$fieldref = $new_alias_name;
1900
			}
1901
		}
1902
	}
1903

    
1904
	if($debug) fclose($fd);
1905

    
1906
}
1907

    
1908
function parse_aliases_file($filename, $type = "url", $max_items = -1) {
1909
	/*
1910
	 * $filename = file to process for example blocklist like DROP:  http://www.spamhaus.org/drop/drop.txt
1911
	 * $type = if set to 'url' then subnets and ips will be returned,
1912
	 *         if set to 'url_ports' port-ranges and ports will be returned
1913
	 * $max_items = sets the maximum amount of valid items to load, -1 the default defines there is no limit.
1914
	 *
1915
	 * RETURNS an array of ip subnets and ip's or ports and port-ranges, returns NULL upon a error conditions (file not found)
1916
	 */
1917

    
1918
	$fd = @fopen($filename, 'r');
1919
	if (!$fd) {
1920
		log_error(gettext("Could not process aliases from alias: {$alias_url}"));
1921
		return null;
1922
	}
1923
	$items = array();
1924
	/* NOTE: fgetss() is not a typo RTFM before being smart */
1925
	while (($fc = fgetss($fd)) !== FALSE) {
1926
		$tmp = trim($fc, " \t\n\r");
1927
		if (empty($tmp))
1928
			continue;
1929
		$tmp_str = strstr($tmp, '#', true);
1930
		if (!empty($tmp_str))
1931
			$tmp = $tmp_str;
1932
		$tmp_str = strstr($tmp, ' ', true);
1933
		if (!empty($tmp_str))
1934
			$tmp = $tmp_str;
1935
		$valid = ($type == "url" && (is_ipaddr($tmp) || is_subnet($tmp))) ||
1936
		         ($type == "url_ports" && (is_port($tmp) || is_portrange($tmp)));
1937
		if ($valid) {
1938
			$items[] = $tmp;
1939
			if (count($items) == $max_items)
1940
				break;
1941
		}
1942
	}
1943
	fclose($fd);
1944
	return $items;
1945
}
1946

    
1947
function update_alias_url_data() {
1948
	global $config, $g;
1949

    
1950
	$updated = false;
1951

    
1952
	/* item is a url type */
1953
	$lockkey = lock('aliasurl');
1954
	if (is_array($config['aliases']['alias'])) {
1955
		foreach ($config['aliases']['alias'] as $x => $alias) {
1956
			if (empty($alias['aliasurl']))
1957
				continue;
1958

    
1959
			$address = null;
1960
			foreach ($alias['aliasurl'] as $alias_url) {
1961
				/* fetch down and add in */
1962
				$temp_filename = tempnam("{$g['tmp_path']}/", "alias_import");
1963
				unlink($temp_filename);
1964
				$verify_ssl = isset($config['system']['checkaliasesurlcert']);
1965
				mkdir($temp_filename);
1966
				download_file($alias_url, $temp_filename . "/aliases", $verify_ssl);
1967

    
1968
				/* if the item is tar gzipped then extract */
1969
				if (stripos($alias_url, '.tgz')) {
1970
					if (!process_alias_tgz($temp_filename))
1971
						continue;
1972
				} else if (stripos($alias_url, '.zip')) {
1973
					if (!process_alias_unzip($temp_filename))
1974
						continue;
1975
				}
1976
				if (file_exists("{$temp_filename}/aliases")) {
1977
					$address = parse_aliases_file("{$temp_filename}/aliases", $alias['type'], 3000);
1978
					mwexec("/bin/rm -rf {$temp_filename}");
1979
				}
1980
			}
1981
			if ($address != null) {
1982
				$config['aliases']['alias'][$x]['address'] = implode(" ", $address);
1983
				$updated = true;
1984
			}
1985
		}
1986
	}
1987
	unlock($lockkey);
1988

    
1989
	/* Report status to callers as well */
1990
	return $updated;
1991
}
1992

    
1993
function process_alias_unzip($temp_filename) {
1994
	if(!file_exists("/usr/local/bin/unzip")) {
1995
		log_error(gettext("Alias archive is a .zip file which cannot be decompressed because utility is missing!"));
1996
		return false;
1997
	}
1998
	rename("{$temp_filename}/aliases", "{$temp_filename}/aliases.zip");
1999
	mwexec("/usr/local/bin/unzip {$temp_filename}/aliases.tgz -d {$temp_filename}/aliases/");
2000
	unlink("{$temp_filename}/aliases.zip");
2001
	$files_to_process = return_dir_as_array("{$temp_filename}/");
2002
	/* foreach through all extracted files and build up aliases file */
2003
	$fd = @fopen("{$temp_filename}/aliases", "w");
2004
	if (!$fd) {
2005
		log_error(gettext("Could not open {$temp_filename}/aliases for writing!"));
2006
		return false;
2007
	}
2008
	foreach($files_to_process as $f2p) {
2009
		$tmpfd = @fopen($f2p, 'r');
2010
		if (!$tmpfd) {
2011
			log_error(gettext("The following file could not be read {$f2p} from {$temp_filename}"));
2012
			continue;
2013
		}
2014
		while (($tmpbuf = fread($tmpfd, 65536)) !== FALSE)
2015
			fwrite($fd, $tmpbuf);
2016
		fclose($tmpfd);
2017
		unlink($f2p);
2018
	}
2019
	fclose($fd);
2020
	unset($tmpbuf);
2021

    
2022
	return true;
2023
}
2024

    
2025
function process_alias_tgz($temp_filename) {
2026
	if(!file_exists('/usr/bin/tar')) {
2027
		log_error(gettext("Alias archive is a .tar/tgz file which cannot be decompressed because utility is missing!"));
2028
		return false;
2029
	}
2030
	rename("{$temp_filename}/aliases", "{$temp_filename}/aliases.tgz");
2031
	mwexec("/usr/bin/tar xzf {$temp_filename}/aliases.tgz -C {$temp_filename}/aliases/");
2032
	unlink("{$temp_filename}/aliases.tgz");
2033
	$files_to_process = return_dir_as_array("{$temp_filename}/");
2034
	/* foreach through all extracted files and build up aliases file */
2035
	$fd = @fopen("{$temp_filename}/aliases", "w");
2036
	if (!$fd) {
2037
		log_error(gettext("Could not open {$temp_filename}/aliases for writing!"));
2038
		return false;
2039
	}
2040
	foreach($files_to_process as $f2p) {
2041
		$tmpfd = @fopen($f2p, 'r');
2042
		if (!$tmpfd) {
2043
			log_error(gettext("The following file could not be read {$f2p} from {$temp_filename}"));
2044
			continue;
2045
		}
2046
		while (($tmpbuf = fread($tmpfd, 65536)) !== FALSE)
2047
			fwrite($fd, $tmpbuf);
2048
		fclose($tmpfd);
2049
		unlink($f2p);
2050
	}
2051
	fclose($fd);
2052
	unset($tmpbuf);
2053

    
2054
	return true;
2055
}
2056

    
2057
function version_compare_dates($a, $b) {
2058
	$a_time = strtotime($a);
2059
	$b_time = strtotime($b);
2060

    
2061
	if ((!$a_time) || (!$b_time)) {
2062
		return FALSE;
2063
	} else {
2064
		if ($a_time < $b_time)
2065
			return -1;
2066
		elseif ($a_time == $b_time)
2067
			return 0;
2068
		else
2069
			return 1;
2070
	}
2071
}
2072
function version_get_string_value($a) {
2073
	$strs = array(
2074
		0 => "ALPHA-ALPHA",
2075
		2 => "ALPHA",
2076
		3 => "BETA",
2077
		4 => "B",
2078
		5 => "C",
2079
		6 => "D",
2080
		7 => "RC",
2081
		8 => "RELEASE",
2082
		9 => "*"			// Matches all release levels
2083
	);
2084
	$major = 0;
2085
	$minor = 0;
2086
	foreach ($strs as $num => $str) {
2087
		if (substr($a, 0, strlen($str)) == $str) {
2088
			$major = $num;
2089
			$n = substr($a, strlen($str));
2090
			if (is_numeric($n))
2091
				$minor = $n;
2092
			break;
2093
		}
2094
	}
2095
	return "{$major}.{$minor}";
2096
}
2097
function version_compare_string($a, $b) {
2098
	// Only compare string parts if both versions give a specific release
2099
	// (If either version lacks a string part, assume intended to match all release levels)
2100
	if (isset($a) && isset($b))
2101
		return version_compare_numeric(version_get_string_value($a), version_get_string_value($b));
2102
	else
2103
		return 0;
2104
}
2105
function version_compare_numeric($a, $b) {
2106
	$a_arr = explode('.', rtrim($a, '.0'));
2107
	$b_arr = explode('.', rtrim($b, '.0'));
2108

    
2109
	foreach ($a_arr as $n => $val) {
2110
		if (array_key_exists($n, $b_arr)) {
2111
			// So far so good, both have values at this minor version level. Compare.
2112
			if ($val > $b_arr[$n])
2113
				return 1;
2114
			elseif ($val < $b_arr[$n])
2115
				return -1;
2116
		} else {
2117
			// a is greater, since b doesn't have any minor version here.
2118
			return 1;
2119
		}
2120
	}
2121
	if (count($b_arr) > count($a_arr)) {
2122
		// b is longer than a, so it must be greater.
2123
		return -1;
2124
	} else {
2125
		// Both a and b are of equal length and value.
2126
		return 0;
2127
	}
2128
}
2129
function pfs_version_compare($cur_time, $cur_text, $remote) {
2130
	// First try date compare
2131
	$v = version_compare_dates($cur_time, $remote);
2132
	if ($v === FALSE) {
2133
		// If that fails, try to compare by string
2134
		// Before anything else, simply test if the strings are equal
2135
		if (($cur_text == $remote) || ($cur_time == $remote))
2136
			return 0;
2137
		list($cur_num, $cur_str) = explode('-', $cur_text);
2138
		list($rem_num, $rem_str) = explode('-', $remote);
2139

    
2140
		// First try to compare the numeric parts of the version string.
2141
		$v = version_compare_numeric($cur_num, $rem_num);
2142

    
2143
		// If the numeric parts are the same, compare the string parts.
2144
		if ($v == 0)
2145
			return version_compare_string($cur_str, $rem_str);
2146
	}
2147
	return $v;
2148
}
2149
function process_alias_urltable($name, $url, $freq, $forceupdate=false) {
2150
	global $config;
2151

    
2152
	$urltable_prefix = "/var/db/aliastables/";
2153
	$urltable_filename = $urltable_prefix . $name . ".txt";
2154

    
2155
	// Make the aliases directory if it doesn't exist
2156
	if (!file_exists($urltable_prefix)) {
2157
		mkdir($urltable_prefix);
2158
	} elseif (!is_dir($urltable_prefix)) {
2159
		unlink($urltable_prefix);
2160
		mkdir($urltable_prefix);
2161
	}
2162

    
2163
	// If the file doesn't exist or is older than update_freq days, fetch a new copy.
2164
	if (!file_exists($urltable_filename)
2165
		|| ((time() - filemtime($urltable_filename)) > ($freq * 86400 - 90))
2166
		|| $forceupdate) {
2167

    
2168
		// Try to fetch the URL supplied
2169
		conf_mount_rw();
2170
		unlink_if_exists($urltable_filename . ".tmp");
2171
		$verify_ssl = isset($config['system']['checkaliasesurlcert']);
2172
		if (download_file($url, $urltable_filename . ".tmp", $verify_ssl)) {
2173
			mwexec("/usr/bin/sed -E 's/\;.*//g; /^[[:space:]]*($|#)/d' ". escapeshellarg($urltable_filename . ".tmp") . " > " . escapeshellarg($urltable_filename));
2174
			if (alias_get_type($name) == "urltable_ports") {
2175
				$ports = explode("\n", file_get_contents($urltable_filename));
2176
				$ports = group_ports($ports);
2177
				file_put_contents($urltable_filename, implode("\n", $ports));
2178
			}
2179
			unlink_if_exists($urltable_filename . ".tmp");
2180
		} else
2181
			touch($urltable_filename);
2182
		conf_mount_ro();
2183
		return true;
2184
	} else {
2185
		// File exists, and it doesn't need updated.
2186
		return -1;
2187
	}
2188
}
2189
function get_real_slice_from_glabel($label) {
2190
	$label = escapeshellarg($label);
2191
	return trim(`/sbin/glabel list | /usr/bin/grep -B2 ufs/{$label} | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' '`);
2192
}
2193
function nanobsd_get_boot_slice() {
2194
	return trim(`/sbin/mount | /usr/bin/grep pfsense | /usr/bin/cut -d'/' -f4 | /usr/bin/cut -d' ' -f1`);
2195
}
2196
function nanobsd_get_boot_drive() {
2197
	return trim(`/sbin/glabel list | /usr/bin/grep -B2 ufs/pfsense | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' ' | /usr/bin/cut -d's' -f1`);
2198
}
2199
function nanobsd_get_active_slice() {
2200
	$boot_drive = nanobsd_get_boot_drive();
2201
	$active = trim(`gpart show $boot_drive | grep '\[active\]' | awk '{print $3;}'`);
2202

    
2203
	return "{$boot_drive}s{$active}";
2204
}
2205
function nanobsd_get_size() {
2206
	return strtoupper(file_get_contents("/etc/nanosize.txt"));
2207
}
2208
function nanobsd_switch_boot_slice() {
2209
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
2210
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
2211
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
2212
	nanobsd_detect_slice_info();
2213

    
2214
	if ($BOOTFLASH == $ACTIVE_SLICE) {
2215
		$slice = $TOFLASH;
2216
	} else {
2217
		$slice = $BOOTFLASH;
2218
	}
2219

    
2220
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
2221
	ob_implicit_flush(1);
2222
	if(strstr($slice, "s2")) {
2223
		$ASLICE="2";
2224
		$AOLDSLICE="1";
2225
		$AGLABEL_SLICE="pfsense1";
2226
		$AUFS_ID="1";
2227
		$AOLD_UFS_ID="0";
2228
	} else {
2229
		$ASLICE="1";
2230
		$AOLDSLICE="2";
2231
		$AGLABEL_SLICE="pfsense0";
2232
		$AUFS_ID="0";
2233
		$AOLD_UFS_ID="1";
2234
	}
2235
	$ATOFLASH="{$BOOT_DRIVE}s{$ASLICE}";
2236
	$ACOMPLETE_PATH="{$BOOT_DRIVE}s{$ASLICE}a";
2237
	$ABOOTFLASH="{$BOOT_DRIVE}s{$AOLDSLICE}";
2238
	conf_mount_rw();
2239
	set_single_sysctl("kern.geom.debugflags", "16");
2240
	exec("gpart set -a active -i {$ASLICE} {$BOOT_DRIVE}");
2241
	exec("/usr/sbin/boot0cfg -s {$ASLICE} -v /dev/{$BOOT_DRIVE}");
2242
	// We can't update these if they are mounted now.
2243
	if ($BOOTFLASH != $slice) {
2244
		exec("/sbin/tunefs -L ${AGLABEL_SLICE} /dev/$ACOMPLETE_PATH");
2245
		nanobsd_update_fstab($AGLABEL_SLICE, $ACOMPLETE_PATH, $AOLD_UFS_ID, $AUFS_ID);
2246
	}
2247
	set_single_sysctl("kern.geom.debugflags", "0");
2248
	conf_mount_ro();
2249
}
2250
function nanobsd_clone_slice() {
2251
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
2252
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
2253
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
2254
	nanobsd_detect_slice_info();
2255

    
2256
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
2257
	ob_implicit_flush(1);
2258
	set_single_sysctl("kern.geom.debugflags", "16");
2259
	exec("/bin/dd if=/dev/zero of=/dev/{$TOFLASH} bs=1m count=1");
2260
	exec("/bin/dd if=/dev/{$BOOTFLASH} of=/dev/{$TOFLASH} bs=64k");
2261
	exec("/sbin/tunefs -L {$GLABEL_SLICE} /dev/{$COMPLETE_PATH}");
2262
	$status = nanobsd_update_fstab($GLABEL_SLICE, $COMPLETE_PATH, $OLD_UFS_ID, $UFS_ID);
2263
	set_single_sysctl("kern.geom.debugflags", "0");
2264
	if($status) {
2265
		return false;
2266
	} else {
2267
		return true;
2268
	}
2269
}
2270
function nanobsd_update_fstab($gslice, $complete_path, $oldufs, $newufs) {
2271
	$tmppath = "/tmp/{$gslice}";
2272
	$fstabpath = "/tmp/{$gslice}/etc/fstab";
2273

    
2274
	mkdir($tmppath);
2275
	exec("/sbin/fsck_ufs -y /dev/{$complete_path}");
2276
	exec("/sbin/mount /dev/ufs/{$gslice} {$tmppath}");
2277
	copy("/etc/fstab", $fstabpath);
2278

    
2279
	if (!file_exists($fstabpath)) {
2280
		$fstab = <<<EOF
2281
/dev/ufs/{$gslice} / ufs ro,noatime 1 1
2282
/dev/ufs/cf /cf ufs ro,noatime 1 1
2283
EOF;
2284
		if (file_put_contents($fstabpath, $fstab))
2285
			$status = true;
2286
		else
2287
			$status = false;
2288
	} else {
2289
		$status = exec("sed -i \"\" \"s/pfsense{$oldufs}/pfsense{$newufs}/g\" {$fstabpath}");
2290
	}
2291
	exec("/sbin/umount {$tmppath}");
2292
	rmdir($tmppath);
2293

    
2294
	return $status;
2295
}
2296
function nanobsd_detect_slice_info() {
2297
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
2298
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
2299
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
2300

    
2301
	$BOOT_DEVICE=nanobsd_get_boot_slice();
2302
	$REAL_BOOT_DEVICE=get_real_slice_from_glabel($BOOT_DEVICE);
2303
	$BOOT_DRIVE=nanobsd_get_boot_drive();
2304
	$ACTIVE_SLICE=nanobsd_get_active_slice();
2305

    
2306
	// Detect which slice is active and set information.
2307
	if(strstr($REAL_BOOT_DEVICE, "s1")) {
2308
		$SLICE="2";
2309
		$OLDSLICE="1";
2310
		$GLABEL_SLICE="pfsense1";
2311
		$UFS_ID="1";
2312
		$OLD_UFS_ID="0";
2313

    
2314
	} else {
2315
		$SLICE="1";
2316
		$OLDSLICE="2";
2317
		$GLABEL_SLICE="pfsense0";
2318
		$UFS_ID="0";
2319
		$OLD_UFS_ID="1";
2320
	}
2321
	$TOFLASH="{$BOOT_DRIVE}s{$SLICE}";
2322
	$COMPLETE_PATH="{$BOOT_DRIVE}s{$SLICE}a";
2323
	$COMPLETE_BOOT_PATH="{$BOOT_DRIVE}s{$OLDSLICE}";
2324
	$BOOTFLASH="{$BOOT_DRIVE}s{$OLDSLICE}";
2325
}
2326

    
2327
function nanobsd_friendly_slice_name($slicename) {
2328
	global $g;
2329
	return strtolower(str_ireplace('pfsense', $g['product_name'], $slicename));
2330
}
2331

    
2332
function get_include_contents($filename) {
2333
	if (is_file($filename)) {
2334
		ob_start();
2335
		include $filename;
2336
		$contents = ob_get_contents();
2337
		ob_end_clean();
2338
		return $contents;
2339
	}
2340
	return false;
2341
}
2342

    
2343
/* This xml 2 array function is courtesy of the php.net comment section on xml_parse.
2344
 * it is roughly 4 times faster then our existing pfSense parser but due to the large
2345
 * size of the RRD xml dumps this is required.
2346
 * The reason we do not use it for pfSense is that it does not know about array fields
2347
 * which causes it to fail on array fields with single items. Possible Todo?
2348
 */
2349
function xml2array($contents, $get_attributes = 1, $priority = 'tag')
2350
{
2351
	if (!function_exists('xml_parser_create'))
2352
	{
2353
		return array ();
2354
	}
2355
	$parser = xml_parser_create('');
2356
	xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
2357
	xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
2358
	xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
2359
	xml_parse_into_struct($parser, trim($contents), $xml_values);
2360
	xml_parser_free($parser);
2361
	if (!$xml_values)
2362
		return; //Hmm...
2363
	$xml_array = array ();
2364
	$parents = array ();
2365
	$opened_tags = array ();
2366
	$arr = array ();
2367
	$current = & $xml_array;
2368
	$repeated_tag_index = array ();
2369
	foreach ($xml_values as $data)
2370
	{
2371
		unset ($attributes, $value);
2372
		extract($data);
2373
		$result = array ();
2374
		$attributes_data = array ();
2375
		if (isset ($value))
2376
		{
2377
			if ($priority == 'tag')
2378
				$result = $value;
2379
			else
2380
				$result['value'] = $value;
2381
		}
2382
		if (isset ($attributes) and $get_attributes)
2383
		{
2384
			foreach ($attributes as $attr => $val)
2385
			{
2386
				if ($priority == 'tag')
2387
					$attributes_data[$attr] = $val;
2388
				else
2389
					$result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
2390
			}
2391
		}
2392
		if ($type == "open")
2393
		{
2394
			$parent[$level -1] = & $current;
2395
			if (!is_array($current) or (!in_array($tag, array_keys($current))))
2396
			{
2397
				$current[$tag] = $result;
2398
				if ($attributes_data)
2399
					$current[$tag . '_attr'] = $attributes_data;
2400
				$repeated_tag_index[$tag . '_' . $level] = 1;
2401
				$current = & $current[$tag];
2402
			}
2403
			else
2404
			{
2405
				if (isset ($current[$tag][0]))
2406
				{
2407
					$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
2408
					$repeated_tag_index[$tag . '_' . $level]++;
2409
				}
2410
				else
2411
				{
2412
					$current[$tag] = array (
2413
						$current[$tag],
2414
						$result
2415
						);
2416
					$repeated_tag_index[$tag . '_' . $level] = 2;
2417
					if (isset ($current[$tag . '_attr']))
2418
					{
2419
						$current[$tag]['0_attr'] = $current[$tag . '_attr'];
2420
						unset ($current[$tag . '_attr']);
2421
					}
2422
				}
2423
				$last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
2424
				$current = & $current[$tag][$last_item_index];
2425
			}
2426
		}
2427
		elseif ($type == "complete")
2428
		{
2429
			if (!isset ($current[$tag]))
2430
			{
2431
				$current[$tag] = $result;
2432
				$repeated_tag_index[$tag . '_' . $level] = 1;
2433
				if ($priority == 'tag' and $attributes_data)
2434
					$current[$tag . '_attr'] = $attributes_data;
2435
			}
2436
			else
2437
			{
2438
				if (isset ($current[$tag][0]) and is_array($current[$tag]))
2439
				{
2440
					$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
2441
					if ($priority == 'tag' and $get_attributes and $attributes_data)
2442
					{
2443
						$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
2444
					}
2445
					$repeated_tag_index[$tag . '_' . $level]++;
2446
				}
2447
				else
2448
				{
2449
					$current[$tag] = array (
2450
						$current[$tag],
2451
						$result
2452
						);
2453
					$repeated_tag_index[$tag . '_' . $level] = 1;
2454
					if ($priority == 'tag' and $get_attributes)
2455
					{
2456
						if (isset ($current[$tag . '_attr']))
2457
						{
2458
							$current[$tag]['0_attr'] = $current[$tag . '_attr'];
2459
							unset ($current[$tag . '_attr']);
2460
						}
2461
						if ($attributes_data)
2462
						{
2463
							$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
2464
						}
2465
					}
2466
					$repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
2467
				}
2468
			}
2469
		}
2470
		elseif ($type == 'close')
2471
		{
2472
			$current = & $parent[$level -1];
2473
		}
2474
	}
2475
	return ($xml_array);
2476
}
2477

    
2478
function get_country_name($country_code) {
2479
	if ($country_code != "ALL" && strlen($country_code) != 2)
2480
		return "";
2481

    
2482
	$country_names_xml = "/usr/local/share/mobile-broadband-provider-info/iso_3166-1_list_en.xml";
2483
	$country_names_contents = file_get_contents($country_names_xml);
2484
	$country_names = xml2array($country_names_contents);
2485

    
2486
	if($country_code == "ALL") {
2487
		$country_list = array();
2488
		foreach($country_names['ISO_3166-1_List_en']['ISO_3166-1_Entry'] as $country) {
2489
			$country_list[] = array("code" => $country['ISO_3166-1_Alpha-2_Code_element'],
2490
						"name" => ucwords(strtolower($country['ISO_3166-1_Country_name'])) );
2491
		}
2492
		return $country_list;
2493
	}
2494

    
2495
	foreach ($country_names['ISO_3166-1_List_en']['ISO_3166-1_Entry'] as $country) {
2496
		if ($country['ISO_3166-1_Alpha-2_Code_element'] == strtoupper($country_code)) {
2497
			return ucwords(strtolower($country['ISO_3166-1_Country_name']));
2498
		}
2499
	}
2500
	return "";
2501
}
2502

    
2503
/* sort by interface only, retain the original order of rules that apply to
2504
   the same interface */
2505
function filter_rules_sort() {
2506
	global $config;
2507

    
2508
	/* mark each rule with the sequence number (to retain the order while sorting) */
2509
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
2510
		$config['filter']['rule'][$i]['seq'] = $i;
2511

    
2512
	usort($config['filter']['rule'], "filter_rules_compare");
2513

    
2514
	/* strip the sequence numbers again */
2515
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
2516
		unset($config['filter']['rule'][$i]['seq']);
2517
}
2518
function filter_rules_compare($a, $b) {
2519
	if (isset($a['floating']) && isset($b['floating']))
2520
		return $a['seq'] - $b['seq'];
2521
	else if (isset($a['floating']))
2522
		return -1;
2523
	else if (isset($b['floating']))
2524
		return 1;
2525
	else if ($a['interface'] == $b['interface'])
2526
		return $a['seq'] - $b['seq'];
2527
	else
2528
		return compare_interface_friendly_names($a['interface'], $b['interface']);
2529
}
2530

    
2531
function generate_ipv6_from_mac($mac) {
2532
	$elements = explode(":", $mac);
2533
	if(count($elements) <> 6)
2534
		return false;
2535

    
2536
	$i = 0;
2537
	$ipv6 = "fe80::";
2538
	foreach($elements as $byte) {
2539
		if($i == 0) {
2540
			$hexadecimal =  substr($byte, 1, 2);
2541
			$bitmap = base_convert($hexadecimal, 16, 2);
2542
			$bitmap = str_pad($bitmap, 4, "0", STR_PAD_LEFT);
2543
			$bitmap = substr($bitmap, 0, 2) ."1". substr($bitmap, 3,4);
2544
			$byte = substr($byte, 0, 1) . base_convert($bitmap, 2, 16);
2545
		}
2546
		$ipv6 .= $byte;
2547
		if($i == 1) {
2548
			$ipv6 .= ":";
2549
		}
2550
		if($i == 3) {
2551
			$ipv6 .= ":";
2552
		}
2553
		if($i == 2) {
2554
			$ipv6 .= "ff:fe";
2555
		}
2556

    
2557
		$i++;
2558
	}
2559
	return $ipv6;
2560
}
2561

    
2562
/****f* pfsense-utils/load_mac_manufacturer_table
2563
 * NAME
2564
 *   load_mac_manufacturer_table
2565
 * INPUTS
2566
 *   none
2567
 * RESULT
2568
 *   returns associative array with MAC-Manufacturer pairs
2569
 ******/
2570
function load_mac_manufacturer_table() {
2571
	/* load MAC-Manufacture data from the file */
2572
	$macs = false;
2573
	if (file_exists("/usr/local/share/nmap/nmap-mac-prefixes"))
2574
		$macs=file("/usr/local/share/nmap/nmap-mac-prefixes");
2575
	if ($macs){
2576
		foreach ($macs as $line){
2577
			if (preg_match('/([0-9A-Fa-f]{6}) (.*)$/', $line, $matches)){
2578
				/* store values like this $mac_man['000C29']='VMware' */
2579
				$mac_man["$matches[1]"]=$matches[2];
2580
			}
2581
		}
2582
		return $mac_man;
2583
	} else
2584
		return -1;
2585

    
2586
}
2587

    
2588
/****f* pfsense-utils/is_ipaddr_configured
2589
 * NAME
2590
 *   is_ipaddr_configured
2591
 * INPUTS
2592
 *   IP Address to check.
2593
 *   If ignore_if is a VIP (not carp), vip array index is passed after string _virtualip
2594
 * RESULT
2595
 *   returns true if the IP Address is
2596
 *   configured and present on this device.
2597
*/
2598
function is_ipaddr_configured($ipaddr, $ignore_if = "", $check_localip = false, $check_subnets = false) {
2599
	global $config;
2600

    
2601
	$pos = strpos($ignore_if, '_virtualip');
2602
	if ($pos !== false) {
2603
		$ignore_vip_id = substr($ignore_if, $pos+10);
2604
		$ignore_vip_if = substr($ignore_if, 0, $pos);
2605
	} else {
2606
		$ignore_vip_id = -1;
2607
		$ignore_vip_if = $ignore_if;
2608
	}
2609

    
2610
	$isipv6 = is_ipaddrv6($ipaddr);
2611

    
2612
	if ($check_subnets) {
2613
		$iflist = get_configured_interface_list();
2614
		foreach ($iflist as $if => $ifname) {
2615
			if ($ignore_if == $if)
2616
				continue;
2617

    
2618
			if ($isipv6 === true) {
2619
				$bitmask = get_interface_subnetv6($if);
2620
				$subnet = gen_subnetv6(get_interface_ipv6($if), $bitmask);
2621
			} else {
2622
				$bitmask = get_interface_subnet($if);
2623
				$subnet = gen_subnet(get_interface_ip($if), $bitmask);
2624
			}
2625

    
2626
			if (ip_in_subnet($ipaddr, $subnet . '/' . $bitmask))
2627
				return true;
2628
		}
2629
	} else {
2630
		if ($isipv6 === true)
2631
			$interface_list_ips = get_configured_ipv6_addresses();
2632
		else
2633
			$interface_list_ips = get_configured_ip_addresses();
2634

    
2635
		foreach($interface_list_ips as $if => $ilips) {
2636
			if ($ignore_if == $if)
2637
				continue;
2638
			if (strcasecmp($ipaddr, $ilips) == 0)
2639
				return true;
2640
		}
2641
	}
2642

    
2643
	$interface_list_vips = get_configured_vips_list(true);
2644
	foreach ($interface_list_vips as $id => $vip) {
2645
		/* Skip CARP interfaces here since they were already checked above */
2646
		if ($id == $ignore_vip_id || (strstr($ignore_if, '_vip') && $ignore_vip_if == $vip['if']))
2647
			continue;
2648
		if (strcasecmp($ipaddr, $vip['ipaddr']) == 0)
2649
			return true;
2650
	}
2651

    
2652
	if ($check_localip) {
2653
		if (is_array($config['pptpd']) && !empty($config['pptpd']['localip']) && (strcasecmp($ipaddr, $config['pptpd']['localip']) == 0))
2654
			return true;
2655

    
2656
		if (!is_array($config['l2tp']) && !empty($config['l2tp']['localip']) && (strcasecmp($ipaddr, $config['l2tp']['localip']) == 0))
2657
			return true;
2658
	}
2659

    
2660
	return false;
2661
}
2662

    
2663
/****f* pfsense-utils/pfSense_handle_custom_code
2664
 * NAME
2665
 *   pfSense_handle_custom_code
2666
 * INPUTS
2667
 *   directory name to process
2668
 * RESULT
2669
 *   globs the directory and includes the files
2670
 */
2671
function pfSense_handle_custom_code($src_dir) {
2672
	// Allow extending of the nat edit page and include custom input validation
2673
	if(is_dir("$src_dir")) {
2674
		$cf = glob($src_dir . "/*.inc");
2675
		foreach($cf as $nf) {
2676
			if($nf == "." || $nf == "..")
2677
				continue;
2678
			// Include the extra handler
2679
			include("$nf");
2680
		}
2681
	}
2682
}
2683

    
2684
function set_language($lang = 'en_US', $encoding = "UTF-8") {
2685
	putenv("LANG={$lang}.{$encoding}");
2686
	setlocale(LC_ALL, "{$lang}.{$encoding}");
2687
	textdomain("pfSense");
2688
	bindtextdomain("pfSense","/usr/local/share/locale");
2689
	bind_textdomain_codeset("pfSense","{$lang}.{$encoding}");
2690
}
2691

    
2692
function get_locale_list() {
2693
	$locales = array(
2694
		"en_US" => gettext("English"),
2695
		"pt_BR" => gettext("Portuguese (Brazil)"),
2696
		"tr" => gettext("Turkish"),
2697
	);
2698
	asort($locales);
2699
	return $locales;
2700
}
2701

    
2702
function system_get_language_code() {
2703
	global $config, $g_languages;
2704

    
2705
	// a language code, as per [RFC3066]
2706
	$language = $config['system']['language'];
2707
	//$code = $g_languages[$language]['code'];
2708
	$code = str_replace("_", "-", $language);
2709

    
2710
	if (empty($code))
2711
		$code = "en-US"; // Set default code.
2712

    
2713
	return $code;
2714
}
2715

    
2716
function system_get_language_codeset() {
2717
	global $config, $g_languages;
2718

    
2719
	$language = $config['system']['language'];
2720
	$codeset = $g_languages[$language]['codeset'];
2721

    
2722
	if (empty($codeset))
2723
		$codeset = "UTF-8"; // Set default codeset.
2724

    
2725
	return $codeset;
2726
}
2727

    
2728
/* Available languages/locales */
2729
$g_languages = array (
2730
	"sq"    => array("codeset" => "UTF-8", "desc" => gettext("Albanian")),
2731
	"bg"    => array("codeset" => "UTF-8", "desc" => gettext("Bulgarian")),
2732
	"zh_CN" => array("codeset" => "UTF-8", "desc" => gettext("Chinese (Simplified)")),
2733
	"zh_TW" => array("codeset" => "UTF-8", "desc" => gettext("Chinese (Traditional)")),
2734
	"nl"    => array("codeset" => "UTF-8", "desc" => gettext("Dutch")),
2735
	"da"    => array("codeset" => "UTF-8", "desc" => gettext("Danish")),
2736
	"en_US" => array("codeset" => "UTF-8", "desc" => gettext("English")),
2737
	"fi"    => array("codeset" => "UTF-8", "desc" => gettext("Finnish")),
2738
	"fr"    => array("codeset" => "UTF-8", "desc" => gettext("French")),
2739
	"de"    => array("codeset" => "UTF-8", "desc" => gettext("German")),
2740
	"el"    => array("codeset" => "UTF-8", "desc" => gettext("Greek")),
2741
	"hu"    => array("codeset" => "UTF-8", "desc" => gettext("Hungarian")),
2742
	"it"    => array("codeset" => "UTF-8", "desc" => gettext("Italian")),
2743
	"ja"    => array("codeset" => "UTF-8", "desc" => gettext("Japanese")),
2744
	"ko"    => array("codeset" => "UTF-8", "desc" => gettext("Korean")),
2745
	"lv"    => array("codeset" => "UTF-8", "desc" => gettext("Latvian")),
2746
	"nb"    => array("codeset" => "UTF-8", "desc" => gettext("Norwegian (Bokmal)")),
2747
	"pl"    => array("codeset" => "UTF-8", "desc" => gettext("Polish")),
2748
	"pt_BR" => array("codeset" => "ISO-8859-1", "desc" => gettext("Portuguese (Brazil)")),
2749
	"pt"    => array("codeset" => "UTF-8", "desc" => gettext("Portuguese (Portugal)")),
2750
	"ro"    => array("codeset" => "UTF-8", "desc" => gettext("Romanian")),
2751
	"ru"    => array("codeset" => "UTF-8", "desc" => gettext("Russian")),
2752
	"sl"    => array("codeset" => "UTF-8", "desc" => gettext("Slovenian")),
2753
	"tr"    => array("codeset" => "UTF-8", "desc" => gettext("Turkish")),
2754
	"es"    => array("codeset" => "UTF-8", "desc" => gettext("Spanish")),
2755
	"sv"    => array("codeset" => "UTF-8", "desc" => gettext("Swedish")),
2756
	"sk"    => array("codeset" => "UTF-8", "desc" => gettext("Slovak")),
2757
	"cs"    => array("codeset" => "UTF-8", "desc" => gettext("Czech"))
2758
);
2759

    
2760
function return_hex_ipv4($ipv4) {
2761
	if(!is_ipaddrv4($ipv4))
2762
		return(false);
2763

    
2764
	/* we need the hex form of the interface IPv4 address */
2765
	$ip4arr = explode(".", $ipv4);
2766
	return (sprintf("%02x%02x%02x%02x", $ip4arr[0], $ip4arr[1], $ip4arr[2], $ip4arr[3]));
2767
}
2768

    
2769
function convert_ipv6_to_128bit($ipv6) {
2770
	if(!is_ipaddrv6($ipv6))
2771
		return(false);
2772

    
2773
	$ip6arr = array();
2774
	$ip6prefix = Net_IPv6::uncompress($ipv6);
2775
	$ip6arr = explode(":", $ip6prefix);
2776
	/* binary presentation of the prefix for all 128 bits. */
2777
	$ip6prefixbin = "";
2778
	foreach($ip6arr as $element) {
2779
		$ip6prefixbin .= sprintf("%016b", hexdec($element));
2780
	}
2781
	return($ip6prefixbin);
2782
}
2783

    
2784
function convert_128bit_to_ipv6($ip6bin) {
2785
	if(strlen($ip6bin) <> 128)
2786
		return(false);
2787

    
2788
	$ip6arr = array();
2789
	$ip6binarr = array();
2790
	$ip6binarr = str_split($ip6bin, 16);
2791
	foreach($ip6binarr as $binpart)
2792
		$ip6arr[] = dechex(bindec($binpart));
2793
	$ip6addr = Net_IPv6::compress(implode(":", $ip6arr));
2794

    
2795
	return($ip6addr);
2796
}
2797

    
2798

    
2799
/* Returns the calculated bit length of the prefix delegation from the WAN interface */
2800
/* DHCP-PD is variable, calculate from the prefix-len on the WAN interface */
2801
/* 6rd is variable, calculate from 64 - (v6 prefixlen - (32 - v4 prefixlen)) */
2802
/* 6to4 is 16 bits, e.g. 65535 */
2803
function calculate_ipv6_delegation_length($if) {
2804
	global $config;
2805

    
2806
	if(!is_array($config['interfaces'][$if]))
2807
		return false;
2808

    
2809
	switch($config['interfaces'][$if]['ipaddrv6']) {
2810
		case "6to4":
2811
			$pdlen = 16;
2812
			break;
2813
		case "6rd":
2814
			$rd6cfg = $config['interfaces'][$if];
2815
			$rd6plen = explode("/", $rd6cfg['prefix-6rd']);
2816
			$pdlen = (64 - ($rd6plen[1] + (32 - $rd6cfg['prefix-6rd-v4plen'])));
2817
			break;
2818
		case "dhcp6":
2819
			$dhcp6cfg = $config['interfaces'][$if];
2820
			$pdlen = $dhcp6cfg['dhcp6-ia-pd-len'];
2821
			break;
2822
		default:
2823
			$pdlen = 0;
2824
			break;
2825
	}
2826
	return($pdlen);
2827
}
2828

    
2829
function huawei_rssi_to_string($rssi) {
2830
	$dbm = array();
2831
	$i = 0;
2832
	$dbstart = -113;
2833
	while($i < 32) {
2834
		$dbm[$i] = $dbstart + ($i * 2);
2835
		$i++;
2836
	}
2837
	$percent = round(($rssi / 31) * 100);
2838
	$string = "rssi:{$rssi} level:{$dbm[$rssi]}dBm percent:{$percent}%";
2839
	return $string;
2840
}
2841

    
2842
function huawei_mode_to_string($mode, $submode) {
2843
	$modes[0] = "None";
2844
	$modes[1] = "AMPS";
2845
	$modes[2] = "CDMA";
2846
	$modes[3] = "GSM/GPRS";
2847
	$modes[4] = "HDR";
2848
	$modes[5] = "WCDMA";
2849
	$modes[6] = "GPS";
2850

    
2851
	$submodes[0] = "No Service";
2852
	$submodes[1] = "GSM";
2853
	$submodes[2] = "GPRS";
2854
	$submodes[3] = "EDGE";
2855
	$submodes[4] = "WCDMA";
2856
	$submodes[5] = "HSDPA";
2857
	$submodes[6] = "HSUPA";
2858
	$submodes[7] = "HSDPA+HSUPA";
2859
	$submodes[8] = "TD-SCDMA";
2860
	$submodes[9] = "HSPA+";
2861
	$string = "{$modes[$mode]}, {$submodes[$submode]} Mode";
2862
	return $string;
2863
}
2864

    
2865
function huawei_service_to_string($state) {
2866
	$modes[0] = "No";
2867
	$modes[1] = "Restricted";
2868
	$modes[2] = "Valid";
2869
	$modes[3] = "Restricted Regional";
2870
	$modes[4] = "Powersaving";
2871
	$string = "{$modes[$state]} Service";
2872
	return $string;
2873
}
2874

    
2875
function huawei_simstate_to_string($state) {
2876
	$modes[0] = "Invalid SIM/locked";
2877
	$modes[1] = "Valid SIM";
2878
	$modes[2] = "Invalid SIM CS";
2879
	$modes[3] = "Invalid SIM PS";
2880
	$modes[4] = "Invalid SIM CS/PS";
2881
	$modes[255] = "Missing SIM";
2882
	$string = "{$modes[$state]} State";
2883
	return $string;
2884
}
2885

    
2886
function zte_rssi_to_string($rssi) {
2887
	return huawei_rssi_to_string($rssi);
2888
}
2889

    
2890
function zte_mode_to_string($mode, $submode) {
2891
	$modes[0] = "No Service";
2892
	$modes[1] = "Limited Service";
2893
	$modes[2] = "GPRS";
2894
	$modes[3] = "GSM";
2895
	$modes[4] = "UMTS";
2896
	$modes[5] = "EDGE";
2897
	$modes[6] = "HSDPA";
2898

    
2899
	$submodes[0] = "CS_ONLY";
2900
	$submodes[1] = "PS_ONLY";
2901
	$submodes[2] = "CS_PS";
2902
	$submodes[3] = "CAMPED";
2903
	$string = "{$modes[$mode]}, {$submodes[$submode]} Mode";
2904
	return $string;
2905
}
2906

    
2907
function zte_service_to_string($state) {
2908
	$modes[0] = "Initializing";
2909
	$modes[1] = "Network Lock error";
2910
	$modes[2] = "Network Locked";
2911
	$modes[3] = "Unlocked or correct MCC/MNC";
2912
	$string = "{$modes[$state]} Service";
2913
	return $string;
2914
}
2915

    
2916
function zte_simstate_to_string($state) {
2917
	$modes[0] = "No action";
2918
	$modes[1] = "Network lock";
2919
	$modes[2] = "(U)SIM card lock";
2920
	$modes[3] = "Network Lock and (U)SIM card Lock";
2921
	$string = "{$modes[$state]} State";
2922
	return $string;
2923
}
2924

    
2925
function get_configured_pppoe_server_interfaces() {
2926
	global $config;
2927
	$iflist = array();
2928
	if (is_array($config['pppoes']['pppoe'])) {
2929
		foreach($config['pppoes']['pppoe'] as $pppoe) {
2930
			if ($pppoe['mode'] == "server") {
2931
				$int = "poes". $pppoe['pppoeid'];
2932
				$iflist[$int] = strtoupper($int);
2933
			}
2934
		}
2935
	}
2936
	return $iflist;
2937
}
2938

    
2939
function get_pppoes_child_interfaces($ifpattern) {
2940
	$if_arr = array();
2941
	if($ifpattern == "")
2942
		return;
2943

    
2944
	exec("ifconfig", $out, $ret);
2945
	foreach($out as $line) {
2946
		if(preg_match("/^({$ifpattern}[0-9]+):/i", $line, $match)) {
2947
			$if_arr[] = $match[1];
2948
		}
2949
	}
2950
	return $if_arr;
2951

    
2952
}
2953

    
2954
/****f* pfsense-utils/pkg_call_plugins
2955
 * NAME
2956
 *   pkg_call_plugins
2957
 * INPUTS
2958
 *   $plugin_type value used to search in package configuration if the plugin is used, also used to create the function name
2959
 *   $plugin_params parameters to pass to the plugin function for passing multiple parameters a array can be used.
2960
 * RESULT
2961
 *   returns associative array results from the plugin calls for each package
2962
 * NOTES
2963
 *   This generic function can be used to notify or retrieve results from functions that are defined in packages.
2964
 ******/
2965
function pkg_call_plugins($plugin_type, $plugin_params) {
2966
	global $g, $config;
2967
	$results = array();
2968
	if (!is_array($config['installedpackages']['package']))
2969
		return $results;
2970
	foreach ($config['installedpackages']['package'] as $package) {
2971
		if(!file_exists("/usr/local/pkg/" . $package['configurationfile']))
2972
			continue;
2973
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], 'packagegui');
2974
		$pkgname = substr(reverse_strrchr($package['configurationfile'], "."),0,-1);
2975
		if (is_array($pkg_config['plugins']['item']))
2976
			foreach ($pkg_config['plugins']['item'] as $plugin) {
2977
				if ($plugin['type'] == $plugin_type) {
2978
					if (file_exists($pkg_config['include_file']))
2979
						require_once($pkg_config['include_file']);
2980
					else
2981
						continue;
2982
					$plugin_function = $pkgname . '_'. $plugin_type;
2983
					$results[$pkgname] = @eval($plugin_function($plugin_params));
2984
				}
2985
			}
2986
	}
2987
	return $results;
2988
}
2989

    
2990
/* Function to find and return the active XML RPC base URL to avoid code duplication */
2991
function get_active_xml_rpc_base_url() {
2992
	global $config, $g;
2993
	/* If the user has activated the option to enable an alternate xmlrpcbaseurl, and it's not empty, then use it */
2994
	if (isset($config['system']['altpkgrepo']['enable']) && !empty($config['system']['altpkgrepo']['xmlrpcbaseurl'])) {
2995
		return $config['system']['altpkgrepo']['xmlrpcbaseurl'];
2996
	} else {
2997
		return $g['xmlrpcbaseurl'];
2998
	}
2999
}
3000

    
3001
?>
(39-39/67)