Project

General

Profile

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

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

    
78
header("X-Frame-Options: SAMEORIGIN");
79
require_once("authgui.inc");
80

    
81
/* parse the configuration and include all configuration functions */
82
require_once("functions.inc");
83

    
84
/* Include the autoloader for all the GUI display classes */
85
require_once("classes/autoload.inc.php");
86

    
87
/* used by progress bar */
88
$lastseen = "-1";
89

    
90
$navlevelsep = ": ";	/* navigation level separator string */
91
$mandfldhtml = "";		/* display this before mandatory input fields */
92
$mandfldhtmlspc = "";	/* same as above, but with spacing */
93

    
94
if (!function_exists('set_language')) {
95
	require_once("pfsense-utils.inc");
96
}
97

    
98
set_language();
99

    
100
/* Some ajax scripts still need access to GUI */
101
if (!$ignorefirmwarelock) {
102
	if (is_subsystem_dirty('firmwarelock')) {
103
		if (!$d_isfwfile) {
104
			header("Location: system_update.php");
105
			exit;
106
		} else {
107
			return;
108
		}
109
	}
110
}
111

    
112
/* Reserved table names to avoid collision */
113
$reserved_table_names = array(
114
	"bogons",
115
	"bogonsv6",
116
	"negate_networks",
117
	"snort2c",
118
	"sshlockout",
119
	"tonatsubnets",
120
	"virusprot",
121
	"vpn_networks",
122
	"webConfiguratorlockout"
123
);
124

    
125
$firewall_rules_dscp_types = array(
126
	"af11",
127
	"af12",
128
	"af13",
129
	"af21",
130
	"af22",
131
	"af23",
132
	"af31",
133
	"af32",
134
	"af33",
135
	"af41",
136
	"af42",
137
	"af43",
138
	"VA",
139
	"EF",
140
	"cs1",
141
	"cs2",
142
	"cs3",
143
	"cs4",
144
	"cs5",
145
	"cs6",
146
	"cs7",
147
	"0x01",
148
	"0x02",
149
	"0x04");
150

    
151
$auth_server_types = array(
152
	'ldap' => "LDAP",
153
	'radius' => "RADIUS");
154

    
155
$ldap_urltypes = array(
156
	'TCP - Standard' => 389,
157
	'SSL - Encrypted' => 636);
158

    
159
$ldap_scopes = array(
160
	'one' => gettext("One Level"),
161
	'subtree' => gettext("Entire Subtree"));
162

    
163
$ldap_protvers = array(
164
	2,
165
	3);
166

    
167
$ldap_templates = array(
168

    
169
	'open' => array(
170
		'desc' => "OpenLDAP",
171
		'attr_user' => "cn",
172
		'attr_group' => "cn",
173
		'attr_member' => "member"),
174

    
175
	'msad' => array(
176
		'desc' => "Microsoft AD",
177
		'attr_user' => "samAccountName",
178
		'attr_group' => "cn",
179
		'attr_member' => "memberOf"),
180

    
181
	'edir' => array(
182
		'desc' => "Novell eDirectory",
183
		'attr_user' => "cn",
184
		'attr_group' => "cn",
185
		'attr_member' => "uniqueMember"));
186

    
187
$radius_srvcs = array(
188
	'both' => gettext("Authentication and Accounting"),
189
	'auth' => gettext("Authentication"),
190
	'acct' => gettext("Accounting"));
191

    
192
$netbios_nodetypes = array(
193
	'0' => "none",
194
	'1' => "b-node",
195
	'2' => "p-node",
196
	'4' => "m-node",
197
	'8' => "h-node");
198

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

    
246
/* TCP flags */
247
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg", "ece", "cwr");
248

    
249
$specialnets = array(
250
	"(self)" => gettext("This Firewall"),
251
	"pppoe" => gettext("PPPoE clients"),
252
	"l2tp" => gettext("L2TP clients"));
253

    
254
$spiflist = get_configured_interface_with_descr(false, true);
255
foreach ($spiflist as $ifgui => $ifdesc) {
256
	$specialnets[$ifgui] = $ifdesc . " net";
257
	$specialnets[$ifgui . 'ip'] = $ifdesc . " address";
258
}
259

    
260
$medias = array(
261
	"auto" => gettext("autoselect"),
262
	"100full" => gettext("100BASE-TX full-duplex"),
263
	"100half" => gettext("100BASE-TX half-duplex"),
264
	"10full" => gettext("10BASE-T full-duplex"),
265
	"10half" => gettext("10BASE-T half-duplex"));
