Project

General

Profile

Download (31.7 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	guiconfig.inc
4
	by Scott Ullrich, Copyright 2004, All rights reserved.
5
	originally based on of m0n0wall (http://m0n0.ch/wall)
6

    
7
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
8
	All rights reserved.
9

    
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12

    
13
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19

    
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31
/*
32
	pfSense_MODULE: base
33
*/
34

    
35
/* Include authentication routines */
36
/* THIS MUST BE ABOVE ALL OTHER CODE */
37
if(!$nocsrf) {
38
	function csrf_startup() {
39
		csrf_conf('rewrite-js', '/csrf/csrf-magic.js');
40
		$timeout_minutes = isset($config['system']['webgui']['session_timeout']) ?	$config['system']['webgui']['session_timeout'] : 240;
41
		csrf_conf('expires', $timeout_minutes * 60);
42
	}
43
	require_once("csrf/csrf-magic.php");
44
}
45

    
46
/* make sure nothing is cached */
47
if (!$omit_nocacheheaders) {
48
	header("Expires: 0");
49
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
50
	header("Cache-Control: no-store, no-cache, must-revalidate");
51
	header("Cache-Control: post-check=0, pre-check=0", false);
52
	header("Pragma: no-cache");
53
}
54

    
55
header("X-Frame-Options: SAMEORIGIN");
56
require_once("authgui.inc");
57

    
58
/* parse the configuration and include all configuration functions */
59
require_once("functions.inc");
60

    
61
/* Pull in all the gui related display classes) */
62
foreach (scandir("/usr/local/www/classes/") as $file) {
63
	if (substr($file, -4) == ".inc") {
64
		require_once("classes/{$file}");
65
	}
66
}
67

    
68
$g['theme'] = get_current_theme();
69

    
70
/* Set the default interface language */
71
if($config['system']['language'] <> "") {
72
	$g['language'] = $config['system']['language'];
73
} elseif ($g['language'] == "") {
74
	$g['language'] = 'en_US';
75
}
76

    
77
set_language($g['language']);
78

    
79
/* used by progress bar */
80
$lastseen = "-1";
81

    
82
$navlevelsep = ": ";	/* navigation level separator string */
83
$mandfldhtml = "";		/* display this before mandatory input fields */
84
$mandfldhtmlspc = "";	/* same as above, but with spacing */
85

    
86
/* Some ajax scripts still need access to GUI */
87
if(!$ignorefirmwarelock) {
88
	if (is_subsystem_dirty('firmwarelock')) {
89
		if (!$d_isfwfile) {
90
			header("Location: system_firmware.php");
91
			exit;
92
		} else {
93
			return;
94
		}
95
	}
96
}
97

    
98
/* Reserved table names to avoid colision */
99
$reserved_table_names = array(
100
		"bogons",
101
		"bogonsv6",
102
		"negate_networks",
103
		"snort2c",
104
		"sshlockout",
105
		"tonatsubnets",
106
		"virusprot",
107
		"vpn_networks",
108
		"webConfiguratorlockout"
109
);
110

    
111
$firewall_rules_dscp_types = array("af11",
112
				"af12",
113
				"af13",
114
				"af21",
115
				"af22",
116
				"af23",
117
				"af31",
118
				"af32",
119
				"af33",
120
				"af41",
121
				"af42",
122
				"af43",
123
				"VA",
124
				"EF",
125
				"cs1",
126
				"cs2",
127
				"cs3",
128
				"cs4",
129
				"cs5",
130
				"cs6",
131
				"cs7",
132
				"0x01",
133
				"0x02",
134
				"0x04");
135

    
136
$auth_server_types = array(
137
	'ldap' => "LDAP",
138
	'radius' => "Radius");
139

    
140
$ldap_urltypes = array(
141
	'TCP - Standard' => 389,
142
	'SSL - Encrypted' => 636);
143

    
144
$ldap_scopes = array(
145
	'one' => "One Level",
146
	'subtree' => "Entire Subtree");
147

    
148
$ldap_protvers = array(
149
	2,
150
	3);
151

    
152
$ldap_templates = array(
153

    
154
	'open' => array(
155
				'desc' => "OpenLDAP",
156
				'attr_user' => "cn",
157
				'attr_group' => "cn",
158
				'attr_member' => "member"),
159

    
160
	'msad' => array(
161
				'desc' => "Microsoft AD",
162
				'attr_user' => "samAccountName",
163
				'attr_group' => "cn",
164
				'attr_member' => "memberOf"),
165

    
166
	'edir' => array(
167
				'desc' => "Novell eDirectory",
168
				'attr_user' => "cn",
169
				'attr_group' => "cn",
170
				'attr_member' => "uniqueMember"));
171

    
172
$radius_srvcs = array(
173
	'both' => "Authentication and Accounting",
174
	'auth' => "Authentication",
175
	'acct' => "Accounting");
176

    
177
$netbios_nodetypes = array(
178
	'0' => "none",
179
	'1' => "b-node",
180
	'2' => "p-node",
181
	'4' => "m-node",
182
	'8' => "h-node");
183

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

    
231
/* TCP flags */
232
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg", "ece", "cwr");
233

    
234
$specialnets = array("(self)" => "This Firewall", "pptp" => "PPTP clients", "pppoe" => "PPPoE clients", "l2tp" => "L2TP clients");
235

    
236
$spiflist = get_configured_interface_with_descr(false, true);
237
foreach ($spiflist as $ifgui => $ifdesc) {
238
	$specialnets[$ifgui] = $ifdesc . " net";
239
	$specialnets[$ifgui . 'ip'] = $ifdesc . " address";
240
}
241

    
242
$medias = array("auto" => "autoselect", "100full" => "100BASE-TX full-duplex",
243
	"100half" => "100BASE-TX half-duplex", "10full" => "10BASE-T full-duplex",
244
	"10half" => "10BASE-T half-duplex");
245

    
246
$wlan_modes = array("bss" => "Infrastructure (BSS)", "adhoc" => "Ad-hoc (IBSS)",
247
	"hostap" => "Access Point");
248

    
249
/* platforms that support firmware updating */
250
$fwupplatforms = array('pfSense', 'net45xx', 'net48xx', 'generic-pc', 'embedded', 'wrap', 'nanobsd');
251

    
252
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, &$input_errors) {
253

    
254
	/* check for bad control characters */
255
	foreach ($postdata as $pn => $pd) {
256
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
257
			$input_errors[] = sprintf(gettext("The field %s contains invalid characters."), $pn);
258
		}
259
	}
260

    
261
	for ($i = 0; $i < count($reqdfields); $i++) {
262
		if ($_POST[$reqdfields[$i]] == "" && $_REQUEST[$reqdfields[$i]] == "") {
263
			$input_errors[] = sprintf(gettext("The field %s is required."), $reqdfieldsn[$i]);
264
		}
265
	}
266
}
267

    
268
function print_input_errors($input_errors) {
269
	echo '<div class="alert alert-danger input-errors">';
270
	echo '<p>' . gettext('The following input errors were detected:') . '</p>';
271
	echo '<ul>';
272

    
273
	foreach ($input_errors as $ierr) {
274
		echo '<li>' . htmlspecialchars($ierr) . '</li>';
275
	}
276

    
277
	echo '</ul>';
278
	echo '</div>';
279
}
280

    
281
function verify_gzip_file($fname) {
282
	$returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
283
	if ($returnvar != 0)
284
		return 0;
285
	else
286
		return 1;
287
}
288

    
289
function print_info_box_np($msg, $name="apply",$value="", $showapply=false, $class="alert-warning") {
290
	global $g;
291

    
292
	if(strpos($class, "alert-") !== 0)
293
		$class = 'alert-' . $class;
294

    
295
	if(empty($value)) {
296
		$value = gettext("Apply changes");
297
	}
298

    
299
	$msg = '<div class="pull-left">' . $msg . '</div>';
300

    
301
	if (stristr($msg, gettext("apply")) != false || stristr($msg, gettext("save")) != false || stristr($msg, gettext("create")) != false || $showapply) {
302
		$msg .= '<form method="post" class="pull-right"><button type="submit" class="btn btn-default" name="'. $name .'" value="'.$value.'">'.$name.'</button>';
303

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

    
307
		$msg .= '</form>';
308
	} else
309
		$msg = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'. $msg;
310

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

    
314
function print_info_box_np_undo($msg, $name="apply",$value="Apply changes", $undo) {
315
	global $g;
316

    
317
	if(stristr($msg, "apply") != false || stristr($msg, "save") != false || stristr($msg, "create") != false) {
318
		$savebutton = "<td class=\"infoboxsave nowrap\">";
319
		$savebutton .= "<input type=\"button\" value=\"". gettext("Undo") . "\" onclick=\"document.location='{$undo}'\" />";
320
		$savebutton .= "<input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\" />";
321
		$savebutton .= "</td>";
322
		if($_POST['if'])
323
			$savebutton .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
324
	}
325
	$nifty_redbox = "#990000";
326
	$nifty_blackbox = "#000000";
327

    
328
	$themename = $g['theme'];
329

    
330
	if(file_exists("/usr/local/www/themes/{$themename}/tabcontrols.php")) {
331
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/tabcontrols.php");
332
		eval($toeval);
333
	}
334

    
335
	if(file_exists("/usr/local/www/themes/{$themename}/infobox.php")) {
336
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/infobox.php");
337
		eval($toeval);
338
	}
339

    
340

    
341
	if(!$savebutton) {
342
		$savebutton = "<td class=\"infoboxsave\"><input value=\"" . gettext("Close") . "\" type=\"button\" onclick=\"jQuery(this).parents('table[id=redboxtable]').hide();\" /></td>";
343
	}
344

    
345
	echo <<<EOFnp
346
	<table class="infobox" id="redboxtable" summary="red box table">
347
		<tr>
348
			<td>
349
				<div class="infoboxnp" id="redbox">
350
					<table class="infoboxnptable2" summary="message">
351
						<tr>
352
							<td class="infoboxnptd">
353
								&nbsp;&nbsp;&nbsp;<img class="infoboxnpimg" src="/themes/{$g['theme']}/images/icons/icon_exclam.gif" alt="exclamation" />
354
							</td>
355
							<td class="infoboxnptd2">
356
								<b>{$msg}</b>
357
							</td>
358
							{$savebutton}
359
							{$undobutton}
360
						</tr>
361
					</table>
362
				</div>
363
				<div>
364
					<p>&nbsp;</p>
365
				</div>
366
			</td>
367
		</tr>
368
	</table>
369
	<script type="text/javascript">
370
	//<![CDATA[
371
		NiftyCheck();
372
		Rounded("div#redbox","all","#FFF","{$nifty_redbox}","smooth");
373
		Rounded("td#blackbox","all","#FFF","{$nifty_blackbox}","smooth");
374
	//]]>
375
	</script>
376
EOFnp;
377

    
378
}
379

    
380
function print_info_box($msg, $class="alert-warning") {
381
	print_info_box_np($msg, null, null, false, $class);
382

    
383
}
384

    
385
function get_std_save_message($ok) {
386
	global $d_sysrebootreqd_path;
387
	$filter_related = false;
388
	$filter_pages = array("nat", "filter");
389
	$to_return = gettext("The changes have been applied successfully.");
390
	foreach($filter_pages as $fp)
391
		if(stristr($_SERVER['SCRIPT_FILENAME'], $fp))
392
			$filter_related = true;
393
	if($filter_related)
394
		$to_return .= "<br />" . gettext("You can also <a href=\"status_filter_reload.php\">monitor</a> the filter reload progress.");
395
	return $to_return;
396
}
397

    
398
function pprint_address($adr) {
399
	global $specialnets;
400

    
401
	if (isset($adr['any'])) {
402
		$padr = "*";
403
	} else if ($adr['network']) {
404
		$padr = $specialnets[$adr['network']];
405
	} else {
406
		$padr = $adr['address'];
407
	}
408

    
409
	if (isset($adr['not']))
410
		$padr = "! " . $padr;
411

    
412
	return $padr;
413
}
414

    
415
function pprint_port($port) {
416
	global $wkports;
417

    
418
	$pport = "";
419

    
420
	if (!$port)
421
		return "*";
422
	else {
423
		$srcport = explode("-", $port);
424
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
425
			$pport = $srcport[0];
426
			if ($wkports[$srcport[0]]) {
427
				$pport .= " (" . $wkports[$srcport[0]] . ")";
428
			}
429
		} else
430
			$pport .= $srcport[0] . " - " . $srcport[1];
431
	}
432

    
433
	return $pport;
434
}
435

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

    
489
	return $item_set;
