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