Project

General

Profile

Download (33.2 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
$g['theme'] = get_current_theme();
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_firmware.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
	$nifty_redbox = "#990000";
355
	$nifty_blackbox = "#000000";
356

    
357
	$themename = $g['theme'];
358

    
359
	if (file_exists("/usr/local/www/themes/{$themename}/tabcontrols.php")) {
360
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/tabcontrols.php");
361
		eval($toeval);
362
	}
363

    
364
	if (file_exists("/usr/local/www/themes/{$themename}/infobox.php")) {
365
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/infobox.php");
366
		eval($toeval);
367
	}
368

    
369

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

    
374
	echo <<<EOFnp
375
	<table class="infobox" id="redboxtable" summary="red box table">
376
		<tr>
377
			<td>
378
				<div class="infoboxnp" id="redbox">
379
					<table class="infoboxnptable2" summary="message">
380
						<tr>
381
							<td class="infoboxnptd">
382
								&nbsp;&nbsp;&nbsp;<img class="infoboxnpimg" src="/themes/{$g['theme']}/images/icons/icon_exclam.gif" alt="exclamation" />
383
							</td>
384
							<td class="infoboxnptd2">
385
								<b>{$msg}</b>
386
							</td>
387
							{$savebutton}
388
							{$undobutton}
389
						</tr>
390
					</table>
391
				</div>
392
				<div>
393
					<p>&nbsp;</p>
394
				</div>
395
			</td>
396
		</tr>
397
	</table>
398
	<script type="text/javascript">
399
	//<![CDATA[
400
		Rounded("div#redbox","all","#FFF","{$nifty_redbox}","smooth");
401
		Rounded("td#blackbox","all","#FFF","{$nifty_blackbox}","smooth");
402
	//]]>
403
	</script>
404
EOFnp;
405

    
406
}
407

    
408
function print_info_box($msg, $class="alert-warning") {
409
	print_info_box_np($msg, null, null, false, $class);
410

    
411
}
412

    
413
function get_std_save_message($ok) {
414
	global $d_sysrebootreqd_path;
415
	$filter_related = false;
416
	$filter_pages = array("nat", "filter");
417
	$to_return = gettext("The changes have been applied successfully.");
418
	foreach ($filter_pages as $fp) {
419
		if (stristr($_SERVER['SCRIPT_FILENAME'], $fp)) {
420
			$filter_related = true;
421
		}
422
	}
423
	if ($filter_related) {
424
		$to_return .= "<br />" . gettext("You can also <a href=\"status_filter_reload.php\">monitor</a> the filter reload progress.");
425
	}
426
	return $to_return;
427
}
428

    
429
function pprint_address($adr) {
430
	global $specialnets;
431

    
432
	if (isset($adr['any'])) {
433
		$padr = "*";
434
	} else if ($adr['network']) {
435
		$padr = $specialnets[$adr['network']];
436
	} else {
437
		$padr = $adr['address'];
438
	}
439

    
440
	if (isset($adr['not'])) {
441
		$padr = "! " . $padr;
442
	}
443

    
444
	return $padr;
445
}
446

    
447
function pprint_port($port) {
448
	global $wkports;
449

    
450
	$pport = "";
451

    
452
	if (!$port) {
453
		return "*";
454
	} else {
455
		$srcport = explode("-", $port);
456
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
457
			$pport = $srcport[0];
458
			if ($wkports[$srcport[0]]) {
459
				$pport .= " (" . $wkports[$srcport[0]] . ")";
460
			}
461
		} else {
462
			$pport .= $srcport[0] . " - " . $srcport[1];
463
		}
464
	}
465

    
466
	return $pport;
467
}
468

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

    
547
	return $item_set;
