Project

General

Profile

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

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

    
339
}
340

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

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

    
403
}
404

    
405
function print_info_box($msg) {
406
	print_info_box_np($msg);
407
}
408

    
409
function get_std_save_message($ok) {
410
	global $d_sysrebootreqd_path;
411

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

    
415
function pprint_address($adr) {
416
	global $specialnets;
417

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

    
430
	if (isset($adr['not']))
431
		$padr = "! " . $padr;
432

    
433
	return $padr;
434
}
435

    
436
function pprint_port($port) {
437
	global $wkports;
438

    
439
	$pport = "";
440

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

    
454
	return $pport;
455
}
456

    
457
/* sort by interface only, retain the original order of rules that apply to
458
   the same interface */
459
function filter_rules_sort() {
460
	global $config;
461

    
462
	/* mark each rule with the sequence number (to retain the order while sorting) */
463
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
464
		$config['filter']['rule'][$i]['seq'] = $i;
465

    
466
	function filtercmp($a, $b) {
467
		if ($a['interface'] == $b['interface'])
468
			return $a['seq'] - $b['seq'];
469
		else
470
			return -strcmp($a['interface'], $b['interface']);
471
	}
472

    
473
	usort($config['filter']['rule'], "filtercmp");
474

    
475
	/* strip the sequence numbers again */
476
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
477
		unset($config['filter']['rule'][$i]['seq']);
478
}
479

    
480
function gentitle($title) {
481
	global $navlevelsep;
482
	if(!is_array($title))
483
		return $title;
484
	else
485
		return join($navlevelsep, $title);
486
}
487

    
488
function genhtmltitle($title) {
489
        global $config;
490
        return gentitle($title);
491
}
492

    
493
/* update the changedesc and changecount(er) variables */
494
function update_changedesc($update) {
495
	global $changedesc;
496
	global $changecount;
497

    
498
	$changedesc .= " {$update}";
499
	$changecount++;
500
}
501

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

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

    
554
			} else {
555
					echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
556
			}
557
			echo "</tr>\n";
558
	}
559
}
560

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

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

    
603
	} else {
604
		if ($orig != $new) {
605
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
606
			$orig = $new;
607
			return true;
608
		}
609
	}
610
	return false;
611
}
612

    
613
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
614

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

    
625
        if (isset($adr['not']))
626
                $pnot = 1;
627
        else
628
                $pnot = 0;
629

    
630
        if ($adr['port']) {
631
                list($pbeginport, $pendport) = explode("-", $adr['port']);
632
                if (!$pendport)
633
                        $pendport = $pbeginport;
634
	} else if (!is_alias($pbeginport) && !is_alias($pendport)) {
635
		$pbeginport = "any";
636
		$pendport = "any";
637
        }
638
}
639

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

    
642
        $adr = array();
643

    
644
        if ($padr == "any")
645
                $adr['any'] = true;
646
        else if (is_specialnet($padr))
647
                $adr['network'] = $padr;
648
        else {
649
                $adr['address'] = $padr;
650
                if ($pmask != 32)
651
                        $adr['address'] .= "/" . $pmask;
652
        }
653

    
654
        if ($pnot)
655
                $adr['not'] = true;
656
        else
657
                unset($adr['not']);
658

    
659
        if (($pbeginport != 0) && ($pbeginport != "any")) {
660
                if ($pbeginport != $pendport)
661
                        $adr['port'] = $pbeginport . "-" . $pendport;
662
                else
663
                        $adr['port'] = $pbeginport;
664
        }
665

    
666
        if(is_alias($pbeginport)) {
667
                $adr['port'] = $pbeginport;
668
        }
669
}
670

    
671
function is_specialnet($net) {
672
        global $specialsrcdst;
673

    
674
	if(!$net) 
675
		return false;
676
        if (in_array($net, $specialsrcdst))
677
                return true;
678
        else
679
                return false;
680
}
681

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

    
718

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

    
731

    
732

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

    
743

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

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

    
798
function is_rfc2616_code($code) {
799
	global $rfc2616;
800
	if (isset($rfc2616[$code]))
801
		return true;
802
	else
803
		return false;
804
}
805

    
806
function print_rfc2616_select($tag, $current){
807
	global $rfc2616;
808

    
809
	/* Default to 200 OK if not set */
810
	if ($current == "")
811
		$current = 200;
812

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

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

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

    
864
        /*  does the user have access to this tab?
865
         *  master user has access to everything.
866
         *  if the user does not have access, simply
867
         *  unset the tab item.
868
         */
869

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

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

    
897
        unset ($tab_array);
898
        $tab_array = & $tab_temp;
899

    
900
        $tab_active_bg   = "#EEEEEE";
901
        $tab_inactive_bg = "#777777";
902
        $nifty_tabs_corners = "#FFF";
903
        $font_color = "white";
904

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

    
913
        $tabcharcount = 0;
914
        foreach ($tab_array as $ta)
915
                $tabcharcount = $tabcharcount + strlen($ta[0]);
916

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

    
980
function add_package_tabs($tabgroup, & $tab_array) {
981
        global $config, $g;
982

    
983
        if(!is_array($config['installedpackages']))
984
                return;
985
        if(!is_array($config['installedpackages']['tab']))
986
                return;
987

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

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

    
1038
                	$alias_descr_substr = $alias_name['descr'];
1039
                	$alias_content_text = htmlspecialchars($alias_objects_with_details);
1040
                	$alias_caption = htmlspecialchars($alias_descr_substr . ":");
1041
                	$strlength = strlen ($alias_caption);
1042
                	if ($strlength >= $maxlength)
1043
                        	$alias_caption = substr($alias_caption, 0, $maxlength) . "...";
1044

    
1045
			$alias_caption_escaped = str_replace("'", "\'", $alias_caption);
1046
			$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>";
1047

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

    
1063
        	return $descriptions;
1064
  	}
1065
}
1066

    
1067
?>
(74-74/222)