Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Captcha Plugin

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
PHPeter
Smarty n00b


Joined: 18 Mar 2011
Posts: 3
Location: The Netherlands

PostPosted: Sat Mar 19, 2011 5:24 pm    Post subject: Captcha Plugin Reply with quote

There are a lot of different Captcha-scripts available, i wrote one as a Smarty-plugin. Just add the script to the plugins-folder.

SYNOPSIS:

index.tpl
---------
Required parameters:
-width (px, int)
-height (px, int)
-font (font.ttf)
-fontsize (int)
-font_x (int, x-position first captcha character)
-font_y (int, y-position first captcha character)
-length (int, number of characters to display in the captcha-image)

Allowed parameters:
-color (Captcha characters color, Hex, example: FFCC00, default FFFFFF)
-shadow (Captcha characters shadow-color, Hex, default 888888)
-background (Captcha background color, Hex, default 000000)
-random_angle (Captchatext in random angle, bool, default false)
-cssclass (class-statement for img-tag, string)
-alttext (alt-statement for img-tag, string)
-font_dir (dir where the font.tff is stored, default = SMARTY_DIR . “font/”)
-session_name (Session-key where md5(captcha-key) is stored, default = "captcha")

Example required tags:
{captcha width="250" height="220" font="RomanStonecut.ttf" fontsize="35" font_x="5" font_y="115" length="7"}

Example all tags:
{captcha width="250" height="220" font="RomanStonecut.ttf" fontsize="35" font_x="5" font_y="115" length="7" color="FF0000" shadow="00FF00" background="EEEEEE" alttext="Captcha image" random_angle=true font_dir=”home/users/anotherfolder/font/” session_name=”captcha_in_md5”}

The script can be downloaded at pastebin.com/g9UambkK

Code:
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/


/**
* Smarty {captcha} function plugin
*
* Type: function<br>
* Name: captcha<br>
* Purpose: Display captcha-images in webpages
* @author PHPeter.eu
* @link http://www.phpeter.eu/
* @param array
* @return string
*/

function smarty_function_captcha_setcaptcha($length, $sessname){
    $chars = '23456789ABCDEFGHJKMNPQRSTVWXYZ';
    $code = '';
    $i = 0;
    while ($i < $length) {
     $code .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
     $i++;
    }
    $_SESSION[$sessname] = md5($code);
    return $code;
}

function smarty_function_captcha_verify($plugin='', $param_model, $params) {
    foreach(array('requires', 'allowed', 'deprecated') as $category) {
        if (!array_key_exists($category, $param_model)) {
            $param_model[$category] = array();
        }
    }
    $_missing = array_diff(array_keys($param_model['requires']), array_keys($params));
    if (!empty($_missing)) {
        throw new SmartyException("[$plugin] missing required parameter(s) for ".join(', ', $_missing), E_USER_NOTICE);
    }
   
    foreach ($param_model['requires'] as $field=>$type) {
        if (array_key_exists($field, $params) && !empty($params[$field])) {
            $_params[$field] = $params[$field];
        }
        else{
            throw new SmartyException("[$plugin] supplied parameter unknown for ".$field." = ".$params[$field], E_USER_NOTICE);
        }
    }
    foreach ($param_model['allowed'] as $field=>$type) {
        if (array_key_exists($field, $params) && isset($params[$field])) {
            $_params[$field] = $params[$field];
        }
    }
    foreach ($param_model['deprecated'] as $field=>$type) {
        if (array_key_exists($field, $params)) {
            throw new SmartyException("[$plugin] deprecated parameter '$field'", E_USER_NOTICE);
            $_params[$field] = $params[$field];
        }
    }
    return $_params;
}