490
}
491

    
492
function gentitle($title) {
493
	if(!is_array($title))
494
		return $title;
495
	else
496
		return join($navlevelsep, $title);
497
}
498

    
499
function genhtmltitle($title) {
500
	if(!is_array($title))
501
		return '<h1 class="page-header">' . $title . '</h1>';
502

    
503
	$heading = '<h1 class="page-header">' . end($title) . '</h1>';
504

    
505
	$bc = '<ol class="breadcrumb">';
506

    
507
	foreach ($title as $el)
508
		$bc .= '<li>'.$el.'</li>';
509

    
510
	$bc .= '</ol>';
511

    
512
	return $heading . $bc;
513
}
514

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

    
520
	$changedesc .= " {$update}";
521
	$changecount++;
522
}
523

    
524
function clear_log_file($logfile = "/var/log/system.log", $restart_syslogd = true) {
525
	global $config, $g;
526
	if ($restart_syslogd)
527
		exec("/usr/bin/killall syslogd");
528
	if(isset($config['system']['disablesyslogclog'])) {
529
		unlink($logfile);
530
		touch($logfile);
531
	} else {
532
		$log_size = isset($config['syslog']['logfilesize']) ? $config['syslog']['logfilesize'] : "511488";
533
		if(isset($config['system']['usefifolog']))
534
			exec("/usr/sbin/fifolog_create -s {$log_size} " . escapeshellarg($logfile));
535
		else
536
			exec("/usr/local/sbin/clog -i -s {$log_size} " . escapeshellarg($logfile));
537
	}
538
	if ($restart_syslogd)
539
		system_syslogd_start();
540
}
541

    
542
function clear_all_log_files() {
543
	global $g;
544
	exec("/usr/bin/killall syslogd");
545

    
546
	$log_files = array("system", "filter", "dhcpd", "vpn", "pptps", "poes", "l2tps", "openvpn", "portalauth", "ipsec", "ppp", "relayd", "wireless", "lighttpd", "ntpd", "gateways", "resolver", "routing");
547
	foreach ($log_files as $lfile) {
548
		clear_log_file("{$g['varlog_path']}/{$lfile}.log", false);
549
	}
550

    
551
	system_syslogd_start();
552
	killbyname("dhcpd");
553
	services_dhcpd_configure();
554
	return;
555
}
556

    
557
// This version of dump_clog() does not output <td></td> or any other table elements. It can be renamed
558
// and the dump_clog() removed once all of the diag_log*.php files have been converted to Bootstrap
559
function dump_clog_no_table($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
560
	global $g, $config;
561
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
562
	$logarr = "";
563
	$grepline = "  ";
564
	if(is_array($grepfor))
565
		$grepline .= " | /usr/bin/egrep " . escapeshellarg(implode("|", $grepfor));
566
	if(is_array($grepinvert))
567
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
568
	if (is_dir($logfile)) {
569
		$logarr = array("File $logfile is a directory.");
570
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
571
		$logarr = array("Log file started.");
572
	} else {
573
		if($config['system']['disablesyslogclog']) {
574
			exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
575
		} else {
576
			if(isset($config['system']['usefifolog']))
577
				exec("/usr/sbin/fifolog_reader " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
578
			else
579
				exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
580
		}
581
	}
582
	echo "\n";
583

    
584
	$rows = 0;
585
	foreach ($logarr as $logent) {
586
		$rows++;
587
		$logent = preg_split("/\s+/", $logent, 6);
588

    
589
		if ($withorig) {
590
				if(isset($config['system']['usefifolog'])) {
591
					$entry_date_time = htmlspecialchars(date("F j, Y, g:i a","" . $logent[1] . ""));
592
					$entry_text = htmlspecialchars($logent[5]);
593
				} else {
594
					$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
595
					$entry_text = ($logent[3] ==  $config['system']['hostname']) ? "" : $logent[3] . " ";
596
					$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
597
				}
598
				echo "{$entry_date_time}";
599
				echo " " . "{$entry_text}"	. "\n";
600
		} else {
601
				echo htmlspecialchars($logent[5]) . "\n";
602
		}
603

    
604
	}
605
	return($rows);
606
}
607

    
608
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
609
	global $g, $config;
610
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
611
	$logarr = "";
612
	$grepline = "  ";
613
	if(is_array($grepfor))
614
		$grepline .= " | /usr/bin/egrep " . escapeshellarg(implode("|", $grepfor));
615
	if(is_array($grepinvert))
616
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
617
	if (is_dir($logfile)) {
618
		$logarr = array("File $logfile is a directory.");
619
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
620
		$logarr = array("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
			if(isset($config['system']['usefifolog']))
626
				exec("/usr/sbin/fifolog_reader " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
627
			else
628
				exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
629
		}
630
	}
631
	foreach ($logarr as $logent) {
632
			$logent = preg_split("/\s+/", $logent, 6);
633
			echo "<tr valign=\"top\">\n";
634
			if ($withorig) {
635
					if(isset($config['system']['usefifolog'])) {
636
						$entry_date_time = htmlspecialchars(date("F j, Y, g:i a","" . $logent[1] . ""));
637
						$entry_text = htmlspecialchars($logent[5]);
638
					} else {
639
						$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
640
						$entry_text = ($logent[3] ==  $config['system']['hostname']) ? "" : $logent[3] . " ";
641
						$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
642
					}
643
					echo "<td class=\"listlr nowrap\">{$entry_date_time}</td>\n";
644
					echo "<td class=\"listr\">{$entry_text}</td>\n";
645

    
646
			} else {
647
					echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
648
			}
649
			echo "</tr>\n";
650
	}
651
}
652

    
653
function return_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
654
	global $g, $config;
655
	$sor = (isset($config['syslog']['reverse']) || $grepreverse) ? "-r" : "";
656
	$logarr = "";
657
	$grepline = "  ";
658
	if(is_array($grepfor))
659
		$grepline .= " | /usr/bin/egrep " . escapeshellarg(implode("|", $grepfor));
660
	if(is_array($grepinvert))
661
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
662
	if($config['system']['disablesyslogclog']) {
663
		exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
664
	} else {
665
		if(isset($config['system']['usefifolog'])) {
666
			exec("/usr/sbin/fifolog_reader " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
667
		} else {
668
			exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
669
		}
670
	}
671
	return($logarr);
672
}
673

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

    
693
	} else {
694
		if ($orig != $new) {
695
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
696
			$orig = $new;
697
			return true;
698
		}
699
	}
