Add CAPTCHA

main
Pk11 2 years ago
parent 015205fde1
commit 6d9f29121a

@ -26,6 +26,7 @@
*/ */
$start_time = microtime(true); $start_time = microtime(true);
$captcha_str = substr(strtr(sha1($start_time), "1234567890", "ABCDEFGHgh"), 0, 6);
// Return source code // Return source code
if(isset($_GET['source'])) { if(isset($_GET['source'])) {
@ -71,7 +72,7 @@
//// Functions: //// //// Functions: ////
// Adds a post to the database // Adds a post to the database
function post($name, $email, $comment, $img, $save_cookie) { function post($name, $email, $comment, $img, $save_cookie, $captcha, $captcha_answer) {
$extensions = [ $extensions = [
'image/bmp' => '.bmp', 'image/bmp' => '.bmp',
'image/gif' => '.gif', 'image/gif' => '.gif',
@ -95,6 +96,12 @@
$_COOKIE['email'] = $email; $_COOKIE['email'] = $email;
} }
if($captcha != $captcha_answer) {
if(!empty($img['tmp_name']))
@unlink($img['tmp_name']);
return 'Invalid CAPTCHA answer, copy the text on the left into the box';
}
// Validate and move the uploaded image file, if necessary // Validate and move the uploaded image file, if necessary
if(!empty($img['tmp_name'])) { if(!empty($img['tmp_name'])) {
if((($img['type'] == 'image/gif') || ($img['type'] == 'image/jpeg') || ($img['type'] == 'image/pjpeg') if((($img['type'] == 'image/gif') || ($img['type'] == 'image/jpeg') || ($img['type'] == 'image/pjpeg')
@ -362,8 +369,10 @@
$comment = trim($_POST['comment']); $comment = trim($_POST['comment']);
$img = $_FILES['img']; $img = $_FILES['img'];
$save_cookie = isset($_POST['save_cookie']); $save_cookie = isset($_POST['save_cookie']);
$captcha = trim($_POST['captcha']);
$captcha_answer = trim($_POST['captcha_answer']);
$err = post($name, $email, $comment, $img, $save_cookie); $err = post($name, $email, $comment, $img, $save_cookie, $captcha, $captcha_answer);
if($err == "") { if($err == "") {
# Redirect to latest post # Redirect to latest post
$query = 'SELECT post_id FROM posts ORDER BY posts.post_time DESC LIMIT 1'; $query = 'SELECT post_id FROM posts ORDER BY posts.post_time DESC LIMIT 1';
@ -456,6 +465,14 @@
<br /> <br />
<br /> <br />
<label for="captcha">CAPTCHA:</label></td>
<br />
<code><?php echo $captcha_str; ?></code>
<input id="captcha" name="captcha" />
<input type="hidden" name="captcha_answer" value="<?php echo $captcha_str; ?>" />
<br />
<br />
<label for="save-cookie">Save cookie:</label> <label for="save-cookie">Save cookie:</label>
<input type="checkbox" id="save-cookie" name="save_cookie" <?php if($_COOKIE['uid']) echo 'checked'; ?> /> <input type="checkbox" id="save-cookie" name="save_cookie" <?php if($_COOKIE['uid']) echo 'checked'; ?> />
<br /> <br />
@ -482,6 +499,14 @@
<td><label for="img">Image:</label></td> <td><label for="img">Image:</label></td>
<td><input type="file" id="img" name="img" /> (Limit: <?php echo MAX_FILE_SIZE >> 10; ?> KiB)</td> <td><input type="file" id="img" name="img" /> (Limit: <?php echo MAX_FILE_SIZE >> 10; ?> KiB)</td>
</tr> </tr>
<tr>
<td><label for="captcha">CAPTCHA:</label></td>
<td>
<code><?php echo $captcha_str; ?></code>
<input id="captcha" name="captcha" />
<input type="hidden" name="captcha_answer" value="<?php echo $captcha_str; ?>" />
</td>
</tr>
<tr> <tr>
<td><label for="save-cookie">Save cookie:</label></td> <td><label for="save-cookie">Save cookie:</label></td>
<td><input type="checkbox" id="save-cookie" name="save_cookie" <?php if($_COOKIE['uid']) echo 'checked'; ?> /> (Allows deleting your own posts)</td> <td><input type="checkbox" id="save-cookie" name="save_cookie" <?php if($_COOKIE['uid']) echo 'checked'; ?> /> (Allows deleting your own posts)</td>

Loading…
Cancel
Save