guiconfig.inc 19.2 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4
	Copyright (C) 2015 Franco Fichtner <franco@opnsense.org>
5
	Copyright (C) 2014 Deciso B.V.
6
	Copyright (C) 2004 Scott Ullrich
Ad Schellevis's avatar
Ad Schellevis committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
	All rights reserved.

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

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

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

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

32
require_once("util.inc");
33
require_once("config.inc");
34

Ad Schellevis's avatar
Ad Schellevis committed
35
/* THIS MUST BE ABOVE ALL OTHER CODE */
36 37 38
if (empty($nocsrf)) {
	function csrf_startup()
	{
Ad Schellevis's avatar
Ad Schellevis committed
39 40 41 42
		csrf_conf('rewrite-js', '/csrf/csrf-magic.js');
		$timeout_minutes = isset($config['system']['webgui']['session_timeout']) ?  $config['system']['webgui']['session_timeout'] : 240;
		csrf_conf('expires', $timeout_minutes * 60);
	}
43
	require_once('csrf/csrf-magic.php');
44 45 46 47 48

        // make sure the session is closed after executing csrf-magic
        if (session_status() != PHP_SESSION_NONE) {
            session_write_close();
        }
49
}
Ad Schellevis's avatar
Ad Schellevis committed
50

51 52 53 54 55 56 57 58 59 60
function set_language($lang)
{
	$lang_encoding = $lang . '.UTF-8';
	$textdomain = 'OPNsense';

	putenv('LANG=' . $lang_encoding);
	setlocale(LC_ALL, $lang_encoding);
	textdomain($textdomain);
	bindtextdomain($textdomain, '/usr/local/share/locale');
	bind_textdomain_codeset($textdomain, $lang_encoding);
Ad Schellevis's avatar
Ad Schellevis committed
61 62
}

63 64 65 66 67 68 69 70 71
function get_current_theme()
{
	global $config;

	$theme = 'opnsense';

	if (isset($config['theme']) && is_dir('/usr/local/opnsense/www/themes/' . $config['theme'])) {
		$theme = $config['theme'];
	}
72

73 74
	return $theme;
}
75

Ad Schellevis's avatar
Ad Schellevis committed
76
/* make sure nothing is cached */
77
if (isset($omit_nocacheheaders) && $omit_nocacheheaders) {
Ad Schellevis's avatar
Ad Schellevis committed
78 79 80 81 82 83 84 85 86 87 88 89
	header("Expires: 0");
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");
}

header("X-Frame-Options: SAMEORIGIN");

$g['theme'] = get_current_theme();

/* Set the default interface language */
90
if(isset($config['system']['language']) && $config['system']['language'] <> "") {
Ad Schellevis's avatar
Ad Schellevis committed
91
	$g['language'] = $config['system']['language'];
92
} elseif (!isset($g['language']) || $g['language'] == "") {
Ad Schellevis's avatar
Ad Schellevis committed
93 94 95 96 97
	$g['language'] = 'en_US';
}

set_language($g['language']);

98 99
require_once("authgui.inc");

Ad Schellevis's avatar
Ad Schellevis committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/* Reserved table names to avoid colision */
$reserved_table_names = array(
        "bogons",
        "bogonsv6",
        "negate_networks",
        "sshlockout",
        "tonatsubnets",
        "virusprot",
        "vpn_networks",
        "webConfiguratorlockout"
);


$netbios_nodetypes = array(
	'0' => "none",
	'1' => "b-node",
	'2' => "p-node",
	'4' => "m-node",
	'5' => "h-node");

