Project

General

Profile

Download (31.2 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-2019 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
if (!$nocsrf) {
33
	function csrf_startup() {
34
		global $config;
35
		csrf_conf('rewrite-js', '/csrf/csrf-magic.js');
36
		$timeout_minutes = isset($config['system']['webgui']['session_timeout']) ? $config['system']['webgui']['session_timeout'] : 240;
37
		csrf_conf('expires', $timeout_minutes * 60);
38
	}
39
	require_once("csrf/csrf-magic.php");
40
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
41
		phpsession_end(true);
42
	}
43
}
44

    
45
/* make sure nothing is cached */
46
if (!$omit_nocacheheaders) {
47
	header("Expires: 0");
48
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
49
	header("Cache-Control: no-cache, no-store, must-revalidate");
50
	header("Pragma: no-cache");
51
}
52

    
53
require_once("authgui.inc");
54

    
55
/* parse the configuration and include all configuration functions */
56
require_once("functions.inc");
57

    
58
/* Include the autoloader for all the GUI display classes */
59
require_once("classes/autoload.inc.php");
60

    
61
/* used by progress bar */
62
$lastseen = "-1";
63

    
64
$navlevelsep = ": ";	/* navigation level separator string */
65
$mandfldhtml = "";		/* display this before mandatory input fields */
66
$mandfldhtmlspc = "";	/* same as above, but with spacing */
67

    
68
if (!function_exists('set_language')) {
69
	require_once("pfsense-utils.inc");
70
}
71

    
72
set_language();
73

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

    
86
/* Reserved table names to avoid collision */
87
$reserved_table_names = array(
88
	"bogons",
89
	"bogonsv6",
90
	"negate_networks",
91
	"snort2c",
92
	"sshguard",
93
	"tonatsubnets",
94
	"virusprot",
95
	"vpn_networks",
96
);
97

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

    
124
$auth_server_types = array(
125
	'ldap' => "LDAP",
126
	'radius' => "RADIUS");
127

    
128
$ldap_urltypes = array(
129
	'TCP - Standard' => 389,
130
	'TCP - STARTTLS' => 389,
131
	'SSL - Encrypted' => 636);
132

    
133
$ldap_scopes = array(
134
	'one' => gettext("One Level"),
135
	'subtree' => gettext("Entire Subtree"));
136

    
137
$ldap_protvers = array(
138
	2,
139
	3);
140

    
141
$ldap_templates = array(
142

    
143
	'open' => array(
144
		'desc' => "OpenLDAP",
145
		'attr_user' => "cn",
146
		'attr_group' => "cn",
147
		'attr_member' => "member",
148
		'allow_unauthenticated' => "true"),
149

    
150
	'msad' => array(
151
		'desc' => "Microsoft AD",
152
		'attr_user' => "samAccountName",
153
		'attr_group' => "cn",
154
		'attr_member' => "memberOf",
155
		'allow_unauthenticated' => "false"),
156

    
157
	'edir' => array(
158
		'desc' => "Novell eDirectory",
159
		'attr_user' => "cn",
160
		'attr_group' => "cn",
161
		'attr_member' => "uniqueMember",
162
		'allow_unauthenticated' => "false"));
163

    
164
$radius_srvcs = array(
165
	'both' => gettext("Authentication and Accounting"),
166
	'auth' => gettext("Authentication"),
167
	'acct' => gettext("Accounting"));
168

    
169
$radius_protocol = array(
170
	'PAP' => "PAP",
171
	'CHAP_MD5' => "MD5-CHAP",
172
	'MSCHAPv1' => "MS-CHAPv1",
173
	'MSCHAPv2' => "MS-CHAPv2");
174

    
175
$netbios_nodetypes = array(
176
	'0' => "none",
177
	'1' => "b-node",
178
	'2' => "p-node",
179
	'4' => "m-node",
180
	'8' => "h-node");
181

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
427
	return $padr;
428
}
429

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

    
433
	$pport = "";
434

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

    
449
	return $pport;
450
}
451

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

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

    
531
	return $item_set;
532
}
533

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

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

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

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

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

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

    
588
	return $bc;
589
}
590

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

    
599
	return $divstr;
600
}
601

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

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

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

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

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

    
648
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
649
	if (isset($adr['any'])) {
650
		$padr = "any";
651
	} else if ($adr['network']) {
652
		$padr = $adr['network'];
653
	} else if ($adr['address']) {
654
		list($padr, $pmask) = explode("/", $adr['address']);
655
		if (!$pmask) {
656
			if (is_ipaddrv6($padr)) {
657
				$pmask = 128;
658
			} else {
659
				$pmask = 32;
660
			}
661
		}
662
	}
