Project

General

Profile

Download (22.8 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 fab7ff44 Bill Marquette
require_once("authgui.inc");
45 97bea981 Bill Marquette
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 6df9d7e3 Scott Ullrich
$d_sysctldirty_path = $g['varrun_path'] . "/sysctl.conf.dirty";
80 f8c08f21 Bill Marquette
$d_vsconfdirty_path = $g['varrun_path'] . "/vs.conf.dirty";
81 a32dea16 Scott Ullrich
$d_shaperconfdirty_path = $g['varrun_path'] . "/shaper.conf.dirty";
82
83 fb7860e0 Scott Ullrich
/* OpenVPN Directories */
84
$d_ovpnsrvdirty_path = "/tmp/ovpn-srv.dirty";
85
$d_ovpncrldirty_path = "/tmp/ovpn-crl.dirty";
86
$d_ovpnclidirty_path = "/tmp/ovpn-cli.dirty";
87 b005c738 Scott Ullrich
/* used by progress bar */
88
$lastseen = "-1";
89 5b237745 Scott Ullrich
90 9d9adc7f Scott Ullrich
$navlevelsep = ": ";	/* navigation level separator string */
91
$mandfldhtml = "";		/* display this before mandatory input fields */
92
$mandfldhtmlspc = "";	/* same as above, but with spacing */
93
94 5b237745 Scott Ullrich
if (file_exists($d_firmwarelock_path)) {
95
	if (!$d_isfwfile) {
96
		header("Location: system_firmware.php");
97
		exit;
98
	} else {
99
		return;
100
	}
101
}
102
103
/* some well knows ports */
104 fb2abb3a Scott Ullrich
$wkports = array(3389 => "MS RDP",
105
	21 => "FTP",
106
	22 => "SSH",
107
	23 => "Telnet",
108
	25 => "SMTP",
109
	53 => "DNS",
110 1d136eeb Scott Ullrich
	69 => "TFTP",
111 fb2abb3a Scott Ullrich
	80 => "HTTP",
112
	110 => "POP3",
113
	113 => "IDENT/AUTH",
114 1d136eeb Scott Ullrich
	119 => "NNTP",
115 fb2abb3a Scott Ullrich
	123 => "NTP",
116 1d136eeb Scott Ullrich
	137 => "NetBIOS-NS",
117 c736881c Scott Ullrich
	138 => "NetBIOS-DGM",
118
	139 => "NetBIOS-SSN",
119 fb2abb3a Scott Ullrich
	143 => "IMAP",
120 1d136eeb Scott Ullrich
	161 => "SNMP",
121
	162 => "SNMP-Trap",
122
	389 => "LDAP",
123 fb2abb3a Scott Ullrich
	443 => "HTTPS",
124 c736881c Scott Ullrich
	445 => "MS DS",
125 1d136eeb Scott Ullrich
	465 => "SMTP/S",
126
	500 => "isakmp",
127
	993 => "IMAP/S",
128
	995 => "POP3/S",
129
	1194 => "OpenVPN",
130
	1512 => "MS WINS",
131
	1701 => "L2TP",
132
	1723 => "PPTP",
133 0f66f9a2 Martin Fuchs
	1755 => "MMS/TCP",
134 1d136eeb Scott Ullrich
	1812 => "Radius",
135
	1813 => "Radius-accounting",
136
	3000 => "HBCI",
137 90b2b093 Martin Fuchs
	3478 => "STUN",
138 348327b7 Chris Buechler
	4500 => "IPsec NAT-T",
139 90b2b093 Martin Fuchs
	5004 => "RTP",
140
	5060 => "SIP",
141 1d136eeb Scott Ullrich
	5190 => "ICQ",
142
	5900 => "VNC",
143 0f66f9a2 Martin Fuchs
	5999 => "CVSup",
144
	7000 => "MMS/UDP");
145 5b237745 Scott Ullrich
146 e825920f Scott Ullrich
$specialnets = array("wanip" => "WAN address", "lanip" => "LAN address", "lan" => "LAN net", "pptp" => "PPTP clients");
147 5b237745 Scott Ullrich
148
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
149
	$specialnets['opt' . $i] = $config['interfaces']['opt' . $i]['descr'] . " net";
150
}
151 be81829f Scott Ullrich
152 5b237745 Scott Ullrich
$medias = array("auto" => "autoselect", "100full" => "100BASE-TX full-duplex",
153
	"100half" => "100BASE-TX half-duplex", "10full" => "10BASE-T full-duplex",
154
	"10half" => "10BASE-T half-duplex");