266

    
267
$wlan_modes = array(
268
	"bss" => gettext("Infrastructure (BSS)"),
269
	"adhoc" => gettext("Ad-hoc (IBSS)"),
270
	"hostap" => gettext("Access Point"));
271

    
272
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, &$input_errors) {
273

    
274
	/* check for bad control characters */
275
	foreach ($postdata as $pn => $pd) {
276
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
277
			$input_errors[] = sprintf(gettext("The field %s contains invalid characters."), $pn);
278
		}
279
	}
280

    
281
	for ($i = 0; $i < count($reqdfields); $i++) {
282
		if ($_POST[$reqdfields[$i]] == "" && $_REQUEST[$reqdfields[$i]] == "") {
283
			$input_errors[] = sprintf(gettext("The field %s is required."), $reqdfieldsn[$i]);
284
		}
285
	}
286
}
287

    
288
function print_input_errors($input_errors) {
289
	echo '<div class="alert alert-danger input-errors">';
290
	echo '<p>' . gettext('The following input errors were detected:') . '</p>';
291
	echo '<ul>';
292

    
293
	foreach ($input_errors as $ierr) {
294
		echo '<li>' . htmlspecialchars($ierr) . '</li>';
295
	}
296

    
297
	echo '</ul>';
298
	echo '</div>';
299
}
300

    
301
function verify_gzip_file($fname) {
302
	$returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
303
	if ($returnvar != 0) {
304
		return 0;
305
	} else {
306
		return 1;
307
	}
308
}
309

    
310
// print_info_box() has been updated so that any required button is explicitly created, rather than relying on the detection of certain
311
// strings in the message (such as "apply"). print_info_box_np() has been exterminated.
312
// $class = the bootstrap style class (default, info, warning, success, danger)
313
// $btnname and btntext describe the optional button and its display text, the default is an 'x' Close button.
314
// Note that there is also a shortcut function print_apply_box here that creates a standard "apply" box for you.
315
// In many cases just substitute that for print_info_box_np() to easily get a warning style "Apply changes" box.
316
function print_info_box($msg, $class="alert-warning", $btnname = "close", $btntext = "", $btnicon = "", $btnclass = "default") {
317

    
318
	if (strpos($class, "alert-") !== 0) {
319
		$class = 'alert-' . $class;
320
	}
321

    
322
	$msg = '<div class="pull-left">' . $msg . '</div>';
323

    
324
	if ($btnname === "close") {
325
		$msg = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' . $msg;
326
	} else if ($btnname != "") {
327
		if (empty($btntext)) {
328
			$btntext = $btnname;
329
		}
330
		if (!empty($btnicon)) {
331
			$btnicon = '<i class="fa ' . $btnicon . ' icon-embed-btn"></i>';
332
		}
333

    
334
		$msg .= '<form method="post" class="pull-right"><button type="submit" class="btn btn-' . $btnclass . '" name="'. $btnname . '" value="' . $btntext . '">' . $btnicon . $btntext . '</button>';
335

    
336
		if ($_POST['if']) {
337
			$msg .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
338
		}
339

    
340
		$msg .= '</form>';
341
	}
342

    
343
	echo '<div class="alert ' . $class . ' clearfix" role="alert">' . $msg . '</div>';
344
}
345

    
346
function print_apply_box($msg) {
347
	print_info_box($msg, "warning", "apply", gettext("Apply Changes"), 'fa-check', 'success');
348
}
349

    
350
/*
351
 * Print Bootstrap callout
352
 *
353
 * @param string $msg     message to display
354
 * @param string $class   contextual class, defaults to info (default | danger | warning | info)
355
 * @param string $heading optional callout heading
356
 */
