Project

General

Profile

Download (33.7 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
/* Include authentication routines */
34
/* THIS MUST BE ABOVE ALL OTHER CODE */
35
require_once("authgui.inc");
36

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

    
46
/* parse the configuration and include all configuration functions */
47
require_once("functions.inc");
48

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

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

    
73
/* used by progress bar */
74
$lastseen = "-1";
75

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

    
80
if (is_subsystem_dirty('firmwarelock')) {
81
	if (!$d_isfwfile) {
82
		header("Location: system_firmware.php");
83
		exit;
84
	} else {
85
		return;
86
	}
87
}
88

    
89
$auth_server_types = array(
90
	'ldap' => "LDAP",
91
	'radius' => "Radius");
92

    
93
$ldap_urltypes = array(
94
	'TCP - Standard' => 389,
95
	'SSL - Encrypted' => 636);
96

    
97
$ldap_scopes = array(
98
	'one' => "One Level",
99
	'subtree' => "Entire Subtree");
100

    
101
$ldap_protvers = array(
102
	2,
103
	3);
104

    
105
$ldap_templates = array(
106

    
107
	'open' => array(
108
				'desc' => "OpenLDAP",
109
				'attr_user' => "cn",
110
				'attr_group' => "cn",
111
				'attr_member' => "member"),
112

    
113
	'msad' => array(
114
				'desc' => "Microsoft AD",
115
				'attr_user' => "samAccountNAme",
116
				'attr_group' => "cn",
117
				'attr_member' => "member"),
118

    
119
	'edir' => array(
120
				'desc' => "Novell eDirectory",
121
				'attr_user' => "cn",
122
				'attr_group' => "cn",
123
				'attr_member' => "uniqueMember"));
124

    
125
$radius_srvcs = array(
126
	'both' => "Authentication and Accounting",
127
	'auth' => "Authentication",
128
	'acct' => "Accounting");
129

    
130
$netbios_nodetypes = array(
131
	'0' => "none",
132
	'1' => "b-node",
133
	'2' => "p-node",
134
	'4' => "m-node",
135
	'5' => "h-node");
136

    
137
/* some well knows ports */
138
$wkports = array(
139
	5999 => "CVSup",	
140
	53 => "DNS",
141
	21 => "FTP",
142
	3000 => "HBCI",
143
	80 => "HTTP",
144
	443 => "HTTPS",
145
	5190 => "ICQ",
146
	113 => "IDENT/AUTH",
147
	143 => "IMAP",
148
	993 => "IMAP/S",
149
	4500 => "IPsec NAT-T",
150
	500 => "ISAKMP",
151
	1701 => "L2TP",
152
	389 => "LDAP",
153
	1755 => "MMS/TCP",
154
	7000 => "MMS/UDP",
155
	445 => "MS DS",
156
	3389 => "MS RDP",
157
	1512 => "MS WINS",
158
	1863 => "MSN",
159
	119 => "NNTP",
160
	123 => "NTP",
161
	138 => "NetBIOS-DGM",
162
	137 => "NetBIOS-NS",
163
	139 => "NetBIOS-SSN",
164
	1194 => "OpenVPN",
165
	110 => "POP3",
166
	995 => "POP3/S",
167
	1723 => "PPTP",	
168
	1812 => "RADIUS",
169
	1813 => "RADIUS accounting",
170
	5004 => "RTP",
171
	5060 => "SIP",
172
	25 => "SMTP",
173
	465 => "SMTP/S",
174
	161 => "SNMP",
175
	162 => "SNMP-Trap",
176
	22 => "SSH",
177
	3478 => "STUN",
178
	23 => "Telnet",
179
	69 => "TFTP",
180
	5900 => "VNC");
181

    
182
$specialnets = array("wanip" => "WAN address", "lanip" => "LAN address", "lan" => "LAN net", "pptp" => "PPTP clients");
183

    
184
$spiflist = get_configured_interface_with_descr(true, true);
185
foreach ($spiflist as $ifgui => $ifdesc) {
186
	$specialnets[$ifgui] = $ifdesc . " net";
187
}
188

    
189
$medias = array("auto" => "autoselect", "100full" => "100BASE-TX full-duplex",
190
	"100half" => "100BASE-TX half-duplex", "10full" => "10BASE-T full-duplex",
191
	"10half" => "10BASE-T half-duplex");
192

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

    
196
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
197

    
198
	/* check for bad control characters */
199
	foreach ($postdata as $pn => $pd) {
200
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
201
			$input_errors[] = "The field '" . $pn . "' contains invalid characters.";
202
		}
203
	}