/* some well knows ports */
$wkports = array(
	5999 => "CVSup",
	53 => "DNS",
	21 => "FTP",
	3000 => "HBCI",
	80 => "HTTP",
	443 => "HTTPS",
	5190 => "ICQ",
	113 => "IDENT/AUTH",
	143 => "IMAP",
	993 => "IMAP/S",
	4500 => "IPsec NAT-T",
	500 => "ISAKMP",
	1701 => "L2TP",
	389 => "LDAP",
	1755 => "MMS/TCP",
	7000 => "MMS/UDP",
	445 => "MS DS",
	3389 => "MS RDP",
	1512 => "MS WINS",
	1863 => "MSN",
	119 => "NNTP",
	123 => "NTP",
	138 => "NetBIOS-DGM",
	137 => "NetBIOS-NS",
	139 => "NetBIOS-SSN",
	1194 => "OpenVPN",
	110 => "POP3",
	995 => "POP3/S",
	1723 => "PPTP",
	1812 => "RADIUS",
	1813 => "RADIUS accounting",
	5004 => "RTP",
	5060 => "SIP",
	25 => "SMTP",
	465 => "SMTP/S",
	161 => "SNMP",
	162 => "SNMP-Trap",
	22 => "SSH",
	3478 => "STUN",
	587 => "SUBMISSION",
	3544 => "Teredo",
	23 => "Telnet",
	69 => "TFTP",
	5900 => "VNC");

167 168 169 170 171
$wlan_modes = array(
	'bss' => 'Infrastructure (BSS)',
	'adhoc' => 'Ad-hoc (IBSS)',
	'hostap' => 'Access Point'
);
Ad Schellevis's avatar
Ad Schellevis committed
172

173 174
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, &$input_errors)
{
Ad Schellevis's avatar
Ad Schellevis committed
175 176 177 178 179 180 181 182
	/* check for bad control characters */
	foreach ($postdata as $pn => $pd) {
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
			$input_errors[] = sprintf(gettext("The field %s contains invalid characters."), $pn);
		}
	}

	for ($i = 0; $i < count($reqdfields); $i++) {
183 184
		// why.. do you want to do this? we send data to validate and then validate something else....
		// can't now for sure if any page uses this wrong behaviour, but it should die eventually.
185
		// TODO: kill
186
		if (empty($_POST[$reqdfields[$i]]) && empty($_REQUEST[$reqdfields[$i]])) {
Ad Schellevis's avatar
Ad Schellevis committed
187 188 189 190 191
			$input_errors[] = sprintf(gettext("The field %s is required."), $reqdfieldsn[$i]);
		}
	}
}

192 193 194
function print_input_errors($input_errors)
{
	echo '<div class="col-xs-12"><div class="alert alert-danger" role="alert">
195 196
            <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
	';
197 198

	echo "<p>" . gettext("The following input errors were detected:") . "</p>\n<ul>";
Ad Schellevis's avatar
Ad Schellevis committed
199 200 201
	foreach ($input_errors as $ierr) {
		echo "<li>" . htmlspecialchars($ierr) . "</li>";
	}
202
	echo "</ul></div></div>";
Ad Schellevis's avatar
Ad Schellevis committed
203 204
}

205 206 207 208 209 210 211 212 213 214 215 216
function print_alert_box($msg, $alert = 'warning', $button = '')
{
	echo <<<EOFnp
<div class="col-xs-12">
	<div class="alert alert-{$alert}" role="alert">
		{$button}
		<p>{$msg}</p>
	</div>
</div>
EOFnp;
}

217 218 219 220 221 222
function print_info_box_apply($msg)
{
	$value = gettext('Apply changes');
	$name= 'apply';

	$savebutton = '<form action="' . $_SERVER['REQUEST_URI'] . '" method="post">';
223
	$savebutton .= '<input name="' . $name . '" type="submit" class="btn btn-primary pull-right" id="' . $name . '" value="' . $value . '" />';
224
	if (!empty($_POST['if'])) {
225
		$savebutton .= '<input type="hidden" name="if" value="' . htmlspecialchars($_POST['if']) . '" />';
226 227
	} elseif (!empty($_GET['if'])) {
		$savebutton .= '<input type="hidden" name="if" value="' . htmlspecialchars($_GET['if']) . '" />';
228 229 230
	}
	$savebutton .= '</form>';

231
	print_alert_box($msg, 'info', $savebutton);
232 233
}