357
function print_callout($msg, $class = 'info', $heading = '') {
358

    
359
	if ('' == $msg) {
360
		return;
361
	}
362
	$class = strtolower($class);
363
	$callout = '';
364

    
365
	if ($class != 'default' && $class != 'danger' && $class != 'warning' && $class != 'info') {
366
		$class = 'info';
367
	}
368
	$callout .= '<div class="bs-callout bs-callout-' . $class . '">';
369

    
370
	if ('' != $heading) {
371
		$callout .= '<h4>' . $heading . '</h4>';
372
	}
373
	$callout .= $msg . '</div>';
374
	echo $callout;
375
}
376

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

    
392
function pprint_address($adr) {
393
	global $specialnets;
394

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

    
403
	if (isset($adr['not'])) {
404
		$padr = "! " . $padr;
405
	}
406

    
407
	return $padr;
408
}
409

    
410
function pprint_port($port) {
411
	global $wkports;
412

    
413
	$pport = "";
414

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

    
429
	return $pport;
430
}
431

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

    
507
	return $item_set;
508
}
509

    
510
function gentitle($title) {
511
	global $navlevelsep;
512
	if (!is_array($title)) {
513
		return $title;
514
	} else {
515
		return join($navlevelsep, $title);
516
	}
517
}
518

    
519
function genhtmltitle($title) {
520

    
521
	// If the array contains only one element, there are no breadcrumbs, so don't
522
	// add anything else
523
	if (count($title) > 1) {
524
		$bc = '<ol class="breadcrumb">';
525

    
526
		foreach ($title as $el) {
527
			$bc .= '<li>'.$el.'</li>';
528
		}
529

    
530
		$bc .= '</ol>';
531
	} else {
532
		$bc = "";
533
	}
534

    
535
	return $heading . $bc;
536
}
537

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

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

    
547
// This version of dump_clog() does not output <td></td> or any other table elements.
548
function dump_clog_no_table($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
549
	global $g, $config;
550
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
551
	$specific_log = basename($logfile, '.log') . '_settings';
552
	if ($config['syslog'][$specific_log]['cronorder'] == 'forward') $sor = "";
553
	if ($config['syslog'][$specific_log]['cronorder'] == 'reverse') $sor = "-r";
554
	$logarr = array();
555
	$grepline = "  ";
556
	if (is_array($grepfor)) {
557
		$invert = '';
558
		if ((strpos($grepfor[0], '!') === 0)) {
559
			$grepfor[0] = substr($grepfor[0], 1);
560
			$invert = '-v';
561
		}
562
		$grepline .= " | /usr/bin/egrep {$invert} " . escapeshellarg(implode("|", $grepfor));
563
	}
564
	if (is_array($grepinvert)) {
565
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
566
	}
567
	if (is_dir($logfile)) {
568
		$logarr = array(sprintf(gettext("File %s is a directory."), $logfile));
569
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
570
		$logarr = array(gettext("Log file started."));
571
	} else {
572
		if ($config['system']['disablesyslogclog']) {
573
			exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
574
		} else {
575
			exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
576
		}
577
	}
578
	echo "\n";
579

    
580
	$rows = 0;
581
	foreach ($logarr as $logent) {
582
		$rows++;
583
		$logent = preg_split("/\s+/", $logent, 6);
584

    
585
		if ($withorig) {
586
				$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
587
				$entry_text = ($logent[3] ==  $config['system']['hostname']) ? "" : $logent[3] . " ";
588
				$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
589
				echo "{$entry_date_time}";
590
				echo " " . "{$entry_text}"	. "\n";
591
		} else {
592
				echo htmlspecialchars($logent[5]) . "\n";
593
		}
594

    
595
	}
596
	return($rows);
597
}
598

    
599
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
600
	global $g, $config;
601
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
602
	$specific_log = basename($logfile, '.log') . '_settings';
603
	if ($config['syslog'][$specific_log]['cronorder'] == 'forward') $sor = "";
604
	if ($config['syslog'][$specific_log]['cronorder'] == 'reverse') $sor = "-r";
605
	$logarr = array();
606
	$grepline = "  ";
607
	if (is_array($grepfor)) {
608
		$invert = '';
609
		if ((strpos($grepfor[0], '!') === 0)) {
610
			$grepfor[0] = substr($grepfor[0], 1);
611
			$invert = '-v';
612
		}
613
		$grepline .= " | /usr/bin/egrep {$invert} " . escapeshellarg(implode("|", $grepfor));
614
	}
615
	if (is_array($grepinvert)) {
616
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
617
	}