204

    
205
	for ($i = 0; $i < count($reqdfields); $i++) {
206
		if ($_POST[$reqdfields[$i]] == "") {
207
			$input_errors[] = "The field '" . $reqdfieldsn[$i] . "' is required.";
208
		}
209
	}
210
}
211

    
212
function print_input_errors($input_errors) {
213
	global $g;
214

    
215
	print <<<EOF
216
	<p>
217
	<table border="0" cellspacing="0" cellpadding="4" width="100%">
218
	<tr>
219
		<td class="inputerrorsleft">
220
			<img src="./themes/{$g['theme']}/images/icons/icon_error.gif">
221
		</td>
222
		<td class="inputerrorsright">
223
			<span class="errmsg"><p>
224
				The following input errors were detected:
225
				<ul>
226
EOF;
227
		foreach ($input_errors as $ierr) {
228
			echo "<li>" . htmlspecialchars($ierr) . "</li>";
229
		}
230

    
231
	print <<<EOF2
232
				</ul>
233
			</span>
234
		</td></tr>
235
	</table>
236
	</p>&nbsp;<br>
237
EOF2;
238
	
239
}
240

    
241
function verify_gzip_file($fname) {
242

    
243
    $returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
244
	if ($returnvar != 0)
245
		return 0;
246
	else
247
		return 1;
248
}
249

    
250
function print_info_box_np($msg, $name="apply",$value="Apply changes") {
251
	global $g;
252
	
253
	if(stristr($msg, "apply") == true || stristr($msg, "save") || stristr($msg, "create")) {
254
		$savebutton = "<td class='infoboxsave'>";
255
		$savebutton .= "<input name=\"{$name}\" type=\"submit\" class=\"formbtn\" id=\"${name}\" value=\"{$value}\">";
256
		if($_POST['if']) 
257
			$savebutton .= "<input type='hidden' name='if' value='{$_POST['if']}'>";
258
		$savebutton.="</td>";
259
	}
260
	$nifty_redbox = "#990000";
261
	$nifty_blackbox = "#000000";
262
	
263
	$themename = $g['theme'];
264
	
265
	if(file_exists("/usr/local/www/themes/{$themename}/tabcontrols.php")) {
266
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/tabcontrols.php");
267
		eval($toeval);
268
	}
269
	
270
	if(file_exists("/usr/local/www/themes/{$themename}/infobox.php")) {
271
		$toeval = file_get_contents("/usr/local/www/themes/{$themename}/infobox.php");
272
		eval($toeval);
273
	}	
274
		
275
	echo <<<EOFnp
276
	<table class='infobox'>
277
		<tr>
278
			<td>
279
				<div class='infoboxnp' id='redbox'>
280
					<table class='infoboxnptable2'>
281
						<tr>
282
							<td class='infoboxnptd'>
283
								&nbsp;&nbsp;&nbsp;<img class='infoboxnpimg' src="/themes/{$g['theme']}/images/icons/icon_exclam.gif" >
284
							</td>
285
							<td class='infoboxnptd2'>
286
								<b>{$msg}</b>
287
							</td>
288
							{$savebutton}
289
						</tr>
290
					</table>
291
				</div>
292
			</td>
293
		</tr>
294
	</table>
295
	<script type="text/javascript">
296
		NiftyCheck();
297
		Rounded("div#redbox","all","#FFF","{$nifty_redbox}","smooth");
298
		Rounded("td#blackbox","all","#FFF","{$nifty_blackbox}","smooth");
299
	</script>
300
	<br/>
301
EOFnp;
302

    
303
}
304

    
305
function print_info_box($msg) {
306
	echo "<p>";
307
	print_info_box_np($msg);
308
	echo "</p>";
309
}
310

    
311
function get_std_save_message($ok) {
312
	global $d_sysrebootreqd_path;
313

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

    
317
function pprint_address($adr) {
318
	global $specialnets;
319

    
320
	if (isset($adr['any'])) {
321
		$padr = "*";
322
	} else if ($adr['network']) {
323
		if (preg_match("/opt[0-999]ip/", $adr['network'])) {
324
			$padr = "Interface IP address";
325
		} else {
326
			$padr = $specialnets[$adr['network']];
327
		}
328
	} else {
329
		$padr = $adr['address'];
330
	}
331

    
332
	if (isset($adr['not']))
333
		$padr = "! " . $padr;
334

    
335
	return $padr;
336
}
337

    
338
function pprint_port($port) {
339
	global $wkports;
340

    
341
	$pport = "";
342

    
343
	if (!$port)
344
		return "*";
345
	else {
346
		$srcport = explode("-", $port);
347
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
348
			$pport = $srcport[0];
349
			if ($wkports[$srcport[0]]) {
350
				$pport .= " (" . $wkports[$srcport[0]] . ")";
351
			}
352
		} else
353
			$pport .= $srcport[0] . " - " . $srcport[1];
354
	}
355

    
356
	return $pport;
357
}
358

    
359
/* sort by interface only, retain the original order of rules that apply to
360
   the same interface */
