Project

General

Profile

Download (33.3 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-2020 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

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

    
37
if (!$nocsrf) {
38
	function csrf_startup() {
39
		global $config;
40
		csrf_conf('rewrite-js', '/csrf/csrf-magic.js');
41
		$timeout_minutes = isset($config['system']['webgui']['session_timeout']) ? $config['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 address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
647
	if (isset($adr['any'])) {
648
		$padr = "any";
649
	} else if ($adr['network']) {
650
		$padr = $adr['network'];
651
	} else if ($adr['address']) {
652
		list($padr, $pmask) = explode("/", $adr['address']);
653
		if (!$pmask) {
654
			if (is_ipaddrv6($padr)) {
655
				$pmask = 128;
656
			} else {
657
				$pmask = 32;
658
			}
659
		}
660
	}
661

    
662
	if (isset($adr['not'])) {
663
		$pnot = 1;
664
	} else {
665
		$pnot = 0;
666
	}
667

    
668
	if ($adr['port']) {
669
		list($pbeginport, $pendport) = explode("-", $adr['port']);
670
		if (!$pendport) {
671
			$pendport = $pbeginport;
672
		}
673
	} else if (!is_alias($pbeginport) && !is_alias($pendport)) {
674
		$pbeginport = "any";
675
		$pendport = "any";
676
	}
677
}
678

    
679
function pconfig_to_address(&$adr, $padr, $pmask, $pnot = false, $pbeginport = 0, $pendport = 0) {
680
	$adr = array();
681

    
682
	if ($padr == "any") {
683
		$adr['any'] = true;
684
	} else if (is_specialnet($padr)) {
685
		$adr['network'] = $padr;
686
	} else {
687
		$adr['address'] = $padr;
688
		if (is_ipaddrv6($padr)) {
689
			if ($pmask != 128) {
690
				$adr['address'] .= "/" . $pmask;
691
			}
692
		} else {
693
			if ($pmask != 32) {
694
				$adr['address'] .= "/" . $pmask;
695
			}
696
		}
697
	}
698

    
699
	if ($pnot) {
700
		$adr['not'] = true;
701
	} else {
702
		unset($adr['not']);
703
	}
704

    
705
	if (($pbeginport != 0) && ($pbeginport != "any")) {
706
		if ($pbeginport != $pendport) {
707
			$adr['port'] = $pbeginport . "-" . $pendport;
708
		} else {
709
			$adr['port'] = $pbeginport;
710
		}
711
	}
712

    
713
	/*
714
	 * If the port is still unset, then it must not be numeric, but could
715
	 * be an alias or a well-known/registered service.
716
	 * See https://redmine.pfsense.org/issues/8410
717
	 */
718
	if (!isset($adr['port']) && is_port_or_alias($pbeginport)) {
719
		$adr['port'] = $pbeginport;
720
	}
721
}
722

    
723
function is_specialnet($net) {
724
	global $specialsrcdst;
725

    
726
	if (!$net) {
727
		return false;
728
	}
729
	if (in_array($net, $specialsrcdst)) {
730
		return true;
731
	} else {
732
		return false;
733
	}
734
}
735

    
736
//function to create widget tabs when called
737
function display_widget_tabs(& $tab_array) {
738
	echo "<div id=\"tabs\">";
739
	$tabscounter = 0;
740
	foreach ($tab_array as $ta) {
741
		$dashpos = strpos($ta[2], '-');
742
		$tabname = $ta[2] . "-tab";
743
		$tabclass = substr($ta[2], 0, $dashpos);
744
		$tabclass = $tabclass . "-class";
745
		if ($ta[1] == true) {
746
			$tabActive = "table-cell";
747
			$tabNonActive = "none";
748
		} else {
749
			$tabActive = "none";
750
			$tabNonActive = "table-cell";
751
		}
752
		echo "<div id=\"{$ta[2]}-active\" class=\"{$tabclass}-tabactive\" style=\"display:{$tabActive}; background-color:#EEEEEE; color:black;\">";
753
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
754
		echo "&nbsp;&nbsp;&nbsp;</b>";
755
		echo "</div>";
756

    
757
		echo "<div id=\"{$ta[2]}-deactive\" class=\"{$tabclass}-tabdeactive\" style=\"display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;\" onclick=\"return changeTabDIV('{$ta[2]}')\">";
758
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
759
		echo "&nbsp;&nbsp;&nbsp;</b>";
760
		echo "</div>";
761
	}
762
	echo "</div>";
763
}
764

    
765

    
766
// Return inline javascript file or CSS to minimize
767
// request count going back to server.
768
function outputJavaScriptFileInline($javascript) {
769
	if (file_exists($javascript)) {
770
		echo "\n<script type=\"text/javascript\">\n";
771
		include_once($javascript);
772
		echo "\n</script>\n";
773
	} else {
774
		echo "\n\n<!-- Could not locate file:  {$javascript} -->\n\n";
775
	}
776
}
777

    
778

    
779

    
780
function outputCSSPrintFileInline($css) {
781
	if (file_exists($css)) {
782
		echo "\n<style media=\"print\" type=\"text/css\">\n";
783
		include_once($css);
784
		echo "\n</style>\n";
785
	} else {
786
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
787
	}
788
}
789

    
790

    
791
function outputCSSFileInline($css) {
792
	if (file_exists($css)) {
793
		echo "\n<style type=\"text/css\">\n";
794
		include_once($css);
795
		echo "\n</style>\n";
796
	} else {
797
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
798
	}
799
}
800

    
801
$rfc2616 = array(
802
	100 => "100 Continue",
803
	101 => "101 Switching Protocols",
804
	200 => "200 OK",
805
	201 => "201 Created",
806
	202 => "202 Accepted",
807
	203 => "203 Non-Authoritative Information",
808
	204 => "204 No Content",
809
	205 => "205 Reset Content",
810
	206 => "206 Partial Content",
811
	300 => "300 Multiple Choices",
812
	301 => "301 Moved Permanently",
813
	302 => "302 Found",
814
	303 => "303 See Other",
815
	304 => "304 Not Modified",
816
	305 => "305 Use Proxy",
817
	306 => "306 (Unused)",
818
	307 => "307 Temporary Redirect",
819
	400 => "400 Bad Request",
820
	401 => "401 Unauthorized",
821
	402 => "402 Payment Required",
822
	403 => "403 Forbidden",
823
	404 => "404 Not Found",
824
	405 => "405 Method Not Allowed",
825
	406 => "406 Not Acceptable",
826
	407 => "407 Proxy Authentication Required",
827
	408 => "408 Request Timeout",
828
	409 => "409 Conflict",
829
	410 => "410 Gone",
830
	411 => "411 Length Required",
831
	412 => "412 Precondition Failed",
832
	413 => "413 Request Entity Too Large",
833
	414 => "414 Request-URI Too Long",
834
	415 => "415 Unsupported Media Type",
835
	416 => "416 Requested Range Not Satisfiable",
836
	417 => "417 Expectation Failed",
837
	500 => "500 Internal Server Error",
838
	501 => "501 Not Implemented",
839
	502 => "502 Bad Gateway",
840
	503 => "503 Service Unavailable",
841
	504 => "504 Gateway Timeout",
842
	505 => "505 HTTP Version Not Supported"
843
);
844

    
845
function is_rfc2616_code($code) {
846
	global $rfc2616;
847
	if (isset($rfc2616[$code])) {
848
		return true;
849
	} else {
850
		return false;
851
	}
852
}
853

    
854
function print_rfc2616_select($tag, $current) {
855
	global $rfc2616;
856

    
857
	/* Default to 200 OK if not set */
858
	if ($current == "") {
859
		$current = 200;
860
	}
861

    
862
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
863
	foreach ($rfc2616 as $code => $message) {
864
		if ($code == $current) {
865
			$sel = " selected";
866
		} else {
867
			$sel = "";
868
		}
869
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
870
	}
871
	echo "</select>\n";
872
}
873

    
874
// Useful debugging function, much cleaner than print_r
875
function echo_array($array, $return_me = false) {
876
	if (is_array($array) == false) {
877
		$return = "The provided variable is not an array.";
878
	} else {
879
		foreach ($array as $name=>$value) {
880
			if (is_array($value)) {
881
				$return .= "";
882
				$return .= "['<b>$name</b>'] {<div style=\"margin-left:10px;\">\n";
883
				$return .= echo_array($value, true);
884
				$return .= "</div>}";
885
				$return .= "\n\n";
886
			} else {
887
				if (is_string($value)) {
888
					$value = "\"$value\"";
889
				}
890
				$return .= "['<b>$name</b>'] = $value\n\n";
891
			}
892
		}
893
	}
894
	if ($return_me == true) {
895
		return $return;
896
	} else {
897
		echo "<pre>".$return."</pre>";
898
	}
899
}
900

    
901
/****f* pfsense-utils/display_top_tabs
902
 * NAME
903
 *	 display_top_tabs - display tabs with rounded edges
904
 * INPUTS
905
 *	 $text	  - array of tabs
906
 * RESULT
907
 *	 null
908
 ******/
909
function display_top_tabs(& $tab_array, $no_drop_down = false, $type = 'pills', $usepost = "") {
910
	global $config;
911
	global $g;
912
	global $tab_array_indent;
913
	global $tab_array_space;
914
	global $tab_array_char_limit;
915

    
916
	/*	does the user have access to this tab?
917
	 *	master user has access to everything.
918
	 *	if the user does not have access, simply
919
	 *	unset the tab item.
920
	 */
921

    
922
	/* empty string code */
923
	if ($tab_array_indent == '') {
924
		$tab_array_indent = 0;
925
	}
926

    
927
	if ($tab_array_space == '') {
928
		$tab_array_space = 1;
929
	}
930

    
931
	if ($tab_array_char_limit == '') {
932
		$tab_array_char_limit = 256;
933
	}
934

    
935
	foreach ($tab_array as $tab_id => $ta) {
936
		if (!isAllowedPage($ta[2])) {
937
			unset ($tab_array[$tab_id]);
938
		}
939
	}
940

    
941
	$tab_active_bg	 = "#EEEEEE";
942
	$tab_inactive_bg = "#777777";
943
	$nifty_tabs_corners = "#FFF";
944
	$font_color = "white";
945

    
946
	$tabcharcount = 0;
947
	foreach ($tab_array as $ta) {
948
		$tabcharcount = $tabcharcount + strlen($ta[0]);
949
	}
950

    
951
	if ($no_drop_down == true) {
952
		$tabcharcount = 0;
953
		unset($tab_array_char_limit);
954
	}
955

    
956
	// If the character count of the tab names is > 670
957
	// then show a select item dropdown menubox.
958
	if ($tabcharcount > $tab_array_char_limit) {
959
		echo gettext("Currently viewing: ");
960
		echo "<select name=\"TabSelect\" onchange=\"tabs_will_go(this)\">\n";
961

    
962
		foreach ($tab_array as $ta) {
963
			if ($ta[1] == "true") {
964
				$selected = " selected";
965
			} else {
966
				$selected = "";
967
			}
968
			// Onclick in option will not work in some browser
969
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
970
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
971
		}
972

    
973
		echo "</select>\n<p>&nbsp;</p>";
974
		echo "<script type=\"text/javascript\">";
975
		echo "\n//<![CDATA[\n";
976
		if ($usepost == 'usepost') {
977
			echo " function tabs_will_go(obj){ var target = obj.value.split(\"?\"); postSubmit(get2post(target[1]),target[0]); }\n";
978
		} else {
979
			echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
980
		}
981
		echo "//]]>\n";
982
		echo "</script>";
983
	} else {
984
		echo '<ul class="nav nav-' . $type . '">';
985

    
986
		foreach ($tab_array as $ta) {
987
			echo '<li role="presentation"';
988
			if ($ta[1]) {
989
				echo ' class="active"';
990
			}
991

    
992
			echo '><a href="' . $ta[2] . '" ' . $usepost . '>' . $ta[0] . '</a></li>';
993
		}
994

    
995
		echo '</ul>';
996
	}
997
}
998

    
999
function add_package_tabs($tabgroup, &$tab_array) {
1000
	global $config, $g;
1001

    
1002
	if (!isset($config['installedpackages']['package'])) {
1003
		return;
1004
	}
1005

    
1006
	foreach ($config['installedpackages']['package'] as $pkg) {
1007
		if (!is_array($pkg['tabs']['tab'])) {
1008
			continue;
1009
		}
1010

    
1011
		foreach ($pkg['tabs']['tab'] as $tab) {
1012
			if ($tab['tabgroup'] != $tabgroup) {
1013
				continue;
1014
			}
1015
			$tab_entry = array();
1016
			if ($tab['name']) {
1017
				$tab_entry[] = $tab['name'];
1018
				$tab_entry[] = false;
1019
				$tab_entry[] = $tab['url'];
1020
				$tab_array[] = $tab_entry;
1021
			}
1022
		}
1023
	}
1024
}
1025

    
1026
function alias_info_popup($alias_id) {
1027
	global $config, $user_settings;
1028

    
1029
	if (!is_array($config['aliases']['alias'][$alias_id])) {
1030
		return;
1031
	}
1032

    
1033
	$maxlength = 60;
1034
	$alias = $config['aliases']['alias'][$alias_id];
1035
	$content = "";
1036

    
1037
	if ($user_settings['webgui']['disablealiaspopupdetail']) {
1038
		if (strlen($alias['descr']) >= $maxlength) {
1039
			$alias['descr'] = substr($alias['descr'], 0, $maxlength) . '&hellip;';
1040
		}
1041

    
1042
		$content .= $alias['descr'];
1043
	} else if ($alias['url']) {
1044
		// TODO: Change it when pf supports tables with ports
1045
		if ($alias['type'] == "urltable") {
1046
			exec("/sbin/pfctl -t {$alias['name']} -T show | wc -l", $total_entries);
1047
			$counter=preg_replace("/\D/", "", $total_entries[0]);
1048
			exec("/sbin/pfctl -t {$alias['name']} -T show | head -10002", $alias_addresses);
1049
		} else {
1050
			$urlfn = alias_expand_urltable($alias['name']);
1051
			$alias_addresses = explode("\n", file_get_contents($urlfn));
1052
			$counter = count($alias_addresses);
1053
		}
1054

    
1055
		$content .= '<h5>'. $alias['url'] .'</h5><ul><li>'. implode('</li><li>', $alias_addresses) .'</li></ul>';
1056
		if ($counter > 10002) {
1057
			$content .= '<i>'. gettext("listing only first 10k items") .'</i>';
1058
		}
1059
	} else {
1060
		$alias_addresses = explode (" ", $alias['address']);
1061
		$alias_details = explode ("||", $alias['detail']);
1062
		$idx = 0;
1063

    
1064
		$content .= "<table>\n";
1065
		$content .= "<thead>\n";
1066
		$content .= "<tr>\n";
1067
		$content .= "<th>" . gettext("Value") . "</th><th  style='padding-left: 10px;'>" . gettext("Description") . "</th></tr>\n";
1068
		$content .= "</thead>\n";
1069
		$content .= "<tbody>\n";
1070

    
1071
		foreach ($alias_addresses as $ap) {
1072
			$content .= "	<tr>\n";
1073
			$content .= "		<td>\n";
1074
			$content .= 			alias_idn_to_utf8($ap);
1075
			$content .=	"		</td>\n";
1076
			$content .= "		<td style='padding-left: 10px;'>\n";
1077
			$content .= 			htmlspecialchars($alias_details[$idx]);
1078
			$content .=	"		</td>\n";
1079
			$content .= "	</tr>\n";
1080
			$idx++;
1081
		}
1082

    
1083
		$content .= "</tbody>\n";
1084
		$content .= "</table>\n";
1085
	}
1086

    
1087
	return $content;
1088
}
1089

    
1090
function gateway_info_popup($showgw) {
1091
	global $config, $user_settings;
1092

    
1093
	init_config_arr(array('gateways', 'gateway_group'));
1094
	$a_gateways = return_gateways_array();
1095
	$gateways_status = return_gateways_status(true);
1096

    
1097
	$content = "";
1098
	$gws = array();
1099
	$bgdanger = array('force_down', 'down', 'highloss', 'highdelay');
1100
	$bgwarning = array('loss', 'delay');
1101
	$bgsuccess = array('none');
1102
	$bgcolor = "bg-info";
1103

    
1104
	if (is_array($a_gateways)) {
1105
		foreach ($a_gateways as $i => $gateway) {
1106
			if ($gateway['name'] == $showgw) {
1107
				$gws[] = $gateway['name'];
1108
			}
1109
		}
1110
	}
1111
	if (is_array($config['gateways']['gateway_group'])) {
1112
		foreach($config['gateways']['gateway_group'] as $gwgroup) {
1113
			if ($gwgroup['name'] == $showgw) {
1114
				foreach ($gwgroup['item'] as $member) {
1115
					$membersplit = explode("|", $member);
1116
					$gws[] = $membersplit[0];
1117
				}
1118
			}
1119
		}
1120
	}
1121

    
1122
	if (!empty($gws)) {
1123
		$content .= "<table>\n";
1124
		$content .= "<thead>\n";
1125
		$content .= "<tr>\n";
1126
		$content .= "<th>" . gettext("Name") . "</th><th style='padding-left: 10px;'>" . gettext("Interface") . "</th>";
1127
		$content .= "<th style='padding-left: 10px;'>" . gettext("Gateway") . "</th></tr>\n";
1128
		$content .= "</thead>\n";
1129
		$content .= "<tbody>\n";
1130
		foreach ($gws as $gw) {
1131
			foreach ($gateways_status as $gwstatus) {
1132
				if ($gwstatus['name'] == $gw) {
1133
					if (in_array($gwstatus['status'], $bgdanger)) {
1134
						$bgcolor = "bg-danger";
1135
					} elseif (in_array($gwstatus['status'], $bgwarning)) {
1136
						$bgcolor = "bg-warning";
1137
					} elseif (in_array($gwstatus['status'], $bgsuccess)) {
1138
						$bgcolor = "bg-success";
1139
					} else {
1140
						$bgcolor = "bg-info";
1141
					}
1142
				}
1143
			}
1144
			$content .= "	<tr class='{$bgcolor}'>\n";
1145
			$content .= "		<td>\n";
1146
			$content .= 			$gw;
1147
			$content .=	"		</td>\n";
1148
			$content .= "		<td style='padding-left: 10px;'>\n";
1149
			$content .= 			$config['interfaces'][lookup_gateway_interface_by_name($gw)]['descr'];
1150
			$content .=	"		</td>\n";
1151
			$content .= "		<td style='padding-left: 10px;'>\n";
1152
			$content .= 			lookup_gateway_ip_by_name($gw);
1153
			$content .=	"		</td>\n";
1154
			$content .= "	</tr>\n";
1155
			$idx++;
1156
		}
1157
		$content .= "</tbody>\n";
1158
		$content .= "</table>\n";
1159
	} else {
1160
		return;
1161
	}
1162

    
1163
	return $content;
1164
}
1165

    
1166
function rule_columns_with_alias($src, $srcport, $dst, $dstport, $target="", $targetport="") {
1167
	global $config;
1168

    
1169
	if ($config['aliases']['alias'] == "" || !is_array($config['aliases']['alias'])) {
1170
		return;
1171
	}
1172

    
1173
	$columns = array();
1174
	foreach ($config['aliases']['alias'] as $alias_id => $alias_name) {
1175
		if ($alias_name['name'] == $src) {
1176
			$columns['src'] = $alias_id;
1177
		}
1178
		if ($alias_name['name'] == $srcport) {
1179
			$columns['srcport'] = $alias_id;
1180
		}
1181
		if ($alias_name['name'] == $dst) {
1182
			$columns['dst'] = $alias_id;
1183
		}
1184
		if ($alias_name['name'] == $dstport) {
1185
			$columns['dstport'] = $alias_id;
1186
		}
1187
		if ($alias_name['name'] == $target) {
1188
			$columns['target'] = $alias_id;
1189
		}
1190
		if ($alias_name['name'] == $targetport) {
1191
			$columns['targetport'] = $alias_id;
1192
		}
1193
	}
1194

    
1195
	return $columns;
1196
}
1197

    
1198
function form_output_row($name, $label, $content) {
1199
var_dump($content);die;
1200
?>
1201
<div class="form-group">
1202
	<label for="<?=$name?>" class="col-sm-2 control-label"><?=gettext($label); ?></label>
1203
	<div class="col-sm-10">
1204
		<?=$content?>
1205
	</div>
1206
</div>
1207
<?php
1208
}
1209

    
1210
function set_flash_message($class, $msg) {
1211
	@phpsession_begin();
1212
	$_SESSION['flash_messages'][$class][] = $msg;
1213
	@phpsession_end(true);
1214
}
1215

    
1216
function get_flash_message() {
1217
	@phpsession_begin();
1218
	if (isset($_SESSION['flash_messages']) && !empty($_SESSION['flash_messages'])) {
1219
		foreach ($_SESSION['flash_messages'] as $class => $flash_message) {
1220
			print_info_box(implode("<br />", $flash_message), $class);
1221
		}
1222
		unset($_SESSION['flash_messages']);
1223
	}
1224
	@phpsession_end(true);
1225
}
1226

    
1227
/* Retrieve GET or POST Value/State
1228
 * Eample Usage:
1229
 * $value = getGETPOSTsettingvalue('get/post parameter name', "");
1230
 * $value = getGETPOSTsettingvalue('get/post parameter name', null);
1231
 * $state = getGETPOSTsettingvalue('get/post parameter name', null);
1232
 * $state = getGETPOSTsettingvalue('get/post parameter name', false);
1233
 */
1234
function getGETPOSTsettingvalue($settingname, $default) {
1235
	$settingvalue = $default;
1236
	if ($_GET[$settingname]) {
1237
		$settingvalue = $_GET[$settingname];
1238
	}
1239
	if ($_POST[$settingname]) {
1240
		$settingvalue = $_POST[$settingname];
1241
	}
1242
	return $settingvalue;
1243
}
1244

    
1245
/* set timezone */
1246
if (isset($config['system']['timezone']) &&
1247
    !empty($config['system']['timezone'])) {
1248
	$timezone = $config['system']['timezone'];
1249
} elseif (isset($g['default_timezone']) && !empty($g['default_timezone'])) {
1250
	$timezone = $g['default_timezone'];
1251
} else {
1252
	$timezone = "Etc/UTC";
1253
}
1254

    
1255
/* Remove files we do not want to see in a crash report */
1256
function cleanup_crash_file_list() {
1257
	$files = glob("/var/crash/*");
1258
	if (!is_array($files) || empty($files)) {
1259
		return array();
1260
	}
1261

    
1262
	$exclude_patterns = array(
1263
		'.*.last',
1264
		'bounds',
1265
		'minfree'
1266
	);
1267

    
1268
	foreach ($files as $idx => $fb) {
1269
		if (preg_match('/' . implode('|', $exclude_patterns) . '/', basename($fb)) == 1) {
1270
			unset($files[$idx]);
1271
		}
1272
	}
1273

    
1274
	return $files;
1275
}
1276

    
1277
function system_has_crash_data() {
1278
	/* Test if there are any crash data files present */
1279
	return count(cleanup_crash_file_list()) > 0;
1280
}
1281

    
1282
function system_has_php_errors() {
1283
	/* Check if the PHP error log is empty. Cast to int in case the file
1284
	 * does not exist and filesize() returns false. */
1285
	return (int) @filesize("/tmp/PHP_errors.log") > 0;
1286
}
1287

    
1288
date_default_timezone_set($timezone);
1289

    
1290
?>
(67-67/230)