Project

General

Profile

Download (32.6 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 Electric Sheep Fencing, LLC
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
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *
16
 * 1. Redistributions of source code must retain the above copyright notice,
17
 *    this list of conditions and the following disclaimer.
18
 *
19
 * 2. Redistributions in binary form must reproduce the above copyright
20
 *    notice, this list of conditions and the following disclaimer in
21
 *    the documentation and/or other materials provided with the
22
 *    distribution.
23
 *
24
 * 3. All advertising materials mentioning features or use of this software
25
 *    must display the following acknowledgment:
26
 *    "This product includes software developed by the pfSense Project
27
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
28
 *
29
 * 4. The names "pfSense" and "pfSense Project" must not be used to
30
 *    endorse or promote products derived from this software without
31
 *    prior written permission. For written permission, please contact
32
 *    coreteam@pfsense.org.
33
 *
34
 * 5. Products derived from this software may not be called "pfSense"
35
 *    nor may "pfSense" appear in their names without prior written
36
 *    permission of the Electric Sheep Fencing, LLC.
37
 *
38
 * 6. Redistributions of any form whatsoever must retain the following
39
 *    acknowledgment:
40
 *
41
 * "This product includes software developed by the pfSense Project
42
 * for use in the pfSense software distribution (http://www.pfsense.org/).
43
 *
44
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
45
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
48
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55
 * OF THE POSSIBILITY OF SUCH DAMAGE.
56
 */
57

    
58
/* Include authentication routines */
59
/* THIS MUST BE ABOVE ALL OTHER CODE */
60
if (!$nocsrf) {
61
	function csrf_startup() {
62
		csrf_conf('rewrite-js', '/csrf/csrf-magic.js');
63
		$timeout_minutes = isset($config['system']['webgui']['session_timeout']) ? $config['system']['webgui']['session_timeout'] : 240;
64
		csrf_conf('expires', $timeout_minutes * 60);
65
	}
66
	require_once("csrf/csrf-magic.php");
67
}
68

    
69
/* make sure nothing is cached */
70
if (!$omit_nocacheheaders) {
71
	header("Expires: 0");
72
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
73
	header("Cache-Control: no-cache, no-store, must-revalidate");
74
	header("Pragma: no-cache");
75
}
76

    
77
header("X-Frame-Options: SAMEORIGIN");
78
require_once("authgui.inc");
79

    
80
/* parse the configuration and include all configuration functions */
81
require_once("functions.inc");
82

    
83
/* Include the autoloader for all the GUI display classes */
84
require_once("classes/autoload.inc.php");
85

    
86
/* used by progress bar */
87
$lastseen = "-1";
88

    
89
$navlevelsep = ": ";	/* navigation level separator string */
90
$mandfldhtml = "";		/* display this before mandatory input fields */
91
$mandfldhtmlspc = "";	/* same as above, but with spacing */
92

    
93
if (!function_exists('set_language')) {
94
	require_once("pfsense-utils.inc");
95
}
96

    
97
set_language();
98

    
99
/* Some ajax scripts still need access to GUI */
100
if (!$ignorefirmwarelock) {
101
	if (is_subsystem_dirty('firmwarelock')) {
102
		if (!$d_isfwfile) {
103
			header("Location: system_update.php");
104
			exit;
105
		} else {
106
			return;
107
		}
108
	}
109
}
110

    
111
/* Reserved table names to avoid collision */
112
$reserved_table_names = array(
113
	"bogons",
114
	"bogonsv6",
115
	"negate_networks",
116
	"snort2c",
117
	"sshlockout",
118
	"tonatsubnets",
119
	"virusprot",
120
	"vpn_networks",
121
	"webConfiguratorlockout"
122
);
123

    
124
$firewall_rules_dscp_types = array(
125
	"af11",
126
	"af12",
127
	"af13",
128
	"af21",
129
	"af22",
130
	"af23",
131
	"af31",
132
	"af32",
133
	"af33",
134
	"af41",
135
	"af42",
136
	"af43",
137
	"VA",
138
	"EF",
139
	"cs1",
140
	"cs2",
141
	"cs3",
142
	"cs4",
143
	"cs5",
144
	"cs6",
145
	"cs7",
146
	"0x01",
147
	"0x02",
148
	"0x04");
149

    
150
$auth_server_types = array(
151
	'ldap' => "LDAP",
152
	'radius' => "RADIUS");
153

    
154
$ldap_urltypes = array(
155
	'TCP - Standard' => 389,
156
	'SSL - Encrypted' => 636);
157

    
158
$ldap_scopes = array(
159
	'one' => gettext("One Level"),
160
	'subtree' => gettext("Entire Subtree"));
161

    
162
$ldap_protvers = array(
163
	2,
164
	3);
165

    
166
$ldap_templates = array(
167

    
168
	'open' => array(
169
		'desc' => "OpenLDAP",
170
		'attr_user' => "cn",
171
		'attr_group' => "cn",
172
		'attr_member' => "member"),
173

    
174
	'msad' => array(
175
		'desc' => "Microsoft AD",
176
		'attr_user' => "samAccountName",
177
		'attr_group' => "cn",
178
		'attr_member' => "memberOf"),
179

    
180
	'edir' => array(
181
		'desc' => "Novell eDirectory",
182
		'attr_user' => "cn",
183
		'attr_group' => "cn",
184
		'attr_member' => "uniqueMember"));
185

    
186
$radius_srvcs = array(
187
	'both' => gettext("Authentication and Accounting"),
188
	'auth' => gettext("Authentication"),
189
	'acct' => gettext("Accounting"));
190

    
191
$netbios_nodetypes = array(
192
	'0' => "none",
193
	'1' => "b-node",
194
	'2' => "p-node",
195
	'4' => "m-node",
196
	'8' => "h-node");
197

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

    
245
/* TCP flags */
246
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg", "ece", "cwr");
247

    
248
$specialnets = array(
249
	"(self)" => gettext("This Firewall"),
250
	"pppoe" => gettext("PPPoE clients"),
251
	"l2tp" => gettext("L2TP clients"));
252

    
253
$spiflist = get_configured_interface_with_descr(false, true);
254
foreach ($spiflist as $ifgui => $ifdesc) {
255
	$specialnets[$ifgui] = $ifdesc . " net";
256
	$specialnets[$ifgui . 'ip'] = $ifdesc . " address";
257
}
258

    
259
$medias = array(
260
	"auto" => gettext("autoselect"),
261
	"100full" => gettext("100BASE-TX full-duplex"),
262
	"100half" => gettext("100BASE-TX half-duplex"),
263
	"10full" => gettext("10BASE-T full-duplex"),
264
	"10half" => gettext("10BASE-T half-duplex"));
265

    
266
$wlan_modes = array(
267
	"bss" => gettext("Infrastructure (BSS)"),
268
	"adhoc" => gettext("Ad-hoc (IBSS)"),
269
	"hostap" => gettext("Access Point"));
270

    
271
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, &$input_errors) {
272

    
273
	/* check for bad control characters */
274
	foreach ($postdata as $pn => $pd) {
275
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
276
			$input_errors[] = sprintf(gettext("The field %s contains invalid characters."), $pn);
277
		}
278
	}
279

    
280
	for ($i = 0; $i < count($reqdfields); $i++) {
281
		if ($_POST[$reqdfields[$i]] == "" && $_REQUEST[$reqdfields[$i]] == "") {
282
			$input_errors[] = sprintf(gettext("The field %s is required."), $reqdfieldsn[$i]);
283
		}
284
	}
285
}
286

    
287
function print_input_errors($input_errors) {
288
	echo '<div class="alert alert-danger input-errors">';
289
	echo '<p>' . gettext('The following input errors were detected:') . '</p>';
290
	echo '<ul>';
291

    
292
	foreach ($input_errors as $ierr) {
293
		echo '<li>' . htmlspecialchars($ierr) . '</li>';
294
	}
295

    
296
	echo '</ul>';
297
	echo '</div>';
298
}
299

    
300
function verify_gzip_file($fname) {
301
	$returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
302
	if ($returnvar != 0) {
303
		return 0;
304
	} else {
305
		return 1;
306
	}
307
}
308

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

    
317
	if (strpos($class, "alert-") !== 0) {
318
		$class = 'alert-' . $class;
319
	}
