Project

General

Profile

Download (28.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	guiconfig.inc
5
	by Scott Ullrich, Copyright 2004, All rights reserved.
6
	originally based on of m0n0wall (http://m0n0.ch/wall)
7

    
8
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
9
	All rights reserved.
10

    
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13

    
14
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16

    
17
	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

    
21
	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
/* Include authentication routines */
34
/* THIS MUST BE ABOVE ALL OTHER CODE */
35
require_once("authgui.inc");
36

    
37
/* make sure nothing is cached */
38
if (!$omit_nocacheheaders) {
39
	header("Expires: 0");
40
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
41
	header("Cache-Control: no-store, no-cache, must-revalidate");
42
	header("Cache-Control: post-check=0, pre-check=0", false);
43
	header("Pragma: no-cache");
44
}
45

    
46
/* parse the configuration and include all configuration functions */
47
require_once("config.inc");
48
require_once("functions.inc");
49

    
50
/* 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
/*
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
/*
66
 *  If this device is an apple ipod/iphone
67
 *  switch the theme to one that works with it.
68
 */
69
$apple_ua = array("iPhone","iPod");
70
foreach($apple_ua as $useragent)
71
	if(strstr($_SERVER['HTTP_USER_AGENT'], $useragent))
72
		$g['theme'] = "pfsense";
73

    
74
$d_landirty_path = $g['varrun_path'] . "/lan.conf.dirty";
75
$d_pppoeuserdirty_path = $g['varrun_path'] . "/vpn-pppoe-users-edit.dirty";
76
$d_hostsdirty_path = $g['varrun_path'] . "/hosts.dirty";
77
$d_natconfdirty_path = $g['varrun_path'] . "/nat.conf.dirty";
78
$d_filterconfdirty_path = $g['varrun_path'] . "/filter.conf.dirty";
79
$d_ipsecconfdirty_path = $g['varrun_path'] . "/ipsec.conf.dirty";
80
$d_shaperconfdirty_path = $g['varrun_path'] . "/shaper.conf.dirty";
81
$d_pptpuserdirty_path = $g['varrun_path'] . "/pptpd.user.dirty";
82
$d_dnsmasqdirty_path = $g['varrun_path'] . "/dnsmasq.dirty";
83
$d_staticmapsdirty_path = $g['varrun_path'] . "/staticmaps.dirty";
84
$d_staticroutesdirty_path = $g['varrun_path'] . "/staticroutes.dirty";
85
$d_aliasesdirty_path = $g['varrun_path'] . "/aliases.dirty";
86
$d_proxyarpdirty_path = $g['varrun_path'] . "/proxyarp.dirty";
87
$d_fwupenabled_path = $g['varrun_path'] . "/fwup.enabled";
88
$d_firmwarelock_path = $g['varrun_path'] . "/firmware.lock";
89
$d_sysrebootreqd_path = $g['varrun_path'] . "/sysreboot.reqd";
90
$d_passthrumacsdirty_path = $g['varrun_path'] . "/passthrumacs.dirty";
91
$d_allowedipsdirty_path = $g['varrun_path'] . "/allowedips.dirty";
92
$d_ovpnclidirty_path = $g['varrun_path'] . "/ovpnclient.dirty";
93
$d_vipconfdirty_path = $g['varrun_path'] . "/vip.conf.dirty";
94
$d_sysctldirty_path = $g['varrun_path'] . "/sysctl.conf.dirty";
95
$d_vsconfdirty_path = $g['varrun_path'] . "/vs.conf.dirty";
96
$d_shaperconfdirty_path = $g['varrun_path'] . "/shaper.conf.dirty";
97

    
98
/* OpenVPN Directories */
99
$d_ovpnsrvdirty_path = "/tmp/ovpn-srv.dirty";
100
$d_ovpncrldirty_path = "/tmp/ovpn-crl.dirty";
101
$d_ovpnclidirty_path = "/tmp/ovpn-cli.dirty";
102
/* used by progress bar */
103
$lastseen = "-1";
104

    
105
$navlevelsep = ": ";	/* navigation level separator string */
106
$mandfldhtml = "";		/* display this before mandatory input fields */
107
$mandfldhtmlspc = "";	/* same as above, but with spacing */
108

    
109
if (file_exists($d_firmwarelock_path)) {
110
	if (!$d_isfwfile) {
111
		header("Location: system_firmware.php");
112
		exit;
113
	} else {
114
		return;
115
	}
116
}
117

    
118
$auth_server_types = array(
119
	'ldap' => "LDAP",
120
	'radius' => "Radius");
121

    
122
$ldap_urltypes = array(
123
	'TCP - Standard' => 389,
124
	'SSL - Encrypted' => 636);
125

    
126
$ldap_scopes = array(
127
	'one' => "One Level",
128
	'subtree' => "Entire Subtree");
129

    
130
$ldap_protvers = array(
131
	2,
132
	3);
133

    
134
$ldap_templates = array(
135

    
136
	'open' => array(
137
				'desc' => "OpenLDAP",
138
				'attr_user' => "cn",
139
				'attr_group' => "cn",
140
				'attr_member' => "member"),
141

    
142
	'msad' => array(
143
				'desc' => "Microsoft AD",
144
				'attr_user' => "samAccountNAme",
145
				'attr_group' => "cn",
146
				'attr_member' => "member"),
147

    
148
	'edir' => array(
149
				'desc' => "Novell eDirectory",
150
				'attr_user' => "cn",
151
				'attr_group' => "cn",
152
				'attr_member' => "uniqueMember"));
153

    
154
$radius_srvcs = array(
155
	'both' => "Authentication and Accounting",
156
	'auth' => "Authentication",
157
	'acct' => "Accounting");
158

    
159
$netbios_nodetypes = array(
160
	'0' => "none",
161
	'1' => "b-node",
162
	'2' => "p-node",
163
	'4' => "m-node",
164
	'5' => "h-node");
165

    
166
/* some well knows ports */
167
$wkports = array(
168
	5999 => "CVSup",	
169
	53 => "DNS",
170
	21 => "FTP",
171
	3000 => "HBCI",
172
	80 => "HTTP",
173
	443 => "HTTPS",
174
	5190 => "ICQ",
175
	113 => "IDENT/AUTH",
176
	143 => "IMAP",
177
	993 => "IMAP/S",
178
	4500 => "IPsec NAT-T",
179
	500 => "ISAKMP",
180
	1701 => "L2TP",
181
	389 => "LDAP",
182
	1755 => "MMS/TCP",
183
	7000 => "MMS/UDP",
184
	445 => "MS DS",
185
	3389 => "MS RDP",
186
	1512 => "MS WINS",
187
	1863 => "MSN",
188
	119 => "NNTP",
189
	123 => "NTP",
190
	138 => "NetBIOS-DGM",
191
	137 => "NetBIOS-NS",
192
	139 => "NetBIOS-SSN",
193
	1194 => "OpenVPN",
194
	110 => "POP3",
195
	995 => "POP3/S",
196
	1723 => "PPTP",	
197
	1812 => "RADIUS",
198
	1813 => "RADIUS accounting",
199
	5004 => "RTP",
200
	5060 => "SIP",
201
	25 => "SMTP",
202
	465 => "SMTP/S",
203
	161 => "SNMP",
204
	162 => "SNMP-Trap",
205
	22 => "SSH",
206
	3478 => "STUN",
207
	23 => "Telnet",
208
	69 => "TFTP",
209
	5900 => "VNC");
210

    
211
$specialnets = array("wanip" => "WAN address", "lanip" => "LAN address", "lan" => "LAN net", "pptp" => "PPTP clients");
212

    
213
$spiflist = get_configured_interface_with_descr(true, true);
214
foreach ($spiflist as $ifgui => $ifdesc) {
215
	$specialnets[$ifgui] = $ifdesc . " net";
216
}
217

    
218
$medias = array("auto" => "autoselect", "100full" => "100BASE-TX full-duplex",
219
	"100half" => "100BASE-TX half-duplex", "10full" => "10BASE-T full-duplex",
220
	"10half" => "10BASE-T half-duplex");
221

    
222
/* platforms that support firmware updating */
223
$fwupplatforms = array('pfSense', 'net45xx', 'net48xx', 'generic-pc', 'embedded', 'wrap');
224

    
225
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
226

    
227
	/* check for bad control characters */
228
	foreach ($postdata as $pn => $pd) {
229
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
230
			$input_errors[] = "The field '" . $pn . "' contains invalid characters.";
231
		}
232
	}
233

    
234
	for ($i = 0; $i < count($reqdfields); $i++) {
235
		if ($_POST[$reqdfields[$i]] == "") {
236
			$input_errors[] = "The field '" . $reqdfieldsn[$i] . "' is required.";
237
		}
238
	}
239
}
240

    
241
function print_input_errors($input_errors) {
242
	global $g;
243

    
244
	print <<<EOF
245
	<p>
246
	<table border="0" cellspacing="0" cellpadding="4" width="100%">
247
	<tr>
248
		<td class="inputerrorsleft">
249
			<img src="./themes/{$g['theme']}/images/icons/icon_error.gif">
250
		</td>
251
		<td class="inputerrorsright">
252
			<span class="errmsg"><p>
253
				The following input errors were detected:
254
				<ul>
255
EOF;
256
		foreach ($input_errors as $ierr) {
257
			echo "<li>" . htmlspecialchars($ierr) . "</li>";
258
		}
259

    
260
	print <<<EOF2
261
				</ul>
262
			</span>
263
		</td></tr>
264
	</table>
265
	</p>&nbsp;<br>
266
EOF2;
267
	
268
}
269

    
270
function exec_rc_script($scriptname) {
271

    
272
	global $d_sysrebootreqd_path;
273

    
274
	$execoutput = "";
275
	$retval = "";
276
	exec($scriptname . " >/dev/null 2>&1", $execoutput, $retval);
277
	return $retval;
278
}
279

    
280
function exec_rc_script_async($scriptname) {
281

    
282
	global $d_sysrebootreqd_path;
283
	$execoutput = "";
284
	$retval = "";
285

    
286
	exec("nohup " . $scriptname . " >/dev/null 2>&1 &", $execoutput, $retval);
287
	return $retval;
288
}
289

    
290
function verify_gzip_file($fname) {
291

    
292
    $returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
293
	if ($returnvar != 0)
294
		return 0;
295
	else
296
		return 1;
297
}
298

    
299
function print_info_box_np($msg, $name="apply",$value="Apply changes") {
300
	global $g;
301
	
302
	if(stristr($msg, "apply") == true || stristr($msg, "save") || stristr($msg, "create")) {
303
		$savebutton = "<td class='infoboxsave'>";
304
		$savebutton .= "<input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\">";
305
		if($_POST['if']) 
306
			$savebutton .= "<input type='hidden' name='if' value='{$_POST['if']}'>";
307
		$savebutton.="</td>";
308
	}
309
	$nifty_redbox = "#990000";
310
	$nifty_blackbox = "#000000";
311
	
312
	$themename = $g['theme'];
313
	
314
	if(file_exists("/usr/local/www/themes/{$themename}/tabcontrols.php")) {
315
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/tabcontrols.php");
316
		eval($toeval);
317
	}
318
	
319
	if(file_exists("/usr/local/www/themes/{$themename}/infobox.php")) {
320
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/infobox.php");
321
		eval($toeval);
322
	}	
323
		
324
	echo <<<EOFnp
325
	<table class='infobox'>
326
		<tr>
327
			<td>
328
				<div class='infoboxnp' id='redbox'>
329
					<table class='infoboxnptable2'>
330
						<tr>
331
							<td class='infoboxnptd'>
332
								&nbsp;&nbsp;&nbsp;<img class='infoboxnpimg' src="/themes/{$g['theme']}/images/icons/icon_exclam.gif" >
333
							</td>
334
							<td class='infoboxnptd2'>
335
								<b>{$msg}</b>
336
							</td>
337
							{$savebutton}
338
						</tr>
339
					</table>
340
				</div>
341
			</td>
342
		</tr>
343
	</table>
344
	<script type="text/javascript">
345
		NiftyCheck();
346
		Rounded("div#redbox","all","#FFF","{$nifty_redbox}","smooth");
347
		Rounded("td#blackbox","all","#FFF","{$nifty_blackbox}","smooth");
348
	</script>
349
	<br/>
350
EOFnp;
351

    
352
}
353

    
354
function print_info_box($msg) {
355
	echo "<p>";
356
	print_info_box_np($msg);
357
	echo "</p>";
358
}
359

    
360
function get_std_save_message($ok) {
361
	global $d_sysrebootreqd_path;
362

    
363
	return "The changes have been applied successfully.  You can also <a href='status_filter_reload.php'>monitor</a> the filter reload progress.";
364
}
365

    
366
function pprint_address($adr) {
367
	global $specialnets;
368

    
369
	if (isset($adr['any'])) {
370
		$padr = "*";
371
	} else if ($adr['network']) {
372
		if (preg_match("/opt[0-999]ip/", $adr['network'])) {
373
			$padr = "Interface IP address";
374
		} else {
375
			$padr = $specialnets[$adr['network']];
376
		}
377
	} else {
378
		$padr = $adr['address'];
379
	}
380

    
381
	if (isset($adr['not']))
382
		$padr = "! " . $padr;
383

    
384
	return $padr;
385
}
386

    
387
function pprint_port($port) {
388
	global $wkports;
389

    
390
	$pport = "";
391

    
392
	if (!$port)
393
		return "*";
394
	else {
395
		$srcport = explode("-", $port);
396
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
397
			$pport = $srcport[0];
398
			if ($wkports[$srcport[0]]) {
399
				$pport .= " (" . $wkports[$srcport[0]] . ")";
400
			}
401
		} else
402
			$pport .= $srcport[0] . " - " . $srcport[1];
403
	}