361
function filter_rules_sort() {
362
	global $config;
363

    
364
	/* mark each rule with the sequence number (to retain the order while sorting) */
365
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
366
		$config['filter']['rule'][$i]['seq'] = $i;
367

    
368
	function filtercmp($a, $b) {
369
		if ($a['interface'] == $b['interface'])
370
			return $a['seq'] - $b['seq'];
371
		else
372
			return -strcmp($a['interface'], $b['interface']);
373
	}
374

    
375
	usort($config['filter']['rule'], "filtercmp");
376

    
377
	/* strip the sequence numbers again */
378
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
379
		unset($config['filter']['rule'][$i]['seq']);
380
}
381

    
382
function aliases_sort() {
383
	global $g, $config;
384

    
385
	function aliascmp($a, $b) {
386
		return strcmp($a['name'], $b['name']);
387
	}
388

    
389
	if($config['aliases'])
390
		usort($config['aliases']['alias'], "aliascmp");
391
}
392

    
393
function gentitle($title) {
394
	global $navlevelsep;
395
	if(!is_array($title))
396
		return $title;
397
	else
398
		return join($navlevelsep, $title); 
399
}
400

    
401
function genhtmltitle($title) {
402
        global $config;
403
        return gentitle($title);
404
}
405

    
406
/* update the changedesc and changecount(er) variables */
407
function update_changedesc($update) {
408
	global $changedesc;
409
	global $changecount;
410

    
411
	$changedesc .= " {$update}";
412
	$changecount++;
413
}
414

    
415
function clear_log_file($logfile = "/var/log/system.log") {
416
	global $config, $g;
417
	exec("/usr/bin/killall syslogd");
418
	if(isset($config['system']['disablesyslogclog'])) {
419
		unlink($logfile);
420
		touch($logfile);
421
	} else {
422
		if(isset($config['system']['usefifolog'])) 
423
			exec("/usr/sbin/fifolog_create -s 511488 {$logfile}");
424
		else
425
			exec("/usr/sbin/clog -i -s 511488 {$logfile}");
426
	}
427
	system_syslogd_start();	
428
}
429

    
430
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
431
	global $g, $config;
432
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
433
	$logarr = "";
434
	$grepline = "  ";
435
	if(is_array($grepfor))
436
		foreach($grepfor as $agrep)
437
			$grepline .= " | grep \"$agrep\"";
438
	if(is_array($grepinvert))
439
		foreach($grepinvert as $agrep)
440
			$grepline .= " | grep -v \"$agrep\"";
