Project

General

Profile

Download (32.5 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
		$timeout_minutes = isset($config['system']['webgui']['session_timeout']) ?  $config['system']['webgui']['session_timeout'] : 240;
41
		csrf_conf('expires', $timeout_minutes * 60);
42
	}
43
	require_once("csrf/csrf-magic.php");
44
}
45

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

    
55
Header("X-Frame-Options: SAMEORIGIN");
56
require_once("authgui.inc");
57

    
58
/* parse the configuration and include all configuration functions */
59
require_once("functions.inc");
60

    
61
/* Pull in all the gui related display classes) */
62
foreach (scandir("/usr/local/www/classes/") as $file) {
63
	if (substr($file, -4) == ".inc") {
64
		require_once("classes/{$file}");
65
	}
66
}
67

    
68
$g['theme'] = get_current_theme();
69

    
70
/* Set the default interface language */
71
if($config['system']['language'] <> "") {
72
	$g['language'] = $config['system']['language'];
73
} elseif ($g['language'] == "") {
74
	$g['language'] = 'en_US';
75
}
76

    
77
set_language($g['language']);
78

    
79
/* used by progress bar */
80
$lastseen = "-1";
81

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

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

    
98
$firewall_rules_dscp_types = array("af11",
99
				"af12",
100
				"af13",
101
				"af21",
102
				"af22",
103
				"af23",
104
				"af31",
105
				"af32",
106
				"af33",
107
				"af41",
108
				"af42",
109
				"af43",
110
				"EF",
111
				"cs1",
112
				"cs2",
113
				"cs3",
114
				"cs4",
115
				"cs5",
116
				"cs6",
117
				"cs7",
118
				"0x01 (reliability, ToS 0x04)",
119
				"0x02 (throughput, ToS 0x08)",
120
				"0x04 (lowdelay, ToS 0x10)");
121

    
122
$auth_server_types = array(
123
	'ldap' => "LDAP",
124
	'radius' => "Radius");
125

    
126
$ldap_urltypes = array(
127
	'TCP - Standard' => 389,
128
	'SSL - Encrypted' => 636);
129

    
130
$ldap_scopes = array(
131
	'one' => "One Level",
132
	'subtree' => "Entire Subtree");
133

    
134
$ldap_protvers = array(
135
	2,
136
	3);
137

    
138
$ldap_templates = array(
139

    
140
	'open' => array(
141
				'desc' => "OpenLDAP",
142
				'attr_user' => "cn",
143
				'attr_group' => "cn",
144
				'attr_member' => "member"),
145

    
146
	'msad' => array(
147
				'desc' => "Microsoft AD",
148
				'attr_user' => "samAccountName",
149
				'attr_group' => "cn",
150
				'attr_member' => "memberOf"),
151

    
152
	'edir' => array(
153
				'desc' => "Novell eDirectory",
154
				'attr_user' => "cn",
155
				'attr_group' => "cn",
156
				'attr_member' => "uniqueMember"));
157

    
158
$radius_srvcs = array(
159
	'both' => "Authentication and Accounting",
160
	'auth' => "Authentication",
161
	'acct' => "Accounting");
162

    
163
$netbios_nodetypes = array(
164
	'0' => "none",
165
	'1' => "b-node",
166
	'2' => "p-node",
167
	'4' => "m-node",
168
	'5' => "h-node");
169

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

    
217
/* TCP flags */
218
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg", "ece", "cwr");
219

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

    
222
$spiflist = get_configured_interface_with_descr(false, true);
223
foreach ($spiflist as $ifgui => $ifdesc) {
224
	$specialnets[$ifgui] = $ifdesc . " net";
225
	$specialnets[$ifgui . 'ip'] = $ifdesc . " address";
226
}
227

    
228
$medias = array("auto" => "autoselect", "100full" => "100BASE-TX full-duplex",
229
	"100half" => "100BASE-TX half-duplex", "10full" => "10BASE-T full-duplex",
230
	"10half" => "10BASE-T half-duplex");
231

    
232
$wlan_modes = array("bss" => "Infrastructure (BSS)", "adhoc" => "Ad-hoc (IBSS)",
233
	"hostap" => "Access Point");