320

    
321
	$msg = '<div class="pull-left">' . $msg . '</div>';
322

    
323
	if ($btnname === "close") {
324
		$msg = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' . $msg;
325
	} else if ($btnname != "") {
326
		if (empty($btntext)) {
327
			$btntext = $btnname;
328
		}
329
		if (!empty($btnicon)) {
330
			$btnicon = '<i class="fa ' . $btnicon . ' icon-embed-btn"></i>';
331
		}
332

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

    
335
		if ($_POST['if']) {
336
			$msg .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
337
		}
338

    
339
		$msg .= '</form>';
340
	}
341

    
342
	echo '<div class="alert ' . $class . ' clearfix" role="alert">' . $msg . '</div>';
343
}
344

    
345
function print_apply_box($msg) {
346
	print_info_box($msg, "warning", "apply", gettext("Apply Changes"), 'fa-check', 'success');
347
}
348

    
349
/*
350
 * Print Bootstrap callout
351
 *
352
 * @param string $msg     message to display
353
 * @param string $class   contextual class, defaults to info (default | danger | warning | info)
354
 * @param string $heading optional callout heading
355
 */
356
function print_callout($msg, $class = 'info', $heading = '') {
357

    
358
	if ('' == $msg) {
359
		return;
360
	}
361
	$class = strtolower($class);
362
	$callout = '';
363

    
364
	if ($class != 'default' && $class != 'danger' && $class != 'warning' && $class != 'info') {
365
		$class = 'info';
366
	}
367
	$callout .= '<div class="bs-callout bs-callout-' . $class . '">';
368

    
369
	if ('' != $heading) {
370
		$callout .= '<h4>' . $heading . '</h4>';
371
	}
372
	$callout .= $msg . '</div>';
373
	echo $callout;
374
}
375

    
376
function get_std_save_message($ok) {
377
	$filter_related = false;
378
	$filter_pages = array("nat", "filter");
379
	$to_return = gettext("The changes have been applied successfully.");
380
	foreach ($filter_pages as $fp) {
381
		if (stristr($_SERVER['SCRIPT_FILENAME'], $fp)) {
382
			$filter_related = true;
383
		}
384
	}
385
	if ($filter_related) {
386
		$to_return .= "<br />" . gettext("<a href=\"status_filter_reload.php\">Monitor</a> the filter reload progress.");
387
	}
388
	return $to_return;
389
}
390

    
391
function pprint_address($adr) {
392
	global $specialnets;
393

    
394
	if (isset($adr['any'])) {
395
		$padr = "*";
396
	} else if ($adr['network']) {
397
		$padr = $specialnets[$adr['network']];
398
	} else {
399
		$padr = $adr['address'];
400
	}
401

    
402
	if (isset($adr['not'])) {
403
		$padr = "! " . $padr;
404
	}
405

    
406
	return $padr;
407
}
408

    
409
function pprint_port($port) {
410
	global $wkports;
411

    
412
	$pport = "";
413

    
414
	if (!$port) {
415
		return "*";
416
	} else {
417
		$srcport = explode("-", $port);
418
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
419
			$pport = $srcport[0];
420
			if ($wkports[$srcport[0]]) {
421
				$pport .= " (" . $wkports[$srcport[0]] . ")";
422
			}
423
		} else {
424
			$pport .= $srcport[0] . " - " . $srcport[1];
425
		}
426
	}
