Universal header and config link

This commit is contained in:
posweg 2020-05-21 15:57:59 +00:00
parent b53cb2811c
commit 4d0deabb16
5 changed files with 78 additions and 44 deletions

View File

@ -1 +1,2 @@
ERROR 404: Page not found <?php include("include/header.php") ?>
<p>ERROR 404: Page not found</p>

16
config.php Normal file
View File

@ -0,0 +1,16 @@
<?php
session_start();
if($_SESSION["login"] === true){
echo($_SESSION["uid"] . " - ");
echo("<a href='/logout'>logout</a>");
echo(" - <a href='/config'>config</a>'");
}
else{
header("Location: /login"); die();
}
?>
<html>
<h1>Config</h1>
</html>

40
include/header.php Normal file
View File

@ -0,0 +1,40 @@
<?php
session_start();
$db = new SQLite3('ask.db');
$users_table = $db->query("CREATE TABLE IF NOT EXISTS users(
id INTEGER PRIMARY KEY,
username TEXT,
shadow TEXT,
name TEXT,
bio TEXT
)");
$db->exec("CREATE TABLE IF NOT EXISTS questions(
id INTEGER PRIMARY KEY,
user INTEGER,
by INTEGER,
question TEXT,
date INTEGER,
answered INTEGER
);");
if(!$db->querySingle("SELECT EXISTS(SELECT * FROM users where id = 0);")){
echo "NOTICE: anonymous user created.";
$db->exec("INSERT INTO users(id, username, name) VALUES('0','anonymous','Anonymous');");
}
if($_SESSION["login"] === true){
echo("<a href='/'>LibreCat</a>");
echo(" | ");
$user = $db->query("SELECT * FROM users WHERE id = ".$_SESSION["uid"].";")->fetchArray(SQLITE3_ASSOC);
echo("<a href='/user/" . $user["username"] . "'>". $user["username"] . "</a>");
echo(" | ");
echo("<a href='/logout'>logout</a>");
}
else{
echo("<a href='/login'>Login</a>");
//header("Location: /login.php"); die();
}
?>

View File

@ -1,38 +1,5 @@
<?php <?php
session_start(); include("include/header.php");
$db = new SQLite3('ask.db');
$users_table = $db->query("CREATE TABLE IF NOT EXISTS users(
id INTEGER PRIMARY KEY,
username TEXT,
shadow TEXT,
name TEXT,
bio TEXT
)");
$db->exec("CREATE TABLE IF NOT EXISTS questions(
id INTEGER PRIMARY KEY,
user INTEGER,
by INTEGER,
question TEXT,
date INTEGER,
answered INTEGER
);");
if(!$db->querySingle("SELECT EXISTS(SELECT * FROM users where id = 0);")){
echo "NOTICE: anonymous user created.";
$db->exec("INSERT INTO users(id, username, name) VALUES('0','anonymous','Anonymous');");
}
if($_SESSION["login"] === true){
echo($_SESSION["uid"] . " - ");
echo("<a href='/logout'>logout</a>");
}
else{
echo("<a href='/login'>Login</a>");
//header("Location: /login.php"); die();
}
?> ?>
<html> <html>

View File

@ -1,12 +1,20 @@
<?php <?php
session_start();
$db = new SQLite3('ask.db'); $db = new SQLite3('ask.db');
$user = $db->query("SELECT * FROM users WHERE username = '" . $_GET["q"] . "';")->fetchArray(SQLITE3_ASSOC); $p_user = $db->query("SELECT * FROM users WHERE username = '" . $_GET["q"] . "';")->fetchArray(SQLITE3_ASSOC);
if(!$user || !$user["id"]){ if(!$p_user || !$p_user["id"]){
include("404.php"); include("404.php");
die(); die();
} }
if(isset($_SESSION["uid"])){
if($_SESSION["uid"] == $p_user["id"]){
$is_current_user = true;
}
}
if($validUser){ if($validUser){
header("Location: /"); die(); header("Location: /"); die();
} }
@ -16,7 +24,7 @@ if(isset($_POST["post-submit"])){
if($_POST["post-text"] == "") $errorMsg = "The question can't be blank."; if($_POST["post-text"] == "") $errorMsg = "The question can't be blank.";
else if(strlen($_POST["post-text"]) > 400) $errorMsg = "The question can't bee longer than 400 characters"; else if(strlen($_POST["post-text"]) > 400) $errorMsg = "The question can't bee longer than 400 characters";
else{ else{
$u = $user["id"]; $u = $p_user["id"];
$by = 0; $by = 0;
$question = htmlspecialchars($_POST["post-text"]); $question = htmlspecialchars($_POST["post-text"]);
@ -29,11 +37,13 @@ if(isset($_POST["post-submit"])){
<html> <html>
<head> <head>
<title><?= $user["username"] ?> | LibreCat</title> <title><?= $p_user["username"] ?> | LibreCat</title>
</head> </head>
<body> <body>
<h2><?= $user["username"] ?></h2> <?php include("include/header.php"); ?>
<p><?= $user["bio"] ?></p> <h2><?= $p_user["username"] ?></h2>
<p><?= $p_user["bio"] ?></p>
<?php if($is_current_user) echo("<a href='/config'>config</a>"); ?>
<form name="input" action="" method="post"> <form name="input" action="" method="post">
<p>Ask me anything</p> <p>Ask me anything</p>
@ -45,7 +55,7 @@ if(isset($_POST["post-submit"])){
<?php <?php
$u_prep = $db->prepare("SELECT * FROM users WHERE id = :id"); $u_prep = $db->prepare("SELECT * FROM users WHERE id = :id");
$qs = $db->query("SELECT * FROM questions WHERE user = '" . $user["id"] . "' LIMIT 0, 10;"); $qs = $db->query("SELECT * FROM questions WHERE user = '" . $p_user["id"] . "' LIMIT 0, 10;");
$time = new DateTime("@0"); $time = new DateTime("@0");
while($current = $qs->fetchArray(SQLITE3_ASSOC)){ while($current = $qs->fetchArray(SQLITE3_ASSOC)){
@ -53,7 +63,7 @@ if(isset($_POST["post-submit"])){
$u_prep->bindValue(":id", $current["by"], SQLITE3_INTEGER); $u_prep->bindValue(":id", $current["by"], SQLITE3_INTEGER);
$q_user = $u_prep->execute()->fetchArray(SQLITE3_ASSOC); $q_user = $u_prep->execute()->fetchArray(SQLITE3_ASSOC);
echo("<h3>". $q_user["name"] ."</h3>"); echo("<h3>". $current["id"] ." - " . $q_user["name"] ."</h3>");
// Time // Time
$time->setTimestamp($current["date"]); $time->setTimestamp($current["date"]);