Project

General

Profile

Download (31.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * guiconfig.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2022 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * originally based on m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

    
28
/* Include authentication routines */
29
/* THIS MUST BE ABOVE ALL OTHER CODE */
30
header("X-Frame-Options: SAMEORIGIN");
31

    
32
/* This includes all necessary files either directly or as depenencies. */
33
require_once("authgui.inc");
34

    
35
function pfSense_csrf_callback() {
36
	include "csrf_error.php";
37
}
38

    
39
if (!$nocsrf) {
40
	function csrf_startup() {
41
		csrf_conf('rewrite-js', '/csrf/csrf-magic.js');
42
		$timeout_minutes = config_get_path('system/webgui/session_timeout', 240);
43
		csrf_conf('expires', $timeout_minutes * 60);
44
		csrf_conf('callback', 'pfSense_csrf_callback');
45
	}
46
	require_once("csrf/csrf-magic.php");
47
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
48
		phpsession_end(true);
49
	}
50
}
51

    
52
/* make sure nothing is cached */
53
if (!$omit_nocacheheaders) {
54
	header("Expires: 0");
55
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
56
	header("Cache-Control: no-cache, no-store, must-revalidate");
57
	header("Pragma: no-cache");
58
}
59

    
60
/* parse the configuration and include all configuration functions */
61
require_once("functions.inc");
62

    
63
/* Include the autoloader for all the GUI display classes */
64
require_once("classes/autoload.inc.php");
65

    
66
/* used by progress bar */
67
$lastseen = "-1";
68

    
69
$navlevelsep = ": ";	/* navigation level separator string */
70
$mandfldhtml = "";		/* display this before mandatory input fields */
71
$mandfldhtmlspc = "";	/* same as above, but with spacing */
72

    
73
if (!function_exists('set_language')) {
74
	require_once("pfsense-utils.inc");
75
}
76

    
77
set_language();
78

    
79
/* Some ajax scripts still need access to GUI */
80
if (!$ignorefirmwarelock) {
81
	if (is_subsystem_dirty('firmwarelock')) {
82
		if (!$d_isfwfile) {
83
			header("Location: system_update.php");
84
			exit;
85
		} else {
86
			return;
87
		}
88
	}
89
}
90

    
91
$firewall_rules_dscp_types = array(
92
	"af11",
93
	"af12",
94
	"af13",
95
	"af21",
96
	"af22",
97
	"af23",
98
	"af31",
99
	"af32",
100
	"af33",
101
	"af41",
102
	"af42",
103
	"af43",
104
	"VA",
105
	"EF",
106
	"cs1",
107
	"cs2",
108
	"cs3",
109
	"cs4",
110
	"cs5",
111
	"cs6",
112
	"cs7",
113
	"0x01",
114
	"0x02",
115
	"0x04");
116

    
117
$auth_server_types = array(
118
	'ldap' => "LDAP",
119
	'radius' => "RADIUS");
120

    
121
$ldap_urltypes = array(
122
	'Standard TCP' => 389,
123
	'STARTTLS Encrypted' => 389,
124
	'SSL/TLS Encrypted' => 636);
125

    
126
$ldap_scopes = array(
127
	'one' => gettext("One Level"),
128
	'subtree' => gettext("Entire Subtree"));
129

    
130
$ldap_protvers = array(
131
	2,
132
	3);
133

    
134
$ldap_templates = array(
135

    
136
	'open' => array(
137
		'desc' => "OpenLDAP",
138
		'attr_user' => "cn",
139
		'attr_group' => "cn",
140
		'attr_member' => "member",
141
		'allow_unauthenticated' => "true"),
142

    
143
	'msad' => array(
144
		'desc' => "Microsoft AD",
145
		'attr_user' => "samAccountName",
146
		'attr_group' => "cn",
147
		'attr_member' => "memberOf",
148
		'allow_unauthenticated' => "false"),
149

    
150
	'edir' => array(
151
		'desc' => "Novell eDirectory",
152
		'attr_user' => "cn",
153
		'attr_group' => "cn",
154
		'attr_member' => "uniqueMember",
155
		'allow_unauthenticated' => "false"));
156

    
157
$radius_srvcs = array(
158
	'both' => gettext("Authentication and Accounting"),
159
	'auth' => gettext("Authentication"),
160
	'acct' => gettext("Accounting"));
161

    
162
$radius_protocol = array(
163
	'PAP' => "PAP",
164
	'CHAP_MD5' => "MD5-CHAP",
165
	'MSCHAPv1' => "MS-CHAPv1",
166
	'MSCHAPv2' => "MS-CHAPv2");
167

    
168
$netbios_nodetypes = array(
169
	'0' => "none",
170
	'1' => "b-node",
171
	'2' => "p-node",
172
	'4' => "m-node",
173
	'8' => "h-node");