function smarty_function_captcha($params){
    $_params = smarty_function_captcha_verify(
    'captcha'
    , array(
    'requires' => array(
    'width' => 'int',
    'height' => 'int',
    'font' => 'string',
    'fontsize' => 'int',
    'font_x' => 'int',
    'font_y' => 'int',
    'length' => 'int'
    )
    , 'allowed' => array(
    'color' => 'hex',
    'background' => 'hex',
    'shadow' => 'hex',
    'random_angle' => 'bool',
    'cssclass' => 'string',
    'alttext' => 'string',
    'font_dir' => 'string',
    'session_name' => 'string'
    )
    , 'deprecated' => array(
    )
    )
    , $params
    );
    $atags = array();
    foreach ($_params as $field=>$value) {
        switch($field){
            case "font":
                $font = $value;
            break;
            case "font_dir":
                define('SMARTY_FONT_DIR', $value . DS);
            break;
            case "fontsize":
                if(is_int((int)$value)){
                    $size = (int)$value;
                }
            break;
            case "font_x":
                if(is_int((int)$value)){
                    $fontx = (int)$value;
                }
            break;
            case "font_y":
                if(is_int((int)$value)){
                    $fonty = (int)$value;
                }
            break;
            case "width":
                if(is_int((int)$value)){
                    $width = (int)$value;
                }
                $atags[] = 'width="'.$value.'"';
            break;
            case "height":
                if(is_int((int)$value)){
                    $height = (int)$value;
                }
                $atags[] = 'height="'.$value.'"';
            break;
            case "length":
                if(is_int((int)$value) && $value >= 1){
                    $length = (int)$value;
                }
                else{
                    $length = 6;
                }
            break;
            case "color":
                if(strlen($value) != 6){
                    throw new SmartyException("[$plugin] wrong value for '$field'", E_USER_NOTICE);
                }
                else{
                    $color_red = hexdec(substr($value, 0, 2));
                    $color_green = hexdec(substr($value, 2, 2));
                    $color_blue = hexdec(substr($value, 4, 2));
                }
            break;
            case "shadow":
                if(strlen($value) != 6){
                    throw new SmartyException("[$plugin] wrong value for '$field'", E_USER_NOTICE);
                }
                else{
                    $shadow_red = hexdec(substr($value, 0, 2));
                    $shadow_green = hexdec(substr($value, 2, 2));
                    $shadow_blue = hexdec(substr($value, 4, 2));
                }
            break;
            case "background":
                if(strlen($value) != 6){
                    throw new SmartyException("[$plugin] wrong value for '$field'", E_USER_NOTICE);
                }
                else{
                    $back_red = hexdec(substr($value, 0, 2));
                    $back_green = hexdec(substr($value, 2, 2));
                    $back_blue = hexdec(substr($value, 4, 2));
                }
            break;
            case "random_angle":
                if(is_bool($value)){
                    $rand_angle = $value;
                }
                else{
                    $rand_angle = false;
                }
                if($rand_angle == true){
                    $angle = (rand(0,50) - 25);
                }
                else{
                    $angle = 0;
                }
            break;
            case "cssclass":
                $atags[] = 'class="'.$value.'"';
            break;
            case "alttext":
                $atags[] = 'alt="'.$value.'"';
            break;
            case "session_name":
                $sessionname = $value;
            break;
            default:
                throw new SmartyException("[$plugin] unknown parameter '$field'", E_USER_NOTICE);
            break;
        }
    }
    if(!isset($sessionname) || strip($sessionname) == ""){
        $sessionname = "captcha";
    }
    $captcha = smarty_function_captcha_setcaptcha($length, $sessionname);
    if(!isset($angle) || !is_numeric($angle) || $angle > 25 || $angle < -25){
        $angle = 0;
    }
    $stags = implode(" ", $atags);
    if(!isset($color_red)){
        $color_red = 255;
        $color_green = 255;
        $color_blue = 255;
    }
    if(!isset($shadow_red)){
        $shadow_red = 128;
        $shadow_green = 128;
        $shadow_blue = 128;
    }
    if(!isset($back_red)){
        $back_red = 0;
        $back_green = 0;
        $back_blue = 0;
    }
    if (!defined('SMARTY_FONT_DIR')) {
        define('SMARTY_FONT_DIR', SMARTY_DIR . 'font' . DS);
    }
    $im = imagecreatetruecolor($width, $height);
    $font_captcha_col = imagecolorallocate($im, $color_red, $color_green, $color_blue);
    $shadow_captcha = imagecolorallocate($im, $shadow_red, $shadow_green, $shadow_blue);
    $background_captcha = imagecolorallocate($im, $back_red, $back_green, $back_blue);
    imagefilledrectangle($im, 0, 0, $width, $height, $background_captcha);
    $font = SMARTY_FONT_DIR . $font;
    imagettftext($im, $size, $angle, $fontx, $fonty, $shadow_captcha, $font, $captcha);
    imagettftext($im, $size, $angle, ($fontx + 5), ($fonty + 8), $font_captcha_col, $font, $captcha);
    ob_start();
    imagegif($im);
    imagedestroy($im);
    $cap_img_str = ob_get_clean();
    $cap_img_crypt = base64_encode($cap_img_str);
    return "<img src=\"data:image/gif;base64,".$cap_img_crypt."\" ".$stags." />";

}

?>
Back to top
View user's profile Send private message Send e-mail Visit poster's website
narian7
Smarty n00b


Joined: 23 Sep 2011
Posts: 1

PostPosted: Fri Sep 23, 2011 4:30 am    Post subject: Black box problem Reply with quote

Thank you for creating the plugin, Peter. It looks very thorough.

I am getting a black box with no text inside the box. I put a DejaVuSans.ttf font in a 'font' folder in the Smarty directory and in the tpl file, included the following:

{captcha width="150" height="120" font="DejaVuSans.ttf" fontsize="15" font_x="5" font_y="5" length="7" color="FF0000"}

However, the captcha is not appearing. Do you have any suggestions?
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP