Project

General

Profile

Download (33.2 KB) Statistics
| Branch: | Tag: | Revision:
1 667b2b60 Ermal
<?php
2
/*
3 c5d81585 Renato Botelho
 * guiconfig.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 81299b5c Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 c5d81585 Renato Botelho
 * All rights reserved.
8
 *
9
 * originally based on m0n0wall (http://m0n0.ch/wall)
10
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
 * All rights reserved.
12
 *
13 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
14
 * you may not use this file except in compliance with the License.
15
 * You may obtain a copy of the License at
16 c5d81585 Renato Botelho
 *
17 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
18 c5d81585 Renato Botelho
 *
19 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
20
 * distributed under the License is distributed on an "AS IS" BASIS,
21
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
 * See the License for the specific language governing permissions and
23
 * limitations under the License.
24 fd9ebcd5 Stephen Beaver
 */
25 667b2b60 Ermal
26
/* Include authentication routines */
27
/* THIS MUST BE ABOVE ALL OTHER CODE */
28 45b4ffc6 Phil Davis
if (!$nocsrf) {
29 64ec1ddf Scott Ullrich
	function csrf_startup() {
30 efa9174a jim-p
		global $config;
31 64ec1ddf Scott Ullrich
		csrf_conf('rewrite-js', '/csrf/csrf-magic.js');
32 6c07db48 Phil Davis
		$timeout_minutes = isset($config['system']['webgui']['session_timeout']) ? $config['system']['webgui']['session_timeout'] : 240;
33 56befec1 jim-p
		csrf_conf('expires', $timeout_minutes * 60);
34 64ec1ddf Scott Ullrich
	}
35
	require_once("csrf/csrf-magic.php");
36 fafd303e Scott Ullrich
}
37 667b2b60 Ermal
38
/* make sure nothing is cached */
39
if (!$omit_nocacheheaders) {
40
	header("Expires: 0");
41
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
42 2ebbb0bc Jose Luis Duran
	header("Cache-Control: no-cache, no-store, must-revalidate");
43 667b2b60 Ermal
	header("Pragma: no-cache");
44
}
45
46 6f3d2063 Renato Botelho
header("X-Frame-Options: SAMEORIGIN");
47 9c59f962 Ermal
require_once("authgui.inc");
48
49 667b2b60 Ermal
/* parse the configuration and include all configuration functions */
50
require_once("functions.inc");
51
52 02cc81ef Doug Wollison
/* Include the autoloader for all the GUI display classes */
53
require_once("classes/autoload.inc.php");
54 667b2b60 Ermal
55
/* used by progress bar */
56
$lastseen = "-1";
57
58
$navlevelsep = ": ";	/* navigation level separator string */
59
$mandfldhtml = "";		/* display this before mandatory input fields */
60
$mandfldhtmlspc = "";	/* same as above, but with spacing */
61
62 55cbefff Stephen Beaver
if (!function_exists('set_language')) {
63
	require_once("pfsense-utils.inc");
64
}
65
66
set_language();
67
68 667b2b60 Ermal
/* Some ajax scripts still need access to GUI */
69 45b4ffc6 Phil Davis
if (!$ignorefirmwarelock) {
70 667b2b60 Ermal
	if (is_subsystem_dirty('firmwarelock')) {
71
		if (!$d_isfwfile) {
72 4d4eefab Stephen Beaver
			header("Location: system_update.php");
73 667b2b60 Ermal
			exit;
74
		} else {
75
			return;
76
		}
77
	}
78
}
79
80 45b4ffc6 Phil Davis
/* Reserved table names to avoid collision */
81 ee9783e9 Ermal
$reserved_table_names = array(
82 45b4ffc6 Phil Davis
	"bogons",
83
	"bogonsv6",
84
	"negate_networks",
85
	"snort2c",
86
	"sshlockout",
87
	"tonatsubnets",
88
	"virusprot",
89
	"vpn_networks",
90
	"webConfiguratorlockout"
91 ee9783e9 Ermal
);
92
93 45b4ffc6 Phil Davis
$firewall_rules_dscp_types = array(
94
	"af11",
95
	"af12",
96
	"af13",
97
	"af21",
98
	"af22",
99
	"af23",
100
	"af31",
101
	"af32",
102
	"af33",
103
	"af41",
104
	"af42",
105
	"af43",
106
	"VA",
107
	"EF",
108
	"cs1",
109
	"cs2",
110
	"cs3",
111
	"cs4",
112
	"cs5",
113
	"cs6",
114
	"cs7",
115
	"0x01",
116
	"0x02",
117
	"0x04");
118 667b2b60 Ermal
119
$auth_server_types = array(
120
	'ldap' => "LDAP",
121 7aaf60a8 k-paulius
	'radius' => "RADIUS");
122 667b2b60 Ermal
123
$ldap_urltypes = array(
124
	'TCP - Standard' => 389,
125 d672403c derelict-pf
	'TCP - STARTTLS' => 389,
126 667b2b60 Ermal
	'SSL - Encrypted' => 636);
127
128
$ldap_scopes = array(
129 f49cd725 Phil Davis
	'one' => gettext("One Level"),
130
	'subtree' => gettext("Entire Subtree"));
131 667b2b60 Ermal
132
$ldap_protvers = array(
133
	2,
134
	3);
135
136
$ldap_templates = array(
137
138
	'open' => array(
139 6c07db48 Phil Davis
		'desc' => "OpenLDAP",
140
		'attr_user' => "cn",
141
		'attr_group' => "cn",
142
		'attr_member' => "member"),
143 667b2b60 Ermal
144
	'msad' => array(
145 6c07db48 Phil Davis
		'desc' => "Microsoft AD",
146
		'attr_user' => "samAccountName",
147
		'attr_group' => "cn",
148
		'attr_member' => "memberOf"),
149 667b2b60 Ermal
150
	'edir' => array(
151 6c07db48 Phil Davis
		'desc' => "Novell eDirectory",
152
		'attr_user' => "cn",
153
		'attr_group' => "cn",
154
		'attr_member' => "uniqueMember"));
155 667b2b60 Ermal
156
$radius_srvcs = array(
157 f49cd725 Phil Davis
	'both' => gettext("Authentication and Accounting"),
158
	'auth' => gettext("Authentication"),
159
	'acct' => gettext("Accounting"));
160 667b2b60 Ermal
161 9da4a575 Renato Botelho
$radius_protocol = array(
162
	'PAP' => "PAP",
163
	'CHAP_MD5' => "MD5-CHAP",
164
	'MSCHAPv1' => "MS-CHAPv1",
165
	'MSCHAPv2' => "MS-CHAPv2");
166
167 667b2b60 Ermal
$netbios_nodetypes = array(
168
	'0' => "none",
169
	'1' => "b-node",
170
	'2' => "p-node",
171
	'4' => "m-node",
172 13ec619c Chris Buechler
	'8' => "h-node");
173 667b2b60 Ermal
174 45b4ffc6 Phil Davis
/* some well known ports */
175 667b2b60 Ermal
$wkports = array(
176 96ccd009 Renato Botelho
	5999 => "CVSup",
177 667b2b60 Ermal
	53 => "DNS",
178
	21 => "FTP",
179
	3000 => "HBCI",
180
	80 => "HTTP",
181
	443 => "HTTPS",
182
	5190 => "ICQ",
183
	113 => "IDENT/AUTH",
184
	143 => "IMAP",
185
	993 => "IMAP/S",
186
	4500 => "IPsec NAT-T",
187
	500 => "ISAKMP",
188
	1701 => "L2TP",
189
	389 => "LDAP",
190
	1755 => "MMS/TCP",
191
	7000 => "MMS/UDP",
192
	445 => "MS DS",
193
	3389 => "MS RDP",
194
	1512 => "MS WINS",
195
	1863 => "MSN",
196
	119 => "NNTP",
197
	123 => "NTP",
198
	138 => "NetBIOS-DGM",
199
	137 => "NetBIOS-NS",
200
	139 => "NetBIOS-SSN",
201
	1194 => "OpenVPN",
202
	110 => "POP3",
203
	995 => "POP3/S",
204 96ccd009 Renato Botelho
	1723 => "PPTP",
205 667b2b60 Ermal
	1812 => "RADIUS",
206
	1813 => "RADIUS accounting",
207
	5004 => "RTP",
208
	5060 => "SIP",
209
	25 => "SMTP",
210
	465 => "SMTP/S",
211
	161 => "SNMP",
212
	162 => "SNMP-Trap",
213
	22 => "SSH",
214
	3478 => "STUN",
215 76e91d3f Warren Baker
	587 => "SUBMISSION",
216 667b2b60 Ermal
	3544 => "Teredo",
217
	23 => "Telnet",
218
	69 => "TFTP",
219
	5900 => "VNC");