174

    
175
/* some well known ports */
176
$wkports = array(
177
	179 => "BGP",
178
	5999 => "CVSup",
179
	53 => "DNS",
180
	853 => "DNS over TLS",
181
	21 => "FTP",
182
	3000 => "HBCI",
183
	80 => "HTTP",
184
	443 => "HTTPS",
185
	5190 => "ICQ",
186
	113 => "IDENT/AUTH",
187
	143 => "IMAP",
188
	993 => "IMAP/S",
189
	4500 => "IPsec NAT-T",
190
	500 => "ISAKMP",
191
	1701 => "L2TP",
192
	389 => "LDAP",
193
	636 => "LDAP/S",
194
	1755 => "MMS/TCP",
195
	7000 => "MMS/UDP",
196
	445 => "MS DS",
197
	3389 => "MS RDP",
198
	1512 => "MS WINS",
199
	1863 => "MSN",
200
	119 => "NNTP",
201
	123 => "NTP",
202
	138 => "NetBIOS-DGM",
203
	137 => "NetBIOS-NS",
204
	139 => "NetBIOS-SSN",
205
	1194 => "OpenVPN",
206
	110 => "POP3",
207
	995 => "POP3/S",
208
	1723 => "PPTP",
209
	1812 => "RADIUS",
210
	1813 => "RADIUS accounting",
211
	5004 => "RTP",
212
	5060 => "SIP",
213
	25 => "SMTP",
214
	465 => "SMTP/S",
215
	161 => "SNMP",
216
	162 => "SNMP-Trap",
217
	22 => "SSH",
218
	3478 => "STUN",
219
	587 => "SUBMISSION",
220
	514 => "Syslog",
221
	3544 => "Teredo",
222
	23 => "Telnet",
223
	69 => "TFTP",
224
	5900 => "VNC");
225

    
226
/* TCP flags */
227
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg", "ece", "cwr");
228

    
229
$specialnets = array(
230
	"(self)" => gettext("This Firewall"),
231
	"pppoe" => gettext("PPPoE clients"),
232
	"l2tp" => gettext("L2TP clients"));
233

    
234
$spiflist = get_configured_interface_with_descr(true);
235
foreach ($spiflist as $ifgui => $ifdesc) {
236
	$specialnets[$ifgui] = $ifdesc . " net";
237
	$specialnets[$ifgui . 'ip'] = $ifdesc . " address";
238
}
239

    
240
$medias = array(
241
	"auto" => gettext("autoselect"),
242
	"100full" => gettext("100BASE-TX full-duplex"),
243
	"100half" => gettext("100BASE-TX half-duplex"),
244
	"10full" => gettext("10BASE-T full-duplex"),
245
	"10half" => gettext("10BASE-T half-duplex"));
246

    
247
$wlan_modes = array(
248
	"bss" => gettext("Infrastructure (BSS)"),
249
	"adhoc" => gettext("Ad-hoc (IBSS)"),
250
	"hostap" => gettext("Access Point"));
251

    
252
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, &$input_errors) {
253

    
254
	/* check for bad control characters */
255
	foreach ($postdata as $pn => $pd) {
256
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
257
			$input_errors[] = sprintf(gettext("The field %s contains invalid characters."), $pn);
258
		}
259
	}
260

    
261
	if (is_array($reqdfields)) {
262
		for ($i = 0; $i < count($reqdfields); $i++) {
263
			if ($postdata[$reqdfields[$i]] == "") {
264
				$input_errors[] = sprintf(gettext("The field %s is required."), $reqdfieldsn[$i]);
265
			}
266
		}
267
	}
