Adding captcha to PunBB. This post has been originally put in Russian in our old blog at www.newlife.com.ua. Dated 23rd of April 2008. Written by Vladimir Boytchentsov, translated by Taras Filatov.
In post.php before:
define(’PUN_ROOT’, ‘./’);
add the code:
session_start();
after code:
// Otherwise it should be in $_POST
else
{
add this:
// IMAGE VERIFICATION MOD BEGIN
// Image verifcation
if ($pun_config['o_regs_verify_image'] == ‘1′)
{
// Make sure what they submitted is not empty
if (trim($_POST['req_image']) == ”) {
unset($_SESSION['captcha_keystring']);
$errors[] = $lang_post['Text mismatch'];
// message($lang_post['Text mismatch']);
}
if($_SESSION['captcha_keystring'] != strtolower(trim($_POST['req_image']))) {
unset($_SESSION['captcha_keystring']);
$errors[] = $lang_post['Text mismatch'];
// message($lang_post['Text mismatch']);
}
if(!isset($_SESSION['captcha_keystring'])){
unset($_SESSION['captcha_keystring']);
$errors[] = $lang_post['Text mismatch2'];
// message($lang_common['Bad request']);
}
unset($_SESSION['captcha_keystring']);
}
// IMAGE VERIFICATION MOD END
then we have to add the captcha itself into the reply form. I have it after the following code:
?> <label class=”conl”><strong><?php echo $lang_post['Guest name'] ?></strong><br /><input type=”text” name=”req_username” value=”<?php if (isset($_POST['req_username'])) echo pun_htmlspecialchars($username); ?>” size=”25″ maxlength=”25″ tabindex=”<?php echo $cur_index++ ?>” /><br /></label>
<label class=”conl”><?php echo $email_label ?><br /><input type=”text” name=”<?php echo $email_form_name ?>” value=”<?php if (isset($_POST[$email_form_name])) echo pun_htmlspecialchars($email); ?>” size=”50″ maxlength=”50″ tabindex=”<?php echo $cur_index++ ?>” /><br /></label>
<div class=”clearer”></div>
add the code:
<?php
// IMAGE VERIFICATION MOD BEGIN
if ($pun_config['o_regs_verify_image'] == ‘1′): ?>
<div class=”inform”>
<div class=”infldset”>
<label id=”gray”><img style=”border: 1px solid #aaa;” src=”<?php echo $pun_config['o_base_url']?>/include/captcha/captcha.php?<?php echo session_name()?>=<?php echo session_id()?>”> <strong><?php echo $lang_post['Image text'] ?></strong> <input type=”text” name=”req_image” size=”16″ maxlength=”16″ /></label>
</div>
</div>
<?php endif;
// IMAGE VERIFICATION MOD END
?>
and around line 400 you will find the following code:
$required_fields['req_username'] = $lang_post['Guest name'];
replace it with the following:
$required_fields = array(’req_email’ => $lang_common['E-mail'], ‘req_subject’ => $lang_common['Subject'], ‘req_message’ => $lang_common['Message'], ‘req_username’ => $lang_post['Guest name'], ‘req_image’ => $lang_post['Imgtxt']);
now we have a working post.php, but we also have to add the language constants.
we will add it to the folder /lang/… [ language ]…/post.php
so we do for each language we want to add
I did it like this:
in lang/russian/post.php:
(you have to add after coma or in the end or between two array elements)
Code:
//Image Verification stuff
//’Image verification’ => ‘Введите правильный текст с картинки’,
‘Imgtxt’ => ‘Текст с картинки’,
‘Image text’ => ‘Введите текст с картинки:’, // ‘Текст’,
//’Image info’ => ‘Введите текст, который Вы видите на картинке. Это необходимо для защиты от спам-ботов, извините за неудобство.’,
‘Text mismatch’ => ‘Введённый Вами текст не совпадает с картинкой’,
‘Text mismatch2′ => ‘Попробуйте правильно ввести текст. Не обновиляйте страницу<br /> Но: если в Вашем браузере отключены картинки и вы открыли эту страницу, сперва включите картинки, затем обновите её.<span style=”color: #aa0000″> При обновлении текст потеряется, поэтому скопируйте его в буфер обмена!</span>’,
In lang/english/post.php:
(you have to add after coma or in the end or between two array elements)
Code:
//Image Verification stuff
//’Image verification’ => ‘Image Verification’,
‘Imgtxt’ => ‘Text from image’,
‘Image text’ => ‘Input text from image to text box’, //’Image Text’,
//’Image info’ => ‘Please copy the text in the image to the text box above’,
‘Text mismatch’ => ‘Please make sure the text and the image match’,
‘Text mismatch2′ => ‘Try again input the text from image. Don\’t reload page<br /> but: if your browser not show images, and you open this page, first turn on images-show, after reload this page.<span style=”color: #aa0000″> If you reloaded page, text in message area will be lost, then copy it\’s to copy-buffer!</span>’,
The files of captcha in the attached file we put into folder: include/captcha/
into register.php before
define(’PUN_ROOT’, ‘./’);
we should add this:
session_start();
then after:
if ($db->num_rows($result))
message(’За последние 10 минут с вашего IP уже регистрировался новый пользователь. Для предотвращения флуд-регистрации, с одного и того же IP можно регистрировать пользователя не чаще чем раз в 10 минут. Простите за неудобство.’);
add this:
// IMAGE VERIFICATION MOD BEGIN
// Image verifcation
if ($pun_config['o_regs_verify_image'] == ‘1′)
{
// Make sure what they submitted is not empty
if (trim($_POST['req_image']) == ”) {
unset($_SESSION['captcha_keystring']);
message($lang_register['Text mismatch']);
}
// Каптча содержит только латиницу. Она работает. Золотое правило -
// Если это работает, то не трогай это! ![]()
if($_SESSION['captcha_keystring'] != strtolower(trim($_POST['req_image']))) {
unset($_SESSION['captcha_keystring']);
message($lang_register['Text mismatch']);
}
if(!isset($_SESSION['captcha_keystring'])){
unset($_SESSION['captcha_keystring']);
message($lang_common['Bad request']);
}
unset($_SESSION['captcha_keystring']);
}
// IMAGE VERIFICATION MOD END
then after
<p class=”clearb”><?php echo $lang_register['Pass info'] ?></p>
</div>
</fieldset>
</div>
<?php endif; ?> <div class=”inform”>
add the following code:
<?php
// IMAGE VERIFICATION MOD BEGIN
if ($pun_config['o_regs_verify_image'] == ‘1′): ?>
<div class=”inform”>
<fieldset>
<legend><?php echo $lang_register['Image verification'] ?></legend>
<div class=”infldset”>
<img id=”capchaimg” src=”<?php echo $pun_config['o_base_url']?>/include/captcha/captcha.php?<?php echo session_name()?>=<?php echo session_id()?>”><br />
<label class=”conl”><strong><?php echo $lang_register['Image text'] ?></strong><br /><input type=”text” name=”req_image” size=”16″ maxlength=”16″ /><br /></label>
<p class=”clearb”><?php echo $lang_register['Image info'] ?></p>
</div>
</fieldset>
</div>
<?php endif;
// IMAGE VERIFICATION MOD END
?>
then we go to /lang/Russian/register.php
and, similarly, add:
Code:
//Image Verification stuff
‘Image verification’ => ‘Введите правильный текст с картинки’,
‘Image text’ => ‘Текст’,
‘Image info’ => ‘Введите текст, который Вы видите на картинке. Это необходимо для защиты от регистрации спам-ботов, извините за неудобство.’,
‘Text mismatch’ => ‘Введённый Вами текст не совпадает с картинкой’,
now we go to /lang/English/register.php
and, similarly, add:
Code:
//Image Verification stuff
‘Image verification’ => ‘Input text from image’,
‘Image text’ => ‘Image Text’,
‘Image info’ => ‘Please copy the text in the image to the text box above’,
‘Text mismatch’ => ‘Please make sure the text and the image match’,
‘Text mismatch2′ => ‘Try again input the text from image. Don’t reload page<br /> but: if your browser not show images, and you open this page, first turn on images-show, after reload this page.<span style=”color: #aa0000″> If you reloaded page, text in message area will be lost, then copy it’s to copy-buffer!</span>’,
(note: I’ve edited both English and Russian texts a little)
Into admin_options.php after the:
if ($form['maintenance_message'] != ”)
$form['maintenance_message'] = pun_linebreaks($form['maintenance_message']);
else
{
$form['maintenance_message'] = ‘The forums are temporarily down for maintenance. Please try again in a few minutes.\n\n/Administrator’;
if ($form['maintenance'] == ‘1′)
$form['maintenance'] = ‘0′;
}
we should add this:
// IMAGE VERIFICATION MOD BEGIN
if ($form['regs_verify_image'] != ”)
$form['regs_verify_image'] = pun_linebreaks($form['regs_verify_image']);
else
{
if ($form['regs_verify_image'] == ‘1′)
$form['regs_verify_image'] = ‘0′;
}
// IMAGE VERIFICATION MOD END
Then after:
<th scope=”row”>Maintenance message</th>
<td>
<textarea name=”form[maintenance_message]” rows=”5″ cols=”55″><?php echo pun_htmlspecialchars($pun_config['o_maintenance_message']) ?></textarea>
<span>The message that will be displayed to users when the board is in maintenance mode. If left blank a default message will be used. This text will not be parsed like regular posts and thus may contain HTML.</span>
</td>
</tr>
</table>
</div>
</fieldset>
</div>
Add this:
<?// IMAGE VERIFICATION MOD BEGIN?>
<div class=”inform”>
<fieldset>
<legend>Anti-Bot Protect</legend>
<div class=”infldset”>
<table class=”aligntop” cellspacing=”0″>
<tr>
<th scope=”row”><a name=”Captcha”>Captcha</a></th>
<td>
<input type=”radio” name=”form[regs_verify_image]” value=”1″<?php if ($pun_config['o_regs_verify_image'] == ‘1′) echo ‘ checked=”checked”‘ ?> /> <strong>Yes</strong> <input type=”radio” name=”form[regs_verify_image]” value=”0″<?php if ($pun_config['o_regs_verify_image'] == ‘0′) echo ‘ checked=”checked”‘ ?> /> <strong>No</strong>
<span>When enabled, guests of your forum will see captcha before posting message.</span>
</td>
</tr>
</table>
</div>
</fieldset>
</div>
<?// IMAGE VERIFICATION MOD END?>
Then we add into db, table config, a new record “o_regs_verify_image” equal to “1″ and edit the cached config in folder /cache/ (file cache_config.php), add to $pun_config the following line:
leave a reply