427

    
428
	return $pport;
429
}
430

    
431
function firewall_check_for_advanced_options(&$item) {
432
	$item_set = "";
433
	if ($item['os']) {
434
			$item_set .= "os {$item['os']} ";
435
	}
436
	if ($item['dscp']) {
437
		$item_set .= "dscp {$item['dscp']} ";
438
	}
439
	if ($item['max']) {
440
		$item_set .= "max {$item['max']} ";
441
	}
442
	if ($item['max-src-nodes']) {
443
		$item_set .= "max-src-nodes {$item['max-src-nodes']} ";
444
	}
445
	if ($item['max-src-conn']) {
446
		$item_set .= "max-src-conn {$item['max-src-conn']} ";
447
	}
448
	if ($item['max-src-states']) {
449
		$item_set .= "max-src-states {$item['max-src-states']} ";
450
	}
451
	if (isset($item['nopfsync'])) {
452
		$item_set .= "nopfsync ";
453
	}
454
	if ($item['statetype'] != "keep state" && $item['statetype'] != "") {
455
		$item_set .= "statetype {$item['statetype']} ";
456
	}
457
	if ($item['statetimeout']) {
458
		$item_set .= "statetimeout {$item['statetimeout']} ";
459
	}
460
	if (isset($item['nosync'])) {
461
		$item_set .= "no XMLRPC Sync ";
462
	}
463
	if ($item['max-src-conn-rate']) {
464
		$item_set .= "max-src-conn-rate {$item['max-src-conn-rate']} ";
465
	}
466
	if ($item['max-src-conn-rates']) {
467
		$item_set .= "max-src-conn-rates {$item['max-src-conn-rates']} ";
468
	}
469
	if ($item['vlanprio']) {
470
		$item_set .= "vlanprio {$item['vlanprio']} ";
471
	}
472
	if ($item['vlanprioset']) {
473
		$item_set .= "vlanprioset {$item['vlanprioset']} ";
474
	}
475
	if ($item['gateway']) {
476
		$item_set .= "gateway {$item['gateway']} ";
477
	}
478
	if ($item['dnpipe']) {
479
		$item_set .= "limiter {$item['dnpipe']} ";
480
	}
481
	if ($item['pdnpipe']) {
482
		$item_set .= "limiter {$item['pdnpipe']} ";
483
	}
484
	if ($item['ackqueue']) {
485
		$item_set .= "ackqueue {$item['ackqueue']} ";
486
	}
487
	if ($item['defaultqueue']) {
488
		$item_set .= "defaultqueue {$item['defaultqueue']} ";
489
	}
490
	if ($item['tag']) {
491
		$item_set .= "tag {$item['tag']} ";
492
	}
493
	if ($item['tagged']) {
494
		$item_set .= "tagged {$item['tagged']} ";
495
	}
496
	if (isset($item['allowopts'])) {
497
		$item_set .= "allowopts ";
498
	}
499
	if (isset($item['disablereplyto'])) {
500
		$item_set .= "disable reply-to ";
501
	}
502
	if ($item['tcpflags_any'] || $item['tcpflags1'] || $item['tcpflags2']) {
503
		$item_set .= "tcpflags set";
504
	}
505

    
506
	return $item_set;
507
}
508

    
509
function gentitle($title) {
510
	global $navlevelsep;
511
	if (!is_array($title)) {
512
		return $title;
513
	} else {
514
		return join($navlevelsep, $title);
515
	}
516
}
517

    
518
function genhtmltitle($title) {
519

    
520
	// If the array contains only one element, there are no breadcrumbs, so don't
521
	// add anything else
522
	if (count($title) > 1) {
523
		$bc = '<ol class="breadcrumb">';
524

    
525
		foreach ($title as $el) {
526
			$bc .= '<li>'.$el.'</li>';
527
		}
528

    
529
		$bc .= '</ol>';
530
	} else {
531
		$bc = "";
532
	}
533

    
534
	return $heading . $bc;
535
}
536

    
537
/* update the changedesc and changecount(er) variables */
538
function update_changedesc($update) {
539
	global $changedesc;
540
	global $changecount;
541

    
542
	$changedesc .= " {$update}";
543
	$changecount++;
544
}
545

    
546
// This version of dump_clog() does not output <td></td> or any other table elements.
547
function dump_clog_no_table($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
548
	global $g, $config;
549
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
550
	$specific_log = basename($logfile, '.log') . '_settings';
551
	if ($config['syslog'][$specific_log]['cronorder'] == 'forward') $sor = "";
552
	if ($config['syslog'][$specific_log]['cronorder'] == 'reverse') $sor = "-r";
553
	$logarr = array();
554
	$grepline = "  ";
555
	if (is_array($grepfor)) {
556
		$invert = '';
557
		if ((strpos($grepfor[0], '!') === 0)) {
558
			$grepfor[0] = substr($grepfor[0], 1);
559
			$invert = '-v';
560
		}
561
		$grepline .= " | /usr/bin/egrep {$invert} " . escapeshellarg(implode("|", $grepfor));
562
	}
563
	if (is_array($grepinvert)) {
564
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
565
	}
566
	if (is_dir($logfile)) {
567
		$logarr = array(sprintf(gettext("File %s is a directory."), $logfile));
568
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
569
		$logarr = array(gettext("Log file started."));
570
	} else {
571
		if ($config['system']['disablesyslogclog']) {
572
			exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
573
		} else {
574
			exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
575
		}
576
	}
577
	echo "\n";
578

    
579
	$rows = 0;
580
	foreach ($logarr as $logent) {
581
		$rows++;
582
		$logent = preg_split("/\s+/", $logent, 6);
583

    
584
		if ($withorig) {
585
				$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
586
				$entry_text = ($logent[3] ==  $config['system']['hostname']) ? "" : $logent[3] . " ";
587
				$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
588
				echo "{$entry_date_time}";
589
				echo " " . "{$entry_text}"	. "\n";
590
		} else {
591
				echo htmlspecialchars($logent[5]) . "\n";
592
		}
593

    
594
	}
595
	return($rows);
596
}
597

    
598
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
599
	global $g, $config;
