Project

General

Profile

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

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

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

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

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

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

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

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

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

    
98
set_language();
99

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

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

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

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

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

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

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

    
167
$ldap_templates = array(
168

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
331
		$msg .= '<form method="post" class="pull-right"><button type="submit" class="btn btn-default" name="'. $btnname . '" value="' . $btntext . '">' . $btntext . '</button>';
332

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

    
337
		$msg .= '</form>';
338
	}
339

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

    
343
function print_apply_box($msg) {
344
	print_info_box($msg, "warning", "apply", gettext("Apply Changes"));
345
}
346

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

    
362
function pprint_address($adr) {
363
	global $specialnets;
364

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

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

    
377
	return $padr;
378
}
379

    
380
function pprint_port($port) {
381
	global $wkports;
382

    
383
	$pport = "";
384

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

    
399
	return $pport;
400
}
401

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

    
477
	return $item_set;
478
}
479

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

    
489
function genhtmltitle($title) {
490
	if (!is_array($title)) {
491
		return '<h1 class="page-header"><a href="">' . $title . '</a></h1>';
492
	}
493

    
494
	$heading = '<h1 class="page-header"><a href="">' . end($title) . '</a></h1>';
495

    
496
	// If the array contains only one element, there are no breadcrumbs, so don't
497
	// add anything else
498
	if (count($title) > 1) {
499
		$bc = '<ol class="breadcrumb">';
500

    
501
		foreach ($title as $el) {
502
			$bc .= '<li>'.$el.'</li>';
503
		}
504

    
505
		$bc .= '</ol>';
506
	} else {
507
		$bc = "";
508
	}
509

    
510
	return $heading . $bc;
511
}
512

    
513
/* update the changedesc and changecount(er) variables */
514
function update_changedesc($update) {
515
	global $changedesc;
516
	global $changecount;
517

    
518
	$changedesc .= " {$update}";
519
	$changecount++;
520
}
521

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

    
555
	$rows = 0;
556
	foreach ($logarr as $logent) {
557
		$rows++;
558
		$logent = preg_split("/\s+/", $logent, 6);
559

    
560
		if ($withorig) {
561
				$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
562
				$entry_text = ($logent[3] ==  $config['system']['hostname']) ? "" : $logent[3] . " ";
563
				$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
564
				echo "{$entry_date_time}";
565
				echo " " . "{$entry_text}"	. "\n";
566
		} else {
567
				echo htmlspecialchars($logent[5]) . "\n";
568
		}
569

    
570
	}
571
	return($rows);
572
}
573

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

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

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

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

    
665
	} else {
666
		if ($orig != $new) {
667
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
668
			$orig = $new;
669
			return true;
670
		}
671
	}
672
	return false;
673
}
674

    
675
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
676
	if (isset($adr['any'])) {
677
		$padr = "any";
678
	} else if ($adr['network']) {
679
		$padr = $adr['network'];
680
	} else if ($adr['address']) {
681
		list($padr, $pmask) = explode("/", $adr['address']);
682
		if (!$pmask) {
683
			if (is_ipaddrv6($padr)) {
684
				$pmask = 128;
685
			} else {
686
				$pmask = 32;
687
			}
688
		}
689
	}
690

    
691
	if (isset($adr['not'])) {
692
		$pnot = 1;
693
	} else {
694
		$pnot = 0;
695
	}
696

    
697
	if ($adr['port']) {
698
		list($pbeginport, $pendport) = explode("-", $adr['port']);
699
		if (!$pendport) {
700
			$pendport = $pbeginport;
701
		}
702
	} else if (!is_alias($pbeginport) && !is_alias($pendport)) {
703
		$pbeginport = "any";
704
		$pendport = "any";
705
	}
706
}
707

    
708
function pconfig_to_address(&$adr, $padr, $pmask, $pnot = false, $pbeginport = 0, $pendport = 0) {
709
	$adr = array();
710

    
711
	if ($padr == "any") {
712
		$adr['any'] = true;
713
	} else if (is_specialnet($padr)) {
714
		$adr['network'] = $padr;
715
	} else {
716
		$adr['address'] = $padr;
717
		if (is_ipaddrv6($padr)) {
718
			if ($pmask != 128) {
719
				$adr['address'] .= "/" . $pmask;
720
			}
721
		} else {
722
			if ($pmask != 32) {
723
				$adr['address'] .= "/" . $pmask;
724
			}
725
		}
726
	}
727

    
728
	if ($pnot) {
729
		$adr['not'] = true;
730
	} else {
731
		unset($adr['not']);
732
	}
733

    
734
	if (($pbeginport != 0) && ($pbeginport != "any")) {
735
		if ($pbeginport != $pendport) {
736
			$adr['port'] = $pbeginport . "-" . $pendport;
737
		} else {
738
			$adr['port'] = $pbeginport;
739
		}
740
	}
741

    
742
	if (is_alias($pbeginport)) {
743
		$adr['port'] = $pbeginport;
744
	}
745
}
746

    
747
function is_specialnet($net) {
748
	global $specialsrcdst;
749

    
750
	if (!$net) {
751
		return false;
752
	}
753
	if (in_array($net, $specialsrcdst)) {
754
		return true;
755
	} else {
756
		return false;
757
	}
758
}
759

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

    
781
		echo "<div id=\"{$ta[2]}-deactive\" class=\"{$tabclass}-tabdeactive\" style=\"display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;\" onclick=\"return changeTabDIV('{$ta[2]}')\">";