618
	if (is_dir($logfile)) {
619
		$logarr = array(sprintf(gettext("File %s is a directory."), $logfile));
620
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
621
		$logarr = array(gettext("Log file started."));
622
	} else {
623
		if ($config['system']['disablesyslogclog']) {
624
			exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
625
		} else {
626
			exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
627
		}
628
	}
629

    
630
	$rows = 0;
631
	foreach ($logarr as $logent) {
632
		$rows++;
633
		$logent = preg_split("/\s+/", $logent, 6);
634
		echo "<tr>\n";
635
		if ($withorig) {
636
			$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
637
			$entry_text = ($logent[3] == $config['system']['hostname']) ? "" : $logent[3] . " ";
638
			$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
639
			echo "<td class=\"text-nowrap\">{$entry_date_time}</td>\n";
640
			echo "<td style=\"word-wrap:break-word; word-break:break-all; white-space:normal\">{$entry_text}</td>\n";
641
		} else {
642
				echo "<td>" . htmlspecialchars($logent[5]) . "</td>\n";
643
		}
644
		echo "</tr>\n";
645
	}
646
	return($rows);
647
}
648

    
649
function return_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
650
	global $g, $config;
651
	$sor = (isset($config['syslog']['reverse']) || $grepreverse) ? "-r" : "";
652
	$specific_log = basename($logfile, '.log') . '_settings';
653
	if (($config['syslog'][$specific_log]['cronorder'] == 'forward') && !$grepreverse) $sor = "";
654
	if (($config['syslog'][$specific_log]['cronorder'] == 'reverse') ||  $grepreverse) $sor = "-r";
655
	$logarr = array();
656
	$grepline = "  ";
657
	if (is_array($grepfor)) {
658
		$grepline .= " | /usr/bin/egrep " . escapeshellarg(implode("|", $grepfor));
659
	}
660
	if (is_array($grepinvert)) {
661
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
662
	}
663
	if ($config['system']['disablesyslogclog']) {
664
		exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
665
	} else {
666
		exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
667
	}
668
	return($logarr);
669
}
670

    
671
/* Check if variable has changed, update and log if it has
672
 * returns true if var changed
673
 * varname = variable name in plain text
674
 * orig = original value
675
 * new = new value
676
 */
677
function update_if_changed($varname, & $orig, $new) {
678
	if (is_array($orig) && is_array($new)) {
679
		$a_diff = array_diff($orig, $new);
680
		foreach ($a_diff as $diff) {
681
			update_changedesc("removed {$varname}: \"{$diff}\"");
682
		}
683
		$a_diff = array_diff($new, $orig);
684
		foreach ($a_diff as $diff) {
685
			update_changedesc("added {$varname}: \"{$diff}\"");
686
		}
687
		$orig = $new;
688
		return true;
689

    
690
	} else {
691
		if ($orig != $new) {
692
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
693
			$orig = $new;
694
			return true;
695
		}
696
	}
697
	return false;
698
}
699

    
700
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
701
	if (isset($adr['any'])) {
702
		$padr = "any";
703
	} else if ($adr['network']) {
704
		$padr = $adr['network'];
705
	} else if ($adr['address']) {
706
		list($padr, $pmask) = explode("/", $adr['address']);
707
		if (!$pmask) {
708
			if (is_ipaddrv6($padr)) {
709
				$pmask = 128;
710
			} else {
711
				$pmask = 32;
712
			}
713
		}
714
	}
715

    
716
	if (isset($adr['not'])) {
717
		$pnot = 1;
718
	} else {
719
		$pnot = 0;
720
	}
721

    
722
	if ($adr['port']) {
723
		list($pbeginport, $pendport) = explode("-", $adr['port']);
724
		if (!$pendport) {
725
			$pendport = $pbeginport;
726
		}
727
	} else if (!is_alias($pbeginport) && !is_alias($pendport)) {
728
		$pbeginport = "any";
729
		$pendport = "any";
730
	}
