Project

General

Profile

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

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

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

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

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

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

    
36
/* Include authentication routines */
37
/* THIS MUST BE ABOVE ALL OTHER CODE */
38
require_once("authgui.inc");
39

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

    
49
/* parse the configuration and include all configuration functions */
50
require_once("functions.inc");
51

    
52
/* Pull in all the gui related display classes) */
53
foreach (scandir("/usr/local/www/classes/") as $file) {
54
	if (stristr($file, ".inc") !== false) {
55
		require_once("classes/{$file}");
56
	}
57
}
58
/*
59
 *   if user has selected a custom template, use it.
60
 *   otherwise default to pfsense template
61
 */
62
if($config['theme'] <> "")
63
        $g['theme'] = $config['theme'];
64
else
65
        $g['theme'] = "pfsense";
66

    
67
/*
68
 *  If this device is an apple ipod/iphone
69
 *  switch the theme to one that works with it.
70
 */
71
$apple_ua = array("iPhone","iPod", "iPad");
72
foreach($apple_ua as $useragent)
73
	if(strstr($_SERVER['HTTP_USER_AGENT'], $useragent))
74
		$g['theme'] = "pfsense";
75

    
76
/* used by progress bar */
77
$lastseen = "-1";
78

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

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

    
95
$firewall_rules_dscp_types = array("af11", 
96
				"af12", 
97
				"af13", 
98
				"af21", 
99
				"af22", 
100
				"af23", 
101
				"af31", 
102
				"af32", 
103
				"af33", 
104
				"af41", 
105
				"af42", 
106
				"af43", 
107
				"EF", 
108
				"1-64", 
109
				"0x10",
110
				"0x04-0xfc");
111

    
112
$auth_server_types = array(
113
	'ldap' => "LDAP",
114
	'radius' => "Radius");
115

    
116
$ldap_urltypes = array(
117
	'TCP - Standard' => 389,
118
	'SSL - Encrypted' => 636);
119

    
120
$ldap_scopes = array(
121
	'one' => "One Level",
122
	'subtree' => "Entire Subtree");
123

    
124
$ldap_protvers = array(
125
	2,
126
	3);
127

    
128
$ldap_templates = array(
129

    
130
	'open' => array(
131
				'desc' => "OpenLDAP",
132
				'attr_user' => "cn",
133
				'attr_group' => "cn",
134
				'attr_member' => "member"),
135

    
136
	'msad' => array(
137
				'desc' => "Microsoft AD",
138
				'attr_user' => "samAccountName",
139
				'attr_group' => "cn",
140
				'attr_member' => "memberOf"),
141

    
142
	'edir' => array(
143
				'desc' => "Novell eDirectory",
144
				'attr_user' => "cn",
145
				'attr_group' => "cn",
146
				'attr_member' => "uniqueMember"));
147

    
148
$radius_srvcs = array(
149
	'both' => "Authentication and Accounting",
150
	'auth' => "Authentication",
151
	'acct' => "Accounting");
152

    
153
$netbios_nodetypes = array(
154
	'0' => "none",
155
	'1' => "b-node",
156
	'2' => "p-node",
157
	'4' => "m-node",
158
	'5' => "h-node");
159

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

    
206
/* TCP flags */
207
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg");
208

    
209
$specialnets = array("wanip" => "WAN address", "lanip" => "LAN address", "lan" => "LAN net", "pptp" => "PPTP clients", "pppoe" => "PPPoE clients", "l2tp" => "L2TP clients");
210

    
211
$spiflist = get_configured_interface_with_descr(true, true);
212
foreach ($spiflist as $ifgui => $ifdesc) {
213
	$specialnets[$ifgui] = $ifdesc . " net";
214
}
215

    
216
$medias = array("auto" => "autoselect", "100full" => "100BASE-TX full-duplex",
217
	"100half" => "100BASE-TX half-duplex", "10full" => "10BASE-T full-duplex",
218
	"10half" => "10BASE-T half-duplex");
219

    
220
$wlan_modes = array("bss" => "Infrastructure (BSS)", "adhoc" => "Ad-hoc (IBSS)",
221
	"hostap" => "Access Point");
222

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

    
226
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
227

    
228
	/* check for bad control characters */
229
	foreach ($postdata as $pn => $pd) {
230
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
231
			$input_errors[] = "The field '" . $pn . "' contains invalid characters.";
232
		}
233
	}
234

    
235
	for ($i = 0; $i < count($reqdfields); $i++) {
236
		if ($_POST[$reqdfields[$i]] == "" && $_REQUEST[$reqdfields[$i]] == "") {
237
			$input_errors[] = "The field '" . $reqdfieldsn[$i] . "' is required.";
238
		}
239
	}
