Project

General

Profile

Download (33.1 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
$firewall_rules_dscp_types = array("af11",
99
				"af12",
100
				"af13",
101
				"af21",
102
				"af22",
103
				"af23",
104
				"af31",
105
				"af32",
106
				"af33",
107
				"af41",
108
				"af42",
109
				"af43",
110
				"EF",
111
				"cs1",
112
				"cs2",
113
				"cs3",
114
				"cs4",
115
				"cs5",
116
				"cs6",
117
				"cs7",
118
				"0x01 (reliability, ToS 0x04)",
119
				"0x02 (throughput, ToS 0x08)",
120
				"0x04 (lowdelay, ToS 0x10)");
121

    
122
$auth_server_types = array(
123
	'ldap' => "LDAP",
124
	'radius' => "Radius");
125

    
126
$ldap_urltypes = array(
127
	'TCP - Standard' => 389,
128
	'SSL - Encrypted' => 636);
129

    
130
$ldap_scopes = array(
131
	'one' => "One Level",
132
	'subtree' => "Entire Subtree");
133

    
134
$ldap_protvers = array(
135
	2,
136
	3);
137

    
138
$ldap_templates = array(
139

    
140
	'open' => array(
141
				'desc' => "OpenLDAP",
142
				'attr_user' => "cn",
143
				'attr_group' => "cn",
144
				'attr_member' => "member"),
145

    
146
	'msad' => array(
147
				'desc' => "Microsoft AD",
148
				'attr_user' => "samAccountName",
149
				'attr_group' => "cn",
150
				'attr_member' => "memberOf"),
151

    
152
	'edir' => array(
153
				'desc' => "Novell eDirectory",
154
				'attr_user' => "cn",
155
				'attr_group' => "cn",
156
				'attr_member' => "uniqueMember"));
157

    
158
$radius_srvcs = array(
159
	'both' => "Authentication and Accounting",
160
	'auth' => "Authentication",
161
	'acct' => "Accounting");
162

    
163
$netbios_nodetypes = array(
164
	'0' => "none",
165
	'1' => "b-node",
166
	'2' => "p-node",
167
	'4' => "m-node",
168
	'5' => "h-node");
169

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

    
217
/* TCP flags */
218
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg", "ece", "cwr");
219

    
220
$specialnets = array("pptp" => "PPTP clients", "pppoe" => "PPPoE clients", "l2tp" => "L2TP clients");
221

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

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

    
232
$wlan_modes = array("bss" => "Infrastructure (BSS)", "adhoc" => "Ad-hoc (IBSS)",
233
	"hostap" => "Access Point");
234

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

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

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

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

    
254
function print_input_errors($input_errors) {
255
	global $g;
256

    
257
	print <<<EOF
258
	<div id="inputerrorsdiv">
259
	<table border="0" cellspacing="0" cellpadding="4" width="100%" summary="input errors">
260
	<tr>
261
		<td class="inputerrorsleft">
262
			<img src="/themes/{$g['theme']}/images/icons/icon_error.gif" alt="errors" />
263
		</td>
264
		<td class="inputerrorsright errmsg">
265
			<p>The following input errors were detected:</p>
266
				<ul>
267
EOF;
268
		foreach ($input_errors as $ierr) {
269
			echo "<li>" . htmlspecialchars($ierr) . "</li>";
270
		}
271

    
272
	print <<<EOF2
273
				</ul>
274
		</td></tr>
275
	</table>
276
	</div>
277
	&nbsp;<br />
278
EOF2;
279

    
280
}
281

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

    
290
function print_info_box_np($msg, $name="apply",$value="", $showapply=false) {
291
	global $g, $nifty_redbox, $nifty_blackbox, $nifty_background;
292

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

    
297
	// Set the Nifty background color if one is not set already (defaults to white)
298
	if($nifty_background == "")
299
		$nifty_background = "#FFF";
300

    
301
	if(stristr($msg, gettext("apply")) != false || stristr($msg, gettext("save")) != false || stristr($msg, gettext("create")) != false || $showapply) {
302
		$savebutton = "<td class=\"infoboxsave\">";
303
		$savebutton .= "<input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\" />";
304
		if($_POST['if'])
305
			$savebutton .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
306
		$savebutton.="</td>";
307
	}
308
	$nifty_redbox = "#990000";
309
	$nifty_blackbox = "#000000";
310

    
311
	$themename = $g['theme'];
312

    
313
	if(file_exists("/usr/local/www/themes/{$themename}/tabcontrols.php")) {
314
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/tabcontrols.php");
315
		eval($toeval);
316
	}
317

    
318
	if(file_exists("/usr/local/www/themes/{$themename}/infobox.php")) {
319
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/infobox.php");
320
		eval($toeval);
321
	}
322

    
323
	if(!$savebutton) {
324
		$savebutton = "<td class=\"infoboxsave\"><input value=\"Close\" type=\"button\" onclick=\"jQuery('#redboxtable').hide();\" /></td>";
325
	}
326

    
327
	echo <<<EOFnp
328
	<table class="infobox" id="redboxtable" summary="red box table">
329
		<tr>
330
			<td>
331
				<div class="infoboxnp" id="redbox">
332
					<table class="infoboxnptable2" summary="message">
333
						<tr>
334
							<td class="infoboxnptd">
335
								&nbsp;&nbsp;&nbsp;<img class="infoboxnpimg" src="/themes/{$g['theme']}/images/icons/icon_exclam.gif" alt="exclamation" />
336
							</td>
337
							<td class="infoboxnptd2">
338
								<b>{$msg}</b>
339
							</td>
340
							{$savebutton}
341
						</tr>
342
					</table>
343
				</div>
344
				<div>
345
					<p>&nbsp;</p>
346
				</div>
347
			</td>
348
		</tr>
349
	</table>
350
	<script type="text/javascript">
351
	//<![CDATA[
352
		NiftyCheck();
353
		Rounded("div#redbox","all","{$nifty_background}","{$nifty_redbox}","smooth");
354
		Rounded("td#blackbox","all","{$nifty_background}","{$nifty_blackbox}","smooth");
355
	//]]>
356
	</script>
357
EOFnp;
358

    
359
}
360

    
361
function print_info_box_np_undo($msg, $name="apply",$value="Apply changes", $undo) {
362
	global $g;
363

    
364
	if(stristr($msg, "apply") != false || stristr($msg, "save") != false || stristr($msg, "create") != false) {
365
		$savebutton = "<td class=\"infoboxsave nowrap\">";
366
		$savebutton .= "<input type=\"button\" value=\"Undo\" onclick=\"document.location='{$undo}'\" />";
367
		$savebutton .= "<input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\" />";
368
		$savebutton .= "</td>";
369
		if($_POST['if'])
370
			$savebutton .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
371
	}
372
	$nifty_redbox = "#990000";
373
	$nifty_blackbox = "#000000";
374

    
375
	$themename = $g['theme'];
376

    
377
	if(file_exists("/usr/local/www/themes/{$themename}/tabcontrols.php")) {
378
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/tabcontrols.php");
379
		eval($toeval);
380
	}
381

    
382
	if(file_exists("/usr/local/www/themes/{$themename}/infobox.php")) {
383
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/infobox.php");
384
		eval($toeval);
385
	}
386

    
387

    
388
	if(!$savebutton) {
389
		$savebutton = "<td class=\"infoboxsave\"><input value=\"Close\" type=\"button\" onclick=\"jQuery('#redboxtable').hide();\" /></td>";
390
	}
391

    
392
	echo <<<EOFnp
393
	<table class="infobox" id="redboxtable" summary="red box table">
394
		<tr>
395
			<td>
396
				<div class="infoboxnp" id="redbox">
397
					<table class="infoboxnptable2" summary="message">
398
						<tr>
399
							<td class="infoboxnptd">
400
								&nbsp;&nbsp;&nbsp;<img class="infoboxnpimg" src="/themes/{$g['theme']}/images/icons/icon_exclam.gif" alt="exclamation" />
401
							</td>
402
							<td class="infoboxnptd2">
403
								<b>{$msg}</b>
404
							</td>
405
							{$savebutton}
406
							{$undobutton}
407
						</tr>
408
					</table>
409
				</div>
410
				<div>
411
					<p>&nbsp;</p>
412
				</div>
413
			</td>
414
		</tr>
415
	</table>
416
	<script type="text/javascript">
417
	//<![CDATA[
418
		NiftyCheck();
419
		Rounded("div#redbox","all","#FFF","{$nifty_redbox}","smooth");
420
		Rounded("td#blackbox","all","#FFF","{$nifty_blackbox}","smooth");
421
	//]]>
422
	</script>
423
EOFnp;
424

    
425
}
426

    
427
function print_info_box($msg) {
428
	print_info_box_np($msg);
429
}
430

    
431
function get_std_save_message($ok) {
432
	global $d_sysrebootreqd_path;
433
	$filter_related = false;
434
	$filter_pages = array("nat", "filter");
435
	$to_return = "The changes have been applied successfully.";
436
	foreach($filter_pages as $fp)
437
		if(stristr($_SERVER['SCRIPT_FILENAME'], $fp))
438
			$filter_related = true;
439
	if($filter_related)
440
		$to_return .= "<br/>You can also <a href=\"status_filter_reload.php\">monitor</a> the filter reload progress.";
441
	return $to_return;
442
}
443

    
444
function pprint_address($adr) {
445
	global $specialnets;
446

    
447
	if (isset($adr['any'])) {
448
		$padr = "*";
449
	} else if ($adr['network']) {
450
		$padr = $specialnets[$adr['network']];
451
	} else {
452
		$padr = $adr['address'];
453
	}
454

    
455
	if (isset($adr['not']))
456
		$padr = "! " . $padr;
457

    
458
	return $padr;
459
}
460

    
461
function pprint_port($port) {
462
	global $wkports;
463

    
464
	$pport = "";
465

    
466
	if (!$port)
467
		return "*";
468
	else {
469
		$srcport = explode("-", $port);
470
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
471
			$pport = $srcport[0];
472
			if ($wkports[$srcport[0]]) {
473
				$pport .= " (" . $wkports[$srcport[0]] . ")";
474
			}
475
		} else
476
			$pport .= $srcport[0] . " - " . $srcport[1];
477
	}
478

    
479
	return $pport;
480
}
481

    
482
function firewall_check_for_advanced_options(&$item) {
483
	$item_set = "";
484
	if($item['max'])
485
		$item_set .= "max {$item['max']} ";
486
	if($item['max-src-nodes'])
487
		$item_set .= "max-src-nodes {$item['max-src-nodes']} ";
488
	if($item['max-src-conn'])
489
		$item_set .= "max-src-conn {$item['max-src-conn']} ";
490
	if($item['max-src-states'])
491
		$item_set .= "max-src-states {$item['max-src-states']} ";
492
	if($item['statetype'] != "keep state" && $item['statetype'] != "")
493
		$item_set .= "statetype {$item['statetype']} ";
494
	if($item['statetimeout'])
495
		$item_set .= "statetimeout {$item['statetimeout']} ";
496
	if($item['nosync'])
497
		$item_set .= "nosync ";
498
	if($item['max-src-conn-rate'])
499
		$item_set .= "max-src-conn-rate {$item['max-src-conn-rate']} ";
500
	if($item['max-src-conn-rates'])
501
		$item_set .= "max-src-conn-rates {$item['max-src-conn-rates']} ";
502
	if($item['gateway'])
503
		$item_set .= "gateway {$item['gateway']} ";
504
	if($item['dnpipe'])
505
		$item_set .= "limiter {$item['dnpipe']} ";
506
	if($item['pdnpipe'])
507
		$item_set .= "limiter {$item['pdnpipe']} ";
508
	if($item['l7container'])
509
		$item_set .= "layer7 {$item['l7container']} ";
510
	if($item['tag'])
511
		$item_set .= "tag {$item['tag']} ";
512
	if($item['tagged'])
513
		$item_set .= "tagged {$item['tagged']} ";
514
	if(isset($item['allowopts']))
515
		$item_set .= "allowopts ";
516
	if(isset($item['disablereplyto']))
517
		$item_set .= "disable reply-to ";
518
	if($item['tcpflags_any'] || $item['tcpflags1'] || $item['tcpflags2'])
519
		$item_set .= "tcpflags set";
520

    
521
	return $item_set;
522
}
523

    
524
function gentitle($title) {
525
	global $navlevelsep;
526
	if(!is_array($title))
527
		return $title;
528
	else
529
		return join($navlevelsep, $title);
530
}
531

    
532
function genhtmltitle($title) {
533
	global $config;
534
	return gentitle($title);
535
}
536

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

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

    
546
function clear_log_file($logfile = "/var/log/system.log", $restart_syslogd = true) {
547
	global $config, $g;
548
	if ($restart_syslogd)
549
		exec("/usr/bin/killall syslogd");
550
	if(isset($config['system']['disablesyslogclog'])) {
551
		unlink($logfile);
552
		touch($logfile);
553
	} else {
554
		$log_size = isset($config['syslog']['logfilesize']) ? $config['syslog']['logfilesize'] : "511488";
555
		if(isset($config['system']['usefifolog']))
556
			exec("/usr/sbin/fifolog_create -s {$log_size} {$logfile}");
557
		else
558
			exec("/usr/sbin/clog -i -s {$log_size} {$logfile}");
559
	}
560
	if ($restart_syslogd)
561
		system_syslogd_start();
562
}
563

    
564
function clear_all_log_files() {
565
	global $g;
566
	exec("/usr/bin/killall syslogd");
567

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

    
573
	system_syslogd_start();
574
	killbyname("dhcpd");
575
	services_dhcpd_configure();
576
	return;
577
}
578

    
579
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
580
	global $g, $config;
