Project

General

Profile

Download (30.9 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * guiconfig.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * originally based on m0n0wall (http://m0n0.ch/wall)
10
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
 * All rights reserved.
12
 *
13
 * Licensed under the Apache License, Version 2.0 (the "License");
14
 * you may not use this file except in compliance with the License.
15
 * You may obtain a copy of the License at
16
 *
17
 * http://www.apache.org/licenses/LICENSE-2.0
18
 *
19
 * Unless required by applicable law or agreed to in writing, software
20
 * distributed under the License is distributed on an "AS IS" BASIS,
21
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
 * See the License for the specific language governing permissions and
23
 * limitations under the License.
24
 */
25

    
26
/* Include authentication routines */
27
/* THIS MUST BE ABOVE ALL OTHER CODE */
28
if (!$nocsrf) {
29
	function csrf_startup() {
30
		global $config;
31
		csrf_conf('rewrite-js', '/csrf/csrf-magic.js');
32
		$timeout_minutes = isset($config['system']['webgui']['session_timeout']) ? $config['system']['webgui']['session_timeout'] : 240;
33
		csrf_conf('expires', $timeout_minutes * 60);
34
	}
35
	require_once("csrf/csrf-magic.php");
36
}
37

    
38
/* make sure nothing is cached */
39
if (!$omit_nocacheheaders) {
40
	header("Expires: 0");
41
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
42
	header("Cache-Control: no-cache, no-store, must-revalidate");
43
	header("Pragma: no-cache");
44
}
45

    
46
header("X-Frame-Options: SAMEORIGIN");
47
require_once("authgui.inc");
48

    
49
/* parse the configuration and include all configuration functions */
50
require_once("functions.inc");
51

    
52
/* Include the autoloader for all the GUI display classes */
53
require_once("classes/autoload.inc.php");
54

    
55
/* used by progress bar */
56
$lastseen = "-1";
57

    
58
$navlevelsep = ": ";	/* navigation level separator string */
59
$mandfldhtml = "";		/* display this before mandatory input fields */
60
$mandfldhtmlspc = "";	/* same as above, but with spacing */
61

    
62
if (!function_exists('set_language')) {
63
	require_once("pfsense-utils.inc");
64
}
65

    
66
set_language();
67

    
68
/* Some ajax scripts still need access to GUI */
69
if (!$ignorefirmwarelock) {
70
	if (is_subsystem_dirty('firmwarelock')) {
71
		if (!$d_isfwfile) {
72
			header("Location: system_update.php");
73
			exit;
74
		} else {
75
			return;
76
		}
77
	}
78
}
79

    
80
/* Reserved table names to avoid collision */
81
$reserved_table_names = array(
82
	"bogons",
83
	"bogonsv6",
84
	"negate_networks",
85
	"snort2c",
86
	"sshlockout",
87
	"tonatsubnets",
88
	"virusprot",
89
	"vpn_networks",
90
	"webConfiguratorlockout"
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
	'TCP - Standard' => 389,
125
	'SSL - 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

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

    
149
	'edir' => array(
150
		'desc' => "Novell eDirectory",
151
		'attr_user' => "cn",
152
		'attr_group' => "cn",
153
		'attr_member' => "uniqueMember"));
154

    
155
$radius_srvcs = array(
156
	'both' => gettext("Authentication and Accounting"),
157
	'auth' => gettext("Authentication"),
158
	'acct' => gettext("Accounting"));
159

    
160
$netbios_nodetypes = array(
161
	'0' => "none",
162
	'1' => "b-node",
163
	'2' => "p-node",
164
	'4' => "m-node",
165
	'8' => "h-node");
166

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

    
214
/* TCP flags */
215
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg", "ece", "cwr");
216

    
217
$specialnets = array(
218
	"(self)" => gettext("This Firewall"),
219
	"pppoe" => gettext("PPPoE clients"),
220
	"l2tp" => gettext("L2TP clients"));
221

    
222
$spiflist = get_configured_interface_with_descr(false, true);
223
foreach ($spiflist as $ifgui => $ifdesc) {
224
	$specialnets[$ifgui] = $ifdesc . " net";
225
	$specialnets[$ifgui . 'ip'] = $ifdesc . " address";
226
}
227

    
228
$medias = array(
229
	"auto" => gettext("autoselect"),
230
	"100full" => gettext("100BASE-TX full-duplex"),
231
	"100half" => gettext("100BASE-TX half-duplex"),
232
	"10full" => gettext("10BASE-T full-duplex"),
233
	"10half" => gettext("10BASE-T half-duplex"));
234

    
235
$wlan_modes = array(
236
	"bss" => gettext("Infrastructure (BSS)"),
237
	"adhoc" => gettext("Ad-hoc (IBSS)"),
238
	"hostap" => gettext("Access Point"));
239

    
240
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, &$input_errors) {
241

    
242
	/* check for bad control characters */
243
	foreach ($postdata as $pn => $pd) {
244
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
245
			$input_errors[] = sprintf(gettext("The field %s contains invalid characters."), $pn);
246
		}
247
	}
248

    
249
	for ($i = 0; $i < count($reqdfields); $i++) {
250
		if ($_POST[$reqdfields[$i]] == "" && $_REQUEST[$reqdfields[$i]] == "") {
251
			$input_errors[] = sprintf(gettext("The field %s is required."), $reqdfieldsn[$i]);
252
		}
253
	}
254
}
255

    
256
function print_input_errors($input_errors) {
257
	echo '<div class="alert alert-danger input-errors">';
258
	echo '<p>' . gettext('The following input errors were detected:') . '</p>';
259
	echo '<ul>';
260

    
261
	foreach ($input_errors as $ierr) {
262
		echo '<li>' . htmlspecialchars($ierr) . '</li>';
263
	}
264

    
265
	echo '</ul>';
266
	echo '</div>';
267
}
268

    
269
function verify_gzip_file($fname) {
270
	$returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
271
	if ($returnvar != 0) {
272
		return 0;
273
	} else {
274
		return 1;
275
	}
276
}
277

    
278
// print_info_box() has been updated so that any required button is explicitly created, rather than relying on the detection of certain
279
// strings in the message (such as "apply"). print_info_box_np() has been exterminated.
280
// $class = the bootstrap style class (default, info, warning, success, danger)
281
// $btnname and btntext describe the optional button and its display text, the default is an 'x' Close button.
282
// Note that there is also a shortcut function print_apply_box here that creates a standard "apply" box for you.
283
// In many cases just substitute that for print_info_box_np() to easily get a warning style "Apply changes" box.
284
function print_info_box($msg, $class="alert-warning", $btnname = "close", $btntext = "", $btnicon = "", $btnclass = "default") {
285

    
286
	if (strpos($class, "alert-") !== 0) {
287
		$class = 'alert-' . $class;
288
	}
289

    
290
	$msg = '<div class="pull-left">' . $msg . '</div>';
291

    
292
	if ($btnname === "close") {
293
		$msg = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' . $msg;
294
	} else if ($btnname != "") {
295
		if (empty($btntext)) {
296
			$btntext = $btnname;
297
		}
298
		if (!empty($btnicon)) {
299
			$btnicon = '<i class="fa ' . $btnicon . ' icon-embed-btn"></i>';
300
		}
301

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

    
304
		if ($_POST['if']) {
305
			$msg .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
306
		}
307

    
308
		$msg .= '</form>';
309
	}
310

    
311
	echo '<div class="alert ' . $class . ' clearfix" role="alert">' . $msg . '</div>';
312
}
313

    
314
function print_apply_box($msg) {
315
	print_info_box($msg, "warning", "apply", gettext("Apply Changes"), 'fa-check', 'success');
316
}
317

    
318
/*
319
 * Print Bootstrap callout
320
 *
321
 * @param string $msg     message to display
322
 * @param string $class   contextual class, defaults to info (default | danger | warning | info)
323
 * @param string $heading optional callout heading
324
 */
