* Copyright (C) 2024 Frédéric France * Copyright (C) 2024 MDW * * 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 * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * or see https://www.gnu.org/ */ /** * \file htdocs/core/modules/security/captcha/modCaptchaStandard.class.php * \ingroup core * \brief File to manage captcha generation according to dolibarr native code */ require_once DOL_DOCUMENT_ROOT.'/core/modules/security/captcha/modules_captcha.php'; require_once DOL_DOCUMENT_ROOT.'/core/modules/security/generate/modGeneratePassStandard.class.php'; /** * Class to generate a password according to a dolibarr standard rule (12 random chars) */ class modCaptchaStandard extends ModeleCaptcha { /** * @var string ID */ public $id; /** * @var string */ public $picto = 'fa-shield-alt'; /** * Constructor * * @param DoliDB $db Database handler * @param Conf $conf Handler de conf * @param Translate $langs Handler de langue * @param User $user Handler du user connected */ public function __construct($db, $conf, $langs, $user) { $this->id = strtolower(preg_replace('/^modCaptcha/i', '', get_class($this))); $this->db = $db; $this->conf = $conf; $this->langs = $langs; $this->user = $user; } /** * Return description of module * * @return string Description of module */ public function getDescription() { global $langs; return $langs->trans("DolibarrStandardCaptcha"); } /** * Return an example of password generated by this module * * @return string Example of password */ public function getExample() { global $db, $conf, $langs, $user; $generator = new modGeneratePassStandard($db, $conf, $langs, $user); $generator->length = '5'; $example = $generator->getExample(); if (function_exists("imagecreate") && function_exists("imagepng")) { $img = imagecreate(80, 32); if (!$img) { return "Problem with GD creation"; } $background_color = imagecolorallocate($img, 250, 250, 250); // do not comment this line $ecriture_color = imagecolorallocate($img, 0, 0, 0); imagestring($img, 4, 15, 8, $example, $ecriture_color); ob_start(); imagepng($img); $image_data = ob_get_contents(); ob_end_clean(); return ''; } else { // Image grise $image_data_base64 = 'iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAFElEQVR4nGNsaGhgwA2Y8MiNYGkA22EBlPG3fjQAAAAASUVORK5CYII='; return ''; } } /** * 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 string The HTML code to output */ public function getCaptchaCodeForForm($php_self = '') { global $langs; // Output the image by calling /core/antispamimage.php // This antispamimage also record the value of code into $_SESSION['dol_antispam_value'] so we will be able to validate by calling // validateCodeAfterLoginSubmit() later when we submit the login form. $out = ' '."\n"; return $out; } /** * Validate a captcha * This function is called after a log to validate a captcha, before validating a password. * * @return int 0 if KO, >0 if OK */ public function validateCodeAfterLoginSubmit() { $sessionkey = 'dol_antispam_value'; // The same key than set into the /core/antispamimage.php file. $ok = (array_key_exists($sessionkey, $_SESSION) && (strtolower($_SESSION[$sessionkey]) === strtolower(GETPOST('code', 'restricthtml')))) ? 1 : 0; return $ok; } }