600
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
601
	$specific_log = basename($logfile, '.log') . '_settings';
602
	if ($config['syslog'][$specific_log]['cronorder'] == 'forward') $sor = "";
603
	if ($config['syslog'][$specific_log]['cronorder'] == 'reverse') $sor = "-r";
604
	$logarr = array();
605
	$grepline = "  ";
606
	if (is_array($grepfor)) {
607
		$invert = '';
608
		if ((strpos($grepfor[0], '!') === 0)) {
609
			$grepfor[0] = substr($grepfor[0], 1);
610
			$invert = '-v';
611
		}
612
		$grepline .= " | /usr/bin/egrep {$invert} " . escapeshellarg(implode("|", $grepfor));
613
	}
614
	if (is_array($grepinvert)) {
615
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
616
	}
617
	if (is_dir($logfile)) {
618
		$logarr = array(sprintf(gettext("File %s is a directory."), $logfile));
619
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
620
		$logarr = array(gettext("Log file started."));
621
	} else {
622
		if ($config['system']['disablesyslogclog']) {
623
			exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
624
		} else {
625
			exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
626
		}
627
	}
628

    
629
	$rows = 0;
630
	foreach ($logarr as $logent) {
631
		$rows++;
632
		$logent = preg_split("/\s+/", $logent, 6);
633
		echo "<tr>\n";
634
		if ($withorig) {
635
			$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
636
			$entry_text = ($logent[3] == $config['system']['hostname']) ? "" : $logent[3] . " ";
637
			$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
638
			echo "<td class=\"text-nowrap\">{$entry_date_time}</td>\n";
639
			echo "<td style=\"word-wrap:break-word; word-break:break-all; white-space:normal\">{$entry_text}</td>\n";
640
		} else {
641
				echo "<td>" . htmlspecialchars($logent[5]) . "</td>\n";
642
		}
643
		echo "</tr>\n";
644
	}
645
	return($rows);
646
}
647

    
648
function return_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
649
	global $g, $config;
650
	$sor = (isset($config['syslog']['reverse']) || $grepreverse) ? "-r" : "";
651
	$specific_log = basename($logfile, '.log') . '_settings';
652
	if (($config['syslog'][$specific_log]['cronorder'] == 'forward') && !$grepreverse) $sor = "";
