Project

General

Profile

Download (34.4 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
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, &$input_errors) {
250

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

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

    
265
function print_input_errors($input_errors) {
266
	global $g;
267

    
268
	print <<<EOF
269
	<div id="inputerrorsdiv">
270
	<table border="0" cellspacing="0" cellpadding="4" width="100%" summary="input errors">
271
	<tr>
272
		<td class="inputerrorsleft">
273
			<img src="/themes/{$g['theme']}/images/icons/icon_error.gif" alt="errors" />
274
		</td>
275
        <td class="inputerrorsright errmsg">
276
EOF;
277
    echo "<p>" . gettext("The following input errors were detected:") . "</p>\n<ul>";
278
	foreach ($input_errors as $ierr) {
279
		echo "<li>" . htmlspecialchars($ierr) . "</li>";
280
	}
281

    
282
	print <<<EOF2
283
				</ul>
284
		</td></tr>
285
	</table>
286
	</div>
287
	&nbsp;<br />
288
EOF2;
289

    
290
}
291

    
292
function verify_gzip_file($fname) {
293
	$returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
294
	if ($returnvar != 0)
295
		return 0;
296
	else
297
		return 1;
298
}
299

    
300
function print_info_box_np($msg, $name="apply",$value="", $showapply=false) {
301
	global $g, $nifty_redbox, $nifty_blackbox, $nifty_background;
302

    
303
	if(empty($value)) {
304
		$value = gettext("Apply changes");
305
	}
306

    
307
	// Set the Nifty background color if one is not set already (defaults to white)
308
	if($nifty_background == "")
309
		$nifty_background = "#FFF";
310

    
311
	if(stristr($msg, gettext("apply")) != false || stristr($msg, gettext("save")) != false || stristr($msg, gettext("create")) != false || $showapply) {
312
		$savebutton = "<td class=\"infoboxsave\">";
313
		$savebutton .= "<input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\" />";
314
		if($_POST['if'])
315
			$savebutton .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
316
		$savebutton.="</td>";
317
	}
318
	$nifty_redbox = "#990000";
319
	$nifty_blackbox = "#000000";
320

    
321
	$themename = $g['theme'];
322

    
323
	if(file_exists("/usr/local/www/themes/{$themename}/tabcontrols.php")) {
324
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/tabcontrols.php");
325
		eval($toeval);
326
	}
327

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

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

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

    
369
}
370

    
371
function print_info_box_np_undo($msg, $name="apply",$value="Apply changes", $undo) {
372
	global $g;
373

    
374
	if(stristr($msg, "apply") != false || stristr($msg, "save") != false || stristr($msg, "create") != false) {
375
		$savebutton = "<td class=\"infoboxsave nowrap\">";
376
		$savebutton .= "<input type=\"button\" value=\"". gettext("Undo") . "\" onclick=\"document.location='{$undo}'\" />";
377
		$savebutton .= "<input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\" />";
378
		$savebutton .= "</td>";
379
		if($_POST['if'])
380
			$savebutton .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
381
	}
382
	$nifty_redbox = "#990000";
383
	$nifty_blackbox = "#000000";
384

    
385
	$themename = $g['theme'];
386

    
387
	if(file_exists("/usr/local/www/themes/{$themename}/tabcontrols.php")) {
388
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/tabcontrols.php");
389
		eval($toeval);
390
	}
391

    
392
	if(file_exists("/usr/local/www/themes/{$themename}/infobox.php")) {
393
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/infobox.php");
394
		eval($toeval);
395
	}
396

    
397

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

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

    
435
}
436

    
437
function print_info_box($msg) {
438
	print_info_box_np($msg);
439
}
440

    
441
function get_std_save_message($ok) {
442
	global $d_sysrebootreqd_path;
443
	$filter_related = false;
444
	$filter_pages = array("nat", "filter");
445
	$to_return = gettext("The changes have been applied successfully.");
446
	foreach($filter_pages as $fp)
447
		if(stristr($_SERVER['SCRIPT_FILENAME'], $fp))
448
			$filter_related = true;
449
	if($filter_related)
450
		$to_return .= "<br />" . gettext("You can also <a href=\"status_filter_reload.php\">monitor</a> the filter reload progress.");
451
	return $to_return;
452
}
453

    
454
function pprint_address($adr) {
455
	global $specialnets;
456

    
457
	if (isset($adr['any'])) {
458
		$padr = "*";
459
	} else if ($adr['network']) {
460
		$padr = $specialnets[$adr['network']];
461
	} else {
462
		$padr = $adr['address'];
463
	}
464

    
465
	if (isset($adr['not']))
466
		$padr = "! " . $padr;
467

    
468
	return $padr;
469
}
470

    
471
function pprint_port($port) {
472
	global $wkports;
473

    
474
	$pport = "";
475

    
476
	if (!$port)
477
		return "*";
478
	else {
479
		$srcport = explode("-", $port);
480
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
481
			$pport = $srcport[0];
482
			if ($wkports[$srcport[0]]) {
483
				$pport .= " (" . $wkports[$srcport[0]] . ")";
484
			}
485
		} else
486
			$pport .= $srcport[0] . " - " . $srcport[1];
487
	}
488

    
489
	return $pport;
490
}
491

    
492
function firewall_check_for_advanced_options(&$item) {
493
	$item_set = "";
494
	if($item['os'])
495
		$item_set .= "os {$item['os']} ";
496
	if($item['dscp'])
497
		$item_set .= "dscp {$item['dscp']} ";
498
	if($item['max'])
499
		$item_set .= "max {$item['max']} ";
500
	if($item['max-src-nodes'])
501
		$item_set .= "max-src-nodes {$item['max-src-nodes']} ";
502
	if($item['max-src-conn'])
503
		$item_set .= "max-src-conn {$item['max-src-conn']} ";
504
	if($item['max-src-states'])
505
		$item_set .= "max-src-states {$item['max-src-states']} ";
506
	if(isset($item['nopfsync']))
507
		$item_set .= "nopfsync ";
508
	if($item['statetype'] != "keep state" && $item['statetype'] != "")
509
		$item_set .= "statetype {$item['statetype']} ";
510
	if($item['statetimeout'])
511
		$item_set .= "statetimeout {$item['statetimeout']} ";
512
	if(isset($item['nosync']))
513
		$item_set .= "no XMLRPC Sync ";
514
	if($item['max-src-conn-rate'])
515
		$item_set .= "max-src-conn-rate {$item['max-src-conn-rate']} ";
516
	if($item['max-src-conn-rates'])
517
		$item_set .= "max-src-conn-rates {$item['max-src-conn-rates']} ";
518
	if($item['vlanprio'])
519
		$item_set .= "vlanprio {$item['vlanprio']} ";
520
	if($item['vlanprioset'])
521
		$item_set .= "vlanprioset {$item['vlanprioset']} ";
522
	if($item['gateway'])
523
		$item_set .= "gateway {$item['gateway']} ";
524
	if($item['dnpipe'])
525
		$item_set .= "limiter {$item['dnpipe']} ";
526
	if($item['pdnpipe'])
527
		$item_set .= "limiter {$item['pdnpipe']} ";
528
	if($item['ackqueue'])
529
		$item_set .= "ackqueue {$item['ackqueue']} ";
530
	if($item['defaultqueue'])
531
		$item_set .= "defaultqueue {$item['defaultqueue']} ";
532
	if($item['l7container'])
533
		$item_set .= "layer7 {$item['l7container']} ";
534
	if($item['tag'])
535
		$item_set .= "tag {$item['tag']} ";
536
	if($item['tagged'])
537
		$item_set .= "tagged {$item['tagged']} ";
538
	if(isset($item['allowopts']))
539
		$item_set .= "allowopts ";
540
	if(isset($item['disablereplyto']))
541
		$item_set .= "disable reply-to ";
542
	if($item['tcpflags_any'] || $item['tcpflags1'] || $item['tcpflags2'])
543
		$item_set .= "tcpflags set";
544

    
545
	return $item_set;
546
}
547

    
548
function gentitle($title) {
549
	global $navlevelsep;
550
	if(!is_array($title))
551
		return $title;
552
	else
553
		return join($navlevelsep, $title);
554
}
555

    
556
function genhtmltitle($title) {
557
	global $config;
558
	return gentitle($title);
559
}
560

    
561
/* update the changedesc and changecount(er) variables */
562
function update_changedesc($update) {
563
	global $changedesc;
564
	global $changecount;
565

    
566
	$changedesc .= " {$update}";
567
	$changecount++;
568
}
569

    
570
function clear_log_file($logfile = "/var/log/system.log", $restart_syslogd = true) {
571
	global $config, $g;
572
	if ($restart_syslogd)
573
		exec("/usr/bin/killall syslogd");
574
	if(isset($config['system']['disablesyslogclog'])) {
575
		unlink($logfile);
576
		touch($logfile);
577
	} else {
578
		$log_size = isset($config['syslog']['logfilesize']) ? $config['syslog']['logfilesize'] : "511488";
579
		if(isset($config['system']['usefifolog']))
580
			exec("/usr/sbin/fifolog_create -s {$log_size} " . escapeshellarg($logfile));
581
		else
582
			exec("/usr/local/sbin/clog -i -s {$log_size} " . escapeshellarg($logfile));
583
	}
584
	if ($restart_syslogd)
585
		system_syslogd_start();
586
}
587

    
588
function clear_all_log_files() {
589
	global $g;
590
	exec("/usr/bin/killall syslogd");
591

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

    
597
	system_syslogd_start();
598
	killbyname("dhcpd");
599
	services_dhcpd_configure();
600
	return;
601
}
602

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

    
641
			} else {
642
					echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
643
			}