234

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

    
238
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, &$input_errors) {
239

    
240
	/* check for bad control characters */
241
	foreach ($postdata as $pn => $pd) {
242
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
243
			$input_errors[] = "The field '" . $pn . "' contains invalid characters.";
244
		}
245
	}
246

    
247
	for ($i = 0; $i < count($reqdfields); $i++) {
248
		if ($_POST[$reqdfields[$i]] == "" && $_REQUEST[$reqdfields[$i]] == "") {
249
			$input_errors[] = "The field '" . $reqdfieldsn[$i] . "' is required.";
250
		}
251
	}
252
}
253

    
254
function print_input_errors($input_errors) {
255
	global $g;
256

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

    
272
	print <<<EOF2
273
				</ul>
274
		</td></tr>
275
	</table>
276
	</div>
277
	&nbsp;<br />
278
EOF2;
279

    
280
}
281

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

    
290
function print_info_box_np($msg, $name="apply",$value="", $showapply=false) {
291
	global $g, $nifty_redbox, $nifty_blackbox, $nifty_background;
292

    
293
	if(empty($value)) {
294
		$value = gettext("Apply changes");
295
	}
296

    
297
	// Set the Nifty background color if one is not set already (defaults to white)
298
	if($nifty_background == "")
299
		$nifty_background = "#FFF";
300

    
301
	if(stristr($msg, gettext("apply")) != false || stristr($msg, gettext("save")) != false || stristr($msg, gettext("create")) != false || $showapply) {
302
		$savebutton = "<td class=\"infoboxsave\">";
303
		$savebutton .= "<input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\" />";
304
		if($_POST['if'])
305
			$savebutton .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
306
		$savebutton.="</td>";
307
	}
308
	$nifty_redbox = "#990000";
309
	$nifty_blackbox = "#000000";
310

    
311
	$themename = $g['theme'];
312

    
313
	if(file_exists("/usr/local/www/themes/{$themename}/tabcontrols.php")) {
314
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/tabcontrols.php");
315
		eval($toeval);
316
	}
317

    
318
	if(file_exists("/usr/local/www/themes/{$themename}/infobox.php")) {
319
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/infobox.php");
320
		eval($toeval);
321
	}
322

    
323
	if(!$savebutton) {
324
		$savebutton = "<td class=\"infoboxsave\"><input value=\"Close\" type=\"button\" onclick=\"jQuery('#redboxtable').hide();\" /></td>";
325
	}
326

    
327
	echo <<<EOFnp
328
	<table class="infobox" id="redboxtable" summary="red box table">
329
		<tr>
330
			<td>
331
				<div class="infoboxnp" id="redbox">
332
					<table class="infoboxnptable2" summary="message">
333
						<tr>
334
							<td class="infoboxnptd">
335
								&nbsp;&nbsp;&nbsp;<img class="infoboxnpimg" src="/themes/{$g['theme']}/images/icons/icon_exclam.gif" alt="exclamation" />
336
							</td>
337
							<td class="infoboxnptd2">
338
								<b>{$msg}</b>
339
							</td>
340
							{$savebutton}
341
						</tr>
342
					</table>
343
				</div>
344
				<div>
345
					<p>&nbsp;</p>
346
				</div>
347
			</td>
348
		</tr>
349
	</table>
350
	<script type="text/javascript">
351
	//<![CDATA[
352
		NiftyCheck();
353
		Rounded("div#redbox","all","{$nifty_background}","{$nifty_redbox}","smooth");
354
		Rounded("td#blackbox","all","{$nifty_background}","{$nifty_blackbox}","smooth");
355
	//]]>
356
	</script>
357
EOFnp;
358

    
359
}
360

    
361
function print_info_box_np_undo($msg, $name="apply",$value="Apply changes", $undo) {
362
	global $g;
363

    
364
	if(stristr($msg, "apply") != false || stristr($msg, "save") != false || stristr($msg, "create") != false) {
365
		$savebutton = "<td class=\"infoboxsave nowrap\">";
366
		$savebutton .= "<input type=\"button\" value=\"Undo\" onclick=\"document.location='{$undo}'\" />";
367
		$savebutton .= "<input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\" />";
368
		$savebutton .= "</td>";
369
		if($_POST['if'])
370
			$savebutton .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
371
	}
372
	$nifty_redbox = "#990000";
373
	$nifty_blackbox = "#000000";
374

    
375
	$themename = $g['theme'];
376

    
377
	if(file_exists("/usr/local/www/themes/{$themename}/tabcontrols.php")) {
378
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/tabcontrols.php");
379
		eval($toeval);
380
	}
381

    
382
	if(file_exists("/usr/local/www/themes/{$themename}/infobox.php")) {
383
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/infobox.php");
384
		eval($toeval);
385
	}
386

    
387

    
388
	if(!$savebutton) {
389
		$savebutton = "<td class=\"infoboxsave\"><input value=\"Close\" type=\"button\" onclick=\"jQuery('#redboxtable').hide();\" /></td>";
390
	}
391

    
392
	echo <<<EOFnp
393
	<table class="infobox" id="redboxtable" summary="red box table">
394
		<tr>
395
			<td>
396
				<div class="infoboxnp" id="redbox">
397
					<table class="infoboxnptable2" summary="message">
398
						<tr>
399
							<td class="infoboxnptd">
400
								&nbsp;&nbsp;&nbsp;<img class="infoboxnpimg" src="/themes/{$g['theme']}/images/icons/icon_exclam.gif" alt="exclamation" />
401
							</td>
402
							<td class="infoboxnptd2">
403
								<b>{$msg}</b>
404
							</td>
405
							{$savebutton}
406
							{$undobutton}
407
						</tr>
408
					</table>
409
				</div>
410
				<div>
411
					<p>&nbsp;</p>
412
				</div>
413
			</td>
414
		</tr>
415
	</table>
416
	<script type="text/javascript">
417
	//<![CDATA[
418
		NiftyCheck();
419
		Rounded("div#redbox","all","#FFF","{$nifty_redbox}","smooth");
420
		Rounded("td#blackbox","all","#FFF","{$nifty_blackbox}","smooth");
421
	//]]>
422
	</script>
423
EOFnp;
424

    
425
}
426

    
427
function print_info_box($msg) {
428
	print_info_box_np($msg);
429
}
430

    
431
function get_std_save_message($ok) {
432
	global $d_sysrebootreqd_path;
433
	$filter_related = false;
434
	$filter_pages = array("nat", "filter");
435
	$to_return = "The changes have been applied successfully.";
436
	foreach($filter_pages as $fp)
437
		if(stristr($_SERVER['SCRIPT_FILENAME'], $fp))
438
			$filter_related = true;
439
	if($filter_related)
440
		$to_return .= "<br/>You can also <a href=\"status_filter_reload.php\">monitor</a> the filter reload progress.";
441
	return $to_return;
442
}
443

    
444
function pprint_address($adr) {
445
	global $specialnets;
446

    
447
	if (isset($adr['any'])) {
448
		$padr = "*";
449
	} else if ($adr['network']) {
450
		$padr = $specialnets[$adr['network']];
451
	} else {
452
		$padr = $adr['address'];
453
	}
454

    
455
	if (isset($adr['not']))
456
		$padr = "! " . $padr;
457

    
458
	return $padr;
459
}
460

    
461
function pprint_port($port) {
462
	global $wkports;
463

    
464
	$pport = "";
465

    
466
	if (!$port)
467
		return "*";
468
	else {
469
		$srcport = explode("-", $port);
470
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
471
			$pport = $srcport[0];
472
			if ($wkports[$srcport[0]]) {
473
				$pport .= " (" . $wkports[$srcport[0]] . ")";
474
			}
475
		} else
476
			$pport .= $srcport[0] . " - " . $srcport[1];
477
	}
