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

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    Copyright (C) 2015-2016 Franco Fichtner <franco@opnsense.org>
    Copyright (C) 2014 Deciso B.V.
    Copyright (C) 2004 Scott Ullrich
    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.
Ad Schellevis's avatar
Ad Schellevis committed
30
*/
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
if (empty($nocsrf)) {
37 38 39 40 41 42 43 44 45 46 47 48
    function csrf_startup()
    {
      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);
    }
    require_once('csrf/csrf-magic.php');

    // 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
function set_language()
52
{
53
    global $config;
54

55 56 57 58
    $lang = 'en_US';
    if (!empty($config['system']['language'])) {
        $lang = $config['system']['language'];
    }
59

60 61
    $lang_encoding = $lang . '.UTF-8';
    $textdomain = 'OPNsense';
62

63 64 65 66 67
    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
68 69
}

70 71
function get_current_theme()
{
72
    global $config;
73

74
    $theme = 'opnsense';
75

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

80
    return $theme;
81
}
82

83 84
function html_safe($text)
{
85 86
    /* gettext() embedded in JavaScript can cause syntax errors */
    return htmlspecialchars($text, ENT_QUOTES | ENT_HTML401);
87 88
}

Ad Schellevis's avatar
Ad Schellevis committed
89
/* make sure nothing is cached */
90
if (isset($omit_nocacheheaders) && $omit_nocacheheaders) {
91 92 93 94 95
    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");
Ad Schellevis's avatar
Ad Schellevis committed
96 97 98 99
}

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

100
set_language();
Ad Schellevis's avatar
Ad Schellevis committed
101

102 103
require_once("authgui.inc");

Ad Schellevis's avatar
Ad Schellevis committed
104 105
/* Reserved table names to avoid colision */
$reserved_table_names = array(
106 107 108 109 110 111 112 113
    "bogons",
    "bogonsv6",
    "negate_networks",
    "sshlockout",
    "tonatsubnets",
    "virusprot",
    "vpn_networks",
    "webConfiguratorlockout"
Ad Schellevis's avatar
Ad Schellevis committed
114 115 116 117
);


$netbios_nodetypes = array(
118 119 120 121 122
    '0' => "none",
    '1' => "b-node",
    '2' => "p-node",
    '4' => "m-node",
    '5' => "h-node");
Ad Schellevis's avatar
Ad Schellevis committed
123 124 125

