Project

General

Profile

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

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

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

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

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19

    
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31
/*
32
	pfSense_MODULE:	base
33
*/
34

    
35
/* Include authentication routines */
36
/* THIS MUST BE ABOVE ALL OTHER CODE */
37
if(!$nocsrf) {
38
	function csrf_startup() {
39
		csrf_conf('rewrite-js', '/csrf/csrf-magic.js');
40
	}
41
	require_once("csrf/csrf-magic.php");
42
}
43
require_once("authgui.inc");
44

    
45
/* make sure nothing is cached */
46
if (!$omit_nocacheheaders) {
47
	header("Expires: 0");
48
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
49
	header("Cache-Control: no-store, no-cache, must-revalidate");
50
	header("Cache-Control: post-check=0, pre-check=0", false);
51
	header("Pragma: no-cache");
52
}
53

    
54
/* parse the configuration and include all configuration functions */
55
require_once("functions.inc");
56

    
57
/* Pull in all the gui related display classes) */
58
foreach (scandir("/usr/local/www/classes/") as $file) {
59
	if (stristr($file, ".inc") !== false) {
60
		require_once("classes/{$file}");
61
	}
62
}
63
/*
64
 *   if user has selected a custom template, use it.
65
 *   otherwise default to pfsense template
66
 */
67
if($config['theme'] <> "")
68
        $g['theme'] = $config['theme'];
69
else
70
        $g['theme'] = "pfsense";
71

    
72
/*
73
 *  If this device is an apple ipod/iphone
74
 *  switch the theme to one that works with it.
75
 */
76
$apple_ua = array("iPhone","iPod", "iPad");
77
foreach($apple_ua as $useragent)
78
	if(strstr($_SERVER['HTTP_USER_AGENT'], $useragent))
79
		$g['theme'] = "pfsense";
80

    
81
/* used by progress bar */
82
$lastseen = "-1";
83

    
84
$navlevelsep = ": ";	/* navigation level separator string */
85
$mandfldhtml = "";		/* display this before mandatory input fields */
86
$mandfldhtmlspc = "";	/* same as above, but with spacing */
87

    
88
/* Some ajax scripts still need access to GUI */
89
if(!$ignorefirmwarelock) {
90
	if (is_subsystem_dirty('firmwarelock')) {
91
		if (!$d_isfwfile) {
92
			header("Location: system_firmware.php");
93
			exit;
94
		} else {
95
			return;
96
		}
97
	}
98
}
99

    
100
$firewall_rules_dscp_types = array("af11", 
101
				"af12", 
102
				"af13", 
103
				"af21", 
104
				"af22", 
105
				"af23", 
106
				"af31", 
107
				"af32", 
108
				"af33", 
109
				"af41", 
110
				"af42", 
111
				"af43", 
112
				"EF", 
113
				"1-64", 
114
				"0x10",
115
				"0x04-0xfc");
116

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

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

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

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

    
133
$ldap_templates = array(
134

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

    
141
	'msad' => array(
142
				'desc' => "Microsoft AD",
143
				'attr_user' => "samAccountName",
144
				'attr_group' => "cn",
145
				'attr_member' => "memberOf"),
146

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

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

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

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

    
211
/* TCP flags */
212
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg");
213

    
214
$specialnets = array("pptp" => "PPTP clients", "pppoe" => "PPPoE clients", "l2tp" => "L2TP clients");
215

    
216
$spiflist = get_configured_interface_with_descr(false, true);
217
foreach ($spiflist as $ifgui => $ifdesc) {
218
	$specialnets[$ifgui] = $ifdesc . " net";
219
	$specialnets[$ifgui . 'ip'] = $ifdesc . " address";
220
}
221

    
222
$medias = array("auto" => "autoselect", "100full" => "100BASE-TX full-duplex",
223
	"100half" => "100BASE-TX half-duplex", "10full" => "10BASE-T full-duplex",
224
	"10half" => "10BASE-T half-duplex");
225

    
226
$wlan_modes = array("bss" => "Infrastructure (BSS)", "adhoc" => "Ad-hoc (IBSS)",
227
	"hostap" => "Access Point");
228

    
229
/* platforms that support firmware updating */
230
$fwupplatforms = array('pfSense', 'net45xx', 'net48xx', 'generic-pc', 'embedded', 'wrap', 'nanobsd');
231

    
232
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
233

    
234
	/* check for bad control characters */
235
	foreach ($postdata as $pn => $pd) {
236
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
237
			$input_errors[] = "The field '" . $pn . "' contains invalid characters.";
238
		}
239
	}