220
221
/* TCP flags */
222 79cc9e6b bcyrill
$tcpflags = array("fin", "syn", "rst", "psh", "ack", "urg", "ece", "cwr");
223 667b2b60 Ermal
224 f49cd725 Phil Davis
$specialnets = array(
225
	"(self)" => gettext("This Firewall"),
226
	"pppoe" => gettext("PPPoE clients"),
227
	"l2tp" => gettext("L2TP clients"));
228 667b2b60 Ermal
229
$spiflist = get_configured_interface_with_descr(false, true);
230
foreach ($spiflist as $ifgui => $ifdesc) {
231
	$specialnets[$ifgui] = $ifdesc . " net";
232
	$specialnets[$ifgui . 'ip'] = $ifdesc . " address";
233
}
234
235 45b4ffc6 Phil Davis
$medias = array(
236 f49cd725 Phil Davis
	"auto" => gettext("autoselect"),
237
	"100full" => gettext("100BASE-TX full-duplex"),
238
	"100half" => gettext("100BASE-TX half-duplex"),
239
	"10full" => gettext("10BASE-T full-duplex"),
240
	"10half" => gettext("10BASE-T half-duplex"));
241 667b2b60 Ermal
242 45b4ffc6 Phil Davis
$wlan_modes = array(
243 f49cd725 Phil Davis
	"bss" => gettext("Infrastructure (BSS)"),
244
	"adhoc" => gettext("Ad-hoc (IBSS)"),
245
	"hostap" => gettext("Access Point"));
246 667b2b60 Ermal
247 eb4ac13e Renato Botelho
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, &$input_errors) {
248 667b2b60 Ermal
249
	/* check for bad control characters */
250
	foreach ($postdata as $pn => $pd) {
251
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
252 41602469 Luiz Gustavo Costa
			$input_errors[] = sprintf(gettext("The field %s contains invalid characters."), $pn);
253 667b2b60 Ermal
		}
254
	}
255
256
	for ($i = 0; $i < count($reqdfields); $i++) {
257
		if ($_POST[$reqdfields[$i]] == "" && $_REQUEST[$reqdfields[$i]] == "") {
258 bd757043 Renato Botelho
			$input_errors[] = sprintf(gettext("The field %s is required."), $reqdfieldsn[$i]);
259 667b2b60 Ermal
		}
260
	}
261
}
262
263
function print_input_errors($input_errors) {
264 c4a7740d Sander van Leeuwen
	echo '<div class="alert alert-danger input-errors">';
265
	echo '<p>' . gettext('The following input errors were detected:') . '</p>';
266
	echo '<ul>';
267 667b2b60 Ermal
268 e28307d0 Luiz Gustavo Costa
	foreach ($input_errors as $ierr) {
269 c4a7740d Sander van Leeuwen
		echo '<li>' . htmlspecialchars($ierr) . '</li>';
270 e28307d0 Luiz Gustavo Costa
	}
271 667b2b60 Ermal
272 c4a7740d Sander van Leeuwen
	echo '</ul>';
273
	echo '</div>';
274 667b2b60 Ermal
}
275
276
function verify_gzip_file($fname) {
277 96ccd009 Renato Botelho
	$returnvar = mwexec("/usr/bin/gzip -t " . escapeshellarg($fname));
278 45b4ffc6 Phil Davis
	if ($returnvar != 0) {
279 667b2b60 Ermal
		return 0;
280 45b4ffc6 Phil Davis
	} else {
281 667b2b60 Ermal
		return 1;
282 45b4ffc6 Phil Davis
	}
283 667b2b60 Ermal
}
284
285 aa64bb65 jim-p
// sprint_info_box() returns a string with a formatted informational box, it does not print the box.
286
// To format and print in one step, call print_info_box() as usual.
287
// Any required button is explicitly created, rather than relying on the detection of certain
288 65e8424e Phil Davis
// strings in the message (such as "apply"). print_info_box_np() has been exterminated.
289
// $class = the bootstrap style class (default, info, warning, success, danger)
290 f14ec73c Stephen Beaver
// $btnname and btntext describe the optional button and its display text, the default is an 'x' Close button.
291 65e8424e Phil Davis
// Note that there is also a shortcut function print_apply_box here that creates a standard "apply" box for you.
292
// In many cases just substitute that for print_info_box_np() to easily get a warning style "Apply changes" box.
293 aa64bb65 jim-p
function sprint_info_box($msg, $class="alert-warning", $btnname = "close", $btntext = "", $btnicon = "", $btnclass = "default") {
294 667b2b60 Ermal
295 aa82505e Phil Davis
	if (strpos($class, "alert-") !== 0) {
296 0074384f sbeaver
		$class = 'alert-' . $class;
297 aa82505e Phil Davis
	}
298 0074384f sbeaver
299 ff916cd2 Sander van Leeuwen
	$msg = '<div class="pull-left">' . $msg . '</div>';
300
301 0d711d38 Phil Davis
	if ($btnname === "close") {
302
		$msg = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' . $msg;
303
	} else if ($btnname != "") {
304
		if (empty($btntext)) {
305
			$btntext = $btnname;
306
		}
307 c0367cb8 jim-p
		if (!empty($btnicon)) {
308
			$btnicon = '<i class="fa ' . $btnicon . ' icon-embed-btn"></i>';
309
		}
310 0d711d38 Phil Davis
311 c0367cb8 jim-p
		$msg .= '<form method="post" class="pull-right"><button type="submit" class="btn btn-' . $btnclass . '" name="'. $btnname . '" value="' . $btntext . '">' . $btnicon . $btntext . '</button>';
312 667b2b60 Ermal
313 aa82505e Phil Davis
		if ($_POST['if']) {
314 01752a98 Sjon Hortensius
			$msg .= "<input type=\"hidden\" name=\"if\" value=\"" . htmlspecialchars($_POST['if']) . "\" />";
315 aa82505e Phil Davis
		}
316 1180e4f0 Sjon Hortensius
317 ff916cd2 Sander van Leeuwen
		$msg .= '</form>';
318 aa82505e Phil Davis
	}
319 667b2b60 Ermal
320 aa64bb65 jim-p
	return '<div class="alert ' . $class . ' clearfix" role="alert">' . $msg . '</div>';
321
}
322
323
// Format and print an info box. See sprint_info_box() for details.
324
function print_info_box($msg, $class="alert-warning", $btnname = "close", $btntext = "", $btnicon = "", $btnclass = "default") {
325
	echo sprint_info_box($msg, $class, $btnname, $btntext, $btnicon, $btnclass);
326 667b2b60 Ermal
}
327
328 3b3a95e5 Phil Davis
function print_apply_box($msg) {
329 c0367cb8 jim-p
	print_info_box($msg, "warning", "apply", gettext("Apply Changes"), 'fa-check', 'success');
330 667b2b60 Ermal
}
331
332 44c42356 Phil Davis
// Format and print a box reporting that changes have been applied
333
// $retval = status value from the functions called to apply the changes
334
// 0 is good
335
// non-zero is a problem
336
// $extra_text = optional extra text to display after the standard message
337
function print_apply_result_box($retval, $extra_text="") {
338
	$result_msg = get_std_save_message($retval);
339
	if ($retval === 0) {
340
		// 0 is success
341
		$severity = "success";
342
	} else {
343
		// non-zero means there was some problem
344
		$severity = "warning";
345
	}
346
347
	if (strlen($extra_text) > 0) {
348
		$result_msg .= " " . $extra_text;
349
	}
350
	print_info_box($result_msg, $severity);
351
}
352
353 8c9d0b20 k-paulius
/*
354
 * Print Bootstrap callout
355
 *
356
 * @param string $msg     message to display
357
 * @param string $class   contextual class, defaults to info (default | danger | warning | info)
358
 * @param string $heading optional callout heading
359
 */
