Project

General

Profile

Download (29.6 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
/* Reserved table names to avoid colision */
99
$reserved_table_names = array(
100
		"bogons",
101
		"bogonsv6",
102
		"negate_networks",
103
		"snort2c",
104
		"sshlockout",
105
		"tonatsubnets",
106
		"virusprot",
107
		"vpn_networks",
108
		"webConfiguratorlockout"
109
);
110

    
111
$firewall_rules_dscp_types = array("af11",
112
				"af12",
113
				"af13",
114
				"af21",
115
				"af22",
116
				"af23",
117
				"af31",
118
				"af32",
119
				"af33",
120
				"af41",
121
				"af42",
122
				"af43",
123
				"VA",
124
				"EF",
125
				"cs1",
126
				"cs2",
127
				"cs3",
128
				"cs4",
129
				"cs5",
130
				"cs6",
131
				"cs7",
132
				"0x01",
133
				"0x02",
134
				"0x04");
135

    
136
$auth_server_types = array(
137
	'ldap' => "LDAP",
138
	'radius' => "Radius");
139

    
140
$ldap_urltypes = array(
141
	'TCP - Standard' => 389,
142
	'SSL - Encrypted' => 636);
143

    
144
$ldap_scopes = array(
145
	'one' => "One Level",
146
	'subtree' => "Entire Subtree");
147

    
148
$ldap_protvers = array(
149
	2,
150
	3);
151

    
152
$ldap_templates = array(
153

    
154
	'open' => array(
155
				'desc' => "OpenLDAP",
156
				'attr_user' => "cn",
157
				'attr_group' => "cn",
158
				'attr_member' => "member"),
159

    
160
	'msad' => array(
161
				'desc' => "Microsoft AD",
162
				'attr_user' => "samAccountName",
163
				'attr_group' => "cn",
164
				'attr_member' => "memberOf"),
165

    
166
	'edir' => array(
167
				'desc' => "Novell eDirectory",
168
				'attr_user' => "cn",
169
				'attr_group' => "cn",
170
				'attr_member' => "uniqueMember"));
171

    
172
$radius_srvcs = array(
173
	'both' => "Authentication and Accounting",
174
	'auth' => "Authentication",
175
	'acct' => "Accounting");
176

    
177
$netbios_nodetypes = array(
178
	'0' => "none",
179
	'1' => "b-node",
180
	'2' => "p-node",
181
	'4' => "m-node",
182
	'8' => "h-node");
183

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

    
231
/* TCP flags */
232
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg", "ece", "cwr");
233

    
234
$specialnets = array("(self)" => "This Firewall", "pptp" => "PPTP clients", "pppoe" => "PPPoE clients", "l2tp" => "L2TP clients");
235

    
236
$spiflist = get_configured_interface_with_descr(false, true);
237
foreach ($spiflist as $ifgui => $ifdesc) {
238
	$specialnets[$ifgui] = $ifdesc . " net";
239
	$specialnets[$ifgui . 'ip'] = $ifdesc . " address";
240
}
241

    
242
$medias = array("auto" => "autoselect", "100full" => "100BASE-TX full-duplex",
243
	"100half" => "100BASE-TX half-duplex", "10full" => "10BASE-T full-duplex",
244
	"10half" => "10BASE-T half-duplex");
245

    
246
$wlan_modes = array("bss" => "Infrastructure (BSS)", "adhoc" => "Ad-hoc (IBSS)",
247
	"hostap" => "Access Point");
248

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

    
252
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, &$input_errors) {
253

    
254
	/* check for bad control characters */
255
	foreach ($postdata as $pn => $pd) {
256
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
257
			$input_errors[] = sprintf(gettext("The field %s contains invalid characters."), $pn);
258
		}
259
	}
260

    
261
	for ($i = 0; $i < count($reqdfields); $i++) {
262
		if ($_POST[$reqdfields[$i]] == "" && $_REQUEST[$reqdfields[$i]] == "") {
263
			$input_errors[] = sprintf(gettext("The field %s is required."), $reqdfieldsn[$i]);
264
		}
265
	}
