dolibarr 24.0.1
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 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
39'@phan-var-force int $error';
40'@phan-var-force string $action';
41'@phan-var-force string $elementtype';
42'@phan-var-force string $value';
43
44$maxsizestring = 255;
45$maxsizeint = 10;
46$mesg = '';
47$mesgs = array();
48
49$extrasize = GETPOST('size', 'intcomma');
50$type = GETPOST('type', 'alphanohtml');
51$param = GETPOST('param', 'alphanohtml');
52$css = GETPOST('css', 'alphanohtml');
53$cssview = GETPOST('cssview', 'alphanohtml');
54$csslist = GETPOST('csslist', 'alphanohtml');
55$confirm = GETPOST('confirm', 'alpha');
56
57if ($type == 'double' && strpos($extrasize, ',') === false) {
58 $extrasize = '24,8';
59}
60if ($type == 'date') {
61 $extrasize = '';
62}
63if ($type == 'datetime') {
64 $extrasize = '';
65}
66if ($type == 'select') {
67 $extrasize = '';
68}
69
70// List of reserved words for databases
71$listofreservedwords = array(
72 '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',
73 'CURSOR', 'DATABASE', 'DATABASES', 'DAY_HOUR', 'DAY_MICROSECOND', 'DAY_MINUTE', 'DAY_SECOND', 'DECIMAL', 'DECLARE', 'DEFAULT', 'DELAYED', 'DELETE', 'DESC', 'DESCRIBE', 'DETERMINISTIC', 'DISTINCT', 'DISTINCTROW', 'DOUBLE', 'DROP', 'DUAL',
74 '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',
75 'IGNORE', 'IGNORE_SERVER_IDS', 'INDEX', 'INFILE', 'INNER', 'INOUT', 'INSENSITIVE', 'INSERT', 'INT', 'INTEGER', 'INTERVAL', 'INTO', 'ITERATE',
76 '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',
77 'OFFSET', 'ON', 'OPTION', 'OPTIONALLY', 'OUTER', 'OUTFILE', 'OVER',
78 'PARTITION', 'POSITION', 'PRECISION', 'PRIMARY', 'PROCEDURE', 'PURGE', 'RANGE', 'READS', 'READ_WRITE', 'REAL', 'REFERENCES', 'REGEXP', 'RELEASE', 'RENAME', 'REPEAT', 'REQUIRE', 'RESTRICT', 'RETURN', 'REVOKE', 'RIGHT', 'RLIKE',
79 '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',
80 'TABLE', 'TERMINATED', 'TINYBLOB', 'TINYINT', 'TINYTEXT', 'TRAILING', 'TRIGGER', 'UNDO', 'UNIQUE', 'UNSIGNED', 'UPDATE', 'USAGE', 'USING', 'UTC_DATE', 'UTC_TIME', 'UTC_TIMESTAMP', 'VALUES', 'VARBINARY', 'VARCHAR', 'VARYING',
81 'WHEN', 'WHERE', 'WHILE', 'WRITE', 'XOR', 'YEAR_MONTH', 'ZEROFILL'
82);
83
84// Add attribute
85if ($action == 'add') {
86 if (GETPOST("button") != $langs->trans("Cancel")) {
87 // Check values
88 if (!$type) {
89 $error++;
90 $langs->load("errors");
91 $mesgs[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type"));
92 $action = 'create';
93 }
94 if ($type == 'varchar' && $extrasize <= 0) {
95 $error++;
96 $langs->load("errors");
97 $mesgs[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Size"));
98 $action = 'edit';
99 }
100 if ($type == 'varchar' && $extrasize > $maxsizestring) {
101 $error++;
102 $langs->load("errors");
103 $mesgs[] = $langs->trans("ErrorSizeTooLongForVarcharType", $maxsizestring);
104 $action = 'create';
105 }
106 if ($type == 'int' && $extrasize > $maxsizeint) {
107 $error++;
108 $langs->load("errors");
109 $mesgs[] = $langs->trans("ErrorSizeTooLongForIntType", $maxsizeint);
110 $action = 'create';
111 }
112 if ($type == 'stars' && ($extrasize < 1 || $extrasize > 10)) {
113 $error++;
114 $langs->load("errors");
115 $mesgs[] = $langs->trans("ErrorSizeForStarsType");
116 $action = 'create';
117 }
118 if ($type == 'select' && !$param) {
119 $error++;
120 $langs->load("errors");
121 $mesgs[] = $langs->trans("ErrorNoValueForSelectType");
122 $action = 'create';
123 }
124 if ($type == 'sellist' && !$param) {
125 $error++;
126 $langs->load("errors");
127 $mesgs[] = $langs->trans("ErrorNoValueForSelectListType");
128 $action = 'create';
129 }
130 if ($type == 'checkbox' && !$param) {
131 $error++;
132 $langs->load("errors");
133 $mesgs[] = $langs->trans("ErrorNoValueForCheckBoxType");
134 $action = 'create';
135 }
136 if ($type == 'link' && !$param) {
137 $error++;
138 $langs->load("errors");
139 $mesgs[] = $langs->trans("ErrorNoValueForLinkType");
140 $action = 'create';
141 }
142 if ($type == 'link' && $param) {
143 // $param is 'ObjectName:classPath'. Check the class file really exists, a wrong path
144 // makes the link render as a raw id later, with nothing shown to the user.
145 $tmplink = explode(':', $param);
146 if (empty($tmplink[1]) || !file_exists(dol_buildpath($tmplink[1]))) {
147 $error++;
148 $langs->load("errors");
149 $mesgs[] = $langs->trans("ErrorFileNotFound", empty($tmplink[1]) ? $param : $tmplink[1]);
150 $action = 'create';
151 }
152 }
153 if ($type == 'radio' && !$param) {
154 $error++;
155 $langs->load("errors");
156 $mesgs[] = $langs->trans("ErrorNoValueForRadioType");
157 $action = 'create';
158 }
159 if ((($type == 'radio') || ($type == 'checkbox')) && $param) {
160 // Construct array for parameter (value of select list)
161 $parameters = $param;
162 $parameters_array = explode("\r\n", $parameters);
163 foreach ($parameters_array as $param_ligne) {
164 if (!empty($param_ligne)) {
165 $matches = array();
166 if (preg_match_all('/,/', $param_ligne, $matches)) {
167 if (count($matches[0]) > 1) {
168 $error++;
169 $langs->load("errors");
170 $mesgs[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
171 $action = 'create';
172 }
173 } else {
174 $error++;
175 $langs->load("errors");
176 $mesgs[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
177 $action = 'create';
178 }
179 }
180 }
181 }
182
183 if (!$error) {
184 if (strlen(GETPOST('attrname', 'aZ09')) < 3) {
185 $error++;
186 $langs->load("errors");
187 $mesgs[] = $langs->trans("ErrorValueLength", $langs->transnoentitiesnoconv("AttributeCode"), 3);
188 $action = 'create';
189 }
190 }
191
192 // Check reserved keyword with more than 3 characters
193 if (!$error) {
194 if (in_array(strtoupper(GETPOST('attrname', 'aZ09')), $listofreservedwords)) {
195 $error++;
196 $langs->load("errors");
197 $mesgs[] = $langs->trans("ErrorReservedKeyword", GETPOST('attrname', 'aZ09'));
198 $action = 'create';
199 }
200 }
201
202 if (!$error) {
203 // attrname must be alphabetical and lower case only
204 if (GETPOSTISSET("attrname") && preg_match("/^[a-z0-9_]+$/", GETPOST('attrname', 'aZ09')) && !is_numeric(GETPOST('attrname', 'aZ09'))) {
205 // Construct array for parameter (value of select list)
206 $default_value = GETPOST('default_value', 'alpha');
207 $parameters = $param;
208 $parameters_array = explode("\r\n", $parameters);
209 $params = array();
210 //In sellist we have only one line and it can have come to do SQL expression
211 if ($type == 'sellist' || $type == 'chkbxlst') {
212 foreach ($parameters_array as $param_ligne) {
213 $params['options'] = array($parameters => null);
214 }
215 } else {
216 // Else it's separated key/value and coma list
217 foreach ($parameters_array as $param_ligne) {
218 if (strpos($param_ligne, ',') !== false) {
219 list($key, $value) = explode(',', $param_ligne);
220 if (!array_key_exists('options', $params)) {
221 $params['options'] = array();
222 }
223 } else {
224 $key = $param_ligne;
225 $value = null;
226 }
227 $params['options'][$key] = $value;
228 }
229 }
230
231 // Visibility: -1=not visible by default in list, 1=visible, 0=hidden
232 $visibility = GETPOST('list', 'alpha');
233 if (in_array($type, ['separate', 'point', 'linestrg', 'polygon'])) {
234 $visibility = 3;
235 }
236
237 $result = $extrafields->addExtraField(
238 GETPOST('attrname', 'aZ09'),
239 GETPOST('label', 'alpha'),
240 $type,
241 GETPOSTINT('pos'),
242 $extrasize,
243 $elementtype,
244 (GETPOST('unique', 'alpha') ? 1 : 0),
245 (GETPOST('required', 'alpha') ? 1 : 0),
246 $default_value,
247 $params,
248 (GETPOST('alwayseditable', 'alpha') ? 1 : 0),
249 (GETPOST('perms', 'alpha') ? GETPOST('perms', 'alpha') : ''),
250 $visibility,
251 GETPOST('help', 'alpha'),
252 GETPOST('computed_value', 'alpha'),
253 (GETPOST('entitycurrentorall', 'alpha') ? 0 : ''),
254 GETPOST('langfile', 'alpha'),
255 '1',
256 (GETPOST('totalizable', 'alpha') ? 1 : 0),
257 GETPOSTINT('printable'),
258 array('css' => $css, 'cssview' => $cssview, 'csslist' => $csslist),
259 GETPOST("ai_prompt"),
260 (GETPOST('emptyonclone', 'alpha') ? 1 : 0),
261 (GETPOST('showintooltip', 'int') ? 1 : 0),
262 GETPOSTINT('personal_data')
263 );
264 if ($result > 0) {
265 setEventMessages($langs->trans('SetupSaved'), null, 'mesgs');
266 header("Location: ".$_SERVER["PHP_SELF"]);
267 exit;
268 } else {
269 $error++;
270 $mesg = $extrafields->error;
271 $mesgs = array_merge($mesgs, $extrafields->errors);
272 setEventMessages($mesg, $mesgs, 'errors');
273 }
274 } else {
275 $error++;
276 $langs->load("errors");
277 $mesg = $langs->trans("ErrorFieldCanNotContainSpecialNorUpperCharacters", $langs->transnoentities("AttributeCode"));
278 setEventMessages($mesg, $mesgs, 'errors');
279 $action = 'create';
280 }
281 } else {
282 setEventMessages($mesg, $mesgs, 'errors');
283 }
284 }
285}
286
287// Rename field
288if ($action == 'update') {
289 if (GETPOST("button") != $langs->trans("Cancel")) {
290 // Check values
291 if (!$type) {
292 $error++;
293 $langs->load("errors");
294 $mesgs[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type"));
295 $action = 'edit';
296 }
297 if ($type == 'varchar' && $extrasize <= 0) {
298 $error++;
299 $langs->load("errors");
300 $mesgs[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Size"));
301 $action = 'edit';
302 }
303 if ($type == 'varchar' && $extrasize > $maxsizestring) {
304 $error++;
305 $langs->load("errors");
306 $mesgs[] = $langs->trans("ErrorSizeTooLongForVarcharType", $maxsizestring);
307 $action = 'edit';
308 }
309 if ($type == 'int' && $extrasize > $maxsizeint) {
310 $error++;
311 $langs->load("errors");
312 $mesgs[] = $langs->trans("ErrorSizeTooLongForIntType", $maxsizeint);
313 $action = 'edit';
314 }
315 if ($type == 'select' && !$param) {
316 $error++;
317 $langs->load("errors");
318 $mesgs[] = $langs->trans("ErrorNoValueForSelectType");
319 $action = 'edit';
320 }
321 if ($type == 'sellist' && !$param) {
322 $error++;
323 $langs->load("errors");
324 $mesgs[] = $langs->trans("ErrorNoValueForSelectListType");
325 $action = 'edit';
326 }
327 if ($type == 'stars' && ($extrasize < 1 || $extrasize > 10)) {
328 $error++;
329 $langs->load("errors");
330 $mesgs[] = $langs->trans("ErrorSizeForStarsType");
331 $action = 'edit';
332 }
333 if ($type == 'checkbox' && !$param) {
334 $error++;
335 $langs->load("errors");
336 $mesgs[] = $langs->trans("ErrorNoValueForCheckBoxType");
337 $action = 'edit';
338 }
339 if ($type == 'radio' && !$param) {
340 $error++;
341 $langs->load("errors");
342 $mesgs[] = $langs->trans("ErrorNoValueForRadioType");
343 $action = 'edit';
344 }
345 if ($type == 'link' && $param) {
346 // $param is 'ObjectName:classPath'. Check the class file really exists, a wrong path
347 // makes the link render as a raw id later, with nothing shown to the user.
348 $tmplink = explode(':', $param);
349 if (empty($tmplink[1]) || !file_exists(dol_buildpath($tmplink[1]))) {
350 $error++;
351 $langs->load("errors");
352 $mesgs[] = $langs->trans("ErrorFileNotFound", empty($tmplink[1]) ? $param : $tmplink[1]);
353 $action = 'edit';
354 }
355 }
356 if ((($type == 'radio') || ($type == 'checkbox')) && $param) {
357 // Construct array for parameter (value of select list)
358 $parameters = $param;
359 $parameters_array = explode("\r\n", $parameters);
360 foreach ($parameters_array as $param_ligne) {
361 if (!empty($param_ligne)) {
362 if (preg_match_all('/,/', $param_ligne, $matches)) {
363 if (count($matches[0]) > 1) {
364 $error++;
365 $langs->load("errors");
366 $mesgs[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
367 $action = 'edit';
368 }
369 } else {
370 $error++;
371 $langs->load("errors");
372 $mesgs[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
373 $action = 'edit';
374 }
375 }
376 }
377 }
378
379 if (!$error) {
380 if (strlen(GETPOST('attrname', 'aZ09')) < 3 && !getDolGlobalString('MAIN_DISABLE_EXTRAFIELDS_CHECK_FOR_UPDATE')) {
381 $error++;
382 $langs->load("errors");
383 $mesgs[] = $langs->trans("ErrorValueLength", $langs->transnoentitiesnoconv("AttributeCode"), 3);
384 $action = 'edit';
385 }
386 }
387
388 // Check reserved keyword with more than 3 characters
389 if (!$error) {
390 if (in_array(strtoupper(GETPOST('attrname', 'aZ09')), $listofreservedwords) && !getDolGlobalString('MAIN_DISABLE_EXTRAFIELDS_CHECK_FOR_UPDATE')) {
391 $error++;
392 $langs->load("errors");
393 $mesgs[] = $langs->trans("ErrorReservedKeyword", GETPOST('attrname', 'aZ09'));
394 $action = 'edit';
395 }
396 }
397
398 if (!$error) {
399 if (GETPOSTISSET("attrname") && preg_match("/^\w[a-zA-Z0-9-_]*$/", GETPOST('attrname', 'aZ09')) && !is_numeric(GETPOST('attrname', 'aZ09'))) {
400 $pos = GETPOSTINT('pos');
401 // Construct array for parameter (value of select list)
402 $parameters = $param;
403 $parameters_array = explode("\r\n", $parameters);
404 $params = array();
405 //In sellist we have only one line and it can have come to do SQL expression
406 if ($type == 'sellist' || $type == 'chkbxlst') {
407 foreach ($parameters_array as $param_ligne) {
408 $params['options'] = array($parameters => null);
409 }
410 } else {
411 //Else it's separated key/value and coma list
412 foreach ($parameters_array as $param_ligne) {
413 $tmp = explode(',', $param_ligne);
414 $key = $tmp[0];
415 if (!empty($tmp[1])) {
416 $value = $tmp[1];
417 }
418 if (!array_key_exists('options', $params)) {
419 $params['options'] = array();
420 }
421 $params['options'][$key] = $value;
422 }
423 }
424
425 // $params['options'][$key] can be 'Facture:/compta/facture/class/facture.class.php' => '/custom'
426
427 // Visibility: -1=not visible by default in list, 1=visible, 0=hidden
428 $visibility = GETPOST('list', 'alpha');
429 if (in_array($type, ['separate', 'point', 'linestrg', 'polygon'])) {
430 $visibility = 3;
431 }
432
433 // Example: is_object($object) ? ($object->id < 10 ? round($object->id / 2, 2) : (2 * $user->id) * (int) substr($mysoc->zip, 1, 2)) : 'objnotdefined'
434 $computedvalue = GETPOST('computed_value', 'nohtml');
435
436 $result = $extrafields->update(
437 GETPOST('attrname', 'aZ09'),
438 GETPOST('label', 'alpha'),
439 $type,
440 $extrasize,
441 $elementtype,
442 (GETPOST('unique', 'alpha') ? 1 : 0),
443 (GETPOST('required', 'alpha') ? 1 : 0),
444 $pos,
445 $params,
446 (GETPOST('alwayseditable', 'alpha') ? 1 : 0),
447 (GETPOST('perms', 'alpha') ? GETPOST('perms', 'alpha') : ''),
448 $visibility,
449 GETPOST('help', 'alpha'),
450 GETPOST('default_value', 'alpha'),
451 $computedvalue,
452 (GETPOST('entitycurrentorall', 'alpha') ? 0 : ''),
453 GETPOST('langfile'),
454 GETPOST('enabled', 'nohtml'),
455 (GETPOST('totalizable', 'alpha') ? 1 : 0),
456 GETPOSTINT('printable'),
457 array('css' => $css, 'cssview' => $cssview, 'csslist' => $csslist),
458 GETPOST("ai_prompt"),
459 (GETPOST('emptyonclone', 'alpha') ? 1 : 0),
460 (GETPOST('showintooltip', 'int') ? 1 : 0),
461 GETPOSTINT('personal_data')
462 );
463 if ($result > 0) {
464 setEventMessages($langs->trans('SetupSaved'), null, 'mesgs');
465 header("Location: ".$_SERVER["PHP_SELF"]);
466 exit;
467 } else {
468 $error++;
469 $mesg = $extrafields->error;
470 $mesgs = array_merge($mesgs, $extrafields->errors);
471 setEventMessages($mesg, $mesgs, 'errors');
472 }
473 } else {
474 $error++;
475 $langs->load("errors");
476 $mesg = $langs->trans("ErrorFieldCanNotContainSpecialCharacters", $langs->transnoentities("AttributeCode"));
477 setEventMessages($mesg, null, 'errors');
478 }
479 } else {
480 setEventMessages($mesg, $mesgs, 'errors');
481 }
482 }
483}
484
485// Delete attribute
486if ($action == 'confirm_delete' && $confirm == "yes") {
487 if (GETPOSTISSET("attrname") && preg_match("/^\w[a-zA-Z0-9-_]*$/", GETPOST("attrname", 'aZ09'))) {
488 $attributekey = GETPOST('attrname', 'aZ09');
489
490 $result = $extrafields->delete($attributekey, $elementtype);
491 if ($result >= 0) {
492 setEventMessages($langs->trans("ExtrafieldsDeleted", $attributekey), null, 'mesgs');
493
494 header("Location: ".$_SERVER["PHP_SELF"]);
495 exit;
496 } else {
497 $mesg = $extrafields->error;
498 $mesgs = array_merge($mesgs, $extrafields->errors);
499 }
500 } else {
501 $error++;
502 $langs->load("errors");
503 $mesg = $langs->trans("ErrorFieldCanNotContainSpecialCharacters", $langs->transnoentities("AttributeCode"));
504 }
505}
506
507// Recrypt data password
508if ($action == 'encrypt') {
509 // Load $extrafields->attributes
510 $extrafields->fetch_name_optionals_label($elementtype);
511 $attributekey = GETPOST('attrname', 'aZ09');
512
513 if (!empty($extrafields->attributes[$elementtype]['type'][$attributekey]) && $extrafields->attributes[$elementtype]['type'][$attributekey] == 'password') {
514 if (!empty($extrafields->attributes[$elementtype]['param'][$attributekey]['options'])) {
515 if (array_key_exists('dolcrypt', $extrafields->attributes[$elementtype]['param'][$attributekey]['options'])) {
516 // We can encrypt data with dolCrypt()
517 $arrayofelement = getElementProperties($elementtype);
518 if (!empty($arrayofelement['table_element'])) {
519 if ($extrafields->attributes[$elementtype]['entityid'][$attributekey] == $conf->entity || empty($extrafields->attributes[$elementtype]['entityid'][$attributekey])) {
520 dol_syslog("Loop on each extafields of table ".$arrayofelement['table_element']);
521
522 $sql = "SELECT te.rowid, te.".$db->sanitize($attributekey);
523 $sql .= " FROM ".MAIN_DB_PREFIX.$db->sanitize($arrayofelement['table_element'])." as t, ".MAIN_DB_PREFIX.$db->sanitize($arrayofelement['table_element']).'_extrafields as te';
524 $sql .= " WHERE te.fk_object = t.rowid";
525 $sql .= " AND te.".$db->sanitize($attributekey)." NOT LIKE 'dolcrypt:%'";
526 $sql .= " AND te.".$db->sanitize($attributekey)." IS NOT NULL";
527 $sql .= " AND te.".$db->sanitize($attributekey)." <> ''";
528 if ($extrafields->attributes[$elementtype]['entityid'][$attributekey] == $conf->entity) {
529 $sql .= " AND t.entity = ".getEntity($arrayofelement['element'], 0);
530 }
531
532 //print $sql;
533 $nbupdatedone = 0;
534 $resql = $db->query($sql);
535 if ($resql) {
536 $num_rows = $db->num_rows($resql);
537 $i = 0;
538 while ($i < $num_rows) {
539 $objtmp = $db->fetch_object($resql);
540 $id = $objtmp->rowid;
541 $pass = $objtmp->$attributekey;
542 if ($pass) {
543 $newpassword = dolEncrypt($pass);
544
545 $sqlupdate = "UPDATE ".MAIN_DB_PREFIX.$db->sanitize($arrayofelement['table_element']).'_extrafields';
546 $sqlupdate .= " SET ".$db->sanitize($attributekey)." = '".$db->escape($newpassword)."'";
547 $sqlupdate .= " WHERE rowid = ".((int) $id);
548
549 $resupdate = $db->query($sqlupdate);
550 if ($resupdate) {
551 $nbupdatedone++;
552 } else {
553 setEventMessages($db->lasterror(), null, 'errors');
554 $error++;
555 break;
556 }
557 }
558
559 $i++;
560 }
561 }
562
563 if ($nbupdatedone > 0) {
564 setEventMessages($langs->trans("PasswordFieldEncrypted", $nbupdatedone), null, 'mesgs');
565 } else {
566 setEventMessages($langs->trans("PasswordFieldEncrypted", $nbupdatedone), null, 'warnings');
567 }
568 }
569 }
570 }
571 }
572 }
573}
$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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
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.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
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.
dolEncrypt($chain, $key='', $ciphering='', $forceseed='', $obfuscationmode='dolcrypt')
Encode a string with a symmetric encryption.