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