268
}
269

    
270
function print_input_errors($input_errors) {
271
	echo '<div class="alert alert-danger input-errors">';
272
	echo '<p>' . gettext('The following input errors were detected:') . '</p>';
273
	echo '<ul>';
274

    
275
	foreach ($input_errors as $ierr) {
276
		echo '<li>' . htmlspecialchars($ierr) . '</li>';
277
	}
278

    
279
	echo '</ul>';
280
	echo '</div>';
281
}
282

    
283
function verify_gzip_file($fname) {
284
	$returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
285
	if ($returnvar != 0) {
286
		return 0;
287
	} else {
288
		return 1;
289
	}
290
}
291

    
292
// sprint_info_box() returns a string with a formatted informational box, it does not print the box.
293
// To format and print in one step, call print_info_box() as usual.
294
// Any required button is explicitly created, rather than relying on the detection of certain
295
// strings in the message (such as "apply"). print_info_box_np() has been exterminated.
296
// $class = the bootstrap style class (default, info, warning, success, danger)
297
// $btnname and btntext describe the optional button and its display text, the default is an 'x' Close button.
298
// Note that there is also a shortcut function print_apply_box here that creates a standard "apply" box for you.
299
// In many cases just substitute that for print_info_box_np() to easily get a warning style "Apply changes" box.
300
function sprint_info_box($msg, $class="alert-warning", $btnname = "close", $btntext = "", $btnicon = "", $btnclass = "default") {
301

    
302
	if (strpos($class, "alert-") !== 0) {
303
		$class = 'alert-' . $class;
304
	}
305

    
306
	$msg = '<div class="pull-left">' . $msg . '</div>';
307

    
308
	if ($btnname === "close") {
309
		$msg = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' . $msg;
310
	} else if ($btnname != "") {
311
		if (empty($btntext)) {
312
			$btntext = $btnname;
313
		}
314
		if (!empty($btnicon)) {
315
			$btnicon = '<i class="fa ' . $btnicon . ' icon-embed-btn"></i>';
316
		}
317

    
318
		$msg .= '<form method="post" class="pull-right"><button type="submit" class="btn btn-' . $btnclass . '" name="'. $btnname . '" value="' . $btntext . '">' . $btnicon . $btntext . '</button>';
319

    
320
		if ( isset($_POST['if']) && !empty($_POST['if'])) {
321
			$msg .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
322
		}
323

    
324
		$msg .= '</form>';
325
	}
326

    
327
	return '<div class="alert ' . $class . ' clearfix" role="alert">' . $msg . '</div>';
328
}
329

    
330
// Format and print an info box. See sprint_info_box() for details.
331
function print_info_box($msg, $class="alert-warning", $btnname = "close", $btntext = "", $btnicon = "", $btnclass = "default") {
332
	echo sprint_info_box($msg, $class, $btnname, $btntext, $btnicon, $btnclass);
333
}
334

    
335
function print_apply_box($msg) {
336
	print_info_box($msg, "warning", "apply", gettext("Apply Changes"), 'fa-check', 'success');
337
}
338

    
339
// Format and print a box reporting that changes have been applied
340
// $retval = status value from the functions called to apply the changes
341
// 0 is good
342
// non-zero is a problem
343
// $extra_text = optional extra text to display after the standard message
344
function print_apply_result_box($retval, $extra_text="") {
345
	$result_msg = get_std_save_message($retval);
346
	if ($retval === 0) {
347
		// 0 is success
348
		$severity = "success";
349
	} else {
350
		// non-zero means there was some problem
351
		$severity = "warning";
352
	}
353

    
354
	if (strlen($extra_text) > 0) {
355
		$result_msg .= " " . $extra_text;
356
	}
357
	print_info_box($result_msg, $severity);
358
}
359

    
360
/*
361
 * Print Bootstrap callout
362
 *
363
 * @param string $msg     message to display
364
 * @param string $class   contextual class, defaults to info (default | danger | warning | info)
365
 * @param string $heading optional callout heading
366
 */
367
function print_callout($msg, $class = 'info', $heading = '') {
368

    
369
	if ('' == $msg) {
370
		return;
371
	}
372
	$class = strtolower($class);
373
	$callout = '';
374

    
375
	if ($class != 'default' && $class != 'danger' && $class != 'warning' && $class != 'info') {
376
		$class = 'info';
377
	}
378
	$callout .= '<div class="bs-callout bs-callout-' . $class . '">';
379

    
380
	if ('' != $heading) {
381
		$callout .= '<h4>' . $heading . '</h4>';
382
	}
383
	$callout .= $msg . '</div>';
384
	echo $callout;
385
}
386

    
387
function get_std_save_message($retval) {
388
	$filter_related = false;
389
	$filter_pages = array("firewall_aliases", "firewall_nat", "firewall_rules", "status_logs_filter");
390
	if ($retval === 0) {
391
		// 0 is success
392
		$to_return = gettext("The changes have been applied successfully.");
393
	} else {
394
		// non-zero means there was some problem
395
		$to_return = sprintf(gettext('There was a problem applying the changes. See the %1$sSystem Logs%2$s.'), '<a href="status_logs.php">', '</a>');
396
	}
397
	foreach ($filter_pages as $fp) {
398
		if (stristr($_SERVER['SCRIPT_FILENAME'], $fp)) {
399
			$filter_related = true;
400
		}
401
	}
402
	if ($filter_related) {
403
		$to_return .= " " . gettext("The firewall rules are now reloading in the background.") . "<br />" .
404
		    sprintf(gettext('%1$sMonitor%2$s the filter reload progress.'), "<a href='status_filter_reload.php'>", "</a>");
405
	}
406
	return $to_return;
407
}
408

    
409
function pprint_address($adr) {
410
	global $specialnets;
411

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

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

    
424
	return $padr;
425
}
426

    
427
function pprint_port($port) {
428
	global $wkports;
429

    
430
	$pport = "";
431

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

    
446
	return $pport;
447
}
448

    
449
function insert_word_breaks_in_domain_name($domain_name) {
450
	return str_replace('.', '<wbr>.', $domain_name);
451
}
452

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

    
528
	return $item_set;