441
	if(file_exists($logfile) && filesize($logfile) == 0) {
442
		$logarr = array("Log file started.");
443
	} else {
444
		if($config['system']['disablesyslogclog']) {
445
			exec("cat {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
446
		} else {
447
			if(isset($config['system']['usefifolog']))
448
				exec("/usr/sbin/fifolog_reader {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
449
			else
450
				exec("/usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
451
		}
452
	}
453
	foreach ($logarr as $logent) {
454
			$logent = preg_split("/\s+/", $logent, 6);
455
			echo "<tr valign=\"top\">\n";
456
			if ($withorig) {
457
					if(isset($config['system']['usefifolog'])) {
458
						$entry_date_time = htmlspecialchars(date("F j, Y, g:i a","" . $logent[1] . ""));
459
						$entry_text = htmlspecialchars($logent[5]);
460
					} else {
461
						$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
462
						$entry_text = htmlspecialchars($logent[4] . " " . $logent[5]);
463
					}
464
					echo "<td class=\"listlr\" nowrap>{$entry_date_time}</td>\n";
465
					echo "<td class=\"listr\">{$entry_text}</td>\n";
466

    
467
			} else {
468
					echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logent[5]) . "</td>\n";
469
			}
470
			echo "</tr>\n";
471
	}
472
}
473

    
474
function return_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
475
	global $g, $config;
476
	$sor = (isset($config['syslog']['reverse']) || $grepreverse) ? "-r" : "";
477
	$logarr = "";
478
	$grepline = "  ";
479
	if(is_array($grepfor))
480
		foreach($grepfor as $agrep)
481
			$grepline .= " | grep \"$agrep\"";
482
	if(is_array($grepinvert))
483
		foreach($grepinvert as $agrep)
484
			$grepline .= " | grep -v \"$agrep\"";
485
	if($config['system']['disablesyslogclog']) {
486
		exec("cat {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);
487
	} else {
488
		if(isset($config['system']['usefifolog'])) {
489
			exec("/usr/sbin/fifolog_reader {$logfile}{$grepline} | /usr/bin/tail {$sor} -n {$tail}", $logarr);			
490
		} else {
491
			exec("/usr/sbin/clog {$logfile}{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n {$tail}", $logarr);
492
		}
493
	}
494
	return($logarr);
495
}
496

    
497
/* Check if variable has changed, update and log if it has
498
 * returns true if var changed
499
 * varname = variable name in plain text
500
 * orig = original value
501
 * new = new value
502
 */
503
function update_if_changed($varname, & $orig, $new) {
504
	if (is_array($orig) && is_array($new)) {
505
		$a_diff = array_diff($orig, $new);
506
		foreach ($a_diff as $diff) {
507
			update_changedesc("removed {$varname}: \"{$diff}\"");
508
		}
509
		$a_diff = array_diff($new, $orig);
510
		foreach ($a_diff as $diff) {
511
			update_changedesc("added {$varname}: \"{$diff}\"");
512
		}
513
		$orig = $new;
514
		return true;
515

    
516
	} else {
517
		if ($orig != $new) {
518
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
519
			$orig = $new;
520
			return true;
521
		}
522
	}
523
	return false;
524
}
525

    
526
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
527

    
528
        if (isset($adr['any']))
529
                $padr = "any";
530
        else if ($adr['network'])
531
                $padr = $adr['network'];
532
        else if ($adr['address']) {
533
                list($padr, $pmask) = explode("/", $adr['address']);
534
                if (!$pmask)
535
                        $pmask = 32;
536
        }
537

    
538
        if (isset($adr['not']))
539
                $pnot = 1;
540
        else
541
                $pnot = 0;
542

    
543
        if ($adr['port']) {
544
                list($pbeginport, $pendport) = explode("-", $adr['port']);
545
                if (!$pendport)
546
                        $pendport = $pbeginport;
547
        } else {
548
                if(alias_expand($pbeginport) <> "" || alias_expand($pendport) <> "") {
549
                        /* Item is a port alias */
550
                } else {
551
                        $pbeginport = "any";
552
                        $pendport = "any";
553
                }
554
        }
555
}
556

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

    
559
        $adr = array();
560

    
561
        if ($padr == "any")
562
                $adr['any'] = true;
563
        else if (is_specialnet($padr))
564
                $adr['network'] = $padr;
565
        else {
566
                $adr['address'] = $padr;
567
                if ($pmask != 32)
568
                        $adr['address'] .= "/" . $pmask;
569
        }
570

    
571
        if ($pnot)
572
                $adr['not'] = true;
573
        else
574
                unset($adr['not']);
575

    
576
        if (($pbeginport != 0) && ($pbeginport != "any")) {
577
                if ($pbeginport != $pendport)
578
                        $adr['port'] = $pbeginport . "-" . $pendport;
579
                else
580
                        $adr['port'] = $pbeginport;
581
        }
582

    
583
        if(alias_expand($pbeginport)) {
584
                $adr['port'] = $pbeginport;
585
        }