240
}
241

    
242
function print_input_errors($input_errors) {
243
	global $g;
244

    
245
	print <<<EOF
246
	<p>
247
	<table border="0" cellspacing="0" cellpadding="4" width="100%">
248
	<tr>
249
		<td class="inputerrorsleft">
250
			<img src="./themes/{$g['theme']}/images/icons/icon_error.gif">
251
		</td>
252
		<td class="inputerrorsright">
253
			<span class="errmsg"><p>
254
				The following input errors were detected:
255
				<ul>
256
EOF;
257
		foreach ($input_errors as $ierr) {
258
			echo "<li>" . htmlspecialchars($ierr) . "</li>";
259
		}
260

    
261
	print <<<EOF2
262
				</ul>
263
			</span>
264
		</td></tr>
265
	</table>
266
	</p>&nbsp;<br>
267
EOF2;
268
	
269
}
270

    
271
function verify_gzip_file($fname) {
272

    
273
    $returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
274
	if ($returnvar != 0)
275
		return 0;
276
	else
277
		return 1;
278
}
279

    
280
function print_info_box_np($msg, $name="apply",$value="Apply changes") {
281
	global $g, $nifty_redbox, $nifty_blackbox, $nifty_background;
282

    
283
	// Set the Nifty background color if one is not set already (defaults to white)
284
	if($nifty_background == "")
285
		$nifty_background = "#FFF";
286

    
287
	if(stristr($msg, "apply") != false || stristr($msg, "save") != false || stristr($msg, "create") != false) {
288
		$savebutton = "<td class='infoboxsave'>";
289
		$savebutton .= "<input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\">";
290
		if($_POST['if']) 
291
			$savebutton .= "<input type='hidden' name='if' value='{$_POST['if']}'>";
292
		$savebutton.="</td>";
293
	}
294
	$nifty_redbox = "#990000";
295
	$nifty_blackbox = "#000000";
296
	
297
	$themename = $g['theme'];
298
	
299
	if(file_exists("/usr/local/www/themes/{$themename}/tabcontrols.php")) {
300
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/tabcontrols.php");
301
		eval($toeval);
302
	}
303
	
304
	if(file_exists("/usr/local/www/themes/{$themename}/infobox.php")) {
305
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/infobox.php");
306
		eval($toeval);
307
	}	
308
		
309
	if(!$savebutton) {
310
		$savebutton = '<td class="infoboxsave"><input value="Close" type="button" onClick="$(\'redboxtable\').hide();"></td>';
311
	}
312

    
313
	echo <<<EOFnp
314
	<table class='infobox' id='redboxtable'>
315
		<tr>
316
			<td>
317
				<div class='infoboxnp' id='redbox'>
318
					<table class='infoboxnptable2'>
319
						<tr>
320
							<td class='infoboxnptd'>
321
								&nbsp;&nbsp;&nbsp;<img class='infoboxnpimg' src="/themes/{$g['theme']}/images/icons/icon_exclam.gif" >
322
							</td>
323
							<td class='infoboxnptd2'>
324
								<b>{$msg}</b>
325
							</td>
326
							{$savebutton}
327
						</tr>
328
					</table>
329
				</div>
330
				<div>
331
					<p/>
332
				</div>
333
			</td>
334
		</tr>
335
	</table>
336
	<script type="text/javascript">
337
		NiftyCheck();
338
		Rounded("div#redbox","all","{$nifty_background}","{$nifty_redbox}","smooth");
339
		Rounded("td#blackbox","all","{$nifty_background}","{$nifty_blackbox}","smooth");
340
	</script>
341
EOFnp;
342

    
343
}
344

    
345
function print_info_box_np_undo($msg, $name="apply",$value="Apply changes", $undo) {
346
	global $g;
347
	
348
	if(stristr($msg, "apply") != false || stristr($msg, "save") != false || stristr($msg, "create") != false) {
349
		$savebutton = "<td class='infoboxsave'><nobr>";
350
		$savebutton .= " <input type=\"button\" value=\"Undo\" onClick=\"document.location='{$undo}'\">";
351
		$savebutton .= " <input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\">";
352
		$savebutton.="</nobr></td>";
353
		if($_POST['if']) 
354
			$savebutton .= "<input type='hidden' name='if' value='{$_POST['if']}'>";
355
	}
356
	$nifty_redbox = "#990000";
357
	$nifty_blackbox = "#000000";
358
	
359
	$themename = $g['theme'];
360
	
361
	if(file_exists("/usr/local/www/themes/{$themename}/tabcontrols.php")) {
362
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/tabcontrols.php");
363
		eval($toeval);
364
	}
365
	
366
	if(file_exists("/usr/local/www/themes/{$themename}/infobox.php")) {
367
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/infobox.php");
368
		eval($toeval);
369
	}	
370
	
371
		
372
	if(!$savebutton) {
373
		$savebutton = '<td class="infoboxsave"><input value="Close" type="button" onClick="$(\'redboxtable\').hide();"></td>';
374
	}
375

    
376
	echo <<<EOFnp
377
	<table class='infobox' id='redboxtable'>
378
		<tr>
379
			<td>
380
				<div class='infoboxnp' id='redbox'>
381
					<table class='infoboxnptable2'>
382
						<tr>
383
							<td class='infoboxnptd'>
384
								&nbsp;&nbsp;&nbsp;<img class='infoboxnpimg' src="/themes/{$g['theme']}/images/icons/icon_exclam.gif" >
385
							</td>
386
							<td class='infoboxnptd2'>
387
								<b>{$msg}</b>
388
							</td>
389
							{$savebutton} 
390
							{$undobutton}
391
						</tr>
392
					</table>
393
				</div>
394
				<div>
395
					<p/>
396
				</div>
397
			</td>
398
		</tr>
399
	</table>
400
	<script type="text/javascript">
401
		NiftyCheck();
402
		Rounded("div#redbox","all","#FFF","{$nifty_redbox}","smooth");
403
		Rounded("td#blackbox","all","#FFF","{$nifty_blackbox}","smooth");
404
	</script>
405
EOFnp;
406

    
407
}
408

    
409
function print_info_box($msg) {
410
	print_info_box_np($msg);
411
}
412

    
413
function get_std_save_message($ok) {
414
	global $d_sysrebootreqd_path;
415

    
416
	return "The changes have been applied successfully.  You can also <a href='status_filter_reload.php'>monitor</a> the filter reload progress.";
417
}
418

    
419
function pprint_address($adr) {
420
	global $specialnets;
421

    
422
	if (isset($adr['any'])) {
423
		$padr = "*";
424
	} else if ($adr['network']) {
425
		if (preg_match("/opt[0-999]ip/", $adr['network'])) {
426
			$padr = "Interface IP address";
427
		} else {
428
			$padr = $specialnets[$adr['network']];
429
		}
430
	} else {
431
		$padr = $adr['address'];
432
	}
433

    
434
	if (isset($adr['not']))
435
		$padr = "! " . $padr;
436

    
437
	return $padr;
438
}
439

    
440
function pprint_port($port) {
441
	global $wkports;
442

    
443
	$pport = "";
444

    
445
	if (!$port)
446
		return "*";
447
	else {
448
		$srcport = explode("-", $port);
449
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
450
			$pport = $srcport[0];
451
			if ($wkports[$srcport[0]]) {
452
				$pport .= " (" . $wkports[$srcport[0]] . ")";
453
			}
454
		} else
455
			$pport .= $srcport[0] . " - " . $srcport[1];
456
	}