360
function print_callout($msg, $class = 'info', $heading = '') {
361
362
	if ('' == $msg) {
363
		return;
364
	}
365
	$class = strtolower($class);
366
	$callout = '';
367
368 b8f62ec2 Phil Davis
	if ($class != 'default' && $class != 'danger' && $class != 'warning' && $class != 'info') {
369 8c9d0b20 k-paulius
		$class = 'info';
370
	}
371
	$callout .= '<div class="bs-callout bs-callout-' . $class . '">';
372
373
	if ('' != $heading) {
374
		$callout .= '<h4>' . $heading . '</h4>';
375
	}
376
	$callout .= $msg . '</div>';
377
	echo $callout;
378
}
379
380 44c42356 Phil Davis
function get_std_save_message($retval) {
381 cfaf6e69 Scott Ullrich
	$filter_related = false;
382 815398fb Phil Davis
	$filter_pages = array("firewall_aliases", "firewall_nat", "firewall_rules", "status_logs_filter");
383 44c42356 Phil Davis
	if ($retval === 0) {
384
		// 0 is success
385
		$to_return = gettext("The changes have been applied successfully.");
386
	} else {
387
		// non-zero means there was some problem
388 f8a1be56 Phil Davis
		$to_return = sprintf(gettext('There was a problem applying the changes. See the %1$sSystem Logs%2$s.'), '<a href=\"status_logs.php\">', '</a>');
389 44c42356 Phil Davis
	}
390 45b4ffc6 Phil Davis
	foreach ($filter_pages as $fp) {
391
		if (stristr($_SERVER['SCRIPT_FILENAME'], $fp)) {
392 96ccd009 Renato Botelho
			$filter_related = true;
393 45b4ffc6 Phil Davis
		}
394
	}
395
	if ($filter_related) {
396 815398fb Phil Davis
		$to_return .= " " . gettext("The firewall rules are now reloading in the background.") . "<br />" .
397 702fa4d0 Phil Davis
		    sprintf(gettext('%1$sMonitor%2$s the filter reload progress.'), "<a href='status_filter_reload.php'>", "</a>");
398 45b4ffc6 Phil Davis
	}
399 cfaf6e69 Scott Ullrich
	return $to_return;
400 667b2b60 Ermal
}
401
402
function pprint_address($adr) {
403
	global $specialnets;
404
405
	if (isset($adr['any'])) {
406
		$padr = "*";
407
	} else if ($adr['network']) {
408
		$padr = $specialnets[$adr['network']];
409
	} else {
410
		$padr = $adr['address'];
411
	}
412
413 45b4ffc6 Phil Davis
	if (isset($adr['not'])) {
414 667b2b60 Ermal
		$padr = "! " . $padr;
415 45b4ffc6 Phil Davis
	}
416 667b2b60 Ermal
417
	return $padr;
418
}
419
420
function pprint_port($port) {
421
	global $wkports;
422
423
	$pport = "";
424
425 45b4ffc6 Phil Davis
	if (!$port) {
426 667b2b60 Ermal
		return "*";
427 45b4ffc6 Phil Davis
	} else {
428 667b2b60 Ermal
		$srcport = explode("-", $port);
429
		if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
430
			$pport = $srcport[0];
431
			if ($wkports[$srcport[0]]) {
432
				$pport .= " (" . $wkports[$srcport[0]] . ")";
433
			}
434 45b4ffc6 Phil Davis
		} else {
435 667b2b60 Ermal
			$pport .= $srcport[0] . " - " . $srcport[1];
436 45b4ffc6 Phil Davis
		}
437 667b2b60 Ermal
	}
438
439
	return $pport;