586
}
587

    
588
function is_specialnet($net) {
589
        global $specialsrcdst;
590
		if(!$net) 
591
			return false;
592
        if (in_array($net, $specialsrcdst) || strstr($net, "opt"))
593
                return true;
594
        else
595
                return false;
596
}
597

    
598
//function to create widget tabs when called
599
function display_widget_tabs(& $tab_array) {	
600
	echo "<div id='tabs'>";
601
	$tabscounter = 0;
602
	foreach ($tab_array as $ta) {
603
	$dashpos = strpos($ta[2],'-');
604
	$tabname = $ta[2] . "-tab";
605
	$tabclass = substr($ta[2],0,$dashpos);
606
	$tabclass = $tabclass . "-class";
607
		if ($ta[1] == true) {
608
			$tabActive = "table-cell";
609
			$tabNonActive = "none";
610
		} 
611
		else {
612
			$tabActive = "none";
613
			$tabNonActive = "table-cell";
614
		}
615
		echo "<div id='{$ta[2]}-active' class='{$tabclass}-tabactive' style='display:{$tabActive}; background-color:#EEEEEE; color:black;'>";
616
		echo "<B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
617
		echo "&nbsp;&nbsp;&nbsp;</B>";
618
		echo "</div>";
619
		
620
		echo "<div id='{$ta[2]}-deactive' class='{$tabclass}-tabdeactive' style='display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;' onClick=\"return changeTabDIV('{$ta[2]}')\">";
621
		echo "<B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
622
		echo "&nbsp;&nbsp;&nbsp;</B>";
623
		echo "</div>";
624
	}
625
	
626
	echo "<script type=\"text/javascript\">";
627
	echo "NiftyCheck();\n";
628
	echo "Rounded(\"div.{$tabclass}-tabactive\",\"top\",\"#CCCCCC\",\"#EEEEEE\",\"smooth\");\n";
629
	echo "Rounded(\"div.{$tabclass}-tabdeactive\",\"top\",\"#CCCCCC\",\"#777777\",\"smooth\");\n";
630
	echo "</script>";
631
	echo "</div>";
632
}
633

    
634

    
635
// Return inline javascript file or CSS to minimizie 
636
// request count going back to server.
637
function outputJavaScriptFileInline($javascript) {
638
	if(file_exists($javascript)) {
639
		echo "\n<script type=\"text/javascript\">\n";
640
		include($javascript);
641
		echo "\n</script>\n";
642
	} else {
643
		echo "\n\n<!-- Could not location file:  {$javascript} -->\n\n";
644
	}
645
}
646

    
647

    
648

    
649
function outputCSSPrintFileInline($css) {
650
	if(file_exists($css)) {
651
		echo "\n<style media=\"print\" type=\"text/css\">\n";
652
		include($css);
653
		echo "\n</style>\n";
654
	} else {
655
		echo "\n\n<!-- Could not location file:  {$css} -->\n\n";
656
	}
657
}
658

    
659

    
660
function outputCSSFileInline($css) {
661
	if(file_exists($css)) {
662
		echo "\n<style type=\"text/css\">\n";
663
		include($css);
664
		echo "\n</style>\n";
665
	} else {
666
		echo "\n\n<!-- Could not location file:  {$css} -->\n\n";
667
	}
668
}
669

    
670
$rfc2616 = array(
671
	100 => "100 Continue",
672
	101 => "101 Switching Protocols",
673
	200 => "200 OK",
674
	201 => "201 Created",
675
	202 => "202 Accepted",
676
	203 => "203 Non-Authoritative Information",
677
	204 => "204 No Content",
678
	205 => "205 Reset Content",
679
	206 => "206 Partial Content",
680
	300 => "300 Multiple Choices",
681
	301 => "301 Moved Permanently",
682
	302 => "302 Found",
683
	303 => "303 See Other",
684
	304 => "304 Not Modified",
685
	305 => "305 Use Proxy",
686
	306 => "306 (Unused)",
687
	307 => "307 Temporary Redirect",
688
	400 => "400 Bad Request",
689
	401 => "401 Unauthorized",
690
	402 => "402 Payment Required",
691
	403 => "403 Forbidden",
692
	404 => "404 Not Found",
693
	405 => "405 Method Not Allowed",
694
	406 => "406 Not Acceptable",
695
	407 => "407 Proxy Authentication Required",
696
	408 => "408 Request Timeout",
697
	409 => "409 Conflict",
698
	410 => "410 Gone",
699
	411 => "411 Length Required",
700
	412 => "412 Precondition Failed",
701
	413 => "413 Request Entity Too Large",
702
	414 => "414 Request-URI Too Long",
703
	415 => "415 Unsupported Media Type",
704
	416 => "416 Requested Range Not Satisfiable",
705
	417 => "417 Expectation Failed",
706
	500 => "500 Internal Server Error",
707
	501 => "501 Not Implemented",
708
	502 => "502 Bad Gateway",
709
	503 => "503 Service Unavailable",
710
	504 => "504 Gateway Timeout",
711
	505 => "505 HTTP Version Not Supported"
712
);
713

    
714
function is_rfc2616_code($code) {
715
	global $rfc2616;
716
	if (isset($rfc2616[$code]))
717
		return true;
718
	else
719
		return false;
720
}
721

    
722
function print_rfc2616_select($tag, $current){
723
	global $rfc2616;
724

    
725
	/* Default to 200 OK if not set */
726
	if ($current == "")
727
		$current = 200;
728

    
729
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";	
730
	foreach($rfc2616 as $code => $message) {
731
		if ($code == $current) {
732
			$sel = " selected";
733
		} else {
734
			$sel = "";
735
		}
736
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
737
	}
738
}
739

    
740
// Useful debugging function, much cleaner than print_r
741
function echo_array($array,$return_me=false){
742
    if(is_array($array) == false){
743
        $return = "The provided variable is not an array.";
744
    }else{
745
        foreach($array as $name=>$value){
746
            if(is_array($value)){
747
                $return .= "";
748
                $return .= "['<b>$name</b>'] {<div style='margin-left:10px;'>\n";
749
                $return .= echo_array($value,true);
750
                $return .= "</div>}";
751
                $return .= "\n\n";
752
            }else{
753
                if(is_string($value)){
754
                    $value = "\"$value\"";
755
                }
756
                $return .= "['<b>$name</b>'] = $value\n\n";
757
            }
758
        }
759
    }
760
    if($return_me == true){
761
        return $return;
762
    }else{
763
        echo "<pre>".$return."</pre>";
764
    }
765
}
766

    
767
/****f* pfsense-utils/display_top_tabs
768
 * NAME
769
 *   display_top_tabs - display tabs with rounded edges
770
 * INPUTS
771
 *   $text      - array of tabs
772
 * RESULT
773
 *   null
774
 ******/
