Project

General

Profile

Download (27.1 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
	3389 => "MS RDP",
169
	21 => "FTP",
170
	22 => "SSH",
171
	23 => "Telnet",
172
	25 => "SMTP",
173
	53 => "DNS",
174
	69 => "TFTP",
175
	80 => "HTTP",
176
	110 => "POP3",
177
	113 => "IDENT/AUTH",
178
	119 => "NNTP",
179
	123 => "NTP",
180
	137 => "NetBIOS-NS",
181
	138 => "NetBIOS-DGM",
182
	139 => "NetBIOS-SSN",
183
	143 => "IMAP",
184
	161 => "SNMP",
185
	162 => "SNMP-Trap",
186
	389 => "LDAP",
187
	443 => "HTTPS",
188
	445 => "MS DS",
189
	465 => "SMTP/S",
190
	500 => "isakmp",
191
	993 => "IMAP/S",
192
	995 => "POP3/S",
193
	1194 => "OpenVPN",
194
	1512 => "MS WINS",
195
	1701 => "L2TP",
196
	1723 => "PPTP",
197
	1755 => "MMS/TCP",
198
	1812 => "Radius",
199
	1813 => "Radius-accounting",
200
	1863 => "MSN",
201
	3000 => "HBCI",
202
	3478 => "STUN",
203
	4500 => "IPsec NAT-T",
204
	5004 => "RTP",
205
	5060 => "SIP",
206
	5190 => "ICQ",
207
	5900 => "VNC",
208
	5999 => "CVSup",
209
	7000 => "MMS/UDP");
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
	function cpusercmp($a, $b) {
412
		return strcasecmp($a['name'], $b['name']);
413
	}
414

    
415
	usort($config['captiveportal']['user'], "cpusercmp");
416
}
417

    
418
function admin_groups_sort() {
419
	global $g, $config;
420

    
421
	function cpusercmp($a, $b) {
422
		return strcasecmp($a['name'], $b['name']);
423
	}
424

    
425
	usort($config['system']['group'], "cpusercmp");
426
}
427

    
428
function admin_users_sort() {
429
	global $g, $config;
430

    
431
	function cpusercmp($a, $b) {
432
		return strcasecmp($a['name'], $b['name']);
433
	}
434

    
435
	usort($config['system']['user'], "cpusercmp");
436
}
437

    
438
/* sort by interface only, retain the original order of rules that apply to
439
   the same interface */