240

    
241
	for ($i = 0; $i < count($reqdfields); $i++) {
242
		if ($_POST[$reqdfields[$i]] == "" && $_REQUEST[$reqdfields[$i]] == "") {
243
			$input_errors[] = "The field '" . $reqdfieldsn[$i] . "' is required.";
244
		}
245
	}
246
}
247

    
248
function print_input_errors($input_errors) {
249
	global $g;
250

    
251
	print <<<EOF
252
	<div id='inputerrorsdiv' name='inputerrorsdiv'>
253
	<p>
254
	<table border="0" cellspacing="0" cellpadding="4" width="100%">
255
	<tr>
256
		<td class="inputerrorsleft">
257
			<img src="/themes/{$g['theme']}/images/icons/icon_error.gif">
258
		</td>
259
		<td class="inputerrorsright">
260
			<span class="errmsg"><p>
261
				The following input errors were detected:
262
				<ul>
263
EOF;
264
		foreach ($input_errors as $ierr) {
265
			echo "<li>" . htmlspecialchars($ierr) . "</li>";
266
		}
267

    
268
	print <<<EOF2
269
				</ul>
270
			</span>
271
		</td></tr>
272
	</table>
273
	</div>
274
	</p>&nbsp;<br>
275
EOF2;
276
	
277
}
278

    
279
function verify_gzip_file($fname) {
280

    
281
    $returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
282
	if ($returnvar != 0)
283
		return 0;
284
	else
285
		return 1;
286
}
287

    
288
function print_info_box_np($msg, $name="apply",$value="Apply changes") {
289
	global $g, $nifty_redbox, $nifty_blackbox, $nifty_background;
290

    
291
	// Set the Nifty background color if one is not set already (defaults to white)
292
	if($nifty_background == "")
293
		$nifty_background = "#FFF";
294

    
295
	if(stristr($msg, "apply") != false || stristr($msg, "save") != false || stristr($msg, "create") != false) {
296
		$savebutton = "<td class='infoboxsave'>";
297
		$savebutton .= "<input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\">";
298
		if($_POST['if']) 
299
			$savebutton .= "<input type='hidden' name='if' value='{$_POST['if']}'>";
300
		$savebutton.="</td>";
301
	}
302
	$nifty_redbox = "#990000";
303
	$nifty_blackbox = "#000000";
304
	
305
	$themename = $g['theme'];
306
	
307
	if(file_exists("/usr/local/www/themes/{$themename}/tabcontrols.php")) {
308
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/tabcontrols.php");
309
		eval($toeval);
310
	}
311
	
312
	if(file_exists("/usr/local/www/themes/{$themename}/infobox.php")) {
313
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/infobox.php");
314
		eval($toeval);
315
	}	
316
		
317
	if(!$savebutton) {
318
		$savebutton = '<td class="infoboxsave"><input value="Close" type="button" onClick="$(\'redboxtable\').hide();"></td>';
319
	}
320

    
321
	echo <<<EOFnp
322
	<table class='infobox' id='redboxtable'>
323
		<tr>
324
			<td>
325
				<div class='infoboxnp' id='redbox'>
326
					<table class='infoboxnptable2'>
327
						<tr>
328
							<td class='infoboxnptd'>
329
								&nbsp;&nbsp;&nbsp;<img class='infoboxnpimg' src="/themes/{$g['theme']}/images/icons/icon_exclam.gif" >
330
							</td>
331
							<td class='infoboxnptd2'>
332
								<b>{$msg}</b>
333
							</td>
334
							{$savebutton}
335
						</tr>
336
					</table>
337
				</div>
338
				<div>
339
					<p/>
340
				</div>
341
			</td>
342
		</tr>
343
	</table>
344
	<script type="text/javascript">
345
		NiftyCheck();
346
		Rounded("div#redbox","all","{$nifty_background}","{$nifty_redbox}","smooth");
347
		Rounded("td#blackbox","all","{$nifty_background}","{$nifty_blackbox}","smooth");
348
	</script>
349
EOFnp;
350

    
351
}
352

    
353
function print_info_box_np_undo($msg, $name="apply",$value="Apply changes", $undo) {
354
	global $g;
355
	
356
	if(stristr($msg, "apply") != false || stristr($msg, "save") != false || stristr($msg, "create") != false) {
357
		$savebutton = "<td class='infoboxsave'><nobr>";
358
		$savebutton .= " <input type=\"button\" value=\"Undo\" onClick=\"document.location='{$undo}'\">";
359
		$savebutton .= " <input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\">";
360
		$savebutton.="</nobr></td>";
361
		if($_POST['if']) 
362
			$savebutton .= "<input type='hidden' name='if' value='{$_POST['if']}'>";
363
	}
364
	$nifty_redbox = "#990000";
365
	$nifty_blackbox = "#000000";
366
	
367
	$themename = $g['theme'];
368
	
369
	if(file_exists("/usr/local/www/themes/{$themename}/tabcontrols.php")) {
370
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/tabcontrols.php");
371
		eval($toeval);
372
	}
373
	
374
	if(file_exists("/usr/local/www/themes/{$themename}/infobox.php")) {
375
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/infobox.php");
376
		eval($toeval);
377
	}	
378
	
379
		
380
	if(!$savebutton) {
381
		$savebutton = '<td class="infoboxsave"><input value="Close" type="button" onClick="$(\'redboxtable\').hide();"></td>';
382
	}
383

    
384
	echo <<<EOFnp
385
	<table class='infobox' id='redboxtable'>
386
		<tr>
387
			<td>
388
				<div class='infoboxnp' id='redbox'>
389
					<table class='infoboxnptable2'>
390
						<tr>
391
							<td class='infoboxnptd'>
392
								&nbsp;&nbsp;&nbsp;<img class='infoboxnpimg' src="/themes/{$g['theme']}/images/icons/icon_exclam.gif" >
393
							</td>
394
							<td class='infoboxnptd2'>
395
								<b>{$msg}</b>
396
							</td>
397
							{$savebutton} 
398
							{$undobutton}
399
						</tr>
400
					</table>
401
				</div>
402
				<div>
403
					<p/>
404
				</div>
405
			</td>
406
		</tr>
407
	</table>
408
	<script type="text/javascript">
409
		NiftyCheck();
410
		Rounded("div#redbox","all","#FFF","{$nifty_redbox}","smooth");
411
		Rounded("td#blackbox","all","#FFF","{$nifty_blackbox}","smooth");
412
	</script>
413
EOFnp;
414

    
415
}
416

    
417
function print_info_box($msg) {
418
	print_info_box_np($msg);
419
}
420

    
421
function get_std_save_message($ok) {
422
	global $d_sysrebootreqd_path;
423
	$filter_related = false;
424
	$filter_pages = array("nat", "filter");
425
	$to_return = "The changes have been applied successfully.";
426
	foreach($filter_pages as $fp) 
427
		if(stristr($_SERVER['SCRIPT_FILENAME'], $fp))
428
			$filter_related = true;	
429
	if($filter_related)
430
		$to_return .= "<br/>You can also <a href='status_filter_reload.php'>monitor</a> the filter reload progress.";
431
	return $to_return;
432
}
433

    
434
function pprint_address($adr) {
435
	global $specialnets;
436

    
437
	if (isset($adr['any'])) {
438
		$padr = "*";
439
	} else if ($adr['network']) {
440
		$padr = $specialnets[$adr['network']];
441
	} else {
442
		$padr = $adr['address'];
443
	}
444

    
445
	if (isset($adr['not']))
446
		$padr = "! " . $padr;
447

    
448
	return $padr;
449
}
450

    
451
function pprint_port($port) {
452
	global $wkports;
453

    
454
	$pport = "";
455

    
456
	if (!$port)
457
		return "*";
458
	else {
459
		$srcport = explode("-", $port);
460
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
461
			$pport = $srcport[0];
462
			if ($wkports[$srcport[0]]) {
463
				$pport .= " (" . $wkports[$srcport[0]] . ")";
464
			}
465
		} else
466
			$pport .= $srcport[0] . " - " . $srcport[1];
467
	}
