Project

General

Profile

Download (33.3 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
	pfSense_MODULE: base
60
*/
61

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

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

    
81
header("X-Frame-Options: SAMEORIGIN");
82
require_once("authgui.inc");
83

    
84
/* parse the configuration and include all configuration functions */
85
require_once("functions.inc");
86

    
87
/* Pull in all the gui related display classes) */
88
foreach (scandir("/usr/local/www/classes/") as $file) {
89
	if (substr($file, -4) == ".inc") {
90
		require_once("classes/{$file}");
91
	}
92
}
93

    
94
/* Set the default interface language */
95
if ($config['system']['language'] <> "") {
96
	$g['language'] = $config['system']['language'];
97
} elseif ($g['language'] == "") {
98
	$g['language'] = 'en_US';
99
}
100

    
101
set_language($g['language']);
102

    
103
/* used by progress bar */
104
$lastseen = "-1";
105

    
106
$navlevelsep = ": ";	/* navigation level separator string */
107
$mandfldhtml = "";		/* display this before mandatory input fields */
108
$mandfldhtmlspc = "";	/* same as above, but with spacing */
109

    
110
/* Some ajax scripts still need access to GUI */
111
if (!$ignorefirmwarelock) {
112
	if (is_subsystem_dirty('firmwarelock')) {
113
		if (!$d_isfwfile) {
114
			header("Location: system_update.php");
115
			exit;
116
		} else {
117
			return;
118
		}
119
	}
120
}
121

    
122
/* Reserved table names to avoid collision */
123
$reserved_table_names = array(
124
	"bogons",
125
	"bogonsv6",
126
	"negate_networks",
127
	"snort2c",
128
	"sshlockout",
129
	"tonatsubnets",
130
	"virusprot",
131
	"vpn_networks",
132
	"webConfiguratorlockout"
133
);
134

    
135
$firewall_rules_dscp_types = array(
136
	"af11",
137
	"af12",
138
	"af13",
139
	"af21",
140
	"af22",
141
	"af23",
142
	"af31",
143
	"af32",
144
	"af33",
145
	"af41",
146
	"af42",
147
	"af43",
148
	"VA",
149
	"EF",
150
	"cs1",
151
	"cs2",
152
	"cs3",
153
	"cs4",
154
	"cs5",
155
	"cs6",
156
	"cs7",
157
	"0x01",
158
	"0x02",
159
	"0x04");
160

    
161
$auth_server_types = array(
162
	'ldap' => "LDAP",
163
	'radius' => "Radius");
164

    
165
$ldap_urltypes = array(
166
	'TCP - Standard' => 389,
167
	'SSL - Encrypted' => 636);
168

    
169
$ldap_scopes = array(
170
	'one' => "One Level",
171
	'subtree' => "Entire Subtree");
172

    
173
$ldap_protvers = array(
174
	2,
175
	3);
176

    
177
$ldap_templates = array(
178

    
179
	'open' => array(
180
		'desc' => "OpenLDAP",
181
		'attr_user' => "cn",
182
		'attr_group' => "cn",
183
		'attr_member' => "member"),
184

    
185
	'msad' => array(
186
		'desc' => "Microsoft AD",
187
		'attr_user' => "samAccountName",
188
		'attr_group' => "cn",
189
		'attr_member' => "memberOf"),
190

    
191
	'edir' => array(
192
		'desc' => "Novell eDirectory",
193
		'attr_user' => "cn",
194
		'attr_group' => "cn",
195
		'attr_member' => "uniqueMember"));
196

    
197
$radius_srvcs = array(
198
	'both' => "Authentication and Accounting",
199
	'auth' => "Authentication",
200
	'acct' => "Accounting");
201

    
202
$netbios_nodetypes = array(
203
	'0' => "none",
204
	'1' => "b-node",
205
	'2' => "p-node",
206
	'4' => "m-node",
207
	'8' => "h-node");
208

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

    
256
/* TCP flags */
257
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg", "ece", "cwr");
258

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

    
261
$spiflist = get_configured_interface_with_descr(false, true);
262
foreach ($spiflist as $ifgui => $ifdesc) {
263
	$specialnets[$ifgui] = $ifdesc . " net";
264
	$specialnets[$ifgui . 'ip'] = $ifdesc . " address";
265
}
266

    
267
$medias = array(
268
	"auto" => "autoselect",
269
	"100full" => "100BASE-TX full-duplex",
270
	"100half" => "100BASE-TX half-duplex",
271
	"10full" => "10BASE-T full-duplex",
272
	"10half" => "10BASE-T half-duplex");
273

    
274
$wlan_modes = array(
275
	"bss" => "Infrastructure (BSS)",
276
	"adhoc" => "Ad-hoc (IBSS)",
277
	"hostap" => "Access Point");
278

    
279
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, &$input_errors) {
280

    
281
	/* check for bad control characters */
282
	foreach ($postdata as $pn => $pd) {
283
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
284
			$input_errors[] = sprintf(gettext("The field %s contains invalid characters."), $pn);
285
		}
286
	}