653
	if (($config['syslog'][$specific_log]['cronorder'] == 'reverse') ||  $grepreverse) $sor = "-r";
654
	$logarr = array();
655
	$grepline = "  ";
656
	if (is_array($grepfor)) {
657
		$grepline .= " | /usr/bin/egrep " . escapeshellarg(implode("|", $grepfor));
658
	}
659
	if (is_array($grepinvert)) {
660
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
661
	}
662
	if ($config['system']['disablesyslogclog']) {
663
		exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
664
	} else {
665
		exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
666
	}
667
	return($logarr);
668
}
669

    
670
/* Check if variable has changed, update and log if it has
671
 * returns true if var changed
672
 * varname = variable name in plain text
673
 * orig = original value
674
 * new = new value
675
 */
676
function update_if_changed($varname, & $orig, $new) {
677
	if (is_array($orig) && is_array($new)) {
678
		$a_diff = array_diff($orig, $new);
679
		foreach ($a_diff as $diff) {
680
			update_changedesc("removed {$varname}: \"{$diff}\"");
681
		}
682
		$a_diff = array_diff($new, $orig);
683
		foreach ($a_diff as $diff) {
684
			update_changedesc("added {$varname}: \"{$diff}\"");
685
		}
686
		$orig = $new;
687
		return true;
688

    
689
	} else {
690
		if ($orig != $new) {
691
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
692
			$orig = $new;
693
			return true;
694
		}
695
	}
696
	return false;
697
}
698

    
699
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
700
	if (isset($adr['any'])) {
701
		$padr = "any";
702
	} else if ($adr['network']) {
703
		$padr = $adr['network'];
704
	} else if ($adr['address']) {
705
		list($padr, $pmask) = explode("/", $adr['address']);
706
		if (!$pmask) {
707
			if (is_ipaddrv6($padr)) {
708
				$pmask = 128;
709
			} else {
710
				$pmask = 32;
711
			}
712
		}
713
	}
714

    
715
	if (isset($adr['not'])) {
716
		$pnot = 1;
717
	} else {
718
		$pnot = 0;
719
	}
720

    
721
	if ($adr['port']) {
722
		list($pbeginport, $pendport) = explode("-", $adr['port']);
723
		if (!$pendport) {
724
			$pendport = $pbeginport;
725
		}
726
	} else if (!is_alias($pbeginport) && !is_alias($pendport)) {
727
		$pbeginport = "any";
728
		$pendport = "any";
729
	}