468

    
469
	return $pport;
470
}
471

    
472
function firewall_check_for_advanced_options(&$item) {
473
        $item_set = "";
474
        if($item['max'])
475
                $item_set .= "max {$item['max']} ";
476
        if($item['max-src-nodes'])
477
                $item_set .= "max-src-nodes {$item['max-src-nodes']} ";
478
        if($item['max-src-conn'])
479
                $item_set .= "max-src-conn {$item['max-src-conn']} ";
480
        if($item['max-src-states'])
481
                $item_set .= "max-src-states {$item['max-src-states']} ";
482
        if($item['statetype'] != "keep state" && $item['statetype'] != "")
483
                $item_set .= "statetype {$item['statetype']} ";
484
        if($item['statetimeout'])
485
                $item_set .= "statetimeout {$item['statetimeout']} ";
486
        if($item['nosync'])
487
                $item_set .= "nosync ";
488
        if($item['max-src-conn-rate'])
489
                $item_set .= "max-src-conn-rate {$item['max-src-conn-rate']} ";
490
        if($item['max-src-conn-rates'])
491
                $item_set .= "max-src-conn-rates {$item['max-src-conn-rates']} ";
492
        if($item['gateway'])
493
                $item_set .= "gateway {$item['gateway']} ";
494
        if($item['dnpipe'])
495
                $item_set .= "limiter {$item['dnpipe']} ";
496
        if($item['pdnpipe'])
497
                $item_set .= "limiter {$item['pdnpipe']} ";
498
        if($item['l7container'])
499
                $item_set .= "layer7 {$item['l7container']} ";
500
        if($item['tag'])
501
                $item_set .= "tag {$item['tag']} ";
502
        if($item['tagged'])
503
                $item_set .= "tagged {$item['tagged']} ";
504
        if(isset($item['allowopts']))
505
                $item_set .= "allowopts ";
506
        if(isset($item['disablereplyto']))
507
                $item_set .= "disable reply-to ";
508
        if($item['tcpflags_any'] || $item['tcpflags1'] || $item['tcpflags2'])
509
                $item_set .= "tcpflags set";
510

    
511
        return $item_set;
512
}
513

    
514
function gentitle($title) {
515
	global $navlevelsep;
516
	if(!is_array($title))
517
		return $title;
518
	else
519
		return join($navlevelsep, $title);
520
}
521

    
522
function genhtmltitle($title) {
523
        global $config;
524
        return gentitle($title);
525
}
526

    
527
/* update the changedesc and changecount(er) variables */
528
function update_changedesc($update) {
529
	global $changedesc;
530
	global $changecount;
531

    
532
	$changedesc .= " {$update}";
533
	$changecount++;
534
}
535

    
536
function clear_log_file($logfile = "/var/log/system.log") {
537
	global $config, $g;
538
	exec("/usr/bin/killall syslogd");
539
	if(isset($config['system']['disablesyslogclog'])) {
540
		unlink($logfile);
541
		touch($logfile);
542
	} else {
543
		if(isset($config['system']['usefifolog'])) 
544
			exec("/usr/sbin/fifolog_create -s 511488 {$logfile}");
545
		else
546
			exec("/usr/sbin/clog -i -s 511488 {$logfile}");
547
	}
548
	system_syslogd_start();	
549
}
550

    
551
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
552
	global $g, $config;