644
			echo "</tr>\n";
645
	}
646
}
647

    
648
function return_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
649
	global $g, $config;
650
	$sor = (isset($config['syslog']['reverse']) || $grepreverse) ? "-r" : "";
651
	$logarr = "";
652
	$grepline = "  ";
653
	if(is_array($grepfor))
654
		$grepline .= " | /usr/bin/egrep " . escapeshellarg(implode("|", $grepfor));
655
	if(is_array($grepinvert))
656
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
657
	if($config['system']['disablesyslogclog']) {
658
		exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
659
	} else {
660
		if(isset($config['system']['usefifolog'])) {
661
			exec("/usr/sbin/fifolog_reader " . 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
	}
666
	return($logarr);
667
}
668

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

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

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

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

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

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

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

    
746
	if ($pnot)
747
		$adr['not'] = true;
748
	else
749
		unset($adr['not']);
750

    
751
	if (($pbeginport != 0) && ($pbeginport != "any")) {
752
		if ($pbeginport != $pendport)
753
			$adr['port'] = $pbeginport . "-" . $pendport;
754
		else
755
			$adr['port'] = $pbeginport;
756
	}
757

    
758
	if(is_alias($pbeginport)) {
759
		$adr['port'] = $pbeginport;
760
	}
761
}
762

    
763
function is_specialnet($net) {
764
	global $specialsrcdst;
765

    
766
	if(!$net)
767
		return false;
768
	if (in_array($net, $specialsrcdst))
769
		return true;
770
	else
771
		return false;
772
}
773

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

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

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

    
812

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

    
825

    
826

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

    
837

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

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

    
892
function is_rfc2616_code($code) {
893
	global $rfc2616;
894
	if (isset($rfc2616[$code]))
895
		return true;
896
	else
897
		return false;
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
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
908
	foreach($rfc2616 as $code => $message) {
909
		if ($code == $current) {
910
			$sel = " selected=\"selected\"";
911
		} else {
912
			$sel = "";
913
		}
914
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
915
	}
916
	echo "</select>\n";
917
}
918

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

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

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

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

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

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

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

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

    
990
	/* if tabcontrols.php exist for a theme, allow it to be overriden */
