dolibarr 25.0.0-alpha
actions_extrafields.inc.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2011-2020 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2025-2026 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 * $elementype must be defined.
21 */
22
40'@phan-var-force int $error';
41'@phan-var-force string $action';
42'@phan-var-force ?string $pagekey';
43'@phan-var-force string $elementtype';
44'@phan-var-force string $value';
45
46// Prefer $pagekey (the whitelisted request key used by the unified admin/extrafields.php
47// controller) for self-referencing redirects; fall back to $elementtype for any other,
48// older-style caller of this shared include that only defines $elementtype.
49$pagekeyforredirect = isset($pagekey) ? $pagekey : $elementtype;
50
51$maxsizestring = 255;
52$maxsizeint = 10;
53$mesg = '';
54$mesgs = array();
55
56$extrasize = GETPOST('size', 'intcomma');
57$type = GETPOST('type', 'alphanohtml');
58$param = GETPOST('param', 'alphanohtml');
59$css = GETPOST('css', 'alphanohtml');
60$cssview = GETPOST('cssview', 'alphanohtml');
61$csslist = GETPOST('csslist', 'alphanohtml');
62$confirm = GETPOST('confirm', 'alpha');
63
64if ($type == 'double' && strpos($extrasize, ',') === false) {
65 $extrasize = '24,8';
66}
67if ($type == 'date') {
68 $extrasize = '';
69}
70if ($type == 'datetime') {
71 $extrasize = '';
72}
73if ($type == 'select') {
74 $extrasize = '';
75}
76
77// List of reserved words for databases
78$listofreservedwords = array(
79 'ADD', 'ALL', 'ALTER', 'ANALYZE', 'AND', 'AS', 'ASENSITIVE', 'BEFORE', 'BETWEEN', 'BINARY', 'BLOB', 'BOTH', 'CALL', 'CASCADE', 'CASE', 'CHANGE', 'CHAR', 'CHARACTER', 'CHECK', 'COLLATE', 'COLUMN', 'CONDITION', 'CONSTRAINT', 'CONTINUE', 'CONVERT', 'CREATE', 'CROSS', 'CURRENT_DATE', 'CURRENT_TIME', 'CURRENT_TIMESTAMP', 'CURRENT_USER',
80 'CURSOR', 'DATABASE', 'DATABASES', 'DAY_HOUR', 'DAY_MICROSECOND', 'DAY_MINUTE', 'DAY_SECOND', 'DECIMAL', 'DECLARE', 'DEFAULT', 'DELAYED', 'DELETE', 'DESC', 'DESCRIBE', 'DETERMINISTIC', 'DISTINCT', 'DISTINCTROW', 'DOUBLE', 'DROP', 'DUAL',
81 'EACH', 'ELSE', 'ELSEIF', 'ENCLOSED', 'ESCAPED', 'EXISTS', 'EXPLAIN', 'FALSE', 'FETCH', 'FLOAT', 'FLOAT4', 'FLOAT8', 'FORCE', 'FOREIGN', 'FULLTEXT', 'GRANT', 'GROUP', 'HAVING', 'HIGH_PRIORITY', 'HOUR_MICROSECOND', 'HOUR_MINUTE', 'HOUR_SECOND',
82 'IGNORE', 'IGNORE_SERVER_IDS', 'INDEX', 'INFILE', 'INNER', 'INOUT', 'INSENSITIVE', 'INSERT', 'INT', 'INTEGER', 'INTERVAL', 'INTO', 'ITERATE',
83 'KEYS', 'KEYWORD', 'LEAD', 'LEADING', 'LEAVE', 'LEFT', 'LIKE', 'LIMIT', 'LINES', 'LOCALTIME', 'LOCALTIMESTAMP', 'LONGBLOB', 'LONGTEXT', 'MASTER_SSL_VERIFY_SERVER_CERT', 'MATCH', 'MEDIUMBLOB', 'MEDIUMINT', 'MEDIUMTEXT', 'MIDDLEINT', 'MINUTE_MICROSECOND', 'MINUTE_SECOND', 'MODIFIES', 'NATURAL', 'NOT', 'NO_WRITE_TO_BINLOG', 'NUMERIC',
84 'OFFSET', 'ON', 'OPTION', 'OPTIONALLY', 'OUTER', 'OUTFILE', 'OVER',
85 'PARTITION', 'POSITION', 'PRECISION', 'PRIMARY', 'PROCEDURE', 'PURGE', 'RANGE', 'READS', 'READ_WRITE', 'REAL', 'REFERENCES', 'REGEXP', 'RELEASE', 'RENAME', 'REPEAT', 'REQUIRE', 'RESTRICT', 'RETURN', 'REVOKE', 'RIGHT', 'RLIKE',
86 'SCHEMAS', 'SECOND_MICROSECOND', 'SENSITIVE', 'SEPARATOR', 'SIGNAL', 'SMALLINT', 'SPATIAL', 'SPECIFIC', 'SQLEXCEPTION', 'SQLSTATE', 'SQLWARNING', 'SQL_BIG_RESULT', 'SQL_CALC_FOUND_ROWS', 'SQL_SMALL_RESULT', 'SSL', 'STARTING', 'STRAIGHT_JOIN',
87 'TABLE', 'TERMINATED', 'TINYBLOB', 'TINYINT', 'TINYTEXT', 'TRAILING', 'TRIGGER', 'UNDO', 'UNIQUE', 'UNSIGNED', 'UPDATE', 'USAGE', 'USING', 'UTC_DATE', 'UTC_TIME', 'UTC_TIMESTAMP', 'VALUES', 'VARBINARY', 'VARCHAR', 'VARYING',
88 'WHEN', 'WHERE', 'WHILE', 'WRITE', 'XOR', 'YEAR_MONTH', 'ZEROFILL'
89);
90
91// Add attribute
92if ($action == 'add') {
93 if (GETPOST("button") != $langs->trans("Cancel")) {
94 // Check values
95 if (!$type) {
96 $error++;
97 $langs->load("errors");
98 $mesgs[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type"));
99 $action = 'create';
100 }
101 if ($type == 'varchar' && $extrasize <= 0) {
102 $error++;
103 $langs->load("errors");
104 $mesgs[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Size"));
105 $action = 'edit';
106 }
107 if ($type == 'varchar' && $extrasize > $maxsizestring) {
108 $error++;
109 $langs->load("errors");
110 $mesgs[] = $langs->trans("ErrorSizeTooLongForVarcharType", $maxsizestring);
111 $action = 'create';
112 }
113 if ($type == 'int' && $extrasize > $maxsizeint) {
114 $error++;
115 $langs->load("errors");
116 $mesgs[] = $langs->trans("ErrorSizeTooLongForIntType", $maxsizeint);
117 $action = 'create';
118 }
119 if ($type == 'stars' && ($extrasize < 1 || $extrasize > 10)) {
120 $error++;
121 $langs->load("errors");
122 $mesgs[] = $langs->trans("ErrorSizeForStarsType");
123 $action = 'create';
124 }
125 if ($type == 'select' && !$param) {
126 $error++;
127 $langs->load("errors");
128 $mesgs[] = $langs->trans("ErrorNoValueForSelectType");
129 $action = 'create';
130 }
131 if ($type == 'sellist' && !$param) {
132 $error++;
133 $langs->load("errors");
134 $mesgs[] = $langs->trans("ErrorNoValueForSelectListType");
135 $action = 'create';
136 }
137 if ($type == 'checkbox' && !$param) {
138 $error++;
139 $langs->load("errors");
140 $mesgs[] = $langs->trans("ErrorNoValueForCheckBoxType");
141 $action = 'create';
142 }
143 if ($type == 'link' && !$param) {
144 $error++;
145 $langs->load("errors");
146 $mesgs[] = $langs->trans("ErrorNoValueForLinkType");
147 $action = 'create';
148 }
149 if ($type == 'radio' && !$param) {
150 $error++;
151 $langs->load("errors");
152 $mesgs[] = $langs->trans("ErrorNoValueForRadioType");
153 $action = 'create';
154 }
155 if ((($type == 'radio') || ($type == 'checkbox')) && $param) {
156 // Construct array for parameter (value of select list)
157 $parameters = $param;
158 $parameters_array = explode("\r\n", $parameters);
159 foreach ($parameters_array as $param_ligne) {
160 if (!empty($param_ligne)) {
161 $matches = array();
162 if (preg_match_all('/,/', $param_ligne, $matches)) {
163 if (count($matches[0]) > 1) {
164 $error++;
165 $langs->load("errors");
166 $mesgs[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
167 $action = 'create';
168 }
169 } else {
170 $error++;
171 $langs->load("errors");
172 $mesgs[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
173 $action = 'create';
174 }
175 }
176 }
177 }
178
179 if (!$error) {
180 if (strlen(GETPOST('attrname', 'aZ09')) < 3) {
181 $error++;
182 $langs->load("errors");
183 $mesgs[] = $langs->trans("ErrorValueLength", $langs->transnoentitiesnoconv("AttributeCode"), 3);
184 $action = 'create';
185 }
186 }
187
188 // Check reserved keyword with more than 3 characters
189 if (!$error) {
190 if (in_array(strtoupper(GETPOST('attrname', 'aZ09')), $listofreservedwords)) {
191 $error++;
192 $langs->load("errors");
193 $mesgs[] = $langs->trans("ErrorReservedKeyword", GETPOST('attrname', 'aZ09'));
194 $action = 'create';
195 }
196 }
197
198 if (!$error) {
199 // attrname must be alphabetical and lower case only
200 if (GETPOSTISSET("attrname") && preg_match("/^[a-z0-9_]+$/", GETPOST('attrname', 'aZ09')) && !is_numeric(GETPOST('attrname', 'aZ09'))) {
201 // Construct array for parameter (value of select list)
202 $default_value = GETPOST('default_value', 'alpha');
203 $parameters = $param;
204 $parameters_array = explode("\r\n", $parameters);
205 $params = array();
206 //In sellist we have only one line and it can have come to do SQL expression
207 if ($type == 'sellist' || $type == 'chkbxlst') {
208 foreach ($parameters_array as $param_ligne) {
209 $params['options'] = array($parameters => null);
210 }
211 } else {
212 // Else it's separated key/value and coma list
213 foreach ($parameters_array as $param_ligne) {
214 if (strpos($param_ligne, ',') !== false) {
215 list($key, $value) = explode(',', $param_ligne);
216 if (!array_key_exists('options', $params)) {
217 $params['options'] = array();
218 }
219 } else {
220 $key = $param_ligne;
221 $value = null;
222 }
223 $params['options'][$key] = $value;
224 }
225 }
226
227 // Visibility: -1=not visible by default in list, 1=visible, 0=hidden
228 $visibility = GETPOST('list', 'alpha');
229 if (in_array($type, ['separate', 'point', 'linestrg', 'polygon'])) {
230 $visibility = 3;
231 }
232
233 $result = $extrafields->addExtraField(
234 GETPOST('attrname', 'aZ09'),
235 GETPOST('label', 'alpha'),
236 $type,
237 GETPOSTINT('pos'),
238 $extrasize,
239 $elementtype,
240 (GETPOST('unique', 'alpha') ? 1 : 0),
241 (GETPOST('required', 'alpha') ? 1 : 0),
242 $default_value,
243 $params,
244 (GETPOST('alwayseditable', 'alpha') ? 1 : 0),
245 (GETPOST('perms', 'alpha') ? GETPOST('perms', 'alpha') : ''),
246 $visibility,
247 GETPOST('help', 'alpha'),
248 GETPOST('computed_value', 'alpha'),
249 (GETPOST('entitycurrentorall', 'alpha') ? 0 : ''),
250 GETPOST('langfile', 'alpha'),
251 '1',
252 (GETPOST('totalizable', 'alpha') ? 1 : 0),
253 GETPOSTINT('printable'),
254 array('css' => $css, 'cssview' => $cssview, 'csslist' => $csslist),
255 GETPOST("ai_prompt"),
256 (GETPOST('emptyonclone', 'alpha') ? 1 : 0),
257 (GETPOST('showintooltip', 'int') ? 1 : 0),
258 GETPOSTINT('personal_data')
259 );
260 if ($result > 0) {
261 setEventMessages($langs->trans('SetupSaved'), null, 'mesgs');
262 header("Location: ".$_SERVER["PHP_SELF"].(empty($pagekeyforredirect) ? '' : '?elementtype='.urlencode($pagekeyforredirect)));
263 exit;
264 } else {
265 $error++;
266 $mesg = $extrafields->error;
267 $mesgs = array_merge($mesgs, $extrafields->errors);
268 setEventMessages($mesg, $mesgs, 'errors');
269 }
270 } else {
271 $error++;
272 $langs->load("errors");
273 $mesg = $langs->trans("ErrorFieldCanNotContainSpecialNorUpperCharacters", $langs->transnoentities("AttributeCode"));
274 setEventMessages($mesg, $mesgs, 'errors');
275 $action = 'create';
276 }
277 } else {
278 setEventMessages($mesg, $mesgs, 'errors');
279 }
280 }
281}
282
283// Rename field
284if ($action == 'update') {
285 if (GETPOST("button") != $langs->trans("Cancel")) {
286 // Check values
287 if (!$type) {
288 $error++;
289 $langs->load("errors");
290 $mesgs[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type"));
291 $action = 'edit';
292 }
293 if ($type == 'varchar' && $extrasize <= 0) {
294 $error++;
295 $langs->load("errors");
296 $mesgs[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Size"));
297 $action = 'edit';
298 }
299 if ($type == 'varchar' && $extrasize > $maxsizestring) {
300 $error++;
301 $langs->load("errors");
302 $mesgs[] = $langs->trans("ErrorSizeTooLongForVarcharType", $maxsizestring);
303 $action = 'edit';
304 }
305 if ($type == 'int' && $extrasize > $maxsizeint) {
306 $error++;
307 $langs->load("errors");
308 $mesgs[] = $langs->trans("ErrorSizeTooLongForIntType", $maxsizeint);
309 $action = 'edit';
310 }
311 if ($type == 'select' && !$param) {
312 $error++;
313 $langs->load("errors");
314 $mesgs[] = $langs->trans("ErrorNoValueForSelectType");
315 $action = 'edit';
316 }
317 if ($type == 'sellist' && !$param) {
318 $error++;
319 $langs->load("errors");
320 $mesgs[] = $langs->trans("ErrorNoValueForSelectListType");
321 $action = 'edit';
322 }
323 if ($type == 'stars' && ($extrasize < 1 || $extrasize > 10)) {
324 $error++;
325 $langs->load("errors");
326 $mesgs[] = $langs->trans("ErrorSizeForStarsType");
327 $action = 'edit';
328 }
329 if ($type == 'checkbox' && !$param) {
330 $error++;
331 $langs->load("errors");
332 $mesgs[] = $langs->trans("ErrorNoValueForCheckBoxType");
333 $action = 'edit';
334 }
335 if ($type == 'radio' && !$param) {
336 $error++;
337 $langs->load("errors");
338 $mesgs[] = $langs->trans("ErrorNoValueForRadioType");
339 $action = 'edit';
340 }
341 if ((($type == 'radio') || ($type == 'checkbox')) && $param) {
342 // Construct array for parameter (value of select list)
343 $parameters = $param;
344 $parameters_array = explode("\r\n", $parameters);
345 foreach ($parameters_array as $param_ligne) {
346 if (!empty($param_ligne)) {
347 if (preg_match_all('/,/', $param_ligne, $matches)) {
348 if (count($matches[0]) > 1) {
349 $error++;
350 $langs->load("errors");
351 $mesgs[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
352 $action = 'edit';
353 }
354 } else {
355 $error++;
356 $langs->load("errors");
357 $mesgs[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
358 $action = 'edit';
359 }
360 }
361 }
362 }
363
364 if (!$error) {
365 if (strlen(GETPOST('attrname', 'aZ09')) < 3 && !getDolGlobalString('MAIN_DISABLE_EXTRAFIELDS_CHECK_FOR_UPDATE')) {
366 $error++;
367 $langs->load("errors");
368 $mesgs[] = $langs->trans("ErrorValueLength", $langs->transnoentitiesnoconv("AttributeCode"), 3);
369 $action = 'edit';
370 }
371 }
372
373 // Check reserved keyword with more than 3 characters
374 if (!$error) {
375 if (in_array(strtoupper(GETPOST('attrname', 'aZ09')), $listofreservedwords) && !getDolGlobalString('MAIN_DISABLE_EXTRAFIELDS_CHECK_FOR_UPDATE')) {
376 $error++;
377 $langs->load("errors");
378 $mesgs[] = $langs->trans("ErrorReservedKeyword", GETPOST('attrname', 'aZ09'));
379 $action = 'edit';
380 }
381 }
382
383 if (!$error) {
384 if (GETPOSTISSET("attrname") && preg_match("/^\w[a-zA-Z0-9-_]*$/", GETPOST('attrname', 'aZ09')) && !is_numeric(GETPOST('attrname', 'aZ09'))) {
385 $pos = GETPOSTINT('pos');
386 // Construct array for parameter (value of select list)
387 $parameters = $param;
388 $parameters_array = explode("\r\n", $parameters);
389 $params = array();
390 //In sellist we have only one line and it can have come to do SQL expression
391 if ($type == 'sellist' || $type == 'chkbxlst') {
392 foreach ($parameters_array as $param_ligne) {
393 $params['options'] = array($parameters => null);
394 }
395 } else {
396 //Else it's separated key/value and coma list
397 foreach ($parameters_array as $param_ligne) {
398 $tmp = explode(',', $param_ligne);
399 $key = $tmp[0];
400 if (!empty($tmp[1])) {
401 $value = $tmp[1];
402 }
403 if (!array_key_exists('options', $params)) {
404 $params['options'] = array();
405 }
406 $params['options'][$key] = $value;
407 }
408 }
409
410 // $params['options'][$key] can be 'Facture:/compta/facture/class/facture.class.php' => '/custom'
411
412 // Visibility: -1=not visible by default in list, 1=visible, 0=hidden
413 $visibility = GETPOST('list', 'alpha');
414 if (in_array($type, ['separate', 'point', 'linestrg', 'polygon'])) {
415 $visibility = 3;
416 }
417
418 // Example: is_object($object) ? ($object->id < 10 ? round($object->id / 2, 2) : (2 * $user->id) * (int) substr($mysoc->zip, 1, 2)) : 'objnotdefined'
419 $computedvalue = GETPOST('computed_value', 'nohtml');
420
421 $result = $extrafields->update(
422 GETPOST('attrname', 'aZ09'),
423 GETPOST('label', 'alpha'),
424 $type,
425 $extrasize,
426 $elementtype,
427 (GETPOST('unique', 'alpha') ? 1 : 0),
428 (GETPOST('required', 'alpha') ? 1 : 0),
429 $pos,
430 $params,
431 (GETPOST('alwayseditable', 'alpha') ? 1 : 0),
432 (GETPOST('perms', 'alpha') ? GETPOST('perms', 'alpha') : ''),
433 $visibility,
434 GETPOST('help', 'alpha'),
435 GETPOST('default_value', 'alpha'),
436 $computedvalue,
437 (GETPOST('entitycurrentorall', 'alpha') ? 0 : ''),
438 GETPOST('langfile'),
439 GETPOST('enabled', 'nohtml'),
440 (GETPOST('totalizable', 'alpha') ? 1 : 0),
441 GETPOSTINT('printable'),
442 array('css' => $css, 'cssview' => $cssview, 'csslist' => $csslist),
443 GETPOST("ai_prompt"),
444 (GETPOST('emptyonclone', 'alpha') ? 1 : 0),
445 (GETPOST('showintooltip', 'int') ? 1 : 0),
446 GETPOSTINT('personal_data')
447 );
448 if ($result > 0) {
449 setEventMessages($langs->trans('SetupSaved'), null, 'mesgs');
450 header("Location: ".$_SERVER["PHP_SELF"].(empty($pagekeyforredirect) ? '' : '?elementtype='.urlencode($pagekeyforredirect)));
451 exit;
452 } else {
453 $error++;
454 $mesg = $extrafields->error;
455 $mesgs = array_merge($mesgs, $extrafields->errors);
456 setEventMessages($mesg, $mesgs, 'errors');
457 }
458 } else {
459 $error++;
460 $langs->load("errors");
461 $mesg = $langs->trans("ErrorFieldCanNotContainSpecialCharacters", $langs->transnoentities("AttributeCode"));
462 setEventMessages($mesg, null, 'errors');
463 }
464 } else {
465 setEventMessages($mesg, $mesgs, 'errors');
466 }
467 }
468}
469
470// Delete attribute
471if ($action == 'confirm_delete' && $confirm == "yes") {
472 if (GETPOSTISSET("attrname") && preg_match("/^\w[a-zA-Z0-9-_]*$/", GETPOST("attrname", 'aZ09'))) {
473 $attributekey = GETPOST('attrname', 'aZ09');
474
475 $result = $extrafields->delete($attributekey, $elementtype);
476 if ($result >= 0) {
477 setEventMessages($langs->trans("ExtrafieldsDeleted", $attributekey), null, 'mesgs');
478
479 header("Location: ".$_SERVER["PHP_SELF"].(empty($pagekeyforredirect) ? '' : '?elementtype='.urlencode($pagekeyforredirect)));
480 exit;
481 } else {
482 $mesg = $extrafields->error;
483 $mesgs = array_merge($mesgs, $extrafields->errors);
484 }
485 } else {
486 $error++;
487 $langs->load("errors");
488 $mesg = $langs->trans("ErrorFieldCanNotContainSpecialCharacters", $langs->transnoentities("AttributeCode"));
489 }
490}
491
492// Recrypt data password
493if ($action == 'encrypt') {
494 // Load $extrafields->attributes
495 $extrafields->fetch_name_optionals_label($elementtype);
496 $attributekey = GETPOST('attrname', 'aZ09');
497
498 if (!empty($extrafields->attributes[$elementtype]['type'][$attributekey]) && $extrafields->attributes[$elementtype]['type'][$attributekey] == 'password') {
499 if (!empty($extrafields->attributes[$elementtype]['param'][$attributekey]['options'])) {
500 if (array_key_exists('dolcrypt', $extrafields->attributes[$elementtype]['param'][$attributekey]['options'])) {
501 // We can encrypt data with dolCrypt()
502 $arrayofelement = getElementProperties($elementtype);
503 if (!empty($arrayofelement['table_element'])) {
504 if ($extrafields->attributes[$elementtype]['entityid'][$attributekey] == $conf->entity || empty($extrafields->attributes[$elementtype]['entityid'][$attributekey])) {
505 dol_syslog("Loop on each extafields of table ".$arrayofelement['table_element']);
506
507 $sql = "SELECT te.rowid, te.".$db->sanitize($attributekey);
508 $sql .= " FROM ".MAIN_DB_PREFIX.$db->sanitize($arrayofelement['table_element'])." as t, ".MAIN_DB_PREFIX.$db->sanitize($arrayofelement['table_element']).'_extrafields as te';
509 $sql .= " WHERE te.fk_object = t.rowid";
510 $sql .= " AND te.".$db->sanitize($attributekey)." NOT LIKE 'dolcrypt:%'";
511 $sql .= " AND te.".$db->sanitize($attributekey)." IS NOT NULL";
512 $sql .= " AND te.".$db->sanitize($attributekey)." <> ''";
513 if ($extrafields->attributes[$elementtype]['entityid'][$attributekey] == $conf->entity) {
514 $sql .= " AND t.entity = ".getEntity($arrayofelement['element'], 0);
515 }
516
517 //print $sql;
518 $nbupdatedone = 0;
519 $resql = $db->query($sql);
520 if ($resql) {
521 $num_rows = $db->num_rows($resql);
522 $i = 0;
523 while ($i < $num_rows) {
524 $objtmp = $db->fetch_object($resql);
525 $id = $objtmp->rowid;
526 $pass = $objtmp->$attributekey;
527 if ($pass) {
528 $newpassword = dolEncrypt($pass);
529
530 $sqlupdate = "UPDATE ".MAIN_DB_PREFIX.$db->sanitize($arrayofelement['table_element']).'_extrafields';
531 $sqlupdate .= " SET ".$db->sanitize($attributekey)." = '".$db->escape($newpassword)."'";
532 $sqlupdate .= " WHERE rowid = ".((int) $id);
533
534 $resupdate = $db->query($sqlupdate);
535 if ($resupdate) {
536 $nbupdatedone++;
537 } else {
538 setEventMessages($db->lasterror(), null, 'errors');
539 $error++;
540 break;
541 }
542 }
543
544 $i++;
545 }
546 }
547
548 if ($nbupdatedone > 0) {
549 setEventMessages($langs->trans("PasswordFieldEncrypted", $nbupdatedone), null, 'mesgs');
550 } else {
551 setEventMessages($langs->trans("PasswordFieldEncrypted", $nbupdatedone), null, 'warnings');
552 }
553 }
554 }
555 }
556 }
557 }
558}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
getElementProperties($elementType)
Get an array with properties of an element.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
dolEncrypt($chain, $key='', $ciphering='', $forceseed='', $obfuscationmode='dolcrypt')
Encode a string with a symmetric encryption.