Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2 3 ... 5

Author Topic: [MOD] Images & Words Captcha  (Read 40916 times)

aRyo S

  • Newbie
  • *
  • Karma: 4
  • Posts: 20
    • YoRapid
[MOD] Images & Words Captcha
« on: July 25, 2007, 11:19:18 AM »

Hope this can be useful for site which most of their visitors not good in math ;)

Code: [Select]
##
##          Mod title:  Images & Words Captcha
##
##        Mod version:  1.0
##     Works on sNews:  1.6.0 Stable
##       Release date:  2007-07-25
##             Author:  Aryo Sandiyudo (MoDelight.org)
##
##        Description:  This mod will replace default Math Captcha with Pretty Captcha
##
##                      Features:
##                      1. No processing on the client side
##                      2. Not text based or hidden field based
##                      3. Not readable by a plain OCR program
##                      4. Have at least some level of complexity for image readers
##                      5. Can be customized, and look nice
##
## Affected functions:  - mathCaptcha()
##                      - snews_startup()
##                      - comment($freeze_status)
##                      - Contact()
##                      - login()
##                      - title()
##
##      Related files:  Copy to /
##                      - captcha.php
##                      - captcha.ttf
##
##         Affects DB:  No
##
##              Notes:  snews.php (moded) is provided for testing
##
##            Credits:  Pretty Captcha by Constantinos Neophytou (http://www.cneophytou.com/)
##
##         DISCLAIMER:  Please note that "mods" are not officially supported by
##                      sNews. Installation of this modification is done at your
##                      own risk. Backup your site database and any and all
##                      applicable files before proceeding.
##
Download here

Live Demo: MoDelight

Screenshot:


Now lets begin the mod:

1. Replace function, mathCaptcha with imgCaptcha

FIND, line 1750
Code: [Select]
// MATH CAPTCHA
function mathCaptcha($input='', $sum='') {
    $length = 4;
if (isset($_POST['calc'])) {
    if (is_numeric($input) && strlen($sum) == $length) {$math = substr(md5($input),0,$length) === $sum ? $input : null;}
}
    else {
        $x = rand(1, 9); $y = rand(1, 9);
        $sum = substr(md5($x+$y),0,$length);
        $math = '<p><label for="calc">* '.l('math_captcha').':</label>
';
        $math .= $x.' + '.$y.' = ';
        $math .= '<input type="text" name="calc" id="calc" /></p>';
        $math .= '<p><input type="hidden" name="sum" value="'.$sum.'" /></p>';
    }
    return $math;
}
REPLACE with
Code: [Select]
// IMAGE CAPTCHA
function imgCaptcha($input='') {
if (isset($_POST['code'])) {
if ($input != $_SESSION['captcha']) {$img = null;}
else {$img = $input;}
}
    else {
        $img = '<p><label for="code">* '.l('captcha_code').':</label>
';
        $img .= '<a href="#" onclick="this.blur();new_captcha();return false;"><img src="captcha.php?'.time().'" id="captcha_img" alt="'.l('captcha_reload').'" title="'.l('captcha_reload').'" /></a>
';
        $img .= '<input type="text" name="code" id="code" /></p>';
    }
unset($_SESSION['captcha']);
    return $img;
}

2. @ STARTUP function

FIND, line 299
Code: [Select]
mathCaptcha($_POST['calc'], $_POST['sum'])REPLACE with
Code: [Select]
imgCaptcha($_POST['code'])
3. @ COMMENTS function

FIND, line 589
Code: [Select]
is_numeric($_POST['calc']) && mathCaptcha($_POST['calc'], $_POST['sum'])REPLACE with
Code: [Select]
imgCaptcha($_POST['code'])
4. Still @ COMMENTS function

FIND, line 652
Code: [Select]
mathCaptcha()REPLACE with
Code: [Select]
imgCaptcha()
5. @ CONTACT FORM function

FIND, line 755
Code: [Select]
mathCaptcha()REPLACE with
Code: [Select]
imgCaptcha()
6. Still @ CONTACT FORM function

FIND, line 779
Code: [Select]
mathCaptcha($_POST['calc'], $_POST['sum'])REPLACE with
Code: [Select]
imgCaptcha($_POST['code'])
7. @ LOGIN function

FIND, line 1008
Code: [Select]
mathCaptcha()REPLACE with
Code: [Select]
imgCaptcha()
8. Add javascript function for reloading captcha when user click on the image

FIND
Code: [Select]
// dependancy limiter
function dependancy() {
var category = document.forms['post']['define_category'];
var page = document.getElementById('def_page');
page.style.display = category.options[category.selectedIndex].value == '-1' ? 'inline' : 'none';
}
//]]>
</script>
<?php ?>
REPLACE with
Code: [Select]
// dependancy limiter
function dependancy() {
var category = document.forms['post']['define_category'];
var page = document.getElementById('def_page');
page.style.display = category.options[category.selectedIndex].value == '-1' ? 'inline' : 'none';
}
//]]>
</script>
<?php }

function 
js_global() { ?>

<script type="text/javascript">
function new_captcha()
{
if(document.getElementById)
{
// extract image name from image source (i.e. cut off ?randomness)
thesrc = document.getElementById("captcha_img").src;
thesrc = thesrc.substring(0,thesrc.lastIndexOf(".")+4);
// add ?(random) to prevent browser/isp caching
document.getElementById("captcha_img").src = thesrc+"?"+Math.round(Math.random()*100000);
} else {
alert("Sorry, cannot autoreload captcha image\nSubmit the form and a new image will be loaded");
}
}
</script>
<?php ?>

9. Output the js_global (javascript function) at header of any page

@ TITLE function, before
Code: [Select]
if ($_SESSION[db('website').'Logged_In'] == token()) {js();}ADD
Code: [Select]
js_global();
10. Alter the language

FIND, line 68
Code: [Select]
$l['ce_reasons'] = '<strong>Possible reasons:</strong> You left blank column, comment is too short or you haven\'t entered the right math captcha code.';REPLACE with
Code: [Select]
$l['ce_reasons'] = '<strong>Possible reasons:</strong> You left blank column, comment is too short or you haven\'t entered the right captcha code.';
and also FIND, line 74
Code: [Select]
$l['math_captcha'] = 'Perform an addition of two integers to avoid spam';REPLACE with
Code: [Select]
$l['captcha_code'] = 'Confirmation code';
$l['captcha_reload'] = 'Click to reload captcha image';
That's it :)
« Last Edit: February 19, 2008, 09:31:01 AM by aRyo S »
Logged

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 955
[MOD] Images & Words Captcha
« Reply #1 on: July 25, 2007, 12:31:27 PM »

Nice ... still, there is one thing to notice: Pretty Captcha code is licensed under Creative Commons Attribution-Noncommercial-Share Alike 3.0

So, commercial users, beware ;)
Logged

piXelatedEmpire

  • MIA
  • Thread Moderator
  • ULTIMATE member
  • *****
  • Karma: 37
  • Posts: 1454
  • currently MIA
[MOD] Images & Words Captcha
« Reply #2 on: July 25, 2007, 12:34:57 PM »

What a terrific MOD!  Well done whiteh0rs3 a great first post! Welcome to the sNews community!
Logged
my apologies to the sNews crew, but I will be MIA for the forseeable future

aRyo S

  • Newbie
  • *
  • Karma: 4
  • Posts: 20
    • YoRapid
[MOD] Images & Words Captcha
« Reply #3 on: July 26, 2007, 11:41:45 AM »

If you worried about license, here is a simple one which is free

Screenshot:


Step by step:

1. Change one line inside imgCaptcha function above

FIND, line 1760
Code: [Select]
$img .= '<a href="#" onclick="this.blur();new_captcha();return false;"><img src="captcha.php?'.time().'" id="captcha_img" alt="'.l('captcha_reload').'" /></a>
';
REPLACE with
Code: [Select]
$img .= '<a href="#" onclick="this.blur();new_captcha();return false;"><img src="captcha/image.php?'.time().'" id="captcha_img" alt="'.l('captcha_reload').'" /></a>
';

