Project

General

Profile

Download (33.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	guiconfig.inc
4
*/
5
/* ====================================================================
6
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *
8
 *  Some or all of this file is based on the m0n0wall project which is
9
 *  Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
10
 *
11
 *	Redistribution and use in source and binary forms, with or without modification,
12
 *	are permitted provided that the following conditions are met:
13
 *
14
 *	1. Redistributions of source code must retain the above copyright notice,
15
 *		this list of conditions and the following disclaimer.
16
 *
17
 *	2. Redistributions in binary form must reproduce the above copyright
18
 *		notice, this list of conditions and the following disclaimer in
19
 *		the documentation and/or other materials provided with the
20
 *		distribution.
21
 *
22
 *	3. All advertising materials mentioning features or use of this software
23
 *		must display the following acknowledgment:
24
 *		"This product includes software developed by the pfSense Project
25
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
26
 *
27
 *	4. The names "pfSense" and "pfSense Project" must not be used to
28
 *		 endorse or promote products derived from this software without
29
 *		 prior written permission. For written permission, please contact
30
 *		 coreteam@pfsense.org.
31
 *
32
 *	5. Products derived from this software may not be called "pfSense"
33
 *		nor may "pfSense" appear in their names without prior written
34
 *		permission of the Electric Sheep Fencing, LLC.
35
 *
36
 *	6. Redistributions of any form whatsoever must retain the following
37
 *		acknowledgment:
38
 *
39
 *	"This product includes software developed by the pfSense Project
40
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
41
 *
42
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
43
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
46
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
51
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
53
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
54
 *
55
 *	====================================================================
56
 *
57
 */
58
/*
59
	pfSense_MODULE: base
60
*/
61

    
62
/* Include authentication routines */
63
/* THIS MUST BE ABOVE ALL OTHER CODE */
64
if (!$nocsrf) {
65
	function csrf_startup() {
66
		csrf_conf('rewrite-js', '/csrf/csrf-magic.js');
67
		$timeout_minutes = isset($config['system']['webgui']['session_timeout']) ? $config['system']['webgui']['session_timeout'] : 240;
68
		csrf_conf('expires', $timeout_minutes * 60);
69
	}
70
	require_once("csrf/csrf-magic.php");
71
}
72

    
73
/* make sure nothing is cached */
74
if (!$omit_nocacheheaders) {
75
	header("Expires: 0");
76
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
77
	header("Cache-Control: no-cache, no-store, must-revalidate");
78
	header("Pragma: no-cache");
79
}
80

    
81
header("X-Frame-Options: SAMEORIGIN");
82
require_once("authgui.inc");
83

    
84
/* parse the configuration and include all configuration functions */
85
require_once("functions.inc");
86

    
87
/* Pull in all the gui related display classes) */
88
foreach (scandir("/usr/local/www/classes/") as $file) {
89
	if (substr($file, -4) == ".inc") {
90
		require_once("classes/{$file}");
91
	}
92
}
93

    
94
/* Set the default interface language */
95
if ($config['system']['language'] <> "") {
96
	$g['language'] = $config['system']['language'];
97
} elseif ($g['language'] == "") {
98
	$g['language'] = 'en_US';
99
}
100

    
101
set_language($g['language']);
102

    
103
/* used by progress bar */
104
$lastseen = "-1";
105

    
106
$navlevelsep = ": ";	/* navigation level separator string */
107
$mandfldhtml = "";		/* display this before mandatory input fields */
108
$mandfldhtmlspc = "";	/* same as above, but with spacing */
109

    
110
/* Some ajax scripts still need access to GUI */
111
if (!$ignorefirmwarelock) {
112
	if (is_subsystem_dirty('firmwarelock')) {
113
		if (!$d_isfwfile) {
114
			header("Location: system_firmware.php");
115
			exit;
116
		} else {
117
			return;
118
		}
119
	}
120
}
121

    
122
/* Reserved table names to avoid collision */
123
$reserved_table_names = array(
124
	"bogons",
125
	"bogonsv6",
126
	"negate_networks",
127
	"snort2c",
128
	"sshlockout",
129
	"tonatsubnets",
130
	"virusprot",
131
	"vpn_networks",
132
	"webConfiguratorlockout"
133
);
134

    
135
$firewall_rules_dscp_types = array(
136
	"af11",
137
	"af12",
138
	"af13",
139
	"af21",
140
	"af22",
141
	"af23",
142
	"af31",
143
	"af32",
144
	"af33",
145
	"af41",
146
	"af42",
147
	"af43",
148
	"VA",
149
	"EF",
150
	"cs1",
151
	"cs2",
152
	"cs3",
153
	"cs4",
154
	"cs5",
155
	"cs6",
156
	"cs7",
157
	"0x01",
158
	"0x02",
159
	"0x04");
160

    
161
$auth_server_types = array(
162
	'ldap' => "LDAP",
163
	'radius' => "Radius");
164

    
165
$ldap_urltypes = array(
166
	'TCP - Standard' => 389,
167
	'SSL - Encrypted' => 636);
168

    
169
$ldap_scopes = array(
170
	'one' => "One Level",
171
	'subtree' => "Entire Subtree");
172

    
173
$ldap_protvers = array(
174
	2,
175
	3);
176

    
177
$ldap_templates = array(
178

    
179
	'open' => array(
180
		'desc' => "OpenLDAP",
181
		'attr_user' => "cn",
182
		'attr_group' => "cn",
183
		'attr_member' => "member"),
184

    
185
	'msad' => array(
186
		'desc' => "Microsoft AD",
187
		'attr_user' => "samAccountName",
188
		'attr_group' => "cn",
189
		'attr_member' => "memberOf"),
190

    
191
	'edir' => array(
192
		'desc' => "Novell eDirectory",
193
		'attr_user' => "cn",
194
		'attr_group' => "cn",
195
		'attr_member' => "uniqueMember"));
196

    
197
$radius_srvcs = array(
198
	'both' => "Authentication and Accounting",
199
	'auth' => "Authentication",
200
	'acct' => "Accounting");
201

    
202
$netbios_nodetypes = array(
203
	'0' => "none",
204
	'1' => "b-node",
205
	'2' => "p-node",
206
	'4' => "m-node",
207
	'8' => "h-node");
208

    
209
/* some well known ports */
210
$wkports = array(
211
	5999 => "CVSup",
212
	53 => "DNS",
213
	21 => "FTP",
214
	3000 => "HBCI",
215
	80 => "HTTP",
216
	443 => "HTTPS",
217
	5190 => "ICQ",
218
	113 => "IDENT/AUTH",
219
	143 => "IMAP",
220
	993 => "IMAP/S",
221
	4500 => "IPsec NAT-T",
222
	500 => "ISAKMP",
223
	1701 => "L2TP",
224
	389 => "LDAP",
225
	1755 => "MMS/TCP",
226
	7000 => "MMS/UDP",
227
	445 => "MS DS",
228
	3389 => "MS RDP",
229
	1512 => "MS WINS",
230
	1863 => "MSN",
231
	119 => "NNTP",
232
	123 => "NTP",
233
	138 => "NetBIOS-DGM",
234
	137 => "NetBIOS-NS",
235
	139 => "NetBIOS-SSN",
236
	1194 => "OpenVPN",
237
	110 => "POP3",
238
	995 => "POP3/S",
239
	1723 => "PPTP",
240
	1812 => "RADIUS",
241
	1813 => "RADIUS accounting",
242
	5004 => "RTP",
243
	5060 => "SIP",
244
	25 => "SMTP",
245
	465 => "SMTP/S",
246
	161 => "SNMP",
247
	162 => "SNMP-Trap",
248
	22 => "SSH",
249
	3478 => "STUN",
250
	587 => "SUBMISSION",
251
	3544 => "Teredo",
252
	23 => "Telnet",
253
	69 => "TFTP",
254
	5900 => "VNC");
255

    
256
/* TCP flags */
257
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg", "ece", "cwr");
258

    
259
$specialnets = array("(self)" => "This Firewall", "pppoe" => "PPPoE clients", "l2tp" => "L2TP clients");
260

    
261
$spiflist = get_configured_interface_with_descr(false, true);
262
foreach ($spiflist as $ifgui => $ifdesc) {
263
	$specialnets[$ifgui] = $ifdesc . " net";
264
	$specialnets[$ifgui . 'ip'] = $ifdesc . " address";
265
}
266

    
267
$medias = array(
268
	"auto" => "autoselect",
269
	"100full" => "100BASE-TX full-duplex",
270
	"100half" => "100BASE-TX half-duplex",
271
	"10full" => "10BASE-T full-duplex",
272
	"10half" => "10BASE-T half-duplex");
273

    
274
$wlan_modes = array(
275
	"bss" => "Infrastructure (BSS)",
276
	"adhoc" => "Ad-hoc (IBSS)",
277
	"hostap" => "Access Point");
278

    
279
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, &$input_errors) {
280

    
281
	/* check for bad control characters */
282
	foreach ($postdata as $pn => $pd) {
283
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
284
			$input_errors[] = sprintf(gettext("The field %s contains invalid characters."), $pn);
285
		}
286
	}