731
}
732

    
733
function pconfig_to_address(&$adr, $padr, $pmask, $pnot = false, $pbeginport = 0, $pendport = 0) {
734
	$adr = array();
735

    
736
	if ($padr == "any") {
737
		$adr['any'] = true;
738
	} else if (is_specialnet($padr)) {
739
		$adr['network'] = $padr;
740
	} else {
741
		$adr['address'] = $padr;
742
		if (is_ipaddrv6($padr)) {
743
			if ($pmask != 128) {
744
				$adr['address'] .= "/" . $pmask;
745
			}
746
		} else {
747
			if ($pmask != 32) {
748
				$adr['address'] .= "/" . $pmask;
749
			}
750
		}
751
	}
752

    
753
	if ($pnot) {
754
		$adr['not'] = true;
755
	} else {
756
		unset($adr['not']);
757
	}
758

    
759
	if (($pbeginport != 0) && ($pbeginport != "any")) {
760
		if ($pbeginport != $pendport) {
761
			$adr['port'] = $pbeginport . "-" . $pendport;
762
		} else {
763
			$adr['port'] = $pbeginport;
764
		}
765
	}
766

    
767
	if (is_alias($pbeginport)) {
768
		$adr['port'] = $pbeginport;
769
	}
770
}
771

    
772
function is_specialnet($net) {
773
	global $specialsrcdst;
774

    
775
	if (!$net) {
776
		return false;
777
	}
778
	if (in_array($net, $specialsrcdst)) {
779
		return true;
780
	} else {
781
		return false;
782
	}
783
}
784

    
785
//function to create widget tabs when called
786
function display_widget_tabs(& $tab_array) {
787
	echo "<div id=\"tabs\">";
788
	$tabscounter = 0;
789
	foreach ($tab_array as $ta) {
790
		$dashpos = strpos($ta[2], '-');
791
		$tabname = $ta[2] . "-tab";
792
		$tabclass = substr($ta[2], 0, $dashpos);
793
		$tabclass = $tabclass . "-class";
794
		if ($ta[1] == true) {
795
			$tabActive = "table-cell";
796
			$tabNonActive = "none";
797
		} else {
798
			$tabActive = "none";
799
			$tabNonActive = "table-cell";
800
		}
801
		echo "<div id=\"{$ta[2]}-active\" class=\"{$tabclass}-tabactive\" style=\"display:{$tabActive}; background-color:#EEEEEE; color:black;\">";
802
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
803
		echo "&nbsp;&nbsp;&nbsp;</b>";
804
		echo "</div>";
805

    
806
		echo "<div id=\"{$ta[2]}-deactive\" class=\"{$tabclass}-tabdeactive\" style=\"display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;\" onclick=\"return changeTabDIV('{$ta[2]}')\">";
807
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
808
		echo "&nbsp;&nbsp;&nbsp;</b>";
809
		echo "</div>";
810
	}
811
	echo "</div>";
812
}
813

    
814

    
815
// Return inline javascript file or CSS to minimize
816
// request count going back to server.
817
function outputJavaScriptFileInline($javascript) {
818
	if (file_exists($javascript)) {
819
		echo "\n<script type=\"text/javascript\">\n";
820
		include($javascript);
821
		echo "\n</script>\n";
822
	} else {
823
		echo "\n\n<!-- Could not locate file:  {$javascript} -->\n\n";
824
	}
825
}
826

    
827

    
828

    
829
function outputCSSPrintFileInline($css) {
830
	if (file_exists($css)) {
831
		echo "\n<style media=\"print\" type=\"text/css\">\n";
832
		include($css);
833
		echo "\n</style>\n";
834
	} else {
835
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
836
	}
837
}
838

    
839

    
840
function outputCSSFileInline($css) {
841
	if (file_exists($css)) {
842
		echo "\n<style type=\"text/css\">\n";
843
		include($css);
844
		echo "\n</style>\n";
845
	} else {
846
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
847
	}
848
}
849

    
850
$rfc2616 = array(
851
	100 => "100 Continue",
852
	101 => "101 Switching Protocols",
853
	200 => "200 OK",
854
	201 => "201 Created",
855
	202 => "202 Accepted",
856
	203 => "203 Non-Authoritative Information",
857
	204 => "204 No Content",
858
	205 => "205 Reset Content",
859
	206 => "206 Partial Content",
860
	300 => "300 Multiple Choices",
861
	301 => "301 Moved Permanently",
862
	302 => "302 Found",
863
	303 => "303 See Other",
864
	304 => "304 Not Modified",
865
	305 => "305 Use Proxy",
866
	306 => "306 (Unused)",
867
	307 => "307 Temporary Redirect",
868
	400 => "400 Bad Request",
869
	401 => "401 Unauthorized",
870
	402 => "402 Payment Required",
871
	403 => "403 Forbidden",
872
	404 => "404 Not Found",
873
	405 => "405 Method Not Allowed",
874
	406 => "406 Not Acceptable",
875
	407 => "407 Proxy Authentication Required",
876
	408 => "408 Request Timeout",
877
	409 => "409 Conflict",
878
	410 => "410 Gone",
879
	411 => "411 Length Required",
880
	412 => "412 Precondition Failed",
881
	413 => "413 Request Entity Too Large",
882
	414 => "414 Request-URI Too Long",
883
	415 => "415 Unsupported Media Type",
884
	416 => "416 Requested Range Not Satisfiable",
885
	417 => "417 Expectation Failed",
886
	500 => "500 Internal Server Error",
887
	501 => "501 Not Implemented",
888
	502 => "502 Bad Gateway",
889
	503 => "503 Service Unavailable",
890
	504 => "504 Gateway Timeout",
891
	505 => "505 HTTP Version Not Supported"
892
);
893

    
894
function is_rfc2616_code($code) {
895
	global $rfc2616;
896
	if (isset($rfc2616[$code])) {
897
		return true;
898
	} else {
899
		return false;
900
	}
901
}
902

    
903
function print_rfc2616_select($tag, $current) {
904
	global $rfc2616;
905

    
906
	/* Default to 200 OK if not set */
907
	if ($current == "") {
908
		$current = 200;
909
	}
910

    
911
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
912
	foreach ($rfc2616 as $code => $message) {
913
		if ($code == $current) {
914
			$sel = " selected";
915
		} else {
916
			$sel = "";
917
		}
918
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
919
	}
920
	echo "</select>\n";
921
}
922

    
923
// Useful debugging function, much cleaner than print_r
924
function echo_array($array, $return_me = false) {
925
	if (is_array($array) == false) {
926
		$return = "The provided variable is not an array.";
927
	} else {
928
		foreach ($array as $name=>$value) {
929
			if (is_array($value)) {
930
				$return .= "";
931
				$return .= "['<b>$name</b>'] {<div style=\"margin-left:10px;\">\n";
932
				$return .= echo_array($value, true);
933
				$return .= "</div>}";
934
				$return .= "\n\n";
935
			} else {
936
				if (is_string($value)) {
937
					$value = "\"$value\"";
938
				}
939
				$return .= "['<b>$name</b>'] = $value\n\n";
940
			}
941
		}
942
	}
943
	if ($return_me == true) {
944
		return $return;
945
	} else {
946
		echo "<pre>".$return."</pre>";
947
	}
948
}
949

    
950
/****f* pfsense-utils/display_top_tabs
951
 * NAME
952
 *	 display_top_tabs - display tabs with rounded edges
953
 * INPUTS
954
 *	 $text	  - array of tabs
955
 * RESULT
956
 *	 null
957
 ******/