2. Save this as image.php, and put inside directory "captcha"
Code: [Select]
<?php
// Start session
session_start();

// This file is a PNG picture
header("Content-type: image/png");

// Generate random number
$string rand(09) . rand(09) . rand(09) . rand(09);

// Save the string
$_SESSION['captcha'] = $string;
setcookie('yo_captcha''true'time()+3600'/');

// Create image
$create_image imagecreatefrompng("background.png");

// Write text on the image
$write_text imagecolorallocate($create_image200200200);

// Define String
imagestring($create_image5355,  $string$write_text);

// Create PNG image
imagepng($create_image);

// Destroy image
imagedestroy($create_image);
?>

3. Create any .png image with size 100x25 pixels,
save as background.png and then put in same directory where captcha.php take place
« Last Edit: February 19, 2008, 09:39:39 AM by aRyo S »
Logged

Ghassem Tofighi

  • Full Member
  • ***
  • Karma: 9
  • Posts: 105
    • http://ght.ir
[MOD] Images & Words Captcha
« Reply #4 on: August 15, 2007, 06:53:46 PM »

I write such captcha mod but I add a feature on it. in setting menu you can select use Math captcha or Image Captcha. Can you add this feature too?
Logged
Be A Color Pencil For Painting The World !

aRyo S

  • Newbie
  • *
  • Karma: 4
  • Posts: 20
    • YoRapid
[MOD] Images & Words Captcha
« Reply #5 on: August 16, 2007, 06:13:20 AM »

Sounds great, it can be a separate mods. Recently i don't need that such a mod perhaps anyone in here can do that for you.
Logged

Ken Dahlin

  • sNews Dude
  • Full Member
  • *****
  • Karma: 30
  • Posts: 143
    • http://www.kendahlin.com/
[MOD] Images & Words Captcha
« Reply #6 on: August 16, 2007, 06:55:09 PM »

I always thought a simple math captcha wasn't the best idea, maybe it's more accessible for the blind or international, but I'm sure a comment spambot could be made to defeat the default more readily than images. Fortunately, I'm only bothered by spammers every week or so, but that just means I don't have a website popular enough for them to bother me.  :(

Thank you for your work on this mod!
Logged

Joost

  • Guest
[MOD] Images & Words Captcha
« Reply #7 on: August 17, 2007, 12:14:26 AM »

Quote from: kendahlin
I always thought a simple math captcha wasn't the best idea, maybe it's more accessible for the blind or international, but I'm sure a comment spambot could be made to defeat the default more readily than images. Fortunately, I'm only bothered by spammers every week or so, but that just means I don't have a website popular enough for them to bother me.  :(

Thank you for your work on this mod!
IMO it is not a question whether a spambot can be designed that is able to submit comments. We know it can be done. But these spambots are only built for popular captchas. It makes no sense to built one for a captcha that is hardly used. Then, it would be much easier to submit spam yourself.
Logged

philmoz

  • High flyer
  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 2001
    • fiddle 'n fly
[MOD] Images & Words Captcha
« Reply #8 on: August 17, 2007, 01:39:25 AM »

Lovely mod. thx whiteh0rs3
Logged
Of all the things I have lost, it is my mind that I miss the most.

Vasile Rusnac

  • Newbie
  • *
  • Karma: 7
  • Posts: 49
Re: [MOD] Images & Words Captcha
« Reply #9 on: September 10, 2007, 05:48:55 PM »

Hi, just wanted to share my experience testing the mod:
it works ok with the contact and login form but when I come to comment I always receive the error message saying that "message not send".

well, it is normal to complete incorrectly the field once or twice :) but it is already tens times since I can not write correctly the captcha code.
I was working with your original file download from the link you provided
I have also tried both captcha files but nothing good.

will appreciate any help. thank you.
Logged