234
function print_info_box($msg)
235
{
236
	print_alert_box($msg, 'info');
Ad Schellevis's avatar
Ad Schellevis committed
237 238
}

239 240
function print_service_banner($service)
{
241 242 243 244
	global $config;

	switch ($service) {
		case 'firewall':
245
			if (!isset($config['system']['disablefilter'])) {
246
				break;
247 248 249 250 251 252 253 254 255 256 257 258 259
			}
			print_alert_box(sprintf(
				gettext(
					'The firewall has globally been disabled and configured rules are ' .
					'currently not enforced. It can be enabled in the %sFirewall/NAT%s ' .
					'settings.'
				),
				'<a href="/system_advanced_firewall.php">',
				'</a>'
			));
			break;
		default:
			break;
260 261 262
	}
}

263
function get_std_save_message() {
Ad Schellevis's avatar
Ad Schellevis committed
264 265 266 267 268 269 270 271 272 273 274 275 276
	global $d_sysrebootreqd_path;
	$filter_related = false;
	$filter_pages = array("nat", "filter");
	$to_return = gettext("The changes have been applied successfully.");
	foreach($filter_pages as $fp)
		if(stristr($_SERVER['SCRIPT_FILENAME'], $fp))
			$filter_related = true;
	if($filter_related)
		$to_return .= "<br />" . gettext("You can also <a href=\"status_filter_reload.php\">monitor</a> the filter reload progress.");
	return $to_return;
}

function pprint_address($adr) {
277 278 279 280 281 282 283 284 285 286
	if (!isset($specialnets)) {
		global $specialnets;
		$specialnets = array("(self)" => "This Firewall", "pptp" => "PPTP clients", "pppoe" => "PPPoE clients", "l2tp" => "L2TP clients");

		$spiflist = get_configured_interface_with_descr(false, true);
		foreach ($spiflist as $ifgui => $ifdesc) {
			$specialnets[$ifgui] = $ifdesc . " net";
			$specialnets[$ifgui . 'ip'] = $ifdesc . " address";
		}
	}
Ad Schellevis's avatar
Ad Schellevis committed
287 288 289

	if (isset($adr['any'])) {
		$padr = "*";
290
	} else if (isset($adr['network'])) {
Ad Schellevis's avatar
Ad Schellevis committed
291 292
		$padr = $specialnets[$adr['network']];
	} else {
293
		$padr = isset($adr['address']) ? $adr['address'] : null;
Ad Schellevis's avatar
Ad Schellevis committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
	}

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

	return $padr;
}

function pprint_port($port) {
	global $wkports;

	$pport = "";

	if (!$port)
		return "*";
	else {
		$srcport = explode("-", $port);
311
		if ((empty($srcport[1])) || ($srcport[0] == $srcport[1])) {
Ad Schellevis's avatar
Ad Schellevis committed
312
			$pport = $srcport[0];
313
			if (!empty($wkports[$srcport[0]])) {
Ad Schellevis's avatar
Ad Schellevis committed
314 315 316 317 318 319 320 321 322
				$pport .= " (" . $wkports[$srcport[0]] . ")";
			}
		} else
			$pport .= $srcport[0] . " - " . $srcport[1];
	}

	return $pport;
}

323
function gentitle($breadcrumbs, $navlevelsep = ': ')
324 325 326 327 328 329 330 331 332 333 334
{
	$output = $breadcrumbs;

	if (isset($breadcrumbs[0]['name'])) {
		$output = array();
		foreach ($breadcrumbs as $crumb) {
			$output[] = gettext($crumb['name']);
		}
	}

	return join($navlevelsep, $output);
Ad Schellevis's avatar
Ad Schellevis committed
335 336
}

