Giter Site home page Giter Site logo

kohana-captcha's Introduction

#Captcha for Kohana 3.x

This is the Captcha library ported from Kohana 2.3.x to 3.x. Very little has changed API-wise, although there have been a few changes.

##Getting Started

Instantiate a captcha:

$captcha = Captcha::instance();

Instantiate using your own config group (other than 'default'):

$captcha = Captcha::instance('myconfig');

Render a captcha:

$captcha->render();

or just:

$captcha;

Validate the captcha:

Captcha::valid($_POST['captcha']);

By default image-based captchas are rendered with HTML, the HTML is a very simple tag. If you want to handle your own rendering of the captcha simply set the first parameter for render() to FALSE:

$captcha->render(FALSE);

##Captcha Styles

  • alpha
  • basic
  • black
  • math
  • riddle
  • word

kohana-captcha's People

Contributors

bistory avatar davidvandertuijn avatar kolanos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kohana-captcha's Issues

Fix headers in ko 3.2 (for png only)

/controller/captcha
public function action_index($group = 'default')
{
+ $this->response->headers('Content-Type' , 'image/png');
+ $this->response->headers('Cache-Control' , 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
+ $this->response->headers('Pragma' , 'no-cache');
+ $this->response->headers('Connection' , 'close');
Captcha::instance($group)->render(FALSE);
}

Typo Upper/LowerCase

Your Version fails with Kohana V3.3.
So you must rename all Variables/Files to right Case.

Kohana 3.3 is case sensitiv so when you call the Class Captcha and the file is named captcha.php it will fail.

To Fix -> rename the file to Captcha.php so Kohana can find it.

Obsolote and not working anymore:

In Word.php under classes/Captcha line 22 Kohana:Config is not longer exist in 3.3 and is now -> Kohana::$config->load('captcha.words')

In Line 29 -> utf8::strlen($word) change it to UTF8::strlen($word) in fact case sensitive writing.

Proposition: setting image content-length header correctly and adding gzip output config option

Hi Michael,
Nice to see you have used my bugs fixes in your corrections.

Here is the working code for other suggestions i like you to consider.
I have tested them on the 9 main browsers and it seems to work correctly

This proposition mainly adds the following functionalities
1: sends the headers to the browser before outputting the image (and not after)
2: compute the content length correctly :-)
3: add gzip output compression config option and the code in order to gzip the image
4. exit the system

A- file: config/captcha.php
Add gzip compression configuration option

// Group Options:
// [+] gzip gzip compression flag, set to TRUE to enable gzip compression when outputting of the image resource

'default' => array( // [+] 'gzip' => FALSE ),

B- file: classes/captcha.php

public static $config = array( // [ + ] 'gzip' => FALSE );

public function image_render($html)
{
// Output html element
if ($html === TRUE)
return 'Captcha';

    // [-] [+] Main modifications are there

    // Pick the correct output function
    $function ='image'.$this->image_type;

    // Get the request instance singleton
    $request = Request::instance();

    // Detect the server HTTP protocol to use (HTTP/1.0 or HTTP/1.1) and set the HTTP status line header, code 200 and message OK (by default)
    $request->headers[ (isset($_SERVER['SERVER_PROTOCOL']))? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1' ] = $request->status.' '.Request::$messages[$request->status];

    // Set the correct HTTP headers to send to the browser
    $request->headers['Content-Type'] = 'image/'.$this->image_type;
    $request->headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0';
    $request->headers['Pragma'] = 'no-cache';


    // Check gzip compression config flag, if TRUE set content encoding header to gzip and output the image gzipped
    if(Captcha::$config['gzip'] === TRUE)
    {
            // Set the content encoding header to gzip 
            $request->headers['Content-Encoding'] = 'gzip';

            // Send the headers before outputing the image 
            $request->send_headers();

             // START the output buffer
             ob_start();

             // Start output buffer with ob_gzhandler callback function to gzip the output, in order to reduce captcha image response time
             // @note that the gzip compression gain is very small unless image uses big width and height
             ob_start("ob_gzhandler");

            // Output the image resource
            $function($this->image);

            // Flush gzipped ouput buffer
            ob_end_flush();
    }
    else
    {
            // Send the headers before outputing the image 
            $request->send_headers();

            // START the output buffer
            ob_start();

            // Output the image resource
            $function($this->image);

    }

    // Send the Content-Length header by getting the output buffer length
    header('Content-Length: '.ob_get_length(), TRUE);
    header('Connection: close', TRUE);

    // Flush (send) the output buffer and turn off output buffering
    ob_end_flush();

    // Free up the image resource from memory
    imagedestroy($this->image);

}

C- file: controller/captcha.php

class Controller_Captcha extends Controller
{

// This line should be removed
// [-] public $auto_render = FALSE;

public function action_index($group = 'default')
{
        Captcha::instance($group)->render(FALSE);
}

public function after()
{
        Captcha::instance()->update_response_session();

        // [+] exit from system as we have already sent the request headers and outputed the image as response to the browser
        exit;
}

} // End Captcha_Controller

I hope you will understand why the [output -> headers -> image] sequence is better designed than in the previous render method

Best regards
Philippe Jos

Text based CAPTCHAs don't call update_response_session()

As text based CAPTCHAs (riddle, math) are rendered without using the route or controller, the method update_response_session() is never called.
Adding this line before the return (string) in render() will fix this;

$this->update_response_session();

I guess making captcha.php like;

public function render($html = TRUE);
{
    if ($html)
        $this->update_response_session();
}

and (eg riddle.php) like;

public function render($html = TRUE)
    parent::render($html);
    return $this->riddle;
}

and taking update_response_session() out of the controller would do the trick.
A bit ugly though. Maybe HMVC is the answer.

Note: the if ($html) above stops update_response_session() being called twice.

[Bug Report] undefined header "Content-Type" for output the captcha challenge

Output the captcha challenge on captcha controller is working well on IE browser,but it seem get in trouble on Firefox or Chrome.So I do some fix as follow:

BEFORE:

$this->request->response = Captcha::instance($group)->render(FALSE);

AFTER:

$captcha = Captcha::instance($group)->render(FALSE);
$this->request->headers['Content-Type'] = File::mime($captcha);
$this->request->headers['Content-length'] = filesize($captcha);
$this->request->response = $captcha;

It is working well on all browsers :)

CAPTCHA controller error

public function after()
{
    Captcha::update_response_session();
}

"Non-static method Captcha::update_response_session() should not be called statically, assuming $this from incompatible context"

Safari browser does not render the captcha image if Content-Length header is sent

First, thank you kolanos for your port of the captcha module for kohana v3.

I am actually doing a cross-browser web application, and the captcha image does not render in Safari 4.5 or 5.0 on windows.
It works well and renders on the following browsers : chrome, explorer, firefox, flock, sea-monkey, opera and netscape.

I have tried to figure out from where the problem was coming and I finally found that it was the content-length header which causes the bug in safari.

in /modules/captcha/classes/controller/captcha.php (line 4)

$captcha = Captcha::instance($group)->render(FALSE);

$this->request->headers['Content-Type'] = File::mime($captcha);

$this->request->headers['Connection'] = 'close';

$this->request->headers['Content-length'] = filesize($captcha);

$this->request->response = $captcha;

Perhaps a problem with the image filesize calculation.
(Note that I have tried to use either a gif or a png background and had the same bug for both mime types.)

Commenting the line which sets the content-length header or not sending it if the user agent is safari, will both fix the bug.
Here is my Bug fix :-)