529
}
530

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

    
540
function genhtmltitle($title, $links=true) {
541
	if (is_array($title)) {
542
		$num_crumbs = count($title);
543
	} else if ($title != NULL) {
544
		$num_crumbs = 1;
545
	} else {
546
		$num_crumbs = 0;
547
	}
548

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

    
554
		if (!is_array($links)) {
555
			$gen_default = ($links === true);
556
			$links = array_fill(0, $num_crumbs, '');
557
			// If no links passed, then default to a link to self on the last entry.
558
			if ($gen_default) {
559
				$links[$num_crumbs-1] = '@self';
560
			}
561
		}
562

    
563
		foreach ($title as $idx => $el) {
564
			$href = $links[$idx];
565
			if (strlen($href) > 0) {
566
				// For convenience, if the caller specifies '@self' then make a link
567
				// to the current page, including any query string.
568
				if ($href == '@self') {
569
					$href = $_SERVER['REQUEST_URI'];
570
				}
571
				if (substr($href, 0, 1) != '/') {
572
					$href = '/' . $href;
573
				}
574
				$bc .= '<li><a href="' . htmlentities($href) . '">' . $el . '</a></li>';
575
			} else {
576
				$bc .= '<li>' . $el . '</li>';
577
			}
578
		}
579

    
580
		$bc .= '</ol>';
581
	} else {
582
		$bc = "";
583
	}
584

    
585
	return $bc;
586
}
587

    
588
function gen_customwidgettitle_div($widgettitle) {
589
	$divstr = '<div class="form-group">';
590
	$divstr .= '  <label for="descr" class="col-sm-4 control-label">' . gettext('Widget title'). '</label>';
591
	$divstr .= '  <div class="col-sm-4">';
592
	$divstr .= '    <input type="text" name="descr" id="descr" value="'. $widgettitle . '" class="form-control" />';
593
	$divstr .= '  </div>';
594
	$divstr .= '</div>';
595

    
596
	return $divstr;
597
}
598

    
599
function set_customwidgettitle(& $user_settings) {
600
	if ($_POST['descr']) {
601
		$user_settings['widgets'][$_POST['widgetkey']]['descr'] = trim($_POST['descr']);
602
	} else {
603
		unset($user_settings['widgets'][$_POST['widgetkey']]['descr']);
604
	}
605
}
606

    
607
/* update the changedesc and changecount(er) variables */
608
function update_changedesc($update) {
609
	global $changedesc;
610
	global $changecount;
611

    
612
	$changedesc .= " {$update}";
613
	$changecount++;
614
}
615

    
616
/* Check if variable has changed, update and log if it has
617
 * returns true if var changed
618
 * varname = variable name in plain text
619
 * orig = original value
620
 * new = new value
621
 */
