Project

General

Profile

Download (20.2 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2 dbdd1456 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	guiconfig.inc
5 be81829f Scott Ullrich
	by Scott Ullrich, Copyright 2004, All rights reserved.
6
	originally based on of m0n0wall (http://m0n0.ch/wall)
7
8 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
9
	All rights reserved.
10 be81829f Scott Ullrich
11 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13 be81829f Scott Ullrich
14 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16 be81829f Scott Ullrich
17 5b237745 Scott Ullrich
	2. Redistributions in binary form must reproduce the above copyright
18
	   notice, this list of conditions and the following disclaimer in the
19
	   documentation and/or other materials provided with the distribution.
20 be81829f Scott Ullrich
21 5b237745 Scott Ullrich
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
	POSSIBILITY OF SUCH DAMAGE.
31
*/
32
33
/* make sure nothing is cached */
34
if (!$omit_nocacheheaders) {
35
	header("Expires: 0");
36
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
37
	header("Cache-Control: no-store, no-cache, must-revalidate");
38
	header("Cache-Control: post-check=0, pre-check=0", false);
39
	header("Pragma: no-cache");
40
}
41
42 97bea981 Bill Marquette
/* Include authentication routines */
43
/* THIS MUST BE ABOVE ALL OTHER CODE */
44
require_once("auth.inc");
45
46 5b237745 Scott Ullrich
/* parse the configuration and include all configuration functions */
47
require_once("config.inc");
48
require_once("functions.inc");
49
50 31977165 Colin Smith
/*
51
 *   if user has selected a custom template, use it.
52
 *   otherwise default to pfsense tempalte
53
 */
54
if($config['theme'] <> "")
55
        $g['theme'] = $config['theme'];
56
else
57
        $g['theme'] = "pfsense";
58
59 3e44d22a Scott Ullrich
$d_landirty_path = $g['varrun_path'] . "/lan.conf.dirty";
60 8ede0ae8 Scott Ullrich
$d_pppoeuserdirty_path = $g['varrun_path'] . "/vpn-pppoe-users-edit.dirty";
61 ceca2719 Scott Ullrich
$d_hostsdirty_path = $g['varrun_path'] . "/hosts.dirty";
62 5b237745 Scott Ullrich
$d_natconfdirty_path = $g['varrun_path'] . "/nat.conf.dirty";
63
$d_filterconfdirty_path = $g['varrun_path'] . "/filter.conf.dirty";
64
$d_ipsecconfdirty_path = $g['varrun_path'] . "/ipsec.conf.dirty";
65
$d_shaperconfdirty_path = $g['varrun_path'] . "/shaper.conf.dirty";
66
$d_pptpuserdirty_path = $g['varrun_path'] . "/pptpd.user.dirty";
67 0c2b5df7 Scott Ullrich
$d_dnsmasqdirty_path = $g['varrun_path'] . "/dnsmasq.dirty";
68 5b237745 Scott Ullrich
$d_staticmapsdirty_path = $g['varrun_path'] . "/staticmaps.dirty";
69
$d_staticroutesdirty_path = $g['varrun_path'] . "/staticroutes.dirty";
70
$d_aliasesdirty_path = $g['varrun_path'] . "/aliases.dirty";
71
$d_proxyarpdirty_path = $g['varrun_path'] . "/proxyarp.dirty";
72
$d_fwupenabled_path = $g['varrun_path'] . "/fwup.enabled";
73
$d_firmwarelock_path = $g['varrun_path'] . "/firmware.lock";
74
$d_sysrebootreqd_path = $g['varrun_path'] . "/sysreboot.reqd";
75
$d_passthrumacsdirty_path = $g['varrun_path'] . "/passthrumacs.dirty";
76
$d_allowedipsdirty_path = $g['varrun_path'] . "/allowedips.dirty";
77
$d_ovpnclidirty_path = $g['varrun_path'] . "/ovpnclient.dirty";
78 1425e067 Bill Marquette
$d_vipconfdirty_path = $g['varrun_path'] . "/vip.conf.dirty";
79 f8c08f21 Bill Marquette
$d_vsconfdirty_path = $g['varrun_path'] . "/vs.conf.dirty";
80 a32dea16 Scott Ullrich
$d_shaperconfdirty_path = $g['varrun_path'] . "/shaper.conf.dirty";
81
82 fb7860e0 Scott Ullrich
/* OpenVPN Directories */
83
$d_ovpnsrvdirty_path = "/tmp/ovpn-srv.dirty";
84
$d_ovpncrldirty_path = "/tmp/ovpn-crl.dirty";
85
$d_ovpnclidirty_path = "/tmp/ovpn-cli.dirty";
86 b005c738 Scott Ullrich
/* used by progress bar */
87
$lastseen = "-1";
88 5b237745 Scott Ullrich
89
if (file_exists($d_firmwarelock_path)) {
90
	if (!$d_isfwfile) {
91
		header("Location: system_firmware.php");
92
		exit;
93
	} else {
94
		return;
95
	}
96
}
97
98
/* some well knows ports */
99 fb2abb3a Scott Ullrich
$wkports = array(3389 => "MS RDP",
100
	21 => "FTP",
101
	22 => "SSH",
102
	23 => "Telnet",
103
	25 => "SMTP",
104
	53 => "DNS",
105
	80 => "HTTP",
106
	110 => "POP3",
107
	113 => "IDENT/AUTH",
108
	123 => "NTP",
109
	143 => "IMAP",
110
	443 => "HTTPS",
111
	993 => "IMAP/S");
112 5b237745 Scott Ullrich
113 e825920f Scott Ullrich
$specialnets = array("wanip" => "WAN address", "lanip" => "LAN address", "lan" => "LAN net", "pptp" => "PPTP clients");
114 5b237745 Scott Ullrich
115
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
116
	$specialnets['opt' . $i] = $config['interfaces']['opt' . $i]['descr'] . " net";
117
}
118 be81829f Scott Ullrich
119 5b237745 Scott Ullrich
$medias = array("auto" => "autoselect", "100full" => "100BASE-TX full-duplex",
120
	"100half" => "100BASE-TX half-duplex", "10full" => "10BASE-T full-duplex",
121
	"10half" => "10BASE-T half-duplex");
122 be81829f Scott Ullrich
123 5b237745 Scott Ullrich
/* platforms that support firmware updating */
124 275cbd3f Scott Ullrich
$fwupplatforms = array('pfSense', 'net45xx', 'net48xx', 'generic-pc', 'embedded', 'wrap');
125 5b237745 Scott Ullrich
126
/* IPsec defines */
127
$my_identifier_list = array('myaddress' => 'My IP address',
128 ee7f808e Scott Ullrich
				'address' => 'IP address',
129
				'fqdn' => 'Domain name',
130
				'user_fqdn' => 'User FQDN',
131
				'dyn_dns' => 'Dynamic DNS');
132 5b237745 Scott Ullrich
133 be81829f Scott Ullrich
$p1_ealgos = array('des' => 'DES', '3des' => '3DES', 'blowfish' => 'Blowfish',
134 9aa4a9b1 Scott Ullrich
		'cast128' => 'CAST128','rijndael' => 'Rijndael (AES)', 'rijndael 256' => 'Rijndael 256');
135 5b237745 Scott Ullrich
$p2_ealgos = array('des' => 'DES', '3des' => '3DES', 'blowfish' => 'Blowfish',
136 9aa4a9b1 Scott Ullrich
		'cast128' => 'CAST128', 'rijndael' => 'Rijndael (AES)', 'rijndael 256' => 'Rijndael 256');
137
138 5b237745 Scott Ullrich
$p1_halgos = array('sha1' => 'SHA1', 'md5' => 'MD5');
139 075b2138 Scott Ullrich
$p1_authentication_methods = array('pre_shared_key' => 'Pre-shared key', 'rsasig' => 'RSA signature');
140 5b237745 Scott Ullrich
$p2_halgos = array('hmac_sha1' => 'SHA1', 'hmac_md5' => 'MD5');
141
$p2_protos = array('esp' => 'ESP', 'ah' => 'AH');
142
$p2_pfskeygroups = array('0' => 'off', '1' => '1', '2' => '2', '5' => '5');
143
144
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
145 be81829f Scott Ullrich
146
	/* check for bad control characters */
147 5b237745 Scott Ullrich
	foreach ($postdata as $pn => $pd) {
148
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
149
			$input_errors[] = "The field '" . $pn . "' contains invalid characters.";
150
		}
151
	}
