librecat/user.php

66 lines
1.7 KiB
PHP

<?php
$db = new SQLite3('ask.db');
$user = $db->query("SELECT * FROM users WHERE username = '" . $_GET["q"] . "';")->fetchArray(SQLITE3_ASSOC);
if(!$user || !$user["id"]){
include("404.php");
die();
}
if($validUser){
header("Location: /"); die();
}
$errorMsg = "";
if(isset($_POST["post-submit"])){
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{
$u = $user["id"];
$by = 0;
$question = htmlspecialchars($_POST["post-text"]);
// Insert user into DB
$db->exec("INSERT INTO questions(user,by,question,answered,date) VALUES ('$u','$by','$question',0,". strtotime('now') .");");
unset($_POST["post-text"]);
}
}
?>
<html>
<head>
<title><?= $user["username"] ?> | LibreCat</title>
</head>
<body>
<h2><?= $user["username"] ?></h2>
<p><?= $user["bio"] ?></p>
<form name="input" action="" method="post">
<p>Ask me anything</p>
<textarea id="post-text" name="post-text"></textarea>
<br/>
<?php if(isset($errorMsg)) echo "<p>$errorMsg</p>\n"; ?>
<input type="submit" name="post-submit"/>
</form>
<?php
$u_prep = $db->prepare("SELECT * FROM users WHERE id = :id");
$qs = $db->query("SELECT * FROM questions WHERE user = '" . $user["id"] . "' LIMIT 0, 10;");
$time = new DateTime("@0");
while($current = $qs->fetchArray(SQLITE3_ASSOC)){
// Execute prepared statement
$u_prep->bindValue(":id", $current["by"], SQLITE3_INTEGER);
$q_user = $u_prep->execute()->fetchArray(SQLITE3_ASSOC);
echo("<h3>". $q_user["name"] ."</h3>");
// Time
$time->setTimestamp($current["date"]);
echo($time->format("Y-m-d H:i:s"));
echo("<p>". $current["question"] ."<p>");
}
?>
</body>
</html>