581
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
582
	$logarr = "";
583
	$grepline = "  ";
584
	if(is_array($grepfor))
585
		foreach($grepfor as $agrep)
586
			$grepline .= " | grep \"$agrep\"";
587
	if(is_array($grepinvert))
588
		foreach($grepinvert as $agrep)
589
			$grepline .= " | grep -v \"$agrep\"";
590
	if(file_exists($logfile) && filesize($logfile) == 0) {
591
		$logarr = array("Log file started.");
592
	} else {
593
		if($config['system']['disablesyslogclog']) {
594
			exec("cat {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
595
		} else {
596
			if(isset($config['system']['usefifolog']))
597
				exec("/usr/sbin/fifolog_reader {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
598
			else
599
				exec("/usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
600
		}
601
	}
602
	foreach ($logarr as $logent) {
603
			$logent = preg_split("/\s+/", $logent, 6);
604
			echo "<tr valign=\"top\">\n";
605
			if ($withorig) {
606
					if(isset($config['system']['usefifolog'])) {
607
						$entry_date_time = htmlspecialchars(date("F j, Y, g:i a","" . $logent[1] . ""));
608
						$entry_text = htmlspecialchars($logent[5]);
609
					} else {
610
						$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
611
						$entry_text = ($logent[3] ==  $config['system']['hostname']) ? "" : $logent[3] . " ";
612
						$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
613
					}
614
					echo "<td class=\"listlr nowrap\">{$entry_date_time}</td>\n";
615
					echo "<td class=\"listr\">{$entry_text}</td>\n";
616

    
617
			} else {
618
					echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
619
			}
620
			echo "</tr>\n";
621
	}
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
	$logarr = "";
628
	$grepline = "  ";
629
	if(is_array($grepfor))
630
		foreach($grepfor as $agrep)
631
			$grepline .= " | grep \"$agrep\"";
632
	if(is_array($grepinvert))
633
		foreach($grepinvert as $agrep)
634
			$grepline .= " | grep -v \"$agrep\"";
635
	if($config['system']['disablesyslogclog']) {
636
		exec("cat {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
637
	} else {
638
		if(isset($config['system']['usefifolog'])) {
639
			exec("/usr/sbin/fifolog_reader {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
640
		} else {
641
			exec("/usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
642
		}
643
	}
644
	return($logarr);
645
}
646

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

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

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

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

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

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

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

    
724
	if ($pnot)
725
		$adr['not'] = true;
726
	else
727
		unset($adr['not']);
728

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

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

    
741
function is_specialnet($net) {
742
	global $specialsrcdst;
743

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

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

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

    
780
	echo "<script type=\"text/javascript\">";
781
	echo "\n//<![CDATA[\n";
782
	echo "NiftyCheck();\n";
783
	echo "Rounded(\"div.{$tabclass}-tabactive\",\"top\",\"#CCCCCC\",\"#EEEEEE\",\"smooth\");\n";
784
	echo "Rounded(\"div.{$tabclass}-tabdeactive\",\"top\",\"#CCCCCC\",\"#777777\",\"smooth\");\n";
785
	echo "//]]>\n";
786
	echo "</script>";
787
	echo "</div>";
788
}
789

    
790

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

    
803

    
804

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

    
815

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

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

    
870
function is_rfc2616_code($code) {
871
	global $rfc2616;
872
	if (isset($rfc2616[$code]))
873
		return true;
874
	else
875
		return false;
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
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
886
	foreach($rfc2616 as $code => $message) {
887
		if ($code == $current) {
888
			$sel = " selected=\"selected\"";
889
		} else {
890
			$sel = "";
891
		}
892
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
893
	}
894
}
895

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

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

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

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

    
949
	if ($tab_array_space == '') {
950
		$tab_array_space = 1;
951
	}
952

    
953
	if ($tab_array_char_limit == '') {
954
		$tab_array_char_limit = 92;
955
	}
956

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

    
962
	$tab_active_bg   = "#EEEEEE";
963
	$tab_inactive_bg = "#777777";
964
	$nifty_tabs_corners = "#FFF";
965
	$font_color = "white";
966

    
967
	/* if tabcontrols.php exist for a theme, allow it to be overriden */
968
	$themename = $config['theme'];
969
	$filename = "/usr/local/www/themes/{$themename}/tabcontrols.php";
970
	if(file_exists($filename)) {
971
		$eval_code = file_get_contents($filename);
972
		eval($eval_code);
973
	}
974

    
975
	$tabcharcount = 0;
976
	foreach ($tab_array as $ta)
977
		$tabcharcount = $tabcharcount + strlen($ta[0]);
978

    
979
	if($no_drop_down == true) {
980
		$tabcharcount = 0;
981
		unset($tab_array_char_limit);
982
	}
983

    
984
	// If the character count of the tab names is > 670
985
	// then show a select item dropdown menubox.
986
	if($tabcharcount > $tab_array_char_limit) {
987
		echo "Currently viewing: ";
988
		echo "<select name=\"TabSelect\" onchange=\"tabs_will_go(this)\">\n";
989
		foreach ($tab_array as $ta) {
990
			if($ta[1]=="true")
991
				$selected = " selected=\"selected\"";
992
			else
993
				$selected = "";
994
			// Onclick in option will not work in some browser
995
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
996
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
997
		}
998
		echo "</select>\n<p>&nbsp;</p>";
999
		echo "<script type=\"text/javascript\">";
1000
		echo "\n//<![CDATA[\n";
1001
		echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
1002
		echo "//]]>\n";
1003
		echo "</script>";
1004
	}  else {
1005
		echo "<div class=\"newtabmenu\" style=\"margin:{$tab_array_space}px {$tab_array_indent}px; width:775px;\">\n";
1006
		echo "<!-- Tabbed bar code-->\n";
1007
		echo "<ul class=\"newtabmenu\">\n";
1008
		$tabscounter = 0;
1009
		foreach ($tab_array as $ta) {
1010
			if ($ta[1] == true) {
1011
				echo "  <li class=\"newtabmenu_active\"><a href=\"{$ta[2]}\"><span>{$ta[0]}</span></a></li>\n";
1012
			} else {
1013
				echo "  <li><a href=\"{$ta[2]}\"><span>{$ta[0]}</span></a></li>\n";
1014
			}
1015
			$tabscounter++;
1016
		}
1017
		echo "</ul>\n</div>\n";
1018
	}
1019
}
1020

    
1021
function add_package_tabs($tabgroup, & $tab_array) {
1022
	global $config, $g;
1023

    
1024
	if(!is_array($config['installedpackages']))
1025
		return;
1026
	if(!is_array($config['installedpackages']['tab']))
1027
		return;
1028

    
1029
	foreach($config['installedpackages']['tab'] as $tab) {
1030
		if ($tab['group'] !== $group)
1031
			continue;
1032
		$tab_entry = array();
1033
		if($tab['name']) {
1034
			$tab_entry[] = $tab['name'];
1035
			$tab_entry[] = false;
1036
			$tab_entry[] = $tab['url'];
1037
			$tab_array[] = $tab_entry;
1038
		}
1039
	}
1040
}
1041

    
1042
function alias_info_popup($alias_id){
1043
	global $config;
1044
	$maxlength = 60;
1045
	$close_title="title='".gettext('move mouse out this alias to hide')."'";
1046
	if (is_array($config['aliases']['alias'][$alias_id])){
1047
		$alias_name=$config['aliases']['alias'][$alias_id];
1048
		$alias_objects_with_details = "<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\" summary=\"alias info popup\">";
1049
		if ($alias_name['url']) {
1050
			// TODO: Change it when pf supports tables with ports
1051
			if ($alias_name['type'] == "urltable") {
1052
				exec("/sbin/pfctl -t {$alias_name['name']} -T show | wc -l", $total_entries);
1053
				$counter=preg_replace("/\D/","",$total_entries[0]);
1054
				exec("/sbin/pfctl -t {$alias_name['name']} -T show | head -10002", $alias_addresses);
1055
			} else {
1056
				$urlfn = alias_expand_urltable($alias_name['name']);
1057
				$alias_addresses = explode("\n", file_get_contents($urlfn));
1058
				$counter = count($alias_addresses);
1059
			}
1060
			$alias_objects_with_details .= "<tr><td colspan=\"3\" $close_title class=\"vncell\">{$alias_name['url']}</td></tr>";
1061
			$x=0;
1062
			foreach ($alias_addresses as $alias_ports_address ) {
1063
				switch ($x) {
1064
				case 0:
1065
					$x++;
1066
					$alias_objects_with_details .= "<tr><td $close_title class=\"vncell\" width=\"33%\" style=\"background: #FFFFFF;color: #000000;\">{$alias_ports_address}</td>";
1067
					break;
1068
				case 1:
1069
					$x++;
1070
					$alias_objects_with_details .= "<td $close_title class=\"vncell\" width=\"33%\" style=\"background: #FFFFFF;color: #000000;\">{$alias_ports_address}</td>";
1071
					break;
1072
				default:
1073
					$x=0;
1074
					$alias_objects_with_details .= "<td  $close_title class=\"vncell\" width=\"33%\" style=\"background: #FFFFFF;color: #000000;\">{$alias_ports_address}</td><tr>";
1075
					break;
1076
				}
1077
			}
1078
			for ($y = $x; $y <= $x; $y++) {
1079
				$alias_objects_with_details .= "<td $close_title class=\"vncell\" width=\"33%\">&nbsp;</td>";
1080
			}
1081
			if ($x > 0) {
1082
				$alias_objects_with_details .= "</tr>";
1083
			}
1084
			if ($counter > 10002) {
1085
				$alias_objects_with_details .= "<tr><td colspan=\"3\"> listing only first 10k items</td><tr>";
1086
			}
1087
		}
1088
		else{
1089
			$alias_addresses = explode (" ", $alias_name['address']);
1090
			$alias_details = explode ("||", $alias_name['detail']);
1091
			$counter = 0;
1092
			foreach ($alias_addresses as $alias_ports_address) {
1093
				$alias_objects_with_details .= "<tr><td $close_title width=\"5%\" class=\"vncell\" style=\"background: #FFFFFF;color: #000000;\">{$alias_addresses[$counter]}</td>";
1094
				$alias_detail_default = strpos ($alias_details[$counter],"Entry added");
1095
				if ($alias_details[$counter] != "" && $alias_detail_default === False)
1096
					$alias_objects_with_details .="<td $close_title width=\"95%\" class=\"vncell\" style=\"background: #FFFFFF;color: #000000;\">{$alias_details[$counter]}</td>";
1097
				else
1098
					$alias_objects_with_details .="<td $close_title width=\"95%\" class=\"vncell\" style=\"background: #FFFFFF;color: #000000;\">&nbsp;</td>";
1099
				$alias_objects_with_details .= "</tr>";
1100
				$counter++;
1101
			}
1102
		}
1103
		$alias_objects_with_details .= "</table>";
1104
	}
1105
	$alias_descr_substr = $alias_name['descr'];
1106
	if ($strlength >= $maxlength)
1107
		$alias_descr_substr = substr($alias_descr_substr, 0, $maxlength) . "...";
1108
	$item_text = ($counter > 1 ? "items" : "item");
1109
	$alias_caption = "{$alias_descr_substr} - {$counter} {$item_text}<a href=\"/firewall_aliases_edit.php?id={$alias_id}\" title=\"".gettext('edit this alias')."\">&nbsp;&nbsp;edit </a>";
1110
	$strlength = strlen ($alias_caption);
1111
	print "<h1>{$alias_caption}</h1>" . $alias_objects_with_details;
1112
}
1113

    
1114
function rule_popup($src,$srcport,$dst,$dstport){
1115
	global $config,$g;
1116
	$aliases_array = array();
1117
	if ($config['aliases']['alias'] <> "" and is_array($config['aliases']['alias'])) {
1118
		$descriptions = array ();
1119
		foreach ($config['aliases']['alias'] as $alias_id=>$alias_name){
1120
			$loading_image="<a><img src=\'/themes/{$g['theme']}/images/misc/loader.gif\' alt=\'loader\' /> " .gettext("loading...")."</a>";
1121
			switch ($alias_name['type']){
1122
			case "port":
1123
				$width="250";
1124
				break;
1125
			case "urltable":
1126
				$width="500";
1127
				break;
1128
			default:
1129
				$width="350";
1130
				break;
1131
			}
1132
			$span_begin = "<span style=\"cursor: help;\" onmouseover=\"var response_html=domTT_activate(this, event, 'id','ttalias_{$alias_id}','content','{$loading_image}', 'trail', true, 'delay', 300, 'fade', 'both', 'fadeMax', 93, 'styleClass', 'niceTitle','type','velcro','width',{$width});alias_popup('{$alias_id}','{$g['theme']}','".gettext('loading...')."');\" onmouseout=\"this.style.color = ''; domTT_mouseout(this, event);\"><u>";
1133
			$span_end = "</u></span>";
1134
			if ($alias_name['name'] == $src) {
1135
				$descriptions['src'] = $span_begin;
1136
				$descriptions['src_end'] = $span_end;
1137
			}
1138
			if ($alias_name['name'] == $srcport) {
1139
				$descriptions['srcport'] = $span_begin;
1140
				$descriptions['srcport_end'] = $span_end;
1141
			}
1142
			if ($alias_name['name'] == $dst ) {
1143
				$descriptions['dst'] = $span_begin;
1144
				$descriptions['dst_end'] = $span_end;
1145
			}
1146
			if ($alias_name['name'] == $dstport) {
1147
				$descriptions['dstport'] = $span_begin;
1148
				$descriptions['dstport_end'] = $span_end;
1149
			}
1150
		}
1151
		return $descriptions;
1152
	}
1153
}
1154

    
1155
$timezone = $config['system']['timezone'];
1156
if (!$timezone)
1157
	$timezone = "Etc/UTC";
1158

    
1159
date_default_timezone_set($timezone);
1160

    
1161
?>
(86-86/246)