287

    
288
	for ($i = 0; $i < count($reqdfields); $i++) {
289
		if ($_POST[$reqdfields[$i]] == "" && $_REQUEST[$reqdfields[$i]] == "") {
290
			$input_errors[] = sprintf(gettext("The field %s is required."), $reqdfieldsn[$i]);
291
		}
292
	}
293
}
294

    
295
function print_input_errors($input_errors) {
296
	echo '<div class="alert alert-danger input-errors">';
297
	echo '<p>' . gettext('The following input errors were detected:') . '</p>';
298
	echo '<ul>';
299

    
300
	foreach ($input_errors as $ierr) {
301
		echo '<li>' . htmlspecialchars($ierr) . '</li>';
302
	}
303

    
304
	echo '</ul>';
305
	echo '</div>';
306
}
307

    
308
function verify_gzip_file($fname) {
309
	$returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
310
	if ($returnvar != 0) {
311
		return 0;
312
	} else {
313
		return 1;
314
	}
315
}
316

    
317
function print_info_box_np($msg, $name="apply", $value="", $showapply=false, $class="alert-warning") {
318
	global $g;
319

    
320
	if (strpos($class, "alert-") !== 0)
321
		$class = 'alert-' . $class;
322

    
323
	if (empty($value)) {
324
		$value = gettext("Apply changes");
325
	}
326

    
327
	$msg = '<div class="pull-left">' . $msg . '</div>';
328

    
329
	if (stristr($msg, gettext("apply")) != false || stristr($msg, gettext("save")) != false || stristr($msg, gettext("create")) != false || $showapply) {
330
		$msg .= '<form method="post" class="pull-right"><button type="submit" class="btn btn-default" name="'. $name .'" value="'.$value.'">'.$name.'</button>';
331

    
332
		if ($_POST['if'])
333
			$msg .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
334

    
335
		$msg .= '</form>';
336
	} else
337
		$msg = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'. $msg;
338

    
339
	echo '<div class="alert ' . $class . ' clearfix" role="alert">'.$msg.'</div>';
340
}
341

    
342
function print_info_box_np_undo($msg, $name = "apply", $value = "Apply changes", $undo) {
343
	global $g;
344

    
345
	if (stristr($msg, "apply") != false || stristr($msg, "save") != false || stristr($msg, "create") != false) {
346
		$savebutton = "<td class=\"infoboxsave nowrap\">";
347
		$savebutton .= "<input type=\"button\" value=\"". gettext("Undo") . "\" onclick=\"document.location='{$undo}'\" />";
348
		$savebutton .= "<input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\" />";
349
		$savebutton .= "</td>";
350
		if ($_POST['if']) {
351
			$savebutton .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
352
		}
353
	}
354

    
355
	$nifty_redbox = "#990000";
356
	$nifty_blackbox = "#000000";
357

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

    
362
	echo <<<EOFnp
363
	<table class="infobox" id="redboxtable" summary="red box table">
364
		<tr>
365
			<td>
366
				<div class="infoboxnp" id="redbox">
367
					<table class="infoboxnptable2" summary="message">
368
						<tr>
369
							<td class="infoboxnptd">
370
								&nbsp;&nbsp;&nbsp;<i class="icon-large icon-exclamation-sign"></i>
371
							</td>
372
							<td class="infoboxnptd2">
373
								<b>{$msg}</b>
374
							</td>
375
							{$savebutton}
376
							{$undobutton}
377
						</tr>
378
					</table>
379
				</div>
380
				<div>
381
					<p>&nbsp;</p>
382
				</div>
383
			</td>
384
		</tr>
385
	</table>
386
	<script type="text/javascript">
387
	//<![CDATA[
388
		Rounded("div#redbox","all","#FFF","{$nifty_redbox}","smooth");
389
		Rounded("td#blackbox","all","#FFF","{$nifty_blackbox}","smooth");
390
	//]]>
391
	</script>
392
EOFnp;
393

    
394
}
395

    
396
function print_info_box($msg, $class="alert-warning") {
397
	print_info_box_np($msg, null, null, false, $class);
398

    
399
}
400

    
401
function get_std_save_message($ok) {
402
	global $d_sysrebootreqd_path;
403
	$filter_related = false;
404
	$filter_pages = array("nat", "filter");
405
	$to_return = gettext("The changes have been applied successfully.");
406
	foreach ($filter_pages as $fp) {
407
		if (stristr($_SERVER['SCRIPT_FILENAME'], $fp)) {
408
			$filter_related = true;
409
		}
410
	}
411
	if ($filter_related) {
412
		$to_return .= "<br />" . gettext("You can also <a href=\"status_filter_reload.php\">monitor</a> the filter reload progress.");
413
	}
414
	return $to_return;
415
}
416

    
417
function pprint_address($adr) {
418
	global $specialnets;
419

    
420
	if (isset($adr['any'])) {
421
		$padr = "*";
422
	} else if ($adr['network']) {
423
		$padr = $specialnets[$adr['network']];
424
	} else {
425
		$padr = $adr['address'];
426
	}
427

    
428
	if (isset($adr['not'])) {
429
		$padr = "! " . $padr;
430
	}
431

    
432
	return $padr;
433
}
434

    
435
function pprint_port($port) {
436
	global $wkports;
437

    
438
	$pport = "";
439

    
440
	if (!$port) {
441
		return "*";
442
	} else {
443
		$srcport = explode("-", $port);
444
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
445
			$pport = $srcport[0];
446
			if ($wkports[$srcport[0]]) {
447
				$pport .= " (" . $wkports[$srcport[0]] . ")";
448
			}
449
		} else {
450
			$pport .= $srcport[0] . " - " . $srcport[1];
451
		}
452
	}
