dolibarr 25.0.0-alpha
passwordfield.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2025 Open-Dsi <support@open-dsi.fr>
3 * Copyright (C) 2026 Frédéric France <frederic.france@free.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25require_once DOL_DOCUMENT_ROOT . '/core/class/fields/commonfield.class.php';
26
27
32{
36 public $emptyValues = array('');
37
38
51 public function printInputSearchField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
52 {
53 $moreCss = $this->getInputCss($fieldInfos, $moreCss);
54 $htmlName = $keyPrefix . $key . $keySuffix;
55
56 return self::$form->inputType('text', $htmlName, (string) $value, $htmlName, $moreCss, $moreAttrib);
57 }
58
71 public function printInputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
72 {
73 $moreCss = $this->getInputCss($fieldInfos, $moreCss);
74 $moreAttrib = trim((string) $moreAttrib);
75 if (empty($moreAttrib)) $moreAttrib = ' ' . $moreAttrib;
76 $autoFocus = $fieldInfos->inputAutofocus ? ' autofocus' : '';
77 $htmlName = $keyPrefix . $key . $keySuffix;
78
79 $out = '<input style="display:none" type="text" name="fakeusernameremembered">'; // Hidden field to reduce impact of evil Google Chrome autopopulate bug.
80 if ($htmlName == 'pass_crypted') {
81 $out .= self::$form->inputType('password', 'pass', '', 'pass', $moreCss, ' autocomplete="new-password"' . $moreAttrib . $autoFocus);
82 $out .= self::$form->inputType('hidden', 'pass_crypted', (string) $value, 'pass_crypted', $moreCss, $moreAttrib);
83 } else {
84 $out .= self::$form->inputType('password', $htmlName, (string) $value, $htmlName, $moreCss, ' autocomplete="new-password"' . $moreAttrib . $autoFocus);
85 }
86
87 return $out;
88 }
89
102 public function printOutputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
103 {
104 global $langs;
105
106 return !$this->isEmptyValue($fieldInfos, $value) ? '<span class="opacitymedium">' . $langs->trans("Encrypted") . '</span>' : '';
107 }
108
118 public function getInputCss($fieldInfos, $moreCss = '', $defaultCss = '')
119 {
120 return parent::getInputCss($fieldInfos, $moreCss, $defaultCss ? $defaultCss : 'maxwidth100');
121 }
122
132 public function verifyFieldValue($fieldInfos, $key, $value)
133 {
134 global $conf, $langs, $user;
135
136 $result = parent::verifyFieldValue($fieldInfos, $key, $value);
137 if ($result && !$this->isEmptyValue($fieldInfos, $value)) {
138 // Todo do we use other method ?
139 if (getDolGlobalString('USER_PASSWORD_GENERATED')) {
140 // Add a check on rules for password syntax using the setup of the password generator
141 require_once DOL_DOCUMENT_ROOT . '/core/modules/security/generate/modules_genpassword.php';
142 $modGeneratePass = ModeleGenPassword::loadAndInstantiate(getDolGlobalString('USER_PASSWORD_GENERATED'), $this->db, $conf, $langs, $user);
143 if ($modGeneratePass) {
144 '@phan-var-force ModeleGenPassword $modGeneratePass';
145
146 // To check an input user password, we disable the cleaning on ambiguous characters (this is used only for auto-generated password)
147 $modGeneratePass->WithoutAmbi = 0;
148
149 // Call to validatePassword($password) to check pass match rules
150 $testpassword = $modGeneratePass->validatePassword($value);
151 if (!$testpassword) {
152 self::$validator->error = $langs->trans('RequireValidValue');
153 return false;
154 }
155 }
156 }
157
158 $result = true;
159 }
160
161 return $result;
162 }
163
174 public function verifyPostFieldValue($fieldInfos, $key, $keyPrefix = '', $keySuffix = '')
175 {
176 return parent::verifyPostFieldValue($fieldInfos, $key, $keyPrefix, $keySuffix);
177 }
178
190 public function getPostFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
191 {
192 $htmlName = $keyPrefix . $key . $keySuffix;
193
194 if (GETPOSTISSET($htmlName)) {
195 $value = GETPOST($htmlName, 'password');
196 } else {
197 $value = $defaultValue;
198 }
199
200 return $value;
201 }
202
203
215 public function getPostSearchFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
216 {
217 $htmlName = $keyPrefix . $key . $keySuffix;
218
219 if (GETPOSTISSET($htmlName)) {
220 $value = GETPOST($htmlName, 'alpha');
221 } else {
222 $value = $defaultValue;
223 }
224
225 return $value;
226 }
227
237 public function sqlFilterSearchField($fieldInfos, $key, $value)
238 {
239 if (!$this->isEmptyValue($fieldInfos, $value)) {
240 $alias = $fieldInfos->sqlAlias ?? 't.';
241
242 // TODO rework search on crypt password
243 return natural_search($alias . ($fieldInfos->nameInTable ?? $key), $value, 0);
244 }
245
246 return '';
247 }
248}
Class to common field.
isEmptyValue($fieldInfos, $value, $emptyValues=null)
Check if the value is deemed as empty.
static loadAndInstantiate($id, $db, $conf, $langs, $user)
Locate, load and instantiate a password generator/validator model by its id (the value stored in USER...
Class to password field.
printInputField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input field into a page.
getInputCss($fieldInfos, $moreCss='', $defaultCss='')
Get input CSS.
verifyPostFieldValue($fieldInfos, $key, $keyPrefix='', $keySuffix='')
Verify if the field value from GET/POST is valid.
verifyFieldValue($fieldInfos, $key, $value)
Verify if the field value is valid.
sqlFilterSearchField($fieldInfos, $key, $value)
Get sql filter for search field.
getPostSearchFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get search field value from GET/POST.
getPostFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get field value from GET/POST.
printOutputField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to show a field into a page.
printInputSearchField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input search field into a page.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
natural_search($fields, $value, $mode=0, $nofirstand=0, $sqltoadd='')
Generate natural SQL search string for a criteria (this criteria can be tested on one or several fiel...
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.