Project

General

Profile

Download (32.5 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",$showbtn = true) {
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 if ($showbtn) {
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", $showbtn = true) {
378
	print_info_box_np($msg, null, null, false, $class, $showbtn);
379
}
380

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

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

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

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

    
411
	return $padr;
412
}
413

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

    
417
	$pport = "";
418

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

    
433
	return $pport;
434
}
435

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

    
511
	return $item_set;
512
}
513

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

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

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

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

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

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

    
544
	return $heading . $bc;
545
}
546

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

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

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

    
589
	$rows = 0;
590
	foreach ($logarr as $logent) {
591
		$rows++;
592
		$logent = preg_split("/\s+/", $logent, 6);
593

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

    
604
	}
605
	return($rows);
606
}
607

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

    
639
	$rows = 0;
640
	foreach ($logarr as $logent) {
641
		$rows++;
642
		$logent = preg_split("/\s+/", $logent, 6);
643
		echo "<tr>\n";
644
		if ($withorig) {
645
			$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
646
			$entry_text = ($logent[3] == $config['system']['hostname']) ? "" : $logent[3] . " ";
647
			$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
648
			echo "<td class=\"text-nowrap\">{$entry_date_time}</td>\n";
649
			echo "<td style=\"word-wrap:break-word; word-break:break-all; white-space:normal\">{$entry_text}</td>\n";
650
		} else {
651
				echo "<td>" . htmlspecialchars($logent[5]) . "</td>\n";
652
		}
653
		echo "</tr>\n";
654
	}
655
	return($rows);
656
}
657

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

    
680
/* Check if variable has changed, update and log if it has
681
 * returns true if var changed
682
 * varname = variable name in plain text
683
 * orig = original value
684
 * new = new value
685
 */
686
function update_if_changed($varname, & $orig, $new) {
687
	if (is_array($orig) && is_array($new)) {
688
		$a_diff = array_diff($orig, $new);
689
		foreach ($a_diff as $diff) {
690
			update_changedesc("removed {$varname}: \"{$diff}\"");
691
		}
692
		$a_diff = array_diff($new, $orig);
693
		foreach ($a_diff as $diff) {
694
			update_changedesc("added {$varname}: \"{$diff}\"");
695
		}
696
		$orig = $new;
697
		return true;
698

    
699
	} else {
700
		if ($orig != $new) {
701
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
702
			$orig = $new;
703
			return true;
704
		}
705
	}
706
	return false;
707
}
708

    
709
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
710
	if (isset($adr['any'])) {
711
		$padr = "any";
712
	} else if ($adr['network']) {
713
		$padr = $adr['network'];
714
	} else if ($adr['address']) {
715
		list($padr, $pmask) = explode("/", $adr['address']);
716
		if (!$pmask) {
717
			if (is_ipaddrv6($padr)) {
718
				$pmask = 128;
719
			} else {
720
				$pmask = 32;
721
			}
722
		}
723
	}
724

    
725
	if (isset($adr['not'])) {
726
		$pnot = 1;
727
	} else {
728
		$pnot = 0;
729
	}
730

    
731
	if ($adr['port']) {
732
		list($pbeginport, $pendport) = explode("-", $adr['port']);
733
		if (!$pendport) {
734
			$pendport = $pbeginport;
735
		}
736
	} else if (!is_alias($pbeginport) && !is_alias($pendport)) {
737
		$pbeginport = "any";
738
		$pendport = "any";
739
	}