457

    
458
	return $pport;
459
}
460

    
461
/* sort by interface only, retain the original order of rules that apply to
462
   the same interface */
463
function filter_rules_sort() {
464
	global $config;
465

    
466
	/* mark each rule with the sequence number (to retain the order while sorting) */
467
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
468
		$config['filter']['rule'][$i]['seq'] = $i;
469

    
470
	function filtercmp($a, $b) {
471
		if ($a['interface'] == $b['interface'])
472
			return $a['seq'] - $b['seq'];
473
		else
474
			return -strcmp($a['interface'], $b['interface']);
475
	}
476

    
477
	usort($config['filter']['rule'], "filtercmp");
478

    
479
	/* strip the sequence numbers again */
480
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
481
		unset($config['filter']['rule'][$i]['seq']);
482
}
483

    
484
function gentitle($title) {
485
	global $navlevelsep;
486
	if(!is_array($title))
487
		return $title;
488
	else
489
		return join($navlevelsep, $title);
490
}
491

    
492
function genhtmltitle($title) {
493
        global $config;
494
        return gentitle($title);
495
}
496

    
497
/* update the changedesc and changecount(er) variables */
498
function update_changedesc($update) {
499
	global $changedesc;
500
	global $changecount;
501

    
502
	$changedesc .= " {$update}";
503
	$changecount++;
504
}
505

    
506
function clear_log_file($logfile = "/var/log/system.log") {
507
	global $config, $g;
508
	exec("/usr/bin/killall syslogd");
509
	if(isset($config['system']['disablesyslogclog'])) {
510
		unlink($logfile);
511
		touch($logfile);
512
	} else {
513
		if(isset($config['system']['usefifolog'])) 
514
			exec("/usr/sbin/fifolog_create -s 511488 {$logfile}");
515
		else
516
			exec("/usr/sbin/clog -i -s 511488 {$logfile}");
517
	}
518
	system_syslogd_start();	
519
}
520

    
521
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
522
	global $g, $config;