622
function update_if_changed($varname, & $orig, $new) {
623
	if (is_array($orig) && is_array($new)) {
624
		$a_diff = array_diff($orig, $new);
625
		foreach ($a_diff as $diff) {
626
			update_changedesc("removed {$varname}: \"{$diff}\"");
627
		}
628
		$a_diff = array_diff($new, $orig);
629
		foreach ($a_diff as $diff) {
630
			update_changedesc("added {$varname}: \"{$diff}\"");
631
		}
632
		$orig = $new;
633
		return true;
634

    
635
	} else {
636
		if ($orig != $new) {
637
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
638
			$orig = $new;
639
			return true;
640
		}
641
	}
642
	return false;
643
}
644

    
645
//function to create widget tabs when called
646
function display_widget_tabs(& $tab_array) {
647
	echo "<div id=\"tabs\">";
648
	foreach ($tab_array as $ta) {
649
		$dashpos = strpos($ta[2], '-');
650
		$tabclass = substr($ta[2], 0, $dashpos);
651
		$tabclass = $tabclass . "-class";
652
		if ($ta[1] == true) {
653
			$tabActive = "table-cell";
654
			$tabNonActive = "none";
655
		} else {
656
			$tabActive = "none";
657
			$tabNonActive = "table-cell";
658
		}
659
		echo "<div id=\"{$ta[2]}-active\" class=\"{$tabclass}-tabactive\" style=\"display:{$tabActive}; background-color:#EEEEEE; color:black;\">";
660
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
661
		echo "&nbsp;&nbsp;&nbsp;</b>";
662
		echo "</div>";
663

    
664
		echo "<div id=\"{$ta[2]}-deactive\" class=\"{$tabclass}-tabdeactive\" style=\"display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;\" onclick=\"return changeTabDIV('{$ta[2]}')\">";
665
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
666
		echo "&nbsp;&nbsp;&nbsp;</b>";
667
		echo "</div>";
668
	}
669
	echo "</div>";
670
}
671

    
672
// Return inline javascript file or CSS to minimize
673
// request count going back to server.
674
function outputJavaScriptFileInline($javascript) {
675
	if (file_exists($javascript)) {
676
		echo "\n<script type=\"text/javascript\">\n";
677
		include_once($javascript);
678
		echo "\n</script>\n";
679
	} else {
680
		echo "\n\n<!-- Could not locate file:  {$javascript} -->\n\n";
681
	}
682
}
683

    
684
function outputCSSPrintFileInline($css) {
685
	if (file_exists($css)) {
686
		echo "\n<style media=\"print\" type=\"text/css\">\n";
687
		include_once($css);
688
		echo "\n</style>\n";
689
	} else {
690
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
691
	}
692
}
693

    
694
function outputCSSFileInline($css) {
695
	if (file_exists($css)) {
696
		echo "\n<style type=\"text/css\">\n";
697
		include_once($css);
698
		echo "\n</style>\n";
699
	} else {
700
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
701
	}
702
}
703

    
704
$rfc2616 = array(
705
	100 => "100 Continue",
706
	101 => "101 Switching Protocols",
707
	200 => "200 OK",
708
	201 => "201 Created",
709
	202 => "202 Accepted",
710
	203 => "203 Non-Authoritative Information",
711
	204 => "204 No Content",
712
	205 => "205 Reset Content",
713
	206 => "206 Partial Content",
714
	300 => "300 Multiple Choices",
715
	301 => "301 Moved Permanently",
716
	302 => "302 Found",
717
	303 => "303 See Other",
718
	304 => "304 Not Modified",
719
	305 => "305 Use Proxy",
720
	306 => "306 (Unused)",
721
	307 => "307 Temporary Redirect",
722
	400 => "400 Bad Request",
723
	401 => "401 Unauthorized",
724
	402 => "402 Payment Required",
725
	403 => "403 Forbidden",
726
	404 => "404 Not Found",
727
	405 => "405 Method Not Allowed",
728
	406 => "406 Not Acceptable",
729
	407 => "407 Proxy Authentication Required",
730
	408 => "408 Request Timeout",
731
	409 => "409 Conflict",
732
	410 => "410 Gone",
733
	411 => "411 Length Required",
734
	412 => "412 Precondition Failed",
735
	413 => "413 Request Entity Too Large",
736
	414 => "414 Request-URI Too Long",
737
	415 => "415 Unsupported Media Type",
738
	416 => "416 Requested Range Not Satisfiable",
739
	417 => "417 Expectation Failed",
740
	500 => "500 Internal Server Error",
741
	501 => "501 Not Implemented",
742
	502 => "502 Bad Gateway",
743
	503 => "503 Service Unavailable",
744
	504 => "504 Gateway Timeout",
745
	505 => "505 HTTP Version Not Supported"
746
);
747

    
748
function is_rfc2616_code($code) {
749
	global $rfc2616;
750
	if (isset($rfc2616[$code])) {
751
		return true;
752
	} else {
753
		return false;
754
	}
755
}
756

    
757
function print_rfc2616_select($tag, $current) {
758
	global $rfc2616;
759

    
760
	/* Default to 200 OK if not set */
761
	if ($current == "") {
762
		$current = 200;
763
	}
764

    
765
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
766
	foreach ($rfc2616 as $code => $message) {
767
		if ($code == $current) {
768
			$sel = " selected";
769
		} else {
770
			$sel = "";
771
		}
772
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
773
	}
774
	echo "</select>\n";
775
}
776

    
777
// Useful debugging function, much cleaner than print_r
778
function echo_array($array, $return_me = false) {
779
	$return = "";
780
	if (is_array($array) == false) {
781
		$return = "The provided variable is not an array.";
782
	} else {
783
		foreach ($array as $name=>$value) {
784
			if (is_array($value)) {
785
				$return .= "";
786
				$return .= "['<b>$name</b>'] {<div style=\"margin-left:10px;\">\n";
787
				$return .= echo_array($value, true);
788
				$return .= "</div>}";
789
				$return .= "\n\n";
790
			} else {
791
				if (is_string($value)) {
792
					$value = "\"$value\"";
793
				}
794
				$return .= "['<b>$name</b>'] = $value\n\n";
795
			}
796
		}
797
	}
798
	if ($return_me == true) {
799
		return $return;
800
	} else {
801
		echo "<pre>".$return."</pre>";
802
	}
803
}
804

    
805
/****f* pfsense-utils/display_top_tabs
806
 * NAME
807
 *	 display_top_tabs - display tabs with rounded edges
808
 * INPUTS
809
 *	 $text	  - array of tabs
810
 * RESULT
811
 *	 null
812
 ******/