287

    
288
	for ($i = 0; $i < count($reqdfields); $i++) {
289
		if ($_POST[$reqdfields[$i]] == "" && $_REQUEST[$reqdfields[$i]] == "") {
290
			$input_errors[] = sprintf(gettext("The field %s is required."), $reqdfieldsn[$i]);
291
		}
292
	}
293
}
294

    
295
function print_input_errors($input_errors) {
296
	echo '<div class="alert alert-danger input-errors">';
297
	echo '<p>' . gettext('The following input errors were detected:') . '</p>';
298
	echo '<ul>';
299

    
300
	foreach ($input_errors as $ierr) {
301
		echo '<li>' . htmlspecialchars($ierr) . '</li>';
302
	}
303

    
304
	echo '</ul>';
305
	echo '</div>';
306
}
307

    
308
function verify_gzip_file($fname) {
309
	$returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
310
	if ($returnvar != 0) {
311
		return 0;
312
	} else {
313
		return 1;
314
	}
315
}
316

    
317
function print_info_box_np($msg, $name="apply", $value="", $showapply=false, $class="alert-warning") {
318
	global $g;
319

    
320
	if (strpos($class, "alert-") !== 0)
321
		$class = 'alert-' . $class;
322

    
323
	if (empty($value)) {
324
		$value = gettext("Apply changes");
325
	}
326

    
327
	$msg = '<div class="pull-left">' . $msg . '</div>';
328

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

    
332
		if ($_POST['if'])
333
			$msg .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
334

    
335
		$msg .= '</form>';
336
	} else
337
		$msg = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'. $msg;
338

    
339
	echo '<div class="alert ' . $class . ' clearfix" role="alert">'.$msg.'</div>';