404

    
405
	return $pport;
406
}
407

    
408
function captiveportal_users_sort() {
409
	global $g, $config;
410

    
411
	if (!is_array($config['captiveportal']['user']))
412
                return;
413

    
414
	function cpusercmp($a, $b) {
415
		return strcasecmp($a['name'], $b['name']);
416
	}
417

    
418
	usort($config['captiveportal']['user'], "cpusercmp");
419
}
420

    
421
function admin_groups_sort() {
422
	global $g, $config;
423

    
424
	if (!is_array($config['system']['group']))
425
                return;
426

    
427
	function cpusercmp($a, $b) {
428
		return strcasecmp($a['name'], $b['name']);
429
	}
430

    
431
	usort($config['system']['group'], "cpusercmp");
432
}
433

    
434
function admin_users_sort() {
435
	global $g, $config;
436

    
437
	if (!is_array($config['system']['user']))
438
                return;
439

    
440
	function cpusercmp($a, $b) {
441
		return strcasecmp($a['name'], $b['name']);
442
	}
443

    
444
	usort($config['system']['user'], "cpusercmp");
445
}
446

    
447
/* sort by interface only, retain the original order of rules that apply to
448
   the same interface */
449
function filter_rules_sort() {
450
	global $config;
451

    
452
	/* mark each rule with the sequence number (to retain the order while sorting) */
453
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
454
		$config['filter']['rule'][$i]['seq'] = $i;
455

    
456
	function filtercmp($a, $b) {
457
		if ($a['interface'] == $b['interface'])
458
			return $a['seq'] - $b['seq'];
459
		else
460
			return -strcmp($a['interface'], $b['interface']);
461
	}
462

    
463
	usort($config['filter']['rule'], "filtercmp");
464

    
465
	/* strip the sequence numbers again */
466
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
467
		unset($config['filter']['rule'][$i]['seq']);
468
}
469

    
470
function nat_rules_sort() {
471
	global $config;
472

    
473
	if (!is_array($config['nat']['rule']))
474
                return;
475

    
476
	function natcmp($a, $b) {
477
		if ($a['external-address'] == $b['external-address']) {
478
			if ($a['protocol'] == $b['protocol']) {
479
				if ($a['external-port'] == $b['external-port']) {
480
					return 0;
481
				} else {
482
					return ($a['external-port'] - $b['external-port']);
483
				}
484
			} else {
485
				return strcmp($a['protocol'], $b['protocol']);
486
			}
487
		} else if (!$a['external-address'])
488
			return 1;
489
		else if (!$b['external-address'])
490
			return -1;
491
		else
492
			return ipcmp($a['external-address'], $b['external-address']);
493
	}
494

    
495
	usort($config['nat']['rule'], "natcmp");
496
}
497

    
498
function nat_1to1_rules_sort() {
499
	global $g, $config;
500

    
501
	if (!is_array($config['nat']['onetoone']))
502
                return;
503

    
504
	function nat1to1cmp($a, $b) {
505
		return ipcmp($a['external'], $b['external']);
506
	}
507

    
508
	usort($config['nat']['onetoone'], "nat1to1cmp");
509
}
510

    
511
function nat_server_rules_sort() {
512
	global $g, $config;
513

    
514
	if (!is_array($config['nat']['servernat']))
515
                return;
516

    
517
	function natservercmp($a, $b) {
518
		return ipcmp($a['ipaddr'], $b['ipaddr']);
519
	}
520

    
521
	usort($config['nat']['servernat'], "natservercmp");
522
}
523

    
524
function nat_out_rules_sort() {
525
	global $g, $config;
526

    
527
	function natoutcmp($a, $b) {
528
		return strcmp($a['source']['network'], $b['source']['network']);
529
	}
530

    
531
	usort($config['nat']['advancedoutbound']['rule'], "natoutcmp");
532
}
533

    
534
function pptpd_users_sort() {
535
	global $g, $config;
536

    
537
	if (!is_array($config['ppptpd']['user']))
538
                return;
539

    
540
	function usercmp($a, $b) {
541
		return strcasecmp($a['name'], $b['name']);
542
	}
543

    
544
	usort($config['pptpd']['user'], "usercmp");
545
}
546

    
547
function  l2tp_users_sort()  { 
548
        global  $g,  $config; 
549
 
550
	if (!is_array($config['l2tp']['user']))
551
		return;
552

    
553
        function  usercmp($a,  $b)  { 
554
                return  strcasecmp($a['name'],  $b['name']); 
555
        } 
556
	 
557
        usort($config['l2tp']['user'],  "usercmp");  
558
}
559

    
560
function openvpn_users_sort() {
561
	global $g, $config;
562

    
563
	if (!is_array($config['openvpn']['user']))
564
                return;
565

    
566
	function usercmp($a, $b) {
567
		return strcasecmp($a['name'], $b['name']);
568
	}
569

    
570
	usort($config['openvpn']['user'], "usercmp");
571
}
572

    
573
function pppoe_users_sort() {
574
	global $g, $config;
575

    
576
	if (!is_array($config['pppoe']['user']))
577
                return;
578

    
579
	function usercmp($a, $b) {
580
		return strcasecmp($a['name'], $b['name']);
581
	}
582

    
583
	usort($config['pppoe']['user'], "usercmp");
584
}
585

    
586
function staticroutes_sort() {
587
	global $g, $config;
588

    
589
	if (!is_array($config['staticroutes']['route']))
590
                return;
591

    
592
	function staticroutecmp($a, $b) {
593
		return strcmp($a['network'], $b['network']);
594
	}
595

    
596
	usort($config['staticroutes']['route'], "staticroutecmp");
597
}
598

    
599
function hosts_sort() {
600
	global $g, $config;
601

    
602
	if (!is_array($config['dnsmasq']['hosts']))
603
                return;
604

    
605
	function hostcmp($a, $b) {
606
		return strcasecmp($a['host'], $b['host']);
607
	}
608

    
609
	usort($config['dnsmasq']['hosts'], "hostcmp");
610
}
611

    
612
function staticmaps_sort($ifgui) {
613
	global $g, $config;
614

    
615
	function staticmapcmp($a, $b) {
616
		return ipcmp($a['ipaddr'], $b['ipaddr']);
617
	}
618

    
619
	usort($config['dhcpd'][$ifgui]['staticmap'], "staticmapcmp");
620
}
621

    
622
function aliases_sort() {
623
	global $g, $config;
624

    
625
	function aliascmp($a, $b) {
626
		return strcmp($a['name'], $b['name']);
627
	}
628

    
629
	if($config['aliases'])
630
		usort($config['aliases']['alias'], "aliascmp");
631
}
632

    
633
function schedule_sort(){
634
	global $g, $config;
635

    
636
	if (!is_array($config['schedules']['schedule']))
637
                return;
638

    
639
	function schedulecmp($a, $b) {
640
		return strcmp($a['name'], $b['name']);
641
	}
642

    
643
	usort($config['schedules']['schedule'], "schedulecmp");
644

    
645
}
646

    
647
function ipsec_mobilekey_sort() {
648
	global $g, $config;
649

    
650
	function mobilekeycmp($a, $b) {
651
		return strcmp($a['ident'][0], $b['ident'][0]);
652
	}
653

    
654
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
655
}
656

    
657
function proxyarp_sort() {
658
	global $g, $config;
659

    
660
	function proxyarpcmp($a, $b) {
661
		if (isset($a['network']))
662
			list($ast,$asn) = explode("/", $a['network']);
663
		else if (isset($a['range'])) {
664
			$ast = $a['range']['from'];
665
			$asn = 32;
666
		}
667
		if (isset($b['network']))
668
			list($bst,$bsn) = explode("/", $b['network']);
669
		else if (isset($b['range'])) {
670
			$bst = $b['range']['from'];
671
			$bsn = 32;
672
		}
673
		if (ipcmp($ast, $bst) == 0)
674
			return ($asn - $bsn);
675
		else
676
			return ipcmp($ast, $bst);
677
	}
678

    
679
	usort($config['proxyarp']['proxyarpnet'], "proxyarpcmp");
680
}
681

    
682
function passthrumacs_sort() {
683
	global $g, $config;
684

    
685
	function passthrumacscmp($a, $b) {
686
		return strcmp($a['mac'], $b['mac']);
687
	}
688

    
689
	usort($config['captiveportal']['passthrumac'],"passthrumacscmp");
690
}
691

    
692
function cpelements_sort() {
693
	global $g, $config;
694

    
695
	function cpelementscmp($a, $b) {
696
		return strcasecmp($a['name'], $b['name']);
697
	}
698

    
699
	usort($config['captiveportal']['element'],"cpelementscmp");
700
}
701

    
702
function allowedips_sort() {
703
	global $g, $config;
704

    
705
	function allowedipscmp($a, $b) {
706
		return strcmp($a['ip'], $b['ip']);
707
	}
708

    
709
	usort($config['captiveportal']['allowedip'],"allowedipscmp");
710
}
711

    
712
function wol_sort() {
713
	global $g, $config;
714

    
715
	function wolcmp($a, $b) {
716
		return strcmp($a['descr'], $b['descr']);
717
	}
718

    
719
	usort($config['wol']['wolentry'], "wolcmp");
720
}
721

    
722
function gentitle($title) {
723
	global $navlevelsep;
724
	if(!is_array($title))
725
		return $title;
726
	else
727
		return join($navlevelsep, $title); 
728
}
729

    
730
function genhtmltitle($title) {
731
        global $config;
732
        return gentitle($title);
733
}
734

    
735
/* update the changedesc and changecount(er) variables */
736
function update_changedesc($update) {
737
	global $changedesc;
738
	global $changecount;
739

    
740
	$changedesc .= " {$update}";
741
	$changecount++;
742
}
743

    
744
function clear_log_file($logfile = "/var/log/system.log") {
745
	global $config, $g;
746
	exec("/usr/bin/killall syslogd");
747
	if(isset($config['system']['disablesyslogclog'])) {
748
		unlink($logfile);
749
		touch($logfile);
750
	} else {
751
		if(isset($config['system']['usefifolog'])) 
752
			exec("/usr/sbin/fifolog_create -s 511488 {$logfile}");
753
		else
754
			exec("/usr/sbin/clog -i -s 511488 {$logfile}");
755
	}
756
	system_syslogd_start();	
757
}
758

    
759
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
760
	global $g, $config;
