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
		csrf_conf('rewrite-js', '/csrf/csrf-magic.js');
31
		$timeout_minutes = isset($config['system']['webgui']['session_timeout']) ? $config['system']['webgui']['session_timeout'] : 240;
32
		csrf_conf('expires', $timeout_minutes * 60);
33
	}
34
	require_once("csrf/csrf-magic.php");
35
}
36

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

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

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

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

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

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

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

    
65
set_language();
66

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

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

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

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

    
134
$ldap_templates = array(
135

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
374
	return $padr;
375
}
376

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

    
380
	$pport = "";
381

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

    
396
	return $pport;
397
}
398

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

    
474
	return $item_set;
475
}
476

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

    
486
function genhtmltitle($title) {
487

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

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

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

    
502
	return $heading . $bc;
503
}
504

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
781

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

    
794

    
795

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

    
806

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1096
	return $content;
1097
}
1098

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

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

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

    
1128
	return $columns;
1129
}
1130

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

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

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

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

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

    
1186
date_default_timezone_set($timezone);
1187

    
1188
?>
(63-63/227)