155 be81829f Scott Ullrich
156 5b237745 Scott Ullrich
/* platforms that support firmware updating */
157 275cbd3f Scott Ullrich
$fwupplatforms = array('pfSense', 'net45xx', 'net48xx', 'generic-pc', 'embedded', 'wrap');
158 5b237745 Scott Ullrich
159
/* IPsec defines */
160
$my_identifier_list = array('myaddress' => 'My IP address',
161 ee7f808e Scott Ullrich
				'address' => 'IP address',
162
				'fqdn' => 'Domain name',
163
				'user_fqdn' => 'User FQDN',
164 66cc67c6 Scott Ullrich
				'asn1dn' => 'Distinguished Name',
165 ee7f808e Scott Ullrich
				'dyn_dns' => 'Dynamic DNS');
166 5b237745 Scott Ullrich
167 be81829f Scott Ullrich
$p1_ealgos = array('des' => 'DES', '3des' => '3DES', 'blowfish' => 'Blowfish',
168 9aa4a9b1 Scott Ullrich
		'cast128' => 'CAST128','rijndael' => 'Rijndael (AES)', 'rijndael 256' => 'Rijndael 256');
169 5b237745 Scott Ullrich
$p2_ealgos = array('des' => 'DES', '3des' => '3DES', 'blowfish' => 'Blowfish',
170 9aa4a9b1 Scott Ullrich
		'cast128' => 'CAST128', 'rijndael' => 'Rijndael (AES)', 'rijndael 256' => 'Rijndael 256');
171
172 5b237745 Scott Ullrich
$p1_halgos = array('sha1' => 'SHA1', 'md5' => 'MD5');
173 075b2138 Scott Ullrich
$p1_authentication_methods = array('pre_shared_key' => 'Pre-shared key', 'rsasig' => 'RSA signature');
174 5b237745 Scott Ullrich
$p2_halgos = array('hmac_sha1' => 'SHA1', 'hmac_md5' => 'MD5');
175
$p2_protos = array('esp' => 'ESP', 'ah' => 'AH');
176
$p2_pfskeygroups = array('0' => 'off', '1' => '1', '2' => '2', '5' => '5');
177
178
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
179 be81829f Scott Ullrich
180
	/* check for bad control characters */
181 5b237745 Scott Ullrich
	foreach ($postdata as $pn => $pd) {
182
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
183
			$input_errors[] = "The field '" . $pn . "' contains invalid characters.";
184
		}
185
	}
186 be81829f Scott Ullrich
187 5b237745 Scott Ullrich
	for ($i = 0; $i < count($reqdfields); $i++) {
188 d1cfae7b Scott Ullrich
		if ($_POST[$reqdfields[$i]] == "") {
189 5b237745 Scott Ullrich
			$input_errors[] = "The field '" . $reqdfieldsn[$i] . "' is required.";
190
		}
191 be81829f Scott Ullrich
	}
