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; $conn = null;
attempt_sql_connect(); 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 /** Attempt to connect to sql database
*/ */
function attempt_sql_connect() { function attempt_sql_connect() {
@ -88,6 +92,14 @@ function construct_account_section(){
* purposes. * purposes.
*/ */
function display_header($pageTitle) { function display_header($pageTitle) {
global $header_displayed;
if($header_displayed){
return;
}
$header_displayed = true;
printf(" printf("
<html lang=\"en\"> <html lang=\"en\">
<head> <head>

View file

@ -367,13 +367,8 @@ function load_blog($conn, $blogId){
// Check DB connection // Check DB connection
if($conn == null){ if($conn == null){
printf(" include($_SERVER["DOCUMENT_ROOT"]."/errors/503.php");
<article> include($_SERVER["DOCUMENT_ROOT"]."/../common/footer.php");
<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");
die(); die();
} }
@ -402,12 +397,8 @@ $blogId = sanitize_input($_GET["guid"]);
// If no ID was entered, display warning and die. // If no ID was entered, display warning and die.
if(!$blogId) { if(!$blogId) {
display_header("No article requested."); display_header("No article requested.");
printf(" include($_SERVER["DOCUMENT_ROOT"]."/errors/404.php");
<article> include($_SERVER["DOCUMENT_ROOT"]."/../common/footer.php");
<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");
die(); die();
} }
@ -416,12 +407,8 @@ $blogPost = load_blog($conn, $blogId);
// If no ID was entered, display warning and die. // If no ID was entered, display warning and die.
if(!$blogPost) { if(!$blogPost) {
display_header("Article not found"); display_header("Article not found");
printf(" include($_SERVER["DOCUMENT_ROOT"]."/errors/404.php");
<article> include($_SERVER["DOCUMENT_ROOT"]."/../common/footer.php");
<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");
die(); die();
} }

View file

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

View file

@ -1,6 +1,4 @@
<?php <?php
display_header("Delete account");
// Error string for the content deletion confirmation // Error string for the content deletion confirmation
$confStringErr = ""; $confStringErr = "";
@ -13,13 +11,8 @@ function delete_user($conn, $confirmString) {
// Check DB connection // Check DB connection
if($conn == null){ if($conn == null){
printf(" include($_SERVER["DOCUMENT_ROOT"]."/errors/503.php");
<article> include($_SERVER["DOCUMENT_ROOT"]."/../common/footer.php");
<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");
die(); die();
} }
@ -73,6 +66,7 @@ function delete_user($conn, $confirmString) {
$stmt->bindParam(":userId", $_SESSION["current_user"]->user_id); $stmt->bindParam(":userId", $_SESSION["current_user"]->user_id);
$stmt->execute(); $stmt->execute();
// Log the user out
if (ini_get("session.use_cookies")) { if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params(); $params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000, setcookie(session_name(), '', time() - 42000,
@ -90,6 +84,8 @@ if (isset($_POST["submit"])) {
delete_user($conn, sanitize_input($_POST["remove_content"])); delete_user($conn, sanitize_input($_POST["remove_content"]));
} }
display_header("Delete account");
if(sanitize_input($_GET["success"]) == "true"){ if(sanitize_input($_GET["success"]) == "true"){
printf("<article><h2>Deletion succesful.</h2></article>"); printf("<article><h2>Deletion succesful.</h2></article>");
} }

View file

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

View file

@ -1,6 +1,4 @@
<?php <?php
display_header("Login");
// Define previous attempt and error variables and set to empty values. // Define previous attempt and error variables and set to empty values.
$usernameOld = $passwordOld = ""; $usernameOld = $passwordOld = "";
$usernameErr = $passwordErr = ""; $usernameErr = $passwordErr = "";
@ -17,13 +15,8 @@ function attempt_login($conn, $username, $password) {
// Check DB connection // Check DB connection
if($conn == null){ if($conn == null){
printf(" include($_SERVER["DOCUMENT_ROOT"]."/errors/503.php");
<article> include($_SERVER["DOCUMENT_ROOT"]."/../common/footer.php");
<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");
die(); die();
} }
@ -100,6 +93,8 @@ if (isset($_POST["submit"])) {
header("Location: "."http://www.zdenekborovec-dev.cz"); header("Location: "."http://www.zdenekborovec-dev.cz");
} }
} }
display_header("Login");
?> ?>
<article> <article>

View file

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