Project

General

Profile

Download (26.1 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 50d86c13 Bill Marquette
/* Pull in all the gui related display classes) */
51
foreach (scandir("/usr/local/www/classes/") as $file) {
52
	if (stristr($file, ".inc") !== false) {
53
		require_once("classes/{$file}");
54
	}
55
}
56 31977165 Colin Smith
/*
57
 *   if user has selected a custom template, use it.
58
 *   otherwise default to pfsense tempalte
59
 */
60
if($config['theme'] <> "")
61
        $g['theme'] = $config['theme'];
62
else
63
        $g['theme'] = "pfsense";
64
65 3e44d22a Scott Ullrich
$d_landirty_path = $g['varrun_path'] . "/lan.conf.dirty";
66 8ede0ae8 Scott Ullrich
$d_pppoeuserdirty_path = $g['varrun_path'] . "/vpn-pppoe-users-edit.dirty";
67 ceca2719 Scott Ullrich
$d_hostsdirty_path = $g['varrun_path'] . "/hosts.dirty";
68 5b237745 Scott Ullrich
$d_natconfdirty_path = $g['varrun_path'] . "/nat.conf.dirty";
69
$d_filterconfdirty_path = $g['varrun_path'] . "/filter.conf.dirty";
70
$d_ipsecconfdirty_path = $g['varrun_path'] . "/ipsec.conf.dirty";
71
$d_shaperconfdirty_path = $g['varrun_path'] . "/shaper.conf.dirty";
72
$d_pptpuserdirty_path = $g['varrun_path'] . "/pptpd.user.dirty";
73 0c2b5df7 Scott Ullrich
$d_dnsmasqdirty_path = $g['varrun_path'] . "/dnsmasq.dirty";
74 5b237745 Scott Ullrich
$d_staticmapsdirty_path = $g['varrun_path'] . "/staticmaps.dirty";
75
$d_staticroutesdirty_path = $g['varrun_path'] . "/staticroutes.dirty";
76
$d_aliasesdirty_path = $g['varrun_path'] . "/aliases.dirty";
77
$d_proxyarpdirty_path = $g['varrun_path'] . "/proxyarp.dirty";
78
$d_fwupenabled_path = $g['varrun_path'] . "/fwup.enabled";
79
$d_firmwarelock_path = $g['varrun_path'] . "/firmware.lock";
80
$d_sysrebootreqd_path = $g['varrun_path'] . "/sysreboot.reqd";
81
$d_passthrumacsdirty_path = $g['varrun_path'] . "/passthrumacs.dirty";
82
$d_allowedipsdirty_path = $g['varrun_path'] . "/allowedips.dirty";
83
$d_ovpnclidirty_path = $g['varrun_path'] . "/ovpnclient.dirty";
84 1425e067 Bill Marquette
$d_vipconfdirty_path = $g['varrun_path'] . "/vip.conf.dirty";
85 6df9d7e3 Scott Ullrich
$d_sysctldirty_path = $g['varrun_path'] . "/sysctl.conf.dirty";
86 f8c08f21 Bill Marquette
$d_vsconfdirty_path = $g['varrun_path'] . "/vs.conf.dirty";
87 a32dea16 Scott Ullrich
$d_shaperconfdirty_path = $g['varrun_path'] . "/shaper.conf.dirty";
88
89 fb7860e0 Scott Ullrich
/* OpenVPN Directories */
90
$d_ovpnsrvdirty_path = "/tmp/ovpn-srv.dirty";
91
$d_ovpncrldirty_path = "/tmp/ovpn-crl.dirty";
92
$d_ovpnclidirty_path = "/tmp/ovpn-cli.dirty";
93 b005c738 Scott Ullrich
/* used by progress bar */
94
$lastseen = "-1";
95 5b237745 Scott Ullrich
96 9d9adc7f Scott Ullrich
$navlevelsep = ": ";	/* navigation level separator string */
97
$mandfldhtml = "";		/* display this before mandatory input fields */
98
$mandfldhtmlspc = "";	/* same as above, but with spacing */
99
100 5b237745 Scott Ullrich
if (file_exists($d_firmwarelock_path)) {
101
	if (!$d_isfwfile) {
102
		header("Location: system_firmware.php");
103
		exit;
104
	} else {
105
		return;
106
	}
107
}
108
109 fbf672cb Matthew Grooms
$auth_server_types = array(
110
	'ldap' => "LDAP",
111
	'radius' => "Radius");
112
113
$ldap_urltypes = array(
114
	'TCP - Standard' => 389,
115
	'SSL - Encrypted' => 636);
116
117
$ldap_scopes = array(
118
	'one' => "One Level",
119
	'subtree' => "Entire Subtree");
120
121
$ldap_protvers = array(
122
	2,
123
	3);
124
125
$ldap_templates = array(
126
127
	'open' => array(
128
				'desc' => "OpenLDAP",
129
				'attr_user' => "cn",
130
				'attr_group' => "cn",
131
				'attr_member' => "member"),
132
133
	'msad' => array(
134
				'desc' => "Microsoft AD",
135
				'attr_user' => "samAccountNAme",
136
				'attr_group' => "cn",
137
				'attr_member' => "member"),
138
139
	'edir' => array(
140
				'desc' => "Novell eDirectory",
141
				'attr_user' => "cn",
142
				'attr_group' => "cn",
143
				'attr_member' => "uniqueMember"));
144
145
$radius_srvcs = array(
146
	'both' => "Authentication and Accounting",
147
	'auth' => "Authentication",
148
	'acct' => "Accounting");
149
150 d799787e Matthew Grooms
$netbios_nodetypes = array(
151
	'0' => "none",
152
	'1' => "b-node",
153
	'2' => "p-node",
154
	'4' => "m-node",
155
	'5' => "h-node");
156
157 5b237745 Scott Ullrich
/* some well knows ports */
158 fbf672cb Matthew Grooms
$wkports = array(
159
	3389 => "MS RDP",
160 fb2abb3a Scott Ullrich
	21 => "FTP",
161
	22 => "SSH",
162
	23 => "Telnet",
163
	25 => "SMTP",
164
	53 => "DNS",
165 1d136eeb Scott Ullrich
	69 => "TFTP",
166 fb2abb3a Scott Ullrich
	80 => "HTTP",
167
	110 => "POP3",
168
	113 => "IDENT/AUTH",
169 1d136eeb Scott Ullrich
	119 => "NNTP",
170 fb2abb3a Scott Ullrich
	123 => "NTP",
171 1d136eeb Scott Ullrich
	137 => "NetBIOS-NS",
172 c736881c Scott Ullrich
	138 => "NetBIOS-DGM",
173
	139 => "NetBIOS-SSN",
174 fb2abb3a Scott Ullrich
	143 => "IMAP",
175 1d136eeb Scott Ullrich
	161 => "SNMP",
176
	162 => "SNMP-Trap",
177
	389 => "LDAP",
178 fb2abb3a Scott Ullrich
	443 => "HTTPS",
179 c736881c Scott Ullrich
	445 => "MS DS",
180 1d136eeb Scott Ullrich
	465 => "SMTP/S",
181
	500 => "isakmp",
182
	993 => "IMAP/S",
183
	995 => "POP3/S",
184
	1194 => "OpenVPN",
185
	1512 => "MS WINS",
186
	1701 => "L2TP",
187
	1723 => "PPTP",
188 0f66f9a2 Martin Fuchs
	1755 => "MMS/TCP",
189 1d136eeb Scott Ullrich
	1812 => "Radius",
190
	1813 => "Radius-accounting",
191 90ee3f4d Martin Fuchs
	1863 => "MSN",
192 1d136eeb Scott Ullrich
	3000 => "HBCI",
193 90b2b093 Martin Fuchs
	3478 => "STUN",
194 348327b7 Chris Buechler
	4500 => "IPsec NAT-T",
195 90b2b093 Martin Fuchs
	5004 => "RTP",
196
	5060 => "SIP",
197 1d136eeb Scott Ullrich
	5190 => "ICQ",
198
	5900 => "VNC",
199 0f66f9a2 Martin Fuchs
	5999 => "CVSup",
200
	7000 => "MMS/UDP");
201 5b237745 Scott Ullrich
202 e825920f Scott Ullrich
$specialnets = array("wanip" => "WAN address", "lanip" => "LAN address", "lan" => "LAN net", "pptp" => "PPTP clients");
203 5b237745 Scott Ullrich
204 b7391125 Ermal Luçi
$spiflist = get_configured_interface_with_descr(true, true);
205 fa550a17 Scott Ullrich
foreach ($spiflist as $ifgui => $ifdesc) {
206
	$specialnets[$ifgui] = $ifdesc . " net";
207 5b237745 Scott Ullrich
}
208 be81829f Scott Ullrich
209 5b237745 Scott Ullrich
$medias = array("auto" => "autoselect", "100full" => "100BASE-TX full-duplex",
210
	"100half" => "100BASE-TX half-duplex", "10full" => "10BASE-T full-duplex",
211
	"10half" => "10BASE-T half-duplex");
212 be81829f Scott Ullrich
213 5b237745 Scott Ullrich
/* platforms that support firmware updating */
214 275cbd3f Scott Ullrich
$fwupplatforms = array('pfSense', 'net45xx', 'net48xx', 'generic-pc', 'embedded', 'wrap');
215 5b237745 Scott Ullrich
216
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
217 be81829f Scott Ullrich
218
	/* check for bad control characters */
219 5b237745 Scott Ullrich
	foreach ($postdata as $pn => $pd) {
220
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
221
			$input_errors[] = "The field '" . $pn . "' contains invalid characters.";
222
		}
223
	}