337
function clear_log($logfile, $restart_syslogd = true)
338
{
339 340
	if ($restart_syslogd) {
		killbyname('syslogd');
Ad Schellevis's avatar
Ad Schellevis committed
341
	}
342 343 344 345 346 347

	foreach (glob($logfile . '.*') as $rotated) {
		@unlink($rotated);
	}

	/* preserve file ownership and permissions */
348 349 350 351 352 353
	if (file_exists($logfile)) {
		$handle = fopen($logfile, 'r+');
		if ($handle) {
			ftruncate($handle, 0);
			fclose($handle);
		}
354 355
	}

356
	if ($restart_syslogd) {
Ad Schellevis's avatar
Ad Schellevis committed
357
		system_syslogd_start();
358
	}
Ad Schellevis's avatar
Ad Schellevis committed
359 360
}

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
function clear_clog($logfile, $restart_syslogd = true)
{
	global $config;

	if ($restart_syslogd) {
		killbyname('syslogd');
	}

	$log_size = isset($config['syslog']['logfilesize']) ? $config['syslog']['logfilesize'] : '511488';
	mwexecf('/usr/local/sbin/clog -i -s %s %s', array($log_size, $logfile));

	if ($restart_syslogd) {
		system_syslogd_start();
	}
}

377
function print_dump($logarr)
378 379 380
{
	global $config;

381 382 383 384 385 386 387
	if (!is_array($logarr)) {
		echo "<tr valign=\"top\">\n";
		echo "<td class=\"listlr\" colspan=\"2\">" . htmlspecialchars($logarr) . "</td>\n";
		echo "</tr>\n";
		return;
	}

388 389 390
	foreach ($logarr as $logent) {
		$logent = preg_split("/\s+/", $logent, 6);
		echo "<tr valign=\"top\">\n";
391 392 393 394 395
		$entry_date_time = htmlspecialchars(join(" ", array_slice($logent, 0, 3)));
		$entry_text = ($logent[3] ==  $config['system']['hostname']) ? "" : $logent[3] . " ";
		$entry_text .= htmlspecialchars($logent[4] . " " . $logent[5]);
		echo "<td class=\"listlr nowrap\">{$entry_date_time}</td>\n";
		echo "<td class=\"listr\">{$entry_text}</td>\n";
396 397 398 399
		echo "</tr>\n";
	}
}

400
function dump_clog($logfile, $tail, $grepfor = '')
401 402 403 404
{
	global $config;

	$sor = isset($config['syslog']['reverse']) ? '-r' : '';
405 406
	$logarr = array();
	$grepline = '';
407

408
	$grepfor = preg_split('/\s+/', trim($grepfor));
409

410 411
	foreach ($grepfor as $pattern) {
		$grepline .= ' | /usr/bin/egrep -i ' . escapeshellarg($pattern);
412 413
	}

Ad Schellevis's avatar
Ad Schellevis committed
414
	if (is_dir($logfile)) {
415
		$logarr = sprintf(gettext('File %s is a directory.'), $logfile);
416
	} elseif (!file_exists($logfile)) {
417
		$logarr = sprintf(gettext('File %s doesn\'t exist.'), $logfile);
Ad Schellevis's avatar
Ad Schellevis committed
418
	} else {
419
		exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . " {$grepline}| grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
Ad Schellevis's avatar
Ad Schellevis committed
420
	}
421

422 423
	if (is_array($logarr) && !count($logarr)) {
		$logarr = sprintf(gettext('File %s yielded no results.'), $logfile);
424 425
	}

426
	print_dump($logarr);
Ad Schellevis's avatar
Ad Schellevis committed
427 428
}

