watch.twip-network.org/index.php
2024-01-25 13:02:32 +01:00

116 lines
3.3 KiB
PHP

<?php
function include_ob($file){
ob_start();
include $file;
return ob_get_clean();
}
function sanitize_template_strings($indata){
$sanit_pattern = '/<template.*>/is';
return preg_replace($sanit_pattern, '', $indata);
}
function replaceTemplateTags($content) {
// Add any additional template replacements here
$hash = md5(strtolower(trim($_SESSION['user_email'])));
$gravatarUrl = "https://www.gravatar.com/avatar/$hash?s=100";
$content = str_replace('<template name="gravatar image">', "<img src='$gravatarUrl' alt='Gravatar Profile Picture'>", $content);
// Add more replacements if needed
return $content;
}
session_start();
require_once 'config.php';
$paths_to_check = array();
$page = basename($_SERVER['QUERY_STRING']);
if (isset($_SESSION['user_id'])) {
if($_SESSION['user_isAdmin'] == 1) {
$nav = include_ob("$template_dir/admin_nav.html");
}
else {
$nav = include_ob("$template_dir/user_nav.html");
}
} else {
$nav = include_ob("$template_dir/navigation.html");
$nav = replaceTemplateTags($nav); // Apply templating to the navigation
}
if($page_dir == "global") {
if(!file_exists("$page_dir/index.html")) {
$page_file = "$page_dir/login.html";
}
}
if(empty($page)){
if(isset($_SESSION['user_id'])){
$page = 'index';
}
else{
$page = 'login';
}
}
// Check if user is logged in
if (isset($_SESSION['user_id'])) {
if ($_SESSION['user_isAdmin'] == 1) {
$paths_to_check[] = "pages/admin";
}
$paths_to_check[] = "pages/user";
}
$paths_to_check[] = "pages/global";
$page_file = "$template_dir/404.html";
foreach($paths_to_check as $page_dir){
$page_file_tmp = "$page_dir/$page.html";
if(file_exists($page_file_tmp)){
$page_file = $page_file_tmp;
break;
}
}
$page_data = include_ob($page_file);
$output = file_get_contents("$template_dir/skeleton.html");
$output = str_replace('<template name="navigation">', $nav, $output);
if (isset($_SESSION['user_id'])) {
$hash = md5(strtolower(trim($_SESSION['user_email'])));
$gravatarUrl = "https://www.gravatar.com/avatar/$hash?s=100";
$gravatarTag = "<img src='$gravatarUrl' alt='Gravatar Profile Picture'>";
$page_data = str_replace('<template name="gravatar image">', $gravatarTag, $page_data);
$page_data = str_replace('<template name="username">', $_SESSION['user_username'], $page_data);
}
$page_regex = '/<!--PAGENAME=(.*?)-->/s';
$page_style_regex = '/<!--PAGESTYLE=(.*?)-->/s';
if (preg_match($page_regex, $page_data, $matches)) {
$page_name = $matches[1];
$page_data = preg_replace($page_regex, '', $page_data);
} else {
$page_name = ucfirst($page);
}
if (preg_match($page_style_regex, $page_data, $style_matches)) {
$page_style = $style_matches[1];
$page_data = preg_replace($page_style_regex, '', $page_data); // Replace the PAGESTYLE tag
} else {
$page_style = "/styles/pages/$page_name.css";
$page_data = str_replace('<template name="page styling">', $page_style, $page_data);
}
$output = str_replace('<template name="page name">', $page_name, $output);
$output = str_replace('<template name="page styling">', $page_style, $output);
$output = str_replace('<template name="page content">', $page_data, $output);
echo $output;
?>