440
function filter_rules_sort() {
441
	global $config;
442

    
443
	/* mark each rule with the sequence number (to retain the order while sorting) */
444
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
445
		$config['filter']['rule'][$i]['seq'] = $i;
446

    
447
	function filtercmp($a, $b) {
448
		if ($a['interface'] == $b['interface'])
449
			return $a['seq'] - $b['seq'];
450
		else
451
			return -strcmp($a['interface'], $b['interface']);
452
	}
453

    
454
	usort($config['filter']['rule'], "filtercmp");
455

    
456
	/* strip the sequence numbers again */
457
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
458
		unset($config['filter']['rule'][$i]['seq']);
459
}
460

    
461
function nat_rules_sort() {
462
	global $config;
463

    
464
	function natcmp($a, $b) {
465
		if ($a['external-address'] == $b['external-address']) {
466
			if ($a['protocol'] == $b['protocol']) {
467
				if ($a['external-port'] == $b['external-port']) {
468
					return 0;
469
				} else {
470
					return ($a['external-port'] - $b['external-port']);
471
				}
472
			} else {
473
				return strcmp($a['protocol'], $b['protocol']);
474
			}
475
		} else if (!$a['external-address'])
476
			return 1;
477
		else if (!$b['external-address'])
478
			return -1;
479
		else
480
			return ipcmp($a['external-address'], $b['external-address']);
481
	}
482

    
483
	usort($config['nat']['rule'], "natcmp");
484
}
485

    
486
function nat_1to1_rules_sort() {
487
	global $g, $config;
488

    
489
	function nat1to1cmp($a, $b) {
490
		return ipcmp($a['external'], $b['external']);
491
	}
492

    
493
	usort($config['nat']['onetoone'], "nat1to1cmp");
494
}
495

    
496
function nat_server_rules_sort() {
497
	global $g, $config;
498

    
499
	function natservercmp($a, $b) {
500
		return ipcmp($a['ipaddr'], $b['ipaddr']);
501
	}
502

    
503
	usort($config['nat']['servernat'], "natservercmp");
504
}
505

    
506
function nat_out_rules_sort() {
507
	global $g, $config;
508

    
509
	function natoutcmp($a, $b) {
510
		return strcmp($a['source']['network'], $b['source']['network']);
511
	}
512

    
513
	usort($config['nat']['advancedoutbound']['rule'], "natoutcmp");
514
}
515

    
516
function pptpd_users_sort() {
517
	global $g, $config;
518

    
519
	function usercmp($a, $b) {
520
		return strcasecmp($a['name'], $b['name']);
521
	}
522

    
523
	usort($config['pptpd']['user'], "usercmp");
524
}
525

    
526
function openvpn_users_sort() {
527
	global $g, $config;
528

    
529
	function usercmp($a, $b) {
530
		return strcasecmp($a['name'], $b['name']);
531
	}
532

    
533
	usort($config['openvpn']['user'], "usercmp");
534
}
535

    
536
function pppoe_users_sort() {
537
	global $g, $config;
538

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

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

    
546
function staticroutes_sort() {
547
	global $g, $config;
548

    
549
	function staticroutecmp($a, $b) {
550
		return strcmp($a['network'], $b['network']);
551
	}
552

    
553
	usort($config['staticroutes']['route'], "staticroutecmp");
554
}
555

    
556
function hosts_sort() {
557
	global $g, $config;
558

    
559
	function hostcmp($a, $b) {
560
		return strcasecmp($a['host'], $b['host']);
561
	}
562

    
563
	usort($config['dnsmasq']['hosts'], "hostcmp");
564
}
565

    
566
function staticmaps_sort($ifgui) {
567
	global $g, $config;
568

    
569
	function staticmapcmp($a, $b) {
570
		return ipcmp($a['ipaddr'], $b['ipaddr']);
571
	}
572

    
573
	usort($config['dhcpd'][$ifgui]['staticmap'], "staticmapcmp");
574
}
575

    
576
function aliases_sort() {
577
	global $g, $config;
578

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

    
583
	if($config['aliases'])
584
		usort($config['aliases']['alias'], "aliascmp");
585
}
586

    
587
function schedule_sort(){
588
	global $g, $config;
589

    
590
	function schedulecmp($a, $b) {
591
		return strcmp($a['name'], $b['name']);
592
	}
593

    
594
	usort($config['schedules']['schedule'], "schedulecmp");
595

    
596
}
597

    
598
function ipsec_mobilekey_sort() {
599
	global $g, $config;
600

    
601
	function mobilekeycmp($a, $b) {
602
		return strcmp($a['ident'][0], $b['ident'][0]);
603
	}
604

    
605
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
606
}
607

    
608
function proxyarp_sort() {
609
	global $g, $config;
610

    
611
	function proxyarpcmp($a, $b) {
612
		if (isset($a['network']))
613
			list($ast,$asn) = explode("/", $a['network']);
614
		else if (isset($a['range'])) {
615
			$ast = $a['range']['from'];
616
			$asn = 32;
617
		}
618
		if (isset($b['network']))
619
			list($bst,$bsn) = explode("/", $b['network']);
620
		else if (isset($b['range'])) {
621
			$bst = $b['range']['from'];
622
			$bsn = 32;
623
		}
624
		if (ipcmp($ast, $bst) == 0)
625
			return ($asn - $bsn);
626
		else
627
			return ipcmp($ast, $bst);
628
	}
629

    
630
	usort($config['proxyarp']['proxyarpnet'], "proxyarpcmp");
631
}
632

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

    
636
	function passthrumacscmp($a, $b) {
637
		return strcmp($a['mac'], $b['mac']);
638
	}
639

    
640
	usort($config['captiveportal']['passthrumac'],"passthrumacscmp");
641
}
642

    
643
function cpelements_sort() {
644
	global $g, $config;
645

    
646
	function cpelementscmp($a, $b) {
647
		return strcasecmp($a['name'], $b['name']);
648
	}
649

    
650
	usort($config['captiveportal']['element'],"cpelementscmp");
651
}
652

    
653
function allowedips_sort() {
654
	global $g, $config;
655

    
656
	function allowedipscmp($a, $b) {
657
		return strcmp($a['ip'], $b['ip']);
658
	}
659

    
660
	usort($config['captiveportal']['allowedip'],"allowedipscmp");
661
}
662

    
663
function wol_sort() {
664
	global $g, $config;
665

    
666
	function wolcmp($a, $b) {
667
		return strcmp($a['descr'], $b['descr']);
668
	}
669

    
670
	usort($config['wol']['wolentry'], "wolcmp");
671
}
672

    
673
function gentitle($title) {
674
	global $navlevelsep;
675
	if(!is_array($title))
676
		return $title;
677
	else
678
		return join($navlevelsep, $title); 
679
}
680

    
681
function genhtmltitle($title) {
682
        global $config;
683
        return gentitle($title);
684
}
685

    
686
/* update the changedesc and changecount(er) variables */
687
function update_changedesc($update) {
688
	global $changedesc;
689
	global $changecount;
690

    
691
	$changedesc .= " {$update}";
692
	$changecount++;
693
}
694

    
695
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
696
	global $g, $config;
