dolibarr 23.0.3
security.lib.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (C) 2008-2021 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2008-2021 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2020 Ferran Marcet <fmarcet@2byte.es>
6 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
7 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 * or see https://www.gnu.org/
22 */
23
41function dol_encode($chain, $key = '1')
42{
43 if (is_numeric($key) && $key == '1') { // rule 1 is offset of 17 for char
44 $output_tab = array();
45 $strlength = dol_strlen($chain);
46 for ($i = 0; $i < $strlength; $i++) {
47 $output_tab[$i] = chr(ord(substr($chain, $i, 1)) + 17);
48 }
49 $chain = implode("", $output_tab);
50 } elseif ($key) {
51 $result = '';
52 $strlength = dol_strlen($chain);
53 for ($i = 0; $i < $strlength; $i++) {
54 $keychar = substr($key, ($i % strlen($key)) - 1, 1);
55 $result .= chr(ord(substr($chain, $i, 1)) + (ord($keychar) - 65));
56 }
57 $chain = $result;
58 }
59
60 return base64_encode($chain);
61}
62
72function dol_decode($chain, $key = '1')
73{
74 $chain = base64_decode($chain);
75
76 if (is_numeric($key) && $key == '1') { // rule 1 is offset of 17 for char
77 $output_tab = array();
78 $strlength = dol_strlen($chain);
79 for ($i = 0; $i < $strlength; $i++) {
80 $output_tab[$i] = chr(ord(substr($chain, $i, 1)) - 17);
81 }
82
83 $chain = implode("", $output_tab);
84 } elseif ($key) {
85 $result = '';
86 $strlength = dol_strlen($chain);
87 for ($i = 0; $i < $strlength; $i++) {
88 $keychar = substr($key, ($i % strlen($key)) - 1, 1);
89 $result .= chr(ord(substr($chain, $i, 1)) - (ord($keychar) - 65));
90 }
91 $chain = $result;
92 }
93
94 return $chain;
95}
96
103function dolGetRandomBytes($length)
104{
105 if (function_exists('random_bytes')) { // Available with PHP 7 only.
106 return bin2hex(random_bytes((int) floor($length / 2))); // the bin2hex will double the number of bytes so we take length / 2
107 }
108
109 return bin2hex(openssl_random_pseudo_bytes((int) floor($length / 2))); // the bin2hex will double the number of bytes so we take length / 2. May be very slow on Windows.
110}
111
112
113define('MAIN_SECURITY_REVERSIBLE_ALGO', 'AES-256-CTR');
114
128function dolEncrypt($chain, $key = '', $ciphering = '', $forceseed = '')
129{
130 global $conf;
131 global $dolibarr_disable_dolcrypt_for_debug;
132
133 if ($chain === '' || is_null($chain)) {
134 return '';
135 }
136
137 $reg = array();
138 if (preg_match('/^dolcrypt:([^:]+):(.+)$/', $chain, $reg)) {
139 // The $chain is already a encrypted string
140 return $chain;
141 }
142
143 if (empty($key)) {
144 if (!empty($conf->file->dolcrypt_key)) {
145 // If dolcrypt_key is defined, we used it in priority. Note: this param was never been set for the moment.
146 $key = $conf->file->dolcrypt_key;
147 } else {
148 // We fall back on the instance_unique_id (coming from $dolibarr_main_instance_unique_id, for backward compatibility).
149 $key = $conf->file->instance_unique_id;
150 }
151 }
152 if (empty($ciphering)) {
153 $ciphering = constant('MAIN_SECURITY_REVERSIBLE_ALGO');
154 }
155
156 $newchain = $chain;
157
158 if (function_exists('openssl_encrypt') && empty($dolibarr_disable_dolcrypt_for_debug)) {
159 if (empty($key)) {
160 return $chain;
161 }
162
163 $ivlen = 16;
164 if (function_exists('openssl_cipher_iv_length')) {
165 $ivlen = openssl_cipher_iv_length($ciphering);
166 }
167 if ($ivlen === false || $ivlen < 1 || $ivlen > 32) {
168 $ivlen = 16;
169 }
170 if (empty($forceseed)) {
171 $ivseed = dolGetRandomBytes($ivlen);
172 } else {
173 $ivseed = dol_substr(md5($forceseed), 0, $ivlen, 'ascii', 1);
174 }
175
176 $newchain = openssl_encrypt($chain, $ciphering, $key, 0, $ivseed);
177 return 'dolcrypt:'.$ciphering.':'.$ivseed.':'.$newchain;
178 } else {
179 return $chain;
180 }
181}
182
193function dolDecrypt($chain, $key = '')
194{
195 global $conf;
196
197 if ($chain === '' || is_null($chain)) {
198 return '';
199 }
200
201 if (empty($key)) {
202 if (!empty($conf->file->dolcrypt_key)) {
203 // If dolcrypt_key is defined, we used it in priority. Note: this param was never been set for the moment.
204 $key = $conf->file->dolcrypt_key;
205 } else {
206 // We fall back on the instance_unique_id (coming from $dolibarr_main_instance_unique_id, for backward compatibility).
207 $key = !empty($conf->file->instance_unique_id) ? $conf->file->instance_unique_id : "";
208 }
209 }
210
211 $reg = array();
212
213 // Old method (no more used, kept for compatibility)
214 if (preg_match('/^crypted:(.+)$/', $chain, $reg)) {
215 return dol_decode($reg[1]);
216 }
217
218 // New method
219 if (preg_match('/^dolcrypt:([^:]+):(.+)$/', $chain, $reg)) {
220 // Do not enable this log, except during debug
221 //dol_syslog("We try to decrypt the chain: ".$chain, LOG_DEBUG);
222
223 $ciphering = $reg[1];
224 if (function_exists('openssl_decrypt')) {
225 if (empty($key)) {
226 dol_syslog("Error dolDecrypt decrypt key is empty", LOG_WARNING);
227 return $chain;
228 }
229 $tmpexplode = explode(':', $reg[2]);
230 if (!empty($tmpexplode[1]) && is_string($tmpexplode[0])) {
231 $newchain = openssl_decrypt($tmpexplode[1], $ciphering, $key, 0, $tmpexplode[0]);
232 } else {
233 $newchain = openssl_decrypt((string) $tmpexplode[0], $ciphering, $key, 0, '');
234 }
235 // Test validity of decryption
236 if (!ascii_check($newchain)) {
237 dol_syslog("Error dolDecrypt failed: The key dolibarr_main_dolcrypt or dolibarr_main_instance_unique_id, found in conf.php file, is the the one used to encrypt this encrypted string", LOG_ERR);
238 return $chain;
239 }
240 } else {
241 dol_syslog("Error dolDecrypt openssl_decrypt is not available", LOG_ERR);
242 return $chain;
243 }
244 return $newchain;
245 } else {
246 return $chain;
247 }
248}
249
270function dol_hash($chain, $type = '0', $nosalt = 0, $mode = 0)
271{
272 // No need to add salt for password_hash
273 if (($type == '0' || $type == 'auto') && getDolGlobalString('MAIN_SECURITY_HASH_ALGO') == 'password_hash' && function_exists('password_hash')) {
274 if (strpos($chain, "\0") !== false) {
275 // String contains a null character that can't be encoded. Return an error instead of fatal error.
276 if ($mode == 1) {
277 return array('pass_encrypted' => 'Invalid string to encrypt. Contains a null character', 'pass_encoding' => '');
278 } else {
279 return 'Invalid string to encrypt. Contains a null character.';
280 }
281 }
282
283 if ($mode == 1) {
284 return array('pass_encrypted' => password_hash($chain, PASSWORD_DEFAULT), 'pass_encoding' => 'password_hash');
285 } else {
286 return password_hash($chain, PASSWORD_DEFAULT);
287 }
288 }
289
290 // Salt value
291 if (getDolGlobalString('MAIN_SECURITY_SALT') && $type != '4' && $type !== 'openldap' && empty($nosalt)) {
292 $chain = getDolGlobalString('MAIN_SECURITY_SALT') . $chain;
293 }
294
295 if ($type == '1' || $type == 'sha1') {
296 if ($mode == 1) {
297 return array('pass_encrypted' => sha1($chain), 'pass_encoding' => 'sha1');
298 } else {
299 return sha1($chain);
300 }
301 } elseif ($type == '2' || $type == 'sha1md5') {
302 if ($mode == 1) {
303 return array('pass_encrypted' => sha1(md5($chain)), 'pass_encoding' => 'sha1md5');
304 } else {
305 return sha1(md5($chain));
306 }
307 } elseif ($type == '3' || $type == 'md5') { // For hashing with no need of security
308 if ($mode == 1) {
309 return array('pass_encrypted' => md5($chain), 'pass_encoding' => 'md5');
310 } else {
311 return md5($chain);
312 }
313 } elseif ($type == '4' || $type == 'openldap') {
314 if ($mode == 1) {
315 return array('pass_encrypted' => dolGetLdapPasswordHash($chain, getDolGlobalString('LDAP_PASSWORD_HASH_TYPE', 'md5')), 'pass_encoding' => 'ldappasswordhash'.getDolGlobalString('LDAP_PASSWORD_HASH_TYPE', 'md5'));
316 } else {
317 return dolGetLdapPasswordHash($chain, getDolGlobalString('LDAP_PASSWORD_HASH_TYPE', 'md5'));
318 }
319 } elseif ($type == '5' || $type == 'sha256') {
320 if ($mode == 1) {
321 return array('pass_encrypted' => hash('sha256', $chain), 'pass_encoding' => 'sha256');
322 } else {
323 return hash('sha256', $chain);
324 }
325 } elseif ($type == '6' || $type == 'password_hash') {
326 if ($mode == 1) {
327 return array('pass_encrypted' => password_hash($chain, PASSWORD_DEFAULT), 'pass_encoding' => 'password_hash');
328 } else {
329 return password_hash($chain, PASSWORD_DEFAULT);
330 }
331 } elseif (getDolGlobalString('MAIN_SECURITY_HASH_ALGO') == 'sha1') {
332 if ($mode == 1) {
333 return array('pass_encrypted' => sha1($chain), 'pass_encoding' => 'sha1');
334 } else {
335 return sha1($chain);
336 }
337 } elseif (getDolGlobalString('MAIN_SECURITY_HASH_ALGO') == 'sha1md5') {
338 if ($mode == 1) {
339 return array('pass_encrypted' => sha1(md5($chain)), 'pass_encoding' => 'sha1md5');
340 } else {
341 return sha1(md5($chain));
342 }
343 }
344
345 // No particular encoding defined, use default
346 if ($mode == 1) {
347 return array('pass_encrypted' => md5($chain), 'pass_encoding' => 'md5');
348 } else {
349 return md5($chain);
350 }
351}
352
365function dol_verifyHash($chain, $hash, $type = '0')
366{
367 if ($type == '0' && getDolGlobalString('MAIN_SECURITY_HASH_ALGO') == 'password_hash' && function_exists('password_verify')) {
368 // Try to autodetect which algo we used
369 if (! empty($hash[0]) && $hash[0] == '$') {
370 return password_verify($chain, $hash);
371 } elseif (dol_strlen($hash) == 32) {
372 return dol_verifyHash($chain, $hash, '3'); // md5
373 } elseif (dol_strlen($hash) == 40) {
374 return dol_verifyHash($chain, $hash, '2'); // sha1md5
375 }
376
377 return false;
378 }
379
380 return dol_hash($chain, $type) == $hash;
381}
382
390function dolGetLdapPasswordHash($password, $type = 'md5')
391{
392 if (empty($type)) {
393 $type = 'md5';
394 }
395
396 $salt = substr(sha1((string) time()), 0, 8);
397
398 if ($type === 'md5') {
399 return '{MD5}' . base64_encode(hash("md5", $password, true)); //For OpenLdap with md5 (based on an unencrypted password in base)
400 } elseif ($type === 'md5frommd5') {
401 return '{MD5}' . base64_encode(hex2bin($password)); // Create OpenLDAP MD5 password from Dolibarr MD5 password
402 } elseif ($type === 'smd5') {
403 return "{SMD5}" . base64_encode(hash("md5", $password . $salt, true) . $salt);
404 } elseif ($type === 'sha') {
405 return '{SHA}' . base64_encode(hash("sha1", $password, true));
406 } elseif ($type === 'ssha') {
407 return "{SSHA}" . base64_encode(hash("sha1", $password . $salt, true) . $salt);
408 } elseif ($type === 'sha256') {
409 return "{SHA256}" . base64_encode(hash("sha256", $password, true));
410 } elseif ($type === 'ssha256') {
411 return "{SSHA256}" . base64_encode(hash("sha256", $password . $salt, true) . $salt);
412 } elseif ($type === 'sha384') {
413 return "{SHA384}" . base64_encode(hash("sha384", $password, true));
414 } elseif ($type === 'ssha384') {
415 return "{SSHA384}" . base64_encode(hash("sha384", $password . $salt, true) . $salt);
416 } elseif ($type === 'sha512') {
417 return "{SHA512}" . base64_encode(hash("sha512", $password, true));
418 } elseif ($type === 'ssha512') {
419 return "{SSHA512}" . base64_encode(hash("sha512", $password . $salt, true) . $salt);
420 } elseif ($type === 'crypt') {
421 return '{CRYPT}' . crypt($password, $salt);
422 } elseif ($type === 'clear') {
423 return '{CLEAR}' . $password; // Just for test, plain text password is not secured !
424 }
425 return "";
426}
427
448function restrictedArea(User $user, $features, $object = 0, $tableandshare = '', $feature2 = '', $dbt_keyfield = 'fk_soc', $dbt_select = 'rowid', $isdraft = 0, $mode = 0)
449{
450 global $hookmanager;
451
452 // Define $objectid
453 if (is_object($object)) {
454 $objectid = $object->id;
455 } else {
456 $objectid = $object; // $objectid can be X or 'X,Y,Z'
457 }
458 if ($objectid == "-1") {
459 $objectid = 0;
460 }
461 if ($objectid) {
462 $objectid = preg_replace('/[^0-9\.\,]/', '', (string) $objectid); // For the case value is coming from a non sanitized user input
463 }
464
465 //dol_syslog("functions.lib:restrictedArea $feature, $objectid, $dbtablename, $feature2, $dbt_socfield, $dbt_select, $isdraft");
466 /*print "user_id=".$user->id.", features=".$features.", feature2=".$feature2.", objectid=".$objectid;
467 print ", dbtablename=".$tableandshare.", dbt_socfield=".$dbt_keyfield.", dbt_select=".$dbt_select;
468 print ", perm: user->hasRight(".$features.($feature2 ? ",".$feature2 : "").", lire) = ".($feature2 ? $user->hasRight($features, $feature2, 'lire') : $user->hasRight($features, 'lire'))."<br>";
469 */
470
471 $parentfortableentity = '';
472
473 // Fix syntax of $features param to support non standard module names.
474 // @todo : use elseif ?
475 $originalfeatures = $features;
476 if ($features == 'agenda') {
477 $tableandshare = 'actioncomm&societe';
478 $feature2 = 'myactions|allactions';
479 $dbt_select = 'id';
480 }
481 if ($features == 'bank') {
482 $features = 'banque';
483 }
484 if ($features == 'facturerec') {
485 $features = 'facture';
486 }
487 if ($features == 'supplier_invoicerec') {
488 $features = 'fournisseur';
489 $feature2 = 'facture';
490 }
491 if ($features == 'mo') {
492 $features = 'mrp';
493 }
494 if ($features == 'member') {
495 $features = 'adherent';
496 }
497 if ($features == 'subscription') {
498 $features = 'adherent';
499 $feature2 = 'cotisation';
500 }
501 if ($features == 'website' && is_object($object) && $object->element == 'websitepage') {
502 $parentfortableentity = 'fk_website@website';
503 }
504 if ($features == 'project') {
505 $features = 'projet';
506 }
507 if ($features == 'product') {
508 $features = 'produit';
509 }
510 if ($features == 'productbatch') {
511 $features = 'produit';
512 }
513 if ($features == 'tax') {
514 $feature2 = 'charges';
515 }
516 if ($features == 'workstation') {
517 $feature2 = 'workstation';
518 }
519 if ($features == 'fournisseur') { // When vendor invoice and purchase order are into module 'fournisseur'
520 $features = 'fournisseur';
521 if (is_object($object) && $object->element == 'invoice_supplier') {
522 $feature2 = 'facture';
523 } elseif (is_object($object) && $object->element == 'order_supplier') {
524 $feature2 = 'commande';
525 }
526 }
527 if ($features == 'payment_sc') {
528 $tableandshare = 'paiementcharge';
529 $parentfortableentity = 'fk_charge@chargesociales';
530 }
531
532 // if commonObjectLine : Using many2one related commonObject
533 // @see commonObjectLine::parentElement
534 if (in_array($features, ['commandedet', 'propaldet', 'facturedet', 'supplier_proposaldet', 'evaluationdet', 'skilldet', 'deliverydet', 'contratdet'])) {
535 $features = substr($features, 0, -3);
536 } elseif (in_array($features, ['stocktransferline', 'inventoryline', 'bomline', 'expensereport_det', 'facture_fourn_det'])) {
537 $features = substr($features, 0, -4);
538 } elseif ($features == 'commandefournisseurdispatch') {
539 $features = 'commandefournisseur';
540 } elseif ($features == 'invoice_supplier_det_rec') {
541 $features = 'invoice_supplier_rec';
542 }
543 if ($features == 'evaluation') {
544 $features = 'hrm';
545 $feature2 = 'evaluation';
546 }
547
548 // @todo check : project_task
549 // @todo possible ?
550 // elseif (substr($features, -3, 3) == 'det') {
551 // $features = substr($features, 0, -3);
552 // } elseif (substr($features, -4, 4) == '_det' || substr($features, -4, 4) == 'line') {
553 // $features = substr($features, 0, -4);
554 // }
555
556 // print $features.' - '.$tableandshare.' - '.$feature2.' - '.$dbt_select."\n";
557
558 // Get more permissions checks from hooks
559 $parameters = array(
560 'features' => $features,
561 'feature2' => $feature2,
562 'originalfeatures' => $originalfeatures,
563 'tableandshare' => $tableandshare,
564 'object' => $object,
565 'objectid' => $objectid,
566 'dbt_keyfield' => $dbt_keyfield,
567 'dbt_select' => $dbt_select,
568 'idtype' => $dbt_select,
569 'isdraft' => $isdraft,
570 'mode' => $mode,
571 );
572 if (!empty($hookmanager)) {
573 $reshook = $hookmanager->executeHooks('restrictedArea', $parameters);
574
575 if (isset($hookmanager->resArray['result'])) {
576 if ($hookmanager->resArray['result'] == 0) {
577 if ($mode) {
578 return 0;
579 } else {
580 accessforbidden(); // Module returns 0, so access forbidden
581 }
582 }
583 }
584 if ($reshook > 0) { // No other test done.
585 return 1;
586 }
587 }
588
589 // Features/modules to check (to support the & and | operator)
590 $featuresarray = array($features);
591 if (preg_match('/&/', $features)) {
592 $featuresarray = explode("&", $features);
593 } elseif (preg_match('/\|/', $features)) {
594 $featuresarray = explode("|", $features);
595 }
596
597 // More subfeatures to check
598 if (!empty($feature2)) {
599 $feature2 = explode("|", $feature2);
600 }
601
602 $listofmodules = explode(',', getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL'));
603
604 // Check read permission from module
605 $readok = 1;
606 $nbko = 0;
607 foreach ($featuresarray as $feature) { // first we check nb of test ko
608 $featureforlistofmodule = $feature;
609 if ($featureforlistofmodule == 'produit') {
610 $featureforlistofmodule = 'product';
611 }
612 if ($featureforlistofmodule == 'supplier_proposal') {
613 $featureforlistofmodule = 'supplierproposal';
614 }
615 if (!empty($user->socid) && getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL') && !in_array($featureforlistofmodule, $listofmodules)) { // If limits on modules for external users, module must be into list of modules for external users
616 $readok = 0;
617 $nbko++;
618 continue;
619 }
620
621 if ($feature == 'societe' && (empty($feature2) || !in_array('contact', $feature2))) {
622 if (!$user->hasRight('societe', 'lire') && !$user->hasRight('fournisseur', 'lire')) {
623 $readok = 0;
624 $nbko++;
625 }
626 } elseif (($feature == 'societe' && (!empty($feature2) && in_array('contact', $feature2))) || $feature == 'contact') {
627 if (!$user->hasRight('societe', 'contact', 'lire')) {
628 $readok = 0;
629 $nbko++;
630 }
631 } elseif ($feature == 'produit|service') {
632 if (!$user->hasRight('produit', 'lire') && !$user->hasRight('service', 'lire')) {
633 $readok = 0;
634 $nbko++;
635 }
636 } elseif ($feature == 'prelevement') {
637 if (!$user->hasRight('prelevement', 'bons', 'lire')) {
638 $readok = 0;
639 $nbko++;
640 }
641 } elseif ($feature == 'cheque') {
642 if (!$user->hasRight('banque', 'cheque')) {
643 $readok = 0;
644 $nbko++;
645 }
646 } elseif ($feature == 'projet') {
647 if (!$user->hasRight('projet', 'lire') && !$user->hasRight('projet', 'all', 'lire')) {
648 $readok = 0;
649 $nbko++;
650 }
651 } elseif ($feature == 'payment') {
652 if (!$user->hasRight('facture', 'lire')) {
653 $readok = 0;
654 $nbko++;
655 }
656 } elseif ($feature == 'payment_supplier') {
657 if (!$user->hasRight('fournisseur', 'facture', 'lire')) {
658 $readok = 0;
659 $nbko++;
660 }
661 } elseif ($feature == 'payment_sc') {
662 if (!$user->hasRight('tax', 'charges', 'lire')) {
663 $readok = 0;
664 $nbko++;
665 }
666 } elseif ($feature == 'webhook') {
667 if (empty($user->admin)) {
668 $readok = 0;
669 $nbko++;
670 }
671 } elseif (!empty($feature2)) { // This is for permissions on 2 levels (module->object->read)
672 $tmpreadok = 1;
673 foreach ($feature2 as $subfeature) {
674 if ($subfeature == 'user' && $user->id == $objectid) {
675 continue; // A user can always read its own card
676 }
677 if ($subfeature == 'fiscalyear' && $user->hasRight('accounting', 'fiscalyear', 'write')) {
678 // only one right for fiscalyear
679 $tmpreadok = 1;
680 continue;
681 }
682 if (!empty($subfeature) && !$user->hasRight($feature, $subfeature, 'lire') && !$user->hasRight($feature, $subfeature, 'read')) {
683 $tmpreadok = 0;
684 } elseif (empty($subfeature) && !$user->hasRight($feature, 'lire') && !$user->hasRight($feature, 'read')) {
685 $tmpreadok = 0;
686 } else {
687 $tmpreadok = 1;
688 break;
689 } // Break is to bypass second test if the first is ok
690 }
691 if (!$tmpreadok) { // We found a test on feature that is ko
692 $readok = 0; // All tests are ko (we manage here the and, the or will be managed later using $nbko).
693 $nbko++;
694 }
695 } elseif (!empty($feature) && ($feature != 'user' && $feature != 'usergroup')) { // This is permissions on 1 level (module->read)
696 if (!$user->hasRight($feature, 'lire')
697 && !$user->hasRight($feature, 'read')
698 && !$user->hasRight($feature, 'run')) {
699 $readok = 0;
700 $nbko++;
701 }
702 }
703 }
704
705 // If a or and at least one ok
706 if (preg_match('/\|/', $features) && $nbko < count($featuresarray)) {
707 $readok = 1;
708 }
709
710 if (!$readok) {
711 if ($mode) {
712 return 0;
713 } else {
715 }
716 }
717 //print "Read access is ok";
718
719 // Check write permission from module (we need to know write permission to create but also to delete drafts record or to upload files)
720 $createok = 1;
721 $nbko = 0;
722 $wemustcheckpermissionforcreate = (GETPOST('sendit', 'alpha') || GETPOST('linkit', 'alpha') || in_array(GETPOST('action', 'aZ09'), array('create', 'update', 'set', 'upload', 'add_element_resource', 'confirm_deletebank', 'confirm_delete_linked_resource')) || GETPOST('roworder', 'alpha', 2));
723 $wemustcheckpermissionfordeletedraft = ((GETPOST("action", "aZ09") == 'confirm_delete' && GETPOST("confirm", "aZ09") == 'yes') || GETPOST("action", "aZ09") == 'delete');
724
725 if ($wemustcheckpermissionforcreate || $wemustcheckpermissionfordeletedraft) {
726 foreach ($featuresarray as $feature) {
727 if ($feature == 'contact') {
728 if (!$user->hasRight('societe', 'contact', 'creer')) {
729 $createok = 0;
730 $nbko++;
731 }
732 } elseif ($feature == 'produit|service') {
733 if (!$user->hasRight('produit', 'creer') && !$user->hasRight('service', 'creer')) {
734 $createok = 0;
735 $nbko++;
736 }
737 } elseif ($feature == 'prelevement') {
738 if (!$user->hasRight('prelevement', 'bons', 'creer')) {
739 $createok = 0;
740 $nbko++;
741 }
742 } elseif ($feature == 'commande_fournisseur') {
743 if (!$user->hasRight('fournisseur', 'commande', 'creer') || !$user->hasRight('supplier_order', 'creer')) {
744 $createok = 0;
745 $nbko++;
746 }
747 } elseif ($feature == 'banque') {
748 if (!$user->hasRight('banque', 'modifier')) {
749 $createok = 0;
750 $nbko++;
751 }
752 } elseif ($feature == 'cheque') {
753 if (!$user->hasRight('banque', 'cheque')) {
754 $createok = 0;
755 $nbko++;
756 }
757 } elseif ($feature == 'import') {
758 if (!$user->hasRight('import', 'run')) {
759 $createok = 0;
760 $nbko++;
761 }
762 } elseif ($feature == 'ecm') {
763 if (!$user->hasRight('ecm', 'upload')) {
764 $createok = 0;
765 $nbko++;
766 }
767 } elseif ($feature == 'modulebuilder') {
768 if (!$user->hasRight('modulebuilder', 'run')) {
769 $createok = 0;
770 $nbko++;
771 }
772 } elseif ($feature == 'webhook') {
773 if (empty($user->admin)) {
774 $createok = 0;
775 $nbko++;
776 }
777 } elseif (!empty($feature2)) { // This is for permissions on 2 levels (module->object->write)
778 foreach ($feature2 as $subfeature) {
779 if ($subfeature == 'user' && $user->id == $objectid && $user->hasRight('user', 'self', 'creer')) {
780 continue; // User can edit its own card
781 }
782 if ($subfeature == 'user' && $user->id == $objectid && $user->hasRight('user', 'self', 'password')) {
783 continue; // User can edit its own password
784 }
785 if ($subfeature == 'user' && $user->id != $objectid && $user->hasRight('user', 'user', 'password')) {
786 continue; // User can edit another user's password
787 }
788
789 if (!$user->hasRight($feature, $subfeature, 'creer')
790 && !$user->hasRight($feature, $subfeature, 'write')
791 && !$user->hasRight($feature, $subfeature, 'create')) {
792 $createok = 0;
793 $nbko++;
794 } else {
795 $createok = 1;
796 // Break to bypass second test if the first is ok
797 break;
798 }
799 }
800 } elseif (!empty($feature)) { // This is for permissions on 1 levels (module->write)
801 //print '<br>feature='.$feature.' creer='.$user->rights->$feature->creer.' write='.$user->rights->$feature->write; exit;
802 if (!$user->hasRight($feature, 'creer')
803 && !$user->hasRight($feature, 'write')
804 && !$user->hasRight($feature, 'create')) {
805 $createok = 0;
806 $nbko++;
807 }
808 }
809 }
810
811 // If a or and at least one ok
812 if (preg_match('/\|/', $features) && $nbko < count($featuresarray)) {
813 $createok = 1;
814 }
815
816 if ($wemustcheckpermissionforcreate && !$createok) {
817 if ($mode) {
818 return 0;
819 } else {
821 }
822 }
823 //print "Write access is ok";
824 }
825
826 // Check create user permission
827 $createuserok = 1;
828 if (GETPOST('action', 'aZ09') == 'confirm_create_user' && GETPOST("confirm", 'aZ09') == 'yes') {
829 if (!$user->hasRight('user', 'user', 'creer')) {
830 $createuserok = 0;
831 }
832
833 if (!$createuserok) {
834 if ($mode) {
835 return 0;
836 } else {
838 }
839 }
840 //print "Create user access is ok";
841 }
842
843 // Check delete permission from module
844 $deleteok = 1;
845 $nbko = 0;
846 if ((GETPOST("action", "aZ09") == 'confirm_delete' && GETPOST("confirm", "aZ09") == 'yes') || GETPOST("action", "aZ09") == 'delete') {
847 foreach ($featuresarray as $feature) {
848 if ($feature == 'bookmark') {
849 if (!$user->hasRight('bookmark', 'supprimer')) {
850 if ($user->id != $object->fk_user || !$user->hasRight('bookmark', 'creer')) {
851 $deleteok = 0;
852 }
853 }
854 } elseif ($feature == 'contact') {
855 if (!$user->hasRight('societe', 'contact', 'supprimer')) {
856 $deleteok = 0;
857 }
858 } elseif ($feature == 'produit|service') {
859 if (!$user->hasRight('produit', 'supprimer') && !$user->hasRight('service', 'supprimer')) {
860 $deleteok = 0;
861 }
862 } elseif ($feature == 'commande_fournisseur') {
863 if (!$user->hasRight('fournisseur', 'commande', 'supprimer')) {
864 $deleteok = 0;
865 }
866 } elseif ($feature == 'payment_supplier') { // Permission to delete a payment of an invoice is permission to edit an invoice.
867 if (!$user->hasRight('fournisseur', 'facture', 'creer')) {
868 $deleteok = 0;
869 }
870 } elseif ($feature == 'payment') {
871 if (!$user->hasRight('facture', 'paiement')) {
872 $deleteok = 0;
873 }
874 } elseif ($feature == 'payment_sc') {
875 if (!$user->hasRight('tax', 'charges', 'creer')) {
876 $deleteok = 0;
877 }
878 } elseif ($feature == 'banque') {
879 if (!$user->hasRight('banque', 'modifier')) {
880 $deleteok = 0;
881 }
882 } elseif ($feature == 'cheque') {
883 if (!$user->hasRight('banque', 'cheque')) {
884 $deleteok = 0;
885 }
886 } elseif ($feature == 'ecm') {
887 if (!$user->hasRight('ecm', 'upload')) {
888 $deleteok = 0;
889 }
890 } elseif ($feature == 'ftp') {
891 if (!$user->hasRight('ftp', 'write')) {
892 $deleteok = 0;
893 }
894 } elseif ($feature == 'salaries') {
895 if (!$user->hasRight('salaries', 'delete')) {
896 $deleteok = 0;
897 }
898 } elseif ($feature == 'adherent') {
899 if (!$user->hasRight('adherent', 'supprimer')) {
900 $deleteok = 0;
901 }
902 } elseif ($feature == 'paymentbybanktransfer') {
903 if (!$user->hasRight('paymentbybanktransfer', 'create')) { // There is no delete permission
904 $deleteok = 0;
905 }
906 } elseif ($feature == 'prelevement') {
907 if (!$user->hasRight('prelevement', 'bons', 'creer')) { // There is no delete permission
908 $deleteok = 0;
909 }
910 } elseif (!empty($feature2)) { // This is for permissions on 2 levels
911 foreach ($feature2 as $subfeature) {
912 if (!$user->hasRight($feature, $subfeature, 'supprimer') && !$user->hasRight($feature, $subfeature, 'delete')) {
913 $deleteok = 0;
914 } else {
915 $deleteok = 1;
916 break;
917 } // For bypass the second test if the first is ok
918 }
919 } elseif (!empty($feature)) { // This is used for permissions on 1 level
920 //print '<br>feature='.$feature.' creer='.$user->rights->$feature->supprimer.' write='.$user->rights->$feature->delete;
921 if (!$user->hasRight($feature, 'supprimer')
922 && !$user->hasRight($feature, 'delete')
923 && !$user->hasRight($feature, 'run')) {
924 $deleteok = 0;
925 }
926 }
927 }
928
929 // If a or and at least one ok
930 if (preg_match('/\|/', $features) && $nbko < count($featuresarray)) {
931 $deleteok = 1;
932 }
933
934 if (!$deleteok && !($isdraft && $createok)) {
935 if ($mode) {
936 return 0;
937 } else {
939 }
940 }
941 //print "Delete access is ok";
942 }
943
944 // If we have a particular object to check permissions on, we check if $user has permission
945 // for this given object (link to company, is contact for project, ...)
946 if (!empty($objectid) && $objectid > 0) {
947 $ok = checkUserAccessToObject($user, $featuresarray, $object, $tableandshare, $feature2, $dbt_keyfield, $dbt_select, $parentfortableentity);
948 $params = array('objectid' => $objectid, 'features' => implode(',', $featuresarray), 'features2' => $feature2);
949 //print 'checkUserAccessToObject ok='.$ok;
950 if ($mode) {
951 return $ok ? 1 : 0;
952 } else {
953 if ($ok) {
954 return 1;
955 } else {
956 accessforbidden('', 1, 1, 0, $params);
957 }
958 }
959 }
960
961 return 1;
962}
963
979function checkUserAccessToObject($user, array $featuresarray, $object = 0, $tableandshare = '', $feature2 = '', $dbt_keyfield = '', $dbt_select = 'rowid', $parenttableforentity = '')
980{
981 global $db, $conf;
982
983 if (is_object($object)) {
984 $objectid = $object->id;
985 } else {
986 $objectid = $object; // $objectid can be X or 'X,Y,Z'
987 }
988 $objectid = preg_replace('/[^0-9\.\,]/', '', $objectid); // For the case value is coming from a non sanitized user input
989
990 //dol_syslog("functions.lib:restrictedArea $feature, $objectid, $dbtablename, $feature2, $dbt_socfield, $dbt_select, $isdraft");
991 //print "user_id=".$user->id.", features=".join(',', $featuresarray).", objectid=".$objectid;
992 //print ", tableandshare=".$tableandshare.", dbt_socfield=".$dbt_keyfield.", dbt_select=".$dbt_select."<br>";
993
994 // More parameters
995 $params = explode('&', $tableandshare);
996 $dbtablename = (!empty($params[0]) ? $params[0] : '');
997 $sharedelement = (!empty($params[1]) ? $params[1] : $dbtablename);
998
999 foreach ($featuresarray as $feature) {
1000 $sql = '';
1001
1002 //var_dump($feature);exit;
1003
1004 // For backward compatibility
1005 if ($feature == 'societe' && !empty($feature2) && is_array($feature2) && in_array('contact', $feature2)) {
1006 $feature = 'contact';
1007 $feature2 = '';
1008 }
1009 if ($feature == 'member') {
1010 $feature = 'adherent';
1011 }
1012 if ($feature == 'category') {
1013 $feature = 'categorie';
1014 }
1015 if ($feature == 'project') {
1016 $feature = 'projet';
1017 }
1018 if ($feature == 'projet' && !empty($feature2) && is_array($feature2) && !empty(array_intersect(array('project_task', 'projet_task'), $feature2))) {
1019 $feature = 'project_task';
1020 }
1021 if ($feature == 'task' || $feature == 'projet_task') {
1022 $feature = 'project_task';
1023 $dbtablename = 'projet_task';
1024 }
1025 if ($feature == 'eventorganization') {
1026 $feature = 'agenda';
1027 $dbtablename = 'actioncomm';
1028 }
1029 if ($feature == 'payment_sc' && empty($parenttableforentity)) {
1030 // If we check perm on payment page but $parenttableforentity not defined, we force value on parent table
1031 $parenttableforentity = '';
1032 $dbtablename = "chargesociales";
1033 $feature = "chargesociales";
1034 $objectid = $object->fk_charge;
1035 }
1036
1037 $checkonentitydone = 0;
1038
1039 // Array to define rules of checks to do
1040 $check = array('adherent', 'banque', 'bom', 'don', 'mrp', 'user', 'usergroup', 'payment', 'payment_supplier', 'payment_sc', 'product', 'produit', 'service', 'produit|service', 'categorie', 'resource', 'expensereport', 'holiday', 'salaries', 'website', 'recruitment', 'chargesociales', 'knowledgemanagement', 'stock'); // Test on entity only (Objects with no link to company)
1041 $checksoc = array('societe'); // Test for object Societe
1042 $checkparentsoc = array('agenda', 'contact', 'contrat'); // Test on entity + link to third party on field $dbt_keyfield. Allowed if link is empty (Ex: contacts...).
1043 $checkproject = array('projet', 'project'); // Test for project object
1044 $checktask = array('projet_task', 'project_task'); // Test for task object
1045 $checkhierarchy = array('expensereport', 'holiday', 'hrm'); // check permission among the hierarchy of user
1046 $checkuser = array('bookmark'); // check permission among the fk_user (must be myself or null)
1047 $nocheck = array('barcode', 'webhook'); // No test
1048
1049 //$checkdefault = 'all other not already defined'; // Test on entity + link to third party on field $dbt_keyfield. Not allowed if link is empty (Ex: invoice, orders...).
1050
1051 // If dbtablename not defined, we use same name for table than module name
1052 if (empty($dbtablename)) {
1053 $dbtablename = $feature;
1054 $sharedelement = (!empty($params[1]) ? $params[1] : $dbtablename); // We change dbtablename, so we set sharedelement too.
1055 }
1056
1057 // To avoid an access forbidden with a numeric ref
1058 if ($dbt_select != 'rowid' && $dbt_select != 'id') {
1059 $objectid = "'".$objectid."'"; // Note: $objectid was already cast into int at begin of this method.
1060 }
1061 // Check permission for objectid on entity only
1062 if (in_array($feature, $check) && $objectid > 0) { // For $objectid = 0, no check
1063 $sql = "SELECT COUNT(dbt.".$dbt_select.") as nb";
1064 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1065 if (($feature == 'user' || $feature == 'usergroup') && isModEnabled('multicompany')) { // Special for multicompany
1066 if (getDolGlobalString('MULTICOMPANY_TRANSVERSE_MODE')) {
1067 if ($conf->entity == 1 && $user->admin && !$user->entity) {
1068 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1069 $sql .= " AND dbt.entity IS NOT NULL";
1070 } else {
1071 $sql .= ",".MAIN_DB_PREFIX."usergroup_user as ug";
1072 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1073 $sql .= " AND ((ug.fk_user = dbt.rowid";
1074 $sql .= " AND ug.entity IN (".getEntity('usergroup')."))";
1075 $sql .= " OR dbt.entity = 0)"; // Show always superadmin
1076 }
1077 } else {
1078 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1079 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
1080 }
1081 } else {
1082 $reg = array();
1083 if ($parenttableforentity && preg_match('/(.*)@(.*)/', $parenttableforentity, $reg)) {
1084 $sql .= ", ".MAIN_DB_PREFIX.$reg[2]." as dbtp";
1085 $sql .= " WHERE dbt.".$reg[1]." = dbtp.rowid AND dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1086 $sql .= " AND dbtp.entity IN (".getEntity($sharedelement, 1).")";
1087 } else {
1088 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1089 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
1090 }
1091 }
1092 $checkonentitydone = 1;
1093 }
1094 if (in_array($feature, $checksoc) && $objectid > 0) { // We check feature = checksoc. For $objectid = 0, no check
1095 // If external user: Check permission for external users
1096 if ($user->socid > 0) {
1097 if ($user->socid != $objectid) {
1098 return false;
1099 }
1100 } elseif (isModEnabled('societe') && !$user->hasRight('societe', 'lire') && !$user->hasRight('societe', 'client', 'voir')) {
1101 dol_syslog("security.lib.php::checkUserAccessToObject Deny access due: (isModEnabled('societe') && !user->hasRight('societe', 'lire') && !user->hasRight('societe', 'client', 'voir'))", LOG_DEBUG);
1102 return false;
1103 } elseif (isModEnabled("societe") && ($user->hasRight('societe', 'lire') && !$user->hasRight('societe', 'client', 'voir'))) {
1104 // If internal user: Check permission for internal users that are restricted on their objects
1105 $sql = "SELECT COUNT(sc.fk_soc) as nb";
1106 $sql .= " FROM (".MAIN_DB_PREFIX."societe_commerciaux as sc";
1107 $sql .= ", ".MAIN_DB_PREFIX."societe as s)";
1108 $sql .= " WHERE sc.fk_soc IN (".$db->sanitize($objectid, 1).")";
1109 $sql .= " AND (sc.fk_user = ".((int) $user->id);
1110 if (getDolGlobalInt('MAIN_SEE_SUBORDINATES')) {
1111 $userschilds = $user->getAllChildIds();
1112 if (!empty($userschilds)) $sql .= " OR sc.fk_user IN (".$db->sanitize(implode(',', $userschilds)).")";
1113 }
1114 $sql .= ")";
1115 $sql .= " AND sc.fk_soc = s.rowid";
1116 $sql .= " AND s.entity IN (".getEntity($sharedelement, 1).")";
1117 } elseif (isModEnabled('multicompany')) {
1118 // If multicompany and internal users with all permissions, check user is in correct entity
1119 $sql = "SELECT COUNT(s.rowid) as nb";
1120 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
1121 $sql .= " WHERE s.rowid IN (".$db->sanitize($objectid, 1).")";
1122 $sql .= " AND s.entity IN (".getEntity($sharedelement, 1).")";
1123 }
1124
1125 $checkonentitydone = 1;
1126 }
1127 if (in_array($feature, $checkparentsoc) && $objectid > 0) { // Test on entity + link to thirdparty. Allowed if link is empty (Ex: contacts...).
1128 // If external user: Check permission for external users
1129 if ($user->socid > 0) {
1130 $sql = "SELECT COUNT(dbt.".$dbt_select.") as nb";
1131 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1132 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1133 $sql .= " AND dbt.fk_soc = ".((int) $user->socid);
1134 } elseif (isModEnabled("societe") && ($user->hasRight('societe', 'lire') && !$user->hasRight('societe', 'client', 'voir'))) {
1135 // If internal user: Check permission for internal users that are restricted on their objects
1136 $sql = "SELECT COUNT(dbt.".$dbt_select.") as nb";
1137 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1138 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON dbt.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
1139 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1140 $sql .= " AND (dbt.fk_soc IS NULL OR sc.fk_soc IS NOT NULL)"; // Contact not linked to a company or to a company of user
1141 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
1142 } elseif (isModEnabled('multicompany')) {
1143 // If multicompany and internal users with all permissions, check user is in correct entity
1144 $sql = "SELECT COUNT(dbt.".$dbt_select.") as nb";
1145 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1146 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1147 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
1148 }
1149
1150 $checkonentitydone = 1;
1151 }
1152 if (in_array($feature, $checkproject) && $objectid > 0) {
1153 if (isModEnabled('project') && !$user->hasRight('projet', 'all', 'lire')) {
1154 $projectid = $objectid;
1155
1156 include_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
1157 $projectstatic = new Project($db);
1158 $tmps = $projectstatic->getProjectsAuthorizedForUser($user, 0, 1, 0);
1159
1160 $tmparray = explode(',', $tmps);
1161 if (!in_array($projectid, $tmparray)) {
1162 return false;
1163 }
1164 } else {
1165 $sql = "SELECT COUNT(dbt.".$dbt_select.") as nb";
1166 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1167 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1168 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
1169 }
1170 $checkonentitydone = 1;
1171 }
1172 if (in_array($feature, $checktask) && (int) $objectid > 0) {
1173 if (isModEnabled('project') && !$user->hasRight('projet', 'all', 'lire')) {
1174 $task = new Task($db);
1175 $task->fetch((int) $objectid);
1176 $projectid = $task->fk_project;
1177
1178 include_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
1179 $projectstatic = new Project($db);
1180 $tmps = $projectstatic->getProjectsAuthorizedForUser($user, 0, 1, 0);
1181
1182 $tmparray = explode(',', $tmps);
1183 if (!in_array($projectid, $tmparray)) {
1184 return false;
1185 }
1186 } else {
1187 $sharedelement = 'project'; // for multicompany compatibility
1188 $sql = "SELECT COUNT(dbt.".$dbt_select.") as nb";
1189 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1190 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1191 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
1192 }
1193
1194 $checkonentitydone = 1;
1195 }
1196 //var_dump($sql);
1197
1198 if (!$checkonentitydone && !in_array($feature, $nocheck) && $objectid > 0) { // By default (case of $checkdefault), we check on object entity + link to third party on field $dbt_keyfield
1199 // If external user: Check permission for external users
1200 if ($user->socid > 0) {
1201 if (empty($dbt_keyfield)) {
1202 dol_print_error(null, 'Param dbt_keyfield is required but not defined');
1203 }
1204 $sql = "SELECT COUNT(dbt.".$dbt_keyfield.") as nb";
1205 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1206 $sql .= " WHERE dbt.rowid IN (".$db->sanitize($objectid, 1).")";
1207 $sql .= " AND dbt.".$dbt_keyfield." = ".((int) $user->socid);
1208 } elseif (isModEnabled("societe") && !$user->hasRight('societe', 'client', 'voir')) {
1209 // If internal user without permission to see all thirdparties: Check permission for internal users that are restricted on their objects
1210 if (empty($dbt_keyfield)) {
1211 dol_print_error(null, 'Param dbt_keyfield is required but not defined');
1212 }
1213 if ($feature != 'ticket') {
1214 $sql = "SELECT COUNT(sc.fk_soc) as nb";
1215 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1216 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
1217 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1218 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
1219 $sql .= " AND sc.fk_soc = dbt.".$dbt_keyfield;
1220 $sql .= " AND (sc.fk_user = ".((int) $user->id);
1221 if (getDolGlobalInt('MAIN_SEE_SUBORDINATES')) {
1222 $userschilds = $user->getAllChildIds();
1223 if (!empty($userschilds)) $sql .= " OR sc.fk_user IN (".$db->sanitize(implode(',', $userschilds)).")";
1224 }
1225 $sql .= ')';
1226 } else {
1227 // On ticket, the thirdparty is not mandatory, so we need a special test to accept record with no thirdparties.
1228 $sql = "SELECT COUNT(dbt.".$dbt_select.") as nb";
1229 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1230 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = dbt.".$dbt_keyfield." AND sc.fk_user = ".((int) $user->id);
1231 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1232 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
1233 $sql .= " AND (sc.fk_user = ".((int) $user->id)." OR dbt.".$dbt_keyfield." IS NULL OR dbt.".$dbt_keyfield." = 0)";
1234 }
1235 } elseif (isModEnabled('multicompany')) {
1236 // If multicompany, and user is an internal user with all permissions, check that object is in correct entity
1237 $sql = "SELECT COUNT(dbt.".$dbt_select.") as nb";
1238 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1239 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1240 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
1241 }
1242 }
1243
1244 // For events, check on users assigned to event
1245 if ($feature === 'agenda' && ((int) $objectid) > 0) {
1246 // Also check owner or attendee for users without allactions->read
1247 if (/* $objectid > 0 && */ !$user->hasRight('agenda', 'allactions', 'read')) {
1248 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
1249 $action = new ActionComm($db);
1250 $action->fetch((int) $objectid);
1251 if ($action->authorid != $user->id && $action->userownerid != $user->id && !(array_key_exists($user->id, $action->userassigned))) {
1252 return false;
1253 }
1254 }
1255 }
1256
1257 // For some object, we also have to check it is in the user hierarchy
1258 // Param $object must be the full object and not a simple id to have this test possible.
1259 if (in_array($feature, $checkhierarchy) && is_object($object) && $objectid > 0) {
1260 $childids = $user->getAllChildIds(1);
1261 $useridtocheck = 0;
1262 if ($feature == 'holiday') {
1263 $useridtocheck = $object->fk_user;
1264 if (!$user->hasRight('holiday', 'readall') && !in_array($useridtocheck, $childids) && !in_array($object->fk_validator, $childids)) {
1265 return false;
1266 }
1267 }
1268 if ($feature == 'expensereport') {
1269 $useridtocheck = $object->fk_user_author;
1270 if (!$user->hasRight('expensereport', 'readall')) {
1271 if (!in_array($useridtocheck, $childids)) {
1272 return false;
1273 }
1274 }
1275 }
1276 if ($feature == 'hrm' && in_array('evaluation', $feature2)) {
1277 $useridtocheck = $object->fk_user;
1278
1279 if ($user->hasRight('hrm', 'evaluation', 'readall')) {
1280 // the user can view evaluations for anyone
1281 return true;
1282 }
1283 if (!$user->hasRight('hrm', 'evaluation', 'read')) {
1284 // the user can't view any evaluations
1285 return false;
1286 }
1287 // the user can only see their own evaluations or their subordinates'
1288 return in_array($useridtocheck, $childids);
1289 }
1290 }
1291
1292 // For some object, we also have to check it is public or owned by user
1293 // Param $object must be the full object and not a simple id to have this test possible.
1294 if (in_array($feature, $checkuser) && is_object($object) && $objectid > 0) {
1295 $useridtocheck = $object->fk_user;
1296 if (!empty($useridtocheck) && $useridtocheck > 0 && $useridtocheck != $user->id && empty($user->admin)) {
1297 return false;
1298 }
1299 }
1300
1301 if ($sql) {
1302 $resql = $db->query($sql);
1303 if ($resql) {
1304 $obj = $db->fetch_object($resql);
1305 if (!$obj || $obj->nb < count(explode(',', $objectid))) { // error if we found 0 or less record than nb of id provided
1306 return false;
1307 }
1308 } else {
1309 dol_syslog("Bad forged sql in security.lib.php::checkUserAccessToObject", LOG_WARNING);
1310 return false;
1311 }
1312 }
1313 }
1314
1315 dol_syslog("security.lib.php::checkUserAccessToObject::return True", LOG_DEBUG);
1316 return true;
1317}
1318
1319
1331function httponly_accessforbidden($message = '1', $http_response_code = 403, $stringalreadysanitized = 0)
1332{
1333 top_httphead();
1334 http_response_code($http_response_code);
1335
1336 if ($stringalreadysanitized) {
1337 print $message;
1338 } else {
1339 print htmlentities($message);
1340 }
1341
1342 exit(1);
1343}
1344
1358function accessforbidden($message = '', $printheader = 1, $printfooter = 1, $showonlymessage = 0, $params = null)
1359{
1360 global $conf, $db, $user, $langs, $hookmanager;
1361 global $action, $object;
1362
1363 if (!is_object($langs)) {
1364 include_once DOL_DOCUMENT_ROOT.'/core/class/translate.class.php';
1365 $langs = new Translate('', $conf);
1366 $langs->setDefaultLang();
1367 }
1368
1369 $langs->loadLangs(array("main", "errors"));
1370
1371 if ($printheader && !defined('NOHEADERNOFOOTER')) {
1372 if (function_exists("llxHeader")) {
1373 llxHeader('');
1374 } elseif (function_exists("llxHeaderVierge")) {
1375 llxHeaderVierge('');
1376 }
1377 print '<div style="padding: 20px">';
1378 }
1379 print '<div class="error">';
1380 if (empty($message)) {
1381 print $langs->trans("ErrorForbidden");
1382 } else {
1383 print $langs->trans($message);
1384 }
1385 print '</div>';
1386 print '<br>';
1387 if (empty($showonlymessage)) {
1388 if (empty($hookmanager)) {
1389 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
1390 $hookmanager = new HookManager($db);
1391 // Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
1392 $hookmanager->initHooks(array('main'));
1393 }
1394
1395 $parameters = array('message' => $message, 'params' => $params);
1396 $reshook = $hookmanager->executeHooks('getAccessForbiddenMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
1397 print $hookmanager->resPrint;
1398 if (empty($reshook)) {
1399 $langs->loadLangs(array("errors"));
1400 if ($user->login) {
1401 print $langs->trans("CurrentLogin").': <span class="error">'.$user->login.'</span><br>';
1402 print $langs->trans("ErrorForbidden2", $langs->transnoentitiesnoconv("Home"), $langs->transnoentitiesnoconv("Users"));
1403 print $langs->trans("ErrorForbidden4");
1404 } else {
1405 print $langs->trans("ErrorForbidden3");
1406 }
1407 }
1408 }
1409 if ($printfooter && !defined('NOHEADERNOFOOTER') && function_exists("llxFooter")) {
1410 print '</div>';
1411 llxFooter();
1412 }
1413
1414 exit(0);
1415}
1416
1417
1425{
1426 $max = getDolGlobalString('MAIN_UPLOAD_DOC'); // In Kb
1427
1428 $maxphp = @ini_get('upload_max_filesize'); // In unknown
1429 if (preg_match('/k$/i', $maxphp)) {
1430 $maxphp = preg_replace('/k$/i', '', $maxphp);
1431 $maxphp = (int) ((float) $maxphp * 1);
1432 }
1433 if (preg_match('/m$/i', $maxphp)) {
1434 $maxphp = preg_replace('/m$/i', '', $maxphp);
1435 $maxphp = (int) ((float) $maxphp * 1024);
1436 }
1437 if (preg_match('/g$/i', $maxphp)) {
1438 $maxphp = preg_replace('/g$/i', '', $maxphp);
1439 $maxphp = (int) ((float) $maxphp * 1024 * 1024);
1440 }
1441 if (preg_match('/t$/i', $maxphp)) {
1442 $maxphp = preg_replace('/t$/i', '', $maxphp);
1443 $maxphp = (int) ((float) $maxphp * 1024 * 1024 * 1024);
1444 }
1445 $maxphp2 = @ini_get('post_max_size'); // In unknown
1446 if (preg_match('/k$/i', $maxphp2)) {
1447 $maxphp2 = preg_replace('/k$/i', '', $maxphp2);
1448 $maxphp2 = (int) ((float) $maxphp2) * 1;
1449 }
1450 if (preg_match('/m$/i', $maxphp2)) {
1451 $maxphp2 = preg_replace('/m$/i', '', $maxphp2);
1452 $maxphp2 = (int) ((float) $maxphp2 * 1024);
1453 }
1454 if (preg_match('/g$/i', $maxphp2)) {
1455 $maxphp2 = preg_replace('/g$/i', '', $maxphp2);
1456 $maxphp2 = (int) ((float) $maxphp2 * 1024 * 1024);
1457 }
1458 if (preg_match('/t$/i', $maxphp2)) {
1459 $maxphp2 = preg_replace('/t$/i', '', $maxphp2);
1460 $maxphp2 = (int) ((float) $maxphp2 * 1024 * 1024 * 1024);
1461 }
1462 // Now $max and $maxphp and $maxphp2 are in Kb
1463 $maxmin = $max;
1464 $maxphptoshow = $maxphptoshowparam = '';
1465 if ($maxphp > 0) {
1466 $maxmin = min($maxmin, $maxphp);
1467 $maxphptoshow = $maxphp;
1468 $maxphptoshowparam = 'upload_max_filesize';
1469 }
1470 if ($maxphp2 > 0) {
1471 $maxmin = min($maxmin, $maxphp2);
1472 if ($maxphp2 < $maxphp) {
1473 $maxphptoshow = $maxphp2;
1474 $maxphptoshowparam = 'post_max_size';
1475 }
1476 }
1477 //var_dump($maxphp.'-'.$maxphp2);
1478 //var_dump($maxmin);
1479
1480 return array('max' => $max, 'maxmin' => $maxmin, 'maxphptoshow' => $maxphptoshow, 'maxphptoshowparam' => $maxphptoshowparam);
1481}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
if(!defined( 'NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined( 'NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) if(!defined( 'NOLOGIN')) if(!defined('NOCSRFCHECK')) if(!defined( 'NOIPCHECK')) llxHeaderVierge($title, $head="", $disablejs=0, $disablehead=0, $arrayofjs=[], $arrayofcss=[])
Header function.
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to manage agenda events (actions)
Class to manage hooks.
Class to manage projects.
Class to manage tasks.
Class to manage translations.
Class to manage Dolibarr users.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_substr($string, $start, $length=null, $stringencoding='', $trunconbytes=0)
Make a substring.
ascii_check($str)
Check if a string is in ASCII.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
dolGetRandomBytes($length)
Return a string of random bytes (hexa string) with length = $length for cryptographic purposes.
httponly_accessforbidden($message='1', $http_response_code=403, $stringalreadysanitized=0)
Show a message to say access is forbidden and stop program.
dol_encode($chain, $key='1')
Encode a string with base 64 algorithm + specific delta change.
dol_hash($chain, $type='0', $nosalt=0, $mode=0)
Returns a hash (non reversible encryption) of a string.
checkUserAccessToObject($user, array $featuresarray, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='', $dbt_select='rowid', $parenttableforentity='')
Check that access by a given user to an object is ok.
dol_verifyHash($chain, $hash, $type='0')
Compute a hash and compare it to the given one For backward compatibility reasons,...
dolEncrypt($chain, $key='', $ciphering='', $forceseed='')
Encode a string with a symmetric encryption.
getMaxFileSizeArray()
Return the max allowed for file upload.
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.
dol_decode($chain, $key='1')
Decode a base 64 encoded + specific delta change.
dolGetLdapPasswordHash($password, $type='md5')
Returns a specific ldap hash of a password.
dolDecrypt($chain, $key='')
Decode a string with a symmetric encryption.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.