1- lazy : Comment the following line

// $this->request->headers['Content-length'] = filesize($captcha);

2- better: Replace with the following code

// If the user agent is safari, don't send the content-length header

if( ! preg_match('/safari/i', strtolower($_SERVER['HTTP_USER_AGENT']) ) ){

$this->request->headers['Content-length'] = filesize($captcha);

}

Issue related to Session, Auth, and Captcha

To explain this issue, please read this first:

http://dev.kohanaframework.org/issues/2957

As I explained in the issue above, when the Session is instantiated before Captcha, the 'captcha_response' value will not be written in the session because Session::write was called first in the queue of register_shutdown_function. Auth will more like cause this issue to arise because it uses the Session, and it's very likely that Auth is called before Captcha.

If in case my suggested fix is rejected (which is very likely), I'm wondering if you can fix this issue by not calling register_shutdown_function but instead, call the Captcha::update_response_session() somewhere else where it doesn't need to be called on shutdown.

Thanks!

I don't know if I considered all the scenarios already, but there should be a solution somewhere.

IE8 and Firefox Problem

In IE8 very often captcha doesn't display.
In Firefox very often captcha doesn't full display: http://i43.tinypic.com/foluoh.jpg (height is 50)

More: http://github.com/kolanos/kohana-captcha/issues/issue/3/#issue/3

EDIT:

I have change ..\kohana\modules\captcha\classes\controller\captcha.php

comment out this line:

//$this->request->headers['Content-length'] = filesize($captcha);

Now its working in Opera, IE8 and Firefox.

EDIT2:

I have found this:

Output is that:

public function action_index($group = 'default')
{
    // Output the Captcha challenge resource (no html)
    // Pull the config group name from the URL
    $captcha = Captcha::instance($group)->render(FALSE);
    $this->request->headers['Content-Type'] = File::mime($captcha);
    $this->request->headers['Connection'] = 'close';
    //$this->request->headers['Content-length'] = filesize($captcha); // fix
    $this->request->response = $captcha;
}

Now i look in render(FALSE) => \modules\captcha\classes\captcha\basic.php
last line => return $this->image_render($html);

Now i look in image_render() => \modules\captcha\classes\captcha.php
here => $this->request->headers['Content-Type'] = 'image/'.$this->image_type;
Why? I can comment it out. :)

Session issue - illegal chars

Hi,

I had some problems with $session_type in this module.
In Config file I was using $session_type = 'database' - be cause I'm using a database to store Sessions.

When Page wiith Captha was loaded, and I click anchor to another page without Captcha (leaving the form), I allways recived this error:
"The session id contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,'"

So, I was looking for solution, and I found it. The problem with session was with the "$session_type" parameter. I changed some code in Core file "captcha.php", and its start to run petty well.

Below it's my solusion:
Before:

Session::instance(Captcha::$config['session_type'])->set('captcha_response', sha1(strtoupper($this->response)));

After:

Session::instance()->set('captcha_response', sha1(strtoupper($this->response)));

Resume:
I changed ALL "Session::instance(Captcha::$config['session_type'])" to Session::instance() and the problem was solved;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.