697
    $sor = isset($config['syslog']['reverse']) ? "-r" : "";
698
    $logarr = "";
699
	$grepline = "  ";
700
	if(is_array($grepfor))
701
		foreach($grepfor as $agrep)
702
			$grepline .= " | grep \"$agrep\"";
703
	if(is_array($grepinvert))
704
		foreach($grepinvert as $agrep)
705
			$grepline .= " | grep -v \"$agrep\"";
706
	if($config['system']['disablesyslogclog']) 
707
		exec("cat {$logfile}{$grepline} | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
708
	else 
709
		exec("/usr/sbin/clog {$logfile}{$grepline} | grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
710
    /* comment out debug code
711
    echo "<!-- /usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail} -->";
712
    */
713
    foreach ($logarr as $logent) {
714
            $logent = preg_split("/\s+/", $logent, 6);
715
            echo "<tr valign=\"top\">\n";
716
            if ($withorig) {
717
            		$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
718
                    echo "<td class=\"listlr\" nowrap>" . $entry_date_time  . "</td>\n";
719
                    echo "<td class=\"listr\">" . htmlspecialchars($logent[4] . " " . $logent[5]) . "</td>\n";
720
            } else {
721
                    echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
722
            }
723
            echo "</tr>\n";
724
    }
725
}
726

    
727
function return_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
728
	global $g, $config;
729
	$sor = (isset($config['syslog']['reverse']) || $grepreverse) ? "-r" : "";
730
	$logarr = "";
731
	$grepline = "  ";
732
	if(is_array($grepfor))
733
		foreach($grepfor as $agrep)
734
			$grepline .= " | grep \"$agrep\"";
735
	if(is_array($grepinvert))
736
		foreach($grepinvert as $agrep)
737
			$grepline .= " | grep -v \"$agrep\"";
738
	/* comment out debug code
739
	echo "<!-- /usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail} -->";
740
	*/
741
	if($config['system']['disablesyslogclog']) 
742
		exec("cat {$logfile}{$grepline} | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
743
	else 
744
		exec("/usr/sbin/clog {$logfile}{$grepline} | grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
745
	return($logarr);
746
}
747

    
748
/* Check if variable has changed, update and log if it has
749
 * returns true if var changed
750
 * varname = variable name in plain text
751
 * orig = original value
752
 * new = new value
753
 */
754
function update_if_changed($varname, & $orig, $new) {
755
	if (is_array($orig) && is_array($new)) {
756
		$a_diff = array_diff($orig, $new);
757
		foreach ($a_diff as $diff) {
758
			update_changedesc("removed {$varname}: \"{$diff}\"");
759
		}
760
		$a_diff = array_diff($new, $orig);
761
		foreach ($a_diff as $diff) {
762
			update_changedesc("added {$varname}: \"{$diff}\"");
763
		}
764
		$orig = $new;
765
		return true;
766

    
767
	} else {
768
		if ($orig != $new) {
769
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
770
			$orig = $new;
771
			return true;
772
		}
773
	}
774
	return false;
775
}
776

    
777
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
778

    
779
        if (isset($adr['any']))
780
                $padr = "any";
781
        else if ($adr['network'])
782
                $padr = $adr['network'];
783
        else if ($adr['address']) {
784
                list($padr, $pmask) = explode("/", $adr['address']);
785
                if (!$pmask)
786
                        $pmask = 32;
787
        }
788

    
789
        if (isset($adr['not']))
790
                $pnot = 1;
791
        else
792
                $pnot = 0;
793

    
794
        if ($adr['port']) {
795
                list($pbeginport, $pendport) = explode("-", $adr['port']);
796
                if (!$pendport)
797
                        $pendport = $pbeginport;
798
        } else {
799
                if(alias_expand($pbeginport) <> "" || alias_expand($pendport) <> "") {
800
                        /* Item is a port alias */
801
                } else {
802
                        $pbeginport = "any";
803
                        $pendport = "any";
804
                }
805
        }
806
}
807

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

    
810
        $adr = array();
811

    
812
        if ($padr == "any")
813
                $adr['any'] = true;
814
        else if (is_specialnet($padr))
815
                $adr['network'] = $padr;
816
        else {
817
                $adr['address'] = $padr;
818
                if ($pmask != 32)
819
                        $adr['address'] .= "/" . $pmask;
820
        }
821

    
822
        if ($pnot)
823
                $adr['not'] = true;
824
        else
825
                unset($adr['not']);
826

    
827
        if (($pbeginport != 0) && ($pbeginport != "any")) {
828
                if ($pbeginport != $pendport)
829
                        $adr['port'] = $pbeginport . "-" . $pendport;
830
                else
831
                        $adr['port'] = $pbeginport;
832
        }
833

    
834
        if(alias_expand($pbeginport)) {
835
                $adr['port'] = $pbeginport;
836
        }
837
}
838

    
839
function is_specialnet($net) {
840
        global $specialsrcdst;
841
		if(!$net) 
842
			return false;
843
        if (in_array($net, $specialsrcdst) || strstr($net, "opt"))
844
                return true;
845
        else
846
                return false;
847
}
848

    
849
function ipsec_ca_sort() {
850
        global $g, $config;
851

    
852
        function ipseccacmp($a, $b) {
853
                return strcmp($a['ident'], $b['ident']);
854
        }
855

    
856
        usort($config['ipsec']['cacert'], "ipseccacmp");
857
}
858

    
859
//function to create widget tabs when called
860
function display_widget_tabs(& $tab_array) {	
861
	echo "<div id='tabs'>";
862
	$tabscounter = 0;
863
	foreach ($tab_array as $ta) {
864
	$dashpos = strpos($ta[2],'-');
865
	$tabname = $ta[2] . "-tab";
866
	$tabclass = substr($ta[2],0,$dashpos);
867
	$tabclass = $tabclass . "-class";
868
		if ($ta[1] == true) {
869
			$tabActive = "table-cell";
870
			$tabNonActive = "none";
871
		} 
872
		else {
873
			$tabActive = "none";
874
			$tabNonActive = "table-cell";
875
		}
876
		echo "<div id='{$ta[2]}-active' class='{$tabclass}-tabactive' style='display:{$tabActive}; background-color:#EEEEEE; color:black;'>";
877
		echo "<B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
878
		echo "&nbsp;&nbsp;&nbsp;</B>";
879
		echo "</div>";
880
		
881
		echo "<div id='{$ta[2]}-deactive' class='{$tabclass}-tabdeactive' style='display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;' onClick=\"return changeTabDIV('{$ta[2]}')\">";
882
		echo "<B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
883
		echo "&nbsp;&nbsp;&nbsp;</B>";
884
		echo "</div>";
885
	}
886
	
887
	echo "<script type=\"text/javascript\">";
888
	echo "NiftyCheck();\n";
889
	echo "Rounded(\"div.{$tabclass}-tabactive\",\"top\",\"#CCCCCC\",\"#EEEEEE\",\"smooth\");\n";
890
	echo "Rounded(\"div.{$tabclass}-tabdeactive\",\"top\",\"#CCCCCC\",\"#777777\",\"smooth\");\n";
891
	echo "</script>";
892
	echo "</div>";
893
}
894

    
895

    
896
// Return inline javascript file or CSS to minimizie 
897
// request count going back to server.
898
function outputJavaScriptFileInline($javascript) {
899
	if(file_exists($javascript)) {
900
		echo "\n<script type=\"text/javascript\">\n";
901
		include($javascript);
902
		echo "\n</script>\n";
903
	} else {
904
		echo "\n\n<!-- Could not location file:  {$javascript} -->\n\n";
905
	}
906
}
907

    
908

    
909

    
910
function outputCSSPrintFileInline($css) {
911
	if(file_exists($css)) {
912
		echo "\n<style media=\"print\" type=\"text/css\">\n";
913
		include($css);
914
		echo "\n</style>\n";
915
	} else {
916
		echo "\n\n<!-- Could not location file:  {$css} -->\n\n";
917
	}
918
}
919

    
920

    
921
function outputCSSFileInline($css) {
922
	if(file_exists($css)) {
923
		echo "\n<style type=\"text/css\">\n";
924
		include($css);
925
		echo "\n</style>\n";
926
	} else {
927
		echo "\n\n<!-- Could not location file:  {$css} -->\n\n";
928
	}
929
}
930

    
931
$rfc2616 = array(
932
	100 => "100 Continue",
933
	101 => "101 Switching Protocols",
934
	200 => "200 OK",
935
	201 => "201 Created",
936
	202 => "202 Accepted",
937
	203 => "203 Non-Authoritative Information",
938
	204 => "204 No Content",
939
	205 => "205 Reset Content",
940
	206 => "206 Partial Content",
941
	300 => "300 Multiple Choices",
942
	301 => "301 Moved Permanently",
943
	302 => "302 Found",
944
	303 => "303 See Other",
945
	304 => "304 Not Modified",
946
	305 => "305 Use Proxy",
947
	306 => "306 (Unused)",
948
	307 => "307 Temporary Redirect",
949
	400 => "400 Bad Request",
950
	401 => "401 Unauthorized",
951
	402 => "402 Payment Required",
952
	403 => "403 Forbidden",
953
	404 => "404 Not Found",
954
	405 => "405 Method Not Allowed",
955
	406 => "406 Not Acceptable",
956
	407 => "407 Proxy Authentication Required",
957
	408 => "408 Request Timeout",
958
	409 => "409 Conflict",
959
	410 => "410 Gone",
960
	411 => "411 Length Required",
961
	412 => "412 Precondition Failed",
962
	413 => "413 Request Entity Too Large",
963
	414 => "414 Request-URI Too Long",
964
	415 => "415 Unsupported Media Type",
965
	416 => "416 Requested Range Not Satisfiable",
966
	417 => "417 Expectation Failed",
967
	500 => "500 Internal Server Error",
968
	501 => "501 Not Implemented",
969
	502 => "502 Bad Gateway",
970
	503 => "503 Service Unavailable",
971
	504 => "504 Gateway Timeout",
972
	505 => "505 HTTP Version Not Supported"
973
);
974

    
975
function is_rfc2616_code($code) {
976
	global $rfc2616;
977
	if (isset($rfc2616[$code]))
978
		return true;
979
	else
980
		return false;
981
}
982

    
983
function print_rfc2616_select($tag, $current){
984
	global $rfc2616;
985

    
986
	/* Default to 200 OK if not set */
987
	if ($current == "")
988
		$current = 200;
989

    
990
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";	
991
	foreach($rfc2616 as $code => $message) {
992
		if ($code == $current) {
993
			$sel = " selected";
994
		} else {
995
			$sel = "";
996
		}
997
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
998
	}
999
}
1000

    
1001
// Useful debugging function, much cleaner than print_r
1002
function echo_array($array,$return_me=false){
1003
    if(is_array($array) == false){
1004
        $return = "The provided variable is not an array.";
1005
    }else{
1006
        foreach($array as $name=>$value){
1007
            if(is_array($value)){
1008
                $return .= "";
1009
                $return .= "['<b>$name</b>'] {<div style='margin-left:10px;'>\n";
1010
                $return .= echo_array($value,true);
1011
                $return .= "</div>}";
1012
                $return .= "\n\n";
1013
            }else{
1014
                if(is_string($value)){
1015
                    $value = "\"$value\"";
1016
                }
1017
                $return .= "['<b>$name</b>'] = $value\n\n";
1018
            }
1019
        }
1020
    }
1021
    if($return_me == true){
1022
        return $return;
1023
    }else{
1024
        echo "<pre>".$return."</pre>";
1025
    }
1026
}
1027

    
1028
function add_package_tabs($tabgroup, & $tab_array) {
1029
	global $config, $g;
1030

    
1031
	if(!is_array($config['installedpackages']))
1032
		return;
1033
	if(!is_array($config['installedpackages']['tab']))
1034
		return;
1035

    
1036
	foreach($config['installedpackages']['tab'] as $tab) {
1037
		if ($tab['group'] !== $group)
1038
			continue;
1039
		$tab_entry = array();
1040
		if($tab['name']) {
1041
			$tab_entry[] = $tab['name'];
1042
			$tab_entry[] = false;
1043
			$tab_entry[] = $tab['url'];
1044
			$tab_array[] = $tab_entry;
1045
		}
1046
	}
1047
}
1048

    
1049
?>
(68-68/212)