use 503 error on db conn error

This commit is contained in:
Zdenek Borovec 2024-05-08 16:00:44 +02:00
parent 86e30c266b
commit 5ddabf9c0f
7 changed files with 55 additions and 68 deletions

View file

@ -16,6 +16,10 @@ if(isset($_COOKIE["PHPSESSID"]))
$conn = null;
attempt_sql_connect();
// this variable is here to ensure header doesn't appear twice (on errors,
// for example)
$header_displayed = false;
/** Attempt to connect to sql database
*/
function attempt_sql_connect() {
@ -88,6 +92,14 @@ function construct_account_section(){
* purposes.
*/
function display_header($pageTitle) {
global $header_displayed;
if($header_displayed){
return;
}
$header_displayed = true;
printf("
<html lang=\"en\">
<head>

View file

@ -367,13 +367,8 @@ function load_blog($conn, $blogId){
// Check DB connection
if($conn == null){
printf("
<article>
<h1>Failed DB connection, cannot proceed!</h1>
If you see this error in production,
please shoot me an email with helpful details.
</article>");
include_once($COMMONS."/footer.php");
include($_SERVER["DOCUMENT_ROOT"]."/errors/503.php");
include($_SERVER["DOCUMENT_ROOT"]."/../common/footer.php");
die();
}
@ -402,12 +397,8 @@ $blogId = sanitize_input($_GET["guid"]);
// If no ID was entered, display warning and die.
if(!$blogId) {
display_header("No article requested.");
printf("
<article>
<h2>You didn't request an article</h2>
To request an article, set the `guid` GET parameter to the GUID of the article.
</article>");
include_once($COMMONS."/footer.php");
include($_SERVER["DOCUMENT_ROOT"]."/errors/404.php");
include($_SERVER["DOCUMENT_ROOT"]."/../common/footer.php");
die();
}
@ -416,12 +407,8 @@ $blogPost = load_blog($conn, $blogId);
// If no ID was entered, display warning and die.
if(!$blogPost) {
display_header("Article not found");
printf("
<article>
<h2>The article you requested doesn't exist.</h2>
If you copied the address manually, try to check if you did so correctly.
</article>");
include_once($COMMONS."/footer.php");
include($_SERVER["DOCUMENT_ROOT"]."/errors/404.php");
include($_SERVER["DOCUMENT_ROOT"]."/../common/footer.php");
die();
}

View file

@ -1,6 +1,4 @@
<?php
display_header("Blogs");
/**
* Display a blog-preview div
*/
@ -39,19 +37,12 @@ function display_blog_preview($blogpost_id, $title, $abstract,
/**
* Select all the blogposts from the database, their tags, then display them.
*/
function display_blog_previews(){
// Access global variables
global $conn;
function display_blog_previews($conn){
// Check DB connection
if($conn == null){
printf("
<article>
<h1>Failed DB connection, cannot proceed!</h1>
If you see this error in production,
please shoot me an email with helpful details.
</article>");
return;
include($_SERVER["DOCUMENT_ROOT"]."/errors/503.php");
include($_SERVER["DOCUMENT_ROOT"]."/../common/footer.php");
die();
}
// Prepare statement for selecting all the blogposts
@ -88,6 +79,7 @@ function display_blog_previews(){
}
}
display_header("Blogs");
?>
<article>
@ -102,5 +94,5 @@ function display_blog_previews(){
<hr>
<?php
display_blog_previews();
display_blog_previews($conn);
?>

View file

@ -1,6 +1,4 @@
<?php
display_header("Delete account");
// Error string for the content deletion confirmation
$confStringErr = "";
@ -13,13 +11,8 @@ function delete_user($conn, $confirmString) {
// Check DB connection
if($conn == null){
printf("
<article>
<h1>Failed DB connection, cannot proceed!</h1>
If you see this error in production,
please shoot me an email with helpful details.
</article>");
include_once($GLOBALS['COMMONS']."/footer.php");
include($_SERVER["DOCUMENT_ROOT"]."/errors/503.php");
include($_SERVER["DOCUMENT_ROOT"]."/../common/footer.php");
die();
}
@ -73,6 +66,7 @@ function delete_user($conn, $confirmString) {
$stmt->bindParam(":userId", $_SESSION["current_user"]->user_id);
$stmt->execute();
// Log the user out
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
@ -90,6 +84,8 @@ if (isset($_POST["submit"])) {
delete_user($conn, sanitize_input($_POST["remove_content"]));
}
display_header("Delete account");
if(sanitize_input($_GET["success"]) == "true"){
printf("<article><h2>Deletion succesful.</h2></article>");
}

View file

@ -4,6 +4,7 @@
if(!(bool)($_SESSION["current_user"]->permissions & 128)) {
header($_SERVER["SERVER_PROTOCOL"]." 403 Forbidden", true, 403);
include($_SERVER["DOCUMENT_ROOT"]."/errors/403.php");
include($_SERVER["DOCUMENT_ROOT"]."/../common/footer.php");
die();
}

View file

@ -1,6 +1,4 @@
<?php
display_header("Login");
// Define previous attempt and error variables and set to empty values.
$usernameOld = $passwordOld = "";
$usernameErr = $passwordErr = "";
@ -17,13 +15,8 @@ function attempt_login($conn, $username, $password) {
// Check DB connection
if($conn == null){
printf("
<article>
<h1>Failed DB connection, cannot proceed!</h1>
If you see this error in production,
please shoot me an email with helpful details.
</article>");
include_once($GLOBALS['COMMONS']."/footer.php");
include($_SERVER["DOCUMENT_ROOT"]."/errors/503.php");
include($_SERVER["DOCUMENT_ROOT"]."/../common/footer.php");
die();
}
@ -100,6 +93,8 @@ if (isset($_POST["submit"])) {
header("Location: "."http://www.zdenekborovec-dev.cz");
}
}
display_header("Login");
?>
<article>

View file

@ -1,12 +1,11 @@
<?php
display_header("Register");
// Define previous attempt and error variables and set to empty values.
$usernameOld = $passwordOld = $passwordConfOld = "";
$usernameErr = $passwordErr = "";
/**
* Process the information, and if there are no errors, log the user in.
* Returns true on success, false on failure.
*/
function attempt_register($conn, $username, $password, $passwordConf) {
// Access global variables
@ -18,13 +17,8 @@ function attempt_register($conn, $username, $password, $passwordConf) {
// Check DB connection
if($conn == null){
printf("
<article>
<h1>Failed DB connection, cannot proceed!</h1>
If you see this error in production,
please shoot me an email with helpful details.
</article>");
include_once($GLOBALS['COMMONS']."/footer.php");
include($_SERVER["DOCUMENT_ROOT"]."/errors/503.php");
include($_SERVER["DOCUMENT_ROOT"]."/../common/footer.php");
die();
}
@ -47,7 +41,7 @@ function attempt_register($conn, $username, $password, $passwordConf) {
$usernameOld = $username;
$passwordOld = $password;
$passwordConfOld = $passwordConf;
return;
return false;
}
// See if a user with this name is already registered
@ -69,7 +63,7 @@ function attempt_register($conn, $username, $password, $passwordConf) {
$usernameOld = $username;
$passwordOld = $password;
$passwordConfOld = $passwordConf;
return;
return false;
}
// Hash the password before inserting
@ -83,20 +77,30 @@ function attempt_register($conn, $username, $password, $passwordConf) {
// Execute the statement
$stmt->execute();
printf("<article><h2>Registration succesful!</h2>
You can now continue to
<a href=\"http://www.zdenekborovec-dev.cz/login\">Login</a>.
</article>");
return true;
}
display_header("Register");
/**
* If user sent the form, process it. This starts a session.
* Either login user and redirect to index or set error message variables.
*/
if (isset($_POST["submit"])) {
// Attempt to register
attempt_register($conn, $_POST["username"], $_POST["password"],
$_POST["password_conf"]);
$registerResult = attempt_register($conn, $_POST["username"],
$_POST["password"], $_POST["password_conf"]);
// If registration was succesful, display message, footer and die.
if($registerResult) {
printf("<article><h2>Registration succesful!</h2>
You can now continue to
<a href=\"http://www.zdenekborovec-dev.cz/login\">Login</a>.
</article>");
include($_SERVER["DOCUMENT_ROOT"]."/../common/footer.php");
die();
}
}
?>