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

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

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

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

    
59
require_once("authgui.inc");
60

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

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

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

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

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

    
78
set_language();
79

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

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

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

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

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

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

    
135
$ldap_templates = array(
136

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
425
	return $padr;
426
}
427

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

    
431
	$pport = "";
432

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

    
447
	return $pport;
448
}
449

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

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

    
529
	return $item_set;
530
}
531

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

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

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

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

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

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

    
586
	return $bc;
587
}
588

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

    
597
	return $divstr;
598
}
599

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
979
	return $content;
980
}
981

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

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

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

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

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

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

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

    
1087
	return $columns;
1088
}
1089

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

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

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

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

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

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

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

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

    
1166
	return $files;
1167
}
1168

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

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

    
1180
date_default_timezone_set($timezone);
1181

    
1182
?>
(66-66/228)