325
function print_callout($msg, $class = 'info', $heading = '') {
326

    
327
	if ('' == $msg) {
328
		return;
329
	}
330
	$class = strtolower($class);
331
	$callout = '';
332

    
333
	if ($class != 'default' && $class != 'danger' && $class != 'warning' && $class != 'info') {
334
		$class = 'info';
335
	}
336
	$callout .= '<div class="bs-callout bs-callout-' . $class . '">';
337

    
338
	if ('' != $heading) {
339
		$callout .= '<h4>' . $heading . '</h4>';
340
	}
341
	$callout .= $msg . '</div>';
342
	echo $callout;
343
}
344

    
345
function get_std_save_message($ok) {
346
	$filter_related = false;
347
	$filter_pages = array("nat", "filter");
348
	$to_return = gettext("The changes have been applied successfully.");
349
	foreach ($filter_pages as $fp) {
350
		if (stristr($_SERVER['SCRIPT_FILENAME'], $fp)) {
351
			$filter_related = true;
352
		}
353
	}
354
	if ($filter_related) {
355
		$to_return .= "<br />" . gettext("<a href=\"status_filter_reload.php\">Monitor</a> the filter reload progress.");
356
	}
357
	return $to_return;
358
}
359

    
360
function pprint_address($adr) {
361
	global $specialnets;
362

    
363
	if (isset($adr['any'])) {
364
		$padr = "*";
365
	} else if ($adr['network']) {
366
		$padr = $specialnets[$adr['network']];
367
	} else {
368
		$padr = $adr['address'];
369
	}
370

    
371
	if (isset($adr['not'])) {
372
		$padr = "! " . $padr;
373
	}
374

    
375
	return $padr;
376
}
377

    
378
function pprint_port($port) {
379
	global $wkports;
380

    
381
	$pport = "";
382

    
383
	if (!$port) {
384
		return "*";
385
	} else {
386
		$srcport = explode("-", $port);
387
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
388
			$pport = $srcport[0];
389
			if ($wkports[$srcport[0]]) {
390
				$pport .= " (" . $wkports[$srcport[0]] . ")";
391
			}
392
		} else {
393
			$pport .= $srcport[0] . " - " . $srcport[1];
394
		}
395
	}