813
function display_top_tabs(& $tab_array, $no_drop_down = false, $type = 'pills', $usepost = "") {
814
	global $tab_array_indent;
815
	global $tab_array_space;
816
	global $tab_array_char_limit;
817

    
818
	/*	does the user have access to this tab?
819
	 *	master user has access to everything.
820
	 *	if the user does not have access, simply
821
	 *	unset the tab item.
822
	 */
823

    
824
	/* empty string code */
825
	if ($tab_array_indent == '') {
826
		$tab_array_indent = 0;
827
	}
828

    
829
	if ($tab_array_space == '') {
830
		$tab_array_space = 1;
831
	}
832

    
833
	if ($tab_array_char_limit == '') {
834
		$tab_array_char_limit = 256;
835
	}
836

    
837
	foreach ($tab_array as $tab_id => $ta) {
838
		if (!isAllowedPage($ta[2])) {
839
			unset ($tab_array[$tab_id]);
840
		}
841
	}
842

    
843
	$tabcharcount = 0;
844
	foreach ($tab_array as $ta) {
845
		$tabcharcount = $tabcharcount + strlen($ta[0]);
846
	}
847

    
848
	if ($no_drop_down == true) {
849
		$tabcharcount = 0;
850
		unset($tab_array_char_limit);
851
	}
852

    
853
	// If the character count of the tab names is > 670
854
	// then show a select item dropdown menubox.
855
	if ($tabcharcount > $tab_array_char_limit) {
856
		echo gettext("Currently viewing: ");
857
		echo "<select name=\"TabSelect\" onchange=\"tabs_will_go(this)\">\n";
858

    
859
		foreach ($tab_array as $ta) {
860
			if ($ta[1] == "true") {
861
				$selected = " selected";
862
			} else {
863
				$selected = "";
864
			}
865
			// Onclick in option will not work in some browser
866
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
867
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
868
		}
869

    
870
		echo "</select>\n<p>&nbsp;</p>";
871
		echo "<script type=\"text/javascript\">";
872
		echo "\n//<![CDATA[\n";
873
		if ($usepost == 'usepost') {
874
			echo " function tabs_will_go(obj){ var target = obj.value.split(\"?\"); postSubmit(get2post(target[1]),target[0]); }\n";
875
		} else {
876
			echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
877
		}
878
		echo "//]]>\n";
879
		echo "</script>";
880
	} else {
881
		echo '<ul class="nav nav-' . $type . '">';
882

    
883
		foreach ($tab_array as $ta) {
884
			echo '<li role="presentation"';
885
			if ($ta[1]) {
886
				echo ' class="active"';
887
			}
888

    
889
			echo '><a href="' . $ta[2] . '" ' . $usepost . '>' . $ta[0] . '</a></li>';
890
		}
891

    
892
		echo '</ul>';
893
	}
894
}
895

    
896
function add_package_tabs($tabgroup, &$tab_array) {
897
	foreach (config_get_path('installedpackages/package', []) as $pkg) {
898
		if (!is_array($pkg['tabs']['tab'])) {
899
			continue;
900
		}
901

    
902
		foreach ($pkg['tabs']['tab'] as $tab) {
903
			if ($tab['tabgroup'] != $tabgroup) {
904
				continue;
905
			}
906
			$tab_entry = array();
907
			if ($tab['name']) {
908
				$tab_entry[] = $tab['name'];
909
				$tab_entry[] = false;
910
				$tab_entry[] = $tab['url'];
911
				$tab_array[] = $tab_entry;
912
			}
913
		}
914
	}
915
}
916

    
917
function alias_info_popup($alias_id) {
918
	global $user_settings;
919

    
920
	$alias = config_get_path("aliases/alias/{$alias_id}");
921
	if (!is_array($alias)) {
922
		return;
923
	}
924

    
925
	$maxlength = 60;
926
	$content = "";
927

    
928
	if ($user_settings['webgui']['disablealiaspopupdetail']) {
929
		if (strlen($alias['descr']) >= $maxlength) {
930
			$alias['descr'] = substr($alias['descr'], 0, $maxlength) . '&hellip;';
931
		}
932

    
933
		$content .= $alias['descr'];
934
	} else if ($alias['url']) {
935
		// TODO: Change it when pf supports tables with ports
936
		if ($alias['type'] == "urltable") {
937
			exec("/sbin/pfctl -t {$alias['name']} -T show | wc -l", $total_entries);
938
			$counter=preg_replace("/\D/", "", $total_entries[0]);
939
			exec("/sbin/pfctl -t {$alias['name']} -T show | head -10002", $alias_addresses);
940
		} else {
941
			$urlfn = alias_expand_urltable($alias['name']);
942
			$alias_addresses = explode("\n", file_get_contents($urlfn));
943
			$counter = count($alias_addresses);
944
		}
945

    
946
		$content .= '<h5>'. htmlspecialchars($alias['url']) .'</h5><ul><li>'. implode('</li><li>', $alias_addresses) .'</li></ul>';
947
		if ($counter > 10002) {
948
			$content .= '<i>'. gettext("listing only first 10k items") .'</i>';
949
		}
950
	} else {
951
		$alias_addresses = explode (" ", $alias['address']);
952
		$alias_details = explode ("||", $alias['detail']);
953
		$idx = 0;
954

    
955
		$content .= "<table>\n";
956
		$content .= "<thead>\n";
957
		$content .= "<tr>\n";
958
		$content .= "<th>" . gettext("Value") . "</th><th  style='padding-left: 10px;'>" . gettext("Description") . "</th></tr>\n";
959
		$content .= "</thead>\n";
960
		$content .= "<tbody>\n";
961

    
962
		foreach ($alias_addresses as $ap) {
963
			$content .= "	<tr>\n";
964
			$content .= "		<td>\n";
965
			$content .= 			alias_idn_to_utf8($ap);
966
			$content .=	"		</td>\n";
967
			$content .= "		<td style='padding-left: 10px;'>\n";
968
			$content .= 			htmlspecialchars($alias_details[$idx]);
969
			$content .=	"		</td>\n";
970
			$content .= "	</tr>\n";
971
			$idx++;
972
		}
973

    
974
		$content .= "</tbody>\n";
975
		$content .= "</table>\n";
976
	}
977

    
978
	return $content;
979
}
980

    
981
function gateway_info_popup($showgw, $gateways_status = false) {
982
	init_config_arr(array('gateways', 'gateway_group'));
983
	$a_gateways = return_gateways_array(true, false, true, true);
984

    
985
	/* Use cached gateway status if available.
986
	 * See https://redmine.pfsense.org/issues/12174 */
987
	if (!is_array($gateways_status)) {
988
		$gateways_status = return_gateways_status(true);
989
	}
990

    
991
	$content = "";
992
	$gws = array();
993
	$bgdanger = array('force_down', 'down', 'highloss', 'highdelay');
994
	$bgwarning = array('loss', 'delay');
995
	$bgsuccess = array('none');
996
	$bgcolor = "bg-info";
997
	$link = "";
998

    
999
	if (is_array($a_gateways)) {
1000
		foreach ($a_gateways as $i => $gateway) {
1001
			if ($gateway['name'] == $showgw) {
1002
				$gws[] = $gateway['name'];
1003
				$link = "/system_gateways_edit.php?id={$i}";
1004
				break;
1005
			}
1006
		}
1007
	}
1008
	foreach(config_get_path('gateways/gateway_group') as $i => $gwgroup) {
1009
		if ($gwgroup['name'] == $showgw) {
1010
			foreach ($gwgroup['item'] as $member) {
1011
				$membersplit = explode("|", $member);
1012
				$gws[] = $membersplit[0];
1013
			}
1014
			$link = "system_gateway_groups_edit.php?id={$i}";
1015
			break;
1016
		}
1017
	}
1018

    
1019
	if (!empty($gws)) {
1020
		$content .= "<table>\n";
1021
		$content .= "<thead>\n";
1022
		$content .= "<tr>\n";
1023
		$content .= "<th>" . gettext("Name") . "</th><th style='padding-left: 10px;'>" . gettext("Interface") . "</th>";
1024
		$content .= "<th style='padding-left: 10px;'>" . gettext("Gateway") . "</th></tr>\n";
1025
		$content .= "</thead>\n";
1026
		$content .= "<tbody>\n";
1027
		foreach ($gws as $gw) {
1028
			foreach ($gateways_status as $gwstatus) {
1029
				if ($gwstatus['name'] == $gw) {
1030
					if (in_array($gwstatus['status'], $bgdanger)) {
1031
						$bgcolor = "bg-danger";
1032
					} elseif (in_array($gwstatus['status'], $bgwarning)) {
1033
						$bgcolor = "bg-warning";
1034
					} elseif (in_array($gwstatus['status'], $bgsuccess)) {
1035
						$bgcolor = "bg-success";
1036
					} else {
1037
						$bgcolor = "bg-info";
1038
					}
1039
				}
1040
			}
1041
			$iface = lookup_gateway_interface_by_name($gw);
1042
			$content .= "	<tr class='{$bgcolor}'>\n";
1043
			$content .= "		<td>\n";
1044
			$content .= 			$gw;
1045
			$content .=	"		</td>\n";
1046
			$content .= "		<td style='padding-left: 10px;'>\n";
1047
			$content .= 			config_get_path("interfaces/{$iface}/descr", "");
1048
			$content .=	"		</td>\n";
1049
			$content .= "		<td style='padding-left: 10px;'>\n";
1050
			$content .= 			lookup_gateway_ip_by_name($gw);
1051
			$content .=	"		</td>\n";
1052
			$content .= "	</tr>\n";
1053
		}
1054
		$content .= "</tbody>\n";
1055
		$content .= "</table>\n";
1056
	} else {
1057
		return;
1058
	}
1059

    
1060
	return "<a href=\"{$link}\" data-toggle=\"popover\" data-trigger=\"hover focus\" title=\"" . gettext('Gateway details') . "\" data-content=\"{$content}\" data-html=\"true\">";
1061
}
1062

    
1063
function rule_columns_with_alias($src, $srcport, $dst, $dstport, $target="", $targetport="") {
1064
	$columns = array();
1065
	foreach (config_get_path('aliases/alias', []) as $alias_id => $alias_name) {
1066
		if ($alias_name['name'] == $src) {
1067
			$columns['src'] = $alias_id;
1068
		}
1069
		if ($alias_name['name'] == $srcport) {
1070
			$columns['srcport'] = $alias_id;
1071
		}
1072
		if ($alias_name['name'] == $dst) {
1073
			$columns['dst'] = $alias_id;
1074
		}
1075
		if ($alias_name['name'] == $dstport) {
1076
			$columns['dstport'] = $alias_id;
1077
		}
1078
		if ($alias_name['name'] == $target) {
1079
			$columns['target'] = $alias_id;
1080
		}
1081
		if ($alias_name['name'] == $targetport) {
1082
			$columns['targetport'] = $alias_id;
1083
		}
1084
	}
1085

    
1086
	return $columns;
1087
}
1088

    
1089
function form_output_row($name, $label, $content) {
1090
var_dump($content);die;
1091
?>
1092
<div class="form-group">
1093
	<label for="<?=$name?>" class="col-sm-2 control-label"><?=gettext($label); ?></label>
1094
	<div class="col-sm-10">
1095
		<?=$content?>
1096
	</div>
1097
</div>
1098
<?php
1099
}
1100

    
1101
function set_flash_message($class, $msg) {
1102
	@phpsession_begin();
1103
	$_SESSION['flash_messages'][$class][] = $msg;
1104
	@phpsession_end(true);
1105
}
1106

    
1107
function get_flash_message() {
1108
	@phpsession_begin();
1109
	if (isset($_SESSION['flash_messages']) && !empty($_SESSION['flash_messages'])) {
1110
		foreach ($_SESSION['flash_messages'] as $class => $flash_message) {
1111
			print_info_box(implode("<br />", $flash_message), $class);
1112
		}
1113
		unset($_SESSION['flash_messages']);
1114
	}
1115
	@phpsession_end(true);
1116
}
1117

    
1118
/* Retrieve GET or POST Value/State
1119
 * Example Usage:
1120
 * $value = getGETPOSTsettingvalue('get/post parameter name', "");
1121
 * $value = getGETPOSTsettingvalue('get/post parameter name', null);
1122
 * $state = getGETPOSTsettingvalue('get/post parameter name', null);
1123
 * $state = getGETPOSTsettingvalue('get/post parameter name', false);
1124
 */