730
}
731

    
732
function pconfig_to_address(&$adr, $padr, $pmask, $pnot = false, $pbeginport = 0, $pendport = 0) {
733
	$adr = array();
734

    
735
	if ($padr == "any") {
736
		$adr['any'] = true;
737
	} else if (is_specialnet($padr)) {
738
		$adr['network'] = $padr;
739
	} else {
740
		$adr['address'] = $padr;
741
		if (is_ipaddrv6($padr)) {
742
			if ($pmask != 128) {
743
				$adr['address'] .= "/" . $pmask;
744
			}
745
		} else {
746
			if ($pmask != 32) {
747
				$adr['address'] .= "/" . $pmask;
748
			}
749
		}
750
	}
751

    
752
	if ($pnot) {
753
		$adr['not'] = true;
754
	} else {
755
		unset($adr['not']);
756
	}
757

    
758
	if (($pbeginport != 0) && ($pbeginport != "any")) {
759
		if ($pbeginport != $pendport) {
760
			$adr['port'] = $pbeginport . "-" . $pendport;
761
		} else {
762
			$adr['port'] = $pbeginport;
763
		}
764
	}
765

    
766
	if (is_alias($pbeginport)) {
767
		$adr['port'] = $pbeginport;
768
	}
769
}
770

    
771
function is_specialnet($net) {
772
	global $specialsrcdst;
773

    
774
	if (!$net) {
775
		return false;
776
	}
777
	if (in_array($net, $specialsrcdst)) {
778
		return true;
779
	} else {
780
		return false;
781
	}
782
}
783

    
784
//function to create widget tabs when called
785
function display_widget_tabs(& $tab_array) {
786
	echo "<div id=\"tabs\">";
787
	$tabscounter = 0;
788
	foreach ($tab_array as $ta) {
789
		$dashpos = strpos($ta[2], '-');
790
		$tabname = $ta[2] . "-tab";
791
		$tabclass = substr($ta[2], 0, $dashpos);
792
		$tabclass = $tabclass . "-class";
793
		if ($ta[1] == true) {
794
			$tabActive = "table-cell";
795
			$tabNonActive = "none";
796
		} else {
797
			$tabActive = "none";
798
			$tabNonActive = "table-cell";
799
		}
800
		echo "<div id=\"{$ta[2]}-active\" class=\"{$tabclass}-tabactive\" style=\"display:{$tabActive}; background-color:#EEEEEE; color:black;\">";
801
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
802
		echo "&nbsp;&nbsp;&nbsp;</b>";
803
		echo "</div>";
804

    
805
		echo "<div id=\"{$ta[2]}-deactive\" class=\"{$tabclass}-tabdeactive\" style=\"display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;\" onclick=\"return changeTabDIV('{$ta[2]}')\">";
806
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
807
		echo "&nbsp;&nbsp;&nbsp;</b>";
808
		echo "</div>";
809
	}
810
	echo "</div>";
811
}
812

    
813

    
814
// Return inline javascript file or CSS to minimize
815
// request count going back to server.
816
function outputJavaScriptFileInline($javascript) {
817
	if (file_exists($javascript)) {
818
		echo "\n<script type=\"text/javascript\">\n";
819
		include_once($javascript);
820
		echo "\n</script>\n";
821
	} else {
822
		echo "\n\n<!-- Could not locate file:  {$javascript} -->\n\n";
823
	}
824
}
825

    
826

    
827

    
828
function outputCSSPrintFileInline($css) {
829
	if (file_exists($css)) {
830
		echo "\n<style media=\"print\" type=\"text/css\">\n";
831
		include_once($css);
832
		echo "\n</style>\n";
833
	} else {
834
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
835
	}
836
}
837

    
838

    
839
function outputCSSFileInline($css) {
840
	if (file_exists($css)) {
841
		echo "\n<style type=\"text/css\">\n";
842
		include_once($css);
843
		echo "\n</style>\n";
844
	} else {
845
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
846
	}
847
}
848

    
849
$rfc2616 = array(
850
	100 => "100 Continue",
851
	101 => "101 Switching Protocols",
852
	200 => "200 OK",
853
	201 => "201 Created",
854
	202 => "202 Accepted",
855
	203 => "203 Non-Authoritative Information",
856
	204 => "204 No Content",
857
	205 => "205 Reset Content",
858
	206 => "206 Partial Content",
859
	300 => "300 Multiple Choices",
860
	301 => "301 Moved Permanently",
861
	302 => "302 Found",
862
	303 => "303 See Other",
863
	304 => "304 Not Modified",
864
	305 => "305 Use Proxy",
865
	306 => "306 (Unused)",
866
	307 => "307 Temporary Redirect",
867
	400 => "400 Bad Request",
868
	401 => "401 Unauthorized",
869
	402 => "402 Payment Required",
870
	403 => "403 Forbidden",
871
	404 => "404 Not Found",
872
	405 => "405 Method Not Allowed",
873
	406 => "406 Not Acceptable",
874
	407 => "407 Proxy Authentication Required",
875
	408 => "408 Request Timeout",
876
	409 => "409 Conflict",
877
	410 => "410 Gone",
878
	411 => "411 Length Required",
879
	412 => "412 Precondition Failed",
880
	413 => "413 Request Entity Too Large",
881
	414 => "414 Request-URI Too Long",
882
	415 => "415 Unsupported Media Type",
883
	416 => "416 Requested Range Not Satisfiable",
884
	417 => "417 Expectation Failed",
885
	500 => "500 Internal Server Error",
886
	501 => "501 Not Implemented",
887
	502 => "502 Bad Gateway",
888
	503 => "503 Service Unavailable",
889
	504 => "504 Gateway Timeout",
890
	505 => "505 HTTP Version Not Supported"
891
);
892

    
893
function is_rfc2616_code($code) {
894
	global $rfc2616;
895
	if (isset($rfc2616[$code])) {
896
		return true;
897
	} else {
898
		return false;
899
	}
900
}
901

    
902
function print_rfc2616_select($tag, $current) {
903
	global $rfc2616;
904

    
905
	/* Default to 200 OK if not set */
906
	if ($current == "") {
907
		$current = 200;
908
	}
909

    
910
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
911
	foreach ($rfc2616 as $code => $message) {
912
		if ($code == $current) {
913
			$sel = " selected";
914
		} else {
915
			$sel = "";
916
		}
917
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
918
	}
919
	echo "</select>\n";
920
}
921

    
922
// Useful debugging function, much cleaner than print_r
923
function echo_array($array, $return_me = false) {
924
	if (is_array($array) == false) {
925
		$return = "The provided variable is not an array.";
926
	} else {
927
		foreach ($array as $name=>$value) {
928
			if (is_array($value)) {
929
				$return .= "";
930
				$return .= "['<b>$name</b>'] {<div style=\"margin-left:10px;\">\n";
931
				$return .= echo_array($value, true);
932
				$return .= "</div>}";
933
				$return .= "\n\n";
934
			} else {
935
				if (is_string($value)) {
936
					$value = "\"$value\"";
937
				}
938
				$return .= "['<b>$name</b>'] = $value\n\n";
939
			}
940
		}
941
	}
942
	if ($return_me == true) {
943
		return $return;
944
	} else {
945
		echo "<pre>".$return."</pre>";
946
	}
947
}
948

    
949
/****f* pfsense-utils/display_top_tabs
950
 * NAME
951
 *	 display_top_tabs - display tabs with rounded edges
952
 * INPUTS
953
 *	 $text	  - array of tabs
954
 * RESULT
955
 *	 null
956
 ******/