548
}
549

    
550
function gentitle($title) {
551
	global $navlevelsep;
552
	if (!is_array($title)) {
553
		return $title;
554
	} else {
555
		return join($navlevelsep, $title);
556
	}
557
}
558

    
559
function genhtmltitle($title) {
560
	if (!is_array($title))
561
		return '<h1 class="page-header"><a href="">' . $title . '</a></h1>';
562

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

    
565
	// If the array contains only one element, there are no breadcrumbs, so don't
566
	// add anything else
567
	if(count($title) > 1) {
568
		$bc = '<ol class="breadcrumb">';
569

    
570
		foreach ($title as $el)
571
			$bc .= '<li>'.$el.'</li>';
572

    
573
		$bc .= '</ol>';
574
	} else {
575
		$bc = "";
576
	}
577

    
578
	return $heading . $bc;
579
}
580

    
581
/* update the changedesc and changecount(er) variables */
582
function update_changedesc($update) {
583
	global $changedesc;
584
	global $changecount;
585

    
586
	$changedesc .= " {$update}";
587
	$changecount++;
588
}
589

    
590
function clear_log_file($logfile = "/var/log/system.log", $restart_syslogd = true) {
591
	global $config, $g;
592
	if ($restart_syslogd) {
593
		exec("/usr/bin/killall syslogd");
594
	}
595
	if (isset($config['system']['disablesyslogclog'])) {
596
		unlink($logfile);
597
		touch($logfile);
598
	} else {
599
		$log_size = isset($config['syslog']['logfilesize']) ? $config['syslog']['logfilesize'] : "511488";
600
		if (isset($config['system']['usefifolog'])) {
601
			exec("/usr/sbin/fifolog_create -s {$log_size} " . escapeshellarg($logfile));
602
		} else {
603
			exec("/usr/local/sbin/clog -i -s {$log_size} " . escapeshellarg($logfile));
604
		}
605
	}
606
	if ($restart_syslogd) {
607
		system_syslogd_start();
608
	}
609
}
610

    
611
function clear_all_log_files() {
612
	global $g;
613
	exec("/usr/bin/killall syslogd");
614

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

    
620
	system_syslogd_start();
621
	killbyname("dhcpd");
622
	services_dhcpd_configure();
623
	return;
624
}
625

    
626
// This version of dump_clog() does not output <td></td> or any other table elements. It can be renamed
627
// and the dump_clog() removed once all of the diag_log*.php files have been converted to Bootstrap
628
function dump_clog_no_table($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
629
	global $g, $config;
630
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
631
	$logarr = "";
632
	$grepline = "  ";
633
	if (is_array($grepfor))
634
		$grepline .= " | /usr/bin/egrep " . escapeshellarg(implode("|", $grepfor));
635
	if (is_array($grepinvert))
636
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
637
	if (is_dir($logfile)) {
638
		$logarr = array("File $logfile is a directory.");
639
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
640
		$logarr = array("Log file started.");
641
	} else {
642
		if ($config['system']['disablesyslogclog']) {
643
			exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
644
		} else {
645
			if (isset($config['system']['usefifolog']))
646
				exec("/usr/sbin/fifolog_reader " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
647
			else
648
				exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
649
		}
650
	}
651
	echo "\n";
652

    
653
	$rows = 0;
654
	foreach ($logarr as $logent) {
655
		$rows++;
656
		$logent = preg_split("/\s+/", $logent, 6);
657

    
658
		if ($withorig) {
659
				if (isset($config['system']['usefifolog'])) {
660
					$entry_date_time = htmlspecialchars(date("F j, Y, g:i a","" . $logent[1] . ""));
661
					$entry_text = htmlspecialchars($logent[5]);
662
				} else {
663
					$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
664
					$entry_text = ($logent[3] ==  $config['system']['hostname']) ? "" : $logent[3] . " ";
665
					$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
666
				}
667
				echo "{$entry_date_time}";
668
				echo " " . "{$entry_text}"	. "\n";
669
		} else {
670
				echo htmlspecialchars($logent[5]) . "\n";
671
		}
672

    
673
	}
674
	return($rows);
675
}
676

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

    
724
function return_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
725
	global $g, $config;
726
	$sor = (isset($config['syslog']['reverse']) || $grepreverse) ? "-r" : "";
727
	$logarr = "";
728
	$grepline = "  ";
729
	if (is_array($grepfor)) {
730
		$grepline .= " | /usr/bin/egrep " . escapeshellarg(implode("|", $grepfor));
731
	}
732
	if (is_array($grepinvert)) {
733
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
734
	}
735
	if ($config['system']['disablesyslogclog']) {
736
		exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
737
	} else {
738
		if (isset($config['system']['usefifolog'])) {
739
			exec("/usr/sbin/fifolog_reader " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
740
		} else {
741
			exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
742
		}
743
	}
744
	return($logarr);
745
}
746

    
747
/* Check if variable has changed, update and log if it has
748
 * returns true if var changed
749
 * varname = variable name in plain text
750
 * orig = original value
751
 * new = new value
752
 */
753
function update_if_changed($varname, & $orig, $new) {
754
	if (is_array($orig) && is_array($new)) {
755
		$a_diff = array_diff($orig, $new);
756
		foreach ($a_diff as $diff) {
757
			update_changedesc("removed {$varname}: \"{$diff}\"");
758
		}
759
		$a_diff = array_diff($new, $orig);
760
		foreach ($a_diff as $diff) {
761
			update_changedesc("added {$varname}: \"{$diff}\"");
762
		}
763
		$orig = $new;
764
		return true;
765

    
766
	} else {
767
		if ($orig != $new) {
768
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
769
			$orig = $new;
770
			return true;
771
		}
772
	}
773
	return false;
774
}
775

    
776
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
777
	if (isset($adr['any'])) {
778
		$padr = "any";
779
	} else if ($adr['network']) {
780
		$padr = $adr['network'];
781
	} else if ($adr['address']) {
782
		list($padr, $pmask) = explode("/", $adr['address']);
783
		if (!$pmask) {
784
			if (is_ipaddrv6($padr)) {
785
				$pmask = 128;
786
			} else {
787
				$pmask = 32;
788
			}
789
		}
790
	}
791

    
792
	if (isset($adr['not'])) {
793
		$pnot = 1;
794
	} else {
795
		$pnot = 0;
796
	}
797

    
798
	if ($adr['port']) {
799
		list($pbeginport, $pendport) = explode("-", $adr['port']);
800
		if (!$pendport) {
801
			$pendport = $pbeginport;
802
		}
803
	} else if (!is_alias($pbeginport) && !is_alias($pendport)) {
804
		$pbeginport = "any";
805
		$pendport = "any";
806
	}
807
}
808

    
809
function pconfig_to_address(&$adr, $padr, $pmask, $pnot = false, $pbeginport = 0, $pendport = 0) {
810
	$adr = array();
811

    
812
	if ($padr == "any") {
813
		$adr['any'] = true;
814
	} else if (is_specialnet($padr)) {
815
		$adr['network'] = $padr;
816
	} else {
817
		$adr['address'] = $padr;
818
		if (is_ipaddrv6($padr)) {
819
			if ($pmask != 128) {
820
				$adr['address'] .= "/" . $pmask;
821
			}
822
		} else {
823
			if ($pmask != 32) {
824
				$adr['address'] .= "/" . $pmask;
825
			}
826
		}
827
	}
828

    
829
	if ($pnot) {
830
		$adr['not'] = true;
831
	} else {
832
		unset($adr['not']);
833
	}
834

    
835
	if (($pbeginport != 0) && ($pbeginport != "any")) {
836
		if ($pbeginport != $pendport) {
837
			$adr['port'] = $pbeginport . "-" . $pendport;
838
		} else {
839
			$adr['port'] = $pbeginport;
840
		}
841
	}
842

    
843
	if (is_alias($pbeginport)) {
844
		$adr['port'] = $pbeginport;
845
	}
846
}
847

    
848
function is_specialnet($net) {
849
	global $specialsrcdst;
850

    
851
	if (!$net) {
852
		return false;
853
	}
854
	if (in_array($net, $specialsrcdst)) {
855
		return true;
856
	} else {
857
		return false;
858
	}
859
}
860

    
861
//function to create widget tabs when called
862
function display_widget_tabs(& $tab_array) {
863
	echo "<div id=\"tabs\">";
864
	$tabscounter = 0;
865
	foreach ($tab_array as $ta) {
866
		$dashpos = strpos($ta[2], '-');
867
		$tabname = $ta[2] . "-tab";
868
		$tabclass = substr($ta[2], 0, $dashpos);
869
		$tabclass = $tabclass . "-class";
870
		if ($ta[1] == true) {
871
			$tabActive = "table-cell";
872
			$tabNonActive = "none";
873
		} else {
874
			$tabActive = "none";
875
			$tabNonActive = "table-cell";
876
		}
877
		echo "<div id=\"{$ta[2]}-active\" class=\"{$tabclass}-tabactive\" style=\"display:{$tabActive}; background-color:#EEEEEE; color:black;\">";
878
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
879
		echo "&nbsp;&nbsp;&nbsp;</b>";
880
		echo "</div>";
881

    
882
		echo "<div id=\"{$ta[2]}-deactive\" class=\"{$tabclass}-tabdeactive\" style=\"display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;\" onclick=\"return changeTabDIV('{$ta[2]}')\">";
883
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
884
		echo "&nbsp;&nbsp;&nbsp;</b>";
885
		echo "</div>";
886
	}
887

    
888
	echo "<script type=\"text/javascript\">";
889
	echo "\n//<![CDATA[\n";
890
	echo "Rounded(\"div.{$tabclass}-tabactive\",\"top\",\"#CCCCCC\",\"#EEEEEE\",\"smooth\");\n";
891
	echo "Rounded(\"div.{$tabclass}-tabdeactive\",\"top\",\"#CCCCCC\",\"#777777\",\"smooth\");\n";
892
	echo "//]]>\n";
893
	echo "</script>";
894
	echo "</div>";
895
}
896

    
897

    
898
// Return inline javascript file or CSS to minimize
899
// request count going back to server.
900
function outputJavaScriptFileInline($javascript) {
901
	if (file_exists($javascript)) {
902
		echo "\n<script type=\"text/javascript\">\n";
903
		include($javascript);
904
		echo "\n</script>\n";
905
	} else {
906
		echo "\n\n<!-- Could not locate file:  {$javascript} -->\n\n";
907
	}
908
}
909

    
910

    
911

    
912
function outputCSSPrintFileInline($css) {
913
	if (file_exists($css)) {
914
		echo "\n<style media=\"print\" type=\"text/css\">\n";
915
		include($css);
916
		echo "\n</style>\n";
917
	} else {
918
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
919
	}
920
}
921

    
922

    
923
function outputCSSFileInline($css) {
924
	if (file_exists($css)) {
925
		echo "\n<style type=\"text/css\">\n";
926
		include($css);
927
		echo "\n</style>\n";
928
	} else {
929
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
930
	}
931
}
932

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

    
977
function is_rfc2616_code($code) {
978
	global $rfc2616;
979
	if (isset($rfc2616[$code])) {
980
		return true;
981
	} else {
982
		return false;
983
	}
984
}
985

    
986
function print_rfc2616_select($tag, $current) {
987
	global $rfc2616;
988

    
989
	/* Default to 200 OK if not set */
990
	if ($current == "") {
991
		$current = 200;
992
	}
993

    
994
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
995
	foreach ($rfc2616 as $code => $message) {
996
		if ($code == $current) {
997
			$sel = " selected=\"selected\"";
998
		} else {
999
			$sel = "";
1000
		}
1001
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
1002
	}
1003
	echo "</select>\n";
1004
}
1005

    
1006
// Useful debugging function, much cleaner than print_r
1007
function echo_array($array, $return_me = false) {
1008
	if (is_array($array) == false) {
1009
		$return = "The provided variable is not an array.";
1010
	} else {
1011
		foreach ($array as $name=>$value) {
1012
			if (is_array($value)) {
1013
				$return .= "";
1014
				$return .= "['<b>$name</b>'] {<div style=\"margin-left:10px;\">\n";
1015
				$return .= echo_array($value, true);
1016
				$return .= "</div>}";
1017
				$return .= "\n\n";
1018
			} else {
1019
				if (is_string($value)) {
1020
					$value = "\"$value\"";
1021
				}
1022
				$return .= "['<b>$name</b>'] = $value\n\n";
1023
			}
1024
		}
1025
	}
1026
	if ($return_me == true) {
1027
		return $return;
1028
	} else {
1029
		echo "<pre>".$return."</pre>";
1030
	}
1031
}
1032

    
1033
/****f* pfsense-utils/display_top_tabs
1034
 * NAME
1035
 *	 display_top_tabs - display tabs with rounded edges
1036
 * INPUTS
1037
 *	 $text	  - array of tabs
1038
 * RESULT
1039
 *	 null
1040
 ******/