152 be81829f Scott Ullrich
153 5b237745 Scott Ullrich
	for ($i = 0; $i < count($reqdfields); $i++) {
154 d1cfae7b Scott Ullrich
		if ($_POST[$reqdfields[$i]] == "") {
155 5b237745 Scott Ullrich
			$input_errors[] = "The field '" . $reqdfieldsn[$i] . "' is required.";
156
		}
157 be81829f Scott Ullrich
	}
158 5b237745 Scott Ullrich
}
159
160
function print_input_errors($input_errors) {
161 30af342f Erik Kristensen
	global $g;
162 5b237745 Scott Ullrich
	echo "<p><table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">\n";
163 677c0869 Erik Kristensen
	echo "<tr><td bgcolor=\"#990000\" width=\"36\" align=\"center\" valign=\"top\"><img src=\"./themes/".$g['theme']."/images/icons/icon_error.gif\" width=\"28\" height=\"32\"></td>\n";
164 5b237745 Scott Ullrich
	echo "<td bgcolor=\"#FFD9D1\" style=\"padding-left: 8px; padding-top: 6px\">";
165 be81829f Scott Ullrich
166 5b237745 Scott Ullrich
	echo "<span class=\"errmsg\"><p>The following input errors were detected:<ul>\n";
167
	foreach ($input_errors as $ierr) {
168
		echo "<li>" . htmlspecialchars($ierr) . "</li>\n";
169
	}
170
	echo "</ul></span>";
171 be81829f Scott Ullrich
172 76dddec6 Scott Ullrich
	echo "</td></tr></table></p>&nbsp;<br>";
173 5b237745 Scott Ullrich
}
174
175
function exec_rc_script($scriptname) {
176 be81829f Scott Ullrich
177 5b237745 Scott Ullrich
	global $d_sysrebootreqd_path;
178 be81829f Scott Ullrich
179 5b237745 Scott Ullrich
	if (file_exists($d_sysrebootreqd_path))
180
		return 0;
181 767a716e Scott Ullrich
	$execoutput = "";
182
	$retval = "";
183 5b237745 Scott Ullrich
	exec($scriptname . " >/dev/null 2>&1", $execoutput, $retval);
184 be81829f Scott Ullrich
	return $retval;
185 5b237745 Scott Ullrich
}
186
187
function exec_rc_script_async($scriptname) {
188 be81829f Scott Ullrich
189 5b237745 Scott Ullrich
	global $d_sysrebootreqd_path;
190 767a716e Scott Ullrich
	$execoutput = "";
191
	$retval = "";
192 5b237745 Scott Ullrich
	if (file_exists($d_sysrebootreqd_path))
193
		return 0;
194 be81829f Scott Ullrich
195 5b237745 Scott Ullrich
	exec("nohup " . $scriptname . " >/dev/null 2>&1 &", $execoutput, $retval);
196 be81829f Scott Ullrich
	return $retval;
197 5b237745 Scott Ullrich
}
198
199
function verify_gzip_file($fname) {
200
201 1ba03d69 Scott Ullrich
    $returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
202 5b237745 Scott Ullrich
	if ($returnvar != 0)
203
		return 0;
204
	else
205
		return 1;
206
}
207
208
function print_info_box_np($msg) {
209 30af342f Erik Kristensen
	global $g;
210 d07299bc Scott Ullrich
	echo "<table height=\"32\" width=\"100%\">\n";
211 c438d2f8 Scott Ullrich
	echo "  <tr>\n";
212
	echo "   <td>\n";
213
	echo "      <div style='background-color:#990000' id='redbox'>\n";
214
	echo "       <table width='100%'><tr><td width='8%'>\n";
215 cb2deda8 Scott Ullrich
	echo "        &nbsp;&nbsp;&nbsp;<img style='vertical-align:middle' src=\"/themes/".$g['theme']."/images/icons/icon_exclam.gif\" width=\"28\" height=\"32\">\n";
216 a0509c58 Scott Ullrich
	echo "        </td>\n";
217
	echo "        <td width='70%'><font color='white'><b>{$msg}</b></font>\n";
218
	echo "        </td>";
219 541207da Scott Ullrich
	if(stristr($msg, "apply") == true) {
220
		echo "         <td>";
221
		echo "           <input name=\"apply\" type=\"submit\" class=\"formbtn\" id=\"apply\" value=\"Apply changes\">\n";
222
		echo "         </td>";
223
	}
224 a0509c58 Scott Ullrich
	echo "        </tr></table>\n";
225
	echo "       </div>\n";
226 c438d2f8 Scott Ullrich
	echo "    </td>\n";
227
	echo "</table>\n";
228
	echo "<script type=\"text/javascript\">\n";
229 d07299bc Scott Ullrich
	echo "NiftyCheck();\n";
230
	echo "Rounded(\"div#redbox\",\"all\",\"#FFF\",\"#990000\",\"smooth\");\n";
231
	echo "Rounded(\"td#blackbox\",\"all\",\"#FFF\",\"#000000\",\"smooth\");\n";
232 c438d2f8 Scott Ullrich
	echo "</script>\n";
233 cd4087bf Scott Ullrich
	echo "\n<br>\n";
234 5b237745 Scott Ullrich
}
235
236
function print_info_box($msg) {
237
	echo "<p>";
238
	print_info_box_np($msg);
239
	echo "</p>";
240
}
241
242
function format_bytes($bytes) {
243
	if ($bytes >= 1073741824) {
244
		return sprintf("%.2f GB", $bytes/1073741824);
245
	} else if ($bytes >= 1048576) {
246
		return sprintf("%.2f MB", $bytes/1048576);
247
	} else if ($bytes >= 1024) {
248
		return sprintf("%.0f KB", $bytes/1024);
249
	} else {
250
		return sprintf("%d bytes", $bytes);
251
	}
252
}
253
254
function get_std_save_message($ok) {
255
	global $d_sysrebootreqd_path;
256
257 4739bd06 Scott Ullrich
	return "The changes have been applied successfully.  You can also <a href='status_filter_reload.php'>monitor</a> the filter reload progress.";
258 5b237745 Scott Ullrich
}
259
260
function pprint_address($adr) {
261
	global $specialnets;
262
263
	if (isset($adr['any'])) {
264
		$padr = "*";
265
	} else if ($adr['network']) {
266 369578b1 Scott Ullrich
		if (preg_match("/opt[0-999]ip/", $adr['network'])) {
267
			$padr = "Interface IP address";
268
		} else {
269
			$padr = $specialnets[$adr['network']];
270
		}
271 5b237745 Scott Ullrich
	} else {
272
		$padr = $adr['address'];
273
	}
274 be81829f Scott Ullrich
275 5b237745 Scott Ullrich
	if (isset($adr['not']))
276
		$padr = "! " . $padr;
277 be81829f Scott Ullrich
278 5b237745 Scott Ullrich
	return $padr;
279
}
280
281
function pprint_port($port) {
282
	global $wkports;
283
284
	$pport = "";
285 be81829f Scott Ullrich
286 5b237745 Scott Ullrich
	if (!$port)
287
		echo "*";
288
	else {
289
		$srcport = explode("-", $port);
290
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
291
			$pport = $srcport[0];
292
			if ($wkports[$srcport[0]]) {
293
				$pport .= " (" . $wkports[$srcport[0]] . ")";
294
			}
295
		} else
296
			$pport .= $srcport[0] . " - " . $srcport[1];
297
	}