192 5b237745 Scott Ullrich
}
193
194
function print_input_errors($input_errors) {
195 30af342f Erik Kristensen
	global $g;
196 5b237745 Scott Ullrich
	echo "<p><table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">\n";
197 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";
198 5b237745 Scott Ullrich
	echo "<td bgcolor=\"#FFD9D1\" style=\"padding-left: 8px; padding-top: 6px\">";
199 be81829f Scott Ullrich
200 5b237745 Scott Ullrich
	echo "<span class=\"errmsg\"><p>The following input errors were detected:<ul>\n";
201
	foreach ($input_errors as $ierr) {
202
		echo "<li>" . htmlspecialchars($ierr) . "</li>\n";
203
	}
204
	echo "</ul></span>";
205 be81829f Scott Ullrich
206 76dddec6 Scott Ullrich
	echo "</td></tr></table></p>&nbsp;<br>";
207 5b237745 Scott Ullrich
}
208
209
function exec_rc_script($scriptname) {
210 be81829f Scott Ullrich
211 5b237745 Scott Ullrich
	global $d_sysrebootreqd_path;
212 be81829f Scott Ullrich
213 767a716e Scott Ullrich
	$execoutput = "";
214
	$retval = "";
215 5b237745 Scott Ullrich
	exec($scriptname . " >/dev/null 2>&1", $execoutput, $retval);
216 be81829f Scott Ullrich
	return $retval;
217 5b237745 Scott Ullrich
}
218
219
function exec_rc_script_async($scriptname) {
220 be81829f Scott Ullrich
221 5b237745 Scott Ullrich
	global $d_sysrebootreqd_path;
222 767a716e Scott Ullrich
	$execoutput = "";
223
	$retval = "";
224 be81829f Scott Ullrich
225 5b237745 Scott Ullrich
	exec("nohup " . $scriptname . " >/dev/null 2>&1 &", $execoutput, $retval);
226 be81829f Scott Ullrich
	return $retval;
227 5b237745 Scott Ullrich
}
228
229
function verify_gzip_file($fname) {
230
231 1ba03d69 Scott Ullrich
    $returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
232 5b237745 Scott Ullrich
	if ($returnvar != 0)
233
		return 0;
234
	else
235
		return 1;
236
}
237
238 a2dab9bc Bill Marquette
function print_info_box_np($msg, $name="apply",$value="Apply changes") {
239 30af342f Erik Kristensen
	global $g;
240 d07299bc Scott Ullrich
	echo "<table height=\"32\" width=\"100%\">\n";
241 c438d2f8 Scott Ullrich
	echo "  <tr>\n";
242
	echo "   <td>\n";
243
	echo "      <div style='background-color:#990000' id='redbox'>\n";
244
	echo "       <table width='100%'><tr><td width='8%'>\n";
245 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";
246 a0509c58 Scott Ullrich
	echo "        </td>\n";
247
	echo "        <td width='70%'><font color='white'><b>{$msg}</b></font>\n";
248
	echo "        </td>";
249 a2dab9bc Bill Marquette
	if(stristr($msg, "apply") == true || stristr($msg, "save")) {
250 541207da Scott Ullrich
		echo "         <td>";
251 a2dab9bc Bill Marquette
		echo "           <input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\">\n";
252 541207da Scott Ullrich
		echo "         </td>";
253
	}
254 a0509c58 Scott Ullrich
	echo "        </tr></table>\n";
255
	echo "       </div>\n";
256 c438d2f8 Scott Ullrich
	echo "    </td>\n";
257
	echo "</table>\n";
258
	echo "<script type=\"text/javascript\">\n";
259 d07299bc Scott Ullrich
	echo "NiftyCheck();\n";
260
	echo "Rounded(\"div#redbox\",\"all\",\"#FFF\",\"#990000\",\"smooth\");\n";
261
	echo "Rounded(\"td#blackbox\",\"all\",\"#FFF\",\"#000000\",\"smooth\");\n";
262 c438d2f8 Scott Ullrich
	echo "</script>\n";
263 cd4087bf Scott Ullrich
	echo "\n<br>\n";
264 5b237745 Scott Ullrich
}
265
266
function print_info_box($msg) {
267
	echo "<p>";
268
	print_info_box_np($msg);
269
	echo "</p>";
270
}
271
272
function format_bytes($bytes) {
273
	if ($bytes >= 1073741824) {
274
		return sprintf("%.2f GB", $bytes/1073741824);
275
	} else if ($bytes >= 1048576) {
276
		return sprintf("%.2f MB", $bytes/1048576);
277
	} else if ($bytes >= 1024) {
278
		return sprintf("%.0f KB", $bytes/1024);
279
	} else {
280
		return sprintf("%d bytes", $bytes);
281
	}
282
}
283
284
function get_std_save_message($ok) {
285
	global $d_sysrebootreqd_path;
286
287 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.";
288 5b237745 Scott Ullrich
}
289
290
function pprint_address($adr) {
291
	global $specialnets;
292
293
	if (isset($adr['any'])) {
294
		$padr = "*";
295
	} else if ($adr['network']) {
296 369578b1 Scott Ullrich
		if (preg_match("/opt[0-999]ip/", $adr['network'])) {
297
			$padr = "Interface IP address";
298
		} else {
299
			$padr = $specialnets[$adr['network']];
300
		}
301 5b237745 Scott Ullrich
	} else {
302
		$padr = $adr['address'];
303
	}
304 be81829f Scott Ullrich
305 5b237745 Scott Ullrich
	if (isset($adr['not']))
306
		$padr = "! " . $padr;
307 be81829f Scott Ullrich
308 5b237745 Scott Ullrich
	return $padr;
309
}
310
311
function pprint_port($port) {
312
	global $wkports;
313
314
	$pport = "";
315 be81829f Scott Ullrich
316 5b237745 Scott Ullrich
	if (!$port)
317 3a54b6ca Scott Dale
		return "*";
318 5b237745 Scott Ullrich
	else {
319
		$srcport = explode("-", $port);
320
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
321
			$pport = $srcport[0];
322
			if ($wkports[$srcport[0]]) {
323
				$pport .= " (" . $wkports[$srcport[0]] . ")";
324
			}
325
		} else
326
			$pport .= $srcport[0] . " - " . $srcport[1];
327
	}