440
}
441
442 8e0c3760 Ermal
function firewall_check_for_advanced_options(&$item) {
443 96ccd009 Renato Botelho
	$item_set = "";
444 45b4ffc6 Phil Davis
	if ($item['os']) {
445
			$item_set .= "os {$item['os']} ";
446
	}
447
	if ($item['dscp']) {
448 f0c1ce21 Phil Davis
		$item_set .= "dscp {$item['dscp']} ";
449 45b4ffc6 Phil Davis
	}
450
	if ($item['max']) {
451 96ccd009 Renato Botelho
		$item_set .= "max {$item['max']} ";
452 45b4ffc6 Phil Davis
	}
453
	if ($item['max-src-nodes']) {
454 96ccd009 Renato Botelho
		$item_set .= "max-src-nodes {$item['max-src-nodes']} ";
455 45b4ffc6 Phil Davis
	}
456
	if ($item['max-src-conn']) {
457 96ccd009 Renato Botelho
		$item_set .= "max-src-conn {$item['max-src-conn']} ";
458 45b4ffc6 Phil Davis
	}
459
	if ($item['max-src-states']) {
460 96ccd009 Renato Botelho
		$item_set .= "max-src-states {$item['max-src-states']} ";
461 45b4ffc6 Phil Davis
	}
462
	if (isset($item['nopfsync'])) {
463 f0c1ce21 Phil Davis
		$item_set .= "nopfsync ";
464 45b4ffc6 Phil Davis
	}
465
	if ($item['statetype'] != "keep state" && $item['statetype'] != "") {
466 96ccd009 Renato Botelho
		$item_set .= "statetype {$item['statetype']} ";
467 45b4ffc6 Phil Davis
	}
468
	if ($item['statetimeout']) {
469 96ccd009 Renato Botelho
		$item_set .= "statetimeout {$item['statetimeout']} ";
470 45b4ffc6 Phil Davis
	}
471
	if (isset($item['nosync'])) {
472 f0c1ce21 Phil Davis
		$item_set .= "no XMLRPC Sync ";
473 45b4ffc6 Phil Davis
	}
474
	if ($item['max-src-conn-rate']) {
475 96ccd009 Renato Botelho
		$item_set .= "max-src-conn-rate {$item['max-src-conn-rate']} ";
476 45b4ffc6 Phil Davis
	}
477
	if ($item['max-src-conn-rates']) {
478 96ccd009 Renato Botelho
		$item_set .= "max-src-conn-rates {$item['max-src-conn-rates']} ";
479 45b4ffc6 Phil Davis
	}
480
	if ($item['vlanprio']) {
481 f0c1ce21 Phil Davis
		$item_set .= "vlanprio {$item['vlanprio']} ";
482 45b4ffc6 Phil Davis
	}
483
	if ($item['vlanprioset']) {
484 f0c1ce21 Phil Davis
		$item_set .= "vlanprioset {$item['vlanprioset']} ";
485 45b4ffc6 Phil Davis
	}
486
	if ($item['gateway']) {
487 96ccd009 Renato Botelho
		$item_set .= "gateway {$item['gateway']} ";
488 45b4ffc6 Phil Davis
	}
489
	if ($item['dnpipe']) {
490 96ccd009 Renato Botelho
		$item_set .= "limiter {$item['dnpipe']} ";
491 45b4ffc6 Phil Davis
	}
492
	if ($item['pdnpipe']) {
493 96ccd009 Renato Botelho
		$item_set .= "limiter {$item['pdnpipe']} ";
494 45b4ffc6 Phil Davis
	}
495
	if ($item['ackqueue']) {
496 f0c1ce21 Phil Davis
		$item_set .= "ackqueue {$item['ackqueue']} ";
497 45b4ffc6 Phil Davis
	}
498
	if ($item['defaultqueue']) {
499 f0c1ce21 Phil Davis
		$item_set .= "defaultqueue {$item['defaultqueue']} ";
500 45b4ffc6 Phil Davis
	}
501
	if ($item['tag']) {
502 96ccd009 Renato Botelho
		$item_set .= "tag {$item['tag']} ";
503 45b4ffc6 Phil Davis
	}
504
	if ($item['tagged']) {
505 96ccd009 Renato Botelho
		$item_set .= "tagged {$item['tagged']} ";
506 45b4ffc6 Phil Davis
	}
507
	if (isset($item['allowopts'])) {
508 96ccd009 Renato Botelho
		$item_set .= "allowopts ";
509 45b4ffc6 Phil Davis
	}
510
	if (isset($item['disablereplyto'])) {
511 96ccd009 Renato Botelho
		$item_set .= "disable reply-to ";
512 45b4ffc6 Phil Davis
	}
513
	if ($item['tcpflags_any'] || $item['tcpflags1'] || $item['tcpflags2']) {
514 96ccd009 Renato Botelho
		$item_set .= "tcpflags set";
515 45b4ffc6 Phil Davis
	}
516 96ccd009 Renato Botelho
517
	return $item_set;
518 8e0c3760 Ermal
}
519
520 667b2b60 Ermal
function gentitle($title) {
521
	global $navlevelsep;
522 45b4ffc6 Phil Davis
	if (!is_array($title)) {
523 667b2b60 Ermal
		return $title;
524 45b4ffc6 Phil Davis
	} else {
525 667b2b60 Ermal
		return join($navlevelsep, $title);
526 45b4ffc6 Phil Davis
	}
527 667b2b60 Ermal
}
528
529 edcd7535 Phil Davis
function genhtmltitle($title, $links=true) {
530
531
	$num_crumbs = count($title);
532 2d26ee5e Sjon Hortensius
533 2f1f9a98 Stephen Beaver
	// If the array contains only one element, there are no breadcrumbs, so don't
534
	// add anything else
535 edcd7535 Phil Davis
	if ($num_crumbs > 1) {
536 2f1f9a98 Stephen Beaver
		$bc = '<ol class="breadcrumb">';
537 2d26ee5e Sjon Hortensius
538 edcd7535 Phil Davis
		if (!is_array($links)) {
539
			$gen_default = ($links === true);
540
			$links = array_fill(0, $num_crumbs, '');
541
			// If no links passed, then default to a link to self on the last entry.
542
			if ($gen_default) {
543
				$links[$num_crumbs-1] = '@self';
544
			}
545
		}
546
547 c50f228a Phil Davis
		foreach ($title as $idx => $el) {
548
			$href = $links[$idx];
549
			if (strlen($href) > 0) {
550
				// For convenience, if the caller specifies '@self' then make a link
551
				// to the current page, including any query string.
552
				if ($href == '@self') {
553
					$href = $_SERVER['REQUEST_URI'];
554
				}
555
				if (substr($href, 0, 1) != '/') {
556
					$href = '/' . $href;
557
				}
558
				$bc .= '<li><a href="' . htmlentities($href) . '">' . $el . '</a></li>';
559
			} else {
560
				$bc .= '<li>' . $el . '</li>';
561
			}
562 aa82505e Phil Davis
		}
563 2d26ee5e Sjon Hortensius
564 2f1f9a98 Stephen Beaver
		$bc .= '</ol>';
565
	} else {
566
		$bc = "";
567
	}
568 2d26ee5e Sjon Hortensius
569 f93e9098 Phil Davis
	return $bc;
570 667b2b60 Ermal
}
571
572
/* update the changedesc and changecount(er) variables */
573
function update_changedesc($update) {
574
	global $changedesc;
575
	global $changecount;
576
577
	$changedesc .= " {$update}";
578
	$changecount++;
579
}
580
581 65a0e193 Stephen Beaver
// This version of dump_clog() does not output <td></td> or any other table elements.
582 33d52df1 sbeaver
function dump_clog_no_table($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
583
	global $g, $config;
584
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
585 76af8cdb NOYB
	$specific_log = basename($logfile, '.log') . '_settings';
586
	if ($config['syslog'][$specific_log]['cronorder'] == 'forward') $sor = "";
587
	if ($config['syslog'][$specific_log]['cronorder'] == 'reverse') $sor = "-r";
588 27e1e9d5 NewEraCracker
	$logarr = array();
589 33d52df1 sbeaver
	$grepline = "  ";
590 aa82505e Phil Davis
	if (is_array($grepfor)) {
591 27e1e9d5 NewEraCracker
		$invert = '';
592 d3d05c15 NOYB
		if ((strpos($grepfor[0], '!') === 0)) {
593
			$grepfor[0] = substr($grepfor[0], 1);
594
			$invert = '-v';
595
		}
596 27e1e9d5 NewEraCracker
		$grepline .= " | /usr/bin/egrep {$invert} " . escapeshellarg(implode("|", $grepfor));
597 aa82505e Phil Davis
	}
598
	if (is_array($grepinvert)) {
599 33d52df1 sbeaver
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
600 aa82505e Phil Davis
	}
601 33d52df1 sbeaver
	if (is_dir($logfile)) {
602 f49cd725 Phil Davis
		$logarr = array(sprintf(gettext("File %s is a directory."), $logfile));
603 33d52df1 sbeaver
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
604 f49cd725 Phil Davis
		$logarr = array(gettext("Log file started."));
605 33d52df1 sbeaver
	} else {
606 ecffb0bb Phil Davis
		if ($config['system']['disablesyslogclog']) {
607 33d52df1 sbeaver
			exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
608
		} else {
609 41df62c1 jim-p
			exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
610 33d52df1 sbeaver
		}
611
	}
612
	echo "\n";
613 01752a98 Sjon Hortensius
614 0074384f sbeaver
	$rows = 0;
615 33d52df1 sbeaver
	foreach ($logarr as $logent) {
616 0074384f sbeaver
		$rows++;
617
		$logent = preg_split("/\s+/", $logent, 6);
618
619
		if ($withorig) {
620 41df62c1 jim-p
				$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
621
				$entry_text = ($logent[3] ==  $config['system']['hostname']) ? "" : $logent[3] . " ";
622
				$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
623 0074384f sbeaver
				echo "{$entry_date_time}";
624
				echo " " . "{$entry_text}"	. "\n";
625
		} else {
626
				echo htmlspecialchars($logent[5]) . "\n";
627
		}
628 33d52df1 sbeaver
629
	}
630 0074384f sbeaver
	return($rows);
631 33d52df1 sbeaver
}
632
633 667b2b60 Ermal
function dump_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "") {
634
	global $g, $config;
635
	$sor = isset($config['syslog']['reverse']) ? "-r" : "";
636 76af8cdb NOYB
	$specific_log = basename($logfile, '.log') . '_settings';
637
	if ($config['syslog'][$specific_log]['cronorder'] == 'forward') $sor = "";
638
	if ($config['syslog'][$specific_log]['cronorder'] == 'reverse') $sor = "-r";
639 27e1e9d5 NewEraCracker
	$logarr = array();
640 667b2b60 Ermal
	$grepline = "  ";
641 45b4ffc6 Phil Davis
	if (is_array($grepfor)) {
642 27e1e9d5 NewEraCracker
		$invert = '';
643 d3d05c15 NOYB
		if ((strpos($grepfor[0], '!') === 0)) {
644
			$grepfor[0] = substr($grepfor[0], 1);
645
			$invert = '-v';
646
		}
647 27e1e9d5 NewEraCracker
		$grepline .= " | /usr/bin/egrep {$invert} " . escapeshellarg(implode("|", $grepfor));
648 45b4ffc6 Phil Davis
	}
649
	if (is_array($grepinvert)) {
650 7b7ad7f6 Renato Botelho
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
651 45b4ffc6 Phil Davis
	}
652 b67cdd05 Matt Smith
	if (is_dir($logfile)) {
653 f49cd725 Phil Davis
		$logarr = array(sprintf(gettext("File %s is a directory."), $logfile));
654 56bd2035 jim-p
	} elseif (file_exists($logfile) && filesize($logfile) == 0) {
655 f49cd725 Phil Davis
		$logarr = array(gettext("Log file started."));
656 667b2b60 Ermal
	} else {
657 45b4ffc6 Phil Davis
		if ($config['system']['disablesyslogclog']) {
658 d31ca336 Renato Botelho
			exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
659 667b2b60 Ermal
		} else {
660 41df62c1 jim-p
			exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
661 667b2b60 Ermal
		}
662
	}
663 ba6c5357 Stephen Beaver
664 9bf8bd8c NOYB
	$rows = 0;
665 667b2b60 Ermal
	foreach ($logarr as $logent) {
666 9bf8bd8c NOYB
		$rows++;
667 45b4ffc6 Phil Davis
		$logent = preg_split("/\s+/", $logent, 6);
668 ba6c5357 Stephen Beaver
		echo "<tr>\n";
669 45b4ffc6 Phil Davis
		if ($withorig) {
670 41df62c1 jim-p
			$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
671
			$entry_text = ($logent[3] == $config['system']['hostname']) ? "" : $logent[3] . " ";
672
			$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
673 b77cef66 Colin Fleming
			echo "<td class=\"text-nowrap\">{$entry_date_time}</td>\n";
674 247418f1 heper
			echo "<td style=\"word-wrap:break-word; word-break:break-all; white-space:normal\">{$entry_text}</td>\n";
675 45b4ffc6 Phil Davis
		} else {
676 ba6c5357 Stephen Beaver
				echo "<td>" . htmlspecialchars($logent[5]) . "</td>\n";
677 45b4ffc6 Phil Davis
		}
678
		echo "</tr>\n";
679 667b2b60 Ermal
	}
680 9bf8bd8c NOYB
	return($rows);
681 667b2b60 Ermal
}
682
683
function return_clog($logfile, $tail, $withorig = true, $grepfor = "", $grepinvert = "", $grepreverse = false) {
684
	global $g, $config;
685
	$sor = (isset($config['syslog']['reverse']) || $grepreverse) ? "-r" : "";
686 76af8cdb NOYB
	$specific_log = basename($logfile, '.log') . '_settings';
687 e00df659 NOYB
	if (($config['syslog'][$specific_log]['cronorder'] == 'forward') && !$grepreverse) $sor = "";
688
	if (($config['syslog'][$specific_log]['cronorder'] == 'reverse') ||  $grepreverse) $sor = "-r";
689 27e1e9d5 NewEraCracker
	$logarr = array();
690 667b2b60 Ermal
	$grepline = "  ";
691 45b4ffc6 Phil Davis
	if (is_array($grepfor)) {
692 7b7ad7f6 Renato Botelho
		$grepline .= " | /usr/bin/egrep " . escapeshellarg(implode("|", $grepfor));
693 45b4ffc6 Phil Davis
	}
694
	if (is_array($grepinvert)) {
695 7b7ad7f6 Renato Botelho
		$grepline .= " | /usr/bin/egrep -v " . escapeshellarg(implode("|", $grepinvert));
696 45b4ffc6 Phil Davis
	}
697
	if ($config['system']['disablesyslogclog']) {
698 d31ca336 Renato Botelho
		exec("cat " . escapeshellarg($logfile) . "{$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
699 667b2b60 Ermal
	} else {
700 41df62c1 jim-p
		exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . "{$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
701 667b2b60 Ermal
	}
702
	return($logarr);
703
}
704
705
/* Check if variable has changed, update and log if it has
706
 * returns true if var changed
707
 * varname = variable name in plain text
708
 * orig = original value
709
 * new = new value
710
 */
711
function update_if_changed($varname, & $orig, $new) {
712
	if (is_array($orig) && is_array($new)) {
713
		$a_diff = array_diff($orig, $new);
714
		foreach ($a_diff as $diff) {
715
			update_changedesc("removed {$varname}: \"{$diff}\"");
716
		}
717
		$a_diff = array_diff($new, $orig);
718
		foreach ($a_diff as $diff) {
719
			update_changedesc("added {$varname}: \"{$diff}\"");
720
		}
721
		$orig = $new;
722
		return true;
723
724
	} else {
725
		if ($orig != $new) {
726
			update_changedesc("{$varname}: \"{$orig}\" -> \"{$new}\"");
727
			$orig = $new;
728
			return true;
729
		}
730
	}
731
	return false;
732
}
733
734
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
735 45b4ffc6 Phil Davis
	if (isset($adr['any'])) {
736 96ccd009 Renato Botelho
		$padr = "any";
737 45b4ffc6 Phil Davis
	} else if ($adr['network']) {
738 96ccd009 Renato Botelho
		$padr = $adr['network'];
739 45b4ffc6 Phil Davis
	} else if ($adr['address']) {
740 96ccd009 Renato Botelho
		list($padr, $pmask) = explode("/", $adr['address']);
741 cb2b59b8 Renato Botelho
		if (!$pmask) {
742 45b4ffc6 Phil Davis
			if (is_ipaddrv6($padr)) {
743 cb2b59b8 Renato Botelho
				$pmask = 128;
744 45b4ffc6 Phil Davis
			} else {
745 cb2b59b8 Renato Botelho
				$pmask = 32;
746 45b4ffc6 Phil Davis
			}
747 cb2b59b8 Renato Botelho
		}
748 96ccd009 Renato Botelho
	}
749 667b2b60 Ermal
750 45b4ffc6 Phil Davis
	if (isset($adr['not'])) {
751 96ccd009 Renato Botelho
		$pnot = 1;
752 45b4ffc6 Phil Davis
	} else {
753 96ccd009 Renato Botelho
		$pnot = 0;
754 45b4ffc6 Phil Davis
	}
755 96ccd009 Renato Botelho
756
	if ($adr['port']) {
757
		list($pbeginport, $pendport) = explode("-", $adr['port']);
758 45b4ffc6 Phil Davis
		if (!$pendport) {
759 96ccd009 Renato Botelho
			$pendport = $pbeginport;
760 45b4ffc6 Phil Davis
		}
761 667b2b60 Ermal
	} else if (!is_alias($pbeginport) && !is_alias($pendport)) {
762
		$pbeginport = "any";
763
		$pendport = "any";
764 96ccd009 Renato Botelho
	}
765 667b2b60 Ermal
}
766
767 6c07db48 Phil Davis
function pconfig_to_address(&$adr, $padr, $pmask, $pnot = false, $pbeginport = 0, $pendport = 0) {
768 96ccd009 Renato Botelho
	$adr = array();
769
770 45b4ffc6 Phil Davis
	if ($padr == "any") {
771 96ccd009 Renato Botelho
		$adr['any'] = true;
772 45b4ffc6 Phil Davis
	} else if (is_specialnet($padr)) {
773 96ccd009 Renato Botelho
		$adr['network'] = $padr;
774 45b4ffc6 Phil Davis
	} else {
775 96ccd009 Renato Botelho
		$adr['address'] = $padr;
776 cb2b59b8 Renato Botelho
		if (is_ipaddrv6($padr)) {
777 45b4ffc6 Phil Davis
			if ($pmask != 128) {
778 cb2b59b8 Renato Botelho
				$adr['address'] .= "/" . $pmask;
779 45b4ffc6 Phil Davis
			}
780 cb2b59b8 Renato Botelho
		} else {
781 45b4ffc6 Phil Davis
			if ($pmask != 32) {
782 cb2b59b8 Renato Botelho
				$adr['address'] .= "/" . $pmask;
783 45b4ffc6 Phil Davis
			}
784 cb2b59b8 Renato Botelho
		}
785 96ccd009 Renato Botelho
	}
786 667b2b60 Ermal
787 45b4ffc6 Phil Davis
	if ($pnot) {
788 96ccd009 Renato Botelho
		$adr['not'] = true;
789 45b4ffc6 Phil Davis
	} else {
790 96ccd009 Renato Botelho
		unset($adr['not']);
791 45b4ffc6 Phil Davis
	}
792 96ccd009 Renato Botelho
793
	if (($pbeginport != 0) && ($pbeginport != "any")) {
794 45b4ffc6 Phil Davis
		if ($pbeginport != $pendport) {
795 96ccd009 Renato Botelho
			$adr['port'] = $pbeginport . "-" . $pendport;
796 45b4ffc6 Phil Davis
		} else {
797 96ccd009 Renato Botelho
			$adr['port'] = $pbeginport;
798 45b4ffc6 Phil Davis
		}
799 96ccd009 Renato Botelho
	}
800
801 45b4ffc6 Phil Davis
	if (is_alias($pbeginport)) {
802 96ccd009 Renato Botelho
		$adr['port'] = $pbeginport;
803
	}
804 667b2b60 Ermal
}
805
806
function is_specialnet($net) {
807 96ccd009 Renato Botelho
	global $specialsrcdst;
808 667b2b60 Ermal
809 45b4ffc6 Phil Davis
	if (!$net) {
810 96ccd009 Renato Botelho
		return false;
811 45b4ffc6 Phil Davis
	}
812
	if (in_array($net, $specialsrcdst)) {
813 96ccd009 Renato Botelho
		return true;
814 45b4ffc6 Phil Davis
	} else {
815 667b2b60 Ermal
		return false;
816 45b4ffc6 Phil Davis
	}
817 667b2b60 Ermal
}
818
819
//function to create widget tabs when called
820 96ccd009 Renato Botelho
function display_widget_tabs(& $tab_array) {
821 f326b978 Colin Fleming
	echo "<div id=\"tabs\">";
822 667b2b60 Ermal
	$tabscounter = 0;
823
	foreach ($tab_array as $ta) {
824 6c07db48 Phil Davis
		$dashpos = strpos($ta[2], '-');
825 45b4ffc6 Phil Davis
		$tabname = $ta[2] . "-tab";
826 6c07db48 Phil Davis
		$tabclass = substr($ta[2], 0, $dashpos);
827 45b4ffc6 Phil Davis
		$tabclass = $tabclass . "-class";
828 667b2b60 Ermal
		if ($ta[1] == true) {
829
			$tabActive = "table-cell";
830
			$tabNonActive = "none";
831 45b4ffc6 Phil Davis
		} else {
832 667b2b60 Ermal
			$tabActive = "none";
833
			$tabNonActive = "table-cell";
834
		}
835 f326b978 Colin Fleming
		echo "<div id=\"{$ta[2]}-active\" class=\"{$tabclass}-tabactive\" style=\"display:{$tabActive}; background-color:#EEEEEE; color:black;\">";
836
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
837
		echo "&nbsp;&nbsp;&nbsp;</b>";
838 667b2b60 Ermal
		echo "</div>";
839 f326b978 Colin Fleming
840
		echo "<div id=\"{$ta[2]}-deactive\" class=\"{$tabclass}-tabdeactive\" style=\"display:{$tabNonActive}; background-color:#777777; color:white; cursor: pointer;\" onclick=\"return changeTabDIV('{$ta[2]}')\">";
841
		echo "<b>&nbsp;&nbsp;&nbsp;{$ta[0]}";
842
		echo "&nbsp;&nbsp;&nbsp;</b>";
843 667b2b60 Ermal
		echo "</div>";
844
	}
845
	echo "</div>";
846
}
847
848
849 45b4ffc6 Phil Davis
// Return inline javascript file or CSS to minimize
850 667b2b60 Ermal
// request count going back to server.
851
function outputJavaScriptFileInline($javascript) {
852 45b4ffc6 Phil Davis
	if (file_exists($javascript)) {
853 667b2b60 Ermal
		echo "\n<script type=\"text/javascript\">\n";
854 86573bb9 Phil Davis
		include_once($javascript);
855 667b2b60 Ermal
		echo "\n</script>\n";
856
	} else {
857 45b4ffc6 Phil Davis
		echo "\n\n<!-- Could not locate file:  {$javascript} -->\n\n";
858 667b2b60 Ermal
	}
859
}
860
861
862
863
function outputCSSPrintFileInline($css) {
864 45b4ffc6 Phil Davis
	if (file_exists($css)) {
865 667b2b60 Ermal
		echo "\n<style media=\"print\" type=\"text/css\">\n";
866 86573bb9 Phil Davis
		include_once($css);
867 667b2b60 Ermal
		echo "\n</style>\n";
868
	} else {
869 45b4ffc6 Phil Davis
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
870 667b2b60 Ermal
	}
871
}
872
873
874
function outputCSSFileInline($css) {
875 45b4ffc6 Phil Davis
	if (file_exists($css)) {
876 667b2b60 Ermal
		echo "\n<style type=\"text/css\">\n";
877 86573bb9 Phil Davis
		include_once($css);
878 667b2b60 Ermal
		echo "\n</style>\n";
879
	} else {
880 45b4ffc6 Phil Davis
		echo "\n\n<!-- Could not locate file:  {$css} -->\n\n";
881 667b2b60 Ermal
	}
882
}
883
884
$rfc2616 = array(
885
	100 => "100 Continue",
886
	101 => "101 Switching Protocols",
887
	200 => "200 OK",
888
	201 => "201 Created",
889
	202 => "202 Accepted",
890
	203 => "203 Non-Authoritative Information",
891
	204 => "204 No Content",
892
	205 => "205 Reset Content",
893
	206 => "206 Partial Content",
894
	300 => "300 Multiple Choices",
895
	301 => "301 Moved Permanently",
896
	302 => "302 Found",
897
	303 => "303 See Other",
898
	304 => "304 Not Modified",
899
	305 => "305 Use Proxy",
900
	306 => "306 (Unused)",
901
	307 => "307 Temporary Redirect",
902
	400 => "400 Bad Request",
903
	401 => "401 Unauthorized",
904
	402 => "402 Payment Required",
905
	403 => "403 Forbidden",
906
	404 => "404 Not Found",
907
	405 => "405 Method Not Allowed",
908
	406 => "406 Not Acceptable",
909
	407 => "407 Proxy Authentication Required",
910
	408 => "408 Request Timeout",
911
	409 => "409 Conflict",
912
	410 => "410 Gone",
913
	411 => "411 Length Required",
914
	412 => "412 Precondition Failed",
915
	413 => "413 Request Entity Too Large",
916
	414 => "414 Request-URI Too Long",
917
	415 => "415 Unsupported Media Type",
918
	416 => "416 Requested Range Not Satisfiable",
919
	417 => "417 Expectation Failed",
920
	500 => "500 Internal Server Error",
921
	501 => "501 Not Implemented",
922
	502 => "502 Bad Gateway",
923
	503 => "503 Service Unavailable",
924
	504 => "504 Gateway Timeout",
925
	505 => "505 HTTP Version Not Supported"
926
);
927
928
function is_rfc2616_code($code) {
929
	global $rfc2616;
930 45b4ffc6 Phil Davis
	if (isset($rfc2616[$code])) {
931 667b2b60 Ermal
		return true;
932 45b4ffc6 Phil Davis
	} else {
933 667b2b60 Ermal
		return false;
934 45b4ffc6 Phil Davis
	}
935 667b2b60 Ermal
}
936
937 45b4ffc6 Phil Davis
function print_rfc2616_select($tag, $current) {
938 667b2b60 Ermal
	global $rfc2616;
939
940
	/* Default to 200 OK if not set */
941 45b4ffc6 Phil Davis
	if ($current == "") {
942 667b2b60 Ermal
		$current = 200;
943 45b4ffc6 Phil Davis
	}
944 667b2b60 Ermal
945 96ccd009 Renato Botelho
	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
946 45b4ffc6 Phil Davis
	foreach ($rfc2616 as $code => $message) {
947 667b2b60 Ermal
		if ($code == $current) {
948 c4b60a9a Colin Fleming
			$sel = " selected";
949 667b2b60 Ermal
		} else {
950
			$sel = "";
951
		}
952
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
953
	}
954 3f08e7ab Colin Fleming
	echo "</select>\n";
955 667b2b60 Ermal
}
956
957
// Useful debugging function, much cleaner than print_r
958 6c07db48 Phil Davis
function echo_array($array, $return_me = false) {
959 45b4ffc6 Phil Davis
	if (is_array($array) == false) {
960 96ccd009 Renato Botelho
		$return = "The provided variable is not an array.";
961 45b4ffc6 Phil Davis
	} else {
962
		foreach ($array as $name=>$value) {
963
			if (is_array($value)) {
964 96ccd009 Renato Botelho
				$return .= "";
965
				$return .= "['<b>$name</b>'] {<div style=\"margin-left:10px;\">\n";
966 6c07db48 Phil Davis
				$return .= echo_array($value, true);
967 96ccd009 Renato Botelho
				$return .= "</div>}";
968
				$return .= "\n\n";
969 45b4ffc6 Phil Davis
			} else {
970
				if (is_string($value)) {
971 96ccd009 Renato Botelho
					$value = "\"$value\"";
972
				}
973
				$return .= "['<b>$name</b>'] = $value\n\n";
974
			}
975
		}
976
	}
977 45b4ffc6 Phil Davis
	if ($return_me == true) {
978 96ccd009 Renato Botelho
		return $return;
979 45b4ffc6 Phil Davis
	} else {
980 96ccd009 Renato Botelho
		echo "<pre>".$return."</pre>";
981
	}
982 667b2b60 Ermal
}
983
984
/****f* pfsense-utils/display_top_tabs
985
 * NAME
986 0074384f sbeaver
 *	 display_top_tabs - display tabs with rounded edges
987 667b2b60 Ermal
 * INPUTS
988 0074384f sbeaver
 *	 $text	  - array of tabs
989 667b2b60 Ermal
 * RESULT
990 0074384f sbeaver
 *	 null
991 667b2b60 Ermal
 ******/
