Project

General

Profile

Download (32.3 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
/*
348
 * Print Bootstrap callout
349
 *
350
 * @param string $msg     message to display
351
 * @param string $class   contextual class, defaults to info (default | danger | warning | info)
352
 * @param string $heading optional callout heading
353
 */
354
function print_callout($msg, $class = 'info', $heading = '') {
355

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

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

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

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

    
389
function pprint_address($adr) {
390
	global $specialnets;
391

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

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

    
404
	return $padr;
405
}
406

    
407
function pprint_port($port) {
408
	global $wkports;
409

    
410
	$pport = "";
411

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

    
426
	return $pport;
427
}
428

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

    
504
	return $item_set;
505
}
506

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

    
516
function genhtmltitle($title) {
517

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

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

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

    
532
	return $heading . $bc;
533
}
534

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

    
540
	$changedesc .= " {$update}";
541
	$changecount++;
542
}
543

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

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

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

    
592
	}
593
	return($rows);
594
}
595

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

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

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

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

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

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

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

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

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

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

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

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

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

    
769
function is_specialnet($net) {
770
	global $specialsrcdst;
771

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

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

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

    
811

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

    
824

    
825

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

    
836

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

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

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

    
900
function print_rfc2616_select($tag, $current) {
901
	global $rfc2616;
902

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

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

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

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

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

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

    
973
	if ($tab_array_space == '') {
974
		$tab_array_space = 1;
975
	}
976

    
977
	if ($tab_array_char_limit == '') {
978
		$tab_array_char_limit = 92;
979
	}
980

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

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

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

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

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

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

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

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

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

    
1037
		echo '</ul>';
1038
	}
1039
}
1040

    
1041
function add_package_tabs($tabgroup, &$tab_array) {
1042
	global $config, $g;
1043

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

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

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

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

    
1067
function alias_info_popup($alias_id) {
1068
	global $config;
1069

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

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

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

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

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

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

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

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

    
1126
	return $content;
1127
}
1128

    
1129
function rule_columns_with_alias($src, $srcport, $dst, $dstport) {
1130
	global $config;
1131

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

    
1136
	$columns = array();
1137
	foreach ($config['aliases']['alias'] as $alias_id => $alias_name) {
1138
		if ($alias_name['name'] == $src) {
1139
			$columns['src'] = $alias_id;
1140
		}
1141
		if ($alias_name['name'] == $srcport) {
1142
			$columns['srcport'] = $alias_id;
1143
		}
1144
		if ($alias_name['name'] == $dst) {
1145
			$columns['dst'] = $alias_id;
1146
		}
1147
		if ($alias_name['name'] == $dstport) {
1148
			$columns['dstport'] = $alias_id;
1149
		}
1150
	}
1151

    
1152
	return $columns;
1153
}
1154

    
1155
function form_output_row($name, $label, $content) {
1156
var_dump($content);die;
1157
?>
1158
<div class="form-group">
1159
	<label for="<?=$name?>" class="col-sm-2 control-label"><?=gettext($label); ?></label>
1160
	<div class="col-sm-10">
1161
		<?=$content?>
1162
	</div>
1163
</div>
1164
<?php
1165
}
1166

    
1167
function set_flash_message($class, $msg) {
1168
	@session_start();
1169
	$_SESSION['flash_messages'][$class][] = $msg;
1170
}
1171

    
1172
function get_flash_message() {
1173
	@session_start();
1174
	if (isset($_SESSION['flash_messages']) && !empty($_SESSION['flash_messages'])) {
1175
		foreach ($_SESSION['flash_messages'] as $class => $flash_message) {
1176
			print_info_box(implode("<br />", $flash_message), $class);
1177
		}
1178
		unset($_SESSION['flash_messages']);
1179
	}
1180
}
1181

    
1182
/* Retrieve GET or POST Value/State
1183
 * Eample Usage:
1184
 * $value = getGETPOSTsettingvalue('get/post parameter name', "");
1185
 * $value = getGETPOSTsettingvalue('get/post parameter name', null);
1186
 * $state = getGETPOSTsettingvalue('get/post parameter name', null);
1187
 * $state = getGETPOSTsettingvalue('get/post parameter name', false);
1188
 */
1189
function getGETPOSTsettingvalue($settingname, $default) {
1190
	$settingvalue = $default;
1191
	if ($_GET[$settingname]) {
1192
		$settingvalue = $_GET[$settingname];
1193
	}
1194
	if ($_POST[$settingname]) {
1195
		$settingvalue = $_POST[$settingname];
1196
	}
1197
	return $settingvalue;
1198
}
1199

    
1200
/* set timezone */
1201
if (isset($config['system']['timezone']) &&
1202
    !empty($config['system']['timezone'])) {
1203
	$timezone = $config['system']['timezone'];
1204
} elseif (isset($g['default_timezone']) && !empty($g['default_timezone'])) {
1205
	$timezone = $g['default_timezone'];
1206
} else {
1207
	$timezone = "Etc/UTC";
1208
}
1209

    
1210
date_default_timezone_set($timezone);
1211

    
1212
?>
(64-64/229)