224 be81829f Scott Ullrich
225 5b237745 Scott Ullrich
	for ($i = 0; $i < count($reqdfields); $i++) {
226 d1cfae7b Scott Ullrich
		if ($_POST[$reqdfields[$i]] == "") {
227 5b237745 Scott Ullrich
			$input_errors[] = "The field '" . $reqdfieldsn[$i] . "' is required.";
228
		}
229 be81829f Scott Ullrich
	}
230 5b237745 Scott Ullrich
}
231
232
function print_input_errors($input_errors) {
233 30af342f Erik Kristensen
	global $g;
234 5b237745 Scott Ullrich
	echo "<p><table border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">\n";
235 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";
236 5b237745 Scott Ullrich
	echo "<td bgcolor=\"#FFD9D1\" style=\"padding-left: 8px; padding-top: 6px\">";
237 be81829f Scott Ullrich
238 5b237745 Scott Ullrich
	echo "<span class=\"errmsg\"><p>The following input errors were detected:<ul>\n";
239
	foreach ($input_errors as $ierr) {
240
		echo "<li>" . htmlspecialchars($ierr) . "</li>\n";
241
	}
242
	echo "</ul></span>";
243 be81829f Scott Ullrich
244 76dddec6 Scott Ullrich
	echo "</td></tr></table></p>&nbsp;<br>";
245 5b237745 Scott Ullrich
}
246
247
function exec_rc_script($scriptname) {
248 be81829f Scott Ullrich
249 5b237745 Scott Ullrich
	global $d_sysrebootreqd_path;
250 be81829f Scott Ullrich
251 767a716e Scott Ullrich
	$execoutput = "";
252
	$retval = "";
253 5b237745 Scott Ullrich
	exec($scriptname . " >/dev/null 2>&1", $execoutput, $retval);
254 be81829f Scott Ullrich
	return $retval;
255 5b237745 Scott Ullrich
}
256
257
function exec_rc_script_async($scriptname) {
258 be81829f Scott Ullrich
259 5b237745 Scott Ullrich
	global $d_sysrebootreqd_path;
260 767a716e Scott Ullrich
	$execoutput = "";
261
	$retval = "";
262 be81829f Scott Ullrich
263 5b237745 Scott Ullrich
	exec("nohup " . $scriptname . " >/dev/null 2>&1 &", $execoutput, $retval);
264 be81829f Scott Ullrich
	return $retval;
265 5b237745 Scott Ullrich
}
266
267
function verify_gzip_file($fname) {
268
269 1ba03d69 Scott Ullrich
    $returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
270 5b237745 Scott Ullrich
	if ($returnvar != 0)
271
		return 0;
272
	else
273
		return 1;
274
}
275
276 a2dab9bc Bill Marquette
function print_info_box_np($msg, $name="apply",$value="Apply changes") {
277 30af342f Erik Kristensen
	global $g;
278 d07299bc Scott Ullrich
	echo "<table height=\"32\" width=\"100%\">\n";
279 c438d2f8 Scott Ullrich
	echo "  <tr>\n";
280
	echo "   <td>\n";
281
	echo "      <div style='background-color:#990000' id='redbox'>\n";
282
	echo "       <table width='100%'><tr><td width='8%'>\n";
283 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";
284 a0509c58 Scott Ullrich
	echo "        </td>\n";
285
	echo "        <td width='70%'><font color='white'><b>{$msg}</b></font>\n";
286
	echo "        </td>";
287 3462a529 Matthew Grooms
	if(stristr($msg, "apply") == true || stristr($msg, "save") || stristr($msg, "create")) {
288 541207da Scott Ullrich
		echo "         <td>";
289 a2dab9bc Bill Marquette
		echo "           <input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\">\n";
290 541207da Scott Ullrich
		echo "         </td>";
291
	}
292 a0509c58 Scott Ullrich
	echo "        </tr></table>\n";
293
	echo "       </div>\n";
294 c438d2f8 Scott Ullrich
	echo "    </td>\n";
295
	echo "</table>\n";
296
	echo "<script type=\"text/javascript\">\n";
297 d07299bc Scott Ullrich
	echo "NiftyCheck();\n";
298
	echo "Rounded(\"div#redbox\",\"all\",\"#FFF\",\"#990000\",\"smooth\");\n";
299
	echo "Rounded(\"td#blackbox\",\"all\",\"#FFF\",\"#000000\",\"smooth\");\n";
300 c438d2f8 Scott Ullrich
	echo "</script>\n";
301 cd4087bf Scott Ullrich
	echo "\n<br>\n";
302 5b237745 Scott Ullrich
}
303
304
function print_info_box($msg) {
305
	echo "<p>";
306
	print_info_box_np($msg);
307
	echo "</p>";
308
}
309
310
function get_std_save_message($ok) {
311
	global $d_sysrebootreqd_path;
312
313 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.";
314 5b237745 Scott Ullrich
}
315
316
function pprint_address($adr) {
317
	global $specialnets;
318
319
	if (isset($adr['any'])) {
320
		$padr = "*";
321
	} else if ($adr['network']) {
322 369578b1 Scott Ullrich
		if (preg_match("/opt[0-999]ip/", $adr['network'])) {
323
			$padr = "Interface IP address";
324
		} else {
325
			$padr = $specialnets[$adr['network']];
326
		}
327 5b237745 Scott Ullrich
	} else {
328
		$padr = $adr['address'];
329
	}
330 be81829f Scott Ullrich
331 5b237745 Scott Ullrich
	if (isset($adr['not']))
332
		$padr = "! " . $padr;
333 be81829f Scott Ullrich
334 5b237745 Scott Ullrich
	return $padr;
335
}
336
337
function pprint_port($port) {
338
	global $wkports;
339
340
	$pport = "";
341 be81829f Scott Ullrich
342 5b237745 Scott Ullrich
	if (!$port)
343 3a54b6ca Scott Dale
		return "*";
344 5b237745 Scott Ullrich
	else {
345
		$srcport = explode("-", $port);
346
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
347
			$pport = $srcport[0];
348
			if ($wkports[$srcport[0]]) {
349
				$pport .= " (" . $wkports[$srcport[0]] . ")";
350
			}
351
		} else
352
			$pport .= $srcport[0] . " - " . $srcport[1];
353
	}