266
}
267

    
268
function print_input_errors($input_errors) {
269
	echo '<div class="alert alert-danger input-errors">';
270
	echo '<p>' . gettext('The following input errors were detected:') . '</p>';
271
	echo '<ul>';
272

    
273
	foreach ($input_errors as $ierr) {
274
		echo '<li>' . htmlspecialchars($ierr) . '</li>';
275
	}
276

    
277
	echo '</ul>';
278
	echo '</div>';
279
}
280

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

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

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

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

    
300
	if(stristr($msg, gettext("apply")) != false || stristr($msg, gettext("save")) != false || stristr($msg, gettext("create")) != false || $showapply) {
301
		$dismiss = "alert-dismissible";
302
		$button = '<button type="submit" class="btn btn-primary" name="login" value="'.$value.'">'.$name.'</button>';
303

    
304
		if($_POST['if'])
305
			$savebutton .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
306
	} else
307
		$button = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
308

    
309
	echo '<div class="alert alert-warning" role="alert">'.$msg.'</div>';
310
}
311

    
312
function print_info_box_np_undo($msg, $name="apply",$value="Apply changes", $undo) {
313
	global $g;
314

    
315
	if(stristr($msg, "apply") != false || stristr($msg, "save") != false || stristr($msg, "create") != false) {
316
		$savebutton = "<td class=\"infoboxsave nowrap\">";
317
		$savebutton .= "<input type=\"button\" value=\"". gettext("Undo") . "\" onclick=\"document.location='{$undo}'\" />";
318
		$savebutton .= "<input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\" />";
319
		$savebutton .= "</td>";
320
		if($_POST['if'])
321
			$savebutton .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
322
	}
323
	$nifty_redbox = "#990000";
324
	$nifty_blackbox = "#000000";
325

    
326
	$themename = $g['theme'];
327

    
328
	if(file_exists("/usr/local/www/themes/{$themename}/tabcontrols.php")) {
329
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/tabcontrols.php");
330
		eval($toeval);
331
	}
332

    
333
	if(file_exists("/usr/local/www/themes/{$themename}/infobox.php")) {
334
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/infobox.php");
335
		eval($toeval);
336
	}
337

    
338

    
339
	if(!$savebutton) {
340
		$savebutton = "<td class=\"infoboxsave\"><input value=\"" . gettext("Close") . "\" type=\"button\" onclick=\"jQuery(this).parents('table[id=redboxtable]').hide();\" /></td>";
341
	}
342

    
343
	echo <<<EOFnp
344
	<table class="infobox" id="redboxtable" summary="red box table">
345
		<tr>
346
			<td>
347
				<div class="infoboxnp" id="redbox">
348
					<table class="infoboxnptable2" summary="message">
349
						<tr>
350
							<td class="infoboxnptd">
351
								&nbsp;&nbsp;&nbsp;<img class="infoboxnpimg" src="/themes/{$g['theme']}/images/icons/icon_exclam.gif" alt="exclamation" />
352
							</td>
353
							<td class="infoboxnptd2">
354
								<b>{$msg}</b>
355
							</td>
356
							{$savebutton}
357
							{$undobutton}
358
						</tr>
359
					</table>
360
				</div>
361
				<div>
362
					<p>&nbsp;</p>
363
				</div>
364
			</td>
365
		</tr>
366
	</table>
367
	<script type="text/javascript">
368
	//<![CDATA[
369
		NiftyCheck();
370
		Rounded("div#redbox","all","#FFF","{$nifty_redbox}","smooth");
371
		Rounded("td#blackbox","all","#FFF","{$nifty_blackbox}","smooth");
372
	//]]>
373
	</script>
374
EOFnp;
375

    
376
}
377

    
378
function print_info_box($msg) {
379
	print_info_box_np($msg);
380
}
381

    
382
function get_std_save_message($ok) {
383
	global $d_sysrebootreqd_path;
384
	$filter_related = false;
385
	$filter_pages = array("nat", "filter");
386
	$to_return = gettext("The changes have been applied successfully.");
387
	foreach($filter_pages as $fp)
388
		if(stristr($_SERVER['SCRIPT_FILENAME'], $fp))
389
			$filter_related = true;
390
	if($filter_related)
391
		$to_return .= "<br />" . gettext("You can also <a href=\"status_filter_reload.php\">monitor</a> the filter reload progress.");
392
	return $to_return;
393
}
394

    
395
function pprint_address($adr) {
396
	global $specialnets;
397

    
398
	if (isset($adr['any'])) {
399
		$padr = "*";
400
	} else if ($adr['network']) {
401
		$padr = $specialnets[$adr['network']];
402
	} else {
403
		$padr = $adr['address'];
404
	}
405

    
406
	if (isset($adr['not']))
407
		$padr = "! " . $padr;
408

    
409
	return $padr;
410
}
411

    
412
function pprint_port($port) {
413
	global $wkports;
414

    
415
	$pport = "";
416

    
417
	if (!$port)
418
		return "*";
419
	else {
420
		$srcport = explode("-", $port);
421
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
422
			$pport = $srcport[0];
423
			if ($wkports[$srcport[0]]) {
424
				$pport .= " (" . $wkports[$srcport[0]] . ")";
425
			}
426
		} else
427
			$pport .= $srcport[0] . " - " . $srcport[1];
428
	}