775
function display_top_tabs(& $tab_array) {
776
        global $HTTP_SERVER_VARS;
777
        global $config;
778
        global $g;
779

    
780
        /*  does the user have access to this tab?
781
         *  master user has access to everything.
782
         *  if the user does not have access, simply
783
         *  unset the tab item.
784
         */
785

    
786
        $tab_temp = array ();
787
        foreach ($tab_array as $ta)
788
                if(isAllowedPage($ta[2]))
789
                        $tab_temp[] = $ta;
790
        /*
791
                // FIXME :      if the checks are not good enough
792
                //                      in isAllowedPage, it needs to be
793
                //                      fixed instead of kludging here
794

    
795
                // TODO: humm what shall we do with pkg_edit.php and pkg.php?
796
                if ((strpos($link, "pkg.php")) !== false || (strpos($link, "pkg_edit.php")) !== false) {
797
                        $pos_equal = strpos($link, "=");
798
                        $pos_xmlsuffix = strpos($link, ".xml");
799
                        // do we match an absolute url including ?xml= foo
800
                        if(!isAllowedPage($link, $allowed))
801
                                $link = substr($link, $pos_equal +1, ($pos_xmlsuffix - $pos_equal +3));
802
                }
803
                // next check - what if the basename contains a query string?
804
                if ((strpos($link, "?")) !== false) {
805
                        $pos_qmark = strpos($link, "?");
806
                        $link = substr($link, 0, $pos_qmark);
807
                }
808
                $authorized_text = print_r($allowed, true);
809
                if(is_array($authorized))
810
                        if (in_array(basename($link), $authorized))
811
        */
812

    
813
        unset ($tab_array);
814
        $tab_array = & $tab_temp;
815

    
816
        $tab_active_bg   = "#EEEEEE";
817
        $tab_inactive_bg = "#777777";
818
        $nifty_tabs_corners = "#FFF";
819
        $font_color = "white";
820

    
821
        /* if tabcontrols.php exist for a theme, allow it to be overriden */
822
        $themename = $config['theme'];
823
        $filename = "/usr/local/www/themes/{$themename}/tabcontrols.php";
824
        if(file_exists($filename)) {
825
                $eval_code = file_get_contents($filename);
826
                eval($eval_code);
827
        }
828

    
829
        $tabcharcount = 0;
830
        foreach ($tab_array as $ta)
831
                $tabcharcount = $tabcharcount + strlen($ta[0]);
832

    
833
        // If the character count of the tab names is > 670
834
        // then show a select item dropdown menubox.
835
        if($tabcharcount > 82) {
836
                echo "Currently viewing: ";
837
                echo "<select name='TabSelect'>\n";
838
                foreach ($tab_array as $ta) {
839
                        if($ta[1]=="true")
840
                                $selected = " SELECTED";
841
                        else
842
                                $selected = "";
843
                        echo "<option onClick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
844
                }
845
                echo "</select>\n<p/>";
846
        }  else {
847
                echo "<table cellpadding='0' cellspacing='0'>\n";
848
                echo " <tr>\n";
849
                $tabscounter = 0;
850
                foreach ($tab_array as $ta) {
851
                        if ($ta[1] == true) {
852
                                echo "  <td bgcolor='{$tab_active_bg}' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"><div id='tabactive'></div></td>\n";
853
                        } else {
854
                                echo "  <td bgcolor='{$tab_inactive_bg}' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"><div id='tabdeactive{$tabscounter}'></div></td>\n";
855
                        }
856
                        $tabscounter++;
857
                }
858
                echo "</tr>\n<tr>\n";
859
                foreach ($tab_array as $ta) {
860
                        if ($ta[1] == true) {
861
                                echo "  <td height=\"15\" valign=\"middle\" bgcolor='{$tab_active_bg}' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"><B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
862
                                echo "&nbsp;&nbsp;&nbsp;";
863
                                echo "<font size='-12'>&nbsp;</font></B></td>\n";
864
                        } else {
865
                                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[
866
2]}'>";
867
                                echo "<font color='{$font_color}'>{$ta[0]}</font></a>&nbsp;&nbsp;&nbsp;";
868
                                echo "<font size='-12'>&nbsp;</font></B></td>\n";
869
                        }
870
                }
871
                echo "</tr>\n<tr>\n";
872
                foreach ($tab_array as $ta) {
873
                        if ($ta[1] == true) {
874
                                echo "  <td bgcolor='{$tab_active_bg}' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"></td>\n";
875
                        } else {
876
                                echo "  <td bgcolor='{$tab_inactive_bg}' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"></td>\n";
877
                        }
878
                        $tabscounter++;
879
                }
880
                echo " </tr>\n";
881
                echo "</table>\n";
882
                echo "<script type=\"text/javascript\">";
883
                echo "NiftyCheck();\n";
884
                echo "Rounded(\"div#tabactive\",\"top\",\"{$nifty_tabs_corners}\",\"{$tab_active_bg}\",\"smooth\");\n";
885
                for ($x = 0; $x < $tabscounter; $x++)
886
                        echo "Rounded(\"div#tabdeactive{$x}\",\"top\",\"{$nifty_tabs_corners}\",\"{$tab_inactive_bg}\",\"smooth\");\n";
887
                echo "</script>";
888
        }
