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