dolibarr 22.0.5
modCaptchaStandard.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
27require_once DOL_DOCUMENT_ROOT.'/core/modules/security/captcha/modules_captcha.php';
28require_once DOL_DOCUMENT_ROOT.'/core/modules/security/generate/modGeneratePassStandard.class.php';
29
30
35{
39 public $id;
40
44 public $picto = 'fa-shield-alt';
45
54 public function __construct($db, $conf, $langs, $user)
55 {
56 $this->id = strtolower(preg_replace('/^modCaptcha/i', '', get_class($this)));
57
58 $this->db = $db;
59 $this->conf = $conf;
60 $this->langs = $langs;
61 $this->user = $user;
62 }
63
69 public function getDescription()
70 {
71 global $langs;
72 return $langs->trans("DolibarrStandardCaptcha");
73 }
74
80 public function getExample()
81 {
82 global $db, $conf, $langs, $user;
83
84 $generator = new modGeneratePassStandard($db, $conf, $langs, $user);
85 $generator->length = '5';
86 $example = $generator->getExample();
87
88 if (function_exists("imagecreate") && function_exists("imagepng")) {
89 $img = imagecreate(80, 32);
90 if (!$img) {
91 return "Problem with GD creation";
92 }
93 $background_color = imagecolorallocate($img, 250, 250, 250); // do not comment this line
94 $ecriture_color = imagecolorallocate($img, 0, 0, 0);
95 imagestring($img, 4, 15, 8, $example, $ecriture_color);
96
97 ob_start();
98 imagepng($img);
99 $image_data = ob_get_contents();
100 ob_end_clean();
101
102 return '<img class="inline-block valignmiddle" src="data:image/png;base64,' . base64_encode($image_data) . '" border="0" width="80" height="32" />';
103 } else {
104 // Image grise
105 $image_data_base64 = 'iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAFElEQVR4nGNsaGhgwA2Y8MiNYGkA22EBlPG3fjQAAAAASUVORK5CYII=';
106 return '<img class="inline-block valignmiddle" src="data:image/png;base64,' . $image_data_base64 . '" border="0" width="80" height="32" />';
107 }
108 }
109
116 public function getCaptchaCodeForForm($php_self = '')
117 {
118 global $langs;
119
120 // Output the image by calling /core/antispamimage.php
121 // This antispamimage also record the value of code into $_SESSION['dol_antispam_value'] so we will be able to validate by calling
122 // validateCodeAfterLoginSubmit() later when we submit the login form.
123
124 $out = '<!-- Captcha -->
125 <div class="trinputlogin">
126 <div class="tagtd tdinputlogin nowrap none valignmiddle">
127
128 <span class="fa fa-unlock"></span>
129 <span class="nofa span-icon-security inline-block">
130 <input id="securitycode" placeholder="'.$langs->trans("SecurityCode").'" class="flat input-icon-security width125" type="text" maxlength="5" name="code" tabindex="3" autocomplete="off" />
131 </span>
132 <span class="nowrap inline-block">
133 <img class="inline-block valignmiddle" src="'.DOL_URL_ROOT.'/core/antispamimage.php" border="0" width="80" height="32" id="img_securitycode" />
134 <a class="inline-block valignmiddle" href="'.$php_self.'" tabindex="4" data-role="button" onclick="submitFormFromCaptcha(event)">'.img_picto($langs->trans("Refresh"), 'refresh', 'id="captcha_refresh_img"').'</a>
135 </span>
136
137 </div>
138 </div>
139
140 <script>
141 function submitFormFromCaptcha(event) {
142 console.log("submitFormFromCaptcha");
143
144 // Prevent the default action of the link
145 event.preventDefault();
146 // Search the form
147 const form = event.target.closest("form");
148
149 // Submit the form if found
150 if (form) {
151 console.log("we set actionlogin to value \"disabled\"");
152 document.getElementById("actionlogin").value = "disabled";
153
154 form.submit();
155 }
156 }
157 </script>
158 <!-- End code for Captcha -->'."\n";
159
160 return $out;
161 }
162
163
164
172 {
173 $sessionkey = 'dol_antispam_value'; // The same key than set into the /core/antispamimage.php file.
174
175 $ok = (array_key_exists($sessionkey, $_SESSION) && (strtolower($_SESSION[$sessionkey]) === strtolower(GETPOST('code', 'restricthtml')))) ? 1 : 0;
176
177 return $ok;
178 }
179}
Parent class for password rules/management modules.
Class to generate a password according to a dolibarr standard rule (12 random chars)
validateCodeAfterLoginSubmit()
Validate a captcha This function is called after a log to validate a captcha, before validating a pas...
getCaptchaCodeForForm($php_self='')
Return the HTML content to output on a form that need the captcha.
__construct($db, $conf, $langs, $user)
Constructor.
getDescription()
Return description of module.
getExample()
Return an example of password generated by this module.
Class to generate a password according to a dolibarr standard rule (12 random chars)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
conf($dolibarr_main_document_root)
Load conf file (file must exists)
Definition inc.php:423
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
$conf db user
Active Directory does not allow anonymous connections.
Definition repair.php:162