782
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
783
		echo "&nbsp;&nbsp;&nbsp;</b>";
784
		echo "</div>";
785
	}
786
	echo "</div>";
787
}
788

    
789

    
790
// Return inline javascript file or CSS to minimize
791
// request count going back to server.
792
function outputJavaScriptFileInline($javascript) {
793
	if (file_exists($javascript)) {
794
		echo "\n<script type=\"text/javascript\">\n";
795
		include($javascript);
796
		echo "\n</script>\n";
797
	} else {
798
		echo "\n\n<!-- Could not locate file:  {$javascript} -->\n\n";
799
	}
800
}
801

    
802

    
803

    
804
function outputCSSPrintFileInline($css) {
805
	if (file_exists($css)) {
806
		echo "\n<style media=\"print\" type=\"text/css\">\n";
807
		include($css);
808
		echo "\n</style>\n";
809
	} else {
810
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
811
	}
812
}
813

    
814

    
815
function outputCSSFileInline($css) {
816
	if (file_exists($css)) {
817
		echo "\n<style type=\"text/css\">\n";
818
		include($css);
819
		echo "\n</style>\n";
820
	} else {
821
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
822
	}
823
}
824

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

    
869
function is_rfc2616_code($code) {
870
	global $rfc2616;
871
	if (isset($rfc2616[$code])) {
872
		return true;
873
	} else {
874
		return false;
875
	}
876
}
877

    
878
function print_rfc2616_select($tag, $current) {
879
	global $rfc2616;
880

    
881
	/* Default to 200 OK if not set */
882
	if ($current == "") {
883
		$current = 200;
884
	}
885

    
886
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
887
	foreach ($rfc2616 as $code => $message) {
888
		if ($code == $current) {
889
			$sel = " selected";
890
		} else {
891
			$sel = "";
892
		}
893
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
894
	}
895
	echo "</select>\n";
896
}
897

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

    
925
/****f* pfsense-utils/display_top_tabs
926
 * NAME
927
 *	 display_top_tabs - display tabs with rounded edges
928
 * INPUTS
929
 *	 $text	  - array of tabs
930
 * RESULT
931
 *	 null
932
 ******/
