<?php
// session gets started, at first without name
session_start();
// Error reporting on Site -------------------------------------------------------------------------------
ini_set('display_errors'1);
ini_set('display_startup_errors'1);
error_reporting(E_ALL);
// -------------------------------------------------------------------------------------------------------


// Function to debug in console --------------------------------------------------------------------------
function debug_to_console($data)
{
    
$output $data;
    if (
is_array($output))
        
$output implode(','$output);

    echo 
"<script>console.log('Debug Objects: " $output "' );</script>";
}

// Database link and setup -------------------------------------------------------------------------------
$LINK mysqli_connect(
    
"127.0.0.1",
    
"janya763",
    
"vOOxhXUhWhgCPWFC",
    
"janya763"
);
// Sets first Page if there is none
if (!isset($_GET["page"]))
    
$_GET["page"] = "welcome";

if (
$_GET["page"] == "logout") {
    unset(
$_SESSION["username"]);
    
$_GET["page"] = "welcome";
}

// ignores cms because it is in directory
if ($_GET["page"] != "cms") {
    
// Query to get content from the table where title = $_GET["page"]
    
$query "SELECT * FROM pages WHERE title LIKE'" mysqli_real_escape_string(
        
$LINK,
        
$_GET["page"]
    ) . 
"'ORDER BY title";
    
$result mysqli_query($LINK$query);
    if (!
$result)
        die(
"Query failed: " mysqli_error($LINK));
}
// -------------------------------------------------------------------------------------------------------

// User login session ------------------------------------------------------------------------------------
if (isset($_POST["login_data"])) {
    
$usernameTemp $_POST["username"];
    
$password $_POST["password"];
    
// query returns user with the name typed in
    
$stmt mysqli_prepare($LINK"SELECT password FROM users WHERE username LIKE ?");
    
$stmt->bind_param("s"$usernameTemp);
    
$stmt->execute();
    
$stmt->bind_result($databasePassword);
    
$stmt->fetch();
    
$stmt->close();
    
debug_to_console($databasePassword $password);
    
// checks if password is the same as the password in the database with a hash encryption
    
if (sha1($password) == $databasePassword && $databasePassword != "") {
        
session_abort();
        
session_name($usernameTemp);
        
session_start();
        
$_SESSION["username"] = $usernameTemp;
    }
}
// -------------------------------------------------------------------------------------------------------
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="Styles/style.css">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link
        href="https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,100..1000;1,9..40,100..1000&display=swap"
        rel="stylesheet">
    <title>CMS - SYSTEM</title>
</head>

<body>
    <div class="navigation">
        <nav>
            <h1>Simple Page</h1>
            <ul>
                <?php
                
// Dynamic Links -------------------------------------------------------------------------
                // second Query to select the titles with a ascending sort
                
$secondQuery "SELECT title FROM pages ORDER BY title DESC";
                
$result2 mysqli_query($LINK$secondQuery);
                
// creates links with a for-loop
                
for ($i 0$i mysqli_num_rows($result2); $i++) {
                    
$row mysqli_fetch_array($result2);
                    echo 
"<li><a href='index.php?page=" strtolower($row["title"])
                        . 
"'>" $row["title"] . "</a></li>";
                }
                
// checks if username is set in Session, then creates Login or CMS and Logout
                
if (!isset($_SESSION["username"])) {
                    
?>
                    <li><a href="index.php?page=login">Login</a></li>
                    <?php
                
} else {
                    if (
session_name() == $_SESSION["username"])
                    
?>
                    <li><a href="index.php?page=cms">CMS</a></li>
                    <li><a href="index.php?page=logout">Logout</a></li><?php
                
}
                
// ---------------------------------------------------------------------------------------
                
?>
            </ul>
        </nav>

        <?php
        
// Display Content -------------------------------------------------------------------------------
        
$page = isset($_GET["page"]) ? $_GET["page"] : "welcome";
        
// pages that are allowed in view (and are in database)
        
$allowed = ["welcome""contact""about"];
        if (
in_array($page$allowed) && $result != null) {
            if (isset(
$result))
                
$row mysqli_fetch_assoc($result);
            
// creation of content
            
?>
        </div>
        <div class="content">
            <h1><?= $row["title"?></h1>
            <p><?= $row["content"?></p>
        </div>
        <?php
            
// in case of login or cms
        
} elseif ($page == "cms" || $page == "login") {
            
?></div>
        <div class="content"><?php
        
include "$page.php";
        
?></div><?php
            
// if page logout is clicked then session username is unset
        
} else {
            
// what doesnt exist, cant be found
            
?></div><?php
            
echo "<h2>Page not found</h2>";
        }
        
// -----------------------------------------------------------------------------------------------
        
?>
</body>

</html>