1041
function display_top_tabs(& $tab_array, $no_drop_down = false, $type = 'pills') {
1042
	global $config;
1043
	global $g;
1044
	global $tab_array_indent;
1045
	global $tab_array_space;
1046
	global $tab_array_char_limit;
1047

    
1048
	/*	does the user have access to this tab?
1049
	 *	master user has access to everything.
1050
	 *	if the user does not have access, simply
1051
	 *	unset the tab item.
1052
	 */
1053

    
1054
	/* empty string code */
1055
	if ($tab_array_indent == '') {
1056
		$tab_array_indent = 0;
1057
	}
1058

    
1059
	if ($tab_array_space == '') {
1060
		$tab_array_space = 1;
1061
	}
1062

    
1063
	if ($tab_array_char_limit == '') {
1064
		$tab_array_char_limit = 92;
1065
	}
1066

    
1067
	foreach ($tab_array as $tab_id => $ta) {
1068
		if (!isAllowedPage($ta[2])) {
1069
			unset ($tab_array[$tab_id]);
1070
		}
1071
	}
1072

    
1073
	$tab_active_bg	 = "#EEEEEE";
1074
	$tab_inactive_bg = "#777777";
1075
	$nifty_tabs_corners = "#FFF";
1076
	$font_color = "white";
1077

    
1078
	$tabcharcount = 0;
1079
	foreach ($tab_array as $ta)
1080
		$tabcharcount = $tabcharcount + strlen($ta[0]);
1081

    
1082
	if ($no_drop_down == true) {
1083
		$tabcharcount = 0;
1084
		unset($tab_array_char_limit);
1085
	}
1086

    
1087
	// If the character count of the tab names is > 670
1088
	// then show a select item dropdown menubox.
1089
	if ($tabcharcount > $tab_array_char_limit): ?>
1090
		echo gettext("Currently viewing: ");
1091
		echo "<select name=\"TabSelect\" onchange=\"tabs_will_go(this)\">\n";
1092
		foreach ($tab_array as $ta) {
1093
			if ($ta[1] == "true")
1094
				$selected = " selected=\"selected\"";
1095
			else
1096
				$selected = "";
1097
			// Onclick in option will not work in some browser
1098
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
1099
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
1100
		}
1101
		echo "</select>\n<p>&nbsp;</p>";
1102
		echo "<script type=\"text/javascript\">";
1103
		echo "\n//<![CDATA[\n";
1104
		echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
1105
		echo "//]]>\n";
1106
		echo "</script>";
1107
<? else: ?>
1108
	<ul class="nav nav-<?= $type ?>">
1109
		<?php foreach ($tab_array as $ta): ?>
1110
			<li role="presentation"<?=($ta[1] ? ' class="active"' : '')?>><a href="<?=$ta[2]?>"><?=$ta[0]?></a></li>
1111
		<?php endforeach; ?>
1112
	</ul>
1113
<?php endif;
1114
}
1115

    
1116
function add_package_tabs($pkgname, &$tab_array) {
1117
	global $config, $g;
1118

    
1119
//	$pkg = get_pkg_data($pkgname);
1120

    
1121
	if (!isset($pkg['configurationfile']) || !file_exists('/usr/local/pkg/' . $pkg['configurationfile'])) {
1122
		return;
1123
	}
1124

    
1125
	$pkg_config = parse_xml_config_pkg('/usr/local/pkg/' . $pkg['configurationfile'], "packagegui");
1126

    
1127
	if (!isset($pkg_config['tabs']['tab'])) {
1128
		return;
1129
	}
1130

    
1131
	foreach ($pkg_config['tabs']['tab'] as $tab) {
1132
		$tab_entry = array();
1133
		if ($tab['name']) {
1134
			$tab_entry[] = $tab['name'];
1135
			$tab_entry[] = false;
1136
			$tab_entry[] = $tab['url'];
1137
			$tab_array[] = $tab_entry;
1138
		}
1139
	}
1140
}
1141

    
1142
function alias_info_popup($alias_id) {
1143
	global $config;
1144

    
1145
	if (!is_array($config['aliases']['alias'][$alias_id]))
1146
		return;
1147

    
1148
	$maxlength = 60;
1149
	$alias = $config['aliases']['alias'][$alias_id];
1150
	$content = "";
1151

    
1152
	if ($alias['url'])
1153
	{
1154
		// TODO: Change it when pf supports tables with ports
1155
		if ($alias['type'] == "urltable") {
1156
			exec("/sbin/pfctl -t {$alias['name']} -T show | wc -l", $total_entries);
1157
			$counter=preg_replace("/\D/","",$total_entries[0]);
1158
			exec("/sbin/pfctl -t {$alias['name']} -T show | head -10002", $alias_addresses);
1159
		} else {
1160
			$urlfn = alias_expand_urltable($alias['name']);
1161
			$alias_addresses = explode("\n", file_get_contents($urlfn));
1162
			$counter = count($alias_addresses);
1163
		}
1164

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

    
1167
		if ($counter > 10002)
1168
			$content .= '<i>'. gettext("listing only first 10k items") .'</i>';
1169
	}
1170
	else
1171
	{
1172
		$alias_addresses = explode (" ", $alias['address']);
1173
		$alias_details = explode ("||", $alias['detail']);
1174

    
1175
		$content .= '<ul><li>'. implode('</li><li>', $alias_addresses) .'</li></ul>';
1176
	}
1177

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

    
1181
	return $content;
1182
}
1183

    
1184
function rule_columns_with_alias($src, $srcport, $dst, $dstport) {
1185
	global $config;
1186

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

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

    
1201
		return $columns;
1202
	}
1203
}
1204

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

    
1217
$timezone = $config['system']['timezone'];
1218
if (!$timezone) {
1219
	$timezone = "Etc/UTC";
1220
}
1221

    
1222
date_default_timezone_set($timezone);
1223

    
1224
?>
(77-77/235)