Project

General

Profile

Download (32.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
/* 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
/* Some ajax scripts still need access to GUI */
95
if (!$ignorefirmwarelock) {
96
	if (is_subsystem_dirty('firmwarelock')) {
97
		if (!$d_isfwfile) {
98
			header("Location: system_update.php");
99
			exit;
100
		} else {
101
			return;
102
		}
103
	}
104
}
105

    
106
/* Reserved table names to avoid collision */
107
$reserved_table_names = array(
108
	"bogons",
109
	"bogonsv6",
110
	"negate_networks",
111
	"snort2c",
112
	"sshlockout",
113
	"tonatsubnets",
114
	"virusprot",
115
	"vpn_networks",
116
	"webConfiguratorlockout"
117
);
118

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

    
145
$auth_server_types = array(
146
	'ldap' => "LDAP",
147
	'radius' => "Radius");
148

    
149
$ldap_urltypes = array(
150
	'TCP - Standard' => 389,
151
	'SSL - Encrypted' => 636);
152

    
153
$ldap_scopes = array(
154
	'one' => "One Level",
155
	'subtree' => "Entire Subtree");
156

    
157
$ldap_protvers = array(
158
	2,
159
	3);
160

    
161
$ldap_templates = array(
162

    
163
	'open' => array(
164
		'desc' => "OpenLDAP",
165
		'attr_user' => "cn",
166
		'attr_group' => "cn",
167
		'attr_member' => "member"),
168

    
169
	'msad' => array(
170
		'desc' => "Microsoft AD",
171
		'attr_user' => "samAccountName",
172
		'attr_group' => "cn",
173
		'attr_member' => "memberOf"),
174

    
175
	'edir' => array(
176
		'desc' => "Novell eDirectory",
177
		'attr_user' => "cn",
178
		'attr_group' => "cn",
179
		'attr_member' => "uniqueMember"));
180

    
181
$radius_srvcs = array(
182
	'both' => "Authentication and Accounting",
183
	'auth' => "Authentication",
184
	'acct' => "Accounting");
185

    
186
$netbios_nodetypes = array(
187
	'0' => "none",
188
	'1' => "b-node",
189
	'2' => "p-node",
190
	'4' => "m-node",
191
	'8' => "h-node");
192

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

    
240
/* TCP flags */
241
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg", "ece", "cwr");
242

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

    
245
$spiflist = get_configured_interface_with_descr(false, true);
246
foreach ($spiflist as $ifgui => $ifdesc) {
247
	$specialnets[$ifgui] = $ifdesc . " net";
248
	$specialnets[$ifgui . 'ip'] = $ifdesc . " address";
249
}
250

    
251
$medias = array(
252
	"auto" => "autoselect",
253
	"100full" => "100BASE-TX full-duplex",
254
	"100half" => "100BASE-TX half-duplex",
255
	"10full" => "10BASE-T full-duplex",
256
	"10half" => "10BASE-T half-duplex");
257

    
258
$wlan_modes = array(
259
	"bss" => "Infrastructure (BSS)",
260
	"adhoc" => "Ad-hoc (IBSS)",
261
	"hostap" => "Access Point");
262

    
263
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, &$input_errors) {
264

    
265
	/* check for bad control characters */
266
	foreach ($postdata as $pn => $pd) {
267
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
268
			$input_errors[] = sprintf(gettext("The field %s contains invalid characters."), $pn);
269
		}
270
	}
271

    
272
	for ($i = 0; $i < count($reqdfields); $i++) {
273
		if ($_POST[$reqdfields[$i]] == "" && $_REQUEST[$reqdfields[$i]] == "") {
274
			$input_errors[] = sprintf(gettext("The field %s is required."), $reqdfieldsn[$i]);
275
		}
276
	}
277
}
278

    
279
function print_input_errors($input_errors) {
280
	echo '<div class="alert alert-danger input-errors">';
281
	echo '<p>' . gettext('The following input errors were detected:') . '</p>';
282
	echo '<ul>';
283

    
284
	foreach ($input_errors as $ierr) {
285
		echo '<li>' . htmlspecialchars($ierr) . '</li>';
286
	}
287

    
288
	echo '</ul>';
289
	echo '</div>';
290
}
291

    
292
function verify_gzip_file($fname) {
293
	$returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
294
	if ($returnvar != 0) {
295
		return 0;
296
	} else {
297
		return 1;
298
	}
299
}
300

    
301
function print_info_box_np($msg, $name="apply", $value="", $showapply=false, $class="alert-warning") {
302
	global $g;
303

    
304
	if (strpos($class, "alert-") !== 0) {
305
		$class = 'alert-' . $class;
306
	}
307

    
308
	if (empty($value)) {
309
		$value = gettext("Apply changes");
310
	}
311

    
312
	$msg = '<div class="pull-left">' . $msg . '</div>';
313

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

    
317
		if ($_POST['if']) {
318
			$msg .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
319
		}
320

    
321
		$msg .= '</form>';
322
	} else {
323
		$msg = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'. $msg;
324
	}