/* some well knows ports */
$wkports = array(
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 167 168 169
    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");
Ad Schellevis's avatar
Ad Schellevis committed
170

171
$wlan_modes = array(
172 173 174
    'bss' => 'Infrastructure (BSS)',
    'adhoc' => 'Ad-hoc (IBSS)',
    'hostap' => 'Access Point'
175
);
Ad Schellevis's avatar
Ad Schellevis committed
176

177 178
function do_input_validation($postdata, $reqdfields, $reqdfieldsn, &$input_errors)
{
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    /* 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++) {
        // 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.
        // TODO: kill
        if (empty($_POST[$reqdfields[$i]]) && empty($_REQUEST[$reqdfields[$i]])) {
            $input_errors[] = sprintf(gettext("The field %s is required."), $reqdfieldsn[$i]);
        }
    }
Ad Schellevis's avatar
Ad Schellevis committed
194 195
}

196 197
function print_input_errors($input_errors)
{
198 199 200 201 202 203 204 205 206
    echo '<div class="col-xs-12"><div class="alert alert-danger" role="alert">
          <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
    ';

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

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

221 222
function print_content_box($msg)
{
223
    echo <<<EOFnp
224 225 226 227 228 229 230 231
<div class="table-responsive">
  <table class="table table-striped">
     <tr><td>{$msg}</td></tr>
  </table>
</div>
EOFnp;
}

232 233
function print_info_box_apply($msg)
{
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    $iface = !empty($_POST['if']) ? $_POST['if'] : (!empty($_GET['if']) ? $_GET['if'] : '');
    $label = gettext('Apply changes');
    $value = 'Apply changes';
    $name= 'apply';

    $savebutton = sprintf('<form action="' . $_SERVER['REQUEST_URI'] . '" method="post">', $_SERVER['REQUEST_URI']);
    $savebutton .= sprintf(
        '<button type="submit" name="%s" id="%s" class="btn btn-primary pull-right" value="%s">%s</button>',
        $name,
        $name,
        $value,
        $label
    );
    if (!empty($iface)) {
        $savebutton .= sprintf(
            '<input type="hidden" name="if" value="%s"/>',
            htmlspecialchars($iface)
        );
    }
    $savebutton .= '</form>';

    print_alert_box($msg, 'info', $savebutton);
256 257
}

258
function print_info_box($msg)
259
{
260
    print_alert_box($msg, 'info');
Ad Schellevis's avatar
Ad Schellevis committed
261 262
}

263 264
function print_service_banner($service)
{
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    global $config;

    switch ($service) {
        case 'firewall':
            if (!isset($config['system']['disablefilter'])) {
                break;
            }
            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;
    }
285 286
}

287 288
function get_std_save_message()
{
289 290 291 292 293 294 295 296 297 298 299 300
    $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;
Ad Schellevis's avatar
Ad Schellevis committed
301 302 303
}

function pprint_address($adr) {
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    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";
        }
    }

    if (isset($adr['any'])) {
        $padr = "*";
    } elseif (isset($adr['network'])) {
        $padr = $specialnets[$adr['network']];
    } else {
        $padr = isset($adr['address']) ? $adr['address'] : null;
    }

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

    return $padr;
Ad Schellevis's avatar
Ad Schellevis committed
328 329 330
}

function pprint_port($port) {
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    global $wkports;

    $pport = "";

    if (!$port) {
        return "*";
    } else {
        $srcport = explode("-", $port);
        if ((empty($srcport[1])) || ($srcport[0] == $srcport[1])) {
            $pport = $srcport[0];
            if (!empty($wkports[$srcport[0]])) {
                $pport .= " (" . $wkports[$srcport[0]] . ")";
            }
        } else {
            $pport .= $srcport[0] . " - " . $srcport[1];
        }
    }

    return $pport;
Ad Schellevis's avatar
Ad Schellevis committed
350 351
}

352
function gentitle($breadcrumbs, $navlevelsep = ': ')
353
{
354
    $output = $breadcrumbs;
355

356 357 358 359 360 361
    if (isset($breadcrumbs[0]['name'])) {
        $output = array();
        foreach ($breadcrumbs as $crumb) {
            $output[] = gettext($crumb['name']);
        }
    }
362

363
    return join($navlevelsep, $output);
Ad Schellevis's avatar
Ad Schellevis committed
364 365
}

366
function clear_log($logfile, $restart_syslogd = true)
367
{
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
    if ($restart_syslogd) {
        killbyname('syslogd');
    }

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

    /* preserve file ownership and permissions */
    if (file_exists($logfile)) {
        $handle = fopen($logfile, 'r+');
        if ($handle) {
            ftruncate($handle, 0);
            fclose($handle);
        }
    }

    if ($restart_syslogd) {
        system_syslogd_start();
    }
Ad Schellevis's avatar
Ad Schellevis committed
388 389
}

390 391
function clear_clog($logfile, $restart_syslogd = true)
{
392
    global $config;
393

394 395 396
    if ($restart_syslogd) {
        killbyname('syslogd');
    }
397

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

401 402 403
    if ($restart_syslogd) {
        system_syslogd_start();
    }
404 405
}

406
function print_dump($logarr)
407
{
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    global $config;

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

    foreach ($logarr as $logent) {
        $logent = preg_split("/\s+/", $logent, 6);
        echo "<tr valign=\"top\">\n";
        $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";
        echo "</tr>\n";
    }
427 428
}

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

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

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

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

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

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

455
    print_dump($logarr);
Ad Schellevis's avatar
Ad Schellevis committed
456 457
}