328 be81829f Scott Ullrich
329 5b237745 Scott Ullrich
	return $pport;
330
}
331
332 7afb67e0 Scott Ullrich
function captiveportal_users_sort() {
333
	global $g, $config;
334 d1cfae7b Scott Ullrich
335 7afb67e0 Scott Ullrich
	function cpusercmp($a, $b) {
336
		return strcasecmp($a['name'], $b['name']);
337
	}
338 d1cfae7b Scott Ullrich
339 7afb67e0 Scott Ullrich
	usort($config['captiveportal']['user'], "cpusercmp");
340
}
341
342 fab7ff44 Bill Marquette
function admin_groups_sort() {
343
	global $g, $config;
344
345
	function cpusercmp($a, $b) {
346
		return strcasecmp($a['name'], $b['name']);
347
	}
348
349
	usort($config['system']['group'], "cpusercmp");
350
}
351
352
function admin_users_sort() {
353
	global $g, $config;
354
355
	function cpusercmp($a, $b) {
356
		return strcasecmp($a['name'], $b['name']);
357
	}
358
359
	usort($config['system']['user'], "cpusercmp");
360
}
361
362 5b237745 Scott Ullrich
/* sort by interface only, retain the original order of rules that apply to
363
   the same interface */
364
function filter_rules_sort() {
365 63637de9 Bill Marquette
	global $config;
366 be81829f Scott Ullrich
367 5b237745 Scott Ullrich
	/* mark each rule with the sequence number (to retain the order while sorting) */
368
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
369
		$config['filter']['rule'][$i]['seq'] = $i;
370 be81829f Scott Ullrich
371 5b237745 Scott Ullrich
	function filtercmp($a, $b) {
372
		if ($a['interface'] == $b['interface'])
373
			return $a['seq'] - $b['seq'];
374
		else
375
			return -strcmp($a['interface'], $b['interface']);
376
	}
377 be81829f Scott Ullrich
378 5b237745 Scott Ullrich
	usort($config['filter']['rule'], "filtercmp");
379 be81829f Scott Ullrich
380 5b237745 Scott Ullrich
	/* strip the sequence numbers again */
381
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
382
		unset($config['filter']['rule'][$i]['seq']);
383
}
384
385
function nat_rules_sort() {
386 63637de9 Bill Marquette
	global $config;
387 be81829f Scott Ullrich
388 5b237745 Scott Ullrich
	function natcmp($a, $b) {
389
		if ($a['external-address'] == $b['external-address']) {
390
			if ($a['protocol'] == $b['protocol']) {
391
				if ($a['external-port'] == $b['external-port']) {
392
					return 0;
393
				} else {
394
					return ($a['external-port'] - $b['external-port']);
395
				}
396
			} else {
397
				return strcmp($a['protocol'], $b['protocol']);
398
			}
399
		} else if (!$a['external-address'])
400
			return 1;
401
		else if (!$b['external-address'])
402
			return -1;
403
		else
404
			return ipcmp($a['external-address'], $b['external-address']);
405
	}
406 be81829f Scott Ullrich
407 5b237745 Scott Ullrich
	usort($config['nat']['rule'], "natcmp");
408
}
409
410
function nat_1to1_rules_sort() {
411
	global $g, $config;
412 be81829f Scott Ullrich
413 5b237745 Scott Ullrich
	function nat1to1cmp($a, $b) {
414
		return ipcmp($a['external'], $b['external']);
415
	}
416 be81829f Scott Ullrich
417 5b237745 Scott Ullrich
	usort($config['nat']['onetoone'], "nat1to1cmp");
418
}
419
420
function nat_server_rules_sort() {
421
	global $g, $config;
422 be81829f Scott Ullrich
423 5b237745 Scott Ullrich
	function natservercmp($a, $b) {
424
		return ipcmp($a['ipaddr'], $b['ipaddr']);
425
	}
426 be81829f Scott Ullrich
427 5b237745 Scott Ullrich
	usort($config['nat']['servernat'], "natservercmp");
428
}
429
430
function nat_out_rules_sort() {
431
	global $g, $config;
432 be81829f Scott Ullrich
433 5b237745 Scott Ullrich
	function natoutcmp($a, $b) {
434
		return strcmp($a['source']['network'], $b['source']['network']);
435
	}
436 be81829f Scott Ullrich
437 5b237745 Scott Ullrich
	usort($config['nat']['advancedoutbound']['rule'], "natoutcmp");
438
}
439
440
function pptpd_users_sort() {
441
	global $g, $config;
442 be81829f Scott Ullrich
443 5b237745 Scott Ullrich
	function usercmp($a, $b) {
444
		return strcasecmp($a['name'], $b['name']);
445
	}
446 be81829f Scott Ullrich
447 5b237745 Scott Ullrich
	usort($config['pptpd']['user'], "usercmp");
448
}
449
450 4f1b515b Scott Ullrich
function pppoe_users_sort() {
451
	global $g, $config;
452
453
	function usercmp($a, $b) {
454
		return strcasecmp($a['name'], $b['name']);
455
	}
456
457
	usort($config['pppoe']['user'], "usercmp");
458
}
459
460 5b237745 Scott Ullrich
function staticroutes_sort() {
461
	global $g, $config;
462
463
	function staticroutecmp($a, $b) {
464
		return strcmp($a['network'], $b['network']);
465
	}
466
467
	usort($config['staticroutes']['route'], "staticroutecmp");
468
}
469
470
function hosts_sort() {
471
	global $g, $config;
472
473
	function hostcmp($a, $b) {
474
		return strcasecmp($a['host'], $b['host']);
475
	}
476
477
	usort($config['dnsmasq']['hosts'], "hostcmp");
478
}
479
480
function staticmaps_sort($if) {
481
	global $g, $config;
482
483
	function staticmapcmp($a, $b) {
484
		return ipcmp($a['ipaddr'], $b['ipaddr']);
485
	}
486
487
	usort($config['dhcpd'][$if]['staticmap'], "staticmapcmp");
488
}
489
490
function aliases_sort() {
491
	global $g, $config;
492
493
	function aliascmp($a, $b) {
494
		return strcmp($a['name'], $b['name']);
495
	}
496
497
	usort($config['aliases']['alias'], "aliascmp");
498
}
499
500 615b27bc Scott Dale
function schedule_sort(){
501
	global $g, $config;
502
503
	function schedulecmp($a, $b) {
504
		return strcmp($a['name'], $b['name']);
505
	}
506
507
	usort($config['schedules']['schedule'], "schedulecmp");
508
509
}
510
511 5b237745 Scott Ullrich
function ipsec_mobilekey_sort() {
512
	global $g, $config;
513
514
	function mobilekeycmp($a, $b) {
515
		return strcmp($a['ident'][0], $b['ident'][0]);
516
	}
517
518
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
519
}
520
521
function proxyarp_sort() {
522
	global $g, $config;
523
524
	function proxyarpcmp($a, $b) {
525
		if (isset($a['network']))
526
			list($ast,$asn) = explode("/", $a['network']);
527
		else if (isset($a['range'])) {
528
			$ast = $a['range']['from'];
529
			$asn = 32;
530
		}
531
		if (isset($b['network']))
532
			list($bst,$bsn) = explode("/", $b['network']);
533
		else if (isset($b['range'])) {
534
			$bst = $b['range']['from'];
535
			$bsn = 32;
536
		}
537
		if (ipcmp($ast, $bst) == 0)
538
			return ($asn - $bsn);
539
		else
540
			return ipcmp($ast, $bst);
541
	}
542
543
	usort($config['proxyarp']['proxyarpnet'], "proxyarpcmp");
544
}
545
546
function passthrumacs_sort() {
547
	global $g, $config;
548
549
	function passthrumacscmp($a, $b) {
550
		return strcmp($a['mac'], $b['mac']);
551
	}
552 be81829f Scott Ullrich
553 5b237745 Scott Ullrich
	usort($config['captiveportal']['passthrumac'],"passthrumacscmp");
554
}
555
556 7afb67e0 Scott Ullrich
function cpelements_sort() {
557
	global $g, $config;
558
559
	function cpelementscmp($a, $b) {
560
		return strcasecmp($a['name'], $b['name']);
561
	}
562 d1cfae7b Scott Ullrich
563 7afb67e0 Scott Ullrich
	usort($config['captiveportal']['element'],"cpelementscmp");
564
}
565
566 5b237745 Scott Ullrich
function allowedips_sort() {
567
	global $g, $config;
568
569
	function allowedipscmp($a, $b) {
570
		return strcmp($a['ip'], $b['ip']);
571
	}
572 be81829f Scott Ullrich
573 5b237745 Scott Ullrich
	usort($config['captiveportal']['allowedip'],"allowedipscmp");
574
}
575
576
function wol_sort() {
577
	global $g, $config;
578
579
	function wolcmp($a, $b) {
580
		return strcmp($a['descr'], $b['descr']);
581
	}
582 be81829f Scott Ullrich
583 5b237745 Scott Ullrich
	usort($config['wol']['wolentry'], "wolcmp");
584
}
585
586 6577d782 Scott Ullrich
function sort_rule_by_queue_priority() {
587
	global $g, $config;
588
	global $queues;
589
590
	function rqpcmp($a, $b) {
591
		global $queues;
592
593
		$ra = $queues[$a['outqueue']];
594
		$rb = $queues[$b['outqueue']];
595
596
		if ($ra == $rb)
597
			return 0;
598 d1cfae7b Scott Ullrich
		/* reverse sort - we want higher priority first */
599 6577d782 Scott Ullrich
		return ($ra < $rb) ? 1 : -1;
600
	}
601
602
	foreach($config['shaper']['queue'] as $q) {
603
		$queues[$q['name']] = $q['priority'];
604
	}
605
606
	usort($config['shaper']['rule'], "rqpcmp");
607
}
608 9d9adc7f Scott Ullrich
609
function gentitle($title) {
610
	global $navlevelsep;
611 c3deaf7f Scott Ullrich
	if(!is_array($title))
612
		return $title;
613
	else
614
		return join($navlevelsep, $title); 
615 9d9adc7f Scott Ullrich
}
616
617
function genhtmltitle($title) {
618
        global $config;
619 479628e6 Scott Ullrich
        return gentitle($title);
620 5b237745 Scott Ullrich
}
621
622 d345bd39 Bill Marquette
/* update the changedesc and changecount(er) variables */
623
function update_changedesc($update) {
624
	global $changedesc;
625
	global $changecount;
626
627
	$changedesc .= " {$update}";
628
	$changecount++;
629
}
630
631 15c37284 Colin Smith
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
632 23f1e22a Colin Smith
	global $g, $config;
