diff --git a/auth_callback.php b/auth_callback.php index b2a459a..a8b9f20 100644 --- a/auth_callback.php +++ b/auth_callback.php @@ -8,8 +8,8 @@ if (isset($_GET['code'])) { if ($success) { redirect('index.php'); } else { - die('Echo error authenticating'); + quit(get_page_header().'Echo error authenticating'); } } else { - die('error authenticating: ' . $_GET['error_message']); + quit(get_page_header().'Echo error authenticating: '. $_GET['error_message']); } diff --git a/globals.php b/globals.php index 22740c1..9be8703 100644 --- a/globals.php +++ b/globals.php @@ -81,12 +81,23 @@ function get_source_set($user, int $base_size, int $max_scale = 3): string return implode(', ', $srcset_entries); } -function make_banner(string $type, string $content): string +function make_banner(string $type, string $content, string $custom_symbol = null): string { + if (empty($custom_symbol)) { + $custom_symbol = $type === 'success' ? '✓' : '𐄂'; + } + return ''; } + +function quit(string $error, string $error_details = '') +{ + die(make_banner('error', $error) . $error_details . get_page_footer()); +} diff --git a/new_poll.php b/new_poll.php index 38e7cff..6ed4533 100644 --- a/new_poll.php +++ b/new_poll.php @@ -13,11 +13,11 @@ use APnutI\Entities\User; try { echo get_page_header('New Poll', true, ['new_poll']); } catch (\Exception $e) { - die('Something went wrong :( "'.$e->getMessage().'"' . get_page_footer()); + quit('Something went wrong :( "'.$e->getMessage().'"'); } if (!$api->isAuthenticated(false, true)) { - die('You need to be logged in to create a new poll!' . get_page_footer()); + quit('You need to be logged in to create a new poll!'); } if (!empty($_POST['submit'])) { @@ -34,7 +34,7 @@ if (!empty($_POST['submit'])) { $poll = Poll::create($api, $prompt, $options, $max_options, $duration_total_minutes, $is_anonymous, $is_public); redirect('post_poll.php?poll_token='.$poll->token.'&id='.$poll->id.'&prompt='.urlencode($prompt)); } catch (\Exception $e) { - die('Something went wrong creating the poll: "' . $e->getMessage() . '"' . get_page_footer()); + quit('Something went wrong creating the poll: "' . $e->getMessage() . '"'); } } ?> diff --git a/post_poll.php b/post_poll.php index 65169b2..ae9dd76 100644 --- a/post_poll.php +++ b/post_poll.php @@ -7,22 +7,22 @@ use APnutI\Entities\Poll; try { echo get_page_header('Post Poll', true, []); } catch (\Exception $e) { - die('Something went wrong :( "'.$e->getMessage().'"' . get_page_footer()); + quit('Something went wrong :( "'.$e->getMessage().'"'); } if (!$api->isAuthenticated(false, true)) { - die('You need to be logged in to create a new post!' . get_page_footer()); + quit('You need to be logged in to create a new post!'); } if (!empty($_POST['submit'])) { if (empty($_POST['poll_id']) || !is_numeric($_POST['poll_id']) || $_POST['poll_id'] <= 0) { - die('Invalid poll ID'.get_page_footer()); + quit('Invalid poll ID'); } if (empty($_POST['poll_token'])) { - die('Invalid poll token'.get_page_footer()); + quit('Invalid poll token'); } if (empty($_POST['post_text'])) { - die('Invalid text'.get_page_footer()); + quit('Invalid text'); } try { $params = [ @@ -31,19 +31,19 @@ if (!empty($_POST['submit'])) { $api->createPostWithParameters($_POST['post_text'], $params); redirect('view_poll.php?poll_created=1&id=' . $_POST['poll_id']); } catch (\Exception $e) { - die('Something went wrong creating your post: "' . $e->getMessage() . '"'.get_page_footer()); + quit('Something went wrong creating your post: "' . $e->getMessage() . '"'); } } if (empty($_GET['id']) || !is_numeric($_GET['id']) || $_GET['id'] <= 0) { - die('Invalid poll ID'.get_page_footer()); + quit('Invalid poll ID'); } if (empty($_GET['poll_token'])) { - die('Invalid poll token'.get_page_footer()); + quit('Invalid poll token'); } if (empty($_GET['prompt'])) { - die('Invalid prompt'.get_page_footer()); + quit('Invalid prompt'); } $poll_id = (int)$_GET['id']; diff --git a/styles/style.css b/styles/style.css index 4fa775c..0964a98 100644 --- a/styles/style.css +++ b/styles/style.css @@ -186,8 +186,9 @@ datewrapper time { padding: 0px; } -.success-banner span { +.banner span { font-size: x-large; + margin-right: 8px; } /* Poll creation */ @@ -269,4 +270,10 @@ footer { footer .sourcecode svg { stroke-width: 1.5em; +} + +/* Other */ + +form.polltoken-input { + margin-top: 8px; } \ No newline at end of file diff --git a/view_poll.php b/view_poll.php index dbaa1a1..0b7a2ec 100644 --- a/view_poll.php +++ b/view_poll.php @@ -10,14 +10,16 @@ use APnutI\Exceptions\PollAccessRestrictedException; use APnutI\Entities\Poll; use APnutI\Entities\User; +$die = null; + try { echo get_page_header('Poll', true, ['poll']); } catch (\Exception $e) { - die('Something went wrong :( "' . $e->getMessage() . '"' . get_page_footer()); + quit('Something went wrong :( "' . $e->getMessage()); } if (empty($_GET['id']) || !is_numeric($_GET['id']) || $_GET['id'] <= 0) { - die('Invalid poll ID'.get_page_footer()); + quit('Invalid poll ID'); } $poll_id = (int)$_GET['id']; $poll = null; @@ -26,43 +28,47 @@ try { $poll_token = array_key_exists('polltoken', $_GET) ? $_GET['polltoken'] : null; $poll = $api->getPoll($poll_id, $poll_token); } catch (NotFoundException $nfe) { - die('Poll not found'.get_page_footer()); + die('Poll not found'); } catch (NotSupportedPollException $nspe) { - die('Sorry, this poll has a not yet supported type: ' . $nspe->getMessage() . get_page_footer()); + quit('Sorry, this poll has a not yet supported type: ' . $nspe->getMessage()); } catch (PollAccessRestrictedException $are) { - $message = array_key_exists('polltoken', $_GET) - ? 'Sorry, your poll token is invalid! Please enter a valid token: ' - : ('Sorry, this poll is private! If you have found this poll on a post, ' - . 'please enter a link to the post, the post ID or the access token for the poll: '); - die( - $message - . '
' - . '
' - . get_page_footer() - ); + $message_header = array_key_exists('polltoken', $_GET) + ? 'Sorry, your poll token is invalid! Please enter a valid token: ' + : 'Sorry, this poll is private!'; + $message_text = array_key_exists('polltoken', $_GET) + ? 'Please enter a valid token: ' + : ('If you have found this poll on a post, ' + . 'please enter a link to the post, the post ID or the access token for the poll:'); + $form = '
' + . '
'; + quit($message_header, $message_text . $form); } catch (\Exception $e) { - die('Something went wrong :( "'.$e->getMessage().'"' . get_page_footer()); + quit('Something went wrong :( "' . $e->getMessage(). '"'); } -$user_avatar_url = $poll->user->getAvatarUrl(50); -$user_avatar_url_srcset = get_source_set($poll->user, 50); +try { + $user_avatar_url = $poll->user->getAvatarUrl(50); + $user_avatar_url_srcset = get_source_set($poll->user, 50); -$username = '@' . $poll->user->username; -$disabled = $poll->canVote() ? '' : 'disabled'; -$user_name = $poll->user->name ?? ''; -$created_at = $poll->created_at; -$closed_at = $poll->closed_at; -$user_votes = $poll->getMyVotes(); + $username = '@' . $poll->user->username; + $disabled = $poll->canVote() ? '' : 'disabled'; + $user_name = $poll->user->name ?? ''; + $created_at = $poll->created_at; + $closed_at = $poll->closed_at; + $user_votes = $poll->getMyVotes(); -$votes_remaining = $poll->max_options - count($user_votes); -$votes_remaining_plural = $votes_remaining === 1 ? '' : 's'; -$votes_remaining_text = "$votes_remaining Vote$votes_remaining_plural remaining"; -$votes_remaining_hidden = $poll->canVote() ? '' : ' hidden'; -$data_can_vote = $poll->canVote() ? 'true' : 'false'; -$disabled_button = ($poll->canVote() && count($user_votes) > 0) ? '' : 'disabled'; + $votes_remaining = $poll->max_options - count($user_votes); + $votes_remaining_plural = $votes_remaining === 1 ? '' : 's'; + $votes_remaining_text = "$votes_remaining Vote$votes_remaining_plural remaining"; + $votes_remaining_hidden = $poll->canVote() ? '' : ' hidden'; + $data_can_vote = $poll->canVote() ? 'true' : 'false'; + $disabled_button = ($poll->canVote() && count($user_votes) > 0) ? '' : 'disabled'; +} catch (\Exception $e) { + quit('Something went wrong :( "' . $e->getMessage(). '"'); +} if (array_key_exists('success', $_GET) && $_GET['success'] == 1) { ?> - + diff --git a/vote_poll.php b/vote_poll.php index 392463d..73a2a7c 100644 --- a/vote_poll.php +++ b/vote_poll.php @@ -12,11 +12,12 @@ if (is_array($_POST['options'])) { try { $res = $api->voteInPoll((int)$_POST['pollid'], $voted_options, $_POST['polltoken']); } catch (\Exception $e) { - get_page_header('Voting error'); - $str = 'Sorry, something went wrong while voting! "' + $header = get_page_header('Voting error'); + $str = $header + . 'Sorry, something went wrong while voting! "' . $e->getMessage() . '"
Please yell at @hutattedonmyarm
' . 'Go back to the poll'; - die($str); + quit($str); } redirect('view_poll.php?id='.$_POST['pollid'].'&success=1');