553
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
554
	$logarr = "";
555
	$grepline = "  ";
556
	if(is_array($grepfor))
557
		foreach($grepfor as $agrep)
558
			$grepline .= " | grep \"$agrep\"";
559
	if(is_array($grepinvert))
560
		foreach($grepinvert as $agrep)
561
			$grepline .= " | grep -v \"$agrep\"";
562
	if(file_exists($logfile) && filesize($logfile) == 0) {
563
		$logarr = array("Log file started.");
564
	} else {
565
		if($config['system']['disablesyslogclog']) {
566
			exec("cat {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
567
		} else {
568
			if(isset($config['system']['usefifolog']))
569
				exec("/usr/sbin/fifolog_reader {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
570
			else
571
				exec("/usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
572
		}
573
	}
574
	foreach ($logarr as $logent) {
575
			$logent = preg_split("/\s+/", $logent, 6);
576
			echo "<tr valign=\"top\">\n";
577
			if ($withorig) {
578
					if(isset($config['system']['usefifolog'])) {
579
						$entry_date_time = htmlspecialchars(date("F j, Y, g:i a","" . $logent[1] . ""));
580
						$entry_text = htmlspecialchars($logent[5]);
581
					} else {
582
						$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
583
						$entry_text = htmlspecialchars($logent[4] . " " . $logent[5]);
584
					}
585
					echo "<td class=\"listlr\" nowrap>{$entry_date_time}</td>\n";
586
					echo "<td class=\"listr\">{$entry_text}</td>\n";
587

    
588
			} else {
589
					echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
590
			}
591
			echo "</tr>\n";
592
	}
593
}
594

    
595
function return_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
596
	global $g, $config;
597
	$sor = (isset($config['syslog']['reverse']) || $grepreverse) ? "-r" : "";
598
	$logarr = "";
599
	$grepline = "  ";
600
	if(is_array($grepfor))
601
		foreach($grepfor as $agrep)
602
			$grepline .= " | grep \"$agrep\"";
603
	if(is_array($grepinvert))
604
		foreach($grepinvert as $agrep)
605
			$grepline .= " | grep -v \"$agrep\"";
606
	if($config['system']['disablesyslogclog']) {
607
		exec("cat {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
608
	} else {
609
		if(isset($config['system']['usefifolog'])) {
610
			exec("/usr/sbin/fifolog_reader {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);			
611
		} else {
612
			exec("/usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
613
		}
614
	}
615
	return($logarr);
616
}
617

    
618
/* Check if variable has changed, update and log if it has
619
 * returns true if var changed
620
 * varname = variable name in plain text
621
 * orig = original value
622
 * new = new value
623
 */
624
function update_if_changed($varname, & $orig, $new) {
625
	if (is_array($orig) && is_array($new)) {
626
		$a_diff = array_diff($orig, $new);
627
		foreach ($a_diff as $diff) {
628
			update_changedesc("removed {$varname}: \"{$diff}\"");
629
		}
630
		$a_diff = array_diff($new, $orig);
631
		foreach ($a_diff as $diff) {
632
			update_changedesc("added {$varname}: \"{$diff}\"");
633
		}
634
		$orig = $new;
635
		return true;
636

    
637
	} else {
638
		if ($orig != $new) {
639
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
640
			$orig = $new;
641
			return true;
642
		}
643
	}
644
	return false;
645
}
646

    
647
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
648

    
649
        if (isset($adr['any']))
650
                $padr = "any";
651
        else if ($adr['network'])
652
                $padr = $adr['network'];
653
        else if ($adr['address']) {
654
                list($padr, $pmask) = explode("/", $adr['address']);
655
                if (!$pmask)
656
                        $pmask = 32;
657
        }
658

    
659
        if (isset($adr['not']))
660
                $pnot = 1;
661
        else
662
                $pnot = 0;
663

    
664
        if ($adr['port']) {
665
                list($pbeginport, $pendport) = explode("-", $adr['port']);
666
                if (!$pendport)
667
                        $pendport = $pbeginport;
668
	} else if (!is_alias($pbeginport) && !is_alias($pendport)) {
669
		$pbeginport = "any";
670
		$pendport = "any";
671
        }
672
}
673

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

    
676
        $adr = array();
677

    
678
        if ($padr == "any")
679
                $adr['any'] = true;
680
        else if (is_specialnet($padr))
681
                $adr['network'] = $padr;
682
        else {
683
                $adr['address'] = $padr;
684
                if ($pmask != 32)
685
                        $adr['address'] .= "/" . $pmask;
686
        }
687

    
688
        if ($pnot)
689
                $adr['not'] = true;
690
        else
691
                unset($adr['not']);
692

    
693
        if (($pbeginport != 0) && ($pbeginport != "any")) {
694
                if ($pbeginport != $pendport)
695
                        $adr['port'] = $pbeginport . "-" . $pendport;
696
                else
697
                        $adr['port'] = $pbeginport;
698
        }
699

    
700
        if(is_alias($pbeginport)) {
701
                $adr['port'] = $pbeginport;
702
        }
703
}
704

    
705
function is_specialnet($net) {
706
        global $specialsrcdst;
707

    
708
	if(!$net) 
709
		return false;
710
        if (in_array($net, $specialsrcdst))
711
                return true;
712
        else
713
                return false;
714
}
715

    
716
//function to create widget tabs when called
717
function display_widget_tabs(& $tab_array) {	
718
	echo "<div id='tabs'>";
719
	$tabscounter = 0;
720
	foreach ($tab_array as $ta) {
721
	$dashpos = strpos($ta[2],'-');
722
	$tabname = $ta[2] . "-tab";
723
	$tabclass = substr($ta[2],0,$dashpos);
724
	$tabclass = $tabclass . "-class";
725
		if ($ta[1] == true) {
726
			$tabActive = "table-cell";
727
			$tabNonActive = "none";
728
		} 
729
		else {
730
			$tabActive = "none";
731
			$tabNonActive = "table-cell";
732
		}
733
		echo "<div id='{$ta[2]}-active' class='{$tabclass}-tabactive' style='display:{$tabActive}; background-color:#EEEEEE; color:black;'>";
734
		echo "<B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
735
		echo "&nbsp;&nbsp;&nbsp;</B>";
736
		echo "</div>";
737
		
738
		echo "<div id='{$ta[2]}-deactive' class='{$tabclass}-tabdeactive' style='display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;' onClick=\"return changeTabDIV('{$ta[2]}')\">";
739
		echo "<B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
740
		echo "&nbsp;&nbsp;&nbsp;</B>";
741
		echo "</div>";
742
	}
743
	
744
	echo "<script type=\"text/javascript\">";
745
	echo "NiftyCheck();\n";
746
	echo "Rounded(\"div.{$tabclass}-tabactive\",\"top\",\"#CCCCCC\",\"#EEEEEE\",\"smooth\");\n";
747
	echo "Rounded(\"div.{$tabclass}-tabdeactive\",\"top\",\"#CCCCCC\",\"#777777\",\"smooth\");\n";
748
	echo "</script>";
749
	echo "</div>";
750
}
751

    
752

    
753
// Return inline javascript file or CSS to minimizie 
754
// request count going back to server.
755
function outputJavaScriptFileInline($javascript) {
756
	if(file_exists($javascript)) {
757
		echo "\n<script type=\"text/javascript\">\n";
758
		include($javascript);
759
		echo "\n</script>\n";
760
	} else {
761
		echo "\n\n<!-- Could not location file:  {$javascript} -->\n\n";
762
	}
763
}
764

    
765

    
766

    
767
function outputCSSPrintFileInline($css) {
768
	if(file_exists($css)) {
769
		echo "\n<style media=\"print\" type=\"text/css\">\n";
770
		include($css);
771
		echo "\n</style>\n";
772
	} else {
773
		echo "\n\n<!-- Could not location file:  {$css} -->\n\n";
774
	}
775
}
776

    
777

    
778
function outputCSSFileInline($css) {
779
	if(file_exists($css)) {
780
		echo "\n<style type=\"text/css\">\n";
781
		include($css);
782
		echo "\n</style>\n";
783
	} else {
784
		echo "\n\n<!-- Could not location file:  {$css} -->\n\n";
785
	}
786
}
787

    
788
$rfc2616 = array(
789
	100 => "100 Continue",
790
	101 => "101 Switching Protocols",
791
	200 => "200 OK",
792
	201 => "201 Created",
793
	202 => "202 Accepted",
794
	203 => "203 Non-Authoritative Information",
795
	204 => "204 No Content",
796
	205 => "205 Reset Content",
797
	206 => "206 Partial Content",
798
	300 => "300 Multiple Choices",
799
	301 => "301 Moved Permanently",
800
	302 => "302 Found",
801
	303 => "303 See Other",
802
	304 => "304 Not Modified",
803
	305 => "305 Use Proxy",
804
	306 => "306 (Unused)",
805
	307 => "307 Temporary Redirect",
806
	400 => "400 Bad Request",
807
	401 => "401 Unauthorized",
808
	402 => "402 Payment Required",
809
	403 => "403 Forbidden",
810
	404 => "404 Not Found",
811
	405 => "405 Method Not Allowed",
812
	406 => "406 Not Acceptable",
813
	407 => "407 Proxy Authentication Required",
814
	408 => "408 Request Timeout",
815
	409 => "409 Conflict",
816
	410 => "410 Gone",
817
	411 => "411 Length Required",
818
	412 => "412 Precondition Failed",
819
	413 => "413 Request Entity Too Large",
820
	414 => "414 Request-URI Too Long",
821
	415 => "415 Unsupported Media Type",
822
	416 => "416 Requested Range Not Satisfiable",
823
	417 => "417 Expectation Failed",
824
	500 => "500 Internal Server Error",
825
	501 => "501 Not Implemented",
826
	502 => "502 Bad Gateway",
827
	503 => "503 Service Unavailable",
828
	504 => "504 Gateway Timeout",
829
	505 => "505 HTTP Version Not Supported"
830
);
831

    
832
function is_rfc2616_code($code) {
833
	global $rfc2616;
834
	if (isset($rfc2616[$code]))
835
		return true;
836
	else
837
		return false;
838
}
839

    
840
function print_rfc2616_select($tag, $current){
841
	global $rfc2616;
842

    
843
	/* Default to 200 OK if not set */
844
	if ($current == "")
845
		$current = 200;
846

    
847
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";	
848
	foreach($rfc2616 as $code => $message) {
849
		if ($code == $current) {
850
			$sel = " selected";
851
		} else {
852
			$sel = "";
853
		}
854
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
855
	}
856
}
857

    
858
// Useful debugging function, much cleaner than print_r
859
function echo_array($array,$return_me=false){
860
    if(is_array($array) == false){
861
        $return = "The provided variable is not an array.";
862
    }else{
863
        foreach($array as $name=>$value){
864
            if(is_array($value)){
865
                $return .= "";
866
                $return .= "['<b>$name</b>'] {<div style='margin-left:10px;'>\n";
867
                $return .= echo_array($value,true);
868
                $return .= "</div>}";
869
                $return .= "\n\n";
870
            }else{
871
                if(is_string($value)){
872
                    $value = "\"$value\"";
873
                }
874
                $return .= "['<b>$name</b>'] = $value\n\n";
875
            }
876
        }
877
    }
878
    if($return_me == true){
879
        return $return;
880
    }else{
881
        echo "<pre>".$return."</pre>";
882
    }
883
}
884

    
885
/****f* pfsense-utils/display_top_tabs
886
 * NAME
887
 *   display_top_tabs - display tabs with rounded edges
888
 * INPUTS
889
 *   $text      - array of tabs
890
 * RESULT
891
 *   null
892
 ******/
893
function display_top_tabs(& $tab_array, $no_drop_down = false) {
894
        global $HTTP_SERVER_VARS;
895
        global $config;
896
        global $g;
897
		global $tab_array_indent;
898
		global $tab_array_space;
899
		global $tab_array_char_limit;
900
		
901
        /*  does the user have access to this tab?
902
         *  master user has access to everything.
903
         *  if the user does not have access, simply
904
         *  unset the tab item.
905
         */
906
		 
907
		/* empty string code */ 
908
		if ($tab_array_indent == '') {
909
			$tab_array_indent = 0;
910
		}
911

    
912
		if ($tab_array_space == '') {
913
			$tab_array_space = 1;
914
		}
915

    
916
		if ($tab_array_char_limit == '') {
917
			$tab_array_char_limit = 82;
918
		}
919

    
920
        $tab_temp = array ();
921
        foreach ($tab_array as $ta)
922
                if(isAllowedPage($ta[2]))
923
                        $tab_temp[] = $ta;
924
        /*
925
                // FIXME :      if the checks are not good enough
926
                //                      in isAllowedPage, it needs to be
927
                //                      fixed instead of kludging here
928

    
929
                // TODO: humm what shall we do with pkg_edit.php and pkg.php?
930
                if ((strpos($link, "pkg.php")) !== false || (strpos($link, "pkg_edit.php")) !== false) {
931
                        $pos_equal = strpos($link, "=");
932
                        $pos_xmlsuffix = strpos($link, ".xml");
933
                        // do we match an absolute url including ?xml= foo
934
                        if(!isAllowedPage($link, $allowed))
935
                                $link = substr($link, $pos_equal +1, ($pos_xmlsuffix - $pos_equal +3));
936
                }
937
                // next check - what if the basename contains a query string?
938
                if ((strpos($link, "?")) !== false) {
939
                        $pos_qmark = strpos($link, "?");
940
                        $link = substr($link, 0, $pos_qmark);
941
                }
942
                $authorized_text = print_r($allowed, true);
943
                if(is_array($authorized))
944
                        if (in_array(basename($link), $authorized))
945
        */
946

    
947
        unset ($tab_array);
948
        $tab_array = & $tab_temp;
949

    
950
        $tab_active_bg   = "#EEEEEE";
951
        $tab_inactive_bg = "#777777";
952
        $nifty_tabs_corners = "#FFF";
953
        $font_color = "white";
954

    
955
        /* if tabcontrols.php exist for a theme, allow it to be overriden */
956
        $themename = $config['theme'];
957
        $filename = "/usr/local/www/themes/{$themename}/tabcontrols.php";
958
        if(file_exists($filename)) {
959
                $eval_code = file_get_contents($filename);
960
                eval($eval_code);
961
        }
962

    
963
        $tabcharcount = 0;
964
        foreach ($tab_array as $ta)
965
                $tabcharcount = $tabcharcount + strlen($ta[0]);
966

    
967
		if($no_drop_down == true) {
968
			$tabcharcount = 0;
969
			unset($tab_array_char_limit);
970
		}
971

    
972
        // If the character count of the tab names is > 670
973
        // then show a select item dropdown menubox.
974
         if($tabcharcount > $tab_array_char_limit) {
975
                echo "Currently viewing: ";
976
                echo "<select name='TabSelect' onchange='tabs_will_go(this)'>\n";
977
                foreach ($tab_array as $ta) {
978
                        if($ta[1]=="true")
979
                                $selected = " SELECTED";
980
                        else
981
                                $selected = "";
982
                        // Onclick in option will not work in some browser
983
                        // echo "<option onClick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
984
                        echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
985
                }
986
                echo "</select>\n<p/>";
987
                echo "<script type=\"text/javascript\">";
988
                echo " function tabs_will_go(obj){ document.location = obj.value; }";
989
                echo "</script>";
990
        }  else {
991
                echo "<div class=\"newtabmenu\" style=\"margin:{$tab_array_space}px {$tab_array_indent}px; width:775px;\">\n";
992
                echo "<!-- Tabbed bar code-->\n";
993
				echo "<ul class=\"newtabmenu\">\n";
994
                $tabscounter = 0;
995
                foreach ($tab_array as $ta) {
996
                        if ($ta[1] == true) {
997
								echo "  <li class=\"newtabmenu_active\"><a href=\"{$ta[2]}\"><span>{$ta[0]}</span></a></li>\n";
998
                        } else {
999
								echo "  <li><a href=\"{$ta[2]}\"><span>{$ta[0]}</span></a></li>\n";
1000
                        }
1001
                        $tabscounter++;
1002
                }
1003
                echo "</ul>\n</div>\n";
1004
        }
1005
}
1006

    
1007
function add_package_tabs($tabgroup, & $tab_array) {
1008
        global $config, $g;
1009

    
1010
        if(!is_array($config['installedpackages']))
1011
                return;
1012
        if(!is_array($config['installedpackages']['tab']))
1013
                return;
1014

    
1015
        foreach($config['installedpackages']['tab'] as $tab) {
1016
                if ($tab['group'] !== $group)
1017
                        continue;
1018
                $tab_entry = array();
1019
                if($tab['name']) {
1020
                        $tab_entry[] = $tab['name'];
1021
                        $tab_entry[] = false;
1022
                        $tab_entry[] = $tab['url'];
1023
                        $tab_array[] = $tab_entry;
1024
                }
1025
        }
1026
}
1027

    
1028
function rule_popup($src,$srcport,$dst,$dstport){
1029
	global $config;
1030
	$aliases_array = array();
1031
	if($config['aliases']['alias'] <> "" and is_array($config['aliases']['alias']))
1032
	{
1033
		$span_begin = "";
1034
                $alias_src_span_begin = "";
1035
                $alias_src_span_end = "";
1036
                $alias_src_port_span_begin = "";
1037
                $alias_src_port_span_end = "";
1038
                $alias_dst_span_begin = "";
1039
                $alias_dst_span_end = "";
1040
                $alias_dst_port_span_begin = "";
1041
                $alias_dst_port_span_end = "";
1042
                $alias_content_text = "";
1043
        	foreach($config['aliases']['alias'] as $alias_name)
1044
        	{
1045
                	$alias_addresses = explode (" ", $alias_name['address']);
1046
                	$alias_details = explode ("||", $alias_name['detail']);
1047
                	$alias_objects_with_details = "";
1048
                	$counter = 0;
1049
			if ($alias_name['url']) {
1050
				$alias_objects_with_details .= $alias_name['url'] . "<br/>";
1051
			}
1052
                	foreach($alias_addresses as $alias_ports_address)
1053
                	{
1054
                        	$alias_objects_with_details .= $alias_addresses[$counter];
1055
                        	$alias_detail_default = strpos ($alias_details[$counter],"Entry added");
1056
                        	if ($alias_details[$counter] != "" && $alias_detail_default === False){
1057
                                	$alias_objects_with_details .=" - " . $alias_details[$counter];
1058
                        	}
1059
                        	$alias_objects_with_details .= "<br>";
1060
                        	$counter++;
1061
                	}
1062
                	//max character length for caption field
1063
                	$maxlength = 60;
1064

    
1065
                	$alias_descr_substr = $alias_name['descr'];
1066
                	$alias_content_text = htmlspecialchars($alias_objects_with_details);
1067
                	$alias_caption = htmlspecialchars($alias_descr_substr . ":");
1068
                	$strlength = strlen ($alias_caption);
1069
                	if ($strlength >= $maxlength)
1070
                        	$alias_caption = substr($alias_caption, 0, $maxlength) . "...";
1071

    
1072
			$alias_caption_escaped = str_replace("'", "\'", $alias_caption);
1073
			$alias_content_escaped = str_replace("'", "\'", $alias_content_text);
1074
			$span_begin = "<span style=\"cursor: help;\" onmouseover=\"domTT_activate(this, event, 'content', '<h1>$alias_caption_escaped</h1><p>$alias_content_escaped</p>', 'trail', true, 'delay', 0, 'fade', 'both', 'fadeMax', 93, 'styleClass', 'niceTitle');\" onmouseout=\"this.style.color = ''; domTT_mouseout(this, event);\"><U>";
1075

    
1076
                	if ($alias_name['name'] == $src)
1077
                        	$alias_src_span_begin = $span_begin;
1078
                	if ($alias_name['name'] == $srcport)
1079
                        	$alias_src_port_span_begin = $span_begin;
1080
                	if ($alias_name['name'] == $dst)
1081
                        	$alias_dst_span_begin = $span_begin;
1082
                	if ($alias_name['name'] == $dstport)
1083
                        	$alias_dst_port_span_begin = $span_begin;
1084
        	}
1085
        	$descriptions = array ();
1086
        	$descriptions['src'] = $alias_src_span_begin;
1087
        	$descriptions['srcport'] = $alias_src_port_span_begin;
1088
        	$descriptions['dst'] = $alias_dst_span_begin;
1089
        	$descriptions['dstport'] = $alias_dst_port_span_begin;
1090

    
1091
        	return $descriptions;
1092
  	}
1093
}
1094

    
1095
?>
(77-77/226)