298 be81829f Scott Ullrich
299 5b237745 Scott Ullrich
	return $pport;
300
}
301
302 7afb67e0 Scott Ullrich
function captiveportal_users_sort() {
303
	global $g, $config;
304 d1cfae7b Scott Ullrich
305 7afb67e0 Scott Ullrich
	function cpusercmp($a, $b) {
306
		return strcasecmp($a['name'], $b['name']);
307
	}
308 d1cfae7b Scott Ullrich
309 7afb67e0 Scott Ullrich
	usort($config['captiveportal']['user'], "cpusercmp");
310
}
311
312 5b237745 Scott Ullrich
/* sort by interface only, retain the original order of rules that apply to
313
   the same interface */
314
function filter_rules_sort() {
315 63637de9 Bill Marquette
	global $config;
316 be81829f Scott Ullrich
317 5b237745 Scott Ullrich
	/* mark each rule with the sequence number (to retain the order while sorting) */
318
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
319
		$config['filter']['rule'][$i]['seq'] = $i;
320 be81829f Scott Ullrich
321 5b237745 Scott Ullrich
	function filtercmp($a, $b) {
322
		if ($a['interface'] == $b['interface'])
323
			return $a['seq'] - $b['seq'];
324
		else
325
			return -strcmp($a['interface'], $b['interface']);
326
	}
327 be81829f Scott Ullrich
328 5b237745 Scott Ullrich
	usort($config['filter']['rule'], "filtercmp");
329 be81829f Scott Ullrich
330 5b237745 Scott Ullrich
	/* strip the sequence numbers again */
331
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
332
		unset($config['filter']['rule'][$i]['seq']);
333
}
334
335
function nat_rules_sort() {
336 63637de9 Bill Marquette
	global $config;
337 be81829f Scott Ullrich
338 5b237745 Scott Ullrich
	function natcmp($a, $b) {
339
		if ($a['external-address'] == $b['external-address']) {
340
			if ($a['protocol'] == $b['protocol']) {
341
				if ($a['external-port'] == $b['external-port']) {
342
					return 0;
343
				} else {
344
					return ($a['external-port'] - $b['external-port']);
345
				}
346
			} else {
347
				return strcmp($a['protocol'], $b['protocol']);
348
			}
349
		} else if (!$a['external-address'])
350
			return 1;
351
		else if (!$b['external-address'])
352
			return -1;
353
		else
354
			return ipcmp($a['external-address'], $b['external-address']);
355
	}
356 be81829f Scott Ullrich
357 5b237745 Scott Ullrich
	usort($config['nat']['rule'], "natcmp");
358
}
359
360
function nat_1to1_rules_sort() {
361
	global $g, $config;
362 be81829f Scott Ullrich
363 5b237745 Scott Ullrich
	function nat1to1cmp($a, $b) {
364
		return ipcmp($a['external'], $b['external']);
365
	}
366 be81829f Scott Ullrich
367 5b237745 Scott Ullrich
	usort($config['nat']['onetoone'], "nat1to1cmp");
368
}
369
370
function nat_server_rules_sort() {
371
	global $g, $config;
372 be81829f Scott Ullrich
373 5b237745 Scott Ullrich
	function natservercmp($a, $b) {
374
		return ipcmp($a['ipaddr'], $b['ipaddr']);
375
	}
376 be81829f Scott Ullrich
377 5b237745 Scott Ullrich
	usort($config['nat']['servernat'], "natservercmp");
378
}
379
380
function nat_out_rules_sort() {
381
	global $g, $config;
382 be81829f Scott Ullrich
383 5b237745 Scott Ullrich
	function natoutcmp($a, $b) {
384
		return strcmp($a['source']['network'], $b['source']['network']);
385
	}
386 be81829f Scott Ullrich
387 5b237745 Scott Ullrich
	usort($config['nat']['advancedoutbound']['rule'], "natoutcmp");
388
}
389
390
function pptpd_users_sort() {
391
	global $g, $config;
392 be81829f Scott Ullrich
393 5b237745 Scott Ullrich
	function usercmp($a, $b) {
394
		return strcasecmp($a['name'], $b['name']);
395
	}
396 be81829f Scott Ullrich
397 5b237745 Scott Ullrich
	usort($config['pptpd']['user'], "usercmp");
398
}
399
400 4f1b515b Scott Ullrich
function pppoe_users_sort() {
401
	global $g, $config;
402
403
	function usercmp($a, $b) {
404
		return strcasecmp($a['name'], $b['name']);
405
	}
406
407
	usort($config['pppoe']['user'], "usercmp");
408
}
409
410 5b237745 Scott Ullrich
function staticroutes_sort() {
411
	global $g, $config;
412
413
	function staticroutecmp($a, $b) {
414
		return strcmp($a['network'], $b['network']);
415
	}
416
417
	usort($config['staticroutes']['route'], "staticroutecmp");
418
}
419
420
function hosts_sort() {
421
	global $g, $config;
422
423
	function hostcmp($a, $b) {
424
		return strcasecmp($a['host'], $b['host']);
425
	}
426
427
	usort($config['dnsmasq']['hosts'], "hostcmp");
428
}
429
430
function staticmaps_sort($if) {
431
	global $g, $config;
432
433
	function staticmapcmp($a, $b) {
434
		return ipcmp($a['ipaddr'], $b['ipaddr']);
435
	}
436
437
	usort($config['dhcpd'][$if]['staticmap'], "staticmapcmp");
438
}
439
440
function aliases_sort() {
441
	global $g, $config;
442
443
	function aliascmp($a, $b) {
444
		return strcmp($a['name'], $b['name']);
445
	}
446
447
	usort($config['aliases']['alias'], "aliascmp");
448
}
449
450
function ipsec_mobilekey_sort() {
451
	global $g, $config;
452
453
	function mobilekeycmp($a, $b) {
454
		return strcmp($a['ident'][0], $b['ident'][0]);
455
	}
456
457
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
458
}
459
460
function proxyarp_sort() {
461
	global $g, $config;
462
463
	function proxyarpcmp($a, $b) {
464
		if (isset($a['network']))
465
			list($ast,$asn) = explode("/", $a['network']);
466
		else if (isset($a['range'])) {
467
			$ast = $a['range']['from'];
468
			$asn = 32;
469
		}
470
		if (isset($b['network']))
471
			list($bst,$bsn) = explode("/", $b['network']);
472
		else if (isset($b['range'])) {
473
			$bst = $b['range']['from'];
474
			$bsn = 32;
475
		}
476
		if (ipcmp($ast, $bst) == 0)
477
			return ($asn - $bsn);
478
		else
479
			return ipcmp($ast, $bst);
480
	}
481
482
	usort($config['proxyarp']['proxyarpnet'], "proxyarpcmp");
483
}
484
485
function passthrumacs_sort() {
486
	global $g, $config;
487
488
	function passthrumacscmp($a, $b) {
489
		return strcmp($a['mac'], $b['mac']);
490
	}
491 be81829f Scott Ullrich
492 5b237745 Scott Ullrich
	usort($config['captiveportal']['passthrumac'],"passthrumacscmp");
493
}
494
495 7afb67e0 Scott Ullrich
function cpelements_sort() {
496
	global $g, $config;
497
498
	function cpelementscmp($a, $b) {
499
		return strcasecmp($a['name'], $b['name']);
500
	}
501 d1cfae7b Scott Ullrich
502 7afb67e0 Scott Ullrich
	usort($config['captiveportal']['element'],"cpelementscmp");
503
}
504
505 5b237745 Scott Ullrich
function allowedips_sort() {
506
	global $g, $config;
507
508
	function allowedipscmp($a, $b) {
509
		return strcmp($a['ip'], $b['ip']);
510
	}
511 be81829f Scott Ullrich
512 5b237745 Scott Ullrich
	usort($config['captiveportal']['allowedip'],"allowedipscmp");
513
}
514
515
function wol_sort() {
516
	global $g, $config;
517
518
	function wolcmp($a, $b) {
519
		return strcmp($a['descr'], $b['descr']);
520
	}
521 be81829f Scott Ullrich
522 5b237745 Scott Ullrich
	usort($config['wol']['wolentry'], "wolcmp");
523
}
524
525 6577d782 Scott Ullrich
function sort_rule_by_queue_priority() {
526
	global $g, $config;
527
	global $queues;
528
529
	function rqpcmp($a, $b) {
530
		global $queues;
531
532
		$ra = $queues[$a['outqueue']];
533
		$rb = $queues[$b['outqueue']];
534
535
		if ($ra == $rb)
536
			return 0;
537 d1cfae7b Scott Ullrich
		/* reverse sort - we want higher priority first */
538 6577d782 Scott Ullrich
		return ($ra < $rb) ? 1 : -1;
539
	}
540
541
	foreach($config['shaper']['queue'] as $q) {
542
		$queues[$q['name']] = $q['priority'];
543
	}
544
545
	usort($config['shaper']['rule'], "rqpcmp");
546
}
547 5b237745 Scott Ullrich
function gentitle($pgname) {
548
	global $config;
549
	return $config['system']['hostname'] . "." . $config['system']['domain'] . " - " . $pgname;
550
}
551
552 d345bd39 Bill Marquette
/* update the changedesc and changecount(er) variables */
553
function update_changedesc($update) {
554
	global $changedesc;
555
	global $changecount;
556
557
	$changedesc .= " {$update}";
558
	$changecount++;
559
}
560
561 15c37284 Colin Smith
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
562 23f1e22a Colin Smith
	global $g, $config;