396

    
397
	return $pport;
398
}
399

    
400
function firewall_check_for_advanced_options(&$item) {
401
	$item_set = "";
402
	if ($item['os']) {
403
			$item_set .= "os {$item['os']} ";
404
	}
405
	if ($item['dscp']) {
406
		$item_set .= "dscp {$item['dscp']} ";
407
	}
408
	if ($item['max']) {
409
		$item_set .= "max {$item['max']} ";
410
	}
411
	if ($item['max-src-nodes']) {
412
		$item_set .= "max-src-nodes {$item['max-src-nodes']} ";
413
	}
414
	if ($item['max-src-conn']) {
415
		$item_set .= "max-src-conn {$item['max-src-conn']} ";
416
	}
417
	if ($item['max-src-states']) {
418
		$item_set .= "max-src-states {$item['max-src-states']} ";
419
	}
420
	if (isset($item['nopfsync'])) {
421
		$item_set .= "nopfsync ";
422
	}
423
	if ($item['statetype'] != "keep state" && $item['statetype'] != "") {
424
		$item_set .= "statetype {$item['statetype']} ";
425
	}
426
	if ($item['statetimeout']) {
427
		$item_set .= "statetimeout {$item['statetimeout']} ";
428
	}
429
	if (isset($item['nosync'])) {
430
		$item_set .= "no XMLRPC Sync ";
431
	}
432
	if ($item['max-src-conn-rate']) {
433
		$item_set .= "max-src-conn-rate {$item['max-src-conn-rate']} ";
434
	}
435
	if ($item['max-src-conn-rates']) {
436
		$item_set .= "max-src-conn-rates {$item['max-src-conn-rates']} ";
437
	}
438
	if ($item['vlanprio']) {
439
		$item_set .= "vlanprio {$item['vlanprio']} ";
440
	}
441
	if ($item['vlanprioset']) {
442
		$item_set .= "vlanprioset {$item['vlanprioset']} ";
443
	}
444
	if ($item['gateway']) {
445
		$item_set .= "gateway {$item['gateway']} ";
446
	}
447
	if ($item['dnpipe']) {
448
		$item_set .= "limiter {$item['dnpipe']} ";
449
	}
450
	if ($item['pdnpipe']) {
451
		$item_set .= "limiter {$item['pdnpipe']} ";
452
	}
453
	if ($item['ackqueue']) {
454
		$item_set .= "ackqueue {$item['ackqueue']} ";
455
	}
456
	if ($item['defaultqueue']) {
457
		$item_set .= "defaultqueue {$item['defaultqueue']} ";
458
	}
459
	if ($item['tag']) {
460
		$item_set .= "tag {$item['tag']} ";
461
	}
462
	if ($item['tagged']) {
463
		$item_set .= "tagged {$item['tagged']} ";
464
	}
465
	if (isset($item['allowopts'])) {
466
		$item_set .= "allowopts ";
467
	}
468
	if (isset($item['disablereplyto'])) {
469
		$item_set .= "disable reply-to ";
470
	}
471
	if ($item['tcpflags_any'] || $item['tcpflags1'] || $item['tcpflags2']) {
472
		$item_set .= "tcpflags set";
473
	}
474

    
475
	return $item_set;
476
}
477

    
478
function gentitle($title) {
479
	global $navlevelsep;
480
	if (!is_array($title)) {
481
		return $title;
482
	} else {
483
		return join($navlevelsep, $title);
484
	}
485
}
486

    
487
function genhtmltitle($title) {
488

    
489
	// If the array contains only one element, there are no breadcrumbs, so don't
490
	// add anything else
491
	if (count($title) > 1) {
492
		$bc = '<ol class="breadcrumb">';
493

    
494
		foreach ($title as $el) {
495
			$bc .= '<li>'.$el.'</li>';
496
		}
497

    
498
		$bc .= '</ol>';
499
	} else {
500
		$bc = "";
501
	}
502

    
503
	return $heading . $bc;
504
}
505

    
506
/* update the changedesc and changecount(er) variables */
507
function update_changedesc($update) {
508
	global $changedesc;
509
	global $changecount;
510

    
511
	$changedesc .= " {$update}";
512
	$changecount++;
513
}
514

    
515
// This version of dump_clog() does not output <td></td> or any other table elements.
516
function dump_clog_no_table($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
517
	global $g, $config;
518
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
519
	$specific_log = basename($logfile, '.log') . '_settings';
520
	if ($config['syslog'][$specific_log]['cronorder'] == 'forward') $sor = "";
521
	if ($config['syslog'][$specific_log]['cronorder'] == 'reverse') $sor = "-r";
522
	$logarr = array();
523
	$grepline = "  ";
524
	if (is_array($grepfor)) {
525
		$invert = '';
526
		if ((strpos($grepfor[0], '!') === 0)) {
527
			$grepfor[0] = substr($grepfor[0], 1);
528
			$invert = '-v';
529
		}
530
		$grepline .= " | /usr/bin/egrep {$invert} " . escapeshellarg(implode("|", $grepfor));
531
	}
532
	if (is_array($grepinvert)) {
533
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
534
	}
535
	if (is_dir($logfile)) {
536
		$logarr = array(sprintf(gettext("File %s is a directory."), $logfile));
537
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
538
		$logarr = array(gettext("Log file started."));
539
	} else {
540
		if ($config['system']['disablesyslogclog']) {
541
			exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
542
		} else {
543
			exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
544
		}
545
	}
546
	echo "\n";
547

    
548
	$rows = 0;
549
	foreach ($logarr as $logent) {
550
		$rows++;
551
		$logent = preg_split("/\s+/", $logent, 6);
552

    
553
		if ($withorig) {
554
				$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
555
				$entry_text = ($logent[3] ==  $config['system']['hostname']) ? "" : $logent[3] . " ";
556
				$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
557
				echo "{$entry_date_time}";
558
				echo " " . "{$entry_text}"	. "\n";
559
		} else {
560
				echo htmlspecialchars($logent[5]) . "\n";
561
		}
562

    
563
	}
564
	return($rows);
565
}
566

    
567
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
568
	global $g, $config;