429

    
430
	return $pport;
431
}
432

    
433
function firewall_check_for_advanced_options(&$item) {
434
	$item_set = "";
435
	if($item['os'])
436
		$item_set .= "os {$item['os']} ";
437
	if($item['dscp'])
438
		$item_set .= "dscp {$item['dscp']} ";
439
	if($item['max'])
440
		$item_set .= "max {$item['max']} ";
441
	if($item['max-src-nodes'])
442
		$item_set .= "max-src-nodes {$item['max-src-nodes']} ";
443
	if($item['max-src-conn'])
444
		$item_set .= "max-src-conn {$item['max-src-conn']} ";
445
	if($item['max-src-states'])
446
		$item_set .= "max-src-states {$item['max-src-states']} ";
447
	if(isset($item['nopfsync']))
448
		$item_set .= "nopfsync ";
449
	if($item['statetype'] != "keep state" && $item['statetype'] != "")
450
		$item_set .= "statetype {$item['statetype']} ";
451
	if($item['statetimeout'])
452
		$item_set .= "statetimeout {$item['statetimeout']} ";
453
	if(isset($item['nosync']))
454
		$item_set .= "no XMLRPC Sync ";
455
	if($item['max-src-conn-rate'])
456
		$item_set .= "max-src-conn-rate {$item['max-src-conn-rate']} ";
457
	if($item['max-src-conn-rates'])
458
		$item_set .= "max-src-conn-rates {$item['max-src-conn-rates']} ";
459
	if($item['vlanprio'])
460
		$item_set .= "vlanprio {$item['vlanprio']} ";
461
	if($item['vlanprioset'])
462
		$item_set .= "vlanprioset {$item['vlanprioset']} ";
463
	if($item['gateway'])
464
		$item_set .= "gateway {$item['gateway']} ";
465
	if($item['dnpipe'])
466
		$item_set .= "limiter {$item['dnpipe']} ";
467
	if($item['pdnpipe'])
468
		$item_set .= "limiter {$item['pdnpipe']} ";
469
	if($item['ackqueue'])
470
		$item_set .= "ackqueue {$item['ackqueue']} ";
471
	if($item['defaultqueue'])
472
		$item_set .= "defaultqueue {$item['defaultqueue']} ";
473
	if($item['l7container'])
474
		$item_set .= "layer7 {$item['l7container']} ";
475
	if($item['tag'])
476
		$item_set .= "tag {$item['tag']} ";
477
	if($item['tagged'])
478
		$item_set .= "tagged {$item['tagged']} ";
479
	if(isset($item['allowopts']))
480
		$item_set .= "allowopts ";
481
	if(isset($item['disablereplyto']))
482
		$item_set .= "disable reply-to ";
483
	if($item['tcpflags_any'] || $item['tcpflags1'] || $item['tcpflags2'])
484
		$item_set .= "tcpflags set";
485

    
486
	return $item_set;
487
}
488

    
489
function gentitle($title) {
490
	if(!is_array($title))
491
		return $title;
492
	else
493
		return join($navlevelsep, $title);
494
}
495

    
496
function genhtmltitle($title) {
497
	if(!is_array($title))
498
		return '<h1 class="page-header">' . $title . '</h1>';
499

    
500
	$heading = '<h1 class="page-header">' . end($title) . '</h1>';
501

    
502
	$bc = '<ol class="breadcrumb">';
503

    
504
	foreach ($title as $el)
505
		$bc .= '<li>'.$el.'</li>';
506

    
507
	$bc .= '</ol>';
508

    
509
	return $heading . $bc;
510
}
511

    
512
/* update the changedesc and changecount(er) variables */
513
function update_changedesc($update) {
514
	global $changedesc;
515
	global $changecount;
516

    
517
	$changedesc .= " {$update}";
518
	$changecount++;
519
}
520

    
521
function clear_log_file($logfile = "/var/log/system.log", $restart_syslogd = true) {
522
	global $config, $g;
523
	if ($restart_syslogd)
524
		exec("/usr/bin/killall syslogd");
525
	if(isset($config['system']['disablesyslogclog'])) {
526
		unlink($logfile);
527
		touch($logfile);
528
	} else {
529
		$log_size = isset($config['syslog']['logfilesize']) ? $config['syslog']['logfilesize'] : "511488";
530
		if(isset($config['system']['usefifolog']))
531
			exec("/usr/sbin/fifolog_create -s {$log_size} " . escapeshellarg($logfile));
532
		else
533
			exec("/usr/local/sbin/clog -i -s {$log_size} " . escapeshellarg($logfile));
534
	}
535
	if ($restart_syslogd)
536
		system_syslogd_start();
537
}
538

    
539
function clear_all_log_files() {
540
	global $g;
541
	exec("/usr/bin/killall syslogd");
542

    
543
	$log_files = array("system", "filter", "dhcpd", "vpn", "pptps", "poes", "l2tps", "openvpn", "portalauth", "ipsec", "ppp", "relayd", "wireless", "lighttpd", "ntpd", "gateways", "resolver", "routing");
544
	foreach ($log_files as $lfile) {
545
		clear_log_file("{$g['varlog_path']}/{$lfile}.log", false);
546
	}
547

    
548
	system_syslogd_start();
549
	killbyname("dhcpd");
550
	services_dhcpd_configure();
551
	return;
552
}
553

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

    
592
			} else {
593
					echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
594
			}