458
function dump_log($logfile, $tail, $grepfor = '')
459
{
460
    global $config;
461

462 463 464
    $sor = isset($config['syslog']['reverse']) ? '-r' : '';
    $logarr = array();
    $grepline = '';
465

466
    $grepfor = preg_split('/\s+/', trim($grepfor));
467

468 469 470
    foreach ($grepfor as $pattern) {
        $grepline .= ' | /usr/bin/egrep -i ' . escapeshellarg($pattern);
    }
471

472 473 474 475 476 477 478
    if (is_dir($logfile)) {
        $logarr = sprintf(gettext('File %s is a directory.'), $logfile);
    } elseif (!file_exists($logfile)) {
        $logarr = sprintf(gettext('File %s doesn\'t exist.'), $logfile);
    } else {
        exec("cat " . escapeshellarg($logfile) . " {$grepline} | /usr/bin/tail {$sor} -n " . escapeshellarg($tail), $logarr);
    }
479

480 481 482
    if (is_array($logarr) && !count($logarr)) {
        $logarr = sprintf(gettext('File %s yielded no results.'), $logfile);
    }
483

484
    print_dump($logarr);
485
}
Ad Schellevis's avatar
Ad Schellevis committed
486

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
function address_to_pconfig($adr, &$padr, &$pmask, &$pnot, &$pbeginport, &$pendport)
{
    if (isset($adr['any'])) {
        $padr = "any";
    } elseif (isset($adr['network'])) {
        $padr = $adr['network'];
    } elseif (isset($adr['address'])) {
        if (strpos($adr['address'], '/') !== false) {
            list($padr, $pmask) = explode("/", $adr['address']);
        } else {
            $padr = $adr['address'];
            if (is_ipaddrv6($padr)) {
                $pmask = 128;
            } else {
                $pmask = 32;
            }
        }
    }

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

    if (isset($adr['port'])) {
        if (strpos($adr['port'], '-') !== false) {
            list($pbeginport, $pendport) = explode("-", $adr['port']);
        } else {
            $pbeginport = $adr['port'];
            $pendport = $pbeginport;
        }
    } elseif (!is_alias($pbeginport) && !is_alias($pendport)) {
        $pbeginport = "any";
        $pendport = "any";
    }
Ad Schellevis's avatar
Ad Schellevis committed
523 524
}

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
function pconfig_to_address(&$adr, $padr, $pmask, $pnot=false, $pbeginport=0, $pendport=0)
{
    $adr = array();
    if ($padr == "any") {
        $adr['any'] = true;
    } elseif (is_specialnet($padr)) {
        $adr['network'] = $padr;
    } elseif (is_alias($padr)) {
        $adr['address'] = $padr;
    } else {
        $adr['address'] = $padr;
        if (is_ipaddrv6($padr)) {
            if ($pmask != 128) {
                $adr['address'] .= "/" . $pmask;
            }
        } else {
            if ($pmask != 32) {
                $adr['address'] .= "/" . $pmask;
            }
        }
    }

    if ($pnot) {
        $adr['not'] = true;
    } elseif (isset($adr['not'])) {
        unset($adr['not']);
    }

    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
562 563
}

564 565 566 567 568 569 570 571 572 573 574 575
function is_specialnet($net)
{
    global $specialsrcdst;

    if (!$net) {
        return false;
    }
    if (is_array($specialsrcdst) && in_array($net, $specialsrcdst)) {
        return true;
    } else {
        return false;
    }
Ad Schellevis's avatar
Ad Schellevis committed
576 577 578 579
}



580
/****f* guiconfig/display_top_tabs
Ad Schellevis's avatar
Ad Schellevis committed
581 582 583 584 585 586 587
 * NAME
 *   display_top_tabs - display tabs with rounded edges
 * INPUTS
 *   $text      - array of tabs
 * RESULT
 *   null
 ******/
588 589
function display_top_tabs(& $tab_array, $no_drop_down = false)
{
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
    global $config;

    $tab_array_char_limit = 92;

    /*  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) {
        echo "<h2>".gettext("Currently viewing: ")."</h2>";
        echo "<select class=\"selectpicker\" name=\"TabSelect\" onchange=\"tabs_will_go(this)\" data-live-search=\"true\" data-style=\"btn-default\">\n";
        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";
        echo "<ul class=\"nav nav-tabs nav-justified\" role=\"tablist\">\n";
        $tabscounter = 0;
        foreach ($tab_array as $ta) {
            if ($ta[1] == true) {
                echo "  <li class=\"active\"><a href=\"{$ta[2]}\" role=\"tab\">{$ta[0]}</a></li>\n";
            } else {
                echo "  <li><a href=\"{$ta[2]}\" role=\"tab\">{$ta[0]}</a></li>\n";
            }
            $tabscounter++;
        }
        echo "</ul>\n";
    }
Ad Schellevis's avatar
Ad Schellevis committed
650 651 652
}

$timezone = $config['system']['timezone'];
653
if (!$timezone) {
654
    $timezone = 'Etc/UTC';
655
}
Ad Schellevis's avatar
Ad Schellevis committed
656 657

date_default_timezone_set($timezone);
658 659 660

function get_crash_report($pedantic = false)
{
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
    $savemsg = sprintf(
        gettext('A problem was detected. Click %shere%s for more information.'),
        '<a href="/crash_reporter.php">',
        '</a>'
    );
    $skip_files = array('.', '..', 'minfree', 'bounds', '');
    $PHP_errors_log = '/tmp/PHP_errors.log';
    $excludes = array();
    $count = 0;

    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));
        }
        $total = shell_exec(sprintf(
            '/bin/cat %s | %s /usr/bin/wc -l | /usr/bin/awk \'{ print $1 }\'',
            $PHP_errors_log,
            $extra
        ));
        if ($total > 0) {
            $count++;
        }
    }

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

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

    return $savemsg;
703
}
704 705 706

function get_menu_messages()
{
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 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
    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);
757
}