diff options
-rw-r--r-- | create_thread.php | 19 | ||||
-rw-r--r-- | includes/Session.php | 2 | ||||
-rw-r--r-- | includes/error.php | 25 | ||||
-rw-r--r-- | includes/templates/header.php | 4 | ||||
-rw-r--r-- | register.php | 10 | ||||
-rw-r--r-- | signin.php | 10 | ||||
-rw-r--r-- | signout.php | 2 | ||||
-rw-r--r-- | viewthread.php | 19 |
8 files changed, 70 insertions, 21 deletions
diff --git a/create_thread.php b/create_thread.php index 6fb7df9..976bd9f 100644 --- a/create_thread.php +++ b/create_thread.php @@ -10,6 +10,7 @@ <h2>Create a new thread</h2> <?php include_once 'includes/Session.php'; +include_once 'includes/error.php'; if (!Session::get()->is_signed_in()) { trigger_error('You must be <a href="signin.php">signed in</a> to create a thread.'); exit(); @@ -26,7 +27,7 @@ if (!Session::get()->is_signed_in()) { $categories = get_all_categories(); if (count($categories) == 0) { - echo 'There are no categories to post to!'; + trigger_error('There are no categories to post to!'); } else { echo '<select name="thread_cat">'; @@ -44,14 +45,28 @@ if (!Session::get()->is_signed_in()) { <?php include_once 'includes/functions_post.php'; include_once 'includes/functions_thread.php'; +include_once 'includes/error.php'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $post_content = filter_input(INPUT_POST, 'post_content', FILTER_SANITIZE_STRING); $thread_subject = filter_input(INPUT_POST, 'thread_subject', FILTER_SANITIZE_STRING); $thread_cat = filter_input(INPUT_POST, 'thread_cat', FILTER_SANITIZE_NUMBER_INT); + $errors = array(); if (empty($thread_subject) or !$thread_subject) { - echo 'Thread subject cannot be empty'; + $errors[] = 'Thread subject cannot be empty'; + } + if (empty($post_content) or !$post_content) { + $errors[] = 'Thread body cannot be empty'; + } + + if (!empty($errors)) { + $errstr = 'Please check the following problems: <ul>'; + foreach ($errors as $err) { + $errstr .= '<li>' . $err . '</li>'; + } + $errstr .= '</ul>'; + trigger_error($errstr); } else { $thread_id = create_thread($thread_subject, $thread_cat); create_post($post_content, $thread_id, $thread_cat); diff --git a/includes/Session.php b/includes/Session.php index 7951d70..ceaa765 100644 --- a/includes/Session.php +++ b/includes/Session.php @@ -54,4 +54,4 @@ class Session return $result; } -}
\ No newline at end of file +} diff --git a/includes/error.php b/includes/error.php new file mode 100644 index 0000000..5e33212 --- /dev/null +++ b/includes/error.php @@ -0,0 +1,25 @@ +<?php +function user_notice($message) { + echo '<p class="error">'. $message .'</p>'; +} + +function handle_error($errno, $errstr, $errfile, $errline) { + if (!(error_reporting() & $errno)) { + // This error code is not included in error_reporting, so let it fall + // through to the standard PHP error handler + return false; + } + + switch ($errno) { + // See https://www.php.net/manual/en/errorfunc.constants.php + case E_USER_NOTICE: + user_notice($errstr); + break; + default: + return false; + } + return true; +} + +$old_error_handler = set_error_handler('handle_error'); +?> diff --git a/includes/templates/header.php b/includes/templates/header.php index 1db9cda..5070cfe 100644 --- a/includes/templates/header.php +++ b/includes/templates/header.php @@ -1,4 +1,6 @@ +<header> <h1>cflip.net forum<sup style="font-size: small;">beta</sup></h1> +<p> [<a href="/">Home</a>] [<a href="/search.php?type=thread&sort=lr">All Threads</a>] [<a href="/search.php?type=post&sort=cd">All Posts</a>] @@ -16,3 +18,5 @@ } ?> </span> +</p> +</header> diff --git a/register.php b/register.php index 02fbe58..4c42610 100644 --- a/register.php +++ b/register.php @@ -21,6 +21,7 @@ <?php include_once './includes/functions_user.php'; +include_once './includes/error.php'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $errors = array(); @@ -61,15 +62,16 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { } if (!empty($errors)) { - echo 'Please check the following problems: <ul>'; + $errstr = 'Please check the following problems: <ul>'; foreach ($errors as $err) { - echo '<li>' . $err . '</li>'; + $errstr .= '<li>' . $err . '</li>'; } - echo '</ul>'; + $errstr .= '</ul>'; + trigger_error($errstr); } else { $pass_hash = password_hash($user_pass, PASSWORD_DEFAULT); register_user($user_name, $pass_hash); - echo 'Account successfully registered! You can now <a href="signin.php">sign in</a>'; + echo '<p class="success">Account successfully registered! You can now <a href="signin.php">sign in</a></p>'; } } ?> @@ -17,6 +17,7 @@ </form> <?php +include_once 'includes/error.php'; function validate($data) { @@ -42,17 +43,18 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { } if (!empty($errors)) { - echo 'Please check the following problems: <ul>'; + $errstr = 'Please check the following problems: <ul>'; foreach ($errors as $err) { - echo '<li>' . $err . '</li>'; + $errstr .= '<li>' . $err . '</li>'; } - echo '</ul>'; + $errstr .= '</ul>'; + trigger_error($errstr); } else { $user = new User(); $result = $user->get_by_name($user_name); if (!$result) { - echo 'There is no user with that name. Did you mean to <a href="register.php">create a new account?</a>'; + trigger_error('There is no user with that name. Did you mean to <a href="register.php">create a new account?</a>'); } else { if (!password_verify($user_pass, $user->password)) { echo 'Password does not match!'; diff --git a/signout.php b/signout.php index 035877b..bbaa47a 100644 --- a/signout.php +++ b/signout.php @@ -13,4 +13,4 @@ include_once './includes/templates/header.php'; echo '<p class="success">You have now been signed out</p>'; ?> </body> -</html>
\ No newline at end of file +</html> diff --git a/viewthread.php b/viewthread.php index e8eda06..fa1c81b 100644 --- a/viewthread.php +++ b/viewthread.php @@ -68,29 +68,30 @@ foreach ($posts as $post) { ?> <hr> <h2>Reply to this thread</h2> -<form method="post"> - <textarea name="post_content" rows="10" cols="50"></textarea> - <br> - <input type="submit" name="submit"> -</form> -</body> -</html> <?php include_once 'includes/functions_post.php'; +include_once 'includes/error.php'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (!Session::get()->is_signed_in()) { - echo 'You must be <a href="signin.php">signed in</a> to reply to this thread.'; + trigger_error('You must be <a href="signin.php">signed in</a> to reply to this thread.', E_USER_NOTICE); return; } $post_content = filter_input(INPUT_POST, 'post_content', FILTER_SANITIZE_STRING); if (empty($post_content) or !$post_content) { - echo 'Thread subject cannot be empty'; + trigger_error('Reply cannot be empty'); } else { create_post($post_content, $current->id, $current->category->id); header('Location: ' . $_SERVER['PHP_SELF'] . '?id=' . $current->id); } } ?> +<form method="post"> + <textarea name="post_content" rows="10" cols="50"></textarea> + <br> + <input type="submit" name="submit"> +</form> +</body> +</html> |