569
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
570
	$specific_log = basename($logfile, '.log') . '_settings';
571
	if ($config['syslog'][$specific_log]['cronorder'] == 'forward') $sor = "";
572
	if ($config['syslog'][$specific_log]['cronorder'] == 'reverse') $sor = "-r";
573
	$logarr = array();
574
	$grepline = "  ";
575
	if (is_array($grepfor)) {
576
		$invert = '';
577
		if ((strpos($grepfor[0], '!') === 0)) {
578
			$grepfor[0] = substr($grepfor[0], 1);
579
			$invert = '-v';
580
		}
581
		$grepline .= " | /usr/bin/egrep {$invert} " . escapeshellarg(implode("|", $grepfor));
582
	}
583
	if (is_array($grepinvert)) {
584
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
585
	}
586
	if (is_dir($logfile)) {
587
		$logarr = array(sprintf(gettext("File %s is a directory."), $logfile));
588
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
589
		$logarr = array(gettext("Log file started."));
590
	} else {
591
		if ($config['system']['disablesyslogclog']) {
592
			exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
593
		} else {
594
			exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
595
		}
596
	}
597

    
598
	$rows = 0;
599
	foreach ($logarr as $logent) {
600
		$rows++;
601
		$logent = preg_split("/\s+/", $logent, 6);
602
		echo "<tr>\n";
603
		if ($withorig) {
604
			$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
605
			$entry_text = ($logent[3] == $config['system']['hostname']) ? "" : $logent[3] . " ";
606
			$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
607
			echo "<td class=\"text-nowrap\">{$entry_date_time}</td>\n";
608
			echo "<td style=\"word-wrap:break-word; word-break:break-all; white-space:normal\">{$entry_text}</td>\n";
609
		} else {
610
				echo "<td>" . htmlspecialchars($logent[5]) . "</td>\n";
611
		}
612
		echo "</tr>\n";
613
	}