889
}
890

    
891
function add_package_tabs($tabgroup, & $tab_array) {
892
        global $config, $g;
893

    
894
        if(!is_array($config['installedpackages']))
895
                return;
896
        if(!is_array($config['installedpackages']['tab']))
897
                return;
898

    
899
        foreach($config['installedpackages']['tab'] as $tab) {
900
                if ($tab['group'] !== $group)
901
                        continue;
902
                $tab_entry = array();
903
                if($tab['name']) {
904
                        $tab_entry[] = $tab['name'];
905
                        $tab_entry[] = false;
906
                        $tab_entry[] = $tab['url'];
907
                        $tab_array[] = $tab_entry;
908
                }
909
        }
910
}
911

    
912
/*
913
 *   update_output_window: update bottom textarea dynamically.
914
 */
915
function update_output_window($text) {
916
        global $pkg_interface;
917
        $log = ereg_replace("\n", "\\n", $text);
918
        if($pkg_interface == "console") {
919
                /* too chatty */
920
        } else {
921
                echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
922
        }
923
        /* ensure that contents are written out */
924
        ob_flush();
925
}
926

    
927
/*
928
 *   update_output_window: update top textarea dynamically.
929
 */
930
function update_status($status) {
931
        global $pkg_interface;
932
        if($pkg_interface == "console") {
933
                echo $status . "\n";
934
        } else {
935
                echo "\n<script type=\"text/javascript\">this.document.forms[0].status.value=\"" . $status . "\";</script>";
936
        }
937
        /* ensure that contents are written out */
938
        ob_flush();
939
}
940

    
941
/*
942
 * update_progress_bar($percent): updates the javascript driven progress bar.
943
 */