340
}
341

    
342
function print_info_box_np_undo($msg, $name = "apply", $value = "Apply changes", $undo) {
343
	global $g;
344

    
345
	if (stristr($msg, "apply") != false || stristr($msg, "save") != false || stristr($msg, "create") != false) {
346
		$savebutton = "<td class=\"infoboxsave nowrap\">";
347
		$savebutton .= "<input type=\"button\" value=\"". gettext("Undo") . "\" onclick=\"document.location='{$undo}'\" />";
348
		$savebutton .= "<input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\" />";
349
		$savebutton .= "</td>";
350
		if ($_POST['if']) {
351
			$savebutton .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
352
		}
353
	}
354

    
355
	$nifty_redbox = "#990000";
356
	$nifty_blackbox = "#000000";
357

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

    
362
	echo <<<EOFnp
363
	<table class="infobox" id="redboxtable" summary="red box table">
364
		<tr>
365
			<td>
366
				<div class="infoboxnp" id="redbox">
367
					<table class="infoboxnptable2" summary="message">
368
						<tr>
369
							<td class="infoboxnptd">
370
								&nbsp;&nbsp;&nbsp;<i class="fa fa-exclamation-circle"></i>
371
							</td>
372
							<td class="infoboxnptd2">
373
								<b>{$msg}</b>
374
							</td>
375
							{$savebutton}
376
							{$undobutton}
377
						</tr>
378
					</table>
379
				</div>
380
				<div>
381
					<p>&nbsp;</p>
382
				</div>
383
			</td>
384
		</tr>
385
	</table>
386
EOFnp;
387

    
388
}
389

    
390
function print_info_box($msg, $class="alert-warning") {
391
	print_info_box_np($msg, null, null, false, $class);
392

    
393
}
394

    
395
function get_std_save_message($ok) {
396
	global $d_sysrebootreqd_path;
397
	$filter_related = false;
398
	$filter_pages = array("nat", "filter");
399
	$to_return = gettext("The changes have been applied successfully.");
400
	foreach ($filter_pages as $fp) {
401
		if (stristr($_SERVER['SCRIPT_FILENAME'], $fp)) {
402
			$filter_related = true;
403
		}
404
	}
405
	if ($filter_related) {
406
		$to_return .= "<br />" . gettext("You can also <a href=\"status_filter_reload.php\">monitor</a> the filter reload progress.");
407
	}
408
	return $to_return;
409
}
410

    
411
function pprint_address($adr) {
412
	global $specialnets;
413

    
414
	if (isset($adr['any'])) {
415
		$padr = "*";
416
	} else if ($adr['network']) {
417
		$padr = $specialnets[$adr['network']];
418
	} else {
419
		$padr = $adr['address'];
420
	}
421

    
422
	if (isset($adr['not'])) {
423
		$padr = "! " . $padr;
424
	}
425

    
426
	return $padr;
427
}
428

    
429
function pprint_port($port) {
430
	global $wkports;
431

    
432
	$pport = "";
433

    
434
	if (!$port) {
435
		return "*";
436
	} else {
437
		$srcport = explode("-", $port);
438
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
439
			$pport = $srcport[0];
440
			if ($wkports[$srcport[0]]) {
441
				$pport .= " (" . $wkports[$srcport[0]] . ")";
442
			}
443
		} else {
444
			$pport .= $srcport[0] . " - " . $srcport[1];
445
		}
446
	}
447

    
448
	return $pport;
449
}
450

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

    
526
	return $item_set;
527
}
528

    
529
function gentitle($title) {
530
	global $navlevelsep;
531
	if (!is_array($title)) {
532
		return $title;
533
	} else {
534
		return join($navlevelsep, $title);
535
	}
536
}
537

    
538
function genhtmltitle($title) {
539
	if (!is_array($title))
540
		return '<h1 class="page-header"><a href="">' . $title . '</a></h1>';
541

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

    
544
	// If the array contains only one element, there are no breadcrumbs, so don't
545
	// add anything else
546
	if(count($title) > 1) {
547
		$bc = '<ol class="breadcrumb">';
548

    
549
		foreach ($title as $el)
550
			$bc .= '<li>'.$el.'</li>';
551

    
552
		$bc .= '</ol>';
553
	} else {
554
		$bc = "";
555
	}
556

    
557
	return $heading . $bc;
558
}
559

    
560
/* update the changedesc and changecount(er) variables */
561
function update_changedesc($update) {
562
	global $changedesc;
563
	global $changecount;
564

    
565
	$changedesc .= " {$update}";
566
	$changecount++;
567
}
568

    
569
function clear_log_file($logfile = "/var/log/system.log", $restart_syslogd = true) {
570
	global $config, $g;
571
	if ($restart_syslogd) {
572
		exec("/usr/bin/killall syslogd");
573
	}
574
	if (isset($config['system']['disablesyslogclog'])) {
575
		unlink($logfile);
576
		touch($logfile);
577
	} else {
578
		$log_size = isset($config['syslog']['logfilesize']) ? $config['syslog']['logfilesize'] : "511488";
579
		if (isset($config['system']['usefifolog'])) {
580
			exec("/usr/sbin/fifolog_create -s {$log_size} " . escapeshellarg($logfile));
581
		} else {
582
			exec("/usr/local/sbin/clog -i -s {$log_size} " . escapeshellarg($logfile));
583
		}
584
	}
585
	if ($restart_syslogd) {
586
		system_syslogd_start();
587
	}
588
}
589

    
590
function clear_all_log_files() {
591
	global $g;
592
	exec("/usr/bin/killall syslogd");
593

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

    
599
	system_syslogd_start();
600
	killbyname("dhcpd");
601
	services_dhcpd_configure();
602
	return;
603
}
604

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

    
631
	$rows = 0;
