Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
FiveYellowMice 的提问箱
<?php
function return_error($code, $message) {
http_response_code($code);
header('Content-Type: text/plain');
echo $message."\n";
die();
}
if (in_array($_SERVER['REQUEST_METHOD'], ['HEAD', 'GET'])) {
$alert_type = null;
$alert_msg = null;
} elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (strlen($_POST['question'] ?? null) == 0) {
http_response_code(400);
$alert_type = 'danger';
$alert_msg = "提问不能为空。";
} elseif (strlen($_POST['question']) > 500) {
http_response_code(400);
$alert_type = 'danger';
$alert_msg = "提问不能超过 500 个字符。";
} elseif (strlen($_POST['email'] ?? null) > 100) {
http_response_code(400);
$alert_type = 'danger';
$alert_msg = "OAO 真、真的有这样长的邮件地址嘛。";
} else {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.telegram.org/bot<token>/sendMessage');
curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json; charset=utf-8' ]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
'chat_id' => 123020835,
'text' => "提问箱:\n".$_POST['question']."\n\nEmail: ".($_POST['email'] ?? ''),
'disable_web_page_preview' => true,
]));
$tg_resp = json_decode(curl_exec($ch), true);
curl_close($ch);
if ($tg_resp['ok'] ?? null) {
$alert_type = 'success';
$alert_msg = "发送成功。";
$_POST['question'] = null;
$_POST['email'] = null;
} else {
http_response_code(500);
$alert_type = 'danger';
$alert_msg = "发送失败。 ".($tg_resp['description'] ?? 'unknown');
}
}
} else {
return_error(405, 'Method not allowed.');
}
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>提问箱</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<header class="pt-4 pb-1 px-2 bg-secondary text-light">
<h1 class="text-center">提问箱</h1>
</header>
<div class="my-4 mx-auto px-2" style="max-width: 700px">
<?php if ($alert_type) { ?>
<div class="alert alert-<?= $alert_type ?>" role="alert"><?= $alert_msg ?></div>
<?php } ?>
<p>这里是 FiveYellowMice 的提问箱,在这里可以匿名地向我提出问题,我愿意回答的话,就会回答啦。也不一定是问题,要是有想匿名说的话,在这里也可以发出。如果有被回答的话,该提问将与回答一起被发布到我的 <a href="https://twitter.com/FiveYellowMice" target="_blank">Twitter</a><a href="https://mastodon.xyz/@FiveYellowMice" target="_blank">Mastodon</a> 上。</p>
<p>如果担心 IP 地址被记录什么的,用代理之类的东西就可以了。</p>
<form method="post">
<div class="form-group">
<label for="form-question">提问:</label>
<textarea id="form-question" name="question" class="form-control" rows="2"><?= htmlspecialchars($_POST['question'] ?? '') ?></textarea>
</div>
<div class="form-group">
<label for="form-email">邮件地址(选填):</label>
<input id="form-email" name="email" type="email" class="form-control" aria-describedby="form-email-description" value="<?= htmlspecialchars($_POST['email'] ?? '') ?>">
<small id="form-email-description" class="form-text">若填写了邮件地址,在提问收到回答时将会收到邮件提醒,但我也会看到邮件地址,所以匿名大概就会被破坏了。</small>
</div>
<button type="submit" class="btn btn-primary d-block mx-auto">提交</button>
</form>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.