991
	$themename = $config['theme'];
992
	$filename = "/usr/local/www/themes/{$themename}/tabcontrols.php";
993
	if(file_exists($filename)) {
994
		$eval_code = file_get_contents($filename);
995
		eval($eval_code);
996
	}
997

    
998
	$tabcharcount = 0;
999
	foreach ($tab_array as $ta)
1000
		$tabcharcount = $tabcharcount + strlen($ta[0]);
1001

    
1002
	if($no_drop_down == true) {
1003
		$tabcharcount = 0;
1004
		unset($tab_array_char_limit);
1005
	}
1006

    
1007
	// If the character count of the tab names is > 670
1008
	// then show a select item dropdown menubox.
1009
	if($tabcharcount > $tab_array_char_limit) {
1010
		echo gettext("Currently viewing: ");
1011
		echo "<select name=\"TabSelect\" onchange=\"tabs_will_go(this)\">\n";
1012
		foreach ($tab_array as $ta) {
1013
			if($ta[1]=="true")
1014
				$selected = " selected=\"selected\"";
1015
			else
1016
				$selected = "";
1017
			// Onclick in option will not work in some browser
1018
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
1019
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
1020
		}
1021
		echo "</select>\n<p>&nbsp;</p>";
1022
		echo "<script type=\"text/javascript\">";
1023
		echo "\n//<![CDATA[\n";
1024
		echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
1025
		echo "//]]>\n";
1026
		echo "</script>";
1027
	}  else {
1028
		echo "<div class=\"newtabmenu\" style=\"margin:{$tab_array_space}px {$tab_array_indent}px; width:775px;\">\n";
1029
		echo "<!-- Tabbed bar code-->\n";
1030
		echo "<ul class=\"newtabmenu\">\n";
1031
		$tabscounter = 0;
1032
		foreach ($tab_array as $ta) {
1033
			if ($ta[1] == true) {
1034
				echo "  <li class=\"newtabmenu_active\"><a href=\"{$ta[2]}\"><span>{$ta[0]}</span></a></li>\n";
1035
			} else {
1036
				echo "  <li><a href=\"{$ta[2]}\"><span>{$ta[0]}</span></a></li>\n";
1037
			}
1038
			$tabscounter++;
1039
		}
1040
		echo "</ul>\n</div>\n";
1041
	}