595
			echo "</tr>\n";
596
	}
597
}
598

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

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

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

    
649
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
650
	if (isset($adr['any']))
651
		$padr = "any";
652
	else if ($adr['network'])
653
		$padr = $adr['network'];
654
	else if ($adr['address']) {
655
		list($padr, $pmask) = explode("/", $adr['address']);
656
		if (!$pmask) {
657
			if (is_ipaddrv6($padr))
658
				$pmask = 128;
659
			else
660
				$pmask = 32;
661
		}
662
	}
663

    
664
	if (isset($adr['not']))
665
		$pnot = 1;
666
	else
667
		$pnot = 0;
668

    
669
	if ($adr['port']) {
670
		list($pbeginport, $pendport) = explode("-", $adr['port']);
671
		if (!$pendport)
672
			$pendport = $pbeginport;
673
	} else if (!is_alias($pbeginport) && !is_alias($pendport)) {
674
		$pbeginport = "any";
675
		$pendport = "any";
676
	}
677
}
678

    
679
function pconfig_to_address(&$adr, $padr, $pmask, $pnot=false, $pbeginport=0, $pendport=0) {
680
	$adr = array();
681

    
682
	if ($padr == "any")
683
		$adr['any'] = true;
684
	else if (is_specialnet($padr))
685
		$adr['network'] = $padr;
686
	else {
687
		$adr['address'] = $padr;
688
		if (is_ipaddrv6($padr)) {
689
			if ($pmask != 128)
690
				$adr['address'] .= "/" . $pmask;
691
		} else {
692
			if ($pmask != 32)
693
				$adr['address'] .= "/" . $pmask;
694
		}
695
	}
696

    
697
	if ($pnot)
698
		$adr['not'] = true;
699
	else
700
		unset($adr['not']);
701

    
702
	if (($pbeginport != 0) && ($pbeginport != "any")) {
703
		if ($pbeginport != $pendport)
704
			$adr['port'] = $pbeginport . "-" . $pendport;
705
		else
706
			$adr['port'] = $pbeginport;
707
	}
708

    
709
	if(is_alias($pbeginport)) {
710
		$adr['port'] = $pbeginport;
711
	}
712
}
713

    
714
function is_specialnet($net) {
715
	global $specialsrcdst;
716

    
717
	if(!$net)
718
		return false;
719
	if (in_array($net, $specialsrcdst))
720
		return true;
721
	else
722
		return false;
723
}
724

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

    
747
		echo "<div id=\"{$ta[2]}-deactive\" class=\"{$tabclass}-tabdeactive\" style=\"display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;\" onclick=\"return changeTabDIV('{$ta[2]}')\">";
