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
include_once('config.inc');
32
include_once('config.lib.inc');
33
include_once('phpsessionmanager.inc');
34
include_once("util.inc");
35

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

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

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

    
61
require_once("authgui.inc");
62

    
63
/* parse the configuration and include all configuration functions */
64
require_once("functions.inc");
65

    
66
/* Include the autoloader for all the GUI display classes */
67
require_once("classes/autoload.inc.php");
68

    
69
/* used by progress bar */
70
$lastseen = "-1";
71

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

    
76
if (!function_exists('set_language')) {
77
	require_once("pfsense-utils.inc");
78
}
79

    
80
set_language();
81

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

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

    
120
$auth_server_types = array(
121
	'ldap' => "LDAP",
122
	'radius' => "RADIUS");
123

    
124
$ldap_urltypes = array(
125
	'Standard TCP' => 389,
126
	'STARTTLS Encrypted' => 389,
127
	'SSL/TLS Encrypted' => 636);
128

    
129
$ldap_scopes = array(
130
	'one' => gettext("One Level"),
131
	'subtree' => gettext("Entire Subtree"));
132

    
133
$ldap_protvers = array(
134
	2,
135
	3);
136

    
137
$ldap_templates = array(
138

    
139
	'open' => array(
140
		'desc' => "OpenLDAP",
141
		'attr_user' => "cn",
142
		'attr_group' => "cn",
143
		'attr_member' => "member",
144
		'allow_unauthenticated' => "true"),
145

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

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

    
160
$radius_srvcs = array(
161
	'both' => gettext("Authentication and Accounting"),
162
	'auth' => gettext("Authentication"),
163
	'acct' => gettext("Accounting"));
164

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

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

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

    
229
/* TCP flags */
230
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg", "ece", "cwr");
231

    
232
$specialnets = array(
233
	"(self)" => gettext("This Firewall"),
234
	"pppoe" => gettext("PPPoE clients"),
235
	"l2tp" => gettext("L2TP clients"));
236

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

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

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

    
255
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, &$input_errors) {
256

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

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

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

    
278
	foreach ($input_errors as $ierr) {
279
		echo '<li>' . htmlspecialchars($ierr) . '</li>';
280
	}
281

    
282
	echo '</ul>';
283
	echo '</div>';
284
}
285

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

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

    
305
	if (strpos($class, "alert-") !== 0) {
306
		$class = 'alert-' . $class;
307
	}
308

    
309
	$msg = '<div class="pull-left">' . $msg . '</div>';
310

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

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

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

    
327
		$msg .= '</form>';
328
	}
329

    
330
	return '<div class="alert ' . $class . ' clearfix" role="alert">' . $msg . '</div>';
331
}
332

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

    
338
function print_apply_box($msg) {
339
	print_info_box($msg, "warning", "apply", gettext("Apply Changes"), 'fa-check', 'success');
340
}
341

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

    
357
	if (strlen($extra_text) > 0) {
358
		$result_msg .= " " . $extra_text;
359
	}
360
	print_info_box($result_msg, $severity);
361
}
362

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

    
372
	if ('' == $msg) {
373
		return;
374
	}
375
	$class = strtolower($class);
376
	$callout = '';
377

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

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

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

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

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

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

    
427
	return $padr;
428
}
429

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

    
433
	$pport = "";
434

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

    
449
	return $pport;
450
}
451

    
452
function insert_word_breaks_in_domain_name($domain_name) {
453
	return str_replace('.', '<wbr>.', $domain_name);
454
}
455

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

    
531
	return $item_set;
532
}
533

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

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

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

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

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

    
583
		$bc .= '</ol>';
584
	} else {
585
		$bc = "";
586
	}
587

    
588
	return $bc;
589
}
590

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

    
599
	return $divstr;
600
}
601

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

    
610
/* update the changedesc and changecount(er) variables */
611
function update_changedesc($update) {
612
	global $changedesc;
613
	global $changecount;
614

    
615
	$changedesc .= " {$update}";
616
	$changecount++;
617
}
618

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

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

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

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

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

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

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

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

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

    
760
function print_rfc2616_select($tag, $current) {
761
	global $rfc2616;
762

    
763
	/* Default to 200 OK if not set */
764
	if ($current == "") {
765
		$current = 200;
766
	}
767

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

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

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

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

    
827
	/* empty string code */
828
	if ($tab_array_indent == '') {
829
		$tab_array_indent = 0;
830
	}
831

    
832
	if ($tab_array_space == '') {
833
		$tab_array_space = 1;
834
	}
835

    
836
	if ($tab_array_char_limit == '') {
837
		$tab_array_char_limit = 256;
838
	}
839

    
840
	foreach ($tab_array as $tab_id => $ta) {
841
		if (!isAllowedPage($ta[2])) {
842
			unset ($tab_array[$tab_id]);
843
		}
844
	}
845

    
846
	$tabcharcount = 0;
847
	foreach ($tab_array as $ta) {
848
		$tabcharcount = $tabcharcount + strlen($ta[0]);
849
	}
850

    
851
	if ($no_drop_down == true) {
852
		$tabcharcount = 0;
853
		unset($tab_array_char_limit);
854
	}
855

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

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

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

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

    
892
			echo '><a href="' . $ta[2] . '" ' . $usepost . '>' . $ta[0] . '</a></li>';
893
		}
894

    
895
		echo '</ul>';
896
	}
897
}
898

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

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

    
920
function alias_info_popup($alias_id) {
921
	global $user_settings;
922

    
923
	$alias = config_get_path("aliases/alias/{$alias_id}");
924
	if (!is_array($alias)) {
925
		return;
926
	}
927

    
928
	$maxlength = 60;
929
	$content = "";
930

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

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

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

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

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

    
977
		$content .= "</tbody>\n";
978
		$content .= "</table>\n";
979
	}
980

    
981
	return $content;
982
}
983

    
984
function gateway_info_popup($showgw, $gateways_status = false) {
985
	init_config_arr(array('gateways', 'gateway_group'));
986
	$a_gateways = return_gateways_array(true, false, true, true);
987

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

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

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

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

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

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

    
1089
	return $columns;
1090
}
1091

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

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

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

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

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

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

    
1156
	$exclude_patterns = array(
1157
		'.*.last',
1158
		'bounds',
1159
		'minfree'
1160
	);
1161

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

    
1168
	return $files;
1169
}
1170

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

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

    
1182
date_default_timezone_set($timezone);
1183

    
1184
?>
(66-66/228)