453

    
454
	return $pport;
455
}
456

    
457
function firewall_check_for_advanced_options(&$item) {
458
	$item_set = "";
459
	if ($item['os']) {
460
			$item_set .= "os {$item['os']} ";
461
	}
462
	if ($item['dscp']) {
463
		$item_set .= "dscp {$item['dscp']} ";
464
	}
465
	if ($item['max']) {
466
		$item_set .= "max {$item['max']} ";
467
	}
468
	if ($item['max-src-nodes']) {
469
		$item_set .= "max-src-nodes {$item['max-src-nodes']} ";
470
	}
471
	if ($item['max-src-conn']) {
472
		$item_set .= "max-src-conn {$item['max-src-conn']} ";
473
	}
474
	if ($item['max-src-states']) {
475
		$item_set .= "max-src-states {$item['max-src-states']} ";
476
	}
477
	if (isset($item['nopfsync'])) {
478
		$item_set .= "nopfsync ";
479
	}
480
	if ($item['statetype'] != "keep state" && $item['statetype'] != "") {
481
		$item_set .= "statetype {$item['statetype']} ";
482
	}
483
	if ($item['statetimeout']) {
484
		$item_set .= "statetimeout {$item['statetimeout']} ";
485
	}
486
	if (isset($item['nosync'])) {
487
		$item_set .= "no XMLRPC Sync ";
488
	}
489
	if ($item['max-src-conn-rate']) {
490
		$item_set .= "max-src-conn-rate {$item['max-src-conn-rate']} ";
491
	}
492
	if ($item['max-src-conn-rates']) {
493
		$item_set .= "max-src-conn-rates {$item['max-src-conn-rates']} ";
494
	}
495
	if ($item['vlanprio']) {
496
		$item_set .= "vlanprio {$item['vlanprio']} ";
497
	}
498
	if ($item['vlanprioset']) {
499
		$item_set .= "vlanprioset {$item['vlanprioset']} ";
500
	}
501
	if ($item['gateway']) {
502
		$item_set .= "gateway {$item['gateway']} ";
503
	}
504
	if ($item['dnpipe']) {
505
		$item_set .= "limiter {$item['dnpipe']} ";
506
	}
507
	if ($item['pdnpipe']) {
508
		$item_set .= "limiter {$item['pdnpipe']} ";
509
	}
510
	if ($item['ackqueue']) {
511
		$item_set .= "ackqueue {$item['ackqueue']} ";
512
	}
513
	if ($item['defaultqueue']) {
514
		$item_set .= "defaultqueue {$item['defaultqueue']} ";
515
	}
516
	if ($item['l7container']) {
517
		$item_set .= "layer7 {$item['l7container']} ";
518
	}
519
	if ($item['tag']) {
520
		$item_set .= "tag {$item['tag']} ";
521
	}
522
	if ($item['tagged']) {
523
		$item_set .= "tagged {$item['tagged']} ";
524
	}
525
	if (isset($item['allowopts'])) {
526
		$item_set .= "allowopts ";
527
	}
528
	if (isset($item['disablereplyto'])) {
529
		$item_set .= "disable reply-to ";
530
	}
531
	if ($item['tcpflags_any'] || $item['tcpflags1'] || $item['tcpflags2']) {
532
		$item_set .= "tcpflags set";
533
	}
534

    
535
	return $item_set;
536
}
537

    
538
function gentitle($title) {
539
	global $navlevelsep;
540
	if (!is_array($title)) {
541
		return $title;
542
	} else {
543
		return join($navlevelsep, $title);
544
	}
545
}
546

    
547
function genhtmltitle($title) {
548
	if (!is_array($title))
549
		return '<h1 class="page-header"><a href="">' . $title . '</a></h1>';
550

    
551
	$heading = '<h1 class="page-header"><a href="">' . end($title) . '</a></h1>';
552

    
553
	// If the array contains only one element, there are no breadcrumbs, so don't
554
	// add anything else
555
	if(count($title) > 1) {
556
		$bc = '<ol class="breadcrumb">';
557

    
558
		foreach ($title as $el)
559
			$bc .= '<li>'.$el.'</li>';
560

    
561
		$bc .= '</ol>';
562
	} else {
563
		$bc = "";
564
	}
565

    
566
	return $heading . $bc;
567
}
568

    
569
/* update the changedesc and changecount(er) variables */
570
function update_changedesc($update) {
571
	global $changedesc;
572
	global $changecount;
573

    
574
	$changedesc .= " {$update}";
575
	$changecount++;
576
}
577

    
578
function clear_log_file($logfile = "/var/log/system.log", $restart_syslogd = true) {
579
	global $config, $g;
580
	if ($restart_syslogd) {
581
		exec("/usr/bin/killall syslogd");
582
	}
583
	if (isset($config['system']['disablesyslogclog'])) {
584
		unlink($logfile);
585
		touch($logfile);
586
	} else {
587
		$log_size = isset($config['syslog']['logfilesize']) ? $config['syslog']['logfilesize'] : "511488";
588
		if (isset($config['system']['usefifolog'])) {
589
			exec("/usr/sbin/fifolog_create -s {$log_size} " . escapeshellarg($logfile));
590
		} else {
591
			exec("/usr/local/sbin/clog -i -s {$log_size} " . escapeshellarg($logfile));
592
		}
593
	}
594
	if ($restart_syslogd) {
595
		system_syslogd_start();
596
	}
597
}
598

    
599
function clear_all_log_files() {
600
	global $g;
601
	exec("/usr/bin/killall syslogd");
602

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

    
608
	system_syslogd_start();
609
	killbyname("dhcpd");
610
	services_dhcpd_configure();
611
	return;
612
}
613

    
614
// This version of dump_clog() does not output <td></td> or any other table elements.
615
function dump_clog_no_table($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
616
	global $g, $config;
617
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
618
	$logarr = "";
619
	$grepline = "  ";
620
	if (is_array($grepfor))
621
		$grepline .= " | /usr/bin/egrep " . escapeshellarg(implode("|", $grepfor));
622
	if (is_array($grepinvert))
623
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
624
	if (is_dir($logfile)) {
625
		$logarr = array("File $logfile is a directory.");
626
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
627
		$logarr = array("Log file started.");
628
	} else {
629
		if ($config['system']['disablesyslogclog']) {
630
			exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
631
		} else {
632
			if (isset($config['system']['usefifolog']))
633
				exec("/usr/sbin/fifolog_reader " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
634
			else
635
				exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
636
		}
637
	}
638
	echo "\n";
639

    
640
	$rows = 0;
641
	foreach ($logarr as $logent) {
642
		$rows++;
643
		$logent = preg_split("/\s+/", $logent, 6);
644

    
645
		if ($withorig) {
646
				if (isset($config['system']['usefifolog'])) {
647
					$entry_date_time = htmlspecialchars(date("F j, Y, g:i a","" . $logent[1] . ""));
648
					$entry_text = htmlspecialchars($logent[5]);
649
				} else {
650
					$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
651
					$entry_text = ($logent[3] ==  $config['system']['hostname']) ? "" : $logent[3] . " ";
652
					$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
653
				}
654
				echo "{$entry_date_time}";
655
				echo " " . "{$entry_text}"	. "\n";
656
		} else {
657
				echo htmlspecialchars($logent[5]) . "\n";
658
		}
659

    
660
	}
661
	return($rows);
662
}
663

    
664
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
665
	global $g, $config;
666
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
667
	$logarr = "";
668
	$grepline = "  ";
669
	if (is_array($grepfor)) {
670
		$grepline .= " | /usr/bin/egrep " . escapeshellarg(implode("|", $grepfor));
671
	}
672
	if (is_array($grepinvert)) {
673
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
674
	}
675
	if (is_dir($logfile)) {
676
		$logarr = array("File $logfile is a directory.");
677
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
678
		$logarr = array("Log file started.");
679
	} else {
680
		if ($config['system']['disablesyslogclog']) {
681
			exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
682
		} else {
683
			if (isset($config['system']['usefifolog'])) {
684
				exec("/usr/sbin/fifolog_reader " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
685
			} else {
686
				exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
687
			}
688
		}
689
	}
690

    
691
	foreach ($logarr as $logent) {
692
		$logent = preg_split("/\s+/", $logent, 6);
693
		echo "<tr>\n";
694
		if ($withorig) {
695
			if (isset($config['system']['usefifolog'])) {
696
				$entry_date_time = htmlspecialchars(date("F j, Y, g:i a", "" . $logent[1] . ""));
697
				$entry_text = htmlspecialchars($logent[5]);
698
			} else {
699
				$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
700
				$entry_text = ($logent[3] == $config['system']['hostname']) ? "" : $logent[3] . " ";
701
				$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
702
			}
703
			echo "<td>{$entry_date_time}</td>\n";
704
			echo "<td>{$entry_text}</td>\n";
705
		} else {
706
				echo "<td>" . htmlspecialchars($logent[5]) . "</td>\n";
707
		}
708
		echo "</tr>\n";
709
	}
710
}
711

    
712
function return_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
713
	global $g, $config;
714
	$sor = (isset($config['syslog']['reverse']) || $grepreverse) ? "-r" : "";
715
	$logarr = "";
716
	$grepline = "  ";
717
	if (is_array($grepfor)) {
718
		$grepline .= " | /usr/bin/egrep " . escapeshellarg(implode("|", $grepfor));
719
	}
720
	if (is_array($grepinvert)) {
721
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
722
	}
723
	if ($config['system']['disablesyslogclog']) {
724
		exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
725
	} else {
726
		if (isset($config['system']['usefifolog'])) {
727
			exec("/usr/sbin/fifolog_reader " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
728
		} else {
729
			exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
730
		}
731
	}
732
	return($logarr);
733
}
734

    
735
/* Check if variable has changed, update and log if it has
736
 * returns true if var changed
737
 * varname = variable name in plain text
738
 * orig = original value
739
 * new = new value
740
 */
741
function update_if_changed($varname, & $orig, $new) {
742
	if (is_array($orig) && is_array($new)) {
743
		$a_diff = array_diff($orig, $new);
744
		foreach ($a_diff as $diff) {
745
			update_changedesc("removed {$varname}: \"{$diff}\"");
746
		}
747
		$a_diff = array_diff($new, $orig);
748
		foreach ($a_diff as $diff) {
749
			update_changedesc("added {$varname}: \"{$diff}\"");
750
		}
751
		$orig = $new;
752
		return true;
753

    
754
	} else {
755
		if ($orig != $new) {
756
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
757
			$orig = $new;
758
			return true;
759
		}
760
	}
761
	return false;
762
}
763

    
764
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
765
	if (isset($adr['any'])) {
766
		$padr = "any";
767
	} else if ($adr['network']) {
768
		$padr = $adr['network'];
769
	} else if ($adr['address']) {
770
		list($padr, $pmask) = explode("/", $adr['address']);
771
		if (!$pmask) {
772
			if (is_ipaddrv6($padr)) {
773
				$pmask = 128;
774
			} else {
775
				$pmask = 32;
776
			}
777
		}
778
	}
779

    
780
	if (isset($adr['not'])) {
781
		$pnot = 1;
782
	} else {
783
		$pnot = 0;
784
	}
785

    
786
	if ($adr['port']) {
787
		list($pbeginport, $pendport) = explode("-", $adr['port']);
788
		if (!$pendport) {
789
			$pendport = $pbeginport;
790
		}
791
	} else if (!is_alias($pbeginport) && !is_alias($pendport)) {
792
		$pbeginport = "any";
793
		$pendport = "any";
794
	}
795
}
796

    
797
function pconfig_to_address(&$adr, $padr, $pmask, $pnot = false, $pbeginport = 0, $pendport = 0) {
798
	$adr = array();
799

    
800
	if ($padr == "any") {
801
		$adr['any'] = true;
802
	} else if (is_specialnet($padr)) {
803
		$adr['network'] = $padr;
804
	} else {
805
		$adr['address'] = $padr;
806
		if (is_ipaddrv6($padr)) {
807
			if ($pmask != 128) {
808
				$adr['address'] .= "/" . $pmask;
809
			}
810
		} else {
811
			if ($pmask != 32) {
812
				$adr['address'] .= "/" . $pmask;
813
			}
814
		}
815
	}
816

    
817
	if ($pnot) {
818
		$adr['not'] = true;
819
	} else {
820
		unset($adr['not']);
821
	}
822

    
823
	if (($pbeginport != 0) && ($pbeginport != "any")) {
824
		if ($pbeginport != $pendport) {
825
			$adr['port'] = $pbeginport . "-" . $pendport;
826
		} else {
827
			$adr['port'] = $pbeginport;
828
		}
829
	}
830

    
831
	if (is_alias($pbeginport)) {
832
		$adr['port'] = $pbeginport;
833
	}
834
}
835

    
836
function is_specialnet($net) {
837
	global $specialsrcdst;
838

    
839
	if (!$net) {
840
		return false;
841
	}
842
	if (in_array($net, $specialsrcdst)) {
843
		return true;
844
	} else {
845
		return false;
846
	}
847
}
848

    
849
//function to create widget tabs when called
850
function display_widget_tabs(& $tab_array) {
851
	echo "<div id=\"tabs\">";
852
	$tabscounter = 0;
853
	foreach ($tab_array as $ta) {
854
		$dashpos = strpos($ta[2], '-');
855
		$tabname = $ta[2] . "-tab";
856
		$tabclass = substr($ta[2], 0, $dashpos);
857
		$tabclass = $tabclass . "-class";
858
		if ($ta[1] == true) {
859
			$tabActive = "table-cell";
860
			$tabNonActive = "none";
861
		} else {
862
			$tabActive = "none";
863
			$tabNonActive = "table-cell";
864
		}
865
		echo "<div id=\"{$ta[2]}-active\" class=\"{$tabclass}-tabactive\" style=\"display:{$tabActive}; background-color:#EEEEEE; color:black;\">";
866
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
867
		echo "&nbsp;&nbsp;&nbsp;</b>";
868
		echo "</div>";
869

    
870
		echo "<div id=\"{$ta[2]}-deactive\" class=\"{$tabclass}-tabdeactive\" style=\"display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;\" onclick=\"return changeTabDIV('{$ta[2]}')\">";
871
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
872
		echo "&nbsp;&nbsp;&nbsp;</b>";
873
		echo "</div>";
874
	}
875

    
876
	echo "<script type=\"text/javascript\">";
877
	echo "\n//<![CDATA[\n";
878
	echo "Rounded(\"div.{$tabclass}-tabactive\",\"top\",\"#CCCCCC\",\"#EEEEEE\",\"smooth\");\n";
879
	echo "Rounded(\"div.{$tabclass}-tabdeactive\",\"top\",\"#CCCCCC\",\"#777777\",\"smooth\");\n";
880
	echo "//]]>\n";
881
	echo "</script>";
882
	echo "</div>";
883
}
884

    
885

    
886
// Return inline javascript file or CSS to minimize
887
// request count going back to server.
888
function outputJavaScriptFileInline($javascript) {
889
	if (file_exists($javascript)) {
890
		echo "\n<script type=\"text/javascript\">\n";
891
		include($javascript);
892
		echo "\n</script>\n";
893
	} else {
894
		echo "\n\n<!-- Could not locate file:  {$javascript} -->\n\n";
895
	}
896
}
897

    
898

    
899

    
900
function outputCSSPrintFileInline($css) {
901
	if (file_exists($css)) {
902
		echo "\n<style media=\"print\" type=\"text/css\">\n";
903
		include($css);
904
		echo "\n</style>\n";
905
	} else {
906
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
907
	}
908
}
909

    
910

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

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

    
965
function is_rfc2616_code($code) {
966
	global $rfc2616;
967
	if (isset($rfc2616[$code])) {
968
		return true;
969
	} else {
970
		return false;
971
	}
972
}
973

    
974
function print_rfc2616_select($tag, $current) {
975
	global $rfc2616;
976

    
977
	/* Default to 200 OK if not set */
978
	if ($current == "") {
979
		$current = 200;
980
	}
981

    
982
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
983
	foreach ($rfc2616 as $code => $message) {
984
		if ($code == $current) {
985
			$sel = " selected=\"selected\"";
986
		} else {
987
			$sel = "";
988
		}
989
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
990
	}
991
	echo "</select>\n";
992
}
993

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

    
1021
/****f* pfsense-utils/display_top_tabs
1022
 * NAME
1023
 *	 display_top_tabs - display tabs with rounded edges
1024
 * INPUTS
1025
 *	 $text	  - array of tabs
1026
 * RESULT
1027
 *	 null
1028
 ******/