740
}
741

    
742
function pconfig_to_address(&$adr, $padr, $pmask, $pnot = false, $pbeginport = 0, $pendport = 0) {
743
	$adr = array();
744

    
745
	if ($padr == "any") {
746
		$adr['any'] = true;
747
	} else if (is_specialnet($padr)) {
748
		$adr['network'] = $padr;
749
	} else {
750
		$adr['address'] = $padr;
751
		if (is_ipaddrv6($padr)) {
752
			if ($pmask != 128) {
753
				$adr['address'] .= "/" . $pmask;
754
			}
755
		} else {
756
			if ($pmask != 32) {
757
				$adr['address'] .= "/" . $pmask;
758
			}
759
		}
760
	}
761

    
762
	if ($pnot) {
763
		$adr['not'] = true;
764
	} else {
765
		unset($adr['not']);
766
	}
767

    
768
	if (($pbeginport != 0) && ($pbeginport != "any")) {
769
		if ($pbeginport != $pendport) {
770
			$adr['port'] = $pbeginport . "-" . $pendport;
771
		} else {
772
			$adr['port'] = $pbeginport;
773
		}
774
	}
775

    
776
	if (is_alias($pbeginport)) {
777
		$adr['port'] = $pbeginport;
778
	}
779
}
780

    
781
function is_specialnet($net) {
782
	global $specialsrcdst;
783

    
784
	if (!$net) {
785
		return false;
786
	}
787
	if (in_array($net, $specialsrcdst)) {
788
		return true;
789
	} else {
790
		return false;
791
	}
792
}
793

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

    
815
		echo "<div id=\"{$ta[2]}-deactive\" class=\"{$tabclass}-tabdeactive\" style=\"display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;\" onclick=\"return changeTabDIV('{$ta[2]}')\">";
816
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
817
		echo "&nbsp;&nbsp;&nbsp;</b>";
818
		echo "</div>";
819
	}
820
	echo "</div>";
821
}
822

    
823

    
824
// Return inline javascript file or CSS to minimize
825
// request count going back to server.
826
function outputJavaScriptFileInline($javascript) {
827
	if (file_exists($javascript)) {
828
		echo "\n<script type=\"text/javascript\">\n";
829
		include($javascript);
830
		echo "\n</script>\n";
831
	} else {
832
		echo "\n\n<!-- Could not locate file:  {$javascript} -->\n\n";
833
	}
834
}
835

    
836

    
837

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

    
848

    
849
function outputCSSFileInline($css) {
850
	if (file_exists($css)) {
851
		echo "\n<style type=\"text/css\">\n";
852
		include($css);
853
		echo "\n</style>\n";
854
	} else {
855
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
856
	}
857
}
858

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

    
903
function is_rfc2616_code($code) {
904
	global $rfc2616;
905
	if (isset($rfc2616[$code])) {
906
		return true;
907
	} else {
908
		return false;
909
	}
910
}
911

    
912
function print_rfc2616_select($tag, $current) {
913
	global $rfc2616;
914

    
915
	/* Default to 200 OK if not set */
916
	if ($current == "") {
917
		$current = 200;
918
	}
919

    
920
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
921
	foreach ($rfc2616 as $code => $message) {
922
		if ($code == $current) {
923
			$sel = " selected";
924
		} else {
925
			$sel = "";
926
		}
927
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
928
	}
929
	echo "</select>\n";
930
}
931

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

    
959
/****f* pfsense-utils/display_top_tabs
960
 * NAME
961
 *	 display_top_tabs - display tabs with rounded edges
962
 * INPUTS
963
 *	 $text	  - array of tabs
964
 * RESULT
965
 *	 null
966
 ******/
