watch.twip-network.org/index.php

116 lines
3.3 KiB
PHP
Raw Normal View History

2024-01-20 16:04:27 +01:00
<?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);
}
2024-01-25 13:02:32 +01:00
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;
}
2024-01-20 16:04:27 +01:00
session_start();
require_once 'config.php';
2024-01-20 19:10:12 +01:00
$paths_to_check = array();
2024-01-20 19:10:12 +01:00
$page = basename($_SERVER['QUERY_STRING']);
2024-01-23 13:11:30 +01:00
if (isset($_SESSION['user_id'])) {
if($_SESSION['user_isAdmin'] == 1) {
2024-01-23 13:11:30 +01:00
$nav = include_ob("$template_dir/admin_nav.html");
}
else {
$nav = include_ob("$template_dir/user_nav.html");
}
2024-01-23 13:16:10 +01:00
} else {
2024-01-23 13:11:30 +01:00
$nav = include_ob("$template_dir/navigation.html");
2024-01-25 13:02:32 +01:00
$nav = replaceTemplateTags($nav); // Apply templating to the navigation
2024-01-23 13:11:30 +01:00
}
2024-01-23 12:55:28 +01:00
if($page_dir == "global") {
if(!file_exists("$page_dir/index.html")) {
$page_file = "$page_dir/login.html";
}
}
2024-01-20 19:10:12 +01:00
if(empty($page)){
2024-01-23 12:55:28 +01:00
if(isset($_SESSION['user_id'])){
$page = 'index';
}
else{
$page = 'login';
}
2024-01-20 19:10:12 +01:00
}
2024-01-20 19:04:39 +01:00
2024-01-20 19:10:12 +01:00
// Check if user is logged in
if (isset($_SESSION['user_id'])) {
2024-01-23 13:01:56 +01:00
if ($_SESSION['user_isAdmin'] == 1) {
2024-01-20 19:04:39 +01:00
$paths_to_check[] = "pages/admin";
}
2024-01-20 20:03:19 +01:00
2024-01-20 19:04:39 +01:00
$paths_to_check[] = "pages/user";
2024-01-20 19:10:12 +01:00
}
2024-01-22 09:32:55 +01:00
$paths_to_check[] = "pages/global";
2024-01-20 19:10:12 +01:00
$page_file = "$template_dir/404.html";
2024-01-20 19:10:12 +01:00
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;
2024-01-20 18:00:30 +01:00
}
2024-01-20 19:10:12 +01:00
}
2024-01-20 20:01:41 +01:00
$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);
2024-01-20 20:04:56 +01:00
$page_data = str_replace('<template name="username">', $_SESSION['user_username'], $page_data);
}
2024-01-20 19:59:54 +01:00
$page_regex = '/<!--PAGENAME=(.*?)-->/s';
2024-01-25 12:48:58 +01:00
$page_style_regex = '/<!--PAGESTYLE=(.*?)-->/s';
2024-01-20 19:56:55 +01:00
2024-01-25 12:48:58 +01:00
if (preg_match($page_regex, $page_data, $matches)) {
2024-01-20 19:56:55 +01:00
$page_name = $matches[1];
2024-01-20 20:01:41 +01:00
$page_data = preg_replace($page_regex, '', $page_data);
2024-01-25 12:48:58 +01:00
} else {
2024-01-20 19:56:55 +01:00
$page_name = ucfirst($page);
}
2024-01-25 12:48:58 +01:00
if (preg_match($page_style_regex, $page_data, $style_matches)) {
$page_style = $style_matches[1];
2024-01-25 13:02:32 +01:00
$page_data = preg_replace($page_style_regex, '', $page_data); // Replace the PAGESTYLE tag
2024-01-25 12:48:58 +01:00
} else {
$page_style = "/styles/pages/$page_name.css";
$page_data = str_replace('<template name="page styling">', $page_style, $page_data);
}
2024-01-25 12:52:20 +01:00
$output = str_replace('<template name="page name">', $page_name, $output);
$output = str_replace('<template name="page styling">', $page_style, $output);
2024-01-20 20:04:56 +01:00
$output = str_replace('<template name="page content">', $page_data, $output);
2024-01-20 19:55:14 +01:00
echo $output;
2024-01-25 13:02:32 +01:00
?>