957
function display_top_tabs(& $tab_array, $no_drop_down = false, $type = 'pills') {
958
	global $config;
959
	global $g;
960
	global $tab_array_indent;
961
	global $tab_array_space;
962
	global $tab_array_char_limit;
963

    
964
	/*	does the user have access to this tab?
965
	 *	master user has access to everything.
966
	 *	if the user does not have access, simply
967
	 *	unset the tab item.
968
	 */
969

    
970
	/* empty string code */
971
	if ($tab_array_indent == '') {
972
		$tab_array_indent = 0;
973
	}
974

    
975
	if ($tab_array_space == '') {
976
		$tab_array_space = 1;
977
	}
978

    
979
	if ($tab_array_char_limit == '') {
980
		$tab_array_char_limit = 92;
981
	}
982

    
983
	foreach ($tab_array as $tab_id => $ta) {
984
		if (!isAllowedPage($ta[2])) {
985
			unset ($tab_array[$tab_id]);
986
		}
987
	}
988

    
989
	$tab_active_bg	 = "#EEEEEE";
990
	$tab_inactive_bg = "#777777";
991
	$nifty_tabs_corners = "#FFF";
992
	$font_color = "white";
993

    
994
	$tabcharcount = 0;
995
	foreach ($tab_array as $ta) {
996
		$tabcharcount = $tabcharcount + strlen($ta[0]);
997
	}
998

    
999
	if ($no_drop_down == true) {
1000
		$tabcharcount = 0;
1001
		unset($tab_array_char_limit);
1002
	}
1003

    
1004
	// If the character count of the tab names is > 670
1005
	// then show a select item dropdown menubox.
1006
	if ($tabcharcount > $tab_array_char_limit) {
1007
		echo gettext("Currently viewing: ");
1008
		echo "<select name=\"TabSelect\" onchange=\"tabs_will_go(this)\">\n";
1009

    
1010
		foreach ($tab_array as $ta) {
1011
			if ($ta[1] == "true") {
1012
				$selected = " selected";
1013
			} else {
1014
				$selected = "";
1015
			}
1016
			// Onclick in option will not work in some browser
1017
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
1018
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
1019
		}
1020

    
1021
		echo "</select>\n<p>&nbsp;</p>";
1022
		echo "<script type=\"text/javascript\">";
1023
		echo "\n//<![CDATA[\n";
1024
		echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
1025
		echo "//]]>\n";
1026
		echo "</script>";
1027
	} else {
1028
		echo '<ul class="nav nav-' . $type . '">';
1029

    
1030
		foreach ($tab_array as $ta) {
1031
			echo '<li role="presentation"';
1032
			if ($ta[1]) {
1033
				echo ' class="active"';
1034
			}
1035

    
1036
			echo '><a href="' . $ta[2] . '">' . $ta[0] . '</a></li>';
1037
		}
1038

    
1039
		echo '</ul>';
1040
	}
1041
}
1042

    
1043
function add_package_tabs($tabgroup, &$tab_array) {
1044
	global $config, $g;
1045

    
1046
	if (!isset($config['installedpackages']['package'])) {
1047
		return;
1048
	}
1049

    
1050
	foreach ($config['installedpackages']['package'] as $pkg) {
1051
		$pkg_config = read_package_configurationfile($pkg['name']);
1052

    
1053
		if (!isset($pkg_config['tabs']['tab'])) {
1054
			continue;
1055
		}
1056

    
1057
		foreach ($pkg_config['tabs']['tab'] as $tab) {
1058
			$tab_entry = array();
1059
			if ($tab['name']) {
1060
				$tab_entry[] = $tab['name'];
1061
				$tab_entry[] = false;
1062
				$tab_entry[] = $tab['url'];
1063
				$tab_array[] = $tab_entry;
1064
			}
1065
		}
1066
	}
1067
}
1068

    
1069
function alias_info_popup($alias_id) {
1070
	global $config;
1071

    
1072
	if (!is_array($config['aliases']['alias'][$alias_id])) {
1073
		return;
1074
	}
1075

    
1076
	$maxlength = 60;
1077
	$alias = $config['aliases']['alias'][$alias_id];
1078
	$content = "";
1079

    
1080
	if ($alias['url']) {
1081
		// TODO: Change it when pf supports tables with ports
1082
		if ($alias['type'] == "urltable") {
1083
			exec("/sbin/pfctl -t {$alias['name']} -T show | wc -l", $total_entries);
1084
			$counter=preg_replace("/\D/", "", $total_entries[0]);
1085
			exec("/sbin/pfctl -t {$alias['name']} -T show | head -10002", $alias_addresses);
1086
		} else {
1087
			$urlfn = alias_expand_urltable($alias['name']);
1088
			$alias_addresses = explode("\n", file_get_contents($urlfn));
1089
			$counter = count($alias_addresses);
1090
		}
1091

    
1092
		$content .= '<h5>'. $alias['url'] .'</h5><ul><li>'. implode('</li><li>', $alias_addresses) .'</li></ul>';
1093
		if ($counter > 10002) {
1094
			$content .= '<i>'. gettext("listing only first 10k items") .'</i>';
1095
		}
1096
	} else {
1097
		$alias_addresses = explode (" ", $alias['address']);
1098
		$alias_details = explode ("||", $alias['detail']);
1099
		$idx = 0;
1100

    
1101
		$content .= "<table>\n";
1102
		$content .= "<thead>\n";
1103
		$content .= "<tr>\n";
1104
		$content .= "<th>" . gettext("Value") . "</th><th  style='padding-left: 10px;'>" . gettext("Description") . "</th></tr>\n";
1105
		$content .= "</thead>\n";
1106
		$content .= "<tbody>\n";
1107

    
1108
		foreach ($alias_addresses as $ap) {
1109
			$content .= "	<tr>\n";
1110
			$content .= "		<td>\n";
1111
			$content .= 			$ap;
1112
			$content .=	"		</td>\n";
1113
			$content .= "		<td style='padding-left: 10px;'>\n";
1114
			$content .= 			htmlspecialchars($alias_details[$idx]);
1115
			$content .=	"		</td>\n";
1116
			$content .= "	</tr>\n";
1117
			$idx++;
1118
		}
1119

    
1120
		$content .= "</tbody>\n";
1121
		$content .= "<table>\n";
1122
	}
1123

    
1124
	if (strlen($alias['descr']) >= $maxlength) {
1125
		$alias['descr'] = substr($alias['descr'], 0, $maxlength) . '&hellip;';
1126
	}
1127

    
1128
	return $content;
1129
}
1130

    
1131
function rule_columns_with_alias($src, $srcport, $dst, $dstport, $target="", $targetport="") {
1132
	global $config;
1133

    
1134
	if ($config['aliases']['alias'] == "" || !is_array($config['aliases']['alias'])) {
1135
		return;
1136
	}
1137

    
1138
	$columns = array();
1139
	foreach ($config['aliases']['alias'] as $alias_id => $alias_name) {
1140
		if ($alias_name['name'] == $src) {
1141
			$columns['src'] = $alias_id;
1142
		}
1143
		if ($alias_name['name'] == $srcport) {
1144
			$columns['srcport'] = $alias_id;
1145
		}
1146
		if ($alias_name['name'] == $dst) {
1147
			$columns['dst'] = $alias_id;
1148
		}
1149
		if ($alias_name['name'] == $dstport) {
1150
			$columns['dstport'] = $alias_id;
1151
		}
1152
		if ($alias_name['name'] == $target) {
1153
			$columns['target'] = $alias_id;
1154
		}
1155
		if ($alias_name['name'] == $targetport) {
1156
			$columns['targetport'] = $alias_id;
1157
		}
1158
	}
1159

    
1160
	return $columns;
1161
}
1162

    
1163
function form_output_row($name, $label, $content) {
1164
var_dump($content);die;
1165
?>
1166
<div class="form-group">
1167
	<label for="<?=$name?>" class="col-sm-2 control-label"><?=gettext($label); ?></label>
1168
	<div class="col-sm-10">
1169
		<?=$content?>
1170
	</div>
1171
</div>
1172
<?php
1173
}
1174

    
1175
function set_flash_message($class, $msg) {
1176
	@session_start();
1177
	$_SESSION['flash_messages'][$class][] = $msg;
1178
}
1179

    
1180
function get_flash_message() {
1181
	@session_start();
1182
	if (isset($_SESSION['flash_messages']) && !empty($_SESSION['flash_messages'])) {
1183
		foreach ($_SESSION['flash_messages'] as $class => $flash_message) {
1184
			print_info_box(implode("<br />", $flash_message), $class);
1185
		}
1186
		unset($_SESSION['flash_messages']);
1187
	}
1188
}
1189

    
1190
/* Retrieve GET or POST Value/State
1191
 * Eample Usage:
1192
 * $value = getGETPOSTsettingvalue('get/post parameter name', "");
1193
 * $value = getGETPOSTsettingvalue('get/post parameter name', null);
1194
 * $state = getGETPOSTsettingvalue('get/post parameter name', null);
1195
 * $state = getGETPOSTsettingvalue('get/post parameter name', false);
1196
 */
1197
function getGETPOSTsettingvalue($settingname, $default) {
1198
	$settingvalue = $default;
1199
	if ($_GET[$settingname]) {
1200
		$settingvalue = $_GET[$settingname];
1201
	}
1202
	if ($_POST[$settingname]) {
1203
		$settingvalue = $_POST[$settingname];
1204
	}
1205
	return $settingvalue;
1206
}
1207

    
1208
/* set timezone */
1209
if (isset($config['system']['timezone']) &&
1210
    !empty($config['system']['timezone'])) {
1211
	$timezone = $config['system']['timezone'];
1212
} elseif (isset($g['default_timezone']) && !empty($g['default_timezone'])) {
1213
	$timezone = $g['default_timezone'];
1214
} else {
1215
	$timezone = "Etc/UTC";
1216
}
1217

    
1218
date_default_timezone_set($timezone);
1219

    
1220
?>
(63-63/227)