967
function display_top_tabs(& $tab_array, $no_drop_down = false, $type = 'pills') {
968
	global $config;
969
	global $g;
970
	global $tab_array_indent;
971
	global $tab_array_space;
972
	global $tab_array_char_limit;
973

    
974
	/*	does the user have access to this tab?
975
	 *	master user has access to everything.
976
	 *	if the user does not have access, simply
977
	 *	unset the tab item.
978
	 */
979

    
980
	/* empty string code */
981
	if ($tab_array_indent == '') {
982
		$tab_array_indent = 0;
983
	}
984

    
985
	if ($tab_array_space == '') {
986
		$tab_array_space = 1;
987
	}
988

    
989
	if ($tab_array_char_limit == '') {
990
		$tab_array_char_limit = 92;
991
	}
992

    
993
	foreach ($tab_array as $tab_id => $ta) {
994
		if (!isAllowedPage($ta[2])) {
995
			unset ($tab_array[$tab_id]);
996
		}
997
	}
998

    
999
	$tab_active_bg	 = "#EEEEEE";
1000
	$tab_inactive_bg = "#777777";
1001
	$nifty_tabs_corners = "#FFF";
1002
	$font_color = "white";
1003

    
1004
	$tabcharcount = 0;
1005
	foreach ($tab_array as $ta) {
1006
		$tabcharcount = $tabcharcount + strlen($ta[0]);
1007
	}
1008

    
1009
	if ($no_drop_down == true) {
1010
		$tabcharcount = 0;
1011
		unset($tab_array_char_limit);
1012
	}
1013

    
1014
	// If the character count of the tab names is > 670
1015
	// then show a select item dropdown menubox.
1016
	if ($tabcharcount > $tab_array_char_limit) {
1017
		echo gettext("Currently viewing: ");
1018
		echo "<select name=\"TabSelect\" onchange=\"tabs_will_go(this)\">\n";
1019

    
1020
		foreach ($tab_array as $ta) {
1021
			if ($ta[1] == "true") {
1022
				$selected = " selected";
1023
			} else {
1024
				$selected = "";
1025
			}
1026
			// Onclick in option will not work in some browser
1027
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
1028
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
1029
		}
1030

    
1031
		echo "</select>\n<p>&nbsp;</p>";
1032
		echo "<script type=\"text/javascript\">";
1033
		echo "\n//<![CDATA[\n";
1034
		echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
1035
		echo "//]]>\n";
1036
		echo "</script>";
1037
	} else {
1038
		echo '<ul class="nav nav-' . $type . '">';
1039

    
1040
		foreach ($tab_array as $ta) {
1041
			echo '<li role="presentation"';
1042
			if ($ta[1]) {
1043
				echo ' class="active"';
1044
			}
1045

    
1046
			echo '><a href="' . $ta[2] . '">' . $ta[0] . '</a></li>';
1047
		}
1048

    
1049
		echo '</ul>';
1050
	}
1051
}
1052

    
1053
function add_package_tabs($tabgroup, &$tab_array) {
1054
	global $config, $g;
1055

    
1056
	if (!isset($config['installedpackages']['package'])) {
1057
		return;
1058
	}
1059

    
1060
	foreach ($config['installedpackages']['package'] as $pkg) {
1061
		$pkg_config = read_package_configurationfile($pkg['name']);
1062

    
1063
		if (!isset($pkg_config['tabs']['tab'])) {
1064
			continue;
1065
		}
1066

    
1067
		foreach ($pkg_config['tabs']['tab'] as $tab) {
1068
			$tab_entry = array();
1069
			if ($tab['name']) {
1070
				$tab_entry[] = $tab['name'];
1071
				$tab_entry[] = false;
1072
				$tab_entry[] = $tab['url'];
1073
				$tab_array[] = $tab_entry;
1074
			}
1075
		}
1076
	}
1077
}
1078

    
1079
function alias_info_popup($alias_id) {
1080
	global $config;
1081

    
1082
	if (!is_array($config['aliases']['alias'][$alias_id])) {
1083
		return;
1084
	}
1085

    
1086
	$maxlength = 60;
1087
	$alias = $config['aliases']['alias'][$alias_id];
1088
	$content = "";
1089

    
1090
	if ($alias['url']) {
1091
		// TODO: Change it when pf supports tables with ports
1092
		if ($alias['type'] == "urltable") {
1093
			exec("/sbin/pfctl -t {$alias['name']} -T show | wc -l", $total_entries);
1094
			$counter=preg_replace("/\D/", "", $total_entries[0]);
1095
			exec("/sbin/pfctl -t {$alias['name']} -T show | head -10002", $alias_addresses);
1096
		} else {
1097
			$urlfn = alias_expand_urltable($alias['name']);
1098
			$alias_addresses = explode("\n", file_get_contents($urlfn));
1099
			$counter = count($alias_addresses);
1100
		}
1101

    
1102
		$content .= '<h5>'. $alias['url'] .'</h5><ul><li>'. implode('</li><li>', $alias_addresses) .'</li></ul>';
1103
		if ($counter > 10002) {
1104
			$content .= '<i>'. gettext("listing only first 10k items") .'</i>';
1105
		}
1106
	} else {
1107
		$alias_addresses = explode (" ", $alias['address']);
1108
		$alias_details = explode ("||", $alias['detail']);
1109
		$idx = 0;
1110

    
1111
		$content .= "<table>\n";
1112
		$content .= "<thead>\n";
1113
		$content .= "<tr>\n";
1114
		$content .= "<th>" . gettext("Value") . "</th><th  style='padding-left: 10px;'>" . gettext("Description") . "</th></tr>\n";
1115
		$content .= "</thead>\n";
1116
		$content .= "<tbody>\n";
1117

    
1118
		foreach ($alias_addresses as $ap) {
1119
			$content .= "	<tr>\n";
1120
			$content .= "		<td>\n";
1121
			$content .= 			$ap;
1122
			$content .=	"		</td>\n";
1123
			$content .= "		<td style='padding-left: 10px;'>\n";
1124
			$content .= 			$alias_details[$idx];
1125
			$content .=	"		</td>\n";
1126
			$content .= "	</tr>\n";
1127
			$idx++;
1128
		}
1129

    
1130
		$content .= "</tbody>\n";
1131
		$content .= "<table>\n";
1132
	}
1133

    
1134
	if (strlen($alias['descr']) >= $maxlength) {
1135
		$alias['descr'] = substr($alias['descr'], 0, $maxlength) . '&hellip;';
1136
	}
1137

    
1138
	return $content;
1139
}
1140

    
1141
function rule_columns_with_alias($src, $srcport, $dst, $dstport) {
1142
	global $config;
1143

    
1144
	if ($config['aliases']['alias'] == "" || !is_array($config['aliases']['alias'])) {
1145
		return;
1146
	}
1147

    
1148
	$columns = array();
1149
	foreach ($config['aliases']['alias'] as $alias_id => $alias_name) {
1150
		if ($alias_name['name'] == $src) {
1151
			$columns['src'] = $alias_id;
1152
		}
1153
		if ($alias_name['name'] == $srcport) {
1154
			$columns['srcport'] = $alias_id;
1155
		}
1156
		if ($alias_name['name'] == $dst) {
1157
			$columns['dst'] = $alias_id;
1158
		}
1159
		if ($alias_name['name'] == $dstport) {
1160
			$columns['dstport'] = $alias_id;
1161
		}
1162
	}
1163

    
1164
	return $columns;
1165
}
1166

    
1167
function form_output_row($name, $label, $content) {
1168
var_dump($content);die;
1169
?>
1170
<div class="form-group">
1171
	<label for="<?=$name?>" class="col-sm-2 control-label"><?=gettext($label); ?></label>
1172
	<div class="col-sm-10">
1173
		<?=$content?>
1174
	</div>
1175
</div>
1176
<?php
1177
}
1178

    
1179
function set_flash_message($class, $msg) {
1180
	@session_start();
1181
	$_SESSION['flash_messages'][$class][] = $msg;
1182
}
1183

    
1184
function get_flash_message() {
1185
	@session_start();
1186
	if (isset($_SESSION['flash_messages']) && !empty($_SESSION['flash_messages'])) {
1187
		foreach ($_SESSION['flash_messages'] as $class => $flash_message) {
1188
			print_info_box_np(implode("<br />", $flash_message), null, null, false, $class);
1189
		}
1190
		unset($_SESSION['flash_messages']);
1191
	}
1192
}
1193

    
1194
/* Retrieve GET or POST Value/State
1195
 * Eample Usage:
1196
 * $value = getGETPOSTsettingvalue('get/post parameter name', "");
1197
 * $value = getGETPOSTsettingvalue('get/post parameter name', null);
1198
 * $state = getGETPOSTsettingvalue('get/post parameter name', null);
1199
 * $state = getGETPOSTsettingvalue('get/post parameter name', false);
1200
 */
1201
function getGETPOSTsettingvalue($settingname, $default) {
1202
	$settingvalue = $default;
1203
	if ($_GET[$settingname]) {
1204
		$settingvalue = $_GET[$settingname];
1205
	}
1206
	if ($_POST[$settingname]) {
1207
		$settingvalue = $_POST[$settingname];
1208
	}
1209
	return $settingvalue;
1210
}
1211

    
1212
/* set timezone */
1213
if (isset($config['system']['timezone']) &&
1214
    !empty($config['system']['timezone'])) {
1215
	$timezone = $config['system']['timezone'];
1216
} elseif (isset($g['default_timezone']) && !empty($g['default_timezone'])) {
1217
	$timezone = $g['default_timezone'];
1218
} else {
1219
	$timezone = "Etc/UTC";
1220
}
1221

    
1222
date_default_timezone_set($timezone);
1223

    
1224
?>
(64-64/229)