944
function update_progress_bar($percent) {
945
        global $pkg_interface;
946
        if($percent > 100) $percent = 1;
947
        if($pkg_interface <> "console") {
948
                echo "\n<script type=\"text/javascript\" language=\"javascript\">";
949
                echo "\ndocument.progressbar.style.width='" . $percent . "%';";
950
                echo "\n</script>";
951
        } else {
952
                echo " {$percent}%";
953
        }
954
}
955

    
956
function read_header($ch, $string) {
957
        global $file_size, $fout;
958
        $length = strlen($string);
959
        $regs = "";
960
        ereg("(Content-Length:) (.*)", $string, $regs);
961
        if($regs[2] <> "") {
962
                $file_size = intval($regs[2]);
963
        }
964
        ob_flush();
965
        return $length;
966
}
967

    
968
function read_body($ch, $string) {
969
        global $fout, $file_size, $downloaded, $sendto, $static_status, $static_output, $lastseen;
970
        $length = strlen($string);
971
        $downloaded += intval($length);
972
        $downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
973
        $downloadProgress = 100 - $downloadProgress;
974
        if($lastseen <> $downloadProgress and $downloadProgress < 101) {
975
                if($sendto == "status") {
976
                        $tostatus = $static_status . $downloadProgress . "%";
977
                        update_status($tostatus);
978
                } else {
979
                        $tooutput = $static_output . $downloadProgress . "%";
980
                        update_output_window($tooutput);
981
                }
982
                update_progress_bar($downloadProgress);
983
                $lastseen = $downloadProgress;
984
        }
985
        if($fout)
986
                fwrite($fout, $string);
987
        ob_flush();
988
        return $length;
989
}
990

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

    
1025
                	$alias_descr_substr = $alias_name['descr'];
1026
                	$alias_content_text = htmlspecialchars($alias_objects_with_details);
1027
                	$alias_caption = htmlspecialchars($alias_descr_substr . ":");
1028
                	$strlength = strlen ($alias_caption);
1029
                	if ($strlength >= $maxlength)
1030
                        	$alias_caption = substr($alias_caption, 0, $maxlength) . "...";
1031

    
1032
                	$span_begin = "<span style=\"cursor: help;\" onmouseover=\"domTT_activate(this, event, 'content', '<h1>$alias_caption</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>";
1033

    
1034
                	if ($alias_name['name'] == $src)
1035
                        	$alias_src_span_begin = $span_begin;
1036
                	if ($alias_name['name'] == $srcport)
1037
                        	$alias_src_port_span_begin = $span_begin;
1038
                	if ($alias_name['name'] == $dst)
1039
                        	$alias_dst_span_begin = $span_begin;
1040
                	if ($alias_name['name'] == $dstport)
1041
                        	$alias_dst_port_span_begin = $span_begin;
1042
        	}
1043
        	$descriptions = array ();
1044
        	$descriptions['src'] = $alias_src_span_begin;
1045
        	$descriptions['srcport'] = $alias_src_port_span_begin;
1046
        	$descriptions['dst'] = $alias_dst_span_begin;
1047
        	$descriptions['dstport'] = $alias_dst_port_span_begin;
1048

    
1049
        	return $descriptions;
1050
  	}
1051
}
1052

    
1053
function download_file_with_progress_bar($url_file, $destination_file, $readbody = 'read_body') {
1054
        global $ch, $fout, $file_size, $downloaded;
1055
        $file_size  = 1;
1056
        $downloaded = 1;
1057
        /* open destination file */
1058
        $fout = fopen($destination_file, "wb");
1059

    
1060
        /*
1061
         *      Originally by Author: Keyvan Minoukadeh
1062
         *      Modified by Scott Ullrich to return Content-Length size
1063
         */
1064

    
1065
        $ch = curl_init();
1066
        curl_setopt($ch, CURLOPT_URL, $url_file);
1067
        curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
1068
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, $readbody);
1069
        curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
1070
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, '5');
1071
        curl_setopt($ch, CURLOPT_TIMEOUT, 0);
1072

    
1073
        curl_exec($ch);
1074
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
1075
        if($fout)
1076
                fclose($fout);
1077
        curl_close($ch);
1078
        return ($http_code == 200) ? true : $http_code;
1079
}
1080

    
1081
?>
(73-73/217)