748
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
749
		echo "&nbsp;&nbsp;&nbsp;</b>";
750
		echo "</div>";
751
	}
752

    
753
	echo "<script type=\"text/javascript\">";
754
	echo "\n//<![CDATA[\n";
755
	echo "NiftyCheck();\n";
756
	echo "Rounded(\"div.{$tabclass}-tabactive\",\"top\",\"#CCCCCC\",\"#EEEEEE\",\"smooth\");\n";
757
	echo "Rounded(\"div.{$tabclass}-tabdeactive\",\"top\",\"#CCCCCC\",\"#777777\",\"smooth\");\n";
758
	echo "//]]>\n";
759
	echo "</script>";
760
	echo "</div>";
761
}
762

    
763

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

    
776

    
777

    
778
function outputCSSPrintFileInline($css) {
779
	if(file_exists($css)) {
780
		echo "\n<style media=\"print\" 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

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

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

    
843
function is_rfc2616_code($code) {
844
	global $rfc2616;
845
	if (isset($rfc2616[$code]))
846
		return true;
847
	else
848
		return false;
849
}
850

    
851
function print_rfc2616_select($tag, $current){
852
	global $rfc2616;
853

    
854
	/* Default to 200 OK if not set */
855
	if ($current == "")
856
		$current = 200;
857

    
858
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
859
	foreach($rfc2616 as $code => $message) {
860
		if ($code == $current) {
861
			$sel = " selected=\"selected\"";
862
		} else {
863
			$sel = "";
864
		}
865
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
866
	}
867
	echo "</select>\n";
868
}
869

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

    
897
/****f* pfsense-utils/display_top_tabs
898
 * NAME
899
 *   display_top_tabs - display tabs with rounded edges
900
 * INPUTS
901
 *   $text	  - array of tabs
902
 * RESULT
903
 *   null
904
 ******/
905
function display_top_tabs(& $tab_array, $no_drop_down = false, $type = 'tabs') {
906
	global $config;
907
	global $g;
908
	global $tab_array_indent;
909
	global $tab_array_space;
910
	global $tab_array_char_limit;
911

    
912
	/*  does the user have access to this tab?
913
	 *  master user has access to everything.
914
	 *  if the user does not have access, simply
915
	 *  unset the tab item.
916
	 */
917

    
918
	/* empty string code */
919
	if ($tab_array_indent == '') {
920
		$tab_array_indent = 0;
921
	}
922

    
923
	if ($tab_array_space == '') {
924
		$tab_array_space = 1;
925
	}
926

    
927
	if ($tab_array_char_limit == '') {
928
		$tab_array_char_limit = 92;
929
	}
930

    
931
	foreach ($tab_array as $tab_id => $ta){
932
		if(!isAllowedPage($ta[2]))
933
			unset ($tab_array[$tab_id]);
934
	}
935

    
936
	$tab_active_bg   = "#EEEEEE";
937
	$tab_inactive_bg = "#777777";
938
	$nifty_tabs_corners = "#FFF";
939
	$font_color = "white";
940

    
941
	$tabcharcount = 0;
942
	foreach ($tab_array as $ta)
943
		$tabcharcount = $tabcharcount + strlen($ta[0]);
944

    
945
	if($no_drop_down == true) {
946
		$tabcharcount = 0;
947
		unset($tab_array_char_limit);
948
	}
949

    
950
	// If the character count of the tab names is > 670
951
	// then show a select item dropdown menubox.
952
	if($tabcharcount > $tab_array_char_limit): ?>
953
		echo gettext("Currently viewing: ");
954
		echo "<select name=\"TabSelect\" onchange=\"tabs_will_go(this)\">\n";
955
		foreach ($tab_array as $ta) {
956
			if($ta[1]=="true")
957
				$selected = " selected=\"selected\"";
958
			else
959
				$selected = "";
960
			// Onclick in option will not work in some browser
961
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
962
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
963
		}
964
		echo "</select>\n<p>&nbsp;</p>";
965
		echo "<script type=\"text/javascript\">";
966
		echo "\n//<![CDATA[\n";
967
		echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
968
		echo "//]]>\n";
969
		echo "</script>";
970
<? else: ?>
971
	<ul class="nav nav-<?= $type ?>">
972
		<?php foreach ($tab_array as $ta): ?>
973
			<li role="presentation"<?=($ta[1] ? ' class="active"' : '')?>><a href="<?=$ta[2]?>"><?=$ta[0]?></a></li>
974
		<?php endforeach; ?>
975
	</ul>
976
<?php endif;
977
}
978

    
979
function add_package_tabs($tabgroup, & $tab_array) {
980
	global $config, $g;
981

    
982
	if(!is_array($config['installedpackages']))
983
		return;
984
	if(!is_array($config['installedpackages']['tab']))
985
		return;
986

    
987
	foreach($config['installedpackages']['tab'] as $tab) {
988
		if ($tab['group'] !== $group)
989
			continue;
990
		$tab_entry = array();
991
		if($tab['name']) {
992
			$tab_entry[] = $tab['name'];
993
			$tab_entry[] = false;
994
			$tab_entry[] = $tab['url'];
995
			$tab_array[] = $tab_entry;
996
		}
997
	}
998
}
999

    
1000
function alias_info_popup($alias_id){
1001
	global $config;
1002

    
1003
	if (!is_array($config['aliases']['alias'][$alias_id]))
1004
		return;
1005

    
1006
	$maxlength = 60;
1007
	$alias = $config['aliases']['alias'][$alias_id];
1008
	$content = "";
1009

    
1010
	if ($alias['url'])
1011
	{
1012
		// TODO: Change it when pf supports tables with ports
1013
		if ($alias['type'] == "urltable") {
1014
			exec("/sbin/pfctl -t {$alias['name']} -T show | wc -l", $total_entries);
1015
			$counter=preg_replace("/\D/","",$total_entries[0]);
1016
			exec("/sbin/pfctl -t {$alias['name']} -T show | head -10002", $alias_addresses);
1017
		} else {
1018
			$urlfn = alias_expand_urltable($alias['name']);
1019
			$alias_addresses = explode("\n", file_get_contents($urlfn));
1020
			$counter = count($alias_addresses);
1021
		}
1022

    
1023
		$content .= '<h5>'. $alias['url'] .'</h5><ul><li>'. implode('</li><li>', $alias_ports_address) .'</li></ul>';
1024

    
1025
		if ($counter > 10002)
1026
			$content .= '<i>'. gettext("listing only first 10k items") .'</i>';
1027
	}
1028
	else
1029
	{
1030
		$alias_addresses = explode (" ", $alias['address']);
1031
		$alias_details = explode ("||", $alias['detail']);
1032

    
1033
		$content .= '<ul><li>'. implode('</li><li>', $alias_addresses) .'</li></ul>';
1034
	}
1035

    
1036
	if (strlen($alias['descr']) >= $maxlength)
1037
		$alias['descr'] = substr($alias['descr'], 0, $maxlength) . '&hellip;';
1038

    
1039
	return $content;
1040
}
1041

    
1042
function rule_columns_with_alias($src, $srcport, $dst, $dstport){
1043
	global $config;
1044

    
1045
	if ($config['aliases']['alias'] == "" || !is_array($config['aliases']['alias']))
1046
		return;
1047

    
1048
	$columns = array();
1049
	foreach ($config['aliases']['alias'] as $alias_id => $alias_name){
1050
		if ($alias_name['name'] == $src)
1051
			$columns['src'] = $alias_id;
1052
		elseif ($alias_name['name'] == $srcport)
1053
			$columns['srcport'] = $alias_id;
1054
		elseif ($alias_name['name'] == $dst )
1055
			$columns['dst'] = $alias_id;
1056
		elseif ($alias_name['name'] == $dstport)
1057
			$columns['dstport'] = $alias_id;
1058

    
1059
		return $columns;
1060
	}
1061
}
1062

    
1063
function form_output_row($name, $label, $content){
1064
var_dump($content);die;
1065
?>
1066
<div class="form-group">
1067
	<label for="<?=$name?>" class="col-sm-2 control-label"><?=gettext($label); ?></label>
1068
	<div class="col-sm-10">
1069
		<?=$content?>
1070
	</div>
1071
</div>
1072
<?php
1073
}
1074

    
1075
$timezone = $config['system']['timezone'];
1076
if (!$timezone)
1077
	$timezone = "Etc/UTC";
1078

    
1079
date_default_timezone_set($timezone);
1080

    
1081
?>
(87-87/252)