563 767a716e Scott Ullrich
    $sor = isset($config['syslog']['reverse']) ? "-r" : "";
564
    $logarr = "";
565 51615fd3 Scott Ullrich
	exec("/usr/sbin/clog {$logfile} | grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
566 cb655be7 Scott Ullrich
567 dc7ddc0e Colin Smith
	if(is_array($grepfor)) {
568
		 $i = 0;
569 d1cfae7b Scott Ullrich
                 foreach($grepfor as $agrep) {
570
                         $regexp = "/" . $agrep . "/i";
571
                         if($grepinvert[$i]) {
572
                                 $logarr = preg_grep($regexp, $logarr, PREG_GREP_INVERT);
573
                         } else {
574
                                 $logarr = preg_grep($regexp, $logarr);
575 dc7ddc0e Colin Smith
                         }
576
			 $i++;
577 d1cfae7b Scott Ullrich
                 }
578 15c37284 Colin Smith
        }
579 2c64eed7 Colin Smith
        foreach ($logarr as $logent) {
580
                $logent = preg_split("/\s+/", $logent, 6);
581
                echo "<tr valign=\"top\">\n";
582
                if ($withorig) {
583 89c8e6d6 Scott Ullrich
                		$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
584
                        echo "<td class=\"listlr\" nowrap>" . $entry_date_time  . "</td>\n";
585 2c64eed7 Colin Smith
                        echo "<td class=\"listr\">" . htmlspecialchars($logent[4] . " " . $logent[5]) . "</td>\n";
586
                } else {
587
                        echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
588
                }
589
                echo "</tr>\n";
590
        }
591
}
592
593 784c448c Scott Ullrich
function return_clog($logfile, $tail, $grepfor = "", $grepinvert = "", $grepreverse = false) {
594
	global $g, $config;
595
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
596
	$sor = isset($grepreverse) ? "-r" : "";
597
	$logarr = "";
598
	exec("/usr/sbin/clog {$logfile} | grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
599
600
	if(is_array($grepfor)) {
601
		$i = 0;
602
		foreach($grepfor as $agrep) {
603
			$regexp = "/" . $agrep . "/i";
604 d1cfae7b Scott Ullrich
			if($grepinvert[$i]) {
605
				$logarr = preg_grep($regexp, $logarr, PREG_GREP_INVERT);
606 784c448c Scott Ullrich
			} else {
607 d1cfae7b Scott Ullrich
				$logarr = preg_grep($regexp, $logarr);
608 784c448c Scott Ullrich
			}
609
			$i++;
610
		}
611
	}
612
	$i = 0;
613
	foreach ($logarr as $logent) {
614
		$logarray[$i] = "$logent";
615
		$i++;
616
	}
617
	return($logarray);
618
}
619
620 4d762703 Bill Marquette
/* Check if variable has changed, update and log if it has
621 edf14245 Bill Marquette
 * returns true if var changed
622 4d762703 Bill Marquette
 * varname = variable name in plain text
623
 * orig = original value
624
 * new = new value
625
 */
626 9eab73da Bill Marquette
function update_if_changed($varname, & $orig, $new) {
627 8b3ccf12 Bill Marquette
	if (is_array($orig) && is_array($new)) {
628
		$a_diff = array_diff($orig, $new);
629
		foreach ($a_diff as $diff) {
630
			update_changedesc("removed {$varname}: \"{$diff}\"");
631
		}
632
		$a_diff = array_diff($new, $orig);
633
		foreach ($a_diff as $diff) {
634
			update_changedesc("added {$varname}: \"{$diff}\"");
635
		}
636 4d762703 Bill Marquette
		$orig = $new;
637 edf14245 Bill Marquette
		return true;
638 d1cfae7b Scott Ullrich
639
	} else {
640 8b3ccf12 Bill Marquette
		if ($orig != $new) {
641
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
642
			$orig = $new;
643
			return true;
644
		}
645 4d762703 Bill Marquette
	}
646 edf14245 Bill Marquette
	return false;
647 4d762703 Bill Marquette
}
648
649 9a4cb6b7 Scott Ullrich
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
650
651
        if (isset($adr['any']))
652
                $padr = "any";
653
        else if ($adr['network'])
654
                $padr = $adr['network'];
655
        else if ($adr['address']) {
656
                list($padr, $pmask) = explode("/", $adr['address']);
657
                if (!$pmask)
658
                        $pmask = 32;
659
        }
660
661
        if (isset($adr['not']))
662
                $pnot = 1;
663
        else
664
                $pnot = 0;
665
666
        if ($adr['port']) {
667
                list($pbeginport, $pendport) = explode("-", $adr['port']);
668
                if (!$pendport)
669
                        $pendport = $pbeginport;
670
        } else {
671
                if(alias_expand($pbeginport) <> "" || alias_expand($pendport) <> "") {
672
                        /* Item is a port alias */
673
                } else {
674
                        $pbeginport = "any";
675
                        $pendport = "any";
676
                }
677
        }
678
}
679
680
function pconfig_to_address(&$adr, $padr, $pmask, $pnot=false, $pbeginport=0, $pendport=0) {
681
682
        $adr = array();
683
684
        if ($padr == "any")
685
                $adr['any'] = true;
686
        else if (is_specialnet($padr))
687
                $adr['network'] = $padr;
688
        else {
689
                $adr['address'] = $padr;
690
                if ($pmask != 32)
691
                        $adr['address'] .= "/" . $pmask;
692
        }
693
694
        if ($pnot)
695
                $adr['not'] = true;
696
        else
697 63637de9 Bill Marquette
                unset($adr['not']);
698 9a4cb6b7 Scott Ullrich
699
        if (($pbeginport != 0) && ($pbeginport != "any")) {
700
                if ($pbeginport != $pendport)
701
                        $adr['port'] = $pbeginport . "-" . $pendport;
702
                else
703
                        $adr['port'] = $pbeginport;
704
        }
705
706
        if(alias_expand($pbeginport)) {
707
                $adr['port'] = $pbeginport;
708
        }
709
}
710
711
function is_specialnet($net) {
712
        global $specialsrcdst;
713
714
        if (in_array($net, $specialsrcdst) || strstr($net, "opt"))
715
                return true;
716
        else
717
                return false;
718
}
719
720 c6a74fa1 Scott Ullrich
function ipsec_ca_sort() {
721
        global $g, $config;
722
723
        function ipseccacmp($a, $b) {
724
                return strcmp($a['ident'], $b['ident']);
725
        }
726
727
        usort($config['ipsec']['cacert'], "ipseccacmp");
728
}
729
730
731 31977165 Colin Smith
?>