Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
884

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

    
897

    
898

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

    
909

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1074
	// If the character count of the tab names is > 670
1075
	// then show a select item dropdown menubox.
1076
	if ($tabcharcount > $tab_array_char_limit): ?>
1077
		echo gettext("Currently viewing: ");
1078
		echo "<select name=\"TabSelect\" onchange=\"tabs_will_go(this)\">\n";
1079
		foreach ($tab_array as $ta) {
1080
			if ($ta[1] == "true")
1081
				$selected = " selected=\"selected\"";
1082
			else
1083
				$selected = "";
1084
			// Onclick in option will not work in some browser
1085
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
1086
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
1087
		}
1088
		echo "</select>\n<p>&nbsp;</p>";
1089
		echo "<script type=\"text/javascript\">";
1090
		echo "\n//<![CDATA[\n";
1091
		echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
1092
		echo "//]]>\n";
1093
		echo "</script>";
1094
<? else: ?>
1095
	<ul class="nav nav-<?= $type ?>">
1096
		<?php foreach ($tab_array as $ta): ?>
1097
			<li role="presentation"<?=($ta[1] ? ' class="active"' : '')?>><a href="<?=$ta[2]?>"><?=$ta[0]?></a></li>
1098
		<?php endforeach; ?>
1099
	</ul>
1100
<?php endif;
1101
}
1102

    
1103
function add_package_tabs($tabgroup, &$tab_array) {
1104
	global $config, $g;
1105

    
1106
	if (!isset($config['installedpackages']['package'])) {
1107
		return;
1108
	}
1109

    
1110
	foreach ($config['installedpackages']['package'] as $pkg){
1111
		$pkg_config = read_package_configurationfile($pkg['name']);
1112

    
1113
		if (!isset($pkg_config['tabs']['tab'])) {
1114
			continue;
1115
		}
1116

    
1117
		foreach ($pkg_config['tabs']['tab'] as $tab) {
1118
			$tab_entry = array();
1119
			if ($tab['name']) {
1120
				$tab_entry[] = $tab['name'];
1121
				$tab_entry[] = false;
1122
				$tab_entry[] = $tab['url'];
1123
				$tab_array[] = $tab_entry;
1124
			}
1125
		}
1126
	}
1127
}
1128

    
1129
function alias_info_popup($alias_id) {
1130
	global $config;
1131

    
1132
	if (!is_array($config['aliases']['alias'][$alias_id]))
1133
		return;
1134

    
1135
	$maxlength = 60;
1136
	$alias = $config['aliases']['alias'][$alias_id];
1137
	$content = "";
1138

    
1139
	if ($alias['url'])
1140
	{
1141
		// TODO: Change it when pf supports tables with ports
1142
		if ($alias['type'] == "urltable") {
1143
			exec("/sbin/pfctl -t {$alias['name']} -T show | wc -l", $total_entries);
1144
			$counter=preg_replace("/\D/","",$total_entries[0]);
1145
			exec("/sbin/pfctl -t {$alias['name']} -T show | head -10002", $alias_addresses);
1146
		} else {
1147
			$urlfn = alias_expand_urltable($alias['name']);
1148
			$alias_addresses = explode("\n", file_get_contents($urlfn));
1149
			$counter = count($alias_addresses);
1150
		}
1151

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

    
1154
		if ($counter > 10002)
1155
			$content .= '<i>'. gettext("listing only first 10k items") .'</i>';
1156
	}
1157
	else
1158
	{
1159
		$alias_addresses = explode (" ", $alias['address']);
1160
		$alias_details = explode ("||", $alias['detail']);
1161
		$idx = 0;
1162

    
1163
		$content .= "<table>\n";
1164
		$content .= "<thead>\n";
1165
		$content .= "<tr>\n";
1166
		$content .= "<th>" . gettext("Value") . "</th><th  style='padding-left: 10px;'>" . gettext("Description") . "</th></tr>\n";
1167
		$content .= "</thead>\n";
1168
		$content .= "<tbody>\n";
1169

    
1170
		foreach ($alias_addresses as $ap) {
1171
			$content .= "	<tr>\n";
1172
			$content .= "		<td>\n";
1173
			$content .= 			$ap;
1174
			$content .=	"		</td>\n";
1175
			$content .= "		<td style='padding-left: 10px;'>\n";
1176
			$content .= 			$alias_details[$idx];
1177
			$content .=	"		</td>\n";
1178
			$content .= "	</tr>\n";
1179
			$idx++;
1180
		}
1181

    
1182
		$content .= "</tbody>\n";
1183
		$content .= "<table>\n";
1184
	}
1185

    
1186
	if (strlen($alias['descr']) >= $maxlength)
1187
		$alias['descr'] = substr($alias['descr'], 0, $maxlength) . '&hellip;';
1188

    
1189
	return $content;
1190
}
1191

    
1192
function rule_columns_with_alias($src, $srcport, $dst, $dstport) {
1193
	global $config;
1194

    
1195
	if ($config['aliases']['alias'] == "" || !is_array($config['aliases']['alias']))
1196
		return;
1197

    
1198
	$columns = array();
1199
	foreach ($config['aliases']['alias'] as $alias_id => $alias_name) {
1200
		if ($alias_name['name'] == $src)
1201
			$columns['src'] = $alias_id;
1202
		if ($alias_name['name'] == $srcport)
1203
			$columns['srcport'] = $alias_id;
1204
		if ($alias_name['name'] == $dst )
1205
			$columns['dst'] = $alias_id;
1206
		if ($alias_name['name'] == $dstport)
1207
			$columns['dstport'] = $alias_id;
1208
	}
1209

    
1210
	return $columns;
1211
}
1212

    
1213
function form_output_row($name, $label, $content) {
1214
var_dump($content);die;
1215
?>
1216
<div class="form-group">
1217
	<label for="<?=$name?>" class="col-sm-2 control-label"><?=gettext($label); ?></label>
1218
	<div class="col-sm-10">
1219
		<?=$content?>
1220
	</div>
1221
</div>
1222
<?php
1223
}
1224

    
1225
$timezone = $config['system']['timezone'];
1226
if (!$timezone) {
1227
	$timezone = "Etc/UTC";
1228
}
1229

    
1230
date_default_timezone_set($timezone);
1231

    
1232
?>
(76-76/234)