614
	return($rows);
615
}
616

    
617
function return_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
618
	global $g, $config;
619
	$sor = (isset($config['syslog']['reverse']) || $grepreverse) ? "-r" : "";
620
	$specific_log = basename($logfile, '.log') . '_settings';
621
	if (($config['syslog'][$specific_log]['cronorder'] == 'forward') && !$grepreverse) $sor = "";
622
	if (($config['syslog'][$specific_log]['cronorder'] == 'reverse') ||  $grepreverse) $sor = "-r";
623
	$logarr = array();
624
	$grepline = "  ";
625
	if (is_array($grepfor)) {
626
		$grepline .= " | /usr/bin/egrep " . escapeshellarg(implode("|", $grepfor));
627
	}
628
	if (is_array($grepinvert)) {
629
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
630
	}
631
	if ($config['system']['disablesyslogclog']) {
632
		exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
633
	} else {
634
		exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
635
	}
636
	return($logarr);
637
}
638

    
639
/* Check if variable has changed, update and log if it has
640
 * returns true if var changed
641
 * varname = variable name in plain text
642
 * orig = original value
643
 * new = new value
644
 */
645
function update_if_changed($varname, & $orig, $new) {
646
	if (is_array($orig) && is_array($new)) {
647
		$a_diff = array_diff($orig, $new);
648
		foreach ($a_diff as $diff) {
649
			update_changedesc("removed {$varname}: \"{$diff}\"");
650
		}
651
		$a_diff = array_diff($new, $orig);
652
		foreach ($a_diff as $diff) {
653
			update_changedesc("added {$varname}: \"{$diff}\"");
654
		}
655
		$orig = $new;
656
		return true;
657

    
658
	} else {
659
		if ($orig != $new) {
660
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
661
			$orig = $new;
662
			return true;
663
		}
664
	}
665
	return false;
666
}
667

    
668
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
669
	if (isset($adr['any'])) {
670
		$padr = "any";
671
	} else if ($adr['network']) {
672
		$padr = $adr['network'];
673
	} else if ($adr['address']) {
674
		list($padr, $pmask) = explode("/", $adr['address']);
675
		if (!$pmask) {
676
			if (is_ipaddrv6($padr)) {
677
				$pmask = 128;
678
			} else {
679
				$pmask = 32;
680
			}
681
		}
682
	}
683

    
684
	if (isset($adr['not'])) {
685
		$pnot = 1;
686
	} else {
687
		$pnot = 0;
688
	}
689

    
690
	if ($adr['port']) {
691
		list($pbeginport, $pendport) = explode("-", $adr['port']);
692
		if (!$pendport) {
693
			$pendport = $pbeginport;
694
		}
695
	} else if (!is_alias($pbeginport) && !is_alias($pendport)) {
696
		$pbeginport = "any";
697
		$pendport = "any";
698
	}
