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
				"VA",
111
				"EF",
112
				"cs1",
113
				"cs2",
114
				"cs3",
115
				"cs4",
116
				"cs5",
117
				"cs6",
118
				"cs7",
119
				"0x01 (reliability, ToS 0x04)",
120
				"0x02 (throughput, ToS 0x08)",
121
				"0x04 (lowdelay, ToS 0x10)");
122

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

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

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

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

    
139
$ldap_templates = array(
140

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
281
}
282

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

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

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

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

    
302
	if(stristr($msg, gettext("apply")) != false || stristr($msg, gettext("save")) != false || stristr($msg, gettext("create")) != false || $showapply) {
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=\"" . htmlspecialchars($_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
	if(!$savebutton) {
325
		$savebutton = "<td class=\"infoboxsave\"><input value=\"Close\" type=\"button\" onclick=\"jQuery('#redboxtable').hide();\" /></td>";
326
	}
327

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

    
360
}
361

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

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

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

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

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

    
388

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

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

    
426
}
427

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

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

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

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

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

    
459
	return $padr;
460
}
461

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

    
465
	$pport = "";
466

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

    
480
	return $pport;
481
}
482

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

    
522
	return $item_set;
523
}
524

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

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

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

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

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

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

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

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

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

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

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

    
670
	if (isset($adr['not']))
671
		$pnot = 1;
672
	else
673
		$pnot = 0;
674

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

    
685
function pconfig_to_address(&$adr, $padr, $pmask, $pnot=false, $pbeginport=0, $pendport=0) {
686
	$adr = array();
687

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

    
703
	if ($pnot)
704
		$adr['not'] = true;
705
	else
706
		unset($adr['not']);
707

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

    
715
	if(is_alias($pbeginport)) {
716
		$adr['port'] = $pbeginport;
717
	}
718
}
719

    
720
function is_specialnet($net) {
721
	global $specialsrcdst;
722

    
723
	if(!$net)
724
		return false;
725
	if (in_array($net, $specialsrcdst))
726
		return true;
727
	else
728
		return false;
729
}
730

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

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

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

    
769

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

    
782

    
783

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

    
794

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

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

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

    
857
function print_rfc2616_select($tag, $current){
858
	global $rfc2616;
859

    
860
	/* Default to 200 OK if not set */
861
	if ($current == "")
862
		$current = 200;
863

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

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

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

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

    
924
	/* empty string code */
925
	if ($tab_array_indent == '') {
926
		$tab_array_indent = 0;
927
	}
928

    
929
	if ($tab_array_space == '') {
930
		$tab_array_space = 1;
931
	}
932

    
933
	if ($tab_array_char_limit == '') {
934
		$tab_array_char_limit = 92;
935
	}
936

    
937
	foreach ($tab_array as $tab_id => $ta){
938
		if(!isAllowedPage($ta[2]))
939
			unset ($tab_array[$tab_id]);
940
	}
941

    
942
	$tab_active_bg   = "#EEEEEE";
943
	$tab_inactive_bg = "#777777";
944
	$nifty_tabs_corners = "#FFF";
945
	$font_color = "white";
946

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

    
955
	$tabcharcount = 0;
956
	foreach ($tab_array as $ta)
957
		$tabcharcount = $tabcharcount + strlen($ta[0]);
958

    
959
	if($no_drop_down == true) {
960
		$tabcharcount = 0;
961
		unset($tab_array_char_limit);
962
	}
963

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

    
1001
function add_package_tabs($tabgroup, & $tab_array) {
1002
	global $config, $g;
1003

    
1004
	if(!is_array($config['installedpackages']))
1005
		return;
1006
	if(!is_array($config['installedpackages']['tab']))
1007
		return;
1008

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

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

    
1087
function rule_popup($src,$srcport,$dst,$dstport){
1088
	global $config,$g;
1089
	$aliases_array = array();
1090
	if ($config['aliases']['alias'] <> "" and is_array($config['aliases']['alias'])) {
1091
		$descriptions = array ();
1092
		foreach ($config['aliases']['alias'] as $alias_id=>$alias_name){
1093
			$loading_image="<a><img src=\'/themes/{$g['theme']}/images/misc/loader.gif\' alt=\'loader\' /> " .gettext("loading...")."</a>";
1094
			switch ($alias_name['type']){
1095
			case "port":
1096
				$width="250";
1097
				break;
1098
			case "urltable":
1099
				$width="500";
1100
				break;
1101
			default:
1102
				$width="350";
1103
				break;
1104
			}
1105
			$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>";
1106
			$span_end = "</u></span>";
1107
			if ($alias_name['name'] == $src) {
1108
				$descriptions['src'] = $span_begin;
1109
				$descriptions['src_end'] = $span_end;
1110
			}
1111
			if ($alias_name['name'] == $srcport) {
1112
				$descriptions['srcport'] = $span_begin;
1113
				$descriptions['srcport_end'] = $span_end;
1114
			}
1115
			if ($alias_name['name'] == $dst ) {
1116
				$descriptions['dst'] = $span_begin;
1117
				$descriptions['dst_end'] = $span_end;
1118
			}
1119
			if ($alias_name['name'] == $dstport) {
1120
				$descriptions['dstport'] = $span_begin;
1121
				$descriptions['dstport_end'] = $span_end;
1122
			}
1123
		}
1124
		return $descriptions;
1125
	}
1126
}
1127

    
1128
$timezone = $config['system']['timezone'];
1129
if (!$timezone)
1130
	$timezone = "Etc/UTC";
1131

    
1132
date_default_timezone_set($timezone);
1133

    
1134
?>
(86-86/246)