354 be81829f Scott Ullrich
355 5b237745 Scott Ullrich
	return $pport;
356
}
357
358 7afb67e0 Scott Ullrich
function captiveportal_users_sort() {
359
	global $g, $config;
360 d1cfae7b Scott Ullrich
361 7afb67e0 Scott Ullrich
	function cpusercmp($a, $b) {
362
		return strcasecmp($a['name'], $b['name']);
363
	}
364 d1cfae7b Scott Ullrich
365 7afb67e0 Scott Ullrich
	usort($config['captiveportal']['user'], "cpusercmp");
366
}
367
368 fab7ff44 Bill Marquette
function admin_groups_sort() {
369
	global $g, $config;
370
371
	function cpusercmp($a, $b) {
372
		return strcasecmp($a['name'], $b['name']);
373
	}
374
375
	usort($config['system']['group'], "cpusercmp");
376
}
377
378
function admin_users_sort() {
379
	global $g, $config;
380
381
	function cpusercmp($a, $b) {
382
		return strcasecmp($a['name'], $b['name']);
383
	}
384
385
	usort($config['system']['user'], "cpusercmp");
386
}
387
388 5b237745 Scott Ullrich
/* sort by interface only, retain the original order of rules that apply to
389
   the same interface */
390
function filter_rules_sort() {
391 63637de9 Bill Marquette
	global $config;
392 be81829f Scott Ullrich
393 5b237745 Scott Ullrich
	/* mark each rule with the sequence number (to retain the order while sorting) */
394
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
395
		$config['filter']['rule'][$i]['seq'] = $i;
396 be81829f Scott Ullrich
397 5b237745 Scott Ullrich
	function filtercmp($a, $b) {
398
		if ($a['interface'] == $b['interface'])
399
			return $a['seq'] - $b['seq'];
400
		else
401
			return -strcmp($a['interface'], $b['interface']);
402
	}
403 be81829f Scott Ullrich
404 5b237745 Scott Ullrich
	usort($config['filter']['rule'], "filtercmp");
405 be81829f Scott Ullrich
406 5b237745 Scott Ullrich
	/* strip the sequence numbers again */
407
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
408
		unset($config['filter']['rule'][$i]['seq']);
409
}
410
411
function nat_rules_sort() {
412 63637de9 Bill Marquette
	global $config;
413 be81829f Scott Ullrich
414 5b237745 Scott Ullrich
	function natcmp($a, $b) {
415
		if ($a['external-address'] == $b['external-address']) {
416
			if ($a['protocol'] == $b['protocol']) {
417
				if ($a['external-port'] == $b['external-port']) {
418
					return 0;
419
				} else {
420
					return ($a['external-port'] - $b['external-port']);
421
				}
422
			} else {
423
				return strcmp($a['protocol'], $b['protocol']);
424
			}
425
		} else if (!$a['external-address'])
426
			return 1;
427
		else if (!$b['external-address'])
428
			return -1;
429
		else
430
			return ipcmp($a['external-address'], $b['external-address']);
431
	}
432 be81829f Scott Ullrich
433 5b237745 Scott Ullrich
	usort($config['nat']['rule'], "natcmp");
434
}
435
436
function nat_1to1_rules_sort() {
437
	global $g, $config;
438 be81829f Scott Ullrich
439 5b237745 Scott Ullrich
	function nat1to1cmp($a, $b) {
440
		return ipcmp($a['external'], $b['external']);
441
	}
442 be81829f Scott Ullrich
443 5b237745 Scott Ullrich
	usort($config['nat']['onetoone'], "nat1to1cmp");
444
}
445
446
function nat_server_rules_sort() {
447
	global $g, $config;
448 be81829f Scott Ullrich
449 5b237745 Scott Ullrich
	function natservercmp($a, $b) {
450
		return ipcmp($a['ipaddr'], $b['ipaddr']);
451
	}
452 be81829f Scott Ullrich
453 5b237745 Scott Ullrich
	usort($config['nat']['servernat'], "natservercmp");
454
}
455
456
function nat_out_rules_sort() {
457
	global $g, $config;
458 be81829f Scott Ullrich
459 5b237745 Scott Ullrich
	function natoutcmp($a, $b) {
460
		return strcmp($a['source']['network'], $b['source']['network']);
461
	}
462 be81829f Scott Ullrich
463 5b237745 Scott Ullrich
	usort($config['nat']['advancedoutbound']['rule'], "natoutcmp");
464
}
465
466
function pptpd_users_sort() {
467
	global $g, $config;
468 be81829f Scott Ullrich
469 5b237745 Scott Ullrich
	function usercmp($a, $b) {
470
		return strcasecmp($a['name'], $b['name']);
471
	}
472 be81829f Scott Ullrich
473 5b237745 Scott Ullrich
	usort($config['pptpd']['user'], "usercmp");
474
}
475
476 a007827a Scott Ullrich
function openvpn_users_sort() {
477
	global $g, $config;
478
479
	function usercmp($a, $b) {
480
		return strcasecmp($a['name'], $b['name']);
481
	}
482
483
	usort($config['openvpn']['user'], "usercmp");
484
}
485
486 4f1b515b Scott Ullrich
function pppoe_users_sort() {
487
	global $g, $config;
488
489
	function usercmp($a, $b) {
490
		return strcasecmp($a['name'], $b['name']);
491
	}
492
493
	usort($config['pppoe']['user'], "usercmp");
494
}
495
496 5b237745 Scott Ullrich
function staticroutes_sort() {
497
	global $g, $config;
498
499
	function staticroutecmp($a, $b) {
500
		return strcmp($a['network'], $b['network']);
501
	}
502
503
	usort($config['staticroutes']['route'], "staticroutecmp");
504
}
505
506
function hosts_sort() {
507
	global $g, $config;
508
509
	function hostcmp($a, $b) {
510
		return strcasecmp($a['host'], $b['host']);
511
	}
512
513
	usort($config['dnsmasq']['hosts'], "hostcmp");
514
}
515
516 fa550a17 Scott Ullrich
function staticmaps_sort($ifgui) {
517 5b237745 Scott Ullrich
	global $g, $config;
518
519
	function staticmapcmp($a, $b) {
520
		return ipcmp($a['ipaddr'], $b['ipaddr']);
521
	}
522
523 fa550a17 Scott Ullrich
	usort($config['dhcpd'][$ifgui]['staticmap'], "staticmapcmp");
524 5b237745 Scott Ullrich
}
525
526
function aliases_sort() {
527
	global $g, $config;
528
529
	function aliascmp($a, $b) {
530
		return strcmp($a['name'], $b['name']);
531
	}
532
533
	usort($config['aliases']['alias'], "aliascmp");
534
}
535
536 615b27bc Scott Dale
function schedule_sort(){
537
	global $g, $config;
538
539
	function schedulecmp($a, $b) {
540
		return strcmp($a['name'], $b['name']);
541
	}
542
543
	usort($config['schedules']['schedule'], "schedulecmp");
544
545
}
546
547 5b237745 Scott Ullrich
function ipsec_mobilekey_sort() {
548
	global $g, $config;
549
550
	function mobilekeycmp($a, $b) {
551
		return strcmp($a['ident'][0], $b['ident'][0]);
552
	}
553
554
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
555
}
556
557
function proxyarp_sort() {
558
	global $g, $config;
559
560
	function proxyarpcmp($a, $b) {
561
		if (isset($a['network']))
562
			list($ast,$asn) = explode("/", $a['network']);
563
		else if (isset($a['range'])) {
564
			$ast = $a['range']['from'];
565
			$asn = 32;
566
		}
567
		if (isset($b['network']))
568
			list($bst,$bsn) = explode("/", $b['network']);
569
		else if (isset($b['range'])) {
570
			$bst = $b['range']['from'];
571
			$bsn = 32;
572
		}
573
		if (ipcmp($ast, $bst) == 0)
574
			return ($asn - $bsn);
575
		else
576
			return ipcmp($ast, $bst);
577
	}
578
579
	usort($config['proxyarp']['proxyarpnet'], "proxyarpcmp");
580
}
581
582
function passthrumacs_sort() {
583
	global $g, $config;
584
585
	function passthrumacscmp($a, $b) {
586
		return strcmp($a['mac'], $b['mac']);
587
	}
588 be81829f Scott Ullrich
589 5b237745 Scott Ullrich
	usort($config['captiveportal']['passthrumac'],"passthrumacscmp");
590
}
591
592 7afb67e0 Scott Ullrich
function cpelements_sort() {
593
	global $g, $config;
594
595
	function cpelementscmp($a, $b) {
596
		return strcasecmp($a['name'], $b['name']);
597
	}
598 d1cfae7b Scott Ullrich
599 7afb67e0 Scott Ullrich
	usort($config['captiveportal']['element'],"cpelementscmp");
600
}
601
602 5b237745 Scott Ullrich
function allowedips_sort() {
603
	global $g, $config;
604
605
	function allowedipscmp($a, $b) {
606
		return strcmp($a['ip'], $b['ip']);
607
	}
608 be81829f Scott Ullrich
609 5b237745 Scott Ullrich
	usort($config['captiveportal']['allowedip'],"allowedipscmp");
610
}
611
612
function wol_sort() {
613
	global $g, $config;
614
615
	function wolcmp($a, $b) {
616
		return strcmp($a['descr'], $b['descr']);
617
	}
618 be81829f Scott Ullrich
619 5b237745 Scott Ullrich
	usort($config['wol']['wolentry'], "wolcmp");
620
}
621
622 9d9adc7f Scott Ullrich
function gentitle($title) {
623
	global $navlevelsep;
624 c3deaf7f Scott Ullrich
	if(!is_array($title))
625
		return $title;
626
	else
627
		return join($navlevelsep, $title); 
628 9d9adc7f Scott Ullrich
}
629
630
function genhtmltitle($title) {
631
        global $config;
632 479628e6 Scott Ullrich
        return gentitle($title);
633 5b237745 Scott Ullrich
}
634
635 d345bd39 Bill Marquette
/* update the changedesc and changecount(er) variables */
636
function update_changedesc($update) {
637
	global $changedesc;
638
	global $changecount;
639
640
	$changedesc .= " {$update}";
641
	$changecount++;
642
}
643
644 15c37284 Colin Smith
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
645 23f1e22a Colin Smith
	global $g, $config;