992 e3947e77 Steve Beaver
function display_top_tabs(& $tab_array, $no_drop_down = false, $type = 'pills', $usepost = "") {
993 96ccd009 Renato Botelho
	global $config;
994
	global $g;
995
	global $tab_array_indent;
996
	global $tab_array_space;
997
	global $tab_array_char_limit;
998
999 0074384f sbeaver
	/*	does the user have access to this tab?
1000
	 *	master user has access to everything.
1001
	 *	if the user does not have access, simply
1002
	 *	unset the tab item.
1003 96ccd009 Renato Botelho
	 */
1004
1005
	/* empty string code */
1006
	if ($tab_array_indent == '') {
1007
		$tab_array_indent = 0;
1008
	}
1009 620ac186 Scott Ullrich
1010 96ccd009 Renato Botelho
	if ($tab_array_space == '') {
1011
		$tab_array_space = 1;
1012
	}
1013 620ac186 Scott Ullrich
1014 96ccd009 Renato Botelho
	if ($tab_array_char_limit == '') {
1015
		$tab_array_char_limit = 92;
1016
	}
1017 667b2b60 Ermal
1018 45b4ffc6 Phil Davis
	foreach ($tab_array as $tab_id => $ta) {
1019
		if (!isAllowedPage($ta[2])) {
1020 96ccd009 Renato Botelho
			unset ($tab_array[$tab_id]);
1021 45b4ffc6 Phil Davis
		}
1022 96ccd009 Renato Botelho
	}
1023 667b2b60 Ermal
1024 0074384f sbeaver
	$tab_active_bg	 = "#EEEEEE";
1025 96ccd009 Renato Botelho
	$tab_inactive_bg = "#777777";
1026
	$nifty_tabs_corners = "#FFF";
1027
	$font_color = "white";
1028
1029
	$tabcharcount = 0;
1030 aa82505e Phil Davis
	foreach ($tab_array as $ta) {
1031 96ccd009 Renato Botelho
		$tabcharcount = $tabcharcount + strlen($ta[0]);
1032 aa82505e Phil Davis
	}
1033 620ac186 Scott Ullrich
1034 ecffb0bb Phil Davis
	if ($no_drop_down == true) {
1035 96ccd009 Renato Botelho
		$tabcharcount = 0;
1036
		unset($tab_array_char_limit);
1037
	}
1038
1039
	// If the character count of the tab names is > 670
1040
	// then show a select item dropdown menubox.
1041 02e2825a Stephen Beaver
	if ($tabcharcount > $tab_array_char_limit) {
1042 e28307d0 Luiz Gustavo Costa
		echo gettext("Currently viewing: ");
1043 96ccd009 Renato Botelho
		echo "<select name=\"TabSelect\" onchange=\"tabs_will_go(this)\">\n";
1044 02e2825a Stephen Beaver
1045 96ccd009 Renato Botelho
		foreach ($tab_array as $ta) {
1046 aa82505e Phil Davis
			if ($ta[1] == "true") {
1047 c4b60a9a Colin Fleming
				$selected = " selected";
1048 aa82505e Phil Davis
			} else {
1049 96ccd009 Renato Botelho
				$selected = "";
1050 aa82505e Phil Davis
			}
1051 96ccd009 Renato Botelho
			// Onclick in option will not work in some browser
1052
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
1053
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
1054
		}
1055 02e2825a Stephen Beaver
1056 96ccd009 Renato Botelho
		echo "</select>\n<p>&nbsp;</p>";
1057
		echo "<script type=\"text/javascript\">";
1058
		echo "\n//<![CDATA[\n";
1059
		echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
1060
		echo "//]]>\n";
1061
		echo "</script>";
1062 02e2825a Stephen Beaver
	} else {
1063
		echo '<ul class="nav nav-' . $type . '">';
1064
1065
		foreach ($tab_array as $ta) {
1066
			echo '<li role="presentation"';
1067
			if ($ta[1]) {
1068
				echo ' class="active"';
1069
			}
1070
1071 e3947e77 Steve Beaver
			echo '><a href="' . $ta[2] . '" ' . $usepost . '>' . $ta[0] . '</a></li>';
1072 02e2825a Stephen Beaver
		}
1073
1074
		echo '</ul>';
1075
	}
1076 667b2b60 Ermal
}
1077
1078 a2b0d909 Renato Botelho
function add_package_tabs($tabgroup, &$tab_array) {
1079 96ccd009 Renato Botelho
	global $config, $g;
1080
1081 a2b0d909 Renato Botelho
	if (!isset($config['installedpackages']['package'])) {
1082 96ccd009 Renato Botelho
		return;
1083 45b4ffc6 Phil Davis
	}
1084 253b37d8 Renato Botelho
1085 aa82505e Phil Davis
	foreach ($config['installedpackages']['package'] as $pkg) {
1086 a2b0d909 Renato Botelho
		$pkg_config = read_package_configurationfile($pkg['name']);
1087 253b37d8 Renato Botelho
1088 a2b0d909 Renato Botelho
		if (!isset($pkg_config['tabs']['tab'])) {
1089
			continue;
1090
		}
1091 96ccd009 Renato Botelho
1092 a2b0d909 Renato Botelho
		foreach ($pkg_config['tabs']['tab'] as $tab) {
1093
			$tab_entry = array();
1094
			if ($tab['name']) {
1095
				$tab_entry[] = $tab['name'];
1096
				$tab_entry[] = false;
1097
				$tab_entry[] = $tab['url'];
1098
				$tab_array[] = $tab_entry;
1099
			}
1100 96ccd009 Renato Botelho
		}
1101
	}
1102 667b2b60 Ermal
}
1103
1104 45b4ffc6 Phil Davis
function alias_info_popup($alias_id) {
1105 667b2b60 Ermal
	global $config;
1106 3b2c83b8 Sjon Hortensius
1107 aa82505e Phil Davis
	if (!is_array($config['aliases']['alias'][$alias_id])) {
1108 3b2c83b8 Sjon Hortensius
		return;
1109 aa82505e Phil Davis
	}
1110 3b2c83b8 Sjon Hortensius
1111 4e8854c6 Charlie Root
	$maxlength = 60;
1112 3b2c83b8 Sjon Hortensius
	$alias = $config['aliases']['alias'][$alias_id];
1113
	$content = "";
1114
1115 aa82505e Phil Davis
	if ($alias['url']) {
1116 3b2c83b8 Sjon Hortensius
		// TODO: Change it when pf supports tables with ports
1117
		if ($alias['type'] == "urltable") {
1118
			exec("/sbin/pfctl -t {$alias['name']} -T show | wc -l", $total_entries);
1119 aa82505e Phil Davis
			$counter=preg_replace("/\D/", "", $total_entries[0]);
1120 3b2c83b8 Sjon Hortensius
			exec("/sbin/pfctl -t {$alias['name']} -T show | head -10002", $alias_addresses);
1121
		} else {
1122
			$urlfn = alias_expand_urltable($alias['name']);
1123
			$alias_addresses = explode("\n", file_get_contents($urlfn));
1124
			$counter = count($alias_addresses);
1125 4e8854c6 Charlie Root
		}
1126 aa82505e Phil Davis
1127 abb00412 heper
		$content .= '<h5>'. $alias['url'] .'</h5><ul><li>'. implode('</li><li>', $alias_addresses) .'</li></ul>';
1128 aa82505e Phil Davis
		if ($counter > 10002) {
1129 3b2c83b8 Sjon Hortensius
			$content .= '<i>'. gettext("listing only first 10k items") .'</i>';
1130 aa82505e Phil Davis
		}
1131
	} else {
1132 3b2c83b8 Sjon Hortensius
		$alias_addresses = explode (" ", $alias['address']);
1133
		$alias_details = explode ("||", $alias['detail']);
1134 8b53fe2f Stephen Beaver
		$idx = 0;
1135
1136
		$content .= "<table>\n";
1137
		$content .= "<thead>\n";
1138
		$content .= "<tr>\n";
1139
		$content .= "<th>" . gettext("Value") . "</th><th  style='padding-left: 10px;'>" . gettext("Description") . "</th></tr>\n";
1140
		$content .= "</thead>\n";
1141
		$content .= "<tbody>\n";
1142
1143
		foreach ($alias_addresses as $ap) {
1144
			$content .= "	<tr>\n";
1145
			$content .= "		<td>\n";
1146
			$content .= 			$ap;
1147
			$content .=	"		</td>\n";
1148
			$content .= "		<td style='padding-left: 10px;'>\n";
1149 f14ec73c Stephen Beaver
			$content .= 			htmlspecialchars($alias_details[$idx]);
1150 8b53fe2f Stephen Beaver
			$content .=	"		</td>\n";
1151
			$content .= "	</tr>\n";
1152
			$idx++;
1153
		}
1154 3b2c83b8 Sjon Hortensius
1155 8b53fe2f Stephen Beaver
		$content .= "</tbody>\n";
1156
		$content .= "<table>\n";
1157 3b2c83b8 Sjon Hortensius
	}
1158
1159 aa82505e Phil Davis
	if (strlen($alias['descr']) >= $maxlength) {
1160 3b2c83b8 Sjon Hortensius
		$alias['descr'] = substr($alias['descr'], 0, $maxlength) . '&hellip;';
1161 aa82505e Phil Davis
	}
1162 3b2c83b8 Sjon Hortensius
1163
	return $content;
1164 4e8854c6 Charlie Root
}
1165
1166 7e752d72 Phil Davis
function rule_columns_with_alias($src, $srcport, $dst, $dstport, $target="", $targetport="") {
1167 3b2c83b8 Sjon Hortensius
	global $config;
1168
1169 aa82505e Phil Davis
	if ($config['aliases']['alias'] == "" || !is_array($config['aliases']['alias'])) {
1170 3b2c83b8 Sjon Hortensius
		return;
1171 aa82505e Phil Davis
	}
1172 3b2c83b8 Sjon Hortensius
1173
	$columns = array();
1174 ecffb0bb Phil Davis
	foreach ($config['aliases']['alias'] as $alias_id => $alias_name) {
1175 aa82505e Phil Davis
		if ($alias_name['name'] == $src) {
1176 3b2c83b8 Sjon Hortensius
			$columns['src'] = $alias_id;
1177 aa82505e Phil Davis
		}
1178
		if ($alias_name['name'] == $srcport) {
1179 3b2c83b8 Sjon Hortensius
			$columns['srcport'] = $alias_id;
1180 aa82505e Phil Davis
		}
1181
		if ($alias_name['name'] == $dst) {
1182 3b2c83b8 Sjon Hortensius
			$columns['dst'] = $alias_id;
1183 aa82505e Phil Davis
		}
1184
		if ($alias_name['name'] == $dstport) {
1185 3b2c83b8 Sjon Hortensius
			$columns['dstport'] = $alias_id;
1186 aa82505e Phil Davis
		}
1187 474e70a2 Stephen Beaver
		if ($alias_name['name'] == $target) {
1188
			$columns['target'] = $alias_id;
1189
		}
1190
		if ($alias_name['name'] == $targetport) {
1191
			$columns['targetport'] = $alias_id;
1192
		}
1193 96ccd009 Renato Botelho
	}
1194 e27eac0c Stephen Beaver
1195
	return $columns;
1196 667b2b60 Ermal
}
1197
1198 ecffb0bb Phil Davis
function form_output_row($name, $label, $content) {
1199 7d5b007c Sjon Hortensius
var_dump($content);die;
1200
?>
1201
<div class="form-group">
1202
	<label for="<?=$name?>" class="col-sm-2 control-label"><?=gettext($label); ?></label>
1203
	<div class="col-sm-10">
1204
		<?=$content?>
1205
	</div>
1206
</div>
1207
<?php
1208
}
1209
1210 0b87fcf5 bruno
function set_flash_message($class, $msg) {
1211
	@session_start();
1212
	$_SESSION['flash_messages'][$class][] = $msg;
1213
}
1214
1215
function get_flash_message() {
1216
	@session_start();
1217
	if (isset($_SESSION['flash_messages']) && !empty($_SESSION['flash_messages'])) {
1218
		foreach ($_SESSION['flash_messages'] as $class => $flash_message) {
1219 0d711d38 Phil Davis
			print_info_box(implode("<br />", $flash_message), $class);
1220 0b87fcf5 bruno
		}
1221
		unset($_SESSION['flash_messages']);
1222
	}
1223
}
1224
1225 f9703596 NOYB
/* Retrieve GET or POST Value/State
1226
 * Eample Usage:
1227
 * $value = getGETPOSTsettingvalue('get/post parameter name', "");
1228
 * $value = getGETPOSTsettingvalue('get/post parameter name', null);
1229
 * $state = getGETPOSTsettingvalue('get/post parameter name', null);
1230
 * $state = getGETPOSTsettingvalue('get/post parameter name', false);
1231
 */
1232
function getGETPOSTsettingvalue($settingname, $default) {
1233
	$settingvalue = $default;
1234
	if ($_GET[$settingname]) {
1235
		$settingvalue = $_GET[$settingname];
1236
	}
1237
	if ($_POST[$settingname]) {
1238
		$settingvalue = $_POST[$settingname];
1239
	}
1240
	return $settingvalue;
1241
}
1242
1243 ecaca752 Renato Botelho
/* set timezone */
1244
if (isset($config['system']['timezone']) &&
1245
    !empty($config['system']['timezone'])) {
1246
	$timezone = $config['system']['timezone'];
1247
} elseif (isset($g['default_timezone']) && !empty($g['default_timezone'])) {
1248
	$timezone = $g['default_timezone'];
1249
} else {
1250 3b49bc25 Scott Ullrich
	$timezone = "Etc/UTC";
1251 45b4ffc6 Phil Davis
}
1252 3b49bc25 Scott Ullrich
1253
date_default_timezone_set($timezone);
1254
1255 667b2b60 Ermal
?>