Last Updated: September 09, 2019
·
3.775K
· Mortezaipo

Add Captcha in RoundCube login page

For adding Captcha in RoundCube we need to modify 2 important files:

In RoundCube version 8 (I just tested 8.4 and 8.7):

edit : roundcubedir/program/include/rcubetemplate.php and add this lines in login_form function and befor $out = $input_task->show(); line:

ob_start();
$rand = rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
$_SESSION['ca'] = $rand;
$im = @imagecreate(150,20);
$back = imagecolorallocatealpha($im,255,255,255,127);
$text = imagecolorallocate($im,233,14,91);
imagestring($im,20,40,2,$rand,$text);
imagepng($im);
$bimg = base64_encode(ob_get_contents());
imagedestroy($im);
ob_end_clean();
$attr['src'] = 'data:image/gif;base64,'.$bimg;
$img_captcha = html::img($attr);
$input_captcha   = new html_inputfield(array('name' => '_captcha', 'id' => 'rcmlogincaptcha'));
$table->add('title', html::label('captcha', Q(rcube_label('captcha_img'))));
$table->add('captcha',$img_captcha);
$table->add('title', html::label('rcmlogincaptcha', Q(rcube_label('captcha'))));
$table->add('input',$input_captcha->show());

Refresh your Login page you can see the Captcha that appeared.

Ok , for validating Captcha , we need to edit index.php file.

edit: roundcube_dir/index.php add these lines befor $RCMAIL->kill_session();:

$ca = $_SESSION['ca'];
$captcha = trim(get_input_value('_captcha', RCUBE_INPUT_POST));

And change this If:

if ($auth['valid'] && !$auth['abort'] && ...

To

if ($ca == $captcha && $auth['valid'] && !$auth['abort'] && ...

Now we need just add captcha and captcha_img to labels in localization.

Open localization file for example en_US.

edit: /roundcubedir/program/localization/enUS/labels.inc and add these lines at the end of file:

$labels['captcha_img']  = 'Captcha image';
$labels['captcha']  = 'captcha';

Now , captcha should work correctly.

Now about Captcha in RoundCube version 9.x (I just checked 9.5):

For adding Captcha in Login page:

edit: /program/include/rcmailoutputhtml.php and add these lines in login_form function and befor the $out = $input_task->show(); :

ob_start();
$rand = rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
$_SESSION['ca'] = $rand;
$im = @imagecreate(150,20);
$back = imagecolorallocatealpha($im,255,255,255,127);
$text = imagecolorallocate($im,233,14,91);
imagestring($im,20,40,2,$rand,$text);
imagepng($im);
$bimg = base64_encode(ob_get_contents());
imagedestroy($im);
ob_end_clean();
$attr['src'] = 'data:image/gif;base64,'.$bimg;
$img_captcha = html::img($attr);
$input_captcha   = new html_inputfield(array('name' => '_captcha', 'id' => 'rcmlogincaptcha'));
$table->add('title', html::label('captcha', html::quote($this->app->gettext('captcha'))));
$table->add('captcha',$img_captcha);
$table->add('title', html::label('rcmlogincaptcha', html::quote($this->app->gettext('captcha'))));
$table->add('input',$input_captcha->show());

Ok , for validating captcha , we need to edit index.php file.

edit: roundcube_dir/index.php add these lines befor $RCMAIL->kill_session();:

$ca = $_SESSION['ca'];
$captcha = trim(rcube_utils::get_input_value('_captcha', rcube_utils::INPUT_POST));

And change this If:

if ($auth['valid'] && !$auth['abort'] && ...

To

if ($ca == $captcha && $auth['valid'] && !$auth['abort'] && ...

And finally add these lines to localization file:

$labels['captcha_img']  = 'Captcha image';
$labels['captcha']  = 'captcha';

Good Luck

1 Response
Add your response

thanks :)

over 1 year ago ·