429
function dump_log($logfile, $tail, $grepfor = '')
430 431 432 433
{
	global $config;

	$sor = isset($config['syslog']['reverse']) ? '-r' : '';
434 435
	$logarr = array();
	$grepline = '';
436

437
	$grepfor = preg_split('/\s+/', trim($grepfor));
438

439 440
	foreach ($grepfor as $pattern) {
		$grepline .= ' | /usr/bin/egrep -i ' . escapeshellarg($pattern);
441 442 443
	}

	if (is_dir($logfile)) {
444
		$logarr = sprintf(gettext('File %s is a directory.'), $logfile);
445
	} elseif (!file_exists($logfile)) {
446
		$logarr = sprintf(gettext('File %s doesn\'t exist.'), $logfile);
447
	} else {
448
		exec("cat " . escapeshellarg($logfile) . " {$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
449 450
	}

451 452
	if (is_array($logarr) && !count($logarr)) {
		$logarr = sprintf(gettext('File %s yielded no results.'), $logfile);
453 454
	}

455
	print_dump($logarr);
456
}
Ad Schellevis's avatar
Ad Schellevis committed
457 458 459 460 461 462 463

/* Check if variable has changed, update and log if it has
 * returns true if var changed
 * varname = variable name in plain text
 * orig = original value
 * new = new value
 */
464 465
function update_if_changed($varname, &$orig, $new)
{
Ad Schellevis's avatar
Ad Schellevis committed
466 467 468 469 470
	if (is_array($orig) && is_array($new)) {
		$a_diff = array_diff($orig, $new);
		$a_diff = array_diff($new, $orig);
		$orig = $new;
		return true;
471
	}
Ad Schellevis's avatar
Ad Schellevis committed
472

473 474 475
	if ($orig != $new) {
		$orig = $new;
		return true;
Ad Schellevis's avatar
Ad Schellevis committed
476
	}
477

Ad Schellevis's avatar
Ad Schellevis committed
478 479 480 481 482 483
	return false;
}

function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport) {
	if (isset($adr['any']))
		$padr = "any";
484
	else if (isset($adr['network']))
Ad Schellevis's avatar
Ad Schellevis committed
485
		$padr = $adr['network'];
486
	else if (isset($adr['address'])) {
487 488 489 490
		if (strpos($adr['address'], '/') !== false) {
			list($padr, $pmask) = explode("/", $adr['address']);
		} else {
			$padr = $adr['address'];
Ad Schellevis's avatar
Ad Schellevis committed
491 492 493 494 495
			if (is_ipaddrv6($padr))
				$pmask = 128;
			else
				$pmask = 32;
		}
496

Ad Schellevis's avatar
Ad Schellevis committed
497 498 499 500 501 502 503
	}

	if (isset($adr['not']))
		$pnot = 1;
	else
		$pnot = 0;

504
	if (isset($adr['port'])) {
505
		if (strpos($adr['port'], '-') !== false) {
506 507 508
			list($pbeginport, $pendport) = explode("-", $adr['port']);
		} else {
			$pbeginport = $adr['port'];
Ad Schellevis's avatar
Ad Schellevis committed
509
			$pendport = $pbeginport;
510
		}
Ad Schellevis's avatar
Ad Schellevis committed
511 512 513 514 515 516 517
	} else if (!is_alias($pbeginport) && !is_alias($pendport)) {
		$pbeginport = "any";
		$pendport = "any";
	}
}

function pconfig_to_address(&$adr, $padr, $pmask, $pnot=false, $pbeginport=0, $pendport=0) {
518 519 520 521 522 523 524
		$adr = array();
		if ($padr == "any") {
				$adr['any'] = true;
		} elseif (is_specialnet($padr)) {
				$adr['network'] = $padr;
		} elseif (is_alias($padr)) {
				$adr['address'] = $padr;
Ad Schellevis's avatar
Ad Schellevis committed
525
		} else {
526 527 528 529 530 531 532 533
			$adr['address'] = $padr;
			if (is_ipaddrv6($padr)) {
				if ($pmask != 128)
					$adr['address'] .= "/" . $pmask;
			} else {
				if ($pmask != 32)
					$adr['address'] .= "/" . $pmask;
			}
Ad Schellevis's avatar
Ad Schellevis committed
534 535
		}

536 537 538 539 540
		if ($pnot) {
				$adr['not'] = true;
		} elseif (isset($adr['not'])) {
				unset($adr['not']);
		}
Ad Schellevis's avatar
Ad Schellevis committed
541

542 543 544 545 546 547 548 549 550
		if(is_alias($pbeginport)) {
				$adr['port'] = $pbeginport;
		} elseif (($pbeginport != 0) && ($pbeginport != "any")) {
				if ($pbeginport != $pendport) {
						$adr['port'] = $pbeginport . "-" . $pendport;
				} else {
						$adr['port'] = $pbeginport;
				}
		}
Ad Schellevis's avatar
Ad Schellevis committed
551 552 553 554 555 556 557
}