478

    
479
	return $pport;
480
}
481

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

    
521
	return $item_set;
522
}
523

    
524
function gentitle($title) {
525
	global $navlevelsep;
526
	if(!is_array($title))
527
		return $title;
528
	else
529
		return join($navlevelsep, $title);
530
}
531

    
532
function genhtmltitle($title) {
533
	global $config;
534
	return gentitle($title);
535
}
536

    
537
/* update the changedesc and changecount(er) variables */
538
function update_changedesc($update) {
539
	global $changedesc;
540
	global $changecount;
541

    
542
	$changedesc .= " {$update}";
543
	$changecount++;
544
}
545

    
546
function clear_log_file($logfile = "/var/log/system.log") {
547
	global $config, $g;
548
	exec("/usr/bin/killall syslogd");
549
	if(isset($config['system']['disablesyslogclog'])) {
550
		unlink($logfile);
551
		touch($logfile);
552
	} else {
553
		if(isset($config['system']['usefifolog']))
554
			exec("/usr/sbin/fifolog_create -s 511488 {$logfile}");
555
		else
556
			exec("/usr/sbin/clog -i -s 511488 {$logfile}");
557
	}
558
	system_syslogd_start();
559
}
560

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

    
599
			} else {
600
					echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
601
			}
602
			echo "</tr>\n";