933
function display_top_tabs(& $tab_array, $no_drop_down = false, $type = 'pills') {
934
	global $config;
935
	global $g;
936
	global $tab_array_indent;
937
	global $tab_array_space;
938
	global $tab_array_char_limit;
939

    
940
	/*	does the user have access to this tab?
941
	 *	master user has access to everything.
942
	 *	if the user does not have access, simply
943
	 *	unset the tab item.
944
	 */
945

    
946
	/* empty string code */
947
	if ($tab_array_indent == '') {
948
		$tab_array_indent = 0;
949
	}
950

    
951
	if ($tab_array_space == '') {
952
		$tab_array_space = 1;
953
	}
954

    
955
	if ($tab_array_char_limit == '') {
956
		$tab_array_char_limit = 92;
957
	}
958

    
959
	foreach ($tab_array as $tab_id => $ta) {
960
		if (!isAllowedPage($ta[2])) {
961
			unset ($tab_array[$tab_id]);
962
		}
963
	}
964

    
965
	$tab_active_bg	 = "#EEEEEE";
966
	$tab_inactive_bg = "#777777";
967
	$nifty_tabs_corners = "#FFF";
968
	$font_color = "white";
969

    
970
	$tabcharcount = 0;
971
	foreach ($tab_array as $ta) {
972
		$tabcharcount = $tabcharcount + strlen($ta[0]);
973
	}
974

    
975
	if ($no_drop_down == true) {
976
		$tabcharcount = 0;
977
		unset($tab_array_char_limit);
978
	}
979

    
980
	// If the character count of the tab names is > 670
981
	// then show a select item dropdown menubox.
982
	if ($tabcharcount > $tab_array_char_limit) {
983
		echo gettext("Currently viewing: ");
984
		echo "<select name=\"TabSelect\" onchange=\"tabs_will_go(this)\">\n";
985

    
986
		foreach ($tab_array as $ta) {
987
			if ($ta[1] == "true") {
988
				$selected = " selected";
989
			} else {
990
				$selected = "";
991
			}
992
			// Onclick in option will not work in some browser
993
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
994
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
995
		}
996

    
997
		echo "</select>\n<p>&nbsp;</p>";
998
		echo "<script type=\"text/javascript\">";
999
		echo "\n//<![CDATA[\n";
1000
		echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
1001
		echo "//]]>\n";
1002
		echo "</script>";
1003
	} else {
1004
		echo '<ul class="nav nav-' . $type . '">';
1005

    
1006
		foreach ($tab_array as $ta) {
1007
			echo '<li role="presentation"';
1008
			if ($ta[1]) {
1009
				echo ' class="active"';
1010
			}
1011

    
1012
			echo '><a href="' . $ta[2] . '">' . $ta[0] . '</a></li>';
1013
		}
1014

    
1015
		echo '</ul>';
1016
	}
1017
}
1018

    
1019
function add_package_tabs($tabgroup, &$tab_array) {
1020
	global $config, $g;
1021

    
1022
	if (!isset($config['installedpackages']['package'])) {
1023
		return;
1024
	}
1025

    
1026
	foreach ($config['installedpackages']['package'] as $pkg) {
1027
		$pkg_config = read_package_configurationfile($pkg['name']);
1028

    
1029
		if (!isset($pkg_config['tabs']['tab'])) {
1030
			continue;
1031
		}
1032

    
1033
		foreach ($pkg_config['tabs']['tab'] as $tab) {
1034
			$tab_entry = array();
1035
			if ($tab['name']) {
1036
				$tab_entry[] = $tab['name'];
1037
				$tab_entry[] = false;
1038
				$tab_entry[] = $tab['url'];
1039
				$tab_array[] = $tab_entry;
1040
			}
1041
		}
1042
	}
1043
}
1044

    
1045
function alias_info_popup($alias_id) {
1046
	global $config;
1047

    
1048
	if (!is_array($config['aliases']['alias'][$alias_id])) {
1049
		return;
1050
	}
1051

    
1052
	$maxlength = 60;
1053
	$alias = $config['aliases']['alias'][$alias_id];
1054
	$content = "";
1055

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

    
1068
		$content .= '<h5>'. $alias['url'] .'</h5><ul><li>'. implode('</li><li>', $alias_addresses) .'</li></ul>';
1069
		if ($counter > 10002) {
1070
			$content .= '<i>'. gettext("listing only first 10k items") .'</i>';
1071
		}
1072
	} else {
1073
		$alias_addresses = explode (" ", $alias['address']);
1074
		$alias_details = explode ("||", $alias['detail']);
1075
		$idx = 0;
1076

    
1077
		$content .= "<table>\n";
1078
		$content .= "<thead>\n";
1079
		$content .= "<tr>\n";
1080
		$content .= "<th>" . gettext("Value") . "</th><th  style='padding-left: 10px;'>" . gettext("Description") . "</th></tr>\n";
1081
		$content .= "</thead>\n";
1082
		$content .= "<tbody>\n";
1083

    
1084
		foreach ($alias_addresses as $ap) {
1085
			$content .= "	<tr>\n";
1086
			$content .= "		<td>\n";
1087
			$content .= 			$ap;
1088
			$content .=	"		</td>\n";
1089
			$content .= "		<td style='padding-left: 10px;'>\n";
1090
			$content .= 			$alias_details[$idx];
1091
			$content .=	"		</td>\n";
1092
			$content .= "	</tr>\n";
1093
			$idx++;
1094
		}
1095

    
1096
		$content .= "</tbody>\n";
1097
		$content .= "<table>\n";
1098
	}
1099

    
1100
	if (strlen($alias['descr']) >= $maxlength) {
1101
		$alias['descr'] = substr($alias['descr'], 0, $maxlength) . '&hellip;';
1102
	}
1103

    
1104
	return $content;
1105
}
1106

    
1107
function rule_columns_with_alias($src, $srcport, $dst, $dstport) {
1108
	global $config;
1109

    
1110
	if ($config['aliases']['alias'] == "" || !is_array($config['aliases']['alias'])) {
1111
		return;
1112
	}
1113

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

    
1130
	return $columns;
1131
}
1132

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

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

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

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

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

    
1188
date_default_timezone_set($timezone);
1189

    
1190
?>
(64-64/229)