dolibarr 24.0.1
security.lib.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (C) 2008-2021 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2008-2021 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2020 Ferran Marcet <fmarcet@2byte.es>
6 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
7 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
8 * Copyright (C) 2026 William Mead <william@m34d.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 * or see https://www.gnu.org/
23 */
24
33include_once DOL_DOCUMENT_ROOT.'/blockedlog/lib/securitycore.lib.php';
34
35
44function dol_encode($chain, $key = '1')
45{
46 if (is_numeric($key) && $key == '1') { // rule 1 is offset of 17 for char
47 $output_tab = array();
48 $strlength = dol_strlen($chain);
49 for ($i = 0; $i < $strlength; $i++) {
50 $output_tab[$i] = chr(ord(substr($chain, $i, 1)) + 17);
51 }
52 $chain = implode("", $output_tab);
53 } elseif ($key) {
54 $result = '';
55 $strlength = dol_strlen($chain);
56 for ($i = 0; $i < $strlength; $i++) {
57 $keychar = substr($key, ($i % strlen($key)) - 1, 1);
58 $result .= chr(ord(substr($chain, $i, 1)) + (ord($keychar) - 65));
59 }
60 $chain = $result;
61 }
62
63 return base64_encode($chain);
64}
65
75function dol_decode($chain, $key = '1')
76{
77 $chain = base64_decode($chain);
78
79 if (is_numeric($key) && $key == '1') { // rule 1 is offset of 17 for char
80 $output_tab = array();
81 $strlength = dol_strlen($chain);
82 for ($i = 0; $i < $strlength; $i++) {
83 $output_tab[$i] = chr(ord(substr($chain, $i, 1)) - 17);
84 }
85
86 $chain = implode("", $output_tab);
87 } elseif ($key) {
88 $result = '';
89 $strlength = dol_strlen($chain);
90 for ($i = 0; $i < $strlength; $i++) {
91 $keychar = substr($key, ($i % strlen($key)) - 1, 1);
92 $result .= chr(ord(substr($chain, $i, 1)) - (ord($keychar) - 65));
93 }
94 $chain = $result;
95 }
96
97 return $chain;
98}
99
106function dolGetRandomBytes($length)
107{
108 if (function_exists('random_bytes')) { // Available with PHP 7+ only.
109 return bin2hex(random_bytes((int) floor($length / 2))); // the bin2hex will double the number of bytes so we take length / 2
110 }
111
112 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.
113}
114
122function dolGetLdapPasswordHash($password, $type = 'md5')
123{
124 if (empty($type)) {
125 $type = 'md5';
126 }
127
128 $salt = substr(sha1((string) time()), 0, 8);
129
130 if ($type === 'md5') {
131 return '{MD5}' . base64_encode(hash("md5", $password, true)); //For OpenLdap with md5 (based on an unencrypted password in base)
132 } elseif ($type === 'md5frommd5') {
133 return '{MD5}' . base64_encode(hex2bin($password)); // Create OpenLDAP MD5 password from Dolibarr MD5 password
134 } elseif ($type === 'smd5') {
135 return "{SMD5}" . base64_encode(hash("md5", $password . $salt, true) . $salt);
136 } elseif ($type === 'sha') {
137 return '{SHA}' . base64_encode(hash("sha1", $password, true));
138 } elseif ($type === 'ssha') {
139 return "{SSHA}" . base64_encode(hash("sha1", $password . $salt, true) . $salt);
140 } elseif ($type === 'sha256') {
141 return "{SHA256}" . base64_encode(hash("sha256", $password, true));
142 } elseif ($type === 'ssha256') {
143 return "{SSHA256}" . base64_encode(hash("sha256", $password . $salt, true) . $salt);
144 } elseif ($type === 'sha384') {
145 return "{SHA384}" . base64_encode(hash("sha384", $password, true));
146 } elseif ($type === 'ssha384') {
147 return "{SSHA384}" . base64_encode(hash("sha384", $password . $salt, true) . $salt);
148 } elseif ($type === 'sha512') {
149 return "{SHA512}" . base64_encode(hash("sha512", $password, true));
150 } elseif ($type === 'ssha512') {
151 return "{SSHA512}" . base64_encode(hash("sha512", $password . $salt, true) . $salt);
152 } elseif ($type === 'crypt') {
153 return '{CRYPT}' . crypt($password, $salt);
154 } elseif ($type === 'clear') {
155 return '{CLEAR}' . $password; // Just for test, plain text password is not secured !
156 }
157 return "";
158}
159
180function restrictedArea(User $user, $features, $object = 0, $tableandshare = '', $feature2 = '', $dbt_keyfield = 'fk_soc', $dbt_select = 'rowid', $isdraft = 0, $mode = 0)
181{
182 global $hookmanager;
183
184 // Define $objectid
185 if (is_object($object)) {
186 $objectid = $object->id;
187 } else {
188 $objectid = $object; // $objectid can be X or 'X,Y,Z'
189 }
190 if ($objectid == "-1") {
191 $objectid = 0;
192 }
193 if ($objectid) {
194 $objectid = preg_replace('/[^0-9\.\,]/', '', (string) $objectid); // For the case value is coming from a non sanitized user input
195 }
196
197 //dol_syslog("functions.lib:restrictedArea $feature, $object, $dbtablename, $feature2, $dbt_socfield, $dbt_select, $isdraft");
198 /*print "user_id=".$user->id.", features=".$features.", feature2=".$feature2.", object=".$object;
199 print ", dbtablename=".$tableandshare.", dbt_socfield=".$dbt_keyfield.", dbt_select=".$dbt_select;
200 print ", perm: user->hasRight(".$features.($feature2 ? ",".$feature2 : "").", lire) = ".($feature2 ? $user->hasRight($features, $feature2, 'lire') : $user->hasRight($features, 'lire'))."<br>";
201 */
202
203 $parentfortableentity = '';
204
205 // Fix syntax of $features param to support non standard module names.
206 // @todo : use elseif ?
207 $originalfeatures = $features;
208 if ($features == 'agenda') {
209 $tableandshare = 'actioncomm&societe';
210 $feature2 = 'myactions|allactions';
211 $dbt_select = 'id';
212 }
213 if ($features == 'bank') {
214 $features = 'banque';
215 }
216 if ($features == 'facturerec') {
217 $features = 'facture';
218 }
219 if ($features == 'supplier_invoicerec') {
220 $features = 'fournisseur';
221 $feature2 = 'facture';
222 }
223 if ($features == 'mo') {
224 $features = 'mrp';
225 }
226 if ($features == 'member') {
227 $features = 'adherent';
228 }
229 if ($features == 'subscription') {
230 $features = 'adherent';
231 $feature2 = 'cotisation';
232 }
233 if ($features == 'website' && is_object($object) && $object->element == 'websitepage') {
234 $parentfortableentity = 'fk_website@website';
235 }
236 if ($features == 'project') {
237 $features = 'projet';
238 }
239 if ($features == 'product') {
240 $features = 'produit';
241 }
242 if ($features == 'productbatch') {
243 $features = 'produit';
244 }
245 if ($features == 'tax') {
246 $feature2 = 'charges';
247 }
248 if ($features == 'workstation') {
249 $feature2 = 'workstation';
250 }
251 if ($features == 'fournisseur') { // When vendor invoice and purchase order are into module 'fournisseur'
252 $features = 'fournisseur';
253 if (is_object($object) && $object->element == 'invoice_supplier') {
254 $feature2 = 'facture';
255 } elseif (is_object($object) && $object->element == 'order_supplier') {
256 $feature2 = 'commande';
257 }
258 }
259 if ($features == 'payment_sc') {
260 $tableandshare = 'paiementcharge';
261 $parentfortableentity = 'fk_charge@chargesociales';
262 }
263 if ($features == 'payment_vat') {
264 $tableandshare = 'payment_vat';
265 $parentfortableentity = 'fk_tva@tva';
266 }
267
268 // if commonObjectLine : Using many2one related commonObject
269 // @see commonObjectLine::parentElement
270 if (in_array($features, ['commandedet', 'propaldet', 'facturedet', 'supplier_proposaldet', 'evaluationdet', 'skilldet', 'deliverydet', 'contratdet'])) {
271 $features = substr($features, 0, -3);
272 } elseif (in_array($features, ['stocktransferline', 'inventoryline', 'bomline', 'expensereport_det', 'facture_fourn_det'])) {
273 $features = substr($features, 0, -4);
274 } elseif ($features == 'commandefournisseurdispatch') {
275 $features = 'commandefournisseur';
276 } elseif ($features == 'invoice_supplier_det_rec') {
277 $features = 'invoice_supplier_rec';
278 }
279 if ($features == 'evaluation') {
280 $features = 'hrm';
281 $feature2 = 'evaluation';
282 }
283
284 // When the object is a task (element='project_task') and $feature2 is empty,
285 // $checkUserAccessToObject() falls into the $checkproject path and uses the task ID
286 // as project ID, which always fails. Setting $feature2='project_task' triggers the
287 // normalization at line 974 that redirects to the $checktask path, which correctly
288 // resolves $task->fk_project before calling getProjectsAuthorizedForUser().
289 if (is_object($object) && in_array($object->element, array('project_task', 'task'))
290 && (empty($features) || in_array($features, array('projet', 'project')))
291 && empty($feature2)) {
292 $features = 'projet';
293 $feature2 = 'project_task';
294 if (empty($tableandshare)) {
295 $tableandshare = 'projet_task';
296 }
297 }
298
299 // print $features.' - '.$tableandshare.' - '.$feature2.' - '.$dbt_select."\n";
300
301 // Get more permissions checks from hooks
302 $parameters = array(
303 'features' => $features,
304 'feature2' => $feature2,
305 'originalfeatures' => $originalfeatures,
306 'tableandshare' => $tableandshare,
307 'object' => $object,
308 'objectid' => $objectid,
309 'dbt_keyfield' => $dbt_keyfield,
310 'dbt_select' => $dbt_select,
311 'idtype' => $dbt_select,
312 'isdraft' => $isdraft,
313 'mode' => $mode,
314 );
315 if (!empty($hookmanager)) {
316 $reshook = $hookmanager->executeHooks('restrictedArea', $parameters);
317
318 if (isset($hookmanager->resArray['result'])) {
319 if ($hookmanager->resArray['result'] == 0) {
320 if ($mode) {
321 return 0;
322 } else {
323 accessforbidden(); // Module returns 0, so access forbidden
324 }
325 }
326 }
327 if ($reshook > 0) { // No other test done.
328 return 1;
329 }
330 }
331
332 // Features/modules to check (to support the & and | operator)
333 $featuresarray = array($features);
334 if (preg_match('/&/', $features)) {
335 $featuresarray = explode("&", $features);
336 } elseif (preg_match('/\|/', $features)) {
337 $featuresarray = explode("|", $features);
338 }
339
340 // More subfeatures to check
341 if (!empty($feature2)) {
342 $feature2 = explode("|", $feature2);
343 }
344
345 $listofmodules = explode(',', getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL'));
346
347 // Check read permission from module
348 $readok = 1;
349 $nbko = 0;
350 foreach ($featuresarray as $feature) { // first we check nb of test ko
351 $featureforlistofmodule = $feature;
352 if ($featureforlistofmodule == 'produit') {
353 $featureforlistofmodule = 'product';
354 }
355 if ($featureforlistofmodule == 'supplier_proposal') {
356 $featureforlistofmodule = 'supplierproposal';
357 }
358 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
359 $readok = 0;
360 $nbko++;
361 continue;
362 }
363
364 if ($feature == 'societe' && (empty($feature2) || !in_array('contact', $feature2))) {
365 if (!$user->hasRight('societe', 'lire') && !$user->hasRight('fournisseur', 'lire')) {
366 $readok = 0;
367 $nbko++;
368 }
369 } elseif (($feature == 'societe' && (!empty($feature2) && in_array('contact', $feature2))) || $feature == 'contact') {
370 if (!$user->hasRight('societe', 'contact', 'lire')) {
371 $readok = 0;
372 $nbko++;
373 }
374 } elseif ($feature == 'produit|service') {
375 if (!$user->hasRight('produit', 'lire') && !$user->hasRight('service', 'lire')) {
376 $readok = 0;
377 $nbko++;
378 }
379 } elseif ($feature == 'prelevement') {
380 if (!$user->hasRight('prelevement', 'bons', 'lire')) {
381 $readok = 0;
382 $nbko++;
383 }
384 } elseif ($feature == 'cheque') {
385 if (!$user->hasRight('banque', 'cheque')) {
386 $readok = 0;
387 $nbko++;
388 }
389 } elseif ($feature == 'projet') {
390 if (!$user->hasRight('projet', 'lire') && !$user->hasRight('projet', 'all', 'lire')) {
391 $readok = 0;
392 $nbko++;
393 }
394 } elseif ($feature == 'payment') {
395 if (!$user->hasRight('facture', 'lire')) {
396 $readok = 0;
397 $nbko++;
398 }
399 } elseif ($feature == 'payment_supplier') {
400 if (!$user->hasRight('fournisseur', 'facture', 'lire')) {
401 $readok = 0;
402 $nbko++;
403 }
404 } elseif ($feature == 'payment_sc') {
405 if (!$user->hasRight('tax', 'charges', 'lire')) {
406 $readok = 0;
407 $nbko++;
408 }
409 } elseif ($feature == 'payment_vat') {
410 if (!$user->hasRight('tax', 'charges', 'lire')) {
411 $readok = 0;
412 $nbko++;
413 }
414 } elseif ($feature == 'webhook') {
415 if (empty($user->admin)) {
416 $readok = 0;
417 $nbko++;
418 }
419 } elseif (!empty($feature2)) { // This is for permissions on 2 levels (module->object->read)
420 $tmpreadok = 1;
421 foreach ($feature2 as $subfeature) {
422 if ($subfeature == 'user' && $user->id == $objectid) {
423 continue; // A user can always read its own card
424 }
425 if ($subfeature == 'fiscalyear' && $user->hasRight('accounting', 'fiscalyear', 'write')) {
426 // only one right for fiscalyear
427 $tmpreadok = 1;
428 continue;
429 }
430 if (!empty($subfeature) && !$user->hasRight($feature, $subfeature, 'lire') && !$user->hasRight($feature, $subfeature, 'read')) {
431 $tmpreadok = 0;
432 } elseif (empty($subfeature) && !$user->hasRight($feature, 'lire') && !$user->hasRight($feature, 'read')) {
433 $tmpreadok = 0;
434 } else {
435 $tmpreadok = 1;
436 break;
437 } // Break is to bypass second test if the first is ok
438 }
439 if (!$tmpreadok) { // We found a test on feature that is ko
440 $readok = 0; // All tests are ko (we manage here the and, the or will be managed later using $nbko).
441 $nbko++;
442 }
443 } elseif (!empty($feature) && ($feature != 'user' && $feature != 'usergroup')) { // This is permissions on 1 level (module->read)
444 if (!$user->hasRight($feature, 'lire')
445 && !$user->hasRight($feature, 'read')
446 && !$user->hasRight($feature, 'run')) {
447 $readok = 0;
448 $nbko++;
449 }
450 }
451 }
452
453 // If a or and at least one ok
454 if (preg_match('/\|/', $features) && $nbko < count($featuresarray)) {
455 $readok = 1;
456 }
457
458 if (!$readok) {
459 if ($mode) {
460 return 0;
461 } else {
463 }
464 }
465 //print "Read access is ok";
466
467 // Check write permission from module (we need to know write permission to create but also to delete drafts record or to upload files)
468 $createok = 1;
469 $nbko = 0;
470 $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));
471 $wemustcheckpermissionfordeletedraft = ((GETPOST("action", "aZ09") == 'confirm_delete' && GETPOST("confirm", "aZ09") == 'yes') || GETPOST("action", "aZ09") == 'delete');
472
473 if ($wemustcheckpermissionforcreate || $wemustcheckpermissionfordeletedraft) {
474 foreach ($featuresarray as $feature) {
475 if ($feature == 'contact') {
476 if (!$user->hasRight('societe', 'contact', 'creer')) {
477 $createok = 0;
478 $nbko++;
479 }
480 } elseif ($feature == 'produit|service') {
481 if (!$user->hasRight('produit', 'creer') && !$user->hasRight('service', 'creer')) {
482 $createok = 0;
483 $nbko++;
484 }
485 } elseif ($feature == 'prelevement') {
486 if (!$user->hasRight('prelevement', 'bons', 'creer')) {
487 $createok = 0;
488 $nbko++;
489 }
490 } elseif ($feature == 'commande_fournisseur') {
491 if (!$user->hasRight('fournisseur', 'commande', 'creer') || !$user->hasRight('supplier_order', 'creer')) {
492 $createok = 0;
493 $nbko++;
494 }
495 } elseif ($feature == 'banque') {
496 if (!$user->hasRight('banque', 'modifier')) {
497 $createok = 0;
498 $nbko++;
499 }
500 } elseif ($feature == 'cheque') {
501 if (!$user->hasRight('banque', 'cheque')) {
502 $createok = 0;
503 $nbko++;
504 }
505 } elseif ($feature == 'import') {
506 if (!$user->hasRight('import', 'run')) {
507 $createok = 0;
508 $nbko++;
509 }
510 } elseif ($feature == 'ecm') {
511 if (!$user->hasRight('ecm', 'upload')) {
512 $createok = 0;
513 $nbko++;
514 }
515 } elseif ($feature == 'modulebuilder') {
516 if (!$user->hasRight('modulebuilder', 'run')) {
517 $createok = 0;
518 $nbko++;
519 }
520 } elseif ($feature == 'payment') {
521 if (!$user->hasRight('facture', 'paiement')) {
522 $createok = 0;
523 $nbko++;
524 }
525 } elseif ($feature == 'payment_supplier') { // Permission to write on a payment of an invoice is permission to edit an invoice.
526 if (!$user->hasRight('fournisseur', 'facture', 'creer')) {
527 $createok = 0;
528 $nbko++;
529 }
530 } elseif ($feature == 'webhook') {
531 if (empty($user->admin)) {
532 $createok = 0;
533 $nbko++;
534 }
535 } elseif (!empty($feature2)) { // This is for permissions on 2 levels (module->object->write)
536 foreach ($feature2 as $subfeature) {
537 if ($subfeature == 'user' && $user->id == $objectid && $user->hasRight('user', 'self', 'creer')) {
538 continue; // User can edit its own card
539 }
540 if ($subfeature == 'user' && $user->id == $objectid && $user->hasRight('user', 'self', 'password')) {
541 continue; // User can edit its own password
542 }
543 if ($subfeature == 'user' && $user->id != $objectid && $user->hasRight('user', 'user', 'password')) {
544 continue; // User can edit another user's password
545 }
546
547 if (!$user->hasRight($feature, $subfeature, 'creer')
548 && !$user->hasRight($feature, $subfeature, 'write')
549 && !$user->hasRight($feature, $subfeature, 'create')) {
550 $createok = 0;
551 $nbko++;
552 } else {
553 $createok = 1;
554 // Break to bypass second test if the first is ok
555 break;
556 }
557 }
558 } elseif (!empty($feature)) { // This is for permissions on 1 levels (module->write)
559 //print '<br>feature='.$feature.' creer='.$user->rights->$feature->creer.' write='.$user->rights->$feature->write; exit;
560 if (!$user->hasRight($feature, 'creer')
561 && !$user->hasRight($feature, 'write')
562 && !$user->hasRight($feature, 'create')) {
563 $createok = 0;
564 $nbko++;
565 }
566 }
567 }
568
569 // If a or and at least one ok
570 if (preg_match('/\|/', $features) && $nbko < count($featuresarray)) {
571 $createok = 1;
572 }
573
574 if ($wemustcheckpermissionforcreate && !$createok) {
575 if ($mode) {
576 return 0;
577 } else {
579 }
580 }
581 //print "Write access is ok";
582 }
583
584 // Check create user permission
585 $createuserok = 1;
586 if (GETPOST('action', 'aZ09') == 'confirm_create_user' && GETPOST("confirm", 'aZ09') == 'yes') {
587 if (!$user->hasRight('user', 'user', 'creer')) {
588 $createuserok = 0;
589 }
590
591 if (!$createuserok) {
592 if ($mode) {
593 return 0;
594 } else {
596 }
597 }
598 //print "Create user access is ok";
599 }
600
601 // Check delete permission from module
602 $deleteok = 1;
603 $nbko = 0;
604 if ((GETPOST("action", "aZ09") == 'confirm_delete' && GETPOST("confirm", "aZ09") == 'yes') || GETPOST("action", "aZ09") == 'delete') {
605 foreach ($featuresarray as $feature) {
606 if ($feature == 'bookmark') {
607 if (!$user->hasRight('bookmark', 'supprimer')) {
608 if ($user->id != $object->fk_user || !$user->hasRight('bookmark', 'creer')) {
609 $deleteok = 0;
610 }
611 }
612 } elseif ($feature == 'contact') {
613 if (!$user->hasRight('societe', 'contact', 'supprimer')) {
614 $deleteok = 0;
615 }
616 } elseif ($feature == 'produit|service') {
617 if (!$user->hasRight('produit', 'supprimer') && !$user->hasRight('service', 'supprimer')) {
618 $deleteok = 0;
619 }
620 } elseif ($feature == 'commande_fournisseur') {
621 if (!$user->hasRight('fournisseur', 'commande', 'supprimer')) {
622 $deleteok = 0;
623 }
624 } elseif ($feature == 'payment_supplier') { // Permission to delete a payment of an invoice is permission to edit an invoice.
625 if (!$user->hasRight('fournisseur', 'facture', 'creer')) {
626 $deleteok = 0;
627 }
628 } elseif ($feature == 'payment') {
629 if (!$user->hasRight('facture', 'paiement')) {
630 $deleteok = 0;
631 }
632 } elseif ($feature == 'payment_sc') {
633 if (!$user->hasRight('tax', 'charges', 'creer')) {
634 $deleteok = 0;
635 }
636 } elseif ($feature == 'banque') {
637 if (!$user->hasRight('banque', 'modifier')) {
638 $deleteok = 0;
639 }
640 } elseif ($feature == 'cheque') {
641 if (!$user->hasRight('banque', 'cheque')) {
642 $deleteok = 0;
643 }
644 } elseif ($feature == 'ecm') {
645 if (!$user->hasRight('ecm', 'upload')) {
646 $deleteok = 0;
647 }
648 } elseif ($feature == 'ftp') {
649 if (!$user->hasRight('ftp', 'write')) {
650 $deleteok = 0;
651 }
652 } elseif ($feature == 'salaries') {
653 if (!$user->hasRight('salaries', 'delete')) {
654 $deleteok = 0;
655 }
656 } elseif ($feature == 'adherent') {
657 if (!$user->hasRight('adherent', 'supprimer')) {
658 $deleteok = 0;
659 }
660 } elseif ($feature == 'paymentbybanktransfer') {
661 if (!$user->hasRight('paymentbybanktransfer', 'create')) { // There is no delete permission
662 $deleteok = 0;
663 }
664 } elseif ($feature == 'prelevement') {
665 if (!$user->hasRight('prelevement', 'bons', 'creer')) { // There is no delete permission
666 $deleteok = 0;
667 }
668 } elseif (!empty($feature2)) { // This is for permissions on 2 levels
669 foreach ($feature2 as $subfeature) {
670 if (!$user->hasRight($feature, $subfeature, 'supprimer') && !$user->hasRight($feature, $subfeature, 'delete')) {
671 $deleteok = 0;
672 } else {
673 $deleteok = 1;
674 break;
675 } // For bypass the second test if the first is ok
676 }
677 } elseif (!empty($feature)) { // This is used for permissions on 1 level
678 //print '<br>feature='.$feature.' creer='.$user->rights->$feature->supprimer.' write='.$user->rights->$feature->delete;
679 if (!$user->hasRight($feature, 'supprimer')
680 && !$user->hasRight($feature, 'delete')
681 && !$user->hasRight($feature, 'run')) {
682 $deleteok = 0;
683 }
684 }
685 }
686
687 // If a or and at least one ok
688 if (preg_match('/\|/', $features) && $nbko < count($featuresarray)) {
689 $deleteok = 1;
690 }
691
692 if (!$deleteok && !($isdraft && $createok)) {
693 if ($mode) {
694 return 0;
695 } else {
697 }
698 }
699 //print "Delete access is ok";
700 }
701
702 // If we have a particular object to check permissions on, we check if $user has permission
703 // for this given object (link to company, is contact for project, ...)
704 if (!empty($objectid) && $objectid > 0) {
705 $ok = checkUserAccessToObject($user, $featuresarray, $object, $tableandshare, $feature2, $dbt_keyfield, $dbt_select, $parentfortableentity);
706 $params = array('objectid' => $objectid, 'features' => implode(',', $featuresarray), 'features2' => $feature2);
707 //print 'checkUserAccessToObject ok='.$ok;
708 if ($mode) {
709 return $ok ? 1 : 0;
710 } else {
711 if ($ok) {
712 return 1;
713 } else {
714 accessforbidden('', 1, 1, 0, $params);
715 }
716 }
717 }
718
719 return 1;
720}
721
737function checkUserAccessToObject($user, array $featuresarray, $object = 0, $tableandshare = '', $feature2 = '', $dbt_keyfield = '', $dbt_select = 'rowid', $parenttableforentity = '')
738{
739 global $db, $conf;
740
741 if (is_object($object)) {
742 $objectid = $object->id;
743 } else {
744 $objectid = $object; // $objectid can be X or 'X,Y,Z'
745 }
746 $objectid = preg_replace('/[^0-9\.\,]/', '', (string) $objectid); // For the case value is coming from a non sanitized user input
747
748 //dol_syslog("functions.lib:restrictedArea $feature, $object, $dbtablename, $feature2, $dbt_socfield, $dbt_select, $isdraft");
749 //print "user_id=".$user->id.", features=".join(',', $featuresarray).", object=".$object;
750 //print ", tableandshare=".$tableandshare.", dbt_socfield=".$dbt_keyfield.", dbt_select=".$dbt_select."<br>";
751
752 // More parameters
753 $params = explode('&', $tableandshare);
754 $dbtablename = (!empty($params[0]) ? $params[0] : '');
755 $sharedelement = (!empty($params[1]) ? $params[1] : $dbtablename);
756
757 foreach ($featuresarray as $feature) {
758 $sql = '';
759
760 //var_dump($feature);exit;
761
762 // For backward compatibility
763 if ($feature == 'societe' && !empty($feature2) && is_array($feature2) && in_array('contact', $feature2)) {
764 $feature = 'contact';
765 $feature2 = '';
766 }
767 if ($feature == 'member') {
768 $feature = 'adherent';
769 }
770 if ($feature == 'category') {
771 $feature = 'categorie';
772 }
773 if ($feature == 'project') {
774 $feature = 'projet';
775 }
776 if ($feature == 'projet' && !empty($feature2) && is_array($feature2) && !empty(array_intersect(array('project_task', 'projet_task'), $feature2))) {
777 $feature = 'project_task';
778 }
779 if ($feature == 'task' || $feature == 'projet_task') {
780 $feature = 'project_task';
781 $dbtablename = 'projet_task';
782 }
783 if ($feature == 'eventorganization') {
784 $feature = 'agenda';
785 $dbtablename = 'actioncomm';
786 }
787 if ($feature == 'payment_sc' && empty($parenttableforentity)) {
788 // If we check perm on payment page but $parenttableforentity not defined, we force value on parent table
789 $parenttableforentity = '';
790 $dbtablename = "chargesociales";
791 $feature = "chargesociales";
792 $objectid = (string) $object->fk_charge;
793 }
794
795 $checkonentityready = 0;
796
797 // Array to define rules of checks to do
798 $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', 'stockmovement'); // Test on entity only (Objects with no link to company)
799 $checksoc = array('societe'); // Test for object Societe
800 $checkparentsoc = array('agenda', 'contact', 'contrat', 'ticket'); // Test on entity + link to third party on field $dbt_keyfield. Allowed if link is empty (Ex: contacts...).
801 $checkproject = array('projet', 'project'); // Test for project object
802 $checktask = array('projet_task', 'project_task'); // Test for task object
803 $checkhierarchy = array('expensereport', 'holiday', 'hrm'); // check permission among the hierarchy of user
804 $checkuser = array('bookmark'); // check permission among the fk_user (must be myself or null)
805 $nocheck = array('barcode', 'webhook'); // No test
806
807 //$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...).
808
809 // If dbtablename not defined, we use same name for table than module name
810 if (empty($dbtablename)) {
811 $dbtablename = $feature;
812 $sharedelement = (!empty($params[1]) ? $params[1] : $dbtablename); // We change dbtablename, so we set sharedelement too.
813 }
814
815 // $objectid was already sanitized at begin of this method (can be an int or a list of int separated by comma).
816 // To avoid an access forbidden with a numeric ref
817 if ($dbt_select != 'rowid' && $dbt_select != 'id') {
818 $objectid = "'".$objectid."'";
819 }
820
821 // Check permission for objectid on entity only
822 if (in_array($feature, $check) && !empty($objectid)) { // For $objectid = 0, no check
823 $sql = "SELECT COUNT(dbt.".$db->sanitize($dbt_select).") as nb";
824 $sql .= " FROM ".MAIN_DB_PREFIX.$db->sanitize($dbtablename)." as dbt";
825 if (($feature == 'user' || $feature == 'usergroup') && isModEnabled('multicompany')) { // Special for multicompany
826 if (getDolGlobalString('MULTICOMPANY_TRANSVERSE_MODE')) {
827 if ($conf->entity == 1 && $user->admin && !$user->entity) {
828 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
829 $sql .= " AND dbt.entity IS NOT NULL";
830 } else {
831 $sql .= ",".MAIN_DB_PREFIX."usergroup_user as ug";
832 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
833 $sql .= " AND ((ug.fk_user = dbt.rowid";
834 $sql .= " AND ug.entity IN (".getEntity('usergroup')."))";
835 $sql .= " OR dbt.entity = 0)"; // Show always superadmin
836 }
837 } else {
838 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
839 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
840 }
841 } else {
842 $reg = array();
843 if ($parenttableforentity && preg_match('/(.*)@(.*)/', $parenttableforentity, $reg)) {
844 $sql .= ", ".MAIN_DB_PREFIX.$db->sanitize($reg[2])." as dbtp";
845 $sql .= " WHERE dbt.".$db->sanitize($reg[1])." = dbtp.rowid AND dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
846 $sql .= " AND dbtp.entity IN (".getEntity($sharedelement, 1).")";
847 } else {
848 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
849 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
850 }
851 }
852 $checkonentityready = 1;
853 }
854
855 if (in_array($feature, $checksoc) && !empty($objectid)) { // We check feature = checksoc. For $objectid = 0, no check
856 // If external user: Check permission for external users
857 if ($user->socid > 0) {
858 if ((string) $user->socid != $objectid) {
859 return false;
860 }
861 } elseif (isModEnabled('societe') && !$user->hasRight('societe', 'lire') && !$user->hasRight('societe', 'client', 'voir')) {
862 dol_syslog("security.lib.php::checkUserAccessToObject Deny access due: (isModEnabled('societe') && !user->hasRight('societe', 'lire') && !user->hasRight('societe', 'client', 'voir'))", LOG_DEBUG);
863 return false;
864 } elseif (isModEnabled("societe") && ($user->hasRight('societe', 'lire') && !$user->hasRight('societe', 'client', 'voir'))) {
865 // If internal user: Check permission for internal users that are restricted on their objects
866 $sql = "SELECT COUNT(sc.fk_soc) as nb";
867 $sql .= " FROM (".MAIN_DB_PREFIX."societe_commerciaux as sc";
868 $sql .= ", ".MAIN_DB_PREFIX."societe as s)";
869 $sql .= " WHERE sc.fk_soc IN (".$db->sanitize($objectid, 1).")";
870 $sql .= " AND (sc.fk_user = ".((int) $user->id);
871 if (getDolGlobalInt('MAIN_SEE_SUBORDINATES')) {
872 $userschilds = $user->getAllChildIds();
873 if (!empty($userschilds)) $sql .= " OR sc.fk_user IN (".$db->sanitize(implode(',', $userschilds)).")";
874 }
875 $sql .= ")";
876 $sql .= " AND sc.fk_soc = s.rowid";
877 $sql .= " AND s.entity IN (".getEntity($sharedelement, 1).")";
878 } elseif (isModEnabled('multicompany')) {
879 // If multicompany and internal users with all permissions, check user is in correct entity
880 $sql = "SELECT COUNT(s.rowid) as nb";
881 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
882 $sql .= " WHERE s.rowid IN (".$db->sanitize($objectid, 1).")";
883 $sql .= " AND s.entity IN (".getEntity($sharedelement, 1).")";
884 }
885
886 $checkonentityready = 1;
887 }
888 if (in_array($feature, $checkparentsoc) && !empty($objectid)) { // Test on entity + link to thirdparty. Allowed if link is empty (Ex: contacts...).
889 if ($user->socid > 0) {
890 // If external user: Check permission for external users (limtited to their company, even object with company link that is null must remain not visible)
891 $sql = "SELECT COUNT(dbt.".$db->sanitize($dbt_select).") as nb";
892 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
893 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")"; // Link to third party
894 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
895 $sql .= " AND dbt.fk_soc = ".((int) $user->socid); // Third party must be user company
896 } elseif (isModEnabled("societe") && ($user->hasRight('societe', 'lire') && !$user->hasRight('societe', 'client', 'voir'))) {
897 // If internal user: Check permission for internal users that are restricted on their objects
898 $sql = "SELECT COUNT(dbt.".$db->sanitize($dbt_select).") as nb";
899 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
900 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON dbt.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
901 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
902 $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
903 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
904 } elseif (isModEnabled('multicompany')) {
905 // If multicompany and internal users with all permissions, check user is in correct entity
906 $sql = "SELECT COUNT(dbt.".$db->sanitize($dbt_select).") as nb";
907 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
908 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
909 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
910 }
911
912 $checkonentityready = 1;
913 }
914 if (in_array($feature, $checkproject) && !empty($objectid)) {
915 if (isModEnabled('project') && !$user->hasRight('projet', 'all', 'lire')) {
916 $projectid = $objectid; // Note that if $objectid is a string list of id; the test later will return false
917
918 include_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
919 $projectstatic = new Project($db);
920 $tmps = $projectstatic->getProjectsAuthorizedForUser($user, 0, 1, 0);
921
922 $tmparray = explode(',', $tmps);
923 if (!in_array($projectid, $tmparray)) {
924 return false;
925 }
926 } else {
927 $sql = "SELECT COUNT(dbt.".$db->sanitize($dbt_select).") as nb";
928 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
929 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
930 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
931 }
932 $checkonentityready = 1;
933 }
934 if (in_array($feature, $checktask) && !empty($objectid)) {
935 if (isModEnabled('project') && !$user->hasRight('projet', 'all', 'lire')) {
936 if (preg_match('/,/', $objectid)) { // if this is a list of id
937 return false;
938 }
939 $task = new Task($db);
940 $task->fetch((int) $objectid);
941 $projectid = $task->fk_project;
942
943 include_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
944 $projectstatic = new Project($db);
945 $tmps = $projectstatic->getProjectsAuthorizedForUser($user, 0, 1, 0);
946
947 $tmparray = explode(',', $tmps);
948 if (!in_array($projectid, $tmparray)) {
949 return false;
950 }
951 } else {
952 $sharedelement = 'project'; // for multicompany compatibility
953 $sql = "SELECT COUNT(dbt.".$db->sanitize($dbt_select).") as nb";
954 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
955 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
956 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
957 }
958
959 $checkonentityready = 1;
960 }
961 //var_dump($sql);
962
963 if (!$checkonentityready && !in_array($feature, $nocheck) && !empty($objectid)) { // By default (case of $checkdefault), we check on object entity + link to third party on field $dbt_keyfield
964 // If external user: Check permission for external users
965 if ($user->socid > 0) {
966 if (empty($dbt_keyfield)) {
967 dol_print_error(null, 'Param dbt_keyfield is required but not defined');
968 }
969 $sql = "SELECT COUNT(dbt.".$db->sanitize($dbt_keyfield).") as nb";
970 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
971 $sql .= " WHERE dbt.rowid IN (".$db->sanitize($objectid, 1).")";
972 $sql .= " AND dbt.".$db->sanitize($dbt_keyfield)." = ".((int) $user->socid);
973 } elseif (isModEnabled("societe") && !$user->hasRight('societe', 'client', 'voir')) {
974 // If internal user without permission to see all thirdparties: Check permission for internal users that are restricted on their objects
975 if (empty($dbt_keyfield)) {
976 dol_print_error(null, 'Param dbt_keyfield is required but not defined');
977 }
978 if ($feature != 'ticket') {
979 $sql = "SELECT COUNT(sc.fk_soc) as nb";
980 $sql .= " FROM ".MAIN_DB_PREFIX.$db->sanitize($dbtablename)." as dbt";
981 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
982 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
983 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
984 $sql .= " AND sc.fk_soc = dbt.".$db->sanitize($dbt_keyfield);
985 $sql .= " AND (sc.fk_user = ".((int) $user->id);
986 if (getDolGlobalInt('MAIN_SEE_SUBORDINATES')) {
987 $userschilds = $user->getAllChildIds();
988 if (!empty($userschilds)) $sql .= " OR sc.fk_user IN (".$db->sanitize(implode(',', $userschilds)).")";
989 }
990 $sql .= ')';
991 } else {
992 // On ticket, the thirdparty is not mandatory, so we need a special test to accept record with no thirdparties.
993 $sql = "SELECT COUNT(dbt.".$db->sanitize($dbt_select).") as nb";
994 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
995 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = dbt.".$db->sanitize($dbt_keyfield)." AND sc.fk_user = ".((int) $user->id);
996 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
997 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
998 $sql .= " AND (sc.fk_user = ".((int) $user->id)." OR dbt.".$dbt_keyfield." IS NULL OR dbt.".$dbt_keyfield." = 0)";
999 }
1000 } elseif (isModEnabled('multicompany') && (!empty($object->ismultientitymanaged) || !isset($object->ismultientitymanaged))) {
1001 // If multicompany, and user is an internal user with all permissions, check that object is in correct entity
1002 $sql = "SELECT COUNT(dbt.".$db->sanitize($dbt_select).") as nb";
1003 $sql .= " FROM ".MAIN_DB_PREFIX.$db->sanitize($dbtablename)." as dbt";
1004 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
1005 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
1006 }
1007 }
1008
1009 // For events, check on users assigned to event
1010 if ($feature === 'agenda' && !empty($objectid)) {
1011 // Also check owner or attendee for users without allactions->read
1012 if (!$user->hasRight('agenda', 'allactions', 'read')) {
1013 if (preg_match('/,/', $objectid)) { // if this is a list of id
1014 return false;
1015 }
1016
1017 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
1018 $action = new ActionComm($db);
1019 $action->fetch((int) $objectid);
1020 if ($action->authorid != $user->id && $action->userownerid != $user->id && !(array_key_exists($user->id, $action->userassigned))) {
1021 return false;
1022 }
1023 }
1024 }
1025
1026 // For some object, we also have to check it is in the user hierarchy
1027 // Param $object must be the full object and not a simple id to have this test possible.
1028 if (in_array($feature, $checkhierarchy) && is_object($object) && !empty($objectid)) {
1029 $childids = $user->getAllChildIds(1);
1030 $useridtocheck = 0;
1031 if ($feature == 'holiday') {
1032 $useridtocheck = $object->fk_user;
1033 if (!$user->hasRight('holiday', 'readall') && !in_array($useridtocheck, $childids) && !in_array($object->fk_validator, $childids)) {
1034 return false;
1035 }
1036 }
1037 if ($feature == 'expensereport') {
1038 $useridtocheck = $object->fk_user_author;
1039 if (!$user->hasRight('expensereport', 'readall')) {
1040 if (!in_array($useridtocheck, $childids)) {
1041 return false;
1042 }
1043 }
1044 }
1045 if ($feature == 'hrm' && in_array('evaluation', $feature2)) {
1046 $useridtocheck = $object->fk_user;
1047
1048 if ($user->hasRight('hrm', 'evaluation', 'readall')) {
1049 // the user can view evaluations for anyone
1050 return true;
1051 }
1052 if (!$user->hasRight('hrm', 'evaluation', 'read')) {
1053 // the user can't view any evaluations
1054 return false;
1055 }
1056 // the user can only see their own evaluations or their subordinates'
1057 return in_array($useridtocheck, $childids);
1058 }
1059 }
1060
1061 // For some object, we also have to check it is public or owned by user
1062 // Param $object must be the full object and not a simple id to have this test possible.
1063 if (in_array($feature, $checkuser) && is_object($object) && !empty($objectid)) {
1064 $useridtocheck = $object->fk_user;
1065 if (!empty($useridtocheck) && $useridtocheck > 0 && $useridtocheck != $user->id && empty($user->admin)) {
1066 return false;
1067 }
1068 }
1069
1070 if ($sql) {
1071 $resql = $db->query($sql);
1072 if ($resql) {
1073 $obj = $db->fetch_object($resql);
1074 if (!$obj || $obj->nb < count(explode(',', $objectid))) { // error if we found 0 or less record than the nb of ids provided
1075 return false;
1076 }
1077 } else {
1078 dol_syslog("Bad forged sql in security.lib.php::checkUserAccessToObject", LOG_WARNING);
1079 return false;
1080 }
1081 }
1082 }
1083
1084 dol_syslog("security.lib.php::checkUserAccessToObject::return True", LOG_DEBUG);
1085 return true;
1086}
1087
1088
1100function httponly_accessforbidden($message = '1', $http_response_code = 403, $stringalreadysanitized = 0)
1101{
1102 top_httphead();
1103 http_response_code($http_response_code);
1104
1105 if ($stringalreadysanitized) {
1106 print $message;
1107 } else {
1108 print htmlentities($message);
1109 }
1110
1111 exit(1);
1112}
1113
1127function accessforbidden($message = '', $printheader = 1, $printfooter = 1, $showonlymessage = 0, $params = null)
1128{
1129 global $conf, $db, $user, $langs, $hookmanager;
1130 global $action, $object;
1131
1132 if (!is_object($langs)) {
1133 include_once DOL_DOCUMENT_ROOT.'/core/class/translate.class.php';
1134 $langs = new Translate('', $conf);
1135 $langs->setDefaultLang();
1136 }
1137
1138 $langs->loadLangs(array("main", "errors"));
1139
1140 if ($printheader && !defined('NOHEADERNOFOOTER')) {
1141 if (function_exists("llxHeader")) {
1142 llxHeader('');
1143 } elseif (function_exists("llxHeaderVierge")) {
1144 llxHeaderVierge('');
1145 }
1146 print '<div style="padding: 20px">';
1147 }
1148 print '<div class="error">';
1149 if (empty($message)) {
1150 print $langs->trans("ErrorForbidden");
1151 } else {
1152 print $langs->trans($message);
1153 }
1154 print '</div>';
1155 print '<br>';
1156 if (empty($showonlymessage)) {
1157 if (empty($hookmanager)) {
1158 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
1159 $hookmanager = new HookManager($db);
1160 // Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
1161 $hookmanager->initHooks(array('main'));
1162 }
1163
1164 $parameters = array('message' => $message, 'params' => $params);
1165 $reshook = $hookmanager->executeHooks('getAccessForbiddenMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
1166 print $hookmanager->resPrint;
1167 if (empty($reshook)) {
1168 $langs->loadLangs(array("errors"));
1169 if ($user->login) {
1170 print $langs->trans("CurrentLogin").': <span class="error">'.$user->login.'</span><br>';
1171 print $langs->trans("ErrorForbidden2", $langs->transnoentitiesnoconv("Home"), $langs->transnoentitiesnoconv("Users"));
1172 print $langs->trans("ErrorForbidden4");
1173 } else {
1174 print $langs->trans("ErrorForbidden3");
1175 }
1176 }
1177 }
1178 if ($printfooter && !defined('NOHEADERNOFOOTER') && function_exists("llxFooter")) {
1179 print '</div>';
1180 llxFooter();
1181 }
1182
1183 // End PHP
1184 exit(0);
1185}
1186
1187
1195{
1196 $max = getDolGlobalString('MAIN_UPLOAD_DOC'); // In Kb
1197
1198 $maxphp = @ini_get('upload_max_filesize'); // In unknown
1199 if (preg_match('/k$/i', $maxphp)) {
1200 $maxphp = preg_replace('/k$/i', '', $maxphp);
1201 $maxphp = (int) ((float) $maxphp * 1);
1202 }
1203 if (preg_match('/m$/i', $maxphp)) {
1204 $maxphp = preg_replace('/m$/i', '', $maxphp);
1205 $maxphp = (int) ((float) $maxphp * 1024);
1206 }
1207 if (preg_match('/g$/i', $maxphp)) {
1208 $maxphp = preg_replace('/g$/i', '', $maxphp);
1209 $maxphp = (int) ((float) $maxphp * 1024 * 1024);
1210 }
1211 if (preg_match('/t$/i', $maxphp)) {
1212 $maxphp = preg_replace('/t$/i', '', $maxphp);
1213 $maxphp = (int) ((float) $maxphp * 1024 * 1024 * 1024);
1214 }
1215 $maxphp2 = @ini_get('post_max_size'); // In unknown
1216 if (preg_match('/k$/i', $maxphp2)) {
1217 $maxphp2 = preg_replace('/k$/i', '', $maxphp2);
1218 $maxphp2 = (int) ((float) $maxphp2) * 1;
1219 }
1220 if (preg_match('/m$/i', $maxphp2)) {
1221 $maxphp2 = preg_replace('/m$/i', '', $maxphp2);
1222 $maxphp2 = (int) ((float) $maxphp2 * 1024);
1223 }
1224 if (preg_match('/g$/i', $maxphp2)) {
1225 $maxphp2 = preg_replace('/g$/i', '', $maxphp2);
1226 $maxphp2 = (int) ((float) $maxphp2 * 1024 * 1024);
1227 }
1228 if (preg_match('/t$/i', $maxphp2)) {
1229 $maxphp2 = preg_replace('/t$/i', '', $maxphp2);
1230 $maxphp2 = (int) ((float) $maxphp2 * 1024 * 1024 * 1024);
1231 }
1232 // Now $max and $maxphp and $maxphp2 are in Kb
1233 $maxmin = $max;
1234 $maxphptoshow = $maxphptoshowparam = '';
1235 if ($maxphp > 0) {
1236 $maxmin = min($maxmin, $maxphp);
1237 $maxphptoshow = $maxphp;
1238 $maxphptoshowparam = 'upload_max_filesize';
1239 }
1240 if ($maxphp2 > 0) {
1241 $maxmin = min($maxmin, $maxphp2);
1242 if ($maxphp2 < $maxphp) {
1243 $maxphptoshow = $maxphp2;
1244 $maxphptoshowparam = 'post_max_size';
1245 }
1246 }
1247 //var_dump($maxphp.'-'.$maxphp2);
1248 //var_dump($maxmin);
1249
1250 return array('max' => $max, 'maxmin' => $maxmin, 'maxphptoshow' => $maxphptoshow, 'maxphptoshowparam' => $maxphptoshowparam);
1251}
1252
1260function checkIPInCidr($ip, $cidr)
1261{
1262 list($network, $prefix) = explode('/', $cidr, 2);
1263
1264 // Convert IPs to binary format
1265 $ip_bin = @inet_pton($ip);
1266 $net_bin = @inet_pton($network);
1267 if ($ip_bin === false || $net_bin === false) {
1268 return -1;
1269 }
1270
1271 // Require same address IPvX family
1272 if (strlen($ip_bin) !== strlen($net_bin)) {
1273 return -1;
1274 }
1275
1276 // Comparison boundaries
1277 $total_bits = strlen($ip_bin) * 8;
1278 $prefix = max(0, min((int) $prefix, $total_bits));
1279 $full_bytes = intdiv($prefix, 8);
1280 $rem_bits = $prefix % 8;
1281
1282 // Compare full bytes and partial bytes
1283 if ($full_bytes > 0) {
1284 if (substr($ip_bin, 0, $full_bytes) !== substr($net_bin, 0, $full_bytes)) {
1285 return 0;
1286 }
1287 }
1288 if ($rem_bits > 0) {
1289 $mask = (0xFF << (8 - $rem_bits)) & 0xFF;
1290 $ip_byte = ord($ip_bin[$full_bytes]);
1291 $net_byte = ord($net_bin[$full_bytes]);
1292 if (($ip_byte & $mask) !== ($net_byte & $mask)) {
1293 return 0;
1294 }
1295 }
1296 return 1;
1297}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
if(!defined( 'NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined( 'NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) if(!defined( 'NOLOGIN')) if(!defined('NOCSRFCHECK')) if(!defined( 'NOIPCHECK')) llxHeaderVierge($title, $head="", $disablejs=0, $disablehead=0, $arrayofjs=[], $arrayofcss=[], $ws='')
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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
dolGetRandomBytes($length)
Return a string of random bytes (hexa string) with length = $length for cryptographic purposes.
httponly_accessforbidden($message='1', $http_response_code=403, $stringalreadysanitized=0)
Show a message to say access is forbidden and stop program.
dol_encode($chain, $key='1')
Encode a string with base 64 algorithm + specific delta change.
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.
checkIPInCidr($ip, $cidr)
Check if IP address is in CIDR range.
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.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.