Project

General

Profile

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

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

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

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

    
413
	return $padr;
414
}
415

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

    
419
	$pport = "";
420

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

    
435
	return $pport;
436
}
437

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

    
513
	return $item_set;
514
}
515

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

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

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

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

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

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

    
546
	return $heading . $bc;
547
}
548

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
815

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

    
828

    
829

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

    
840

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1130
	return $content;
1131
}
1132

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

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

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

    
1156
	return $columns;
1157
}
1158

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

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

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

    
1186
/* set timezone */
1187
if (isset($config['system']['timezone']) &&
1188
    !empty($config['system']['timezone'])) {
1189
	$timezone = $config['system']['timezone'];
1190
} elseif (isset($g['default_timezone']) && !empty($g['default_timezone'])) {
1191
	$timezone = $g['default_timezone'];
1192
} else {
1193
	$timezone = "Etc/UTC";
1194
}
1195

    
1196
date_default_timezone_set($timezone);
1197

    
1198
?>
(64-64/228)