Project

General

Profile

Download (33.9 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
function clear_log_file($logfile = "/var/log/system.log", $restart_syslogd = true) {
559
	global $config, $g;
560
	if ($restart_syslogd) {
561
		exec("/usr/bin/killall syslogd");
562
	}
563
	if (isset($config['system']['disablesyslogclog'])) {
564
		unlink($logfile);
565
		touch($logfile);
566
	} else {
567
		$log_size = isset($config['syslog']['logfilesize']) ? $config['syslog']['logfilesize'] : "511488";
568
		$log_size = isset($config['syslog'][basename($logfile, '.log') . '_settings']['logfilesize']) ? $config['syslog'][basename($logfile, '.log') . '_settings']['logfilesize'] : $log_size;
569
		if (isset($config['system']['usefifolog'])) {
570
			exec("/usr/sbin/fifolog_create -s {$log_size} " . escapeshellarg($logfile));
571
		} else {
572
			exec("/usr/local/sbin/clog -i -s {$log_size} " . escapeshellarg($logfile));
573
		}
574
	}
575
	if ($restart_syslogd) {
576
		system_syslogd_start();
577
	}
578
}
579

    
580
function clear_all_log_files() {
581
	global $g;
582
	exec("/usr/bin/killall syslogd");
583

    
584
	$log_files = array("system", "filter", "dhcpd", "vpn", "pptps", "poes", "l2tps", "openvpn", "portalauth", "ipsec", "ppp", "relayd", "wireless", "lighttpd", "ntpd", "gateways", "resolver", "routing");
585
	foreach ($log_files as $lfile) {
586
		clear_log_file("{$g['varlog_path']}/{$lfile}.log", false);
587
	}
588

    
589
	system_syslogd_start();
590
	killbyname("dhcpd");
591
	services_dhcpd_configure();
592
	return;
593
}
594

    
595
// This version of dump_clog() does not output <td></td> or any other table elements.
596
function dump_clog_no_table($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
597
	global $g, $config;
598
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
599
	$specific_log = basename($logfile, '.log') . '_settings';
600
	if ($config['syslog'][$specific_log]['cronorder'] == 'forward') $sor = "";
601
	if ($config['syslog'][$specific_log]['cronorder'] == 'reverse') $sor = "-r";
602
	$logarr = "";
603
	$grepline = "  ";
604
	if (is_array($grepfor)) {
605
		$grepline .= " | /usr/bin/egrep " . escapeshellarg(implode("|", $grepfor));
606
	}
607
	if (is_array($grepinvert)) {
608
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
609
	}
610
	if (is_dir($logfile)) {
611
		$logarr = array("File $logfile is a directory.");
612
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
613
		$logarr = array("Log file started.");
614
	} else {
615
		if ($config['system']['disablesyslogclog']) {
616
			exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
617
		} else {
618
			if (isset($config['system']['usefifolog'])) {
619
				exec("/usr/sbin/fifolog_reader " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
620
			} else {
621
				exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
622
			}
623
		}
624
	}
625
	echo "\n";
626

    
627
	$rows = 0;
628
	foreach ($logarr as $logent) {
629
		$rows++;
630
		$logent = preg_split("/\s+/", $logent, 6);
631

    
632
		if ($withorig) {
633
				if (isset($config['system']['usefifolog'])) {
634
					$entry_date_time = htmlspecialchars(date("F j, Y, g:i a", "" . $logent[1] . ""));
635
					$entry_text = htmlspecialchars($logent[5]);
636
				} else {
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
				}
641
				echo "{$entry_date_time}";
642
				echo " " . "{$entry_text}"	. "\n";
643
		} else {
644
				echo htmlspecialchars($logent[5]) . "\n";
645
		}
646

    
647
	}
648
	return($rows);
649
}
650

    
651
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
652
	global $g, $config;
653
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
654
	$specific_log = basename($logfile, '.log') . '_settings';
655
	if ($config['syslog'][$specific_log]['cronorder'] == 'forward') $sor = "";
656
	if ($config['syslog'][$specific_log]['cronorder'] == 'reverse') $sor = "-r";
657
	$logarr = "";
658
	$grepline = "  ";
659
	if (is_array($grepfor)) {
660
		$grepline .= " | /usr/bin/egrep " . escapeshellarg(implode("|", $grepfor));
661
	}
662
	if (is_array($grepinvert)) {
663
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
664
	}
665
	if (is_dir($logfile)) {
666
		$logarr = array("File $logfile is a directory.");
667
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
668
		$logarr = array("Log file started.");
669
	} else {
670
		if ($config['system']['disablesyslogclog']) {
671
			exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
672
		} else {
673
			if (isset($config['system']['usefifolog'])) {
674
				exec("/usr/sbin/fifolog_reader " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
675
			} else {
676
				exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
677
			}
678
		}
679
	}
680

    
681
	$rows = 0;
682
	foreach ($logarr as $logent) {
683
		$rows++;
684
		$logent = preg_split("/\s+/", $logent, 6);
685
		echo "<tr>\n";
686
		if ($withorig) {
687
			if (isset($config['system']['usefifolog'])) {
688
				$entry_date_time = htmlspecialchars(date("F j, Y, g:i a", "" . $logent[1] . ""));
689
				$entry_text = htmlspecialchars($logent[5]);
690
			} else {
691
				$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
692
				$entry_text = ($logent[3] == $config['system']['hostname']) ? "" : $logent[3] . " ";
693
				$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
694
			}
695
			echo "<td style=\"white-space:nowrap;\">{$entry_date_time}</td>\n";
696
			echo "<td style=\"word-wrap:break-word; word-break:break-all; white-space:normal\">{$entry_text}</td>\n";
697
		} else {
698
				echo "<td>" . htmlspecialchars($logent[5]) . "</td>\n";
699
		}
700
		echo "</tr>\n";
701
	}
702
	return($rows);
703
}
704

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

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

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

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

    
776
	if (isset($adr['not'])) {
777
		$pnot = 1;
778
	} else {
779
		$pnot = 0;
780
	}
781

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

    
793
function pconfig_to_address(&$adr, $padr, $pmask, $pnot = false, $pbeginport = 0, $pendport = 0) {
794
	$adr = array();
795

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

    
813
	if ($pnot) {
814
		$adr['not'] = true;
815
	} else {
816
		unset($adr['not']);
817
	}
818

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

    
827
	if (is_alias($pbeginport)) {
828
		$adr['port'] = $pbeginport;
829
	}
830
}
831

    
832
function is_specialnet($net) {
833
	global $specialsrcdst;
834

    
835
	if (!$net) {
836
		return false;
837
	}
838
	if (in_array($net, $specialsrcdst)) {
839
		return true;
840
	} else {
841
		return false;
842
	}
843
}
844

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

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

    
874

    
875
// Return inline javascript file or CSS to minimize
876
// request count going back to server.
877
function outputJavaScriptFileInline($javascript) {
878
	if (file_exists($javascript)) {
879
		echo "\n<script type=\"text/javascript\">\n";
880
		include($javascript);
881
		echo "\n</script>\n";
882
	} else {
883
		echo "\n\n<!-- Could not locate file:  {$javascript} -->\n\n";
884
	}
885
}
886

    
887

    
888

    
889
function outputCSSPrintFileInline($css) {
890
	if (file_exists($css)) {
891
		echo "\n<style media=\"print\" type=\"text/css\">\n";
892
		include($css);
893
		echo "\n</style>\n";
894
	} else {
895
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
896
	}
897
}
898

    
899

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

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

    
954
function is_rfc2616_code($code) {
955
	global $rfc2616;
956
	if (isset($rfc2616[$code])) {
957
		return true;
958
	} else {
959
		return false;
960
	}
961
}
962

    
963
function print_rfc2616_select($tag, $current) {
964
	global $rfc2616;
965

    
966
	/* Default to 200 OK if not set */
967
	if ($current == "") {
968
		$current = 200;
969
	}
970

    
971
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
972
	foreach ($rfc2616 as $code => $message) {
973
		if ($code == $current) {
974
			$sel = " selected";
975
		} else {
976
			$sel = "";
977
		}
978
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
979
	}
980
	echo "</select>\n";
981
}
982

    
983
// Useful debugging function, much cleaner than print_r
984
function echo_array($array, $return_me = false) {
985
	if (is_array($array) == false) {
986
		$return = "The provided variable is not an array.";
987
	} else {
988
		foreach ($array as $name=>$value) {
989
			if (is_array($value)) {
990
				$return .= "";
991
				$return .= "['<b>$name</b>'] {<div style=\"margin-left:10px;\">\n";
992
				$return .= echo_array($value, true);
993
				$return .= "</div>}";
994
				$return .= "\n\n";
995
			} else {
996
				if (is_string($value)) {
997
					$value = "\"$value\"";
998
				}
999
				$return .= "['<b>$name</b>'] = $value\n\n";
1000
			}
1001
		}
1002
	}
1003
	if ($return_me == true) {
1004
		return $return;
1005
	} else {
1006
		echo "<pre>".$return."</pre>";
1007
	}
1008
}
1009

    
1010
/****f* pfsense-utils/display_top_tabs
1011
 * NAME
1012
 *	 display_top_tabs - display tabs with rounded edges
1013
 * INPUTS
1014
 *	 $text	  - array of tabs
1015
 * RESULT
1016
 *	 null
1017
 ******/
1018
function display_top_tabs(& $tab_array, $no_drop_down = false, $type = 'pills') {
1019
	global $config;
1020
	global $g;
1021
	global $tab_array_indent;
1022
	global $tab_array_space;
1023
	global $tab_array_char_limit;
1024

    
1025
	/*	does the user have access to this tab?
1026
	 *	master user has access to everything.
1027
	 *	if the user does not have access, simply
1028
	 *	unset the tab item.
1029
	 */
1030

    
1031
	/* empty string code */
1032
	if ($tab_array_indent == '') {
1033
		$tab_array_indent = 0;
1034
	}
1035

    
1036
	if ($tab_array_space == '') {
1037
		$tab_array_space = 1;
1038
	}
1039

    
1040
	if ($tab_array_char_limit == '') {
1041
		$tab_array_char_limit = 92;
1042
	}
1043

    
1044
	foreach ($tab_array as $tab_id => $ta) {
1045
		if (!isAllowedPage($ta[2])) {
1046
			unset ($tab_array[$tab_id]);
1047
		}
1048
	}
1049

    
1050
	$tab_active_bg	 = "#EEEEEE";
1051
	$tab_inactive_bg = "#777777";
1052
	$nifty_tabs_corners = "#FFF";
1053
	$font_color = "white";
1054

    
1055
	$tabcharcount = 0;
1056
	foreach ($tab_array as $ta) {
1057
		$tabcharcount = $tabcharcount + strlen($ta[0]);
1058
	}
1059

    
1060
	if ($no_drop_down == true) {
1061
		$tabcharcount = 0;
1062
		unset($tab_array_char_limit);
1063
	}
1064

    
1065
	// If the character count of the tab names is > 670
1066
	// then show a select item dropdown menubox.
1067
	if ($tabcharcount > $tab_array_char_limit) {
1068
		echo gettext("Currently viewing: ");
1069
		echo "<select name=\"TabSelect\" onchange=\"tabs_will_go(this)\">\n";
1070

    
1071
		foreach ($tab_array as $ta) {
1072
			if ($ta[1] == "true") {
1073
				$selected = " selected";
1074
			} else {
1075
				$selected = "";
1076
			}
1077
			// Onclick in option will not work in some browser
1078
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
1079
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
1080
		}
1081

    
1082
		echo "</select>\n<p>&nbsp;</p>";
1083
		echo "<script type=\"text/javascript\">";
1084
		echo "\n//<![CDATA[\n";
1085
		echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
1086
		echo "//]]>\n";
1087
		echo "</script>";
1088
	} else {
1089
		echo '<ul class="nav nav-' . $type . '">';
1090

    
1091
		foreach ($tab_array as $ta) {
1092
			echo '<li role="presentation"';
1093
			if ($ta[1]) {
1094
				echo ' class="active"';
1095
			}
1096

    
1097
			echo '><a href="' . $ta[2] . '">' . $ta[0] . '</a></li>';
1098
		}
1099

    
1100
		echo '</ul>';
1101
	}
1102
}
1103

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

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

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

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

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

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

    
1133
	if (!is_array($config['aliases']['alias'][$alias_id])) {
1134
		return;
1135
	}
1136

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

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

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

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

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

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

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

    
1189
	return $content;
1190
}
1191

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

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

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

    
1215
	return $columns;
1216
}
1217

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

    
1230
function set_flash_message($class, $msg) {
1231
	@session_start();
1232
	$_SESSION['flash_messages'][$class][] = $msg;
1233
}
1234

    
1235
function get_flash_message() {
1236
	@session_start();
1237
	if (isset($_SESSION['flash_messages']) && !empty($_SESSION['flash_messages'])) {
1238
		foreach ($_SESSION['flash_messages'] as $class => $flash_message) {
1239
			print_info_box_np(implode("<br />", $flash_message), null, null, false, $class);
1240
		}
1241
		unset($_SESSION['flash_messages']);
1242
	}
1243
}
1244

    
1245
/* set timezone */
1246
if (isset($config['system']['timezone']) &&
1247
    !empty($config['system']['timezone'])) {
1248
	$timezone = $config['system']['timezone'];
1249
} elseif (isset($g['default_timezone']) && !empty($g['default_timezone'])) {
1250
	$timezone = $g['default_timezone'];
1251
} else {
1252
	$timezone = "Etc/UTC";
1253
}
1254

    
1255
date_default_timezone_set($timezone);
1256

    
1257
?>
(64-64/228)