632
	foreach ($logarr as $logent) {
633
		$rows++;
634
		$logent = preg_split("/\s+/", $logent, 6);
635

    
636
		if ($withorig) {
637
				if (isset($config['system']['usefifolog'])) {
638
					$entry_date_time = htmlspecialchars(date("F j, Y, g:i a","" . $logent[1] . ""));
639
					$entry_text = htmlspecialchars($logent[5]);
640
				} else {
641
					$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
642
					$entry_text = ($logent[3] ==  $config['system']['hostname']) ? "" : $logent[3] . " ";
643
					$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
644
				}
645
				echo "{$entry_date_time}";
646
				echo " " . "{$entry_text}"	. "\n";
647
		} else {
648
				echo htmlspecialchars($logent[5]) . "\n";
649
		}
650

    
651
	}
652
	return($rows);
653
}
654

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

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

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

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

    
745
	} else {
746
		if ($orig != $new) {
747
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
748
			$orig = $new;
749
			return true;
750
		}
751
	}
752
	return false;
753
}
754

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

    
771
	if (isset($adr['not'])) {
772
		$pnot = 1;
773
	} else {
774
		$pnot = 0;
775
	}
776

    
777
	if ($adr['port']) {
778
		list($pbeginport, $pendport) = explode("-", $adr['port']);
779
		if (!$pendport) {
780
			$pendport = $pbeginport;
781
		}
782
	} else if (!is_alias($pbeginport) && !is_alias($pendport)) {
783
		$pbeginport = "any";
784
		$pendport = "any";
785
	}
786
}
787

    
788
function pconfig_to_address(&$adr, $padr, $pmask, $pnot = false, $pbeginport = 0, $pendport = 0) {
789
	$adr = array();
790

    
791
	if ($padr == "any") {
792
		$adr['any'] = true;
793
	} else if (is_specialnet($padr)) {
794
		$adr['network'] = $padr;
795
	} else {
796
		$adr['address'] = $padr;
797
		if (is_ipaddrv6($padr)) {
798
			if ($pmask != 128) {
799
				$adr['address'] .= "/" . $pmask;
800
			}
801
		} else {
802
			if ($pmask != 32) {
803
				$adr['address'] .= "/" . $pmask;
804
			}
805
		}
806
	}
807

    
808
	if ($pnot) {
809
		$adr['not'] = true;
810
	} else {
811
		unset($adr['not']);
812
	}
813

    
814
	if (($pbeginport != 0) && ($pbeginport != "any")) {
815
		if ($pbeginport != $pendport) {
816
			$adr['port'] = $pbeginport . "-" . $pendport;
817
		} else {
818
			$adr['port'] = $pbeginport;
819
		}
820
	}
821

    
822
	if (is_alias($pbeginport)) {
823
		$adr['port'] = $pbeginport;
824
	}
825
}
826

    
827
function is_specialnet($net) {
828
	global $specialsrcdst;
829

    
830
	if (!$net) {
831
		return false;
832
	}
833
	if (in_array($net, $specialsrcdst)) {
834
		return true;
835
	} else {
836
		return false;
837
	}
838
}
839

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

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

    
869

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

    
882

    
883

    
884
function outputCSSPrintFileInline($css) {
885
	if (file_exists($css)) {
886
		echo "\n<style media=\"print\" type=\"text/css\">\n";
887
		include($css);
888
		echo "\n</style>\n";
889
	} else {
890
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
891
	}
892
}
893

    
894

    
895
function outputCSSFileInline($css) {
896
	if (file_exists($css)) {
897
		echo "\n<style type=\"text/css\">\n";
898
		include($css);
899
		echo "\n</style>\n";
900
	} else {
901
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
902
	}
903
}
904

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

    
949
function is_rfc2616_code($code) {
950
	global $rfc2616;
951
	if (isset($rfc2616[$code])) {
952
		return true;
953
	} else {
954
		return false;
955
	}
956
}
957

    
958
function print_rfc2616_select($tag, $current) {
959
	global $rfc2616;
960

    
961
	/* Default to 200 OK if not set */
962
	if ($current == "") {
963
		$current = 200;
964
	}
965

    
966
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
967
	foreach ($rfc2616 as $code => $message) {
968
		if ($code == $current) {
969
			$sel = " selected=\"selected\"";
970
		} else {
971
			$sel = "";
972
		}
973
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
974
	}
975
	echo "</select>\n";
976
}
977

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

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

    
1020
	/*	does the user have access to this tab?
1021
	 *	master user has access to everything.
1022
	 *	if the user does not have access, simply
1023
	 *	unset the tab item.
1024
	 */
