Skip to content

Commit

Permalink
WIP Captcha architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Nov 6, 2024
1 parent dc50590 commit 0e55db7
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 39 deletions.
6 changes: 1 addition & 5 deletions htdocs/admin/security_captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,7 @@
print '<span class="opacitymedium">'.$langs->trans("CaptchaDesc")."</span><br>\n";
print "<br>\n";

$dirModCaptcha = array_merge(array('/core/modules/security/captcha'), is_array($conf->modules_parts['captcha']) ? $conf->modules_parts['captcha'] : array());
foreach ($conf->modules_parts['captcha'] as $mo) {
//Add more models
$dirModCaptcha[] = $mo.'core/modules/security/captcha';
}
$dirModCaptcha = array_merge(array('/core/modules/security/captcha/'), is_array($conf->modules_parts['captcha']) ? $conf->modules_parts['captcha'] : array());

// Load array with all captcha generation modules
$arrayhandler = array();
Expand Down
37 changes: 26 additions & 11 deletions htdocs/core/class/html.formticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -793,21 +793,36 @@ function(response) {
require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
$captcha = getDolGlobalString('MAIN_SECURITY_ENABLECAPTCHA_HANDLER', 'standard');

$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;
// List of directories where we can find captcha handlers
$dirModCaptcha = array_merge(array('main' => '/core/modules/security/captcha/'), is_array($conf->modules_parts['captcha']) ? $conf->modules_parts['captcha'] : array());

$captchaobj = new $classname($this->db, $conf, $langs, $user);
$fullpathclassfile = '';
foreach ($dirModCaptcha as $dir) {
$fullpathclassfile = dol_buildpath($dir."modCaptcha".ucfirst($captcha).'.class.php', 0, 2);
if ($fullpathclassfile) {
break;
}
}

if (is_object($captchaobj) && method_exists($captchaobj, 'getCaptchaCodeForForm')) {
print $captchaobj->getCaptchaCodeForForm();
if ($fullpathclassfile) {
include_once $fullpathclassfile;
$captchaobj = null;

// Charging the numbering class
$classname = "modCaptcha".ucfirst($captcha);
if (class_exists($classname)) {
$captchaobj = new $classname($this->db, $conf, $langs, $user);

if (is_object($captchaobj) && method_exists($captchaobj, 'getCaptchaCodeForForm')) {
print $captchaobj->getCaptchaCodeForForm();

Check warning on line 817 in htdocs/core/class/html.formticket.class.php

View workflow job for this annotation

GitHub Actions / phan / Run phan

html.formticket.class.php: PhanPluginUnknownObjectMethodCall: Phan could not infer any class/interface types for the object of the method call $captchaobj-&gt;getCaptchaCodeForForm() - inferred a type of object
} else {
print 'Error, the captcha handler '.get_class($captchaobj).' does not have any method getCaptchaCodeForForm()';
}
} else {
print 'Error, the captcha handler class '.$classname.' was not found after the include';
}
} else {
print 'Error, the captcha handler '.get_class($captchaobj).' does not have any method getCaptchaCodeForForm()';
print 'Error, the captcha handler '.$captcha.' has no class file found modCaptcha'.ucfirst($captcha);
}

print '<br></td></tr>';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function getExample()
* Return the HTML content to output on a form that need the captcha
*
* @param string $php_self An URL for the a href link
* @return int 0 if KO, >0 if OK
* @return string The HTML code to output
*/
public function getCaptchaCodeForForm($php_self = '')
{
Expand Down
36 changes: 25 additions & 11 deletions htdocs/core/tpl/login.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,21 +307,35 @@
$php_self .= '?time='.dol_print_date(dol_now(), 'dayhourlog');
}

$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)) {
// List of directories where we can find captcha handlers
$dirModCaptcha = array_merge(array('main' => '/core/modules/security/captcha/'), is_array($conf->modules_parts['captcha']) ? $conf->modules_parts['captcha'] : array());
$fullpathclassfile = '';
foreach ($dirModCaptcha as $dir) {
$fullpathclassfile = dol_buildpath($dir."modCaptcha".ucfirst($captcha).'.class.php', 0, 2);
if ($fullpathclassfile) {
break;
}
}

if ($fullpathclassfile) {
include_once $fullpathclassfile;
$captchaobj = null;

// Charging the numbering class
$classname = "modCaptcha".ucfirst($captcha);
require_once $classfile;
if (class_exists($classname)) {
$captchaobj = new $classname($db, $conf, $langs, $user);

$captchaobj = new $classname($db, $conf, $langs, $user);
}

if (is_object($captchaobj) && method_exists($captchaobj, 'getCaptchaCodeForForm')) {
print $captchaobj->getCaptchaCodeForForm($php_self);
if (is_object($captchaobj) && method_exists($captchaobj, 'getCaptchaCodeForForm')) {
print $captchaobj->getCaptchaCodeForForm();

Check warning on line 330 in htdocs/core/tpl/login.tpl.php

View workflow job for this annotation

GitHub Actions / phan / Run phan

login.tpl.php: PhanPluginUnknownObjectMethodCall: Phan could not infer any class/interface types for the object of the method call $captchaobj-&gt;getCaptchaCodeForForm() - inferred a type of object
} else {
print 'Error, the captcha handler '.get_class($captchaobj).' does not have any method getCaptchaCodeForForm()';
}
} else {
print 'Error, the captcha handler class '.$classname.' was not found after the include';
}
} else {
print 'Error, the captcha handler '.get_class($captchaobj).' does not have any method getCaptchaCodeForForm()';
print 'Error, the captcha handler '.$captcha.' has no class file found modCaptcha'.ucfirst($captcha);
}
}