633 767a716e Scott Ullrich
    $sor = isset($config['syslog']['reverse']) ? "-r" : "";
634
    $logarr = "";
635 0fcfdd3d Scott Ullrich
	$grepline = "  ";
636 fa626e70 Scott Ullrich
	if(is_array($grepfor))
637
		foreach($grepfor as $agrep)
638
			$grepline .= " | grep \"$agrep\"";
639
	if(is_array($grepinvert))
640
		foreach($grepinvert as $agrep)
641
			$grepline .= " | grep -v \"$agrep\"";
642 0fcfdd3d Scott Ullrich
	exec("/usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
643 5e3de917 Chris Buechler
    /* comment out debug code
644 0fcfdd3d Scott Ullrich
    echo "<!-- /usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail} -->";
645 5e3de917 Chris Buechler
    */
646 0fcfdd3d Scott Ullrich
    foreach ($logarr as $logent) {
647
            $logent = preg_split("/\s+/", $logent, 6);
648
            echo "<tr valign=\"top\">\n";
649
            if ($withorig) {
650
            		$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
651
                    echo "<td class=\"listlr\" nowrap>" . $entry_date_time  . "</td>\n";
652
                    echo "<td class=\"listr\">" . htmlspecialchars($logent[4] . " " . $logent[5]) . "</td>\n";
653
            } else {
654
                    echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
655
            }
656
            echo "</tr>\n";
657
    }
658 2c64eed7 Colin Smith
}
659
660 784c448c Scott Ullrich
function return_clog($logfile, $tail, $grepfor = "", $grepinvert = "", $grepreverse = false) {
661
	global $g, $config;
662
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
663
	$sor = isset($grepreverse) ? "-r" : "";
664
	$logarr = "";
665 a0e4bd9f Scott Ullrich
	$grepline = "  ";
666 fa626e70 Scott Ullrich
	if(is_array($grepfor))
667
		foreach($grepfor as $agrep)
668
			$grepline .= " | grep \"$agrep\"";
669
	if(is_array($grepinvert))
670
		foreach($grepinvert as $agrep)
671
			$grepline .= " | grep -v \"$agrep\"";
672 5e3de917 Chris Buechler
	/* comment out debug code
673 fa626e70 Scott Ullrich
	echo "<!-- /usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail} -->";
674 5e3de917 Chris Buechler
	*/
675 a0e4bd9f Scott Ullrich
	exec("/usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
676 fa626e70 Scott Ullrich
	return($logarr);
677 784c448c Scott Ullrich
}
678
679 4d762703 Bill Marquette
/* Check if variable has changed, update and log if it has
680 edf14245 Bill Marquette
 * returns true if var changed
681 4d762703 Bill Marquette
 * varname = variable name in plain text
682
 * orig = original value
683
 * new = new value
684
 */
685 9eab73da Bill Marquette
function update_if_changed($varname, & $orig, $new) {
686 8b3ccf12 Bill Marquette
	if (is_array($orig) && is_array($new)) {
687
		$a_diff = array_diff($orig, $new);
688
		foreach ($a_diff as $diff) {
689
			update_changedesc("removed {$varname}: \"{$diff}\"");
690
		}
691
		$a_diff = array_diff($new, $orig);
692
		foreach ($a_diff as $diff) {
693
			update_changedesc("added {$varname}: \"{$diff}\"");
694
		}
695 4d762703 Bill Marquette
		$orig = $new;
696 edf14245 Bill Marquette
		return true;
697 d1cfae7b Scott Ullrich
698
	} else {
699 8b3ccf12 Bill Marquette
		if ($orig != $new) {
700
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
701
			$orig = $new;
702
			return true;
703
		}
704 4d762703 Bill Marquette
	}
705 edf14245 Bill Marquette
	return false;
706 4d762703 Bill Marquette
}
707
708 9a4cb6b7 Scott Ullrich
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
709
710
        if (isset($adr['any']))
711
                $padr = "any";
712
        else if ($adr['network'])
713
                $padr = $adr['network'];
714
        else if ($adr['address']) {
715
                list($padr, $pmask) = explode("/", $adr['address']);
716
                if (!$pmask)
717
                        $pmask = 32;
718
        }
719
720
        if (isset($adr['not']))
721
                $pnot = 1;
722
        else
723
                $pnot = 0;
724
725
        if ($adr['port']) {
726
                list($pbeginport, $pendport) = explode("-", $adr['port']);
727
                if (!$pendport)
728
                        $pendport = $pbeginport;
729
        } else {
730
                if(alias_expand($pbeginport) <> "" || alias_expand($pendport) <> "") {
731
                        /* Item is a port alias */
732
                } else {
733
                        $pbeginport = "any";
734
                        $pendport = "any";
735
                }
736
        }
737
}
738
739
function pconfig_to_address(&$adr, $padr, $pmask, $pnot=false, $pbeginport=0, $pendport=0) {
740
741
        $adr = array();
742
743
        if ($padr == "any")
744
                $adr['any'] = true;
745
        else if (is_specialnet($padr))
746
                $adr['network'] = $padr;
747
        else {
748
                $adr['address'] = $padr;
749
                if ($pmask != 32)
750
                        $adr['address'] .= "/" . $pmask;
751
        }
752
753
        if ($pnot)
754
                $adr['not'] = true;
755
        else
756 63637de9 Bill Marquette
                unset($adr['not']);
757 9a4cb6b7 Scott Ullrich
758
        if (($pbeginport != 0) && ($pbeginport != "any")) {
759
                if ($pbeginport != $pendport)
760
                        $adr['port'] = $pbeginport . "-" . $pendport;
761
                else
762
                        $adr['port'] = $pbeginport;
763
        }
764
765
        if(alias_expand($pbeginport)) {
766
                $adr['port'] = $pbeginport;
767
        }
768
}
769
770
function is_specialnet($net) {
771
        global $specialsrcdst;
772 8c984f57 Scott Ullrich
		if(!$net) 
773
			return false;
774 9a4cb6b7 Scott Ullrich
        if (in_array($net, $specialsrcdst) || strstr($net, "opt"))
775
                return true;
776
        else
777
                return false;
778
}
779
780 c6a74fa1 Scott Ullrich
function ipsec_ca_sort() {
781
        global $g, $config;
782
783
        function ipseccacmp($a, $b) {
784
                return strcmp($a['ident'], $b['ident']);
785
        }
786
787
        usort($config['ipsec']['cacert'], "ipseccacmp");
788
}
789
790 b93f9af4 Scott Dale
//function to create widget tabs when called
791
function display_widget_tabs(& $tab_array) {	
792
	echo "<div id='tabs'>";
793
	$tabscounter = 0;
794
	foreach ($tab_array as $ta) {
795
	$dashpos = strpos($ta[2],'-');
796
	$tabname = $ta[2] . "-tab";
797
	$tabclass = substr($ta[2],0,$dashpos);
798
	$tabclass = $tabclass . "-class";
799
		if ($ta[1] == true) {
800
			$tabActive = "table-cell";
801
			$tabNonActive = "none";
802
		} 
803
		else {
804
			$tabActive = "none";
805
			$tabNonActive = "table-cell";
806
		}
807
		echo "<div id='{$ta[2]}-active' class='{$tabclass}-tabactive' style='display:{$tabActive}; background-color:#EEEEEE; color:black;'>";
808
		echo "<B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
809
		echo "&nbsp;&nbsp;&nbsp;</B>";
810
		echo "</div>";
811
		
812
		echo "<div id='{$ta[2]}-deactive' class='{$tabclass}-tabdeactive' style='display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;' onClick=\"return changeTabDIV('{$ta[2]}')\">";
813
		echo "<B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
814
		echo "&nbsp;&nbsp;&nbsp;</B>";
815
		echo "</div>";
816
	}
817
	
818
	echo "<script type=\"text/javascript\">";
819
	echo "NiftyCheck();\n";
820
	echo "Rounded(\"div.{$tabclass}-tabactive\",\"top\",\"#CCCCCC\",\"#EEEEEE\",\"smooth\");\n";
821
	echo "Rounded(\"div.{$tabclass}-tabdeactive\",\"top\",\"#CCCCCC\",\"#777777\",\"smooth\");\n";
822
	echo "</script>";
823
	echo "</div>";
824
}
825
826 c6a74fa1 Scott Ullrich
827 d7028c74 Chris Buechler
?>