700
	return false;
701
}
702

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

    
718
	if (isset($adr['not']))
719
		$pnot = 1;
720
	else
721
		$pnot = 0;
722

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

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

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

    
751
	if ($pnot)
752
		$adr['not'] = true;
753
	else
754
		unset($adr['not']);
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
	if(is_alias($pbeginport)) {
764
		$adr['port'] = $pbeginport;
765
	}
766
}
767

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

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

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

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

    
807
	echo "<script type=\"text/javascript\">";
808
	echo "\n//<![CDATA[\n";
809
	echo "NiftyCheck();\n";
810
	echo "Rounded(\"div.{$tabclass}-tabactive\",\"top\",\"#CCCCCC\",\"#EEEEEE\",\"smooth\");\n";
811
	echo "Rounded(\"div.{$tabclass}-tabdeactive\",\"top\",\"#CCCCCC\",\"#777777\",\"smooth\");\n";
812
	echo "//]]>\n";
813
	echo "</script>";
814
	echo "</div>";
815
}
816

    
817

    
818
// Return inline javascript file or CSS to minimizie
819
// request count going back to server.
820
function outputJavaScriptFileInline($javascript) {
821
	if(file_exists($javascript)) {
822
		echo "\n<script type=\"text/javascript\">\n";
823
		include($javascript);
824
		echo "\n</script>\n";
825
	} else {
826
		echo "\n\n<!-- Could not location file:	 {$javascript} -->\n\n";
827
	}
828
}
829

    
830

    
831

    
832
function outputCSSPrintFileInline($css) {
833
	if(file_exists($css)) {
834
		echo "\n<style media=\"print\" type=\"text/css\">\n";
835
		include($css);
836
		echo "\n</style>\n";
837
	} else {
838
		echo "\n\n<!-- Could not location file:	 {$css} -->\n\n";
839
	}
840
}
841

    
842

    
843
function outputCSSFileInline($css) {
844
	if(file_exists($css)) {
845
		echo "\n<style type=\"text/css\">\n";
846
		include($css);
847
		echo "\n</style>\n";
848
	} else {
849
		echo "\n\n<!-- Could not location file:	 {$css} -->\n\n";
850
	}
851
}
852

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

    
897
function is_rfc2616_code($code) {
898
	global $rfc2616;
899
	if (isset($rfc2616[$code]))
900
		return true;
901
	else
902
		return false;
903
}
904

    
905
function print_rfc2616_select($tag, $current){
906
	global $rfc2616;
907

    
908
	/* Default to 200 OK if not set */
909
	if ($current == "")
910
		$current = 200;
911

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

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

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

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

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

    
977
	if ($tab_array_space == '') {
978
		$tab_array_space = 1;
979
	}
980

    
981
	if ($tab_array_char_limit == '') {
982
		$tab_array_char_limit = 92;
983
	}
984

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

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

    
995
	$tabcharcount = 0;
996
	foreach ($tab_array as $ta)
997
		$tabcharcount = $tabcharcount + strlen($ta[0]);
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
		foreach ($tab_array as $ta) {
1010
			if($ta[1]=="true")
1011
				$selected = " selected=\"selected\"";
1012
			else
1013
				$selected = "";
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
		echo "</select>\n<p>&nbsp;</p>";
1019
		echo "<script type=\"text/javascript\">";
1020
		echo "\n//<![CDATA[\n";
1021
		echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
1022
		echo "//]]>\n";
1023
		echo "</script>";
1024
<? else: ?>
1025
	<ul class="nav nav-<?= $type ?>">
1026
		<?php foreach ($tab_array as $ta): ?>
1027
			<li role="presentation"<?=($ta[1] ? ' class="active"' : '')?>><a href="<?=$ta[2]?>"><?=$ta[0]?></a></li>
1028
		<?php endforeach; ?>
1029
	</ul>
1030
<?php endif;
1031
}
1032

    
1033
function add_package_tabs($tabgroup, & $tab_array) {
1034
	global $config, $g;
1035

    
1036
	if(!is_array($config['installedpackages']))
1037
		return;
1038
	if(!is_array($config['installedpackages']['tab']))
1039
		return;
1040

    
1041
	foreach($config['installedpackages']['tab'] as $tab) {
1042
		if ($tab['group'] !== $group)
1043
			continue;
1044
		$tab_entry = array();
1045
		if($tab['name']) {
1046
			$tab_entry[] = $tab['name'];
1047
			$tab_entry[] = false;
1048
			$tab_entry[] = $tab['url'];
1049
			$tab_array[] = $tab_entry;
1050
		}
1051
	}
1052
}
1053

    
1054
function alias_info_popup($alias_id){
1055
	global $config;
1056

    
1057
	if (!is_array($config['aliases']['alias'][$alias_id]))
1058
		return;
1059

    
1060
	$maxlength = 60;
1061
	$alias = $config['aliases']['alias'][$alias_id];
1062
	$content = "";
1063

    
1064
	if ($alias['url'])
1065
	{
1066
		// TODO: Change it when pf supports tables with ports
1067
		if ($alias['type'] == "urltable") {
1068
			exec("/sbin/pfctl -t {$alias['name']} -T show | wc -l", $total_entries);
1069
			$counter=preg_replace("/\D/","",$total_entries[0]);
1070
			exec("/sbin/pfctl -t {$alias['name']} -T show | head -10002", $alias_addresses);
1071
		} else {
1072
			$urlfn = alias_expand_urltable($alias['name']);
1073
			$alias_addresses = explode("\n", file_get_contents($urlfn));
1074
			$counter = count($alias_addresses);
1075
		}
1076

    
1077
		$content .= '<h5>'. $alias['url'] .'</h5><ul><li>'. implode('</li><li>', $alias_ports_address) .'</li></ul>';
1078

    
1079
		if ($counter > 10002)
1080
			$content .= '<i>'. gettext("listing only first 10k items") .'</i>';
1081
	}
1082
	else
1083
	{
1084
		$alias_addresses = explode (" ", $alias['address']);
1085
		$alias_details = explode ("||", $alias['detail']);
1086

    
1087
		$content .= '<ul><li>'. implode('</li><li>', $alias_addresses) .'</li></ul>';
1088
	}
1089

    
1090
	if (strlen($alias['descr']) >= $maxlength)
1091
		$alias['descr'] = substr($alias['descr'], 0, $maxlength) . '&hellip;';
1092

    
1093
	return $content;
1094
}
1095

    
1096
function rule_columns_with_alias($src, $srcport, $dst, $dstport){
1097
	global $config;
1098

    
1099
	if ($config['aliases']['alias'] == "" || !is_array($config['aliases']['alias']))
1100
		return;
1101

    
1102
	$columns = array();
1103
	foreach ($config['aliases']['alias'] as $alias_id => $alias_name){
1104
		if ($alias_name['name'] == $src)
1105
			$columns['src'] = $alias_id;
1106
		elseif ($alias_name['name'] == $srcport)
1107
			$columns['srcport'] = $alias_id;
1108
		elseif ($alias_name['name'] == $dst )
1109
			$columns['dst'] = $alias_id;
1110
		elseif ($alias_name['name'] == $dstport)
1111
			$columns['dstport'] = $alias_id;
1112

    
1113
		return $columns;
1114
	}
1115
}
1116

    
1117
function form_output_row($name, $label, $content){
1118
var_dump($content);die;
1119
?>
1120
<div class="form-group">
1121
	<label for="<?=$name?>" class="col-sm-2 control-label"><?=gettext($label); ?></label>
1122
	<div class="col-sm-10">
1123
		<?=$content?>
1124
	</div>
1125
</div>
1126
<?php
1127
}
1128

    
1129
$timezone = $config['system']['timezone'];
1130
if (!$timezone)
1131
	$timezone = "Etc/UTC";
1132

    
1133
date_default_timezone_set($timezone);
1134

    
1135
?>
(76-76/241)