1025

    
1026
	/* empty string code */
1027
	if ($tab_array_indent == '') {
1028
		$tab_array_indent = 0;
1029
	}
1030

    
1031
	if ($tab_array_space == '') {
1032
		$tab_array_space = 1;
1033
	}
1034

    
1035
	if ($tab_array_char_limit == '') {
1036
		$tab_array_char_limit = 92;
1037
	}
1038

    
1039
	foreach ($tab_array as $tab_id => $ta) {
1040
		if (!isAllowedPage($ta[2])) {
1041
			unset ($tab_array[$tab_id]);
1042
		}
1043
	}
1044

    
1045
	$tab_active_bg	 = "#EEEEEE";
1046
	$tab_inactive_bg = "#777777";
1047
	$nifty_tabs_corners = "#FFF";
1048
	$font_color = "white";
1049

    
1050
	$tabcharcount = 0;
1051
	foreach ($tab_array as $ta)
1052
		$tabcharcount = $tabcharcount + strlen($ta[0]);
1053

    
1054
	if ($no_drop_down == true) {
1055
		$tabcharcount = 0;
1056
		unset($tab_array_char_limit);
1057
	}
1058

    
1059
	// If the character count of the tab names is > 670
1060
	// then show a select item dropdown menubox.
1061
	if ($tabcharcount > $tab_array_char_limit) {
1062
		echo gettext("Currently viewing: ");
1063
		echo "<select name=\"TabSelect\" onchange=\"tabs_will_go(this)\">\n";
1064

    
1065
		foreach ($tab_array as $ta) {
1066
			if ($ta[1] == "true")
1067
				$selected = " selected=\"selected\"";
1068
			else
1069
				$selected = "";
1070
			// Onclick in option will not work in some browser
1071
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
1072
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
1073
		}
1074

    
1075
		echo "</select>\n<p>&nbsp;</p>";
1076
		echo "<script type=\"text/javascript\">";
1077
		echo "\n//<![CDATA[\n";
1078
		echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
1079
		echo "//]]>\n";
1080
		echo "</script>";
1081
	} else {
1082
		echo '<ul class="nav nav-' . $type . '">';
1083

    
1084
		foreach ($tab_array as $ta) {
1085
			echo '<li role="presentation"';
1086
			if ($ta[1]) {
1087
				echo ' class="active"';
1088
			}
1089

    
1090
			echo '><a href="' . $ta[2] . '">' . $ta[0] . '</a></li>';
1091
		}
1092

    
1093
		echo '</ul>';
1094
	}
1095
}
1096

    
1097
function add_package_tabs($tabgroup, &$tab_array) {
1098
	global $config, $g;
1099

    
1100
	if (!isset($config['installedpackages']['package'])) {
1101
		return;
1102
	}
1103

    
1104
	foreach ($config['installedpackages']['package'] as $pkg){
1105
		$pkg_config = read_package_configurationfile($pkg['name']);
1106

    
1107
		if (!isset($pkg_config['tabs']['tab'])) {
1108
			continue;
1109
		}
1110

    
1111
		foreach ($pkg_config['tabs']['tab'] as $tab) {
1112
			$tab_entry = array();
1113
			if ($tab['name']) {
1114
				$tab_entry[] = $tab['name'];
1115
				$tab_entry[] = false;
1116
				$tab_entry[] = $tab['url'];
1117
				$tab_array[] = $tab_entry;
1118
			}
1119
		}
1120
	}
1121
}
1122

    
1123
function alias_info_popup($alias_id) {
1124
	global $config;
1125

    
1126
	if (!is_array($config['aliases']['alias'][$alias_id]))
1127
		return;
1128

    
1129
	$maxlength = 60;
1130
	$alias = $config['aliases']['alias'][$alias_id];
1131
	$content = "";
1132

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

    
1146
		$content .= '<h5>'. $alias['url'] .'</h5><ul><li>'. implode('</li><li>', $alias_ports_address) .'</li></ul>';
1147

    
1148
		if ($counter > 10002)
1149
			$content .= '<i>'. gettext("listing only first 10k items") .'</i>';
1150
	}