699
}
700

    
701
function pconfig_to_address(&$adr, $padr, $pmask, $pnot = false, $pbeginport = 0, $pendport = 0) {
702
	$adr = array();
703

    
704
	if ($padr == "any") {
705
		$adr['any'] = true;
706
	} else if (is_specialnet($padr)) {
707
		$adr['network'] = $padr;
708
	} else {
709
		$adr['address'] = $padr;
710
		if (is_ipaddrv6($padr)) {
711
			if ($pmask != 128) {
712
				$adr['address'] .= "/" . $pmask;
713
			}
714
		} else {
715
			if ($pmask != 32) {
716
				$adr['address'] .= "/" . $pmask;
717
			}
718
		}
719
	}
720

    
721
	if ($pnot) {
722
		$adr['not'] = true;
723
	} else {
724
		unset($adr['not']);
725
	}
726

    
727
	if (($pbeginport != 0) && ($pbeginport != "any")) {
728
		if ($pbeginport != $pendport) {
729
			$adr['port'] = $pbeginport . "-" . $pendport;
730
		} else {
731
			$adr['port'] = $pbeginport;
732
		}
733
	}
734

    
735
	if (is_alias($pbeginport)) {
736
		$adr['port'] = $pbeginport;
737
	}
738
}
739

    
740
function is_specialnet($net) {
741
	global $specialsrcdst;
742

    
743
	if (!$net) {
744
		return false;
745
	}
746
	if (in_array($net, $specialsrcdst)) {
747
		return true;
748
	} else {
749
		return false;
750
	}
751
}
752

    
753
//function to create widget tabs when called
754
function display_widget_tabs(& $tab_array) {
755
	echo "<div id=\"tabs\">";
756
	$tabscounter = 0;
757
	foreach ($tab_array as $ta) {
758
		$dashpos = strpos($ta[2], '-');
759
		$tabname = $ta[2] . "-tab";
760
		$tabclass = substr($ta[2], 0, $dashpos);
761
		$tabclass = $tabclass . "-class";
762
		if ($ta[1] == true) {
763
			$tabActive = "table-cell";
764
			$tabNonActive = "none";
765
		} else {
766
			$tabActive = "none";
767
			$tabNonActive = "table-cell";
768
		}
769
		echo "<div id=\"{$ta[2]}-active\" class=\"{$tabclass}-tabactive\" style=\"display:{$tabActive}; background-color:#EEEEEE; color:black;\">";
770
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
771
		echo "&nbsp;&nbsp;&nbsp;</b>";
772
		echo "</div>";
773

    
774
		echo "<div id=\"{$ta[2]}-deactive\" class=\"{$tabclass}-tabdeactive\" style=\"display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;\" onclick=\"return changeTabDIV('{$ta[2]}')\">";
775
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
776
		echo "&nbsp;&nbsp;&nbsp;</b>";
777
		echo "</div>";
778
	}
779
	echo "</div>";
780
}
781

    
782

    
783
// Return inline javascript file or CSS to minimize
784
// request count going back to server.
785
function outputJavaScriptFileInline($javascript) {
786
	if (file_exists($javascript)) {
787
		echo "\n<script type=\"text/javascript\">\n";
788
		include_once($javascript);
789
		echo "\n</script>\n";
790
	} else {
791
		echo "\n\n<!-- Could not locate file:  {$javascript} -->\n\n";
792
	}
793
}
794

    
795

    
796

    
797
function outputCSSPrintFileInline($css) {
798
	if (file_exists($css)) {
799
		echo "\n<style media=\"print\" type=\"text/css\">\n";
800
		include_once($css);
801
		echo "\n</style>\n";
802
	} else {
803
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
804
	}
805
}
806

    
807

    
808
function outputCSSFileInline($css) {
809
	if (file_exists($css)) {
810
		echo "\n<style type=\"text/css\">\n";
811
		include_once($css);
812
		echo "\n</style>\n";
813
	} else {
814
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
815
	}
816
}
817

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

    
862
function is_rfc2616_code($code) {
863
	global $rfc2616;
864
	if (isset($rfc2616[$code])) {
865
		return true;
866
	} else {
867
		return false;
868
	}
869
}
870

    
871
function print_rfc2616_select($tag, $current) {
872
	global $rfc2616;
873

    
874
	/* Default to 200 OK if not set */
875
	if ($current == "") {
876
		$current = 200;
877
	}
878

    
879
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
880
	foreach ($rfc2616 as $code => $message) {
881
		if ($code == $current) {
882
			$sel = " selected";
883
		} else {
884
			$sel = "";
885
		}
886
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
887
	}
888
	echo "</select>\n";
889
}
890

    
891
// Useful debugging function, much cleaner than print_r
892
function echo_array($array, $return_me = false) {
893
	if (is_array($array) == false) {
894
		$return = "The provided variable is not an array.";
895
	} else {
896
		foreach ($array as $name=>$value) {
897
			if (is_array($value)) {
898
				$return .= "";
899
				$return .= "['<b>$name</b>'] {<div style=\"margin-left:10px;\">\n";
900
				$return .= echo_array($value, true);
901
				$return .= "</div>}";
902
				$return .= "\n\n";
903
			} else {
904
				if (is_string($value)) {
905
					$value = "\"$value\"";
906
				}
907
				$return .= "['<b>$name</b>'] = $value\n\n";
908
			}
909
		}
910
	}
911
	if ($return_me == true) {
912
		return $return;
913
	} else {
914
		echo "<pre>".$return."</pre>";
915
	}
916
}
917

    
918
/****f* pfsense-utils/display_top_tabs
919
 * NAME
920
 *	 display_top_tabs - display tabs with rounded edges
921
 * INPUTS
922
 *	 $text	  - array of tabs
923
 * RESULT
924
 *	 null
925
 ******/
