Project

General

Profile

Download (32 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
		global $config;
41
		csrf_conf('rewrite-js', '/csrf/csrf-magic.js');
42
		$timeout_minutes = isset($config['system']['webgui']['session_timeout']) ? $config['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
require_once("authgui.inc");
61

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

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

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

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

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

    
79
set_language();
80

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

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

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

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

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

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

    
136
$ldap_templates = array(
137

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
426
	return $padr;
427
}
428

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

    
432
	$pport = "";
433

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

    
448
	return $pport;
449
}
450

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

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

    
530
	return $item_set;
531
}
532

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

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

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

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

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

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

    
587
	return $bc;
588
}
589

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

    
598
	return $divstr;
599
}
600

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
781
// Useful debugging function, much cleaner than print_r
782
function echo_array($array, $return_me = false) {
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 $config;
818
	global $g;
819
	global $tab_array_indent;
820
	global $tab_array_space;
821
	global $tab_array_char_limit;
822

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

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

    
834
	if ($tab_array_space == '') {
835
		$tab_array_space = 1;
836
	}
837

    
838
	if ($tab_array_char_limit == '') {
839
		$tab_array_char_limit = 256;
840
	}
841

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

    
848
	$tab_active_bg	 = "#EEEEEE";
849
	$tab_inactive_bg = "#777777";
850
	$nifty_tabs_corners = "#FFF";
851
	$font_color = "white";
852

    
853
	$tabcharcount = 0;
854
	foreach ($tab_array as $ta) {
855
		$tabcharcount = $tabcharcount + strlen($ta[0]);
856
	}
857

    
858
	if ($no_drop_down == true) {
859
		$tabcharcount = 0;
860
		unset($tab_array_char_limit);
861
	}
862

    
863
	// If the character count of the tab names is > 670
864
	// then show a select item dropdown menubox.
865
	if ($tabcharcount > $tab_array_char_limit) {
866
		echo gettext("Currently viewing: ");
867
		echo "<select name=\"TabSelect\" onchange=\"tabs_will_go(this)\">\n";
868

    
869
		foreach ($tab_array as $ta) {
870
			if ($ta[1] == "true") {
871
				$selected = " selected";
872
			} else {
873
				$selected = "";
874
			}
875
			// Onclick in option will not work in some browser
876
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
877
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
878
		}
879

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

    
893
		foreach ($tab_array as $ta) {
894
			echo '<li role="presentation"';
895
			if ($ta[1]) {
896
				echo ' class="active"';
897
			}
898

    
899
			echo '><a href="' . $ta[2] . '" ' . $usepost . '>' . $ta[0] . '</a></li>';
900
		}
901

    
902
		echo '</ul>';
903
	}
904
}
905

    
906
function add_package_tabs($tabgroup, &$tab_array) {
907
	global $config, $g;
908

    
909
	if (!isset($config['installedpackages']['package'])) {
910
		return;
911
	}
912

    
913
	foreach ($config['installedpackages']['package'] as $pkg) {
914
		if (!is_array($pkg['tabs']['tab'])) {
915
			continue;
916
		}
917

    
918
		foreach ($pkg['tabs']['tab'] as $tab) {
919
			if ($tab['tabgroup'] != $tabgroup) {
920
				continue;
921
			}
922
			$tab_entry = array();
923
			if ($tab['name']) {
924
				$tab_entry[] = $tab['name'];
925
				$tab_entry[] = false;
926
				$tab_entry[] = $tab['url'];
927
				$tab_array[] = $tab_entry;
928
			}
929
		}
930
	}
931
}
932

    
933
function alias_info_popup($alias_id) {
934
	global $config, $user_settings;
935

    
936
	if (!is_array($config['aliases']['alias'][$alias_id])) {
937
		return;
938
	}
939

    
940
	$maxlength = 60;
941
	$alias = $config['aliases']['alias'][$alias_id];
942
	$content = "";
943

    
944
	if ($user_settings['webgui']['disablealiaspopupdetail']) {
945
		if (strlen($alias['descr']) >= $maxlength) {
946
			$alias['descr'] = substr($alias['descr'], 0, $maxlength) . '&hellip;';
947
		}
948

    
949
		$content .= $alias['descr'];
950
	} else if ($alias['url']) {
951
		// TODO: Change it when pf supports tables with ports
952
		if ($alias['type'] == "urltable") {
953
			exec("/sbin/pfctl -t {$alias['name']} -T show | wc -l", $total_entries);
954
			$counter=preg_replace("/\D/", "", $total_entries[0]);
955
			exec("/sbin/pfctl -t {$alias['name']} -T show | head -10002", $alias_addresses);
956
		} else {
957
			$urlfn = alias_expand_urltable($alias['name']);
958
			$alias_addresses = explode("\n", file_get_contents($urlfn));
959
			$counter = count($alias_addresses);
960
		}
961

    
962
		$content .= '<h5>'. $alias['url'] .'</h5><ul><li>'. implode('</li><li>', $alias_addresses) .'</li></ul>';
963
		if ($counter > 10002) {
964
			$content .= '<i>'. gettext("listing only first 10k items") .'</i>';
965
		}
966
	} else {
967
		$alias_addresses = explode (" ", $alias['address']);
968
		$alias_details = explode ("||", $alias['detail']);
969
		$idx = 0;
970

    
971
		$content .= "<table>\n";
972
		$content .= "<thead>\n";
973
		$content .= "<tr>\n";
974
		$content .= "<th>" . gettext("Value") . "</th><th  style='padding-left: 10px;'>" . gettext("Description") . "</th></tr>\n";
975
		$content .= "</thead>\n";
976
		$content .= "<tbody>\n";
977

    
978
		foreach ($alias_addresses as $ap) {
979
			$content .= "	<tr>\n";
980
			$content .= "		<td>\n";
981
			$content .= 			alias_idn_to_utf8($ap);
982
			$content .=	"		</td>\n";
983
			$content .= "		<td style='padding-left: 10px;'>\n";
984
			$content .= 			htmlspecialchars($alias_details[$idx]);
985
			$content .=	"		</td>\n";
986
			$content .= "	</tr>\n";
987
			$idx++;
988
		}
989

    
990
		$content .= "</tbody>\n";
991
		$content .= "</table>\n";
992
	}
993

    
994
	return $content;
995
}
996

    
997
function gateway_info_popup($showgw, $gateways_status = false) {
998
	global $config, $user_settings;
999

    
1000
	init_config_arr(array('gateways', 'gateway_group'));
1001
	$a_gateways = return_gateways_array(true, false, true, true);
1002

    
1003
	/* Use cached gateway status if available.
1004
	 * See https://redmine.pfsense.org/issues/12174 */
1005
	if (!is_array($gateways_status)) {
1006
		$gateways_status = return_gateways_status(true);
1007
	}
1008

    
1009
	$content = "";
1010
	$gws = array();
1011
	$bgdanger = array('force_down', 'down', 'highloss', 'highdelay');
1012
	$bgwarning = array('loss', 'delay');
1013
	$bgsuccess = array('none');
1014
	$bgcolor = "bg-info";
1015

    
1016
	if (is_array($a_gateways)) {
1017
		foreach ($a_gateways as $i => $gateway) {
1018
			if ($gateway['name'] == $showgw) {
1019
				$gws[] = $gateway['name'];
1020
				$link = "/system_gateways_edit.php?id={$i}";
1021
				break;
1022
			}
1023
		}
1024
	}
1025
	if (is_array($config['gateways']['gateway_group'])) {
1026
		foreach($config['gateways']['gateway_group'] as $i => $gwgroup) {
1027
			if ($gwgroup['name'] == $showgw) {
1028
				foreach ($gwgroup['item'] as $member) {
1029
					$membersplit = explode("|", $member);
1030
					$gws[] = $membersplit[0];
1031
				}
1032
				$link = "system_gateway_groups_edit.php?id={$i}";
1033
				break;
1034
			}
1035
		}
1036
	}
1037

    
1038
	if (!empty($gws)) {
1039
		$content .= "<table>\n";
1040
		$content .= "<thead>\n";
1041
		$content .= "<tr>\n";
1042
		$content .= "<th>" . gettext("Name") . "</th><th style='padding-left: 10px;'>" . gettext("Interface") . "</th>";
1043
		$content .= "<th style='padding-left: 10px;'>" . gettext("Gateway") . "</th></tr>\n";
1044
		$content .= "</thead>\n";
1045
		$content .= "<tbody>\n";
1046
		foreach ($gws as $gw) {
1047
			foreach ($gateways_status as $gwstatus) {
1048
				if ($gwstatus['name'] == $gw) {
1049
					if (in_array($gwstatus['status'], $bgdanger)) {
1050
						$bgcolor = "bg-danger";
1051
					} elseif (in_array($gwstatus['status'], $bgwarning)) {
1052
						$bgcolor = "bg-warning";
1053
					} elseif (in_array($gwstatus['status'], $bgsuccess)) {
1054
						$bgcolor = "bg-success";
1055
					} else {
1056
						$bgcolor = "bg-info";
1057
					}
1058
				}
1059
			}
1060
			$content .= "	<tr class='{$bgcolor}'>\n";
1061
			$content .= "		<td>\n";
1062
			$content .= 			$gw;
1063
			$content .=	"		</td>\n";
1064
			$content .= "		<td style='padding-left: 10px;'>\n";
1065
			$content .= 			$config['interfaces'][lookup_gateway_interface_by_name($gw)]['descr'];
1066
			$content .=	"		</td>\n";
1067
			$content .= "		<td style='padding-left: 10px;'>\n";
1068
			$content .= 			lookup_gateway_ip_by_name($gw);
1069
			$content .=	"		</td>\n";
1070
			$content .= "	</tr>\n";
1071
			$idx++;
1072
		}
1073
		$content .= "</tbody>\n";
1074
		$content .= "</table>\n";
1075
	} else {
1076
		return;
1077
	}
1078

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

    
1082
function rule_columns_with_alias($src, $srcport, $dst, $dstport, $target="", $targetport="") {
1083
	global $config;
1084

    
1085
	if ($config['aliases']['alias'] == "" || !is_array($config['aliases']['alias'])) {
1086
		return;
1087
	}
1088

    
1089
	$columns = array();
1090
	foreach ($config['aliases']['alias'] as $alias_id => $alias_name) {
1091
		if ($alias_name['name'] == $src) {
1092
			$columns['src'] = $alias_id;
1093
		}
1094
		if ($alias_name['name'] == $srcport) {
1095
			$columns['srcport'] = $alias_id;
1096
		}
1097
		if ($alias_name['name'] == $dst) {
1098
			$columns['dst'] = $alias_id;
1099
		}
1100
		if ($alias_name['name'] == $dstport) {
1101
			$columns['dstport'] = $alias_id;
1102
		}
1103
		if ($alias_name['name'] == $target) {
1104
			$columns['target'] = $alias_id;
1105
		}
1106
		if ($alias_name['name'] == $targetport) {
1107
			$columns['targetport'] = $alias_id;
1108
		}
1109
	}
1110

    
1111
	return $columns;
1112
}
1113

    
1114
function form_output_row($name, $label, $content) {
1115
var_dump($content);die;
1116
?>
1117
<div class="form-group">
1118
	<label for="<?=$name?>" class="col-sm-2 control-label"><?=gettext($label); ?></label>
1119
	<div class="col-sm-10">
1120
		<?=$content?>
1121
	</div>
1122
</div>
1123
<?php
1124
}
1125

    
1126
function set_flash_message($class, $msg) {
1127
	@phpsession_begin();
1128
	$_SESSION['flash_messages'][$class][] = $msg;
1129
	@phpsession_end(true);
1130
}
1131

    
1132
function get_flash_message() {
1133
	@phpsession_begin();
1134
	if (isset($_SESSION['flash_messages']) && !empty($_SESSION['flash_messages'])) {
1135
		foreach ($_SESSION['flash_messages'] as $class => $flash_message) {
1136
			print_info_box(implode("<br />", $flash_message), $class);
1137
		}
1138
		unset($_SESSION['flash_messages']);
1139
	}
1140
	@phpsession_end(true);
1141
}
1142

    
1143
/* Retrieve GET or POST Value/State
1144
 * Eample Usage:
1145
 * $value = getGETPOSTsettingvalue('get/post parameter name', "");
1146
 * $value = getGETPOSTsettingvalue('get/post parameter name', null);
1147
 * $state = getGETPOSTsettingvalue('get/post parameter name', null);
1148
 * $state = getGETPOSTsettingvalue('get/post parameter name', false);
1149
 */
1150
function getGETPOSTsettingvalue($settingname, $default) {
1151
	$settingvalue = $default;
1152
	if ($_GET[$settingname]) {
1153
		$settingvalue = $_GET[$settingname];
1154
	}
1155
	if ($_POST[$settingname]) {
1156
		$settingvalue = $_POST[$settingname];
1157
	}
1158
	return $settingvalue;
1159
}
1160

    
1161
/* set timezone */
1162
if (isset($config['system']['timezone']) &&
1163
    !empty($config['system']['timezone'])) {
1164
	$timezone = $config['system']['timezone'];
1165
} elseif (isset($g['default_timezone']) && !empty($g['default_timezone'])) {
1166
	$timezone = $g['default_timezone'];
1167
} else {
1168
	$timezone = "Etc/UTC";
1169
}
1170

    
1171
/* Remove files we do not want to see in a crash report */
1172
function cleanup_crash_file_list() {
1173
	$files = glob("/var/crash/*");
1174
	if (!is_array($files) || empty($files)) {
1175
		return array();
1176
	}
1177

    
1178
	$exclude_patterns = array(
1179
		'.*.last',
1180
		'bounds',
1181
		'minfree'
1182
	);
1183

    
1184
	foreach ($files as $idx => $fb) {
1185
		if (preg_match('/' . implode('|', $exclude_patterns) . '/', basename($fb)) == 1) {
1186
			unset($files[$idx]);
1187
		}
1188
	}
1189

    
1190
	return $files;
1191
}
1192

    
1193
function system_has_crash_data() {
1194
	/* Test if there are any crash data files present */
1195
	return count(cleanup_crash_file_list()) > 0;
1196
}
1197

    
1198
function system_has_php_errors() {
1199
	/* Check if the PHP error log is empty. Cast to int in case the file
1200
	 * does not exist and filesize() returns false. */
1201
	return (int) @filesize("/tmp/PHP_errors.log") > 0;
1202
}
1203

    
1204
date_default_timezone_set($timezone);
1205

    
1206
?>
(66-66/228)