1151
	else
1152
	{
1153
		$alias_addresses = explode (" ", $alias['address']);
1154
		$alias_details = explode ("||", $alias['detail']);
1155
		$idx = 0;
1156

    
1157
		$content .= "<table>\n";
1158
		$content .= "<thead>\n";
1159
		$content .= "<tr>\n";
1160
		$content .= "<th>" . gettext("Value") . "</th><th  style='padding-left: 10px;'>" . gettext("Description") . "</th></tr>\n";
1161
		$content .= "</thead>\n";
1162
		$content .= "<tbody>\n";
1163

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

    
1176
		$content .= "</tbody>\n";
1177
		$content .= "<table>\n";
1178
	}
1179

    
1180
	if (strlen($alias['descr']) >= $maxlength)
1181
		$alias['descr'] = substr($alias['descr'], 0, $maxlength) . '&hellip;';
1182

    
1183
	return $content;
1184
}
1185

    
1186
function rule_columns_with_alias($src, $srcport, $dst, $dstport) {
1187
	global $config;
1188

    
1189
	if ($config['aliases']['alias'] == "" || !is_array($config['aliases']['alias']))
1190
		return;
1191

    
1192
	$columns = array();
1193
	foreach ($config['aliases']['alias'] as $alias_id => $alias_name) {
1194
		if ($alias_name['name'] == $src)
1195
			$columns['src'] = $alias_id;
1196
		if ($alias_name['name'] == $srcport)
1197
			$columns['srcport'] = $alias_id;
1198
		if ($alias_name['name'] == $dst )
1199
			$columns['dst'] = $alias_id;
1200
		if ($alias_name['name'] == $dstport)
1201
			$columns['dstport'] = $alias_id;
1202
	}
1203

    
1204
	return $columns;
1205
}
1206

    
1207
function form_output_row($name, $label, $content) {
1208
var_dump($content);die;
1209
?>
1210
<div class="form-group">
1211
	<label for="<?=$name?>" class="col-sm-2 control-label"><?=gettext($label); ?></label>
1212
	<div class="col-sm-10">
1213
		<?=$content?>
1214
	</div>
1215
</div>
1216
<?php
1217
}
1218

    
1219
function set_flash_message($class, $msg) {
1220
	@session_start();
1221
	$_SESSION['flash_messages'][$class][] = $msg;
1222
}
1223

    
1224
function get_flash_message() {
1225
	@session_start();
1226
	if (isset($_SESSION['flash_messages']) && !empty($_SESSION['flash_messages'])) {
1227
		foreach ($_SESSION['flash_messages'] as $class => $flash_message) {
1228
			print_info_box_np(implode("<br />", $flash_message), null, null, false, $class);
1229
		}
1230
		unset($_SESSION['flash_messages']);
1231
	}
1232
}
1233

    
1234
/* set timezone */
1235
if (isset($config['system']['timezone']) &&
1236
    !empty($config['system']['timezone'])) {
1237
	$timezone = $config['system']['timezone'];
1238
} elseif (isset($g['default_timezone']) && !empty($g['default_timezone'])) {
1239
	$timezone = $g['default_timezone'];
1240
} else {
1241
	$timezone = "Etc/UTC";
1242
}
1243

    
1244
date_default_timezone_set($timezone);
1245

    
1246
?>
(74-74/228)