603
	}
604
}
605

    
606
function return_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
607
	global $g, $config;
608
	$sor = (isset($config['syslog']['reverse']) || $grepreverse) ? "-r" : "";
609
	$logarr = "";
610
	$grepline = "  ";
611
	if(is_array($grepfor))
612
		foreach($grepfor as $agrep)
613
			$grepline .= " | grep \"$agrep\"";
614
	if(is_array($grepinvert))
615
		foreach($grepinvert as $agrep)
616
			$grepline .= " | grep -v \"$agrep\"";
617
	if($config['system']['disablesyslogclog']) {
618
		exec("cat {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
619
	} else {
620
		if(isset($config['system']['usefifolog'])) {
621
			exec("/usr/sbin/fifolog_reader {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
622
		} else {
623
			exec("/usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
624
		}
625
	}
626
	return($logarr);
627
}
628

    
629
/* Check if variable has changed, update and log if it has
630
 * returns true if var changed
631
 * varname = variable name in plain text
632
 * orig = original value
633
 * new = new value
634
 */
635
function update_if_changed($varname, & $orig, $new) {
636
	if (is_array($orig) && is_array($new)) {
637
		$a_diff = array_diff($orig, $new);
638
		foreach ($a_diff as $diff) {
639
			update_changedesc("removed {$varname}: \"{$diff}\"");
640
		}
641
		$a_diff = array_diff($new, $orig);
642
		foreach ($a_diff as $diff) {
643
			update_changedesc("added {$varname}: \"{$diff}\"");
644
		}
645
		$orig = $new;
646
		return true;
647

    
648
	} else {
649
		if ($orig != $new) {
650
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
651
			$orig = $new;
652
			return true;
653
		}
654
	}
655
	return false;
656
}
657

    
658
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
659
	if (isset($adr['any']))
660
		$padr = "any";
661
	else if ($adr['network'])
662
		$padr = $adr['network'];
663
	else if ($adr['address']) {
664
		list($padr, $pmask) = explode("/", $adr['address']);
665
		if (!$pmask) {
666
			if (is_ipaddrv6($padr))
667
				$pmask = 128;
668
			else
669
				$pmask = 32;
670
		}
671
	}
672

    
673
	if (isset($adr['not']))
674
		$pnot = 1;
675
	else
676
		$pnot = 0;
677

    
678
	if ($adr['port']) {
679
		list($pbeginport, $pendport) = explode("-", $adr['port']);
680
		if (!$pendport)
681
			$pendport = $pbeginport;
682
	} else if (!is_alias($pbeginport) && !is_alias($pendport)) {
683
		$pbeginport = "any";
684
		$pendport = "any";
685
	}
686
}
687

    
688
function pconfig_to_address(&$adr, $padr, $pmask, $pnot=false, $pbeginport=0, $pendport=0) {
689
	$adr = array();
690

    
691
	if ($padr == "any")
692
		$adr['any'] = true;
693
	else if (is_specialnet($padr))
694
		$adr['network'] = $padr;
695
	else {
696
		$adr['address'] = $padr;
697
		if (is_ipaddrv6($padr)) {
698
			if ($pmask != 128)
699
				$adr['address'] .= "/" . $pmask;
700
		} else {
701
			if ($pmask != 32)
702
				$adr['address'] .= "/" . $pmask;
703
		}
704
	}
705

    
706
	if ($pnot)
707
		$adr['not'] = true;
708
	else
709
		unset($adr['not']);
710

    
711
	if (($pbeginport != 0) && ($pbeginport != "any")) {
712
		if ($pbeginport != $pendport)
713
			$adr['port'] = $pbeginport . "-" . $pendport;
714
		else
715
			$adr['port'] = $pbeginport;
716
	}
717

    
718
	if(is_alias($pbeginport)) {
719
		$adr['port'] = $pbeginport;
720
	}
721
}
722

    
723
function is_specialnet($net) {
724
	global $specialsrcdst;
725

    
726
	if(!$net)
727
		return false;
728
	if (in_array($net, $specialsrcdst))
729
		return true;
730
	else
731
		return false;
732
}
733

    
734
//function to create widget tabs when called
735
function display_widget_tabs(& $tab_array) {
736
	echo "<div id=\"tabs\">";
737
	$tabscounter = 0;
738
	foreach ($tab_array as $ta) {
739
	$dashpos = strpos($ta[2],'-');
740
	$tabname = $ta[2] . "-tab";
741
	$tabclass = substr($ta[2],0,$dashpos);
742
	$tabclass = $tabclass . "-class";
743
		if ($ta[1] == true) {
744
			$tabActive = "table-cell";
745
			$tabNonActive = "none";
746
		}
747
		else {
748
			$tabActive = "none";
749
			$tabNonActive = "table-cell";
750
		}
751
		echo "<div id=\"{$ta[2]}-active\" class=\"{$tabclass}-tabactive\" style=\"display:{$tabActive}; background-color:#EEEEEE; color:black;\">";
752
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
753
		echo "&nbsp;&nbsp;&nbsp;</b>";
754
		echo "</div>";
755

    
756
		echo "<div id=\"{$ta[2]}-deactive\" class=\"{$tabclass}-tabdeactive\" style=\"display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;\" onclick=\"return changeTabDIV('{$ta[2]}')\">";
757
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
758
		echo "&nbsp;&nbsp;&nbsp;</b>";
759
		echo "</div>";
760
	}
761

    
762
	echo "<script type=\"text/javascript\">";
763
	echo "\n//<![CDATA[\n";
764
	echo "NiftyCheck();\n";
765
	echo "Rounded(\"div.{$tabclass}-tabactive\",\"top\",\"#CCCCCC\",\"#EEEEEE\",\"smooth\");\n";
766
	echo "Rounded(\"div.{$tabclass}-tabdeactive\",\"top\",\"#CCCCCC\",\"#777777\",\"smooth\");\n";
767
	echo "//]]>\n";
768
	echo "</script>";
769
	echo "</div>";
770
}
771

    
772

    
773
// Return inline javascript file or CSS to minimizie
774
// request count going back to server.
775
function outputJavaScriptFileInline($javascript) {
776
	if(file_exists($javascript)) {
777
		echo "\n<script type=\"text/javascript\">\n";
778
		include($javascript);
779
		echo "\n</script>\n";
780
	} else {
781
		echo "\n\n<!-- Could not location file:  {$javascript} -->\n\n";
782
	}
783
}
784

    
785

    
786

    
787
function outputCSSPrintFileInline($css) {
788
	if(file_exists($css)) {
789
		echo "\n<style media=\"print\" type=\"text/css\">\n";
790
		include($css);
791
		echo "\n</style>\n";
792
	} else {
793
		echo "\n\n<!-- Could not location file:  {$css} -->\n\n";
794
	}
795
}
796

    
797

    
798
function outputCSSFileInline($css) {
799
	if(file_exists($css)) {
800
		echo "\n<style type=\"text/css\">\n";
801
		include($css);
802
		echo "\n</style>\n";
803
	} else {
804
		echo "\n\n<!-- Could not location file:  {$css} -->\n\n";
805
	}
806
}
807

    
808
$rfc2616 = array(
809
	100 => "100 Continue",
810
	101 => "101 Switching Protocols",
811
	200 => "200 OK",
812
	201 => "201 Created",
813
	202 => "202 Accepted",
814
	203 => "203 Non-Authoritative Information",
815
	204 => "204 No Content",
816
	205 => "205 Reset Content",
817
	206 => "206 Partial Content",
818
	300 => "300 Multiple Choices",
819
	301 => "301 Moved Permanently",
820
	302 => "302 Found",
821
	303 => "303 See Other",
822
	304 => "304 Not Modified",
823
	305 => "305 Use Proxy",
824
	306 => "306 (Unused)",
825
	307 => "307 Temporary Redirect",
826
	400 => "400 Bad Request",
827
	401 => "401 Unauthorized",
828
	402 => "402 Payment Required",
829
	403 => "403 Forbidden",
830
	404 => "404 Not Found",
831
	405 => "405 Method Not Allowed",
832
	406 => "406 Not Acceptable",
833
	407 => "407 Proxy Authentication Required",
834
	408 => "408 Request Timeout",
835
	409 => "409 Conflict",
836
	410 => "410 Gone",
837
	411 => "411 Length Required",
838
	412 => "412 Precondition Failed",
839
	413 => "413 Request Entity Too Large",
840
	414 => "414 Request-URI Too Long",
841
	415 => "415 Unsupported Media Type",
842
	416 => "416 Requested Range Not Satisfiable",
843
	417 => "417 Expectation Failed",
844
	500 => "500 Internal Server Error",
845
	501 => "501 Not Implemented",
846
	502 => "502 Bad Gateway",
847
	503 => "503 Service Unavailable",
848
	504 => "504 Gateway Timeout",
849
	505 => "505 HTTP Version Not Supported"
850
);
851

    
852
function is_rfc2616_code($code) {
853
	global $rfc2616;
854
	if (isset($rfc2616[$code]))
855
		return true;
856
	else
857
		return false;
858
}
859

    
860
function print_rfc2616_select($tag, $current){
861
	global $rfc2616;
862

    
863
	/* Default to 200 OK if not set */
864
	if ($current == "")
865
		$current = 200;
866

    
867
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
868
	foreach($rfc2616 as $code => $message) {
869
		if ($code == $current) {
870
			$sel = " selected=\"selected\"";
871
		} else {
872
			$sel = "";
873
		}
874
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
875
	}
876
}
877

    
878
// Useful debugging function, much cleaner than print_r
879
function echo_array($array,$return_me=false){
880
	if(is_array($array) == false){
881
		$return = "The provided variable is not an array.";
882
	}else{
883
		foreach($array as $name=>$value){
884
			if(is_array($value)){
885
				$return .= "";
886
				$return .= "['<b>$name</b>'] {<div style=\"margin-left:10px;\">\n";
887
				$return .= echo_array($value,true);
888
				$return .= "</div>}";
889
				$return .= "\n\n";
890
			}else{
891
				if(is_string($value)){
892
					$value = "\"$value\"";
893
				}
894
				$return .= "['<b>$name</b>'] = $value\n\n";
895
			}
896
		}
897
	}
898
	if($return_me == true){
899
		return $return;
900
	}else{
901
		echo "<pre>".$return."</pre>";
902
	}
903
}
904

    
905
/****f* pfsense-utils/display_top_tabs
906
 * NAME
907
 *   display_top_tabs - display tabs with rounded edges
908
 * INPUTS
909
 *   $text      - array of tabs
910
 * RESULT
911
 *   null
912
 ******/
913
function display_top_tabs(& $tab_array, $no_drop_down = false) {
914
	global $HTTP_SERVER_VARS;
915
	global $config;
916
	global $g;
917
	global $tab_array_indent;
918
	global $tab_array_space;
919
	global $tab_array_char_limit;
920

    
921
	/*  does the user have access to this tab?
922
	 *  master user has access to everything.
923
	 *  if the user does not have access, simply
924
	 *  unset the tab item.
925
	 */
926

    
927
	/* empty string code */
928
	if ($tab_array_indent == '') {
929
		$tab_array_indent = 0;
930
	}
931

    
932
	if ($tab_array_space == '') {
933
		$tab_array_space = 1;
934
	}
935

    
936
	if ($tab_array_char_limit == '') {
937
		$tab_array_char_limit = 92;
938
	}
939

    
940
	foreach ($tab_array as $tab_id => $ta){
941
		if(!isAllowedPage($ta[2]))
942
			unset ($tab_array[$tab_id]);
943
	}
944

    
945
	$tab_active_bg   = "#EEEEEE";
946
	$tab_inactive_bg = "#777777";
947
	$nifty_tabs_corners = "#FFF";
948
	$font_color = "white";
949

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

    
958
	$tabcharcount = 0;
959
	foreach ($tab_array as $ta)
960
		$tabcharcount = $tabcharcount + strlen($ta[0]);
961

    
962
	if($no_drop_down == true) {
963
		$tabcharcount = 0;
964
		unset($tab_array_char_limit);
965
	}
966

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

    
1004
function add_package_tabs($tabgroup, & $tab_array) {
1005
	global $config, $g;
1006

    
1007
	if(!is_array($config['installedpackages']))
1008
		return;
1009
	if(!is_array($config['installedpackages']['tab']))
1010
		return;
1011

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

    
1025
function alias_info_popup($alias_id){
1026
	global $config;
1027
	$maxlength = 60;
1028
	$close_title="title='".gettext('move mouse out this alias to hide')."'";
1029
	if (is_array($config['aliases']['alias'][$alias_id])){
1030
		$alias_name=$config['aliases']['alias'][$alias_id];
1031
		$alias_objects_with_details = "<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\" summary=\"alias info popup\">";
1032
		if ($alias_name['url']) {
1033
			// TODO: Change it when pf supports tables with ports
1034
			if ($alias_name['type'] == "urltable") {
1035
				exec("/sbin/pfctl -t {$alias_name['name']} -T show | wc -l", $total_entries);
1036
				$counter=preg_replace("/\D/","",$total_entries[0]);
1037
				exec("/sbin/pfctl -t {$alias_name['name']} -T show | head -10002", $alias_addresses);
1038
			} else {
1039
				$urlfn = alias_expand_urltable($alias_name['name']);
1040
				$alias_addresses = explode("\n", file_get_contents($urlfn));
1041
				$counter = count($alias_addresses);
1042
			}
1043
			$alias_objects_with_details .= "<tr><td colspan=\"3\" $close_title class=\"vncell\">{$alias_name['url']}</td></tr>";
1044
			$x=0;
1045
			foreach ($alias_addresses as $alias_ports_address ) {
1046
				switch ($x) {
1047
				case 0:
1048
					$x++;
1049
					$alias_objects_with_details .= "<tr><td $close_title class=\"vncell\" width=\"33%\" style=\"background: #FFFFFF;color: #000000;\">{$alias_ports_address}</td>";
1050
					break;
1051
				case 1:
1052
					$x++;
1053
					$alias_objects_with_details .= "<td $close_title class=\"vncell\" width=\"33%\" style=\"background: #FFFFFF;color: #000000;\">{$alias_ports_address}</td>";
1054
					break;
1055
				default:
1056
					$x=0;
1057
					$alias_objects_with_details .= "<td  $close_title class=\"vncell\" width=\"33%\" style=\"background: #FFFFFF;color: #000000;\">{$alias_ports_address}</td><tr>";
1058
					break;
1059
				}
1060
			}
1061
			for ($y = $x; $y <= $x; $y++) {
1062
				$alias_objects_with_details .= "<td $close_title class=\"vncell\" width=\"33%\">&nbsp;</td>";
1063
			}
1064
			if ($x > 0) {
1065
				$alias_objects_with_details .= "</tr>";
1066
			}
1067
			if ($counter > 10002) {
1068
				$alias_objects_with_details .= "<tr><td colspan=\"3\"> listing only first 10k items</td><tr>";
1069
			}
1070
		}
1071
		else{
1072
			$alias_addresses = explode (" ", $alias_name['address']);
1073
			$alias_details = explode ("||", $alias_name['detail']);
1074
			$counter = 0;
1075
			foreach ($alias_addresses as $alias_ports_address) {
1076
				$alias_objects_with_details .= "<tr><td $close_title width=\"5%\" class=\"vncell\" style=\"background: #FFFFFF;color: #000000;\">{$alias_addresses[$counter]}</td>";
1077
				$alias_detail_default = strpos ($alias_details[$counter],"Entry added");
1078
				if ($alias_details[$counter] != "" && $alias_detail_default === False)
1079
					$alias_objects_with_details .="<td $close_title width=\"95%\" class=\"vncell\" style=\"background: #FFFFFF;color: #000000;\">{$alias_details[$counter]}</td>";
1080
				else
1081
					$alias_objects_with_details .="<td $close_title width=\"95%\" class=\"vncell\" style=\"background: #FFFFFF;color: #000000;\">&nbsp;</td>";
1082
				$alias_objects_with_details .= "</tr>";
1083
				$counter++;
1084
			}
1085
		}
1086
		$alias_objects_with_details .= "</table>";
1087
	}
1088
	$alias_descr_substr = $alias_name['descr'];
1089
	if ($strlength >= $maxlength)
1090
		$alias_descr_substr = substr($alias_descr_substr, 0, $maxlength) . "...";
1091
	$item_text = ($counter > 1 ? "items" : "item");
1092
	$alias_caption = "{$alias_descr_substr} - {$counter} {$item_text}<a href=\"/firewall_aliases_edit.php?id={$alias_id}\" title=\"".gettext('edit this alias')."\">&nbsp;&nbsp;edit </a>";
1093
	$strlength = strlen ($alias_caption);
1094
	print "<h1>{$alias_caption}</h1>" . $alias_objects_with_details;
1095
}
1096

    
1097
function rule_popup($src,$srcport,$dst,$dstport){
1098
	global $config,$g;
1099
	$aliases_array = array();
1100
	if ($config['aliases']['alias'] <> "" and is_array($config['aliases']['alias'])) {
1101
		$descriptions = array ();
1102
		foreach ($config['aliases']['alias'] as $alias_id=>$alias_name){
1103
			$loading_image="<a><img src=\'/themes/{$g['theme']}/images/misc/loader.gif\' alt=\'loader\' /> " .gettext("loading...")."</a>";
1104
			switch ($alias_name['type']){
1105
			case "port":
1106
				$width="250";
1107
				break;
1108
			case "urltable":
1109
				$width="500";
1110
				break;
1111
			default:
1112
				$width="350";
1113
				break;
1114
			}
1115
			$span_begin = "<span style=\"cursor: help;\" onmouseover=\"var response_html=domTT_activate(this, event, 'id','ttalias_{$alias_id}','content','{$loading_image}', 'trail', true, 'delay', 300, 'fade', 'both', 'fadeMax', 93, 'styleClass', 'niceTitle','type','velcro','width',{$width});alias_popup('{$alias_id}','{$g['theme']}','".gettext('loading...')."');\" onmouseout=\"this.style.color = ''; domTT_mouseout(this, event);\"><u>";
1116
			$span_end = "</u></span>";
1117
			if ($alias_name['name'] == $src) {
1118
				$descriptions['src'] = $span_begin;
1119
				$descriptions['src_end'] = $span_end;
1120
			}
1121
			if ($alias_name['name'] == $srcport) {
1122
				$descriptions['srcport'] = $span_begin;
1123
				$descriptions['srcport_end'] = $span_end;
1124
			}
1125
			if ($alias_name['name'] == $dst ) {
1126
				$descriptions['dst'] = $span_begin;
1127
				$descriptions['dst_end'] = $span_end;
1128
			}
1129
			if ($alias_name['name'] == $dstport) {
1130
				$descriptions['dstport'] = $span_begin;
1131
				$descriptions['dstport_end'] = $span_end;
1132
			}
1133
		}
1134
		return $descriptions;
1135
	}
1136
}
1137

    
1138
$timezone = $config['system']['timezone'];
1139
if (!$timezone)
1140
	$timezone = "Etc/UTC";
1141

    
1142
date_default_timezone_set($timezone);
1143

    
1144
?>
(86-86/246)