958
function display_top_tabs(& $tab_array, $no_drop_down = false, $type = 'pills') {
959
	global $config;
960
	global $g;
961
	global $tab_array_indent;
962
	global $tab_array_space;
963
	global $tab_array_char_limit;
964

    
965
	/*	does the user have access to this tab?
966
	 *	master user has access to everything.
967
	 *	if the user does not have access, simply
968
	 *	unset the tab item.
969
	 */
970

    
971
	/* empty string code */
972
	if ($tab_array_indent == '') {
973
		$tab_array_indent = 0;
974
	}
975

    
976
	if ($tab_array_space == '') {
977
		$tab_array_space = 1;
978
	}
979

    
980
	if ($tab_array_char_limit == '') {
981
		$tab_array_char_limit = 92;
982
	}
983

    
984
	foreach ($tab_array as $tab_id => $ta) {
985
		if (!isAllowedPage($ta[2])) {
986
			unset ($tab_array[$tab_id]);
987
		}
988
	}
989

    
990
	$tab_active_bg	 = "#EEEEEE";
991
	$tab_inactive_bg = "#777777";
992
	$nifty_tabs_corners = "#FFF";
993
	$font_color = "white";
994

    
995
	$tabcharcount = 0;
996
	foreach ($tab_array as $ta) {
997
		$tabcharcount = $tabcharcount + strlen($ta[0]);
998
	}
999

    
1000
	if ($no_drop_down == true) {
1001
		$tabcharcount = 0;
1002
		unset($tab_array_char_limit);
1003
	}
1004

    
1005
	// If the character count of the tab names is > 670
1006
	// then show a select item dropdown menubox.
1007
	if ($tabcharcount > $tab_array_char_limit) {
1008
		echo gettext("Currently viewing: ");
1009
		echo "<select name=\"TabSelect\" onchange=\"tabs_will_go(this)\">\n";
1010

    
1011
		foreach ($tab_array as $ta) {
1012
			if ($ta[1] == "true") {
1013
				$selected = " selected";
1014
			} else {
1015
				$selected = "";
1016
			}
1017
			// Onclick in option will not work in some browser
1018
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
1019
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
1020
		}
1021

    
1022
		echo "</select>\n<p>&nbsp;</p>";
1023
		echo "<script type=\"text/javascript\">";
1024
		echo "\n//<![CDATA[\n";
1025
		echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
1026
		echo "//]]>\n";
1027
		echo "</script>";
1028
	} else {
1029
		echo '<ul class="nav nav-' . $type . '">';
1030

    
1031
		foreach ($tab_array as $ta) {
1032
			echo '<li role="presentation"';
1033
			if ($ta[1]) {
1034
				echo ' class="active"';
1035
			}
1036

    
1037
			echo '><a href="' . $ta[2] . '">' . $ta[0] . '</a></li>';
1038
		}
1039

    
1040
		echo '</ul>';
1041
	}
1042
}
1043

    
1044
function add_package_tabs($tabgroup, &$tab_array) {
1045
	global $config, $g;
1046

    
1047
	if (!isset($config['installedpackages']['package'])) {
1048
		return;
1049
	}
1050

    
1051
	foreach ($config['installedpackages']['package'] as $pkg) {
1052
		$pkg_config = read_package_configurationfile($pkg['name']);
1053

    
1054
		if (!isset($pkg_config['tabs']['tab'])) {
1055
			continue;
1056
		}
1057

    
1058
		foreach ($pkg_config['tabs']['tab'] as $tab) {
1059
			$tab_entry = array();
1060
			if ($tab['name']) {
1061
				$tab_entry[] = $tab['name'];
1062
				$tab_entry[] = false;
1063
				$tab_entry[] = $tab['url'];
1064
				$tab_array[] = $tab_entry;
1065
			}
1066
		}
1067
	}
1068
}
1069

    
1070
function alias_info_popup($alias_id) {
1071
	global $config;
1072

    
1073
	if (!is_array($config['aliases']['alias'][$alias_id])) {
1074
		return;
1075
	}
1076

    
1077
	$maxlength = 60;
1078
	$alias = $config['aliases']['alias'][$alias_id];
1079
	$content = "";
1080

    
1081
	if ($alias['url']) {
1082
		// TODO: Change it when pf supports tables with ports
1083
		if ($alias['type'] == "urltable") {
1084
			exec("/sbin/pfctl -t {$alias['name']} -T show | wc -l", $total_entries);
1085
			$counter=preg_replace("/\D/", "", $total_entries[0]);
1086
			exec("/sbin/pfctl -t {$alias['name']} -T show | head -10002", $alias_addresses);
1087
		} else {
1088
			$urlfn = alias_expand_urltable($alias['name']);
1089
			$alias_addresses = explode("\n", file_get_contents($urlfn));
1090
			$counter = count($alias_addresses);
1091
		}
1092

    
1093
		$content .= '<h5>'. $alias['url'] .'</h5><ul><li>'. implode('</li><li>', $alias_addresses) .'</li></ul>';
1094
		if ($counter > 10002) {
1095
			$content .= '<i>'. gettext("listing only first 10k items") .'</i>';
1096
		}
1097
	} else {
1098
		$alias_addresses = explode (" ", $alias['address']);
1099
		$alias_details = explode ("||", $alias['detail']);
1100
		$idx = 0;
1101

    
1102
		$content .= "<table>\n";
1103
		$content .= "<thead>\n";
1104
		$content .= "<tr>\n";
1105
		$content .= "<th>" . gettext("Value") . "</th><th  style='padding-left: 10px;'>" . gettext("Description") . "</th></tr>\n";
1106
		$content .= "</thead>\n";
1107
		$content .= "<tbody>\n";
1108

    
1109
		foreach ($alias_addresses as $ap) {
1110
			$content .= "	<tr>\n";
1111
			$content .= "		<td>\n";
1112
			$content .= 			$ap;
1113
			$content .=	"		</td>\n";
1114
			$content .= "		<td style='padding-left: 10px;'>\n";
1115
			$content .= 			htmlspecialchars($alias_details[$idx]);
1116
			$content .=	"		</td>\n";
1117
			$content .= "	</tr>\n";
1118
			$idx++;
1119
		}
1120

    
1121
		$content .= "</tbody>\n";
1122
		$content .= "<table>\n";
1123
	}
1124

    
1125
	if (strlen($alias['descr']) >= $maxlength) {
1126
		$alias['descr'] = substr($alias['descr'], 0, $maxlength) . '&hellip;';
1127
	}
1128

    
1129
	return $content;
1130
}
1131

    
1132
function rule_columns_with_alias($src, $srcport, $dst, $dstport) {
1133
	global $config;
1134

    
1135
	if ($config['aliases']['alias'] == "" || !is_array($config['aliases']['alias'])) {
1136
		return;
1137
	}
1138

    
1139
	$columns = array();
1140
	foreach ($config['aliases']['alias'] as $alias_id => $alias_name) {
1141
		if ($alias_name['name'] == $src) {
1142
			$columns['src'] = $alias_id;
1143
		}
1144
		if ($alias_name['name'] == $srcport) {
1145
			$columns['srcport'] = $alias_id;
1146
		}
1147
		if ($alias_name['name'] == $dst) {
1148
			$columns['dst'] = $alias_id;
1149
		}
1150
		if ($alias_name['name'] == $dstport) {
1151
			$columns['dstport'] = $alias_id;
1152
		}
1153
	}
1154

    
1155
	return $columns;
1156
}
1157

    
1158
function form_output_row($name, $label, $content) {
1159
var_dump($content);die;
1160
?>
1161
<div class="form-group">
1162
	<label for="<?=$name?>" class="col-sm-2 control-label"><?=gettext($label); ?></label>
1163
	<div class="col-sm-10">
1164
		<?=$content?>
1165
	</div>
1166
</div>
1167
<?php
1168
}
1169

    
1170
function set_flash_message($class, $msg) {
1171
	@session_start();
1172
	$_SESSION['flash_messages'][$class][] = $msg;
1173
}
1174

    
1175
function get_flash_message() {
1176
	@session_start();
1177
	if (isset($_SESSION['flash_messages']) && !empty($_SESSION['flash_messages'])) {
1178
		foreach ($_SESSION['flash_messages'] as $class => $flash_message) {
1179
			print_info_box(implode("<br />", $flash_message), $class);
1180
		}
1181
		unset($_SESSION['flash_messages']);
1182
	}
1183
}
1184

    
1185
/* Retrieve GET or POST Value/State
1186
 * Eample Usage:
1187
 * $value = getGETPOSTsettingvalue('get/post parameter name', "");
1188
 * $value = getGETPOSTsettingvalue('get/post parameter name', null);
1189
 * $state = getGETPOSTsettingvalue('get/post parameter name', null);
1190
 * $state = getGETPOSTsettingvalue('get/post parameter name', false);
1191
 */
1192
function getGETPOSTsettingvalue($settingname, $default) {
1193
	$settingvalue = $default;
1194
	if ($_GET[$settingname]) {
1195
		$settingvalue = $_GET[$settingname];
1196
	}
1197
	if ($_POST[$settingname]) {
1198
		$settingvalue = $_POST[$settingname];
1199
	}
1200
	return $settingvalue;
1201
}
1202

    
1203
/* set timezone */
1204
if (isset($config['system']['timezone']) &&
1205
    !empty($config['system']['timezone'])) {
1206
	$timezone = $config['system']['timezone'];
1207
} elseif (isset($g['default_timezone']) && !empty($g['default_timezone'])) {
1208
	$timezone = $g['default_timezone'];
1209
} else {
1210
	$timezone = "Etc/UTC";
1211
}
1212

    
1213
date_default_timezone_set($timezone);
1214

    
1215
?>
(64-64/225)