philmoz

  • High flyer
  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 2001
    • fiddle 'n fly
Re: [MOD] Images & Words Captcha
« Reply #10 on: September 12, 2007, 06:57:53 AM »

I think it might be a timeout issue with the captcha image.
... tho' not sure
Logged
Of all the things I have lost, it is my mind that I miss the most.

aRyo S

  • Newbie
  • *
  • Karma: 4
  • Posts: 20
    • YoRapid
Re: [MOD] Images & Words Captcha
« Reply #11 on: September 12, 2007, 08:45:16 AM »

Oops it's my mistake

FIX: please look in step 3 above

@ COMMENTS function

FIND, line 589
Code: [Select]
is_numeric($_POST['calc']) && mathCaptcha($_POST['calc'], $_POST['sum'])
REPLACE with
Code: [Select]
imgCaptcha($_POST['code'])
Logged

Vasile Rusnac

  • Newbie
  • *
  • Karma: 7
  • Posts: 49
Re: [MOD] Images & Words Captcha
« Reply #12 on: September 12, 2007, 11:08:38 AM »

Thank you for intervention, now it works flawlessly
Logged

aRyo S

  • Newbie
  • *
  • Karma: 4
  • Posts: 20
    • YoRapid
Re: [MOD] Images & Words Captcha
« Reply #13 on: September 12, 2007, 11:28:51 AM »

your welcome :)
please don't hesitate to ask if you have any other problem and thank you for the time testing it
Logged

Vasile Rusnac

  • Newbie
  • *
  • Karma: 7
  • Posts: 49
Re: [MOD] Images & Words Captcha
« Reply #14 on: September 12, 2007, 01:07:17 PM »

Hi again,

since you have presented 2 captcha generation scripts I have tested them both and stopped over the simple one, just to understand how it works

following the captcha example from http://psyrens.com/captcha/ I have tried to modify a bit your simple script and made it look more or less as the psyrens one.
here is it what I came to:
Code: [Select]
<?php
// Start session
session_start();

// This file is a PNG picture
header("Content-type: image/png");
header('Cache-control: no-cache');

// Create image
$image imagecreatefrompng("background.png");

// Write text on the image
$colour imagecolorallocate($image183178152);

// Set the font
$font 'fonts/Anorexia.ttf';

// Set a random integer for the rotation between -15 and 15 degrees
$rotate rand(-1515);

// Generate random number
$char strtoupper(substr(str_shuffle('abcdefghjkmnpqrstuvwxyz'), 02));
$string rand(17) . rand(17) . $char rand(17);

// Save the string
$_SESSION['captcha'] = $string;
setcookie('yo_captcha''true'time()+3600'/');

// Define String
imagettftext($image16$rotate2530$colour$font$string);

// Create PNG image
imagepng($image);

?>
it works fine on my localhost (PHP Version 5.2.3, Windows) except it is case sensitive and all letters appear uppercase.
but when I upload it to my server (PHP Version 5.1.6, Unix) I can not get the image loaded, and when I look to the error_log I can see the explanation:
PHP Fatal error:  Call to undefined function imagettftext()

so, for my server I came to the code you have presented, with small modifications:
Code: [Select]
<?php
// Start session
session_start();

// This file is a PNG picture
header("Content-type: image/png");

// Generate random number
$char strtoupper(substr(str_shuffle('abcdefghjkmnpqrstuvwxyz'), 03));
$string rand(19) . rand(19) . $char rand(19);

// Save the string
$_SESSION['captcha'] = $string;
setcookie('yo_captcha''true'time()+3600'/');

// Create image
$create_image imagecreatefrompng("background.png");

// Write text on the image
$write_text imagecolorallocate($create_image200200200);

// Define String
imagestring($create_image104020,  $string$write_text);

// Create PNG image
imagepng($create_image);

// Destroy image
imagedestroy($create_image);
?>

so, my question is why on my localhost imagettftext function works while on the server doesnt.
Thank you in advance.
Logged
Pages: [1] 2 3 ... 5