663

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

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

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

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

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

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

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

    
725
function is_specialnet($net) {
726
	global $specialsrcdst;
727

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

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

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

    
767

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

    
780

    
781

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

    
792

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

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

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

    
856
function print_rfc2616_select($tag, $current) {
857
	global $rfc2616;
858

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

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

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

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

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

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

    
929
	if ($tab_array_space == '') {
930
		$tab_array_space = 1;
931
	}
932

    
933
	if ($tab_array_char_limit == '') {
934
		$tab_array_char_limit = 256;
935
	}
936

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

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

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

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

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

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

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

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

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

    
997
		echo '</ul>';
998
	}
999
}
1000

    
1001
function add_package_tabs($tabgroup, &$tab_array) {
1002
	global $config, $g;
1003

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

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

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

    
1028
function alias_info_popup($alias_id) {
1029
	global $config, $user_settings;
1030

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

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

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

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

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

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

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

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

    
1089
	return $content;
1090
}
1091

    
1092
function rule_columns_with_alias($src, $srcport, $dst, $dstport, $target="", $targetport="") {
1093
	global $config;
1094

    
1095
	if ($config['aliases']['alias'] == "" || !is_array($config['aliases']['alias'])) {
1096
		return;
1097
	}
1098

    
1099
	$columns = array();
1100
	foreach ($config['aliases']['alias'] as $alias_id => $alias_name) {
1101
		if ($alias_name['name'] == $src) {
1102
			$columns['src'] = $alias_id;
1103
		}
1104
		if ($alias_name['name'] == $srcport) {
1105
			$columns['srcport'] = $alias_id;
1106
		}
1107
		if ($alias_name['name'] == $dst) {
1108
			$columns['dst'] = $alias_id;
1109
		}
1110
		if ($alias_name['name'] == $dstport) {
1111
			$columns['dstport'] = $alias_id;
1112
		}
1113
		if ($alias_name['name'] == $target) {
1114
			$columns['target'] = $alias_id;
1115
		}
1116
		if ($alias_name['name'] == $targetport) {
1117
			$columns['targetport'] = $alias_id;
1118
		}
1119
	}
1120

    
1121
	return $columns;
1122
}
1123

    
1124
function form_output_row($name, $label, $content) {
1125
var_dump($content);die;
1126
?>
1127
<div class="form-group">
1128
	<label for="<?=$name?>" class="col-sm-2 control-label"><?=gettext($label); ?></label>
1129
	<div class="col-sm-10">
1130
		<?=$content?>
1131
	</div>
1132
</div>
1133
<?php
1134
}
1135

    
1136
function set_flash_message($class, $msg) {
1137
	@phpsession_begin();
1138
	$_SESSION['flash_messages'][$class][] = $msg;
1139
	@phpsession_end(true);
1140
}
1141

    
1142
function get_flash_message() {
1143
	@phpsession_begin();
1144
	if (isset($_SESSION['flash_messages']) && !empty($_SESSION['flash_messages'])) {
1145
		foreach ($_SESSION['flash_messages'] as $class => $flash_message) {
1146
			print_info_box(implode("<br />", $flash_message), $class);
1147
		}
1148
		unset($_SESSION['flash_messages']);
1149
	}
1150
	@phpsession_end(true);
1151
}
1152

    
1153
/* Retrieve GET or POST Value/State
1154
 * Eample Usage:
1155
 * $value = getGETPOSTsettingvalue('get/post parameter name', "");
1156
 * $value = getGETPOSTsettingvalue('get/post parameter name', null);
1157
 * $state = getGETPOSTsettingvalue('get/post parameter name', null);
1158
 * $state = getGETPOSTsettingvalue('get/post parameter name', false);
1159
 */
1160
function getGETPOSTsettingvalue($settingname, $default) {
1161
	$settingvalue = $default;
1162
	if ($_GET[$settingname]) {
1163
		$settingvalue = $_GET[$settingname];
1164
	}
1165
	if ($_POST[$settingname]) {
1166
		$settingvalue = $_POST[$settingname];
1167
	}
1168
	return $settingvalue;
1169
}
1170

    
1171
/* set timezone */
1172
if (isset($config['system']['timezone']) &&
1173
    !empty($config['system']['timezone'])) {
1174
	$timezone = $config['system']['timezone'];
1175
} elseif (isset($g['default_timezone']) && !empty($g['default_timezone'])) {
1176
	$timezone = $g['default_timezone'];
1177
} else {
1178
	$timezone = "Etc/UTC";
1179
}
1180

    
1181
/* Remove files we do not want to see in a crash report */
1182
function cleanup_crash_file_list() {
1183
	$files = glob("/var/crash/*");
1184
	if (!is_array($files) || empty($files)) {
1185
		return array();
1186
	}
1187

    
1188
	$exclude_patterns = array(
1189
		'.*.last',
1190
		'bounds',
1191
		'minfree'
1192
	);
1193

    
1194
	foreach ($files as $idx => $fb) {
1195
		if (preg_match('/' . implode('|', $exclude_patterns) . '/', basename($fb)) == 1) {
1196
			unset($files[$idx]);
1197
		}
1198
	}
1199

    
1200
	return $files;
1201
}
1202

    
1203
function system_has_crash_data() {
1204
	/* Test if there are any crash data files present */
1205
	return count(cleanup_crash_file_list()) > 0;
1206
}
1207

    
1208
function system_has_php_errors() {
1209
	/* Check if the PHP error log is empty. Cast to int in case the file
1210
	 * does not exist and filesize() returns false. */
1211
	return (int) @filesize("/tmp/PHP_errors.log") > 0;
1212
}
1213

    
1214
date_default_timezone_set($timezone);
1215

    
1216
?>
(66-66/225)