Work on generic captcha architecture

This commit is contained in:
Laurent Destailleur 2024-10-28 03:06:35 +01:00
parent 791651467c
commit 09668af4f4
5 changed files with 112 additions and 35 deletions

View file

@ -105,7 +105,7 @@ print "<br>\n";
// Load array with all captcha generation modules
$dir = "../core/modules/security/captcha";
$dir = DOL_DOCUMENT_ROOT."/core/modules/security/captcha";
clearstatcache();
$handle = opendir($dir);
$i = 1;
@ -142,23 +142,41 @@ print '<br>';
print $langs->trans("UseCaptchaCode");
if (function_exists("imagecreatefrompng")) {
if (!empty($conf->use_javascript_ajax)) {
print ajax_constantonoff('MAIN_SECURITY_ENABLECAPTCHA', array(), null, 0, 0, 1);
} else {
if (!getDolGlobalString('MAIN_SECURITY_ENABLECAPTCHA')) {
print '<a href="'.$_SERVER['PHP_SELF'].'?action=set_MAIN_SECURITY_ENABLECAPTCHA&token='.newToken().'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
} else {
print '<a href="'.$_SERVER['PHP_SELF'].'?action=del_MAIN_SECURITY_ENABLECAPTCHA&token='.newToken().'">'.img_picto($langs->trans("Enabled"), 'on').'</a>';
}
}
if (!empty($conf->use_javascript_ajax)) {
print ajax_constantonoff('MAIN_SECURITY_ENABLECAPTCHA', array(), null, 0, 0, 1);
} else {
$desc = $form->textwithpicto('', $langs->transnoentities("EnableGDLibraryDesc"), 1, 'warning');
print $desc;
if (!getDolGlobalString('MAIN_SECURITY_ENABLECAPTCHA')) {
print '<a href="'.$_SERVER['PHP_SELF'].'?action=set_MAIN_SECURITY_ENABLECAPTCHA&token='.newToken().'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
} else {
print '<a href="'.$_SERVER['PHP_SELF'].'?action=del_MAIN_SECURITY_ENABLECAPTCHA&token='.newToken().'">'.img_picto($langs->trans("Enabled"), 'on').'</a>';
}
}
if (isModEnabled('ticket')) {
print '<br>';
print $langs->trans("TicketUseCaptchaCodeHelp");
if (!empty($conf->use_javascript_ajax)) {
print ajax_constantonoff('MAIN_SECURITY_ENABLECAPTCHA_TICKET', array(), null, 0, 0, 1);
} else {
if (!getDolGlobalString('MAIN_SECURITY_ENABLECAPTCHA_TICKET')) {
print '<a href="'.$_SERVER['PHP_SELF'].'?action=set_MAIN_SECURITY_ENABLECAPTCHA_TICKET&token='.newToken().'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
} else {
print '<a href="'.$_SERVER['PHP_SELF'].'?action=del_MAIN_SECURITY_ENABLECAPTCHA_TICKET&token='.newToken().'">'.img_picto($langs->trans("Enabled"), 'on').'</a>';
}
}
}
$showavailablecaptcha = 0;
if (getDolGlobalString('MAIN_SECURITY_ENABLECAPTCHA')) {
$showavailablecaptcha = 1;
}
if (isModEnabled('ticket') && getDolGlobalString('MAIN_SECURITY_ENABLECAPTCHA_TICKET')) {
$showavailablecaptcha = 1;
}
$selectedcaptcha = getDolGlobalString('MAIN_SECURITY_ENABLECAPTCHA_HANDLER', 'standard');
if ($showavailablecaptcha) {
print '<br>';
print '<br>';
print '<br>';
@ -171,13 +189,6 @@ if (getDolGlobalString('MAIN_SECURITY_ENABLECAPTCHA')) {
print '<td class="right" width="100">'.$langs->trans("Status").'</td>';
print '</tr>';
$arrayofcaptcha = array(
'standard' => array('label' => 'Standard', 'picto' => 'ee')
);
//$arrayofcaptcha['google'] = array('label' => 'Google');
$selectedcaptcha = 'standard';
// Loop on each available captcha
foreach ($arrayhandler as $key => $module) {
print '<tr class="oddeven">';
@ -192,9 +203,22 @@ if (getDolGlobalString('MAIN_SECURITY_ENABLECAPTCHA')) {
print $module->getExample().'<br>';
print '</td>';
print '<td class="right" width="100">';
if ($key == $selectedcaptcha) {
print 'On';
if (function_exists("imagecreatefrompng")) {
if ($key != $selectedcaptcha) {
print '<a href="'.$_SERVER['PHP_SELF'].'?action=enabledcaptchahandler&token='.newToken().'&handler=standard"><input type="checkbox"></a>';
} else {
print '<a href="'.$_SERVER['PHP_SELF'].'?action=disablecaptchahandler&token='.newToken().'&handler=standard"><input type="checkbox" checked="checked"';
if (count($arrayhandler) <= 1) {
print 'disabled="disabled"';
}
print '></a>';
}
} else {
$desc = $form->textwithpicto('', $langs->transnoentities("EnableGDLibraryDesc"), 1, 'warning');
print $desc;
}
print '</td>';
print '</tr>';
}

View file

@ -273,9 +273,11 @@ if (!function_exists('dol_loginfunction')) {
// Security graphical code
$captcha = 0;
$captcha_refresh = '';
if (function_exists("imagecreatefrompng") && getDolGlobalString('MAIN_SECURITY_ENABLECAPTCHA')) {
$captcha = 1;
$captcha_refresh = img_picto($langs->trans("Refresh"), 'refresh', 'id="captcha_refresh_img"');
if (getDolGlobalString('MAIN_SECURITY_ENABLECAPTCHA')) {
$captcha = getDolGlobalString('MAIN_SECURITY_ENABLECAPTCHA_HANDLER', 'standard');
if ($captcha == 'standard') {
$captcha_refresh = img_picto($langs->trans("Refresh"), 'refresh', 'id="captcha_refresh_img"');
}
}
// Extra link

View file

@ -41,6 +41,7 @@ if ($size > 10000) {
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
'
@phan-var-force string $captcha
@phan-var-force string $captcha_refresh
@phan-var-force int<0,1> $dol_hide_leftmenu
@phan-var-force int<0,1> $dol_hide_topmenu
@ -278,7 +279,22 @@ if (!empty($captcha)) {
} else {
$php_self .= '?time='.dol_print_date(dol_now(), 'dayhourlog');
}
// TODO: provide accessible captcha variants?>
$classfile = DOL_DOCUMENT_ROOT."/core/modules/security/captcha/modCaptcha".ucfirst($captcha).'.class.php';
include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$captchaobj = null;
if (dol_is_file($classfile)) {
// Charging the numbering class
$classname = "modCaptcha".ucfirst($captcha);
require_once $classfile;
$captchaobj = new $classname($db, $conf, $langs, $user);
}
if (is_object($captchaobj) && method_exists($captchaobj, 'getCaptchaCodeForForm')) {
// TODO: get this code using a method of captcha
} else {
?>
<!-- Captcha -->
<div class="trinputlogin">
<div class="tagtd none valignmiddle tdinputlogin nowrap">
@ -293,7 +309,8 @@ if (!empty($captcha)) {
</span>
</div></div>
<?php
<?php
}
}
if (!empty($morelogincontent)) {

View file

@ -1,6 +1,6 @@
<?php
/* Copyright (C) 2009-2010 Regis Houssin <regis.houssin@inodbox.com>
* Copyright (C) 2011-2013 Laurent Destailleur <eldy@users.sourceforge.net>
/* Copyright (C) 2009-2010 Regis Houssin <regis.houssin@inodbox.com>
* Copyright (C) 2011-2024 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
*
* This program is free software; you can redistribute it and/or modify
@ -17,6 +17,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Page to ask email for password forgotten
if (!defined('NOBROWSERNOTIF')) {
define('NOBROWSERNOTIF', 1);
}
@ -164,7 +166,22 @@ if (!empty($captcha)) {
} else {
$php_self .= '?time='.dol_print_date(dol_now(), 'dayhourlog');
}
// TODO: provide accessible captcha variants?>
$classfile = DOL_DOCUMENT_ROOT."/core/modules/security/captcha/modCaptcha".ucfirst($captcha).'.class.php';
include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$captchaobj = null;
if (dol_is_file($classfile)) {
// Charging the numbering class
$classname = "modCaptcha".ucfirst($captcha);
require_once $classfile;
$captchaobj = new $classname($db, $conf, $langs, $user);
}
if (is_object($captchaobj) && method_exists($captchaobj, 'getCaptchaCodeForForm')) {
// TODO: get this code using a method of captcha
} else {
?>
<!-- Captcha -->
<div class="trinputlogin">
<div class="tagtd tdinputlogin nowrap none valignmiddle">
@ -179,7 +196,8 @@ if (!empty($captcha)) {
</span>
</div></div>
<?php
<?php
}
}
if (!empty($morelogincontent)) {

View file

@ -1,6 +1,6 @@
<?php
/* Copyright (C) 2022 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
/* Copyright (C) 2022 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -205,7 +205,22 @@ if (!empty($captcha)) {
} else {
$php_self .= '?time='.dol_print_date(dol_now(), 'dayhourlog');
}
// TODO: provide accessible captcha variants?>
$classfile = DOL_DOCUMENT_ROOT."/core/modules/security/captcha/modCaptcha".ucfirst($captcha).'.class.php';
include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$captchaobj = null;
if (dol_is_file($classfile)) {
// Charging the numbering class
$classname = "modCaptcha".ucfirst($captcha);
require_once $classfile;
$captchaobj = new $classname($db, $conf, $langs, $user);
}
if (is_object($captchaobj) && method_exists($captchaobj, 'getCaptchaCodeForForm')) {
// TODO: get this code using a method of captcha
} else {
?>
<!-- Captcha -->
<div class="trinputlogin">
<div class="tagtd tdinputlogin nowrap none valignmiddle">
@ -220,7 +235,8 @@ if (!empty($captcha)) {
</span>
</div></div>
<?php
<?php
}
}
if (!empty($morelogincontent)) {