dolibarr 22.0.5
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 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * or see https://www.gnu.org/
21 */
22
40function dol_encode($chain, $key = '1')
41{
42 if (is_numeric($key) && $key == '1') { // rule 1 is offset of 17 for char
43 $output_tab = array();
44 $strlength = dol_strlen($chain);
45 for ($i = 0; $i < $strlength; $i++) {
46 $output_tab[$i] = chr(ord(substr($chain, $i, 1)) + 17);
47 }
48 $chain = implode("", $output_tab);
49 } elseif ($key) {
50 $result = '';
51 $strlength = dol_strlen($chain);
52 for ($i = 0; $i < $strlength; $i++) {
53 $keychar = substr($key, ($i % strlen($key)) - 1, 1);
54 $result .= chr(ord(substr($chain, $i, 1)) + (ord($keychar) - 65));
55 }
56 $chain = $result;
57 }
58
59 return base64_encode($chain);
60}
61
71function dol_decode($chain, $key = '1')
72{
73 $chain = base64_decode($chain);
74
75 if (is_numeric($key) && $key == '1') { // rule 1 is offset of 17 for char
76 $output_tab = array();
77 $strlength = dol_strlen($chain);
78 for ($i = 0; $i < $strlength; $i++) {
79 $output_tab[$i] = chr(ord(substr($chain, $i, 1)) - 17);
80 }
81
82 $chain = implode("", $output_tab);
83 } elseif ($key) {
84 $result = '';
85 $strlength = dol_strlen($chain);
86 for ($i = 0; $i < $strlength; $i++) {
87 $keychar = substr($key, ($i % strlen($key)) - 1, 1);
88 $result .= chr(ord(substr($chain, $i, 1)) - (ord($keychar) - 65));
89 }
90 $chain = $result;
91 }
92
93 return $chain;
94}
95
102function dolGetRandomBytes($length)
103{
104 if (function_exists('random_bytes')) { // Available with PHP 7 only.
105 return bin2hex(random_bytes((int) floor($length / 2))); // the bin2hex will double the number of bytes so we take length / 2
106 }
107
108 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.
109}
110
111
112define('MAIN_SECURITY_REVERSIBLE_ALGO', 'AES-256-CTR');
113
127function dolEncrypt($chain, $key = '', $ciphering = '', $forceseed = '')
128{
129 global $conf;
130 global $dolibarr_disable_dolcrypt_for_debug;
131
132 if ($chain === '' || is_null($chain)) {
133 return '';
134 }
135
136 $reg = array();
137 if (preg_match('/^dolcrypt:([^:]+):(.+)$/', $chain, $reg)) {
138 // The $chain is already a encrypted string
139 return $chain;
140 }
141
142 if (empty($key)) {
143 $key = $conf->file->instance_unique_id;
144 }
145 if (empty($ciphering)) {
146 $ciphering = constant('MAIN_SECURITY_REVERSIBLE_ALGO');
147 }
148
149 $newchain = $chain;
150
151 if (function_exists('openssl_encrypt') && empty($dolibarr_disable_dolcrypt_for_debug)) {
152 if (empty($key)) {
153 return $chain;
154 }
155
156 $ivlen = 16;
157 if (function_exists('openssl_cipher_iv_length')) {
158 $ivlen = openssl_cipher_iv_length($ciphering);
159 }
160 if ($ivlen === false || $ivlen < 1 || $ivlen > 32) {
161 $ivlen = 16;
162 }
163 if (empty($forceseed)) {
164 $ivseed = dolGetRandomBytes($ivlen);
165 } else {
166 $ivseed = dol_substr(md5($forceseed), 0, $ivlen, 'ascii', 1);
167 }
168
169 $newchain = openssl_encrypt($chain, $ciphering, $key, 0, $ivseed);
170 return 'dolcrypt:'.$ciphering.':'.$ivseed.':'.$newchain;
171 } else {
172 return $chain;
173 }
174}
175
186function dolDecrypt($chain, $key = '')
187{
188 global $conf;
189
190 if ($chain === '' || is_null($chain)) {
191 return '';
192 }
193
194 if (empty($key)) {
195 if (!empty($conf->file->dolcrypt_key)) {
196 // If dolcrypt_key is defined, we used it in priority (coming from $dolibarr_main_instance_unique_id)
197 $key = $conf->file->dolcrypt_key;
198 } else {
199 // We fall back on the instance_unique_id (coming from $dolibarr_main_instance_unique_id)
200 $key = !empty($conf->file->instance_unique_id) ? $conf->file->instance_unique_id : "";
201 }
202 }
203
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 $dbtablename = 'projet_task';
980 }
981 if ($feature == 'eventorganization') {
982 $feature = 'agenda';
983 $dbtablename = 'actioncomm';
984 }
985 if ($feature == 'payment_sc' && empty($parenttableforentity)) {
986 // If we check perm on payment page but $parenttableforentity not defined, we force value on parent table
987 $parenttableforentity = '';
988 $dbtablename = "chargesociales";
989 $feature = "chargesociales";
990 $objectid = $object->fk_charge;
991 }
992
993 $checkonentitydone = 0;
994
995 // Array to define rules of checks to do
996 $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)
997 $checksoc = array('societe'); // Test for object Societe
998 $checkparentsoc = array('agenda', 'contact', 'contrat'); // Test on entity + link to third party on field $dbt_keyfield. Allowed if link is empty (Ex: contacts...).
999 $checkproject = array('projet', 'project'); // Test for project object
1000 $checktask = array('projet_task', 'project_task'); // Test for task object
1001 $checkhierarchy = array('expensereport', 'holiday', 'hrm'); // check permission among the hierarchy of user
1002 $checkuser = array('bookmark'); // check permission among the fk_user (must be myself or null)
1003 $nocheck = array('barcode'); // No test
1004
1005 //$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...).
1006
1007 // If dbtablename not defined, we use same name for table than module name
1008 if (empty($dbtablename)) {
1009 $dbtablename = $feature;
1010 $sharedelement = (!empty($params[1]) ? $params[1] : $dbtablename); // We change dbtablename, so we set sharedelement too.
1011 }
1012
1013 // To avoid an access forbidden with a numeric ref
1014 if ($dbt_select != 'rowid' && $dbt_select != 'id') {
1015 $objectid = "'".$objectid."'"; // Note: $objectid was already cast into int at begin of this method.
1016 }
1017 // Check permission for objectid on entity only
1018 if (in_array($feature, $check) && $objectid > 0) { // For $objectid = 0, no check
1019 $sql = "SELECT COUNT(dbt.".$dbt_select.") as nb";
1020 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1021 if (($feature == 'user' || $feature == 'usergroup') && isModEnabled('multicompany')) { // Special for multicompany
1022 if (getDolGlobalString('MULTICOMPANY_TRANSVERSE_MODE')) {
1023 if ($conf->entity == 1 && $user->admin && !$user->entity) {
1024 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1025 $sql .= " AND dbt.entity IS NOT NULL";
1026 } else {
1027 $sql .= ",".MAIN_DB_PREFIX."usergroup_user as ug";
1028 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1029 $sql .= " AND ((ug.fk_user = dbt.rowid";
1030 $sql .= " AND ug.entity IN (".getEntity('usergroup')."))";
1031 $sql .= " OR dbt.entity = 0)"; // Show always superadmin
1032 }
1033 } else {
1034 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1035 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
1036 }
1037 } else {
1038 $reg = array();
1039 if ($parenttableforentity && preg_match('/(.*)@(.*)/', $parenttableforentity, $reg)) {
1040 $sql .= ", ".MAIN_DB_PREFIX.$reg[2]." as dbtp";
1041 $sql .= " WHERE dbt.".$reg[1]." = dbtp.rowid AND dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1042 $sql .= " AND dbtp.entity IN (".getEntity($sharedelement, 1).")";
1043 } else {
1044 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1045 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
1046 }
1047 }
1048 $checkonentitydone = 1;
1049 }
1050 if (in_array($feature, $checksoc) && $objectid > 0) { // We check feature = checksoc. For $objectid = 0, no check
1051 // If external user: Check permission for external users
1052 if ($user->socid > 0) {
1053 if ($user->socid != $objectid) {
1054 return false;
1055 }
1056 } elseif (isModEnabled('societe') && !$user->hasRight('societe', 'lire') && !$user->hasRight('societe', 'client', 'voir')) {
1057 dol_syslog("security.lib.php::checkUserAccessToObject Deny access due: (isModEnabled('societe') && !user->hasRight('societe', 'lire') && !user->hasRight('societe', 'client', 'voir'))", LOG_DEBUG);
1058 return false;
1059 } elseif (isModEnabled("societe") && ($user->hasRight('societe', 'lire') && !$user->hasRight('societe', 'client', 'voir'))) {
1060 // If internal user: Check permission for internal users that are restricted on their objects
1061 $sql = "SELECT COUNT(sc.fk_soc) as nb";
1062 $sql .= " FROM (".MAIN_DB_PREFIX."societe_commerciaux as sc";
1063 $sql .= ", ".MAIN_DB_PREFIX."societe as s)";
1064 $sql .= " WHERE sc.fk_soc IN (".$db->sanitize($objectid, 1).")";
1065 $sql .= " AND (sc.fk_user = ".((int) $user->id);
1066 if (getDolGlobalInt('MAIN_SEE_SUBORDINATES')) {
1067 $userschilds = $user->getAllChildIds();
1068 if (!empty($userschilds)) $sql .= " OR sc.fk_user IN (".$db->sanitize(implode(',', $userschilds)).")";
1069 }
1070 $sql .= ")";
1071 $sql .= " AND sc.fk_soc = s.rowid";
1072 $sql .= " AND s.entity IN (".getEntity($sharedelement, 1).")";
1073 } elseif (isModEnabled('multicompany')) {
1074 // If multicompany and internal users with all permissions, check user is in correct entity
1075 $sql = "SELECT COUNT(s.rowid) as nb";
1076 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
1077 $sql .= " WHERE s.rowid IN (".$db->sanitize($objectid, 1).")";
1078 $sql .= " AND s.entity IN (".getEntity($sharedelement, 1).")";
1079 }
1080
1081 $checkonentitydone = 1;
1082 }
1083 if (in_array($feature, $checkparentsoc) && $objectid > 0) { // Test on entity + link to thirdparty. Allowed if link is empty (Ex: contacts...).
1084 // If external user: Check permission for external users
1085 if ($user->socid > 0) {
1086 $sql = "SELECT COUNT(dbt.".$dbt_select.") as nb";
1087 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1088 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1089 $sql .= " AND dbt.fk_soc = ".((int) $user->socid);
1090 } elseif (isModEnabled("societe") && ($user->hasRight('societe', 'lire') && !$user->hasRight('societe', 'client', 'voir'))) {
1091 // If internal user: Check permission for internal users that are restricted on their objects
1092 $sql = "SELECT COUNT(dbt.".$dbt_select.") as nb";
1093 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1094 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON dbt.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
1095 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1096 $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
1097 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
1098 } elseif (isModEnabled('multicompany')) {
1099 // If multicompany and internal users with all permissions, check user is in correct entity
1100 $sql = "SELECT COUNT(dbt.".$dbt_select.") as nb";
1101 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1102 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1103 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
1104 }
1105
1106 $checkonentitydone = 1;
1107 }
1108 if (in_array($feature, $checkproject) && $objectid > 0) {
1109 if (isModEnabled('project') && !$user->hasRight('projet', 'all', 'lire')) {
1110 $projectid = $objectid;
1111
1112 include_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
1113 $projectstatic = new Project($db);
1114 $tmps = $projectstatic->getProjectsAuthorizedForUser($user, 0, 1, 0);
1115
1116 $tmparray = explode(',', $tmps);
1117 if (!in_array($projectid, $tmparray)) {
1118 return false;
1119 }
1120 } else {
1121 $sql = "SELECT COUNT(dbt.".$dbt_select.") as nb";
1122 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1123 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1124 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
1125 }
1126 $checkonentitydone = 1;
1127 }
1128 if (in_array($feature, $checktask) && (int) $objectid > 0) {
1129 if (isModEnabled('project') && !$user->hasRight('projet', 'all', 'lire')) {
1130 $task = new Task($db);
1131 $task->fetch((int) $objectid);
1132 $projectid = $task->fk_project;
1133
1134 include_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
1135 $projectstatic = new Project($db);
1136 $tmps = $projectstatic->getProjectsAuthorizedForUser($user, 0, 1, 0);
1137
1138 $tmparray = explode(',', $tmps);
1139 if (!in_array($projectid, $tmparray)) {
1140 return false;
1141 }
1142 } else {
1143 $sharedelement = 'project'; // for multicompany compatibility
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 //var_dump($sql);
1153
1154 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
1155 // If external user: Check permission for external users
1156 if ($user->socid > 0) {
1157 if (empty($dbt_keyfield)) {
1158 dol_print_error(null, 'Param dbt_keyfield is required but not defined');
1159 }
1160 $sql = "SELECT COUNT(dbt.".$dbt_keyfield.") as nb";
1161 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1162 $sql .= " WHERE dbt.rowid IN (".$db->sanitize($objectid, 1).")";
1163 $sql .= " AND dbt.".$dbt_keyfield." = ".((int) $user->socid);
1164 } elseif (isModEnabled("societe") && !$user->hasRight('societe', 'client', 'voir')) {
1165 // If internal user without permission to see all thirdparties: Check permission for internal users that are restricted on their objects
1166 if ($feature != 'ticket') {
1167 if (empty($dbt_keyfield)) {
1168 dol_print_error(null, 'Param dbt_keyfield is required but not defined');
1169 }
1170 $sql = "SELECT COUNT(sc.fk_soc) as nb";
1171 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1172 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
1173 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1174 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
1175 $sql .= " AND sc.fk_soc = dbt.".$dbt_keyfield;
1176 $sql .= " AND (sc.fk_user = ".((int) $user->id);
1177 if (getDolGlobalInt('MAIN_SEE_SUBORDINATES')) {
1178 $userschilds = $user->getAllChildIds();
1179 if (!empty($userschilds)) $sql .= " OR sc.fk_user IN (".$db->sanitize(implode(',', $userschilds)).")";
1180 }
1181 $sql .= ')';
1182 } else {
1183 // On ticket, the thirdparty is not mandatory, so we need a special test to accept record with no thirdparties.
1184 $sql = "SELECT COUNT(dbt.".$dbt_select.") as nb";
1185 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1186 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = dbt.".$dbt_keyfield." AND sc.fk_user = ".((int) $user->id);
1187 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1188 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
1189 $sql .= " AND (sc.fk_user = ".((int) $user->id)." OR sc.fk_user IS NULL)";
1190 }
1191 } elseif (isModEnabled('multicompany')) {
1192 // If multicompany, and user is an internal user with all permissions, check that object is in correct entity
1193 $sql = "SELECT COUNT(dbt.".$dbt_select.") as nb";
1194 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
1195 $sql .= " WHERE dbt.".$dbt_select." IN (".$db->sanitize($objectid, 1).")";
1196 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
1197 }
1198 }
1199
1200 // For events, check on users assigned to event
1201 if ($feature === 'agenda' && ((int) $objectid) > 0) {
1202 // Also check owner or attendee for users without allactions->read
1203 if (/* $objectid > 0 && */ !$user->hasRight('agenda', 'allactions', 'read')) {
1204 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
1205 $action = new ActionComm($db);
1206 $action->fetch((int) $objectid);
1207 if ($action->authorid != $user->id && $action->userownerid != $user->id && !(array_key_exists($user->id, $action->userassigned))) {
1208 return false;
1209 }
1210 }
1211 }
1212
1213 // For some object, we also have to check it is in the user hierarchy
1214 // Param $object must be the full object and not a simple id to have this test possible.
1215 if (in_array($feature, $checkhierarchy) && is_object($object) && $objectid > 0) {
1216 $childids = $user->getAllChildIds(1);
1217 $useridtocheck = 0;
1218 if ($feature == 'holiday') {
1219 $useridtocheck = $object->fk_user;
1220 if (!$user->hasRight('holiday', 'readall') && !in_array($useridtocheck, $childids) && !in_array($object->fk_validator, $childids)) {
1221 return false;
1222 }
1223 }
1224 if ($feature == 'expensereport') {
1225 $useridtocheck = $object->fk_user_author;
1226 if (!$user->hasRight('expensereport', 'readall')) {
1227 if (!in_array($useridtocheck, $childids)) {
1228 return false;
1229 }
1230 }
1231 }
1232 if ($feature == 'hrm' && in_array('evaluation', $feature2)) {
1233 $useridtocheck = $object->fk_user;
1234
1235 if ($user->hasRight('hrm', 'evaluation', 'readall')) {
1236 // the user can view evaluations for anyone
1237 return true;
1238 }
1239 if (!$user->hasRight('hrm', 'evaluation', 'read')) {
1240 // the user can't view any evaluations
1241 return false;
1242 }
1243 // the user can only see their own evaluations or their subordinates'
1244 return in_array($useridtocheck, $childids);
1245 }
1246 }
1247
1248 // For some object, we also have to check it is public or owned by user
1249 // Param $object must be the full object and not a simple id to have this test possible.
1250 if (in_array($feature, $checkuser) && is_object($object) && $objectid > 0) {
1251 $useridtocheck = $object->fk_user;
1252 if (!empty($useridtocheck) && $useridtocheck > 0 && $useridtocheck != $user->id && empty($user->admin)) {
1253 return false;
1254 }
1255 }
1256
1257 if ($sql) {
1258 $resql = $db->query($sql);
1259 if ($resql) {
1260 $obj = $db->fetch_object($resql);
1261 if (!$obj || $obj->nb < count(explode(',', $objectid))) { // error if we found 0 or less record than nb of id provided
1262 return false;
1263 }
1264 } else {
1265 dol_syslog("Bad forged sql in security.lib.php::checkUserAccessToObject", LOG_WARNING);
1266 return false;
1267 }
1268 }
1269 }
1270
1271 dol_syslog("security.lib.php::checkUserAccessToObject::return True", LOG_DEBUG);
1272 return true;
1273}
1274
1275
1287function httponly_accessforbidden($message = '1', $http_response_code = 403, $stringalreadysanitized = 0)
1288{
1289 top_httphead();
1290 http_response_code($http_response_code);
1291
1292 if ($stringalreadysanitized) {
1293 print $message;
1294 } else {
1295 print htmlentities($message);
1296 }
1297
1298 exit(1);
1299}
1300
1314function accessforbidden($message = '', $printheader = 1, $printfooter = 1, $showonlymessage = 0, $params = null)
1315{
1316 global $conf, $db, $user, $langs, $hookmanager;
1317 global $action, $object;
1318
1319 if (!is_object($langs)) {
1320 include_once DOL_DOCUMENT_ROOT.'/core/class/translate.class.php';
1321 $langs = new Translate('', $conf);
1322 $langs->setDefaultLang();
1323 }
1324
1325 $langs->loadLangs(array("main", "errors"));
1326
1327 if ($printheader && !defined('NOHEADERNOFOOTER')) {
1328 if (function_exists("llxHeader")) {
1329 llxHeader('');
1330 } elseif (function_exists("llxHeaderVierge")) {
1331 llxHeaderVierge('');
1332 }
1333 print '<div style="padding: 20px">';
1334 }
1335 print '<div class="error">';
1336 if (empty($message)) {
1337 print $langs->trans("ErrorForbidden");
1338 } else {
1339 print $langs->trans($message);
1340 }
1341 print '</div>';
1342 print '<br>';
1343 if (empty($showonlymessage)) {
1344 if (empty($hookmanager)) {
1345 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
1346 $hookmanager = new HookManager($db);
1347 // Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
1348 $hookmanager->initHooks(array('main'));
1349 }
1350
1351 $parameters = array('message' => $message, 'params' => $params);
1352 $reshook = $hookmanager->executeHooks('getAccessForbiddenMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
1353 print $hookmanager->resPrint;
1354 if (empty($reshook)) {
1355 $langs->loadLangs(array("errors"));
1356 if ($user->login) {
1357 print $langs->trans("CurrentLogin").': <span class="error">'.$user->login.'</span><br>';
1358 print $langs->trans("ErrorForbidden2", $langs->transnoentitiesnoconv("Home"), $langs->transnoentitiesnoconv("Users"));
1359 print $langs->trans("ErrorForbidden4");
1360 } else {
1361 print $langs->trans("ErrorForbidden3");
1362 }
1363 }
1364 }
1365 if ($printfooter && !defined('NOHEADERNOFOOTER') && function_exists("llxFooter")) {
1366 print '</div>';
1367 llxFooter();
1368 }
1369
1370 exit(0);
1371}
1372
1373
1381{
1382 $max = getDolGlobalString('MAIN_UPLOAD_DOC'); // In Kb
1383
1384 $maxphp = @ini_get('upload_max_filesize'); // In unknown
1385 if (preg_match('/k$/i', $maxphp)) {
1386 $maxphp = preg_replace('/k$/i', '', $maxphp);
1387 $maxphp = (int) ((float) $maxphp * 1);
1388 }
1389 if (preg_match('/m$/i', $maxphp)) {
1390 $maxphp = preg_replace('/m$/i', '', $maxphp);
1391 $maxphp = (int) ((float) $maxphp * 1024);
1392 }
1393 if (preg_match('/g$/i', $maxphp)) {
1394 $maxphp = preg_replace('/g$/i', '', $maxphp);
1395 $maxphp = (int) ((float) $maxphp * 1024 * 1024);
1396 }
1397 if (preg_match('/t$/i', $maxphp)) {
1398 $maxphp = preg_replace('/t$/i', '', $maxphp);
1399 $maxphp = (int) ((float) $maxphp * 1024 * 1024 * 1024);
1400 }
1401 $maxphp2 = @ini_get('post_max_size'); // In unknown
1402 if (preg_match('/k$/i', $maxphp2)) {
1403 $maxphp2 = preg_replace('/k$/i', '', $maxphp2);
1404 $maxphp2 = (int) ((float) $maxphp2) * 1;
1405 }
1406 if (preg_match('/m$/i', $maxphp2)) {
1407 $maxphp2 = preg_replace('/m$/i', '', $maxphp2);
1408 $maxphp2 = (int) ((float) $maxphp2 * 1024);
1409 }
1410 if (preg_match('/g$/i', $maxphp2)) {
1411 $maxphp2 = preg_replace('/g$/i', '', $maxphp2);
1412 $maxphp2 = (int) ((float) $maxphp2 * 1024 * 1024);
1413 }
1414 if (preg_match('/t$/i', $maxphp2)) {
1415 $maxphp2 = preg_replace('/t$/i', '', $maxphp2);
1416 $maxphp2 = (int) ((float) $maxphp2 * 1024 * 1024 * 1024);
1417 }
1418 // Now $max and $maxphp and $maxphp2 are in Kb
1419 $maxmin = $max;
1420 $maxphptoshow = $maxphptoshowparam = '';
1421 if ($maxphp > 0) {
1422 $maxmin = min($maxmin, $maxphp);
1423 $maxphptoshow = $maxphp;
1424 $maxphptoshowparam = 'upload_max_filesize';
1425 }
1426 if ($maxphp2 > 0) {
1427 $maxmin = min($maxmin, $maxphp2);
1428 if ($maxphp2 < $maxphp) {
1429 $maxphptoshow = $maxphp2;
1430 $maxphptoshowparam = 'post_max_size';
1431 }
1432 }
1433 //var_dump($maxphp.'-'.$maxphp2);
1434 //var_dump($maxmin);
1435
1436 return array('max' => $max, 'maxmin' => $maxmin, 'maxphptoshow' => $maxphptoshow, 'maxphptoshowparam' => $maxphptoshowparam);
1437}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
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.
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.