<?php # Script 17.1 - header.html
/* This script...
 * - starts the HTML template
 * - indicates the encoding using header()
 * - starts the session
 * - gets the language-specific words from the database
 * - lists the available languages
 */

// Indicate the encoding:
header('Content-Type: text/html; charset=UTF-8');

// Start the session:
session_start();

// For testing purposes:
$_SESSION['user_id'] = 1;
$_SESSION['user_tz'] = 'America/New_York';
// For logging out:
//$_SESSION = [];

// Need the database connection:
require('../mysqli_connect.php');

// Check for a new language ID...
// Then store the language ID in the session:
if (isset($_GET['lid']) &&
    filter_var($_GET['lid'], FILTER_VALIDATE_INT, array('min_range' => 1))
    ) {
    $_SESSION['lid'] = $_GET['lid'];
} elseif (!isset($_SESSION['lid'])) {
    $_SESSION['lid'] = 1; // Default.
}

// Get the words for this language:
$q = "SELECT * FROM words WHERE lang_id = {$_SESSION['lid']}";
$r = mysqli_query($dbc, $q);
if (mysqli_num_rows($r) == 0) { // Invalid language ID!

    // Use the default language:
    $_SESSION['lid'] = 1; // Default.
    $q = "SELECT * FROM words WHERE lang_id = {$_SESSION['lid']}";
    $r = mysqli_query($dbc, $q);

}

// Fetch the results into a variable:
$words = mysqli_fetch_array($r, MYSQLI_ASSOC);

// Free the results:
mysqli_free_result($r);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?php echo $words['title']; ?></title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <style type="text/css">
body {
  padding-top: 50px;
}
.starter-template {
  padding: 40px 15px;
  text-align: left;
}
</style>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <a class="navbar-brand" href="index.php">Site Name</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
            <ul class="nav navbar-nav">
<?php // Display links:

// Default links:
echo '<li><a href="index.php">' . $words['home'] . '</a></li>
<li><a href="forum.php">' . $words['forum_home'] . '</a></li>';

// Display links based upon login status:
if (isset($_SESSION['user_id'])) {

    // If this is the forum page, add a link for posting new threads:
    if (basename($_SERVER['PHP_SELF']) == 'forum.php') {
        echo '<li><a href="post.php">' . $words['new_thread'] . '</a></li>';
    }

    // Add the logout link:
    echo '<li><a href="logout.php">' . $words['logout'] . '</a></li>';

} else {

    // Register and login links:
    echo '<li><a href="register.php">' . $words['register'] . '</a></li>
    <li><a href="login.php">' . $words['login'] . '</a></li>';
}

// Retrieve all the languages...
echo '<li class="dropdown"><a href="forum.php" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">' . $words['language'] . ' <span class="caret"></span></a>
<ul class="dropdown-menu">';

$q = "SELECT lang_id, lang FROM languages ORDER BY lang_eng ASC";
$r = mysqli_query($dbc, $q);
if (mysqli_num_rows($r) > 0) {
  while ($menu_row = mysqli_fetch_array($r, MYSQLI_NUM)) {
    echo '<li><a href="forum.php?lid=' . $menu_row[0] . '">' . $menu_row[1] . '</a></li>';
  }
}
mysqli_free_result($r);
?>
                </ul></li>
            </ul>
        </div><!-- navbar -->
    </div><!-- container -->
</nav>

<div class="container">
    <div class="starter-template">