1029
function display_top_tabs(& $tab_array, $no_drop_down = false, $type = 'pills') {
1030
	global $config;
1031
	global $g;
1032
	global $tab_array_indent;
1033
	global $tab_array_space;
1034
	global $tab_array_char_limit;
1035

    
1036
	/*	does the user have access to this tab?
1037
	 *	master user has access to everything.
1038
	 *	if the user does not have access, simply
1039
	 *	unset the tab item.
1040
	 */
1041

    
1042
	/* empty string code */
1043
	if ($tab_array_indent == '') {
1044
		$tab_array_indent = 0;
1045
	}
1046

    
1047
	if ($tab_array_space == '') {
1048
		$tab_array_space = 1;
1049
	}
1050

    
1051
	if ($tab_array_char_limit == '') {
1052
		$tab_array_char_limit = 92;
1053
	}
1054

    
1055
	foreach ($tab_array as $tab_id => $ta) {
1056
		if (!isAllowedPage($ta[2])) {
1057
			unset ($tab_array[$tab_id]);
1058
		}
1059
	}
1060

    
1061
	$tab_active_bg	 = "#EEEEEE";
1062
	$tab_inactive_bg = "#777777";
1063
	$nifty_tabs_corners = "#FFF";
1064
	$font_color = "white";
1065

    
1066
	$tabcharcount = 0;
1067
	foreach ($tab_array as $ta)
1068
		$tabcharcount = $tabcharcount + strlen($ta[0]);
1069

    
1070
	if ($no_drop_down == true) {
1071
		$tabcharcount = 0;
1072
		unset($tab_array_char_limit);
1073
	}
1074

    
1075
	// If the character count of the tab names is > 670
1076
	// then show a select item dropdown menubox.
1077
	if ($tabcharcount > $tab_array_char_limit) {
1078
		echo gettext("Currently viewing: ");
1079
		echo "<select name=\"TabSelect\" onchange=\"tabs_will_go(this)\">\n";
1080

    
1081
		foreach ($tab_array as $ta) {
1082
			if ($ta[1] == "true")
1083
				$selected = " selected=\"selected\"";
1084
			else
1085
				$selected = "";
1086
			// Onclick in option will not work in some browser
1087
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
1088
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
1089
		}
1090

    
1091
		echo "</select>\n<p>&nbsp;</p>";
1092
		echo "<script type=\"text/javascript\">";
1093
		echo "\n//<![CDATA[\n";
1094
		echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
1095
		echo "//]]>\n";
1096
		echo "</script>";
1097
	} else {
1098
		echo '<ul class="nav nav-' . $type . '">';
1099

    
1100
		foreach ($tab_array as $ta) {
1101
			echo '<li role="presentation"';
1102
			if ($ta[1]) {
1103
				echo ' class="active"';
1104
			}
1105

    
1106
			echo '><a href="' . $ta[2] . '">' . $ta[0] . '</a></li>';
1107
		}
1108

    
1109
		echo '</ul>';
1110
	}
1111
}
1112

    
1113
function add_package_tabs($tabgroup, &$tab_array) {
1114
	global $config, $g;
1115

    
1116
	if (!isset($config['installedpackages']['package'])) {
1117
		return;
1118
	}
1119

    
1120
	foreach ($config['installedpackages']['package'] as $pkg){
1121
		$pkg_config = read_package_configurationfile($pkg['name']);
1122

    
1123
		if (!isset($pkg_config['tabs']['tab'])) {
1124
			continue;
1125
		}
1126

    
1127
		foreach ($pkg_config['tabs']['tab'] as $tab) {
1128
			$tab_entry = array();
1129
			if ($tab['name']) {
1130
				$tab_entry[] = $tab['name'];
1131
				$tab_entry[] = false;
1132
				$tab_entry[] = $tab['url'];
1133
				$tab_array[] = $tab_entry;
1134
			}
1135
		}
1136
	}
1137
}
1138

    
1139
function alias_info_popup($alias_id) {
1140
	global $config;
1141

    
1142
	if (!is_array($config['aliases']['alias'][$alias_id]))
1143
		return;
1144

    
1145
	$maxlength = 60;
1146
	$alias = $config['aliases']['alias'][$alias_id];
1147
	$content = "";
1148

    
1149
	if ($alias['url'])
1150
	{
1151
		// TODO: Change it when pf supports tables with ports
1152
		if ($alias['type'] == "urltable") {
1153
			exec("/sbin/pfctl -t {$alias['name']} -T show | wc -l", $total_entries);
1154
			$counter=preg_replace("/\D/","",$total_entries[0]);
1155
			exec("/sbin/pfctl -t {$alias['name']} -T show | head -10002", $alias_addresses);
1156
		} else {
1157
			$urlfn = alias_expand_urltable($alias['name']);
1158
			$alias_addresses = explode("\n", file_get_contents($urlfn));
1159
			$counter = count($alias_addresses);
1160
		}
1161

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

    
1164
		if ($counter > 10002)
1165
			$content .= '<i>'. gettext("listing only first 10k items") .'</i>';
1166
	}
1167
	else
1168
	{
1169
		$alias_addresses = explode (" ", $alias['address']);
1170
		$alias_details = explode ("||", $alias['detail']);
1171
		$idx = 0;
1172

    
1173
		$content .= "<table>\n";
1174
		$content .= "<thead>\n";
1175
		$content .= "<tr>\n";
1176
		$content .= "<th>" . gettext("Value") . "</th><th  style='padding-left: 10px;'>" . gettext("Description") . "</th></tr>\n";
1177
		$content .= "</thead>\n";
1178
		$content .= "<tbody>\n";
1179

    
1180
		foreach ($alias_addresses as $ap) {
1181
			$content .= "	<tr>\n";
1182
			$content .= "		<td>\n";
1183
			$content .= 			$ap;
1184
			$content .=	"		</td>\n";
1185
			$content .= "		<td style='padding-left: 10px;'>\n";
1186
			$content .= 			$alias_details[$idx];
1187
			$content .=	"		</td>\n";
1188
			$content .= "	</tr>\n";
1189
			$idx++;
1190
		}
1191

    
1192
		$content .= "</tbody>\n";
1193
		$content .= "<table>\n";
1194
	}
1195

    
1196
	if (strlen($alias['descr']) >= $maxlength)
1197
		$alias['descr'] = substr($alias['descr'], 0, $maxlength) . '&hellip;';
1198

    
1199
	return $content;
1200
}
1201

    
1202
function rule_columns_with_alias($src, $srcport, $dst, $dstport) {
1203
	global $config;
1204

    
1205
	if ($config['aliases']['alias'] == "" || !is_array($config['aliases']['alias']))
1206
		return;
1207

    
1208
	$columns = array();
1209
	foreach ($config['aliases']['alias'] as $alias_id => $alias_name) {
1210
		if ($alias_name['name'] == $src)
1211
			$columns['src'] = $alias_id;
1212
		if ($alias_name['name'] == $srcport)
1213
			$columns['srcport'] = $alias_id;
1214
		if ($alias_name['name'] == $dst )
1215
			$columns['dst'] = $alias_id;
1216
		if ($alias_name['name'] == $dstport)
1217
			$columns['dstport'] = $alias_id;
1218
	}
1219

    
1220
	return $columns;
1221
}
1222

    
1223
function form_output_row($name, $label, $content) {
1224
var_dump($content);die;
1225
?>
1226
<div class="form-group">
1227
	<label for="<?=$name?>" class="col-sm-2 control-label"><?=gettext($label); ?></label>
1228
	<div class="col-sm-10">
1229
		<?=$content?>
1230
	</div>
1231
</div>
1232
<?php
1233
}
1234

    
1235
/* set timezone */
1236
if (isset($config['system']['timezone']) &&
1237
    !empty($config['system']['timezone'])) {
1238
	$timezone = $config['system']['timezone'];
1239
} elseif (isset($g['default_timezone']) && !empty($g['default_timezone'])) {
1240
	$timezone = $g['default_timezone'];
1241
} else {
1242
	$timezone = "Etc/UTC";
1243
}
1244

    
1245
date_default_timezone_set($timezone);
1246

    
1247
?>
(76-76/234)