function is_specialnet($net) {
	global $specialsrcdst;

	if(!$net)
		return false;
558
	if (is_array($specialsrcdst) && in_array($net, $specialsrcdst))
Ad Schellevis's avatar
Ad Schellevis committed
559 560 561 562 563 564 565
		return true;
	else
		return false;
}



566
/****f* guiconfig/display_top_tabs
Ad Schellevis's avatar
Ad Schellevis committed
567 568 569 570 571 572 573
 * NAME
 *   display_top_tabs - display tabs with rounded edges
 * INPUTS
 *   $text      - array of tabs
 * RESULT
 *   null
 ******/
574 575
function display_top_tabs(& $tab_array, $no_drop_down = false)
{
Ad Schellevis's avatar
Ad Schellevis committed
576
	global $config;
577

578
	$tab_array_char_limit = 92;
Ad Schellevis's avatar
Ad Schellevis committed
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601

	/*  does the user have access to this tab?
	 *  master user has access to everything.
	 *  if the user does not have access, simply
	 *  unset the tab item.
	 */
	foreach ($tab_array as $tab_id => $ta){
		if(!isAllowedPage($ta[2]))
			unset ($tab_array[$tab_id]);
	}

	$tabcharcount = 0;
	foreach ($tab_array as $ta)
		$tabcharcount = $tabcharcount + strlen($ta[0]);

	if($no_drop_down == true) {
		$tabcharcount = 0;
		unset($tab_array_char_limit);
	}

	// If the character count of the tab names is > 670
	// then show a select item dropdown menubox.
	if($tabcharcount > $tab_array_char_limit) {
602
		echo "<h2>".gettext("Currently viewing: ")."</h2>";
603
		echo "<select class=\"selectpicker\" name=\"TabSelect\" onchange=\"tabs_will_go(this)\" data-live-search=\"true\" data-style=\"btn-default\">\n";
Ad Schellevis's avatar
Ad Schellevis committed
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
		foreach ($tab_array as $ta) {
			if($ta[1]=="true")
				$selected = " selected=\"selected\"";
			else
				$selected = "";
			// Onclick in option will not work in some browser
			// echo "<option onclick=\"document.location='{$ta[2]}';\"{$selected}>{$ta['0']}</option>\n";
			echo "<option value=\"{$ta[2]}\"{$selected}>{$ta['0']}</option>\n";
		}
		echo "</select>\n<p>&nbsp;</p>";
		echo "<script type=\"text/javascript\">";
		echo "\n//<![CDATA[\n";
		echo " function tabs_will_go(obj){ document.location = obj.value; }\n";
		echo "//]]>\n";
		echo "</script>";
	}  else {
		echo "<!-- Tabbed bar code-->\n";
621
		echo "<ul class=\"nav nav-tabs nav-justified\" role=\"tablist\">\n";
Ad Schellevis's avatar
Ad Schellevis committed
622 623 624
		$tabscounter = 0;
		foreach ($tab_array as $ta) {
			if ($ta[1] == true) {
625
				echo "  <li class=\"active\"><a href=\"{$ta[2]}\" role=\"tab\">{$ta[0]}</a></li>\n";
Ad Schellevis's avatar
Ad Schellevis committed
626
			} else {
627
				echo "  <li><a href=\"{$ta[2]}\" role=\"tab\">{$ta[0]}</a></li>\n";
Ad Schellevis's avatar
Ad Schellevis committed
628 629 630
			}
			$tabscounter++;
		}
631
		echo "</ul>\n";
Ad Schellevis's avatar
Ad Schellevis committed
632 633 634 635
	}
}