1125
function getGETPOSTsettingvalue($settingname, $default) {
1126
	$settingvalue = $default;
1127
	if ($_GET[$settingname]) {
1128
		$settingvalue = $_GET[$settingname];
1129
	}
1130
	if ($_POST[$settingname]) {
1131
		$settingvalue = $_POST[$settingname];
1132
	}
1133
	return $settingvalue;
1134
}
1135

    
1136
/* set timezone */
1137
$cfgtz = config_get_path('system/timezone');
1138
if ($cfgtz) {
1139
	$timezone = $cfgtz;
1140
} elseif (isset($g['default_timezone']) && !empty(g_get('default_timezone'))) {
1141
	$timezone = g_get('default_timezone');
1142
} else {
1143
	$timezone = "Etc/UTC";
1144
}
1145

    
1146
/* Remove files we do not want to see in a crash report */
1147
function cleanup_crash_file_list() {
1148
	$files = glob("/var/crash/*");
1149
	if (!is_array($files) || empty($files)) {
1150
		return array();
1151
	}
1152

    
1153
	$exclude_patterns = array(
1154
		'.*.last',
1155
		'bounds',
1156
		'minfree'
1157
	);
1158

    
1159
	foreach ($files as $idx => $fb) {
1160
		if (preg_match('/' . implode('|', $exclude_patterns) . '/', basename($fb)) == 1) {
1161
			unset($files[$idx]);
1162
		}
1163
	}
1164

    
1165
	return $files;
1166
}
1167

    
1168
function system_has_crash_data() {
1169
	/* Test if there are any crash data files present */
1170
	return count(cleanup_crash_file_list()) > 0;
1171
}
1172

    
1173
function system_has_php_errors() {
1174
	/* Check if the PHP error log is empty. Cast to int in case the file
1175
	 * does not exist and filesize() returns false. */
1176
	return (int) @filesize("/tmp/PHP_errors.log") > 0;
1177
}
1178

    
1179
date_default_timezone_set($timezone);
1180

    
1181
?>
(66-66/228)