926
function display_top_tabs(& $tab_array, $no_drop_down = false, $type = 'pills') {
927
	global $config;
928
	global $g;
929
	global $tab_array_indent;
930
	global $tab_array_space;
931
	global $tab_array_char_limit;
932

    
933
	/*	does the user have access to this tab?
934
	 *	master user has access to everything.
935
	 *	if the user does not have access, simply
936
	 *	unset the tab item.
937
	 */
938

    
939
	/* empty string code */
940
	if ($tab_array_indent == '') {
941
		$tab_array_indent = 0;
942
	}
943

    
944
	if ($tab_array_space == '') {
945
		$tab_array_space = 1;
946
	}
947

    
948
	if ($tab_array_char_limit == '') {
949
		$tab_array_char_limit = 92;
950
	}
951

    
952
	foreach ($tab_array as $tab_id => $ta) {
953
		if (!isAllowedPage($ta[2])) {
954
			unset ($tab_array[$tab_id]);
955
		}
956
	}
957

    
958
	$tab_active_bg	 = "#EEEEEE";
959
	$tab_inactive_bg = "#777777";
960
	$nifty_tabs_corners = "#FFF";
961
	$font_color = "white";
962

    
963
	$tabcharcount = 0;
964
	foreach ($tab_array as $ta) {
965
		$tabcharcount = $tabcharcount + strlen($ta[0]);
966
	}
967

    
968
	if ($no_drop_down == true) {
969
		$tabcharcount = 0;
970
		unset($tab_array_char_limit);
971
	}
972

    
973
	// If the character count of the tab names is > 670
974
	// then show a select item dropdown menubox.
975
	if ($tabcharcount > $tab_array_char_limit) {
976
		echo gettext("Currently viewing: ");
977
		echo "<select name=\"TabSelect\" onchange=\"tabs_will_go(this)\">\n";
978

    
979
		foreach ($tab_array as $ta) {
980
			if ($ta[1] == "true") {
981
				$selected = " selected";
982
			} else {
983
				$selected = "";
984
			}
985
			// Onclick in option will not work in some browser
986
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
987
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
988
		}
989

    
990
		echo "</select>\n<p>&nbsp;</p>";
991
		echo "<script type=\"text/javascript\">";
992
		echo "\n//<![CDATA[\n";
993
		echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
994
		echo "//]]>\n";
995
		echo "</script>";
996
	} else {
997
		echo '<ul class="nav nav-' . $type . '">';
998

    
999
		foreach ($tab_array as $ta) {
1000
			echo '<li role="presentation"';
1001
			if ($ta[1]) {
1002
				echo ' class="active"';
1003
			}
1004

    
1005
			echo '><a href="' . $ta[2] . '">' . $ta[0] . '</a></li>';
1006
		}
1007

    
1008
		echo '</ul>';
1009
	}
1010
}
1011

    
1012
function add_package_tabs($tabgroup, &$tab_array) {
1013
	global $config, $g;
1014

    
1015
	if (!isset($config['installedpackages']['package'])) {
1016
		return;
1017
	}
1018

    
1019
	foreach ($config['installedpackages']['package'] as $pkg) {
1020
		$pkg_config = read_package_configurationfile($pkg['name']);
1021

    
1022
		if (!isset($pkg_config['tabs']['tab'])) {
1023
			continue;
1024
		}
1025

    
1026
		foreach ($pkg_config['tabs']['tab'] as $tab) {
1027
			$tab_entry = array();
1028
			if ($tab['name']) {
1029
				$tab_entry[] = $tab['name'];
1030
				$tab_entry[] = false;
1031
				$tab_entry[] = $tab['url'];
1032
				$tab_array[] = $tab_entry;
1033
			}
1034
		}
1035
	}
1036
}
1037

    
1038
function alias_info_popup($alias_id) {
1039
	global $config;
1040

    
1041
	if (!is_array($config['aliases']['alias'][$alias_id])) {
1042
		return;
1043
	}
1044

    
1045
	$maxlength = 60;
1046
	$alias = $config['aliases']['alias'][$alias_id];
1047
	$content = "";
1048

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

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

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

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

    
1089
		$content .= "</tbody>\n";
1090
		$content .= "<table>\n";
1091
	}