1042
}
1043

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

    
1047
	if(!is_array($config['installedpackages']))
1048
		return;
1049
	if(!is_array($config['installedpackages']['tab']))
1050
		return;
1051

    
1052
	foreach($config['installedpackages']['tab'] as $tab) {
1053
		if ($tab['group'] !== $group)
1054
			continue;
1055
		$tab_entry = array();
1056
		if($tab['name']) {
1057
			$tab_entry[] = $tab['name'];
1058
			$tab_entry[] = false;
1059
			$tab_entry[] = $tab['url'];
1060
			$tab_array[] = $tab_entry;
1061
		}
1062
	}
1063
}
1064

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

    
1137
function rule_popup($src,$srcport,$dst,$dstport){
1138
	global $config,$g;
1139
	$aliases_array = array();
1140
	if ($config['aliases']['alias'] <> "" and is_array($config['aliases']['alias'])) {
1141
		$descriptions = array ();
1142
		foreach ($config['aliases']['alias'] as $alias_id=>$alias_name){
1143
			$loading_image="<a><img src=\'/themes/{$g['theme']}/images/misc/loader.gif\' alt=\'loader\' /> " .gettext("loading...")."</a>";
1144
			switch ($alias_name['type']){
1145
			case "port":
1146
				$width="250";
1147
				break;
1148
			case "urltable":
1149
				$width="500";
1150
				break;
1151
			default:
1152
				$width="350";
1153
				break;
1154
			}
1155
			$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>";
1156
			$span_end = "</u></span>";
1157
			if ($alias_name['name'] == $src) {
1158
				$descriptions['src'] = $span_begin;
1159
				$descriptions['src_end'] = $span_end;
1160
			}
1161
			if ($alias_name['name'] == $srcport) {
1162
				$descriptions['srcport'] = $span_begin;
1163
				$descriptions['srcport_end'] = $span_end;
1164
			}
1165
			if ($alias_name['name'] == $dst ) {
1166
				$descriptions['dst'] = $span_begin;
1167
				$descriptions['dst_end'] = $span_end;
1168
			}
1169
			if ($alias_name['name'] == $dstport) {
1170
				$descriptions['dstport'] = $span_begin;
1171
				$descriptions['dstport_end'] = $span_end;
1172
			}
1173
		}
1174
		return $descriptions;
1175
	}
1176
}
1177

    
1178
$timezone = $config['system']['timezone'];
1179
if (!$timezone)
1180
	$timezone = "Etc/UTC";
1181

    
1182
date_default_timezone_set($timezone);
1183

    
1184
?>
(87-87/251)