325

    
326
	echo '<div class="alert ' . $class . ' clearfix" role="alert">'.$msg.'</div>';
327
}
328

    
329
function print_info_box_np_undo($msg, $name = "apply", $value = "Apply changes", $undo) {
330
	global $g;
331

    
332
	if (stristr($msg, "apply") != false || stristr($msg, "save") != false || stristr($msg, "create") != false) {
333
		$savebutton = "<td class=\"infoboxsave text-nowrap\">";
334
		$savebutton .= "<input type=\"button\" value=\"". gettext("Undo") . "\" onclick=\"document.location='{$undo}'\" />";
335
		$savebutton .= "<input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\" />";
336
		$savebutton .= "</td>";
337
		if ($_POST['if']) {
338
			$savebutton .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
339
		}
340
	}
341

    
342
	$nifty_redbox = "#990000";
343
	$nifty_blackbox = "#000000";
344

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

    
349
	echo <<<EOFnp
350
	<table class="infobox" id="redboxtable" summary="red box table">
351
		<tr>
352
			<td>
353
				<div class="infoboxnp" id="redbox">
354
					<table class="infoboxnptable2" summary="message">
355
						<tr>
356
							<td class="infoboxnptd">
357
								&nbsp;&nbsp;&nbsp;<i class="fa fa-exclamation-circle"></i>
358
							</td>
359
							<td class="infoboxnptd2">
360
								<b>{$msg}</b>
361
							</td>
362
							{$savebutton}
363
							{$undobutton}
364
						</tr>
365
					</table>
366
				</div>
367
				<div>
368
					<p>&nbsp;</p>
369
				</div>
370
			</td>
371
		</tr>
372
	</table>
373
EOFnp;
374

    
375
}
376

    
377
function print_info_box($msg, $class="alert-warning") {
378
	print_info_box_np($msg, null, null, false, $class);
379

    
380
}
381

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

    
397
function pprint_address($adr) {
398
	global $specialnets;
399

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

    
408
	if (isset($adr['not'])) {
409
		$padr = "! " . $padr;
410
	}
411

    
412
	return $padr;
413
}
414

    
415
function pprint_port($port) {
416
	global $wkports;
417

    
418
	$pport = "";
419

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

    
434
	return $pport;
435
}
436

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

    
512
	return $item_set;
513
}
514

    
515
function gentitle($title) {
516
	global $navlevelsep;
517
	if (!is_array($title)) {
518
		return $title;
519
	} else {
520
		return join($navlevelsep, $title);
521
	}
522
}
523

    
524
function genhtmltitle($title) {
525
	if (!is_array($title)) {
526
		return '<h1 class="page-header"><a href="">' . $title . '</a></h1>';
527
	}
528

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

    
531
	// If the array contains only one element, there are no breadcrumbs, so don't
532
	// add anything else
533
	if (count($title) > 1) {
534
		$bc = '<ol class="breadcrumb">';
535

    
536
		foreach ($title as $el) {
537
			$bc .= '<li>'.$el.'</li>';
538
		}
539

    
540
		$bc .= '</ol>';
541
	} else {
542
		$bc = "";
543
	}
544

    
545
	return $heading . $bc;
546
}
547

    
548
/* update the changedesc and changecount(er) variables */
549
function update_changedesc($update) {
550
	global $changedesc;
551
	global $changecount;
552

    
553
	$changedesc .= " {$update}";
554
	$changecount++;
555
}
556

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

    
585
	$rows = 0;
586
	foreach ($logarr as $logent) {
587
		$rows++;
588
		$logent = preg_split("/\s+/", $logent, 6);
589

    
590
		if ($withorig) {
591
				$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
592
				$entry_text = ($logent[3] ==  $config['system']['hostname']) ? "" : $logent[3] . " ";
593
				$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
594
				echo "{$entry_date_time}";
595
				echo " " . "{$entry_text}"	. "\n";
596
		} else {
597
				echo htmlspecialchars($logent[5]) . "\n";
598
		}
599

    
600
	}
601
	return($rows);
602
}
603

    
604
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
605
	global $g, $config;
606
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
607
	$specific_log = basename($logfile, '.log') . '_settings';
608
	if ($config['syslog'][$specific_log]['cronorder'] == 'forward') $sor = "";
609
	if ($config['syslog'][$specific_log]['cronorder'] == 'reverse') $sor = "-r";
610
	$logarr = "";
611
	$grepline = "  ";
612
	if (is_array($grepfor)) {
613
		$grepline .= " | /usr/bin/egrep " . 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("File $logfile is a directory.");
620
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
621
		$logarr = array("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 = "";
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 .= 			$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_np(implode("<br />", $flash_message), null, null, false, $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/229)