523
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
524
	$logarr = "";
525
	$grepline = "  ";
526
	if(is_array($grepfor))
527
		foreach($grepfor as $agrep)
528
			$grepline .= " | grep \"$agrep\"";
529
	if(is_array($grepinvert))
530
		foreach($grepinvert as $agrep)
531
			$grepline .= " | grep -v \"$agrep\"";
532
	if(file_exists($logfile) && filesize($logfile) == 0) {
533
		$logarr = array("Log file started.");
534
	} else {
535
		if($config['system']['disablesyslogclog']) {
536
			exec("cat {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
537
		} else {
538
			if(isset($config['system']['usefifolog']))
539
				exec("/usr/sbin/fifolog_reader {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
540
			else
541
				exec("/usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
542
		}
543
	}
544
	foreach ($logarr as $logent) {
545
			$logent = preg_split("/\s+/", $logent, 6);
546
			echo "<tr valign=\"top\">\n";
547
			if ($withorig) {
548
					if(isset($config['system']['usefifolog'])) {
549
						$entry_date_time = htmlspecialchars(date("F j, Y, g:i a","" . $logent[1] . ""));
550
						$entry_text = htmlspecialchars($logent[5]);
551
					} else {
552
						$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
553
						$entry_text = htmlspecialchars($logent[4] . " " . $logent[5]);
554
					}
555
					echo "<td class=\"listlr\" nowrap>{$entry_date_time}</td>\n";
556
					echo "<td class=\"listr\">{$entry_text}</td>\n";
557

    
558
			} else {
559
					echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
560
			}
561
			echo "</tr>\n";
562
	}
563
}
564

    
565
function return_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
566
	global $g, $config;
567
	$sor = (isset($config['syslog']['reverse']) || $grepreverse) ? "-r" : "";
568
	$logarr = "";
569
	$grepline = "  ";
570
	if(is_array($grepfor))
571
		foreach($grepfor as $agrep)
572
			$grepline .= " | grep \"$agrep\"";
573
	if(is_array($grepinvert))
574
		foreach($grepinvert as $agrep)
575
			$grepline .= " | grep -v \"$agrep\"";
576
	if($config['system']['disablesyslogclog']) {
577
		exec("cat {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
578
	} else {
579
		if(isset($config['system']['usefifolog'])) {
580
			exec("/usr/sbin/fifolog_reader {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);			
581
		} else {
582
			exec("/usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
583
		}
584
	}
585
	return($logarr);
586
}
587

    
588
/* Check if variable has changed, update and log if it has
589
 * returns true if var changed
590
 * varname = variable name in plain text
591
 * orig = original value
592
 * new = new value
593
 */
594
function update_if_changed($varname, & $orig, $new) {
595
	if (is_array($orig) && is_array($new)) {
596
		$a_diff = array_diff($orig, $new);
597
		foreach ($a_diff as $diff) {
598
			update_changedesc("removed {$varname}: \"{$diff}\"");
599
		}
600
		$a_diff = array_diff($new, $orig);
601
		foreach ($a_diff as $diff) {
602
			update_changedesc("added {$varname}: \"{$diff}\"");
603
		}
604
		$orig = $new;
605
		return true;
606

    
607
	} else {
608
		if ($orig != $new) {
609
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
610
			$orig = $new;
611
			return true;
612
		}
613
	}
614
	return false;
615
}
616

    
617
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
618

    
619
        if (isset($adr['any']))
620
                $padr = "any";
621
        else if ($adr['network'])
622
                $padr = $adr['network'];
623
        else if ($adr['address']) {
624
                list($padr, $pmask) = explode("/", $adr['address']);
625
                if (!$pmask)
626
                        $pmask = 32;
627
        }
628

    
629
        if (isset($adr['not']))
630
                $pnot = 1;
631
        else
632
                $pnot = 0;
633

    
634
        if ($adr['port']) {
635
                list($pbeginport, $pendport) = explode("-", $adr['port']);
636
                if (!$pendport)
637
                        $pendport = $pbeginport;
638
	} else if (!is_alias($pbeginport) && !is_alias($pendport)) {
639
		$pbeginport = "any";
640
		$pendport = "any";
641
        }
642
}
643

    
644
function pconfig_to_address(&$adr, $padr, $pmask, $pnot=false, $pbeginport=0, $pendport=0) {
645

    
646
        $adr = array();
647

    
648
        if ($padr == "any")
649
                $adr['any'] = true;
650
        else if (is_specialnet($padr))
651
                $adr['network'] = $padr;
652
        else {
653
                $adr['address'] = $padr;
654
                if ($pmask != 32)
655
                        $adr['address'] .= "/" . $pmask;
656
        }
657

    
658
        if ($pnot)
659
                $adr['not'] = true;
660
        else
661
                unset($adr['not']);
662

    
663
        if (($pbeginport != 0) && ($pbeginport != "any")) {
664
                if ($pbeginport != $pendport)
665
                        $adr['port'] = $pbeginport . "-" . $pendport;
666
                else
667
                        $adr['port'] = $pbeginport;
668
        }
669

    
670
        if(is_alias($pbeginport)) {
671
                $adr['port'] = $pbeginport;
672
        }
673
}
674

    
675
function is_specialnet($net) {
676
        global $specialsrcdst;
677

    
678
	if(!$net) 
679
		return false;
680
        if (in_array($net, $specialsrcdst))
681
                return true;
682
        else
683
                return false;
684
}
685

    
686
//function to create widget tabs when called
687
function display_widget_tabs(& $tab_array) {	
688
	echo "<div id='tabs'>";
689
	$tabscounter = 0;
690
	foreach ($tab_array as $ta) {
691
	$dashpos = strpos($ta[2],'-');
692
	$tabname = $ta[2] . "-tab";
693
	$tabclass = substr($ta[2],0,$dashpos);
694
	$tabclass = $tabclass . "-class";
695
		if ($ta[1] == true) {
696
			$tabActive = "table-cell";
697
			$tabNonActive = "none";
698
		} 
699
		else {
700
			$tabActive = "none";
701
			$tabNonActive = "table-cell";
702
		}
703
		echo "<div id='{$ta[2]}-active' class='{$tabclass}-tabactive' style='display:{$tabActive}; background-color:#EEEEEE; color:black;'>";
704
		echo "<B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
705
		echo "&nbsp;&nbsp;&nbsp;</B>";
706
		echo "</div>";
707
		
708
		echo "<div id='{$ta[2]}-deactive' class='{$tabclass}-tabdeactive' style='display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;' onClick=\"return changeTabDIV('{$ta[2]}')\">";
709
		echo "<B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
710
		echo "&nbsp;&nbsp;&nbsp;</B>";
711
		echo "</div>";
712
	}
713
	
714
	echo "<script type=\"text/javascript\">";
715
	echo "NiftyCheck();\n";
716
	echo "Rounded(\"div.{$tabclass}-tabactive\",\"top\",\"#CCCCCC\",\"#EEEEEE\",\"smooth\");\n";
717
	echo "Rounded(\"div.{$tabclass}-tabdeactive\",\"top\",\"#CCCCCC\",\"#777777\",\"smooth\");\n";
718
	echo "</script>";
719
	echo "</div>";
720
}
721

    
722

    
723
// Return inline javascript file or CSS to minimizie 
724
// request count going back to server.
725
function outputJavaScriptFileInline($javascript) {
726
	if(file_exists($javascript)) {
727
		echo "\n<script type=\"text/javascript\">\n";
728
		include($javascript);
729
		echo "\n</script>\n";
730
	} else {
731
		echo "\n\n<!-- Could not location file:  {$javascript} -->\n\n";
732
	}
733
}
734

    
735

    
736

    
737
function outputCSSPrintFileInline($css) {
738
	if(file_exists($css)) {
739
		echo "\n<style media=\"print\" type=\"text/css\">\n";
740
		include($css);
741
		echo "\n</style>\n";
742
	} else {
743
		echo "\n\n<!-- Could not location file:  {$css} -->\n\n";
744
	}
745
}
746

    
747

    
748
function outputCSSFileInline($css) {
749
	if(file_exists($css)) {
750
		echo "\n<style type=\"text/css\">\n";
751
		include($css);
752
		echo "\n</style>\n";
753
	} else {
754
		echo "\n\n<!-- Could not location file:  {$css} -->\n\n";
755
	}
756
}
757

    
758
$rfc2616 = array(
759
	100 => "100 Continue",
760
	101 => "101 Switching Protocols",
761
	200 => "200 OK",
762
	201 => "201 Created",
763
	202 => "202 Accepted",
764
	203 => "203 Non-Authoritative Information",
765
	204 => "204 No Content",
766
	205 => "205 Reset Content",
767
	206 => "206 Partial Content",
768
	300 => "300 Multiple Choices",
769
	301 => "301 Moved Permanently",
770
	302 => "302 Found",
771
	303 => "303 See Other",
772
	304 => "304 Not Modified",
773
	305 => "305 Use Proxy",
774
	306 => "306 (Unused)",
775
	307 => "307 Temporary Redirect",
776
	400 => "400 Bad Request",
777
	401 => "401 Unauthorized",
778
	402 => "402 Payment Required",
779
	403 => "403 Forbidden",
780
	404 => "404 Not Found",
781
	405 => "405 Method Not Allowed",
782
	406 => "406 Not Acceptable",
783
	407 => "407 Proxy Authentication Required",
784
	408 => "408 Request Timeout",
785
	409 => "409 Conflict",
786
	410 => "410 Gone",
787
	411 => "411 Length Required",
788
	412 => "412 Precondition Failed",
789
	413 => "413 Request Entity Too Large",
790
	414 => "414 Request-URI Too Long",
791
	415 => "415 Unsupported Media Type",
792
	416 => "416 Requested Range Not Satisfiable",
793
	417 => "417 Expectation Failed",
794
	500 => "500 Internal Server Error",
795
	501 => "501 Not Implemented",
796
	502 => "502 Bad Gateway",
797
	503 => "503 Service Unavailable",
798
	504 => "504 Gateway Timeout",
799
	505 => "505 HTTP Version Not Supported"
800
);
801

    
802
function is_rfc2616_code($code) {
803
	global $rfc2616;
804
	if (isset($rfc2616[$code]))
805
		return true;
806
	else
807
		return false;
808
}
809

    
810
function print_rfc2616_select($tag, $current){
811
	global $rfc2616;
812

    
813
	/* Default to 200 OK if not set */
814
	if ($current == "")
815
		$current = 200;
816

    
817
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";	
818
	foreach($rfc2616 as $code => $message) {
819
		if ($code == $current) {
820
			$sel = " selected";
821
		} else {
822
			$sel = "";
823
		}
824
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
825
	}
826
}
827

    
828
// Useful debugging function, much cleaner than print_r
829
function echo_array($array,$return_me=false){
830
    if(is_array($array) == false){
831
        $return = "The provided variable is not an array.";
832
    }else{
833
        foreach($array as $name=>$value){
834
            if(is_array($value)){
835
                $return .= "";
836
                $return .= "['<b>$name</b>'] {<div style='margin-left:10px;'>\n";
837
                $return .= echo_array($value,true);
838
                $return .= "</div>}";
839
                $return .= "\n\n";
840
            }else{
841
                if(is_string($value)){
842
                    $value = "\"$value\"";
843
                }
844
                $return .= "['<b>$name</b>'] = $value\n\n";
845
            }
846
        }
847
    }
848
    if($return_me == true){
849
        return $return;
850
    }else{
851
        echo "<pre>".$return."</pre>";
852
    }
853
}
854

    
855
/****f* pfsense-utils/display_top_tabs
856
 * NAME
857
 *   display_top_tabs - display tabs with rounded edges
858
 * INPUTS
859
 *   $text      - array of tabs
860
 * RESULT
861
 *   null
862
 ******/
863
function display_top_tabs(& $tab_array) {
864
        global $HTTP_SERVER_VARS;
865
        global $config;
866
        global $g;
867

    
868
        /*  does the user have access to this tab?
869
         *  master user has access to everything.
870
         *  if the user does not have access, simply
871
         *  unset the tab item.
872
         */
873

    
874
        $tab_temp = array ();
875
        foreach ($tab_array as $ta)
876
                if(isAllowedPage($ta[2]))
877
                        $tab_temp[] = $ta;
878
        /*
879
                // FIXME :      if the checks are not good enough
880
                //                      in isAllowedPage, it needs to be
881
                //                      fixed instead of kludging here
882

    
883
                // TODO: humm what shall we do with pkg_edit.php and pkg.php?
884
                if ((strpos($link, "pkg.php")) !== false || (strpos($link, "pkg_edit.php")) !== false) {
885
                        $pos_equal = strpos($link, "=");
886
                        $pos_xmlsuffix = strpos($link, ".xml");
887
                        // do we match an absolute url including ?xml= foo
888
                        if(!isAllowedPage($link, $allowed))
889
                                $link = substr($link, $pos_equal +1, ($pos_xmlsuffix - $pos_equal +3));
890
                }
891
                // next check - what if the basename contains a query string?
892
                if ((strpos($link, "?")) !== false) {
893
                        $pos_qmark = strpos($link, "?");
894
                        $link = substr($link, 0, $pos_qmark);
895
                }
896
                $authorized_text = print_r($allowed, true);
897
                if(is_array($authorized))
898
                        if (in_array(basename($link), $authorized))
899
        */
900

    
901
        unset ($tab_array);
902
        $tab_array = & $tab_temp;
903

    
904
        $tab_active_bg   = "#EEEEEE";
905
        $tab_inactive_bg = "#777777";
906
        $nifty_tabs_corners = "#FFF";
907
        $font_color = "white";
908

    
909
        /* if tabcontrols.php exist for a theme, allow it to be overriden */
910
        $themename = $config['theme'];
911
        $filename = "/usr/local/www/themes/{$themename}/tabcontrols.php";
912
        if(file_exists($filename)) {
913
                $eval_code = file_get_contents($filename);
914
                eval($eval_code);
915
        }
916

    
917
        $tabcharcount = 0;
918
        foreach ($tab_array as $ta)
919
                $tabcharcount = $tabcharcount + strlen($ta[0]);
920

    
921
        // If the character count of the tab names is > 670
922
        // then show a select item dropdown menubox.
923
        if($tabcharcount > 82) {
924
                echo "Currently viewing: ";
925
                echo "<select name='TabSelect' onchange='tabs_will_go(this)'>\n";
926
                foreach ($tab_array as $ta) {
927
                        if($ta[1]=="true")
928
                                $selected = " SELECTED";
929
                        else
930
                                $selected = "";
931
                        // Onclick in option will not work in some browser
932
                        // echo "<option onClick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
933
                        echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
934
                }
935
                echo "</select>\n<p/>";
936
                echo "<script type=\"text/javascript\">";
937
                echo " function tabs_will_go(obj){ document.location = obj.value; }";
938
                echo "</script>";
939
        }  else {
940
                echo "<table cellpadding='0' cellspacing='0'>\n";
941
                echo " <tr>\n";
942
                $tabscounter = 0;
943
                foreach ($tab_array as $ta) {
944
                        if ($ta[1] == true) {
945
                                echo "  <td bgcolor='{$tab_active_bg}' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"><div id='tabactive'></div></td>\n";
946
                        } else {
947
                                echo "  <td bgcolor='{$tab_inactive_bg}' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"><div id='tabdeactive{$tabscounter}'></div></td>\n";
948
                        }
949
                        $tabscounter++;
950
                }
951
                echo "</tr>\n<tr>\n";
952
                foreach ($tab_array as $ta) {
953
                        if ($ta[1] == true) {
954
                                echo "  <td height=\"15\" valign=\"middle\" bgcolor='{$tab_active_bg}' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"><B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
955
                                echo "&nbsp;&nbsp;&nbsp;";
956
                                echo "<font size='-12'>&nbsp;</font></B></td>\n";
957
                        } else {
958
                                echo "  <td height=\"15\" valign=\"middle\" bgcolor='{$tab_inactive_bg}' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"><B>&nbsp;&nbsp;&nbsp;<a href='{$ta[
959
2]}'>";
960
                                echo "<font color='{$font_color}'>{$ta[0]}</font></a>&nbsp;&nbsp;&nbsp;";
961
                                echo "<font size='-12'>&nbsp;</font></B></td>\n";
962
                        }
963
                }
964
                echo "</tr>\n<tr>\n";
965
                foreach ($tab_array as $ta) {
966
                        if ($ta[1] == true) {
967
                                echo "  <td bgcolor='{$tab_active_bg}' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"></td>\n";
968
                        } else {
969
                                echo "  <td bgcolor='{$tab_inactive_bg}' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"></td>\n";
970
                        }
971
                        $tabscounter++;
972
                }
973
                echo " </tr>\n";
974
                echo "</table>\n";
975
                echo "<script type=\"text/javascript\">";
976
                echo "NiftyCheck();\n";
977
                echo "Rounded(\"div#tabactive\",\"top\",\"{$nifty_tabs_corners}\",\"{$tab_active_bg}\",\"smooth\");\n";
978
                for ($x = 0; $x < $tabscounter; $x++)
979
                        echo "Rounded(\"div#tabdeactive{$x}\",\"top\",\"{$nifty_tabs_corners}\",\"{$tab_inactive_bg}\",\"smooth\");\n";
980
                echo "</script>";
981
        }
982
}
983

    
984
function add_package_tabs($tabgroup, & $tab_array) {
985
        global $config, $g;
986

    
987
        if(!is_array($config['installedpackages']))
988
                return;
989
        if(!is_array($config['installedpackages']['tab']))
990
                return;
991

    
992
        foreach($config['installedpackages']['tab'] as $tab) {
993
                if ($tab['group'] !== $group)
994
                        continue;
995
                $tab_entry = array();
996
                if($tab['name']) {
997
                        $tab_entry[] = $tab['name'];
998
                        $tab_entry[] = false;
999
                        $tab_entry[] = $tab['url'];
1000
                        $tab_array[] = $tab_entry;
1001
                }
1002
        }
1003
}
1004

    
1005
function rule_popup($src,$srcport,$dst,$dstport){
1006
	global $config;
1007
	$aliases_array = array();
1008
	if($config['aliases']['alias'] <> "" and is_array($config['aliases']['alias']))
1009
	{
1010
		$span_begin = "";
1011
                $alias_src_span_begin = "";
1012
                $alias_src_span_end = "";
1013
                $alias_src_port_span_begin = "";
1014
                $alias_src_port_span_end = "";
1015
                $alias_dst_span_begin = "";
1016
                $alias_dst_span_end = "";
1017
                $alias_dst_port_span_begin = "";
1018
                $alias_dst_port_span_end = "";
1019
                $alias_content_text = "";
1020
        	foreach($config['aliases']['alias'] as $alias_name)
1021
        	{
1022
                	$alias_addresses = explode (" ", $alias_name['address']);
1023
                	$alias_details = explode ("||", $alias_name['detail']);
1024
                	$alias_objects_with_details = "";
1025
                	$counter = 0;
1026
			if ($alias_name['url']) {
1027
				$alias_objects_with_details .= $alias_name['url'] . "<br/>";
1028
			}
1029
                	foreach($alias_addresses as $alias_ports_address)
1030
                	{
1031
                        	$alias_objects_with_details .= $alias_addresses[$counter];
1032
                        	$alias_detail_default = strpos ($alias_details[$counter],"Entry added");
1033
                        	if ($alias_details[$counter] != "" && $alias_detail_default === False){
1034
                                	$alias_objects_with_details .=" - " . $alias_details[$counter];
1035
                        	}
1036
                        	$alias_objects_with_details .= "<br>";
1037
                        	$counter++;
1038
                	}
1039
                	//max character length for caption field
1040
                	$maxlength = 60;
1041

    
1042
                	$alias_descr_substr = $alias_name['descr'];
1043
                	$alias_content_text = htmlspecialchars($alias_objects_with_details);
1044
                	$alias_caption = htmlspecialchars($alias_descr_substr . ":");
1045
                	$strlength = strlen ($alias_caption);
1046
                	if ($strlength >= $maxlength)
1047
                        	$alias_caption = substr($alias_caption, 0, $maxlength) . "...";
1048

    
1049
			$alias_caption_escaped = str_replace("'", "\'", $alias_caption);
1050
			$span_begin = "<span style=\"cursor: help;\" onmouseover=\"domTT_activate(this, event, 'content', '<h1>$alias_caption_escaped</h1><p>$alias_content_text</p>', 'trail', true, 'delay', 0, 'fade', 'both', 'fadeMax', 93, 'styleClass', 'niceTitle');\" onmouseout=\"this.style.color = ''; domTT_mouseout(this, event);\"><U>";
1051

    
1052
                	if ($alias_name['name'] == $src)
1053
                        	$alias_src_span_begin = $span_begin;
1054
                	if ($alias_name['name'] == $srcport)
1055
                        	$alias_src_port_span_begin = $span_begin;
1056
                	if ($alias_name['name'] == $dst)
1057
                        	$alias_dst_span_begin = $span_begin;
1058
                	if ($alias_name['name'] == $dstport)
1059
                        	$alias_dst_port_span_begin = $span_begin;
1060
        	}
1061
        	$descriptions = array ();
1062
        	$descriptions['src'] = $alias_src_span_begin;
1063
        	$descriptions['srcport'] = $alias_src_port_span_begin;
1064
        	$descriptions['dst'] = $alias_dst_span_begin;
1065
        	$descriptions['dstport'] = $alias_dst_port_span_begin;
1066

    
1067
        	return $descriptions;
1068
  	}
1069
}
1070

    
1071
?>
(74-74/222)