1092

    
1093
	if (strlen($alias['descr']) >= $maxlength) {
1094
		$alias['descr'] = substr($alias['descr'], 0, $maxlength) . '&hellip;';
1095
	}
1096

    
1097
	return $content;
1098
}
1099

    
1100
function rule_columns_with_alias($src, $srcport, $dst, $dstport, $target="", $targetport="") {
1101
	global $config;
1102

    
1103
	if ($config['aliases']['alias'] == "" || !is_array($config['aliases']['alias'])) {
1104
		return;
1105
	}
1106

    
1107
	$columns = array();
1108
	foreach ($config['aliases']['alias'] as $alias_id => $alias_name) {
1109
		if ($alias_name['name'] == $src) {
1110
			$columns['src'] = $alias_id;
1111
		}
1112
		if ($alias_name['name'] == $srcport) {
1113
			$columns['srcport'] = $alias_id;
1114
		}
1115
		if ($alias_name['name'] == $dst) {
1116
			$columns['dst'] = $alias_id;
1117
		}
1118
		if ($alias_name['name'] == $dstport) {
1119
			$columns['dstport'] = $alias_id;
1120
		}
1121
		if ($alias_name['name'] == $target) {
1122
			$columns['target'] = $alias_id;
1123
		}
1124
		if ($alias_name['name'] == $targetport) {
1125
			$columns['targetport'] = $alias_id;
1126
		}
1127
	}
1128

    
1129
	return $columns;
1130
}
1131

    
1132
function form_output_row($name, $label, $content) {
1133
var_dump($content);die;
1134
?>
1135
<div class="form-group">
1136
	<label for="<?=$name?>" class="col-sm-2 control-label"><?=gettext($label); ?></label>
1137
	<div class="col-sm-10">
1138
		<?=$content?>
1139
	</div>
1140
</div>
1141
<?php
1142
}
1143

    
1144
function set_flash_message($class, $msg) {
1145
	@session_start();
1146
	$_SESSION['flash_messages'][$class][] = $msg;
1147
}
1148

    
1149
function get_flash_message() {
1150
	@session_start();
1151
	if (isset($_SESSION['flash_messages']) && !empty($_SESSION['flash_messages'])) {
1152
		foreach ($_SESSION['flash_messages'] as $class => $flash_message) {
1153
			print_info_box(implode("<br />", $flash_message), $class);
1154
		}
1155
		unset($_SESSION['flash_messages']);
1156
	}
1157
}
1158

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

    
1177
/* set timezone */
1178
if (isset($config['system']['timezone']) &&
1179
    !empty($config['system']['timezone'])) {
1180
	$timezone = $config['system']['timezone'];
1181
} elseif (isset($g['default_timezone']) && !empty($g['default_timezone'])) {
1182
	$timezone = $g['default_timezone'];
1183
} else {
1184
	$timezone = "Etc/UTC";
1185
}
1186

    
1187
date_default_timezone_set($timezone);
1188

    
1189
?>
(63-63/227)