dragonpolls/post_poll.php

148 lines
4.3 KiB
PHP
Raw Normal View History

2021-04-05 08:56:15 +00:00
<?php
require_once __DIR__ .'/bootstrap.php';
use APnutI\Entities\Poll;
2021-04-18 13:14:41 +00:00
use APnutI\Entities\Channel;
2021-04-05 08:56:15 +00:00
try {
2021-04-18 11:44:11 +00:00
echo get_page_header('Post Poll', true, ['post_poll']);
2021-04-05 08:56:15 +00:00
} catch (\Exception $e) {
2021-04-17 14:04:34 +00:00
quit('Something went wrong :( "'.$e->getMessage().'"');
2021-04-05 08:56:15 +00:00
}
if (!$api->isAuthenticated(false, true)) {
2021-04-17 14:04:34 +00:00
quit('You need to be logged in to create a new post!');
2021-04-05 08:56:15 +00:00
}
if (!empty($_POST['submit'])) {
if (empty($_POST['poll_id']) || !is_numeric($_POST['poll_id']) || $_POST['poll_id'] <= 0) {
2021-04-17 14:04:34 +00:00
quit('Invalid poll ID');
2021-04-05 08:56:15 +00:00
}
if (empty($_POST['poll_token'])) {
2021-04-17 14:04:34 +00:00
quit('Invalid poll token');
2021-04-05 08:56:15 +00:00
}
if (empty($_POST['post_text'])) {
2021-04-17 14:04:34 +00:00
quit('Invalid text');
2021-04-05 08:56:15 +00:00
}
2021-04-18 13:14:41 +00:00
$channel_id = -1;
if (!empty($_POST['channelid']) && is_numeric($_POST['channelid'])) {
$channel_id = (int)$_POST['channelid'];
}
$broadcast = !empty($_POST['broadcast']);
2021-04-05 08:56:15 +00:00
try {
2021-04-18 13:14:41 +00:00
$channel_invite = [];
$poll_raw = Poll::makePollNoticeRaw($_POST['poll_id'], $_POST['poll_token']);
if ($channel_id > 0) {
# No broadcast, post to channel and end
if (!$broadcast) {
$channel = $api->getChannel($channel_id);
$channel->postMessage($_POST['post_text'], $poll_raw);
redirect('view_poll.php?poll_created=1&id=' . $_POST['poll_id']);
die();
}
# Broadcast, post to global, then to channel
$channel_invite = Channel::makeChannelInviteRaw($channel_id);
}
2021-04-05 08:56:15 +00:00
$params = [
2021-04-18 13:14:41 +00:00
'raw' => array_merge($channel_invite, $poll_raw)
2021-04-05 08:56:15 +00:00
];
2021-04-18 13:14:41 +00:00
$post = $api->createPostWithParameters($_POST['post_text'], $params);
if ($broadcast) {
$channel = $api->getChannel($channel_id);
$broadcast_raw = Channel::makeBroadcastNoticeRaw($post->id);
$channel_raw = array_merge($poll_raw, $broadcast_raw);
$channel->postMessage($_POST['post_text'], $channel_raw);
}
2021-04-05 08:56:15 +00:00
redirect('view_poll.php?poll_created=1&id=' . $_POST['poll_id']);
} catch (\Exception $e) {
2021-04-17 14:04:34 +00:00
quit('Something went wrong creating your post: "' . $e->getMessage() . '"');
2021-04-05 08:56:15 +00:00
}
}
if (empty($_GET['id']) || !is_numeric($_GET['id']) || $_GET['id'] <= 0) {
2021-04-17 14:04:34 +00:00
quit('Invalid poll ID');
2021-04-05 08:56:15 +00:00
}
$poll_token = empty($_GET['poll_token']) ? null : $_GET['poll_token'];
$prompt = empty($_GET['prompt']) ? null : $_GET['prompt'];
$poll_id = (int)$_GET['id'];
$poll = null;
$is_public = false;
try {
$poll = $api->getPoll($poll_id, $poll_token);
$poll_token = $poll->token;
$prompt = $poll->prompt;
$is_public = $poll->is_public;
} catch (\Exception $e) {
// Do nothing, check for token and prompt later
}
if (empty($poll_token)) {
2021-04-17 14:04:34 +00:00
quit('Invalid poll token');
2021-04-05 08:56:15 +00:00
}
if (empty($prompt)) {
2021-04-17 14:04:34 +00:00
quit('Invalid prompt');
2021-04-05 08:56:15 +00:00
}
$poll_id = (int)$_GET['id'];
$dir_name = dirname($_SERVER['SCRIPT_NAME']);
if ($dir_name === '.' || $dir_name === '/') {
$dir_name = '';
}
$scheme = empty($_SERVER['REQUEST_SCHEME']) ? 'http' : $_SERVER['REQUEST_SCHEME'];
$url = $scheme
. '://'
. $_SERVER['HTTP_HOST']
. $dir_name
. '/view_poll.php?id='
. $poll_id;
2021-04-18 11:44:11 +00:00
$channels = [];
$channels_error_banner = '';
try {
$channels = $api->getSubscribedChannels(false);
} catch (\Exception $e) {
$channels_error_banner = make_banner('error', 'Could not load channels: "'.$e->getMessage().'"');
}
echo $channels_error_banner;
2021-04-05 08:56:15 +00:00
?>
Do you want to post about your poll?
<form method="POST" class="post-poll">
<textarea rows="4" cols="50" name="post_text" maxlength="256">I created a new poll:
<?= $prompt ?>
<?= $url ?>
</textarea><br>
<input type="hidden" name="poll_id" value="<?= $poll_id ?>">
<input type="hidden" name="poll_token" value="<?= $poll_token ?>">
2021-04-18 11:44:11 +00:00
<label>Post to channel
<select name="channelid">
<option value="-1">---</option>
<?php
foreach ($channels as $channel) { ?>
<option value="<?= $channel->id ?>"><?= $channel->name ?></option>
<?php } ?>
</select>
</label>
<br />
<label>Broadcast to global
<input type="checkbox" name="broadcast">
</label><br />
2021-04-05 08:56:15 +00:00
<button type="submit" name="submit" value="submit">Post to pnut</button>
</form>
<a href="/view_poll.php?id=<?= $poll_id ?>">Take me straight to the poll</a>
<?php if (!$is_public) { ?>
<p>
Note, that your poll is set to private. You will either need to share your poll with a post,
or give the poll's access token to everyone who should be able to vote in your poll. Your access token is:
<pre><?= $poll_token ?></pre>
</p>
<?php } ?>
2021-04-05 08:56:15 +00:00
<?= get_page_footer() ?>