$timezone = $config['system']['timezone'];
636 637 638
if (!$timezone) {
	$timezone = 'Etc/UTC';
}
Ad Schellevis's avatar
Ad Schellevis committed
639 640

date_default_timezone_set($timezone);
641 642 643

function get_crash_report($pedantic = false)
{
644 645 646 647 648 649
	$savemsg = sprintf(
		gettext('A problem was detected. Click %shere%s for more information.'),
		'<a href="/crash_reporter.php">',
		'</a>'
	);
	$skip_files = array('.', '..', 'minfree', 'bounds', '');
650 651
	$PHP_errors_log = '/tmp/PHP_errors.log';
	$excludes = array();
652 653
	$count = 0;

654 655 656 657 658 659 660 661 662
	if (!$pedantic) {
		$excludes[] = 'PHP Notice:';
		$excludes[] = 'PHP Startup: Unable to load dynamic library \'/usr/local/lib/php/20131226/suhosin.so\' - /usr/local/lib/php/20131226/suhosin.so: Undefined symbol "ps_globals" in Unknown on line 0';
	}

	if (file_exists($PHP_errors_log)) {
		$extra = '';
		foreach ($excludes as $exclude) {
			$extra .= sprintf('/usr/bin/grep -v %s | ', escapeshellarg($exclude));
663
		}
664 665 666 667 668
		$total = shell_exec(sprintf(
			'/bin/cat %s | %s /usr/bin/wc -l | /usr/bin/awk \'{ print $1 }\'',
			$PHP_errors_log,
			$extra
		));
669
		if ($total > 0) {
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
			$count++;
		}
	}

	$crashes = glob('/var/crash/*');
	foreach ($crashes as $crash) {
		if (!in_array(basename($crash), $skip_files)) {
			$count++;
		}
	}

	if (!$count) {
		$savemsg = '';
	}

	return $savemsg;
}
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741

function get_menu_messages()
{
	global $config;

	$menu_messages = '';
	if (are_notices_pending()) {
		$notices = get_notices();
		$requests = array();

		## Get Query Arguments from URL ###
		foreach ($_REQUEST as $key => $value) {
			if ($key != "PHPSESSID") {
				$requests[] = $key.'='.$value;
			}
		}

		if (is_array($requests)) {
			$request_string = implode("&", $requests);
		}

		if (is_array($notices)) {
			$notice_msgs = "<ul class=\"dropdown-menu\" role=\"menu\">";

			$notice_msgs .= "<li><a href=\"#\" onclick=\"notice_action('acknowledge','all');\" >".gettext("Acknowledge All Notices")."</a></li><li class=\"divider\"></li>";

			foreach ($notices as $key => $value) {
				$date = date("m-d-y H:i:s", $key);
				$noticemsg = ($value['notice'] != "" ? $value['notice'] : $value['id']);
				$noticemsg = preg_replace("/(\"|\'|\n|<.?\w+>)/i","",$noticemsg);
				$alert_action ="onclick=\"notice_action('acknowledge','{$key}'); jQuery(this).parent().parent().remove();\"";
				$notice_msgs .= "<li><a href=\"#\"  {$alert_action}>{$date} [ ".htmlspecialchars($noticemsg)."]</a></li>";
			}

			$notice_msgs .="</ul>";

			if (count($notices) == 1) {
				$msg= sprintf("%1$02d",count($notices))." ".gettext("unread notice");
			} else {
				$msg= sprintf("%1$02d",count($notices))." ".gettext("unread notices");
			}

			$menu_messages.="<a href=\"/\" class=\"dropdown-toggle \" data-toggle=\"dropdown\" role=\"button\" aria-expanded=\"false\"><span class=\"text-primary\">{$msg}&nbsp;</span><span class=\"caret text-primary\"></span></a>{$notice_msgs}\n";
		}
	} else {
		$menu_messages .= sprintf(
			'<a href="#">%s@%s.%s</a>',
			$_SESSION['Username'],
			$config['system']['hostname'],
			$config['system']['domain']
		);
	}

	return ($menu_messages);
}