Expand Down
36 changes: 25 additions & 11 deletions htdocs/core/tpl/passwordforgotten.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,35 @@
$php_self .= '?time='.dol_print_date(dol_now(), 'dayhourlog');
}

$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)) {
// List of directories where we can find captcha handlers
$dirModCaptcha = array_merge(array('main' => '/core/modules/security/captcha/'), is_array($conf->modules_parts['captcha']) ? $conf->modules_parts['captcha'] : array());
$fullpathclassfile = '';
foreach ($dirModCaptcha as $dir) {
$fullpathclassfile = dol_buildpath($dir."modCaptcha".ucfirst($captcha).'.class.php', 0, 2);
if ($fullpathclassfile) {
break;
}
}

if ($fullpathclassfile) {
include_once $fullpathclassfile;
$captchaobj = null;

// Charging the numbering class
$classname = "modCaptcha".ucfirst($captcha);
require_once $classfile;
if (class_exists($classname)) {
$captchaobj = new $classname($db, $conf, $langs, $user);

$captchaobj = new $classname($db, $conf, $langs, $user);
}

if (is_object($captchaobj) && method_exists($captchaobj, 'getCaptchaCodeForForm')) {
print $captchaobj->getCaptchaCodeForForm($php_self);
if (is_object($captchaobj) && method_exists($captchaobj, 'getCaptchaCodeForForm')) {
print $captchaobj->getCaptchaCodeForForm();

Check warning on line 197 in htdocs/core/tpl/passwordforgotten.tpl.php

View workflow job for this annotation

GitHub Actions / phan / Run phan

passwordforgotten.tpl.php: PhanPluginUnknownObjectMethodCall: Phan could not infer any class/interface types for the object of the method call $captchaobj-&gt;getCaptchaCodeForForm() - inferred a type of object
} else {
print 'Error, the captcha handler '.get_class($captchaobj).' does not have any method getCaptchaCodeForForm()';
}
} else {
print 'Error, the captcha handler class '.$classname.' was not found after the include';
}
} else {
print 'Error, the captcha handler '.get_class($captchaobj).' does not have any method getCaptchaCodeForForm()';
print 'Error, the captcha handler '.$captcha.' has no class file found modCaptcha'.ucfirst($captcha);
}
}

Expand Down

0 comments on commit 0e55db7

Please sign in to comment.