646 767a716e Scott Ullrich
    $sor = isset($config['syslog']['reverse']) ? "-r" : "";
647
    $logarr = "";
648 0fcfdd3d Scott Ullrich
	$grepline = "  ";
649 fa626e70 Scott Ullrich
	if(is_array($grepfor))
650
		foreach($grepfor as $agrep)
651
			$grepline .= " | grep \"$agrep\"";
652
	if(is_array($grepinvert))
653
		foreach($grepinvert as $agrep)
654
			$grepline .= " | grep -v \"$agrep\"";
655 2d29214c Scott Ullrich
	if($config['system']['disablesyslogclog']) 
656
		exec("cat {$logfile}{$grepline} | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
657
	else 
658
		exec("/usr/sbin/clog {$logfile}{$grepline} | grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
659 5e3de917 Chris Buechler
    /* comment out debug code
660 0fcfdd3d Scott Ullrich
    echo "<!-- /usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail} -->";
661 5e3de917 Chris Buechler
    */
662 0fcfdd3d Scott Ullrich
    foreach ($logarr as $logent) {
663
            $logent = preg_split("/\s+/", $logent, 6);
664
            echo "<tr valign=\"top\">\n";
665
            if ($withorig) {
666
            		$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
667
                    echo "<td class=\"listlr\" nowrap>" . $entry_date_time  . "</td>\n";
668
                    echo "<td class=\"listr\">" . htmlspecialchars($logent[4] . " " . $logent[5]) . "</td>\n";
669
            } else {
670
                    echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
671
            }
672
            echo "</tr>\n";
673
    }
674 2c64eed7 Colin Smith
}
675
676 43e7c36c Bill Marquette
function return_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
677 784c448c Scott Ullrich
	global $g, $config;
678 43e7c36c Bill Marquette
	$sor = (isset($config['syslog']['reverse']) || $grepreverse) ? "-r" : "";
679 784c448c Scott Ullrich
	$logarr = "";
680 a0e4bd9f Scott Ullrich
	$grepline = "  ";
681 fa626e70 Scott Ullrich
	if(is_array($grepfor))
682
		foreach($grepfor as $agrep)
683
			$grepline .= " | grep \"$agrep\"";
684
	if(is_array($grepinvert))
685
		foreach($grepinvert as $agrep)
686
			$grepline .= " | grep -v \"$agrep\"";
687 5e3de917 Chris Buechler
	/* comment out debug code
688 fa626e70 Scott Ullrich
	echo "<!-- /usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail} -->";
689 5e3de917 Chris Buechler
	*/
690 2d29214c Scott Ullrich
	if($config['system']['disablesyslogclog']) 
691
		exec("cat {$logfile}{$grepline} | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
692
	else 
693
		exec("/usr/sbin/clog {$logfile}{$grepline} | grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
694 fa626e70 Scott Ullrich
	return($logarr);
695 784c448c Scott Ullrich
}
696
697 4d762703 Bill Marquette
/* Check if variable has changed, update and log if it has
698 edf14245 Bill Marquette
 * returns true if var changed
699 4d762703 Bill Marquette
 * varname = variable name in plain text
700
 * orig = original value
701
 * new = new value
702
 */
703 9eab73da Bill Marquette
function update_if_changed($varname, & $orig, $new) {
704 8b3ccf12 Bill Marquette
	if (is_array($orig) && is_array($new)) {
705
		$a_diff = array_diff($orig, $new);
706
		foreach ($a_diff as $diff) {
707
			update_changedesc("removed {$varname}: \"{$diff}\"");
708
		}
709
		$a_diff = array_diff($new, $orig);
710
		foreach ($a_diff as $diff) {
711
			update_changedesc("added {$varname}: \"{$diff}\"");
712
		}
713 4d762703 Bill Marquette
		$orig = $new;
714 edf14245 Bill Marquette
		return true;
715 d1cfae7b Scott Ullrich
716
	} else {
717 8b3ccf12 Bill Marquette
		if ($orig != $new) {
718
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
719
			$orig = $new;
720
			return true;
721
		}
722 4d762703 Bill Marquette
	}
723 edf14245 Bill Marquette
	return false;
724 4d762703 Bill Marquette
}
725
726 9a4cb6b7 Scott Ullrich
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
727
728
        if (isset($adr['any']))
729
                $padr = "any";
730
        else if ($adr['network'])
731
                $padr = $adr['network'];
732
        else if ($adr['address']) {
733
                list($padr, $pmask) = explode("/", $adr['address']);
734
                if (!$pmask)
735
                        $pmask = 32;
736
        }
737
738
        if (isset($adr['not']))
739
                $pnot = 1;
740
        else
741
                $pnot = 0;
742
743
        if ($adr['port']) {
744
                list($pbeginport, $pendport) = explode("-", $adr['port']);
745
                if (!$pendport)
746
                        $pendport = $pbeginport;
747
        } else {
748
                if(alias_expand($pbeginport) <> "" || alias_expand($pendport) <> "") {
749
                        /* Item is a port alias */
750
                } else {
751
                        $pbeginport = "any";
752
                        $pendport = "any";
753
                }
754
        }
755
}
756
757
function pconfig_to_address(&$adr, $padr, $pmask, $pnot=false, $pbeginport=0, $pendport=0) {
758
759
        $adr = array();
760
761
        if ($padr == "any")
762
                $adr['any'] = true;
763
        else if (is_specialnet($padr))
764
                $adr['network'] = $padr;
765
        else {
766
                $adr['address'] = $padr;
767
                if ($pmask != 32)
768
                        $adr['address'] .= "/" . $pmask;
769
        }
770
771
        if ($pnot)
772
                $adr['not'] = true;
773
        else
774 63637de9 Bill Marquette
                unset($adr['not']);
775 9a4cb6b7 Scott Ullrich
776
        if (($pbeginport != 0) && ($pbeginport != "any")) {
777
                if ($pbeginport != $pendport)
778
                        $adr['port'] = $pbeginport . "-" . $pendport;
779
                else
780
                        $adr['port'] = $pbeginport;
781
        }
782
783
        if(alias_expand($pbeginport)) {
784
                $adr['port'] = $pbeginport;
785
        }
786
}
787
788
function is_specialnet($net) {
789
        global $specialsrcdst;
790 8c984f57 Scott Ullrich
		if(!$net) 
791
			return false;
792 9a4cb6b7 Scott Ullrich
        if (in_array($net, $specialsrcdst) || strstr($net, "opt"))
793
                return true;
794
        else
795
                return false;
796
}
797
798 c6a74fa1 Scott Ullrich
function ipsec_ca_sort() {
799
        global $g, $config;
800
801
        function ipseccacmp($a, $b) {
802
                return strcmp($a['ident'], $b['ident']);
803
        }
804
805
        usort($config['ipsec']['cacert'], "ipseccacmp");
806
}
807
808 b93f9af4 Scott Dale
//function to create widget tabs when called
809
function display_widget_tabs(& $tab_array) {	
810
	echo "<div id='tabs'>";
811
	$tabscounter = 0;
812
	foreach ($tab_array as $ta) {
813
	$dashpos = strpos($ta[2],'-');
814
	$tabname = $ta[2] . "-tab";
815
	$tabclass = substr($ta[2],0,$dashpos);
816
	$tabclass = $tabclass . "-class";
817
		if ($ta[1] == true) {
818
			$tabActive = "table-cell";
819
			$tabNonActive = "none";
820
		} 
821
		else {
822
			$tabActive = "none";
823
			$tabNonActive = "table-cell";
824
		}
825
		echo "<div id='{$ta[2]}-active' class='{$tabclass}-tabactive' style='display:{$tabActive}; background-color:#EEEEEE; color:black;'>";
826
		echo "<B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
827
		echo "&nbsp;&nbsp;&nbsp;</B>";
828
		echo "</div>";
829
		
830
		echo "<div id='{$ta[2]}-deactive' class='{$tabclass}-tabdeactive' style='display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;' onClick=\"return changeTabDIV('{$ta[2]}')\">";
831
		echo "<B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
832
		echo "&nbsp;&nbsp;&nbsp;</B>";
833
		echo "</div>";
834
	}
835
	
836
	echo "<script type=\"text/javascript\">";
837
	echo "NiftyCheck();\n";
838
	echo "Rounded(\"div.{$tabclass}-tabactive\",\"top\",\"#CCCCCC\",\"#EEEEEE\",\"smooth\");\n";
839
	echo "Rounded(\"div.{$tabclass}-tabdeactive\",\"top\",\"#CCCCCC\",\"#777777\",\"smooth\");\n";
840
	echo "</script>";
841
	echo "</div>";
842
}
843
844 06a9dc5a Scott Ullrich
845
// Return inline javascript file or CSS to minimizie 
846
// request count going back to server.
847
function outputJavaScriptFileInline($javascript) {
848
	if(file_exists($javascript)) {
849 b77e126b Scott Ullrich
		echo "\n<script type=\"text/javascript\">\n";
850 06a9dc5a Scott Ullrich
		include($javascript);
851 b77e126b Scott Ullrich
		echo "\n</script>\n";
852 06a9dc5a Scott Ullrich
	} else {
853 fb673a31 Scott Ullrich
		echo "\n\n<!-- Could not location file:  {$javascript} -->\n\n";
854 06a9dc5a Scott Ullrich
	}
855
}
856
857
858 76cd0d89 Scott Ullrich
859
function outputCSSPrintFileInline($css) {
860
	if(file_exists($css)) {
861
		echo "\n<style media=\"print\" type=\"text/css\">\n";
862
		include($css);
863
		echo "\n</style>\n";
864
	} else {
865 fb673a31 Scott Ullrich
		echo "\n\n<!-- Could not location file:  {$css} -->\n\n";
866 76cd0d89 Scott Ullrich
	}
867
}
868
869
870 06a9dc5a Scott Ullrich
function outputCSSFileInline($css) {
871
	if(file_exists($css)) {
872 b77e126b Scott Ullrich
		echo "\n<style type=\"text/css\">\n";
873 06a9dc5a Scott Ullrich
		include($css);
874 b77e126b Scott Ullrich
		echo "\n</style>\n";
875 06a9dc5a Scott Ullrich
	} else {
876 fb673a31 Scott Ullrich
		echo "\n\n<!-- Could not location file:  {$css} -->\n\n";
877 06a9dc5a Scott Ullrich
	}
878
}
879
880 50d86c13 Bill Marquette
$rfc2616 = array(
881
	100 => "100 Continue",
882
	101 => "101 Switching Protocols",
883
	200 => "200 OK",
884
	201 => "201 Created",
885
	202 => "202 Accepted",
886
	203 => "203 Non-Authoritative Information",
887
	204 => "204 No Content",
888
	205 => "205 Reset Content",
889
	206 => "206 Partial Content",
890
	300 => "300 Multiple Choices",
891
	301 => "301 Moved Permanently",
892
	302 => "302 Found",
893
	303 => "303 See Other",
894
	304 => "304 Not Modified",
895
	305 => "305 Use Proxy",
896
	306 => "306 (Unused)",
897
	307 => "307 Temporary Redirect",
898
	400 => "400 Bad Request",
899
	401 => "401 Unauthorized",
900
	402 => "402 Payment Required",
901
	403 => "403 Forbidden",
902
	404 => "404 Not Found",
903
	405 => "405 Method Not Allowed",
904
	406 => "406 Not Acceptable",
905
	407 => "407 Proxy Authentication Required",
906
	408 => "408 Request Timeout",
907
	409 => "409 Conflict",
908
	410 => "410 Gone",
909
	411 => "411 Length Required",
910
	412 => "412 Precondition Failed",
911
	413 => "413 Request Entity Too Large",
912
	414 => "414 Request-URI Too Long",
913
	415 => "415 Unsupported Media Type",
914
	416 => "416 Requested Range Not Satisfiable",
915
	417 => "417 Expectation Failed",
916
	500 => "500 Internal Server Error",
917
	501 => "501 Not Implemented",
918
	502 => "502 Bad Gateway",
919
	503 => "503 Service Unavailable",
920
	504 => "504 Gateway Timeout",
921
	505 => "505 HTTP Version Not Supported"
922
);
923
924
function is_rfc2616_code($code) {
925
	global $rfc2616;
926
	if (isset($rfc2616[$code]))
927
		return true;
928
	else
929
		return false;
930
}
931
932
function print_rfc2616_select($tag, $current){
933
	global $rfc2616;
934 6b07c15a Matthew Grooms
935 50d86c13 Bill Marquette
	/* Default to 200 OK if not set */
936
	if ($current == "")
937
		$current = 200;
938
939
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";	
940
	foreach($rfc2616 as $code => $message) {
941
		if ($code == $current) {
942
			$sel = " selected";
943
		} else {
944
			$sel = "";
945
		}
946
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
947
	}
948
}
949
950 0919224f Bill Marquette
// Useful debugging function, much cleaner than print_r
951
function echo_array($array,$return_me=false){
952
    if(is_array($array) == false){
953
        $return = "The provided variable is not an array.";
954
    }else{
955
        foreach($array as $name=>$value){
956
            if(is_array($value)){
957
                $return .= "";
958
                $return .= "['<b>$name</b>'] {<div style='margin-left:10px;'>\n";
959
                $return .= echo_array($value,true);
960
                $return .= "</div>}";
961
                $return .= "\n\n";
962
            }else{
963
                if(is_string($value)){
964
                    $value = "\"$value\"";
965
                }
966
                $return .= "['<b>$name</b>'] = $value\n\n";
967
            }
968
        }
969
    }
970
    if($return_me == true){
971
        return $return;
972
    }else{
973
        echo "<pre>".$return."</pre>";
974
    }
975
}
976
977
978 4476d447 Ermal Luçi
?>