761
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
762
	$logarr = "";
763
	$grepline = "  ";
764
	if(is_array($grepfor))
765
		foreach($grepfor as $agrep)
766
			$grepline .= " | grep \"$agrep\"";
767
	if(is_array($grepinvert))
768
		foreach($grepinvert as $agrep)
769
			$grepline .= " | grep -v \"$agrep\"";
770
	if(file_exists($logfile) && filesize($logfile) == 0) {
771
		$logarr = array("Log file started.");
772
	} else {
773
		if($config['system']['disablesyslogclog']) {
774
			exec("cat {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
775
		} else {
776
			if(isset($config['system']['usefifolog']))
777
				exec("/usr/sbin/fifolog_reader {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
778
			else
779
				exec("/usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
780
		}
781
	}
782
	foreach ($logarr as $logent) {
783
			$logent = preg_split("/\s+/", $logent, 6);
784
			echo "<tr valign=\"top\">\n";
785
			if ($withorig) {
786
					if(isset($config['system']['usefifolog'])) {
787
						$entry_date_time = htmlspecialchars(date("F j, Y, g:i a","" . $logent[1] . ""));
788
						$entry_text = htmlspecialchars($logent[5]);
789
					} else {
790
						$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
791
						$entry_text = htmlspecialchars($logent[4] . " " . $logent[5]);
792
					}
793
					echo "<td class=\"listlr\" nowrap>{$entry_date_time}</td>\n";
794
					echo "<td class=\"listr\">{$entry_text}</td>\n";
795

    
796
			} else {
797
					echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
798
			}
799
			echo "</tr>\n";
800
	}
801
}
802

    
803
function return_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
804
	global $g, $config;
805
	$sor = (isset($config['syslog']['reverse']) || $grepreverse) ? "-r" : "";
806
	$logarr = "";
807
	$grepline = "  ";
808
	if(is_array($grepfor))
809
		foreach($grepfor as $agrep)
810
			$grepline .= " | grep \"$agrep\"";
811
	if(is_array($grepinvert))
812
		foreach($grepinvert as $agrep)
813
			$grepline .= " | grep -v \"$agrep\"";
814
	if($config['system']['disablesyslogclog']) {
815
		exec("cat {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
816
	} else {
817
		if(isset($config['system']['usefifolog'])) {
818
			exec("/usr/sbin/fifolog_reader {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);			
819
		} else {
820
			exec("/usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
821
		}
822
	}
823
	return($logarr);
824
}
825

    
826
/* Check if variable has changed, update and log if it has
827
 * returns true if var changed
828
 * varname = variable name in plain text
829
 * orig = original value
830
 * new = new value
831
 */
832
function update_if_changed($varname, & $orig, $new) {
833
	if (is_array($orig) && is_array($new)) {
834
		$a_diff = array_diff($orig, $new);
835
		foreach ($a_diff as $diff) {
836
			update_changedesc("removed {$varname}: \"{$diff}\"");
837
		}
838
		$a_diff = array_diff($new, $orig);
839
		foreach ($a_diff as $diff) {
840
			update_changedesc("added {$varname}: \"{$diff}\"");
841
		}
842
		$orig = $new;
843
		return true;
844

    
845
	} else {
846
		if ($orig != $new) {
847
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
848
			$orig = $new;
849
			return true;
850
		}
851
	}
852
	return false;
853
}
854

    
855
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
856

    
857
        if (isset($adr['any']))
858
                $padr = "any";
859
        else if ($adr['network'])
860
                $padr = $adr['network'];
861
        else if ($adr['address']) {
862
                list($padr, $pmask) = explode("/", $adr['address']);
863
                if (!$pmask)
864
                        $pmask = 32;
865
        }
866

    
867
        if (isset($adr['not']))
868
                $pnot = 1;
869
        else
870
                $pnot = 0;
871

    
872
        if ($adr['port']) {
873
                list($pbeginport, $pendport) = explode("-", $adr['port']);
874
                if (!$pendport)
875
                        $pendport = $pbeginport;
876
        } else {
877
                if(alias_expand($pbeginport) <> "" || alias_expand($pendport) <> "") {
878
                        /* Item is a port alias */
879
                } else {
880
                        $pbeginport = "any";
881
                        $pendport = "any";
882
                }
883
        }
884
}
885

    
886
function pconfig_to_address(&$adr, $padr, $pmask, $pnot=false, $pbeginport=0, $pendport=0) {
887

    
888
        $adr = array();
889

    
890
        if ($padr == "any")
891
                $adr['any'] = true;
892
        else if (is_specialnet($padr))
893
                $adr['network'] = $padr;
894
        else {
895
                $adr['address'] = $padr;
896
                if ($pmask != 32)
897
                        $adr['address'] .= "/" . $pmask;
898
        }
899

    
900
        if ($pnot)
901
                $adr['not'] = true;
902
        else
903
                unset($adr['not']);
904

    
905
        if (($pbeginport != 0) && ($pbeginport != "any")) {
906
                if ($pbeginport != $pendport)
907
                        $adr['port'] = $pbeginport . "-" . $pendport;
908
                else
909
                        $adr['port'] = $pbeginport;
910
        }
911

    
912
        if(alias_expand($pbeginport)) {
913
                $adr['port'] = $pbeginport;
914
        }
915
}
916

    
917
function is_specialnet($net) {
918
        global $specialsrcdst;
919
		if(!$net) 
920
			return false;
921
        if (in_array($net, $specialsrcdst) || strstr($net, "opt"))
922
                return true;
923
        else
924
                return false;
925
}
926

    
927
function ipsec_ca_sort() {
928
        global $g, $config;
929

    
930
        function ipseccacmp($a, $b) {
931
                return strcmp($a['ident'], $b['ident']);
932
        }
933

    
934
        usort($config['ipsec']['cacert'], "ipseccacmp");
935
}
936

    
937
//function to create widget tabs when called
938
function display_widget_tabs(& $tab_array) {	
939
	echo "<div id='tabs'>";
940
	$tabscounter = 0;
941
	foreach ($tab_array as $ta) {
942
	$dashpos = strpos($ta[2],'-');
943
	$tabname = $ta[2] . "-tab";
944
	$tabclass = substr($ta[2],0,$dashpos);
945
	$tabclass = $tabclass . "-class";
946
		if ($ta[1] == true) {
947
			$tabActive = "table-cell";
948
			$tabNonActive = "none";
949
		} 
950
		else {
951
			$tabActive = "none";
952
			$tabNonActive = "table-cell";
953
		}
954
		echo "<div id='{$ta[2]}-active' class='{$tabclass}-tabactive' style='display:{$tabActive}; background-color:#EEEEEE; color:black;'>";
955
		echo "<B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
956
		echo "&nbsp;&nbsp;&nbsp;</B>";
957
		echo "</div>";
958
		
959
		echo "<div id='{$ta[2]}-deactive' class='{$tabclass}-tabdeactive' style='display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;' onClick=\"return changeTabDIV('{$ta[2]}')\">";
960
		echo "<B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
961
		echo "&nbsp;&nbsp;&nbsp;</B>";
962
		echo "</div>";
963
	}
964
	
965
	echo "<script type=\"text/javascript\">";
966
	echo "NiftyCheck();\n";
967
	echo "Rounded(\"div.{$tabclass}-tabactive\",\"top\",\"#CCCCCC\",\"#EEEEEE\",\"smooth\");\n";
968
	echo "Rounded(\"div.{$tabclass}-tabdeactive\",\"top\",\"#CCCCCC\",\"#777777\",\"smooth\");\n";
969
	echo "</script>";
970
	echo "</div>";
971
}
972

    
973

    
974
// Return inline javascript file or CSS to minimizie 
975
// request count going back to server.
976
function outputJavaScriptFileInline($javascript) {
977
	if(file_exists($javascript)) {
978
		echo "\n<script type=\"text/javascript\">\n";
979
		include($javascript);
980
		echo "\n</script>\n";
981
	} else {
982
		echo "\n\n<!-- Could not location file:  {$javascript} -->\n\n";
983
	}
984
}
985

    
986

    
987

    
988
function outputCSSPrintFileInline($css) {
989
	if(file_exists($css)) {
990
		echo "\n<style media=\"print\" type=\"text/css\">\n";
991
		include($css);
992
		echo "\n</style>\n";
993
	} else {
994
		echo "\n\n<!-- Could not location file:  {$css} -->\n\n";
995
	}
996
}
997

    
998

    
999
function outputCSSFileInline($css) {
1000
	if(file_exists($css)) {
1001
		echo "\n<style type=\"text/css\">\n";
1002
		include($css);
1003
		echo "\n</style>\n";
1004
	} else {
1005
		echo "\n\n<!-- Could not location file:  {$css} -->\n\n";
1006
	}
1007
}
1008

    
1009
$rfc2616 = array(
1010
	100 => "100 Continue",
1011
	101 => "101 Switching Protocols",
1012
	200 => "200 OK",
1013
	201 => "201 Created",
1014
	202 => "202 Accepted",
1015
	203 => "203 Non-Authoritative Information",
1016
	204 => "204 No Content",
1017
	205 => "205 Reset Content",
1018
	206 => "206 Partial Content",
1019
	300 => "300 Multiple Choices",
1020
	301 => "301 Moved Permanently",
1021
	302 => "302 Found",
1022
	303 => "303 See Other",
1023
	304 => "304 Not Modified",
1024
	305 => "305 Use Proxy",
1025
	306 => "306 (Unused)",
1026
	307 => "307 Temporary Redirect",
1027
	400 => "400 Bad Request",
1028
	401 => "401 Unauthorized",
1029
	402 => "402 Payment Required",
1030
	403 => "403 Forbidden",
1031
	404 => "404 Not Found",
1032
	405 => "405 Method Not Allowed",
1033
	406 => "406 Not Acceptable",
1034
	407 => "407 Proxy Authentication Required",
1035
	408 => "408 Request Timeout",
1036
	409 => "409 Conflict",
1037
	410 => "410 Gone",
1038
	411 => "411 Length Required",
1039
	412 => "412 Precondition Failed",
1040
	413 => "413 Request Entity Too Large",
1041
	414 => "414 Request-URI Too Long",
1042
	415 => "415 Unsupported Media Type",
1043
	416 => "416 Requested Range Not Satisfiable",
1044
	417 => "417 Expectation Failed",
1045
	500 => "500 Internal Server Error",
1046
	501 => "501 Not Implemented",
1047
	502 => "502 Bad Gateway",
1048
	503 => "503 Service Unavailable",
1049
	504 => "504 Gateway Timeout",
1050
	505 => "505 HTTP Version Not Supported"
1051
);
1052

    
1053
function is_rfc2616_code($code) {
1054
	global $rfc2616;
1055
	if (isset($rfc2616[$code]))
1056
		return true;
1057
	else
1058
		return false;
1059
}
1060

    
1061
function print_rfc2616_select($tag, $current){
1062
	global $rfc2616;
1063

    
1064
	/* Default to 200 OK if not set */
1065
	if ($current == "")
1066
		$current = 200;
1067

    
1068
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";	
1069
	foreach($rfc2616 as $code => $message) {
1070
		if ($code == $current) {
1071
			$sel = " selected";
1072
		} else {
1073
			$sel = "";
1074
		}
1075
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
1076
	}
1077
}
1078

    
1079
// Useful debugging function, much cleaner than print_r
1080
function echo_array($array,$return_me=false){
1081
    if(is_array($array) == false){
1082
        $return = "The provided variable is not an array.";
1083
    }else{
1084
        foreach($array as $name=>$value){
1085
            if(is_array($value)){
1086
                $return .= "";
1087
                $return .= "['<b>$name</b>'] {<div style='margin-left:10px;'>\n";
1088
                $return .= echo_array($value,true);
1089
                $return .= "</div>}";
1090
                $return .= "\n\n";
1091
            }else{
1092
                if(is_string($value)){
1093
                    $value = "\"$value\"";
1094
                }
1095
                $return .= "['<b>$name</b>'] = $value\n\n";
1096
            }
1097
        }
1098
    }
1099
    if($return_me == true){
1100
        return $return;
1101
    }else{
1102
        echo "<pre>".$return."</pre>";
1103
    }
1104
}
1105

    
1106
function add_package_tabs($tabgroup, & $tab_array) {
1107
	global $config, $g;
1108

    
1109
	if(!is_array($config['installedpackages']))
1110
		return;
1111
	if(!is_array($config['installedpackages']['tab']))
1112
		return;
1113

    
1114
	foreach($config['installedpackages']['tab'] as $tab) {
1115
		if ($tab['group'] !== $group)
1116
			continue;
1117
		$tab_entry = array();
1118
		if($tab['name']) {
1119
			$tab_entry[] = $tab['name'];
1120
			$tab_entry[] = false;
1121
			$tab_entry[] = $tab['url'];
1122
			$tab_array[] = $tab_entry;
1123
		}
1124
	}
1125
}
1126

    
1127
?>
(68-68/207)