Project

General

Profile

Download (33.1 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
 *	Copyright (c)  2004, 2005 Scott Ullrich
8
 *
9
 *	Redistribution and use in source and binary forms, with or without modification,
10
 *	are permitted provided that the following conditions are met:
11
 *
12
 *	1. Redistributions of source code must retain the above copyright notice,
13
 *		this list of conditions and the following disclaimer.
14
 *
15
 *	2. Redistributions in binary form must reproduce the above copyright
16
 *		notice, this list of conditions and the following disclaimer in
17
 *		the documentation and/or other materials provided with the
18
 *		distribution.
19
 *
20
 *	3. All advertising materials mentioning features or use of this software
21
 *		must display the following acknowledgment:
22
 *		"This product includes software developed by the pfSense Project
23
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
24
 *
25
 *	4. The names "pfSense" and "pfSense Project" must not be used to
26
 *		 endorse or promote products derived from this software without
27
 *		 prior written permission. For written permission, please contact
28
 *		 coreteam@pfsense.org.
29
 *
30
 *	5. Products derived from this software may not be called "pfSense"
31
 *		nor may "pfSense" appear in their names without prior written
32
 *		permission of the Electric Sheep Fencing, LLC.
33
 *
34
 *	6. Redistributions of any form whatsoever must retain the following
35
 *		acknowledgment:
36
 *
37
 *	"This product includes software developed by the pfSense Project
38
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
39
 *
40
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
41
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
44
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
52
 *
53
 *	====================================================================
54
 *
55
 */
56
/*
57
	pfSense_MODULE: base
58
*/
59

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

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

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

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

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

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

    
99
set_language($g['language']);
100

    
101
/* used by progress bar */
102
$lastseen = "-1";
103

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

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

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

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

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

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

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

    
171
$ldap_protvers = array(
172
	2,
173
	3);
174

    
175
$ldap_templates = array(
176

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

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

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

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

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

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

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

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

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

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

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

    
277
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, &$input_errors) {
278

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

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

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

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

    
302
	echo '</ul>';
303
	echo '</div>';
304
}
305

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

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

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

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

    
325
	$msg = '<div class="pull-left">' . $msg . '</div>';
326

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

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

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

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

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

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

    
353
	$nifty_redbox = "#990000";
354
	$nifty_blackbox = "#000000";
355

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

    
360
	echo <<<EOFnp
361
	<table class="infobox" id="redboxtable" summary="red box table">
362
		<tr>
363
			<td>
364
				<div class="infoboxnp" id="redbox">
365
					<table class="infoboxnptable2" summary="message">
366
						<tr>
367
							<td class="infoboxnptd">
368
								&nbsp;&nbsp;&nbsp;<i class="icon-large icon-exclamation-sign"></i>
369
							</td>
370
							<td class="infoboxnptd2">
371
								<b>{$msg}</b>
372
							</td>
373
							{$savebutton}
374
							{$undobutton}
375
						</tr>
376
					</table>
377
				</div>
378
				<div>
379
					<p>&nbsp;</p>
380
				</div>
381
			</td>
382
		</tr>
383
	</table>
384
	<script type="text/javascript">
385
	//<![CDATA[
386
		Rounded("div#redbox","all","#FFF","{$nifty_redbox}","smooth");
387
		Rounded("td#blackbox","all","#FFF","{$nifty_blackbox}","smooth");
388
	//]]>
389
	</script>
390
EOFnp;
391

    
392
}
393

    
394
function print_info_box($msg, $class="alert-warning") {
395
	print_info_box_np($msg, null, null, false, $class);
396

    
397
}
398

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

    
415
function pprint_address($adr) {
416
	global $specialnets;
417

    
418
	if (isset($adr['any'])) {
419
		$padr = "*";
420
	} else if ($adr['network']) {
421
		$padr = $specialnets[$adr['network']];
422
	} else {
423
		$padr = $adr['address'];
424
	}
425

    
426
	if (isset($adr['not'])) {
427
		$padr = "! " . $padr;
428
	}
429

    
430
	return $padr;
431
}
432

    
433
function pprint_port($port) {
434
	global $wkports;
435

    
436
	$pport = "";
437

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

    
452
	return $pport;
453
}
454

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

    
533
	return $item_set;
534
}
535

    
536
function gentitle($title) {
537
	global $navlevelsep;
538
	if (!is_array($title)) {
539
		return $title;
540
	} else {
541
		return join($navlevelsep, $title);
542
	}
543
}
544

    
545
function genhtmltitle($title) {
546
	if (!is_array($title))
547
		return '<h1 class="page-header"><a href="">' . $title . '</a></h1>';
548

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

    
551
	// If the array contains only one element, there are no breadcrumbs, so don't
552
	// add anything else
553
	if(count($title) > 1) {
554
		$bc = '<ol class="breadcrumb">';
555

    
556
		foreach ($title as $el)
557
			$bc .= '<li>'.$el.'</li>';
558

    
559
		$bc .= '</ol>';
560
	} else {
561
		$bc = "";
562
	}
563

    
564
	return $heading . $bc;
565
}
566

    
567
/* update the changedesc and changecount(er) variables */
568
function update_changedesc($update) {
569
	global $changedesc;
570
	global $changecount;
571

    
572
	$changedesc .= " {$update}";
573
	$changecount++;
574
}
575

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

    
597
function clear_all_log_files() {
598
	global $g;
599
	exec("/usr/bin/killall syslogd");
600

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

    
606
	system_syslogd_start();
607
	killbyname("dhcpd");
608
	services_dhcpd_configure();
609
	return;
610
}
611

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

    
638
	$rows = 0;
639
	foreach ($logarr as $logent) {
640
		$rows++;
641
		$logent = preg_split("/\s+/", $logent, 6);
642

    
643
		if ($withorig) {
644
				if (isset($config['system']['usefifolog'])) {
645
					$entry_date_time = htmlspecialchars(date("F j, Y, g:i a","" . $logent[1] . ""));
646
					$entry_text = htmlspecialchars($logent[5]);
647
				} else {
648
					$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
649
					$entry_text = ($logent[3] ==  $config['system']['hostname']) ? "" : $logent[3] . " ";
650
					$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
651
				}
652
				echo "{$entry_date_time}";
653
				echo " " . "{$entry_text}"	. "\n";
654
		} else {
655
				echo htmlspecialchars($logent[5]) . "\n";
656
		}
657

    
658
	}
659
	return($rows);
660
}
661

    
662
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
663
	global $g, $config;
664
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
665
	$logarr = "";
666
	$grepline = "  ";
667
	if (is_array($grepfor)) {
668
		$grepline .= " | /usr/bin/egrep " . escapeshellarg(implode("|", $grepfor));
669
	}
670
	if (is_array($grepinvert)) {
671
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
672
	}
673
	if (is_dir($logfile)) {
674
		$logarr = array("File $logfile is a directory.");
675
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
676
		$logarr = array("Log file started.");
677
	} else {
678
		if ($config['system']['disablesyslogclog']) {
679
			exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
680
		} else {
681
			if (isset($config['system']['usefifolog'])) {
682
				exec("/usr/sbin/fifolog_reader " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
683
			} else {
684
				exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
685
			}
686
		}
687
	}
688
	foreach ($logarr as $logent) {
689
		$logent = preg_split("/\s+/", $logent, 6);
690
		echo "<tr valign=\"top\">\n";
691
		if ($withorig) {
692
			if (isset($config['system']['usefifolog'])) {
693
				$entry_date_time = htmlspecialchars(date("F j, Y, g:i a", "" . $logent[1] . ""));
694
				$entry_text = htmlspecialchars($logent[5]);
695
			} else {
696
				$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
697
				$entry_text = ($logent[3] == $config['system']['hostname']) ? "" : $logent[3] . " ";
698
				$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
699
			}
700
			echo "<td class=\"listlr nowrap\">{$entry_date_time}</td>\n";
701
			echo "<td class=\"listr\">{$entry_text}</td>\n";
702
		} else {
703
				echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
704
		}
705
		echo "</tr>\n";
706
	}
707
}
708

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
873
	echo "<script type=\"text/javascript\">";
874
	echo "\n//<![CDATA[\n";
875
	echo "Rounded(\"div.{$tabclass}-tabactive\",\"top\",\"#CCCCCC\",\"#EEEEEE\",\"smooth\");\n";
876
	echo "Rounded(\"div.{$tabclass}-tabdeactive\",\"top\",\"#CCCCCC\",\"#777777\",\"smooth\");\n";
877
	echo "//]]>\n";
878
	echo "</script>";
879
	echo "</div>";
880
}
881

    
882

    
883
// Return inline javascript file or CSS to minimize
884
// request count going back to server.
885
function outputJavaScriptFileInline($javascript) {
886
	if (file_exists($javascript)) {
887
		echo "\n<script type=\"text/javascript\">\n";
888
		include($javascript);
889
		echo "\n</script>\n";
890
	} else {
891
		echo "\n\n<!-- Could not locate file:  {$javascript} -->\n\n";
892
	}
893
}
894

    
895

    
896

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

    
907

    
908
function outputCSSFileInline($css) {
909
	if (file_exists($css)) {
910
		echo "\n<style type=\"text/css\">\n";
911
		include($css);
912
		echo "\n</style>\n";
913
	} else {
914
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
915
	}
916
}
917

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

    
962
function is_rfc2616_code($code) {
963
	global $rfc2616;
964
	if (isset($rfc2616[$code])) {
965
		return true;
966
	} else {
967
		return false;
968
	}
969
}
970

    
971
function print_rfc2616_select($tag, $current) {
972
	global $rfc2616;
973

    
974
	/* Default to 200 OK if not set */
975
	if ($current == "") {
976
		$current = 200;
977
	}
978

    
979
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
980
	foreach ($rfc2616 as $code => $message) {
981
		if ($code == $current) {
982
			$sel = " selected=\"selected\"";
983
		} else {
984
			$sel = "";
985
		}
986
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
987
	}
988
	echo "</select>\n";
989
}
990

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

    
1018
/****f* pfsense-utils/display_top_tabs
1019
 * NAME
1020
 *	 display_top_tabs - display tabs with rounded edges
1021
 * INPUTS
1022
 *	 $text	  - array of tabs
1023
 * RESULT
1024
 *	 null
1025
 ******/
1026
function display_top_tabs(& $tab_array, $no_drop_down = false, $type = 'pills') {
1027
	global $config;
1028
	global $g;
1029
	global $tab_array_indent;
1030
	global $tab_array_space;
1031
	global $tab_array_char_limit;
1032

    
1033
	/*	does the user have access to this tab?
1034
	 *	master user has access to everything.
1035
	 *	if the user does not have access, simply
1036
	 *	unset the tab item.
1037
	 */
1038

    
1039
	/* empty string code */
1040
	if ($tab_array_indent == '') {
1041
		$tab_array_indent = 0;
1042
	}
1043

    
1044
	if ($tab_array_space == '') {
1045
		$tab_array_space = 1;
1046
	}
1047

    
1048
	if ($tab_array_char_limit == '') {
1049
		$tab_array_char_limit = 92;
1050
	}
1051

    
1052
	foreach ($tab_array as $tab_id => $ta) {
1053
		if (!isAllowedPage($ta[2])) {
1054
			unset ($tab_array[$tab_id]);
1055
		}
1056
	}
1057

    
1058
	$tab_active_bg	 = "#EEEEEE";
1059
	$tab_inactive_bg = "#777777";
1060
	$nifty_tabs_corners = "#FFF";
1061
	$font_color = "white";
1062

    
1063
	$tabcharcount = 0;
1064
	foreach ($tab_array as $ta)
1065
		$tabcharcount = $tabcharcount + strlen($ta[0]);
1066

    
1067
	if ($no_drop_down == true) {
1068
		$tabcharcount = 0;
1069
		unset($tab_array_char_limit);
1070
	}
1071

    
1072
	// If the character count of the tab names is > 670
1073
	// then show a select item dropdown menubox.
1074
	if ($tabcharcount > $tab_array_char_limit): ?>
1075
		echo gettext("Currently viewing: ");
1076
		echo "<select name=\"TabSelect\" onchange=\"tabs_will_go(this)\">\n";
1077
		foreach ($tab_array as $ta) {
1078
			if ($ta[1] == "true")
1079
				$selected = " selected=\"selected\"";
1080
			else
1081
				$selected = "";
1082
			// Onclick in option will not work in some browser
1083
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
1084
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
1085
		}
1086
		echo "</select>\n<p>&nbsp;</p>";
1087
		echo "<script type=\"text/javascript\">";
1088
		echo "\n//<![CDATA[\n";
1089
		echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
1090
		echo "//]]>\n";
1091
		echo "</script>";
1092
<? else: ?>
1093
	<ul class="nav nav-<?= $type ?>">
1094
		<?php foreach ($tab_array as $ta): ?>
1095
			<li role="presentation"<?=($ta[1] ? ' class="active"' : '')?>><a href="<?=$ta[2]?>"><?=$ta[0]?></a></li>
1096
		<?php endforeach; ?>
1097
	</ul>
1098
<?php endif;
1099
}
1100

    
1101
function add_package_tabs($tabgroup, &$tab_array) {
1102
	global $config, $g;
1103

    
1104
	if (!isset($config['installedpackages']['package'])) {
1105
		return;
1106
	}
1107

    
1108
	foreach ($config['installedpackages']['package'] as $pkg){
1109
		$pkg_config = read_package_configurationfile($pkg['name']);
1110

    
1111
		if (!isset($pkg_config['tabs']['tab'])) {
1112
			continue;
1113
		}
1114

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

    
1127
function alias_info_popup($alias_id) {
1128
	global $config;
1129

    
1130
	if (!is_array($config['aliases']['alias'][$alias_id]))
1131
		return;
1132

    
1133
	$maxlength = 60;
1134
	$alias = $config['aliases']['alias'][$alias_id];
1135
	$content = "";
1136

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

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

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

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

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

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

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

    
1187
	return $content;
1188
}
1189

    
1190
function rule_columns_with_alias($src, $srcport, $dst, $dstport) {
1191
	global $config;
1192

    
1193
	if ($config['aliases']['alias'] == "" || !is_array($config['aliases']['alias']))
1194
		return;
1195

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

    
1208
	return $columns;
1209
}
1210

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

    
1223
$timezone = $config['system']['timezone'];
1224
if (!$timezone) {
1225
	$timezone = "Etc/UTC";
1226
}
1227

    
1228
date_default_timezone_set($timezone);
1229

    
1230
?>
(76-76/233)