dolibarr 25.0.0-alpha
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
264 // if commonObjectLine : Using many2one related commonObject
265 // @see commonObjectLine::parentElement
266 if (in_array($features, ['commandedet', 'propaldet', 'facturedet', 'supplier_proposaldet', 'evaluationdet', 'skilldet', 'deliverydet', 'contratdet'])) {
267 $features = substr($features, 0, -3);
268 } elseif (in_array($features, ['stocktransferline', 'inventoryline', 'bomline', 'expensereport_det', 'facture_fourn_det'])) {
269 $features = substr($features, 0, -4);
270 } elseif ($features == 'commandefournisseurdispatch') {
271 $features = 'commandefournisseur';
272 } elseif ($features == 'invoice_supplier_det_rec') {
273 $features = 'invoice_supplier_rec';
274 }
275 if ($features == 'evaluation') {
276 $features = 'hrm';
277 $feature2 = 'evaluation';
278 }
279
280 // When the object is a task (element='project_task') and $feature2 is empty,
281 // $checkUserAccessToObject() falls into the $checkproject path and uses the task ID
282 // as project ID, which always fails. Setting $feature2='project_task' triggers the
283 // normalization at line 974 that redirects to the $checktask path, which correctly
284 // resolves $task->fk_project before calling getProjectsAuthorizedForUser().
285 if (is_object($object) && in_array($object->element, array('project_task', 'task'))
286 && (empty($features) || in_array($features, array('projet', 'project')))
287 && empty($feature2)) {
288 $features = 'projet';
289 $feature2 = 'project_task';
290 if (empty($tableandshare)) {
291 $tableandshare = 'projet_task';
292 }
293 }
294
295 // print $features.' - '.$tableandshare.' - '.$feature2.' - '.$dbt_select."\n";
296
297 // Get more permissions checks from hooks
298 $parameters = array(
299 'features' => $features,
300 'feature2' => $feature2,
301 'originalfeatures' => $originalfeatures,
302 'tableandshare' => $tableandshare,
303 'object' => $object,
304 'objectid' => $objectid,
305 'dbt_keyfield' => $dbt_keyfield,
306 'dbt_select' => $dbt_select,
307 'idtype' => $dbt_select,
308 'isdraft' => $isdraft,
309 'mode' => $mode,
310 );
311 if (!empty($hookmanager)) {
312 $reshook = $hookmanager->executeHooks('restrictedArea', $parameters);
313
314 if (isset($hookmanager->resArray['result'])) {
315 if ($hookmanager->resArray['result'] == 0) {
316 if ($mode) {
317 return 0;
318 } else {
319 accessforbidden(); // Module returns 0, so access forbidden
320 }
321 }
322 }
323 if ($reshook > 0) { // No other test done.
324 return 1;
325 }
326 }
327
328 // Features/modules to check (to support the & and | operator)
329 $featuresarray = array($features);
330 if (preg_match('/&/', $features)) {
331 $featuresarray = explode("&", $features);
332 } elseif (preg_match('/\|/', $features)) {
333 $featuresarray = explode("|", $features);
334 }
335
336 // More subfeatures to check
337 if (!empty($feature2)) {
338 $feature2 = explode("|", $feature2);
339 }
340
341 $listofmodules = explode(',', getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL'));
342
343 // Check read permission from module
344 $readok = 1;
345 $nbko = 0;
346 foreach ($featuresarray as $feature) { // first we check nb of test ko
347 $featureforlistofmodule = $feature;
348 if ($featureforlistofmodule == 'produit') {
349 $featureforlistofmodule = 'product';
350 }
351 if ($featureforlistofmodule == 'supplier_proposal') {
352 $featureforlistofmodule = 'supplierproposal';
353 }
354 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
355 $readok = 0;
356 $nbko++;
357 continue;
358 }
359
360 if ($feature == 'societe' && (empty($feature2) || !in_array('contact', $feature2))) {
361 if (!$user->hasRight('societe', 'lire') && !$user->hasRight('fournisseur', 'lire')) {
362 $readok = 0;
363 $nbko++;
364 }
365 } elseif (($feature == 'societe' && (!empty($feature2) && in_array('contact', $feature2))) || $feature == 'contact') {
366 if (!$user->hasRight('societe', 'contact', 'lire')) {
367 $readok = 0;
368 $nbko++;
369 }
370 } elseif ($feature == 'produit|service') {
371 if (!$user->hasRight('produit', 'lire') && !$user->hasRight('service', 'lire')) {
372 $readok = 0;
373 $nbko++;
374 }
375 } elseif ($feature == 'prelevement') {
376 if (!$user->hasRight('prelevement', 'bons', 'lire')) {
377 $readok = 0;
378 $nbko++;
379 }
380 } elseif ($feature == 'cheque') {
381 if (!$user->hasRight('banque', 'cheque')) {
382 $readok = 0;
383 $nbko++;
384 }
385 } elseif ($feature == 'projet') {
386 if (!$user->hasRight('projet', 'lire') && !$user->hasRight('projet', 'all', 'lire')) {
387 $readok = 0;
388 $nbko++;
389 }
390 } elseif ($feature == 'payment') {
391 if (!$user->hasRight('facture', 'lire')) {
392 $readok = 0;
393 $nbko++;
394 }
395 } elseif ($feature == 'payment_supplier') {
396 if (!$user->hasRight('fournisseur', 'facture', 'lire')) {
397 $readok = 0;
398 $nbko++;
399 }
400 } elseif ($feature == 'payment_sc') {
401 if (!$user->hasRight('tax', 'charges', 'lire')) {
402 $readok = 0;
403 $nbko++;
404 }
405 } elseif ($feature == 'webhook') {
406 if (empty($user->admin)) {
407 $readok = 0;
408 $nbko++;
409 }
410 } elseif (!empty($feature2)) { // This is for permissions on 2 levels (module->object->read)
411 $tmpreadok = 1;
412 foreach ($feature2 as $subfeature) {
413 if ($subfeature == 'user' && $user->id == $objectid) {
414 continue; // A user can always read its own card
415 }
416 if ($subfeature == 'fiscalyear' && $user->hasRight('accounting', 'fiscalyear', 'write')) {
417 // only one right for fiscalyear
418 $tmpreadok = 1;
419 continue;
420 }
421 if (!empty($subfeature) && !$user->hasRight($feature, $subfeature, 'lire') && !$user->hasRight($feature, $subfeature, 'read')) {
422 $tmpreadok = 0;
423 } elseif (empty($subfeature) && !$user->hasRight($feature, 'lire') && !$user->hasRight($feature, 'read')) {
424 $tmpreadok = 0;
425 } else {
426 $tmpreadok = 1;
427 break;
428 } // Break is to bypass second test if the first is ok
429 }
430 if (!$tmpreadok) { // We found a test on feature that is ko
431 $readok = 0; // All tests are ko (we manage here the and, the or will be managed later using $nbko).
432 $nbko++;
433 }
434 } elseif (!empty($feature) && ($feature != 'user' && $feature != 'usergroup')) { // This is permissions on 1 level (module->read)
435 if (!$user->hasRight($feature, 'lire')
436 && !$user->hasRight($feature, 'read')
437 && !$user->hasRight($feature, 'run')) {
438 $readok = 0;
439 $nbko++;
440 }
441 }
442 }
443
444 // If a or and at least one ok
445 if (preg_match('/\|/', $features) && $nbko < count($featuresarray)) {
446 $readok = 1;
447 }
448
449 if (!$readok) {
450 if ($mode) {
451 return 0;
452 } else {
454 }
455 }
456 //print "Read access is ok";
457
458 // Check write permission from module (we need to know write permission to create but also to delete drafts record or to upload files)
459 $createok = 1;
460 $nbko = 0;
461 $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));
462 $wemustcheckpermissionfordeletedraft = ((GETPOST("action", "aZ09") == 'confirm_delete' && GETPOST("confirm", "aZ09") == 'yes') || GETPOST("action", "aZ09") == 'delete');
463
464 if ($wemustcheckpermissionforcreate || $wemustcheckpermissionfordeletedraft) {
465 foreach ($featuresarray as $feature) {
466 if ($feature == 'contact') {
467 if (!$user->hasRight('societe', 'contact', 'creer')) {
468 $createok = 0;
469 $nbko++;
470 }
471 } elseif ($feature == 'produit|service') {
472 if (!$user->hasRight('produit', 'creer') && !$user->hasRight('service', 'creer')) {
473 $createok = 0;
474 $nbko++;
475 }
476 } elseif ($feature == 'prelevement') {
477 if (!$user->hasRight('prelevement', 'bons', 'creer')) {
478 $createok = 0;
479 $nbko++;
480 }
481 } elseif ($feature == 'commande_fournisseur') {
482 if (!$user->hasRight('fournisseur', 'commande', 'creer') || !$user->hasRight('supplier_order', 'creer')) {
483 $createok = 0;
484 $nbko++;
485 }
486 } elseif ($feature == 'banque') {
487 if (!$user->hasRight('banque', 'modifier')) {
488 $createok = 0;
489 $nbko++;
490 }
491 } elseif ($feature == 'cheque') {
492 if (!$user->hasRight('banque', 'cheque')) {
493 $createok = 0;
494 $nbko++;
495 }
496 } elseif ($feature == 'import') {
497 if (!$user->hasRight('import', 'run')) {
498 $createok = 0;
499 $nbko++;
500 }
501 } elseif ($feature == 'ecm') {
502 if (!$user->hasRight('ecm', 'upload')) {
503 $createok = 0;
504 $nbko++;
505 }
506 } elseif ($feature == 'modulebuilder') {
507 if (!$user->hasRight('modulebuilder', 'run')) {
508 $createok = 0;
509 $nbko++;
510 }
511 } elseif ($feature == 'payment') {
512 if (!$user->hasRight('facture', 'paiement')) {
513 $createok = 0;
514 $nbko++;
515 }
516 } elseif ($feature == 'webhook') {
517 if (empty($user->admin)) {
518 $createok = 0;
519 $nbko++;
520 }
521 } elseif (!empty($feature2)) { // This is for permissions on 2 levels (module->object->write)
522 foreach ($feature2 as $subfeature) {
523 if ($subfeature == 'user' && $user->id == $objectid && $user->hasRight('user', 'self', 'creer')) {
524 continue; // User can edit its own card
525 }
526 if ($subfeature == 'user' && $user->id == $objectid && $user->hasRight('user', 'self', 'password')) {
527 continue; // User can edit its own password
528 }
529 if ($subfeature == 'user' && $user->id != $objectid && $user->hasRight('user', 'user', 'password')) {
530 continue; // User can edit another user's password
531 }
532
533 if (!$user->hasRight($feature, $subfeature, 'creer')
534 && !$user->hasRight($feature, $subfeature, 'write')
535 && !$user->hasRight($feature, $subfeature, 'create')) {
536 $createok = 0;
537 $nbko++;
538 } else {
539 $createok = 1;
540 // Break to bypass second test if the first is ok
541 break;
542 }
543 }
544 } elseif (!empty($feature)) { // This is for permissions on 1 levels (module->write)
545 //print '<br>feature='.$feature.' creer='.$user->rights->$feature->creer.' write='.$user->rights->$feature->write; exit;
546 if (!$user->hasRight($feature, 'creer')
547 && !$user->hasRight($feature, 'write')
548 && !$user->hasRight($feature, 'create')) {
549 $createok = 0;
550 $nbko++;
551 }
552 }
553 }
554
555 // If a or and at least one ok
556 if (preg_match('/\|/', $features) && $nbko < count($featuresarray)) {
557 $createok = 1;
558 }
559
560 if ($wemustcheckpermissionforcreate && !$createok) {
561 if ($mode) {
562 return 0;
563 } else {
565 }
566 }
567 //print "Write access is ok";
568 }
569
570 // Check create user permission
571 $createuserok = 1;
572 if (GETPOST('action', 'aZ09') == 'confirm_create_user' && GETPOST("confirm", 'aZ09') == 'yes') {
573 if (!$user->hasRight('user', 'user', 'creer')) {
574 $createuserok = 0;
575 }
576
577 if (!$createuserok) {
578 if ($mode) {
579 return 0;
580 } else {
582 }
583 }
584 //print "Create user access is ok";
585 }
586
587 // Check delete permission from module
588 $deleteok = 1;
589 $nbko = 0;
590 if ((GETPOST("action", "aZ09") == 'confirm_delete' && GETPOST("confirm", "aZ09") == 'yes') || GETPOST("action", "aZ09") == 'delete') {
591 foreach ($featuresarray as $feature) {
592 if ($feature == 'bookmark') {
593 if (!$user->hasRight('bookmark', 'supprimer')) {
594 if ($user->id != $object->fk_user || !$user->hasRight('bookmark', 'creer')) {
595 $deleteok = 0;
596 }
597 }
598 } elseif ($feature == 'contact') {
599 if (!$user->hasRight('societe', 'contact', 'supprimer')) {
600 $deleteok = 0;
601 }
602 } elseif ($feature == 'produit|service') {
603 if (!$user->hasRight('produit', 'supprimer') && !$user->hasRight('service', 'supprimer')) {
604 $deleteok = 0;
605 }
606 } elseif ($feature == 'commande_fournisseur') {
607 if (!$user->hasRight('fournisseur', 'commande', 'supprimer')) {
608 $deleteok = 0;
609 }
610 } elseif ($feature == 'payment_supplier') { // Permission to delete a payment of an invoice is permission to edit an invoice.
611 if (!$user->hasRight('fournisseur', 'facture', 'creer')) {
612 $deleteok = 0;
613 }
614 } elseif ($feature == 'payment') {
615 if (!$user->hasRight('facture', 'paiement')) {
616 $deleteok = 0;
617 }
618 } elseif ($feature == 'payment_sc') {
619 if (!$user->hasRight('tax', 'charges', 'creer')) {
620 $deleteok = 0;
621 }
622 } elseif ($feature == 'banque') {
623 if (!$user->hasRight('banque', 'modifier')) {
624 $deleteok = 0;
625 }
626 } elseif ($feature == 'cheque') {
627 if (!$user->hasRight('banque', 'cheque')) {
628 $deleteok = 0;
629 }
630 } elseif ($feature == 'ecm') {
631 if (!$user->hasRight('ecm', 'upload')) {
632 $deleteok = 0;
633 }
634 } elseif ($feature == 'ftp') {
635 if (!$user->hasRight('ftp', 'write')) {
636 $deleteok = 0;
637 }
638 } elseif ($feature == 'salaries') {
639 if (!$user->hasRight('salaries', 'delete')) {
640 $deleteok = 0;
641 }
642 } elseif ($feature == 'adherent') {
643 if (!$user->hasRight('adherent', 'supprimer')) {
644 $deleteok = 0;
645 }
646 } elseif ($feature == 'paymentbybanktransfer') {
647 if (!$user->hasRight('paymentbybanktransfer', 'create')) { // There is no delete permission
648 $deleteok = 0;
649 }
650 } elseif ($feature == 'prelevement') {
651 if (!$user->hasRight('prelevement', 'bons', 'creer')) { // There is no delete permission
652 $deleteok = 0;
653 }
654 } elseif (!empty($feature2)) { // This is for permissions on 2 levels
655 foreach ($feature2 as $subfeature) {
656 if (!$user->hasRight($feature, $subfeature, 'supprimer') && !$user->hasRight($feature, $subfeature, 'delete')) {
657 $deleteok = 0;
658 } else {
659 $deleteok = 1;
660 break;
661 } // For bypass the second test if the first is ok
662 }
663 } elseif (!empty($feature)) { // This is used for permissions on 1 level
664 //print '<br>feature='.$feature.' creer='.$user->rights->$feature->supprimer.' write='.$user->rights->$feature->delete;
665 if (!$user->hasRight($feature, 'supprimer')
666 && !$user->hasRight($feature, 'delete')
667 && !$user->hasRight($feature, 'run')) {
668 $deleteok = 0;
669 }
670 }
671 }
672
673 // If a or and at least one ok
674 if (preg_match('/\|/', $features) && $nbko < count($featuresarray)) {
675 $deleteok = 1;
676 }
677
678 if (!$deleteok && !($isdraft && $createok)) {
679 if ($mode) {
680 return 0;
681 } else {
683 }
684 }
685 //print "Delete access is ok";
686 }
687
688 // If we have a particular object to check permissions on, we check if $user has permission
689 // for this given object (link to company, is contact for project, ...)
690 if (!empty($objectid) && $objectid > 0) {
691 $ok = checkUserAccessToObject($user, $featuresarray, $object, $tableandshare, $feature2, $dbt_keyfield, $dbt_select, $parentfortableentity);
692 $params = array('objectid' => $objectid, 'features' => implode(',', $featuresarray), 'features2' => $feature2);
693 //print 'checkUserAccessToObject ok='.$ok;
694 if ($mode) {
695 return $ok ? 1 : 0;
696 } else {
697 if ($ok) {
698 return 1;
699 } else {
700 accessforbidden('', 1, 1, 0, $params);
701 }
702 }
703 }
704
705 return 1;
706}
707
723function checkUserAccessToObject($user, array $featuresarray, $object = 0, $tableandshare = '', $feature2 = '', $dbt_keyfield = '', $dbt_select = 'rowid', $parenttableforentity = '')
724{
725 global $db, $conf;
726
727 if (is_object($object)) {
728 $objectid = $object->id;
729 } else {
730 $objectid = $object; // $objectid can be X or 'X,Y,Z'
731 }
732 $objectid = preg_replace('/[^0-9\.\,]/', '', (string) $objectid); // For the case value is coming from a non sanitized user input
733
734 //dol_syslog("functions.lib:restrictedArea $feature, $object, $dbtablename, $feature2, $dbt_socfield, $dbt_select, $isdraft");
735 //print "user_id=".$user->id.", features=".join(',', $featuresarray).", object=".$object;
736 //print ", tableandshare=".$tableandshare.", dbt_socfield=".$dbt_keyfield.", dbt_select=".$dbt_select."<br>";
737
738 // More parameters
739 $params = explode('&', $tableandshare);
740 $dbtablename = (!empty($params[0]) ? $params[0] : '');
741 $sharedelement = (!empty($params[1]) ? $params[1] : $dbtablename);
742
743 foreach ($featuresarray as $feature) {
744 $sql = '';
745
746 //var_dump($feature);exit;
747
748 // For backward compatibility
749 if ($feature == 'societe' && !empty($feature2) && is_array($feature2) && in_array('contact', $feature2)) {
750 $feature = 'contact';
751 $feature2 = '';
752 }
753 if ($feature == 'member') {
754 $feature = 'adherent';
755 }
756 if ($feature == 'category') {
757 $feature = 'categorie';
758 }
759 if ($feature == 'project') {
760 $feature = 'projet';
761 }
762 if ($feature == 'projet' && !empty($feature2) && is_array($feature2) && !empty(array_intersect(array('project_task', 'projet_task'), $feature2))) {
763 $feature = 'project_task';
764 }
765 if ($feature == 'task' || $feature == 'projet_task') {
766 $feature = 'project_task';
767 $dbtablename = 'projet_task';
768 }
769 if ($feature == 'eventorganization') {
770 $feature = 'agenda';
771 $dbtablename = 'actioncomm';
772 }
773 if ($feature == 'payment_sc' && empty($parenttableforentity)) {
774 // If we check perm on payment page but $parenttableforentity not defined, we force value on parent table
775 $parenttableforentity = '';
776 $dbtablename = "chargesociales";
777 $feature = "chargesociales";
778 $objectid = (string) $object->fk_charge;
779 }
780
781 $checkonentitydone = 0;
782
783 // Array to define rules of checks to do
784 $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)
785 $checksoc = array('societe'); // Test for object Societe
786 $checkparentsoc = array('agenda', 'contact', 'contrat'); // Test on entity + link to third party on field $dbt_keyfield. Allowed if link is empty (Ex: contacts...).
787 $checkproject = array('projet', 'project'); // Test for project object
788 $checktask = array('projet_task', 'project_task'); // Test for task object
789 $checkhierarchy = array('expensereport', 'holiday', 'hrm'); // check permission among the hierarchy of user
790 $checkuser = array('bookmark'); // check permission among the fk_user (must be myself or null)
791 $nocheck = array('barcode', 'webhook'); // No test
792
793 //$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...).
794
795 // If dbtablename not defined, we use same name for table than module name
796 if (empty($dbtablename)) {
797 $dbtablename = $feature;
798 $sharedelement = (!empty($params[1]) ? $params[1] : $dbtablename); // We change dbtablename, so we set sharedelement too.
799 }
800
801 // $objectid was already sanitized at begin of this method (can be an int or a list of int separated by comma).
802 // To avoid an access forbidden with a numeric ref
803 if ($dbt_select != 'rowid' && $dbt_select != 'id') {
804 $objectid = "'".$objectid."'";
805 }
806
807 // Check permission for objectid on entity only
808 if (in_array($feature, $check) && !empty($objectid)) { // For $objectid = 0, no check
809 $sql = "SELECT COUNT(dbt.".$db->sanitize($dbt_select).") as nb";
810 $sql .= " FROM ".MAIN_DB_PREFIX.$db->sanitize($dbtablename)." as dbt";
811 if (($feature == 'user' || $feature == 'usergroup') && isModEnabled('multicompany')) { // Special for multicompany
812 if (getDolGlobalString('MULTICOMPANY_TRANSVERSE_MODE')) {
813 if ($conf->entity == 1 && $user->admin && !$user->entity) {
814 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
815 $sql .= " AND dbt.entity IS NOT NULL";
816 } else {
817 $sql .= ",".MAIN_DB_PREFIX."usergroup_user as ug";
818 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
819 $sql .= " AND ((ug.fk_user = dbt.rowid";
820 $sql .= " AND ug.entity IN (".getEntity('usergroup')."))";
821 $sql .= " OR dbt.entity = 0)"; // Show always superadmin
822 }
823 } else {
824 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
825 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
826 }
827 } else {
828 $reg = array();
829 if ($parenttableforentity && preg_match('/(.*)@(.*)/', $parenttableforentity, $reg)) {
830 $sql .= ", ".MAIN_DB_PREFIX.$db->sanitize($reg[2])." as dbtp";
831 $sql .= " WHERE dbt.".$db->sanitize($reg[1])." = dbtp.rowid AND dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
832 $sql .= " AND dbtp.entity IN (".getEntity($sharedelement, 1).")";
833 } else {
834 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
835 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
836 }
837 }
838 $checkonentitydone = 1;
839 }
840 if (in_array($feature, $checksoc) && !empty($objectid)) { // We check feature = checksoc. For $objectid = 0, no check
841 // If external user: Check permission for external users
842 if ($user->socid > 0) {
843 if ((string) $user->socid != $objectid) {
844 return false;
845 }
846 } elseif (isModEnabled('societe') && !$user->hasRight('societe', 'lire') && !$user->hasRight('societe', 'client', 'voir')) {
847 dol_syslog("security.lib.php::checkUserAccessToObject Deny access due: (isModEnabled('societe') && !user->hasRight('societe', 'lire') && !user->hasRight('societe', 'client', 'voir'))", LOG_DEBUG);
848 return false;
849 } elseif (isModEnabled("societe") && ($user->hasRight('societe', 'lire') && !$user->hasRight('societe', 'client', 'voir'))) {
850 // If internal user: Check permission for internal users that are restricted on their objects
851 $sql = "SELECT COUNT(sc.fk_soc) as nb";
852 $sql .= " FROM (".MAIN_DB_PREFIX."societe_commerciaux as sc";
853 $sql .= ", ".MAIN_DB_PREFIX."societe as s)";
854 $sql .= " WHERE sc.fk_soc IN (".$db->sanitize($objectid, 1).")";
855 $sql .= " AND (sc.fk_user = ".((int) $user->id);
856 if (getDolGlobalInt('MAIN_SEE_SUBORDINATES')) {
857 $userschilds = $user->getAllChildIds();
858 if (!empty($userschilds)) $sql .= " OR sc.fk_user IN (".$db->sanitize(implode(',', $userschilds)).")";
859 }
860 $sql .= ")";
861 $sql .= " AND sc.fk_soc = s.rowid";
862 $sql .= " AND s.entity IN (".getEntity($sharedelement, 1).")";
863 } elseif (isModEnabled('multicompany')) {
864 // If multicompany and internal users with all permissions, check user is in correct entity
865 $sql = "SELECT COUNT(s.rowid) as nb";
866 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
867 $sql .= " WHERE s.rowid IN (".$db->sanitize($objectid, 1).")";
868 $sql .= " AND s.entity IN (".getEntity($sharedelement, 1).")";
869 }
870
871 $checkonentitydone = 1;
872 }
873 if (in_array($feature, $checkparentsoc) && !empty($objectid)) { // Test on entity + link to thirdparty. Allowed if link is empty (Ex: contacts...).
874 // If external user: Check permission for external users
875 if ($user->socid > 0) {
876 $sql = "SELECT COUNT(dbt.".$db->sanitize($dbt_select).") as nb";
877 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
878 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
879 $sql .= " AND dbt.fk_soc = ".((int) $user->socid);
880 } elseif (isModEnabled("societe") && ($user->hasRight('societe', 'lire') && !$user->hasRight('societe', 'client', 'voir'))) {
881 // If internal user: Check permission for internal users that are restricted on their objects
882 $sql = "SELECT COUNT(dbt.".$db->sanitize($dbt_select).") as nb";
883 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
884 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON dbt.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
885 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
886 $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
887 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
888 } elseif (isModEnabled('multicompany')) {
889 // If multicompany and internal users with all permissions, check user is in correct entity
890 $sql = "SELECT COUNT(dbt.".$db->sanitize($dbt_select).") as nb";
891 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
892 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
893 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
894 }
895
896 $checkonentitydone = 1;
897 }
898 if (in_array($feature, $checkproject) && !empty($objectid)) {
899 if (isModEnabled('project') && !$user->hasRight('projet', 'all', 'lire')) {
900 $projectid = $objectid; // Note that if $objectid is a string list of id; the test later will return false
901
902 include_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
903 $projectstatic = new Project($db);
904 $tmps = $projectstatic->getProjectsAuthorizedForUser($user, 0, 1, 0);
905
906 $tmparray = explode(',', $tmps);
907 if (!in_array($projectid, $tmparray)) {
908 return false;
909 }
910 } else {
911 $sql = "SELECT COUNT(dbt.".$db->sanitize($dbt_select).") as nb";
912 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
913 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
914 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
915 }
916 $checkonentitydone = 1;
917 }
918 if (in_array($feature, $checktask) && !empty($objectid)) {
919 if (isModEnabled('project') && !$user->hasRight('projet', 'all', 'lire')) {
920 if (preg_match('/,/', $objectid)) { // if this is a list of id
921 return false;
922 }
923 $task = new Task($db);
924 $task->fetch((int) $objectid);
925 $projectid = $task->fk_project;
926
927 include_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
928 $projectstatic = new Project($db);
929 $tmps = $projectstatic->getProjectsAuthorizedForUser($user, 0, 1, 0);
930
931 $tmparray = explode(',', $tmps);
932 if (!in_array($projectid, $tmparray)) {
933 return false;
934 }
935 } else {
936 $sharedelement = 'project'; // for multicompany compatibility
937 $sql = "SELECT COUNT(dbt.".$db->sanitize($dbt_select).") as nb";
938 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
939 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
940 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
941 }
942
943 $checkonentitydone = 1;
944 }
945 //var_dump($sql);
946
947 if (!$checkonentitydone && !in_array($feature, $nocheck) && !empty($objectid)) { // By default (case of $checkdefault), we check on object entity + link to third party on field $dbt_keyfield
948 // If external user: Check permission for external users
949 if ($user->socid > 0) {
950 if (empty($dbt_keyfield)) {
951 dol_print_error(null, 'Param dbt_keyfield is required but not defined');
952 }
953 $sql = "SELECT COUNT(dbt.".$db->sanitize($dbt_keyfield).") as nb";
954 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
955 $sql .= " WHERE dbt.rowid IN (".$db->sanitize($objectid, 1).")";
956 $sql .= " AND dbt.".$db->sanitize($dbt_keyfield)." = ".((int) $user->socid);
957 } elseif (isModEnabled("societe") && !$user->hasRight('societe', 'client', 'voir')) {
958 // If internal user without permission to see all thirdparties: Check permission for internal users that are restricted on their objects
959 if (empty($dbt_keyfield)) {
960 dol_print_error(null, 'Param dbt_keyfield is required but not defined');
961 }
962 if ($feature != 'ticket') {
963 $sql = "SELECT COUNT(sc.fk_soc) as nb";
964 $sql .= " FROM ".MAIN_DB_PREFIX.$db->sanitize($dbtablename)." as dbt";
965 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
966 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
967 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
968 $sql .= " AND sc.fk_soc = dbt.".$db->sanitize($dbt_keyfield);
969 $sql .= " AND (sc.fk_user = ".((int) $user->id);
970 if (getDolGlobalInt('MAIN_SEE_SUBORDINATES')) {
971 $userschilds = $user->getAllChildIds();
972 if (!empty($userschilds)) $sql .= " OR sc.fk_user IN (".$db->sanitize(implode(',', $userschilds)).")";
973 }
974 $sql .= ')';
975 } else {
976 // On ticket, the thirdparty is not mandatory, so we need a special test to accept record with no thirdparties.
977 $sql = "SELECT COUNT(dbt.".$db->sanitize($dbt_select).") as nb";
978 $sql .= " FROM ".MAIN_DB_PREFIX.$dbtablename." as dbt";
979 $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);
980 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
981 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
982 $sql .= " AND (sc.fk_user = ".((int) $user->id)." OR dbt.".$dbt_keyfield." IS NULL OR dbt.".$dbt_keyfield." = 0)";
983 }
984 } elseif (isModEnabled('multicompany') && (!empty($object->ismultientitymanaged) || !isset($object->ismultientitymanaged))) {
985 // If multicompany, and user is an internal user with all permissions, check that object is in correct entity
986 $sql = "SELECT COUNT(dbt.".$db->sanitize($dbt_select).") as nb";
987 $sql .= " FROM ".MAIN_DB_PREFIX.$db->sanitize($dbtablename)." as dbt";
988 $sql .= " WHERE dbt.".$db->sanitize($dbt_select)." IN (".$db->sanitize($objectid, 1).")";
989 $sql .= " AND dbt.entity IN (".getEntity($sharedelement, 1).")";
990 }
991 }
992
993 // For events, check on users assigned to event
994 if ($feature === 'agenda' && !empty($objectid)) {
995 // Also check owner or attendee for users without allactions->read
996 if (!$user->hasRight('agenda', 'allactions', 'read')) {
997 if (preg_match('/,/', $objectid)) { // if this is a list of id
998 return false;
999 }
1000
1001 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
1002 $action = new ActionComm($db);
1003 $action->fetch((int) $objectid);
1004 if ($action->authorid != $user->id && $action->userownerid != $user->id && !(array_key_exists($user->id, $action->userassigned))) {
1005 return false;
1006 }
1007 }
1008 }
1009
1010 // For some object, we also have to check it is in the user hierarchy
1011 // Param $object must be the full object and not a simple id to have this test possible.
1012 if (in_array($feature, $checkhierarchy) && is_object($object) && !empty($objectid)) {
1013 $childids = $user->getAllChildIds(1);
1014 $useridtocheck = 0;
1015 if ($feature == 'holiday') {
1016 $useridtocheck = $object->fk_user;
1017 if (!$user->hasRight('holiday', 'readall') && !in_array($useridtocheck, $childids) && !in_array($object->fk_validator, $childids)) {
1018 return false;
1019 }
1020 }
1021 if ($feature == 'expensereport') {
1022 $useridtocheck = $object->fk_user_author;
1023 if (!$user->hasRight('expensereport', 'readall')) {
1024 if (!in_array($useridtocheck, $childids)) {
1025 return false;
1026 }
1027 }
1028 }
1029 if ($feature == 'hrm' && in_array('evaluation', $feature2)) {
1030 $useridtocheck = $object->fk_user;
1031
1032 if ($user->hasRight('hrm', 'evaluation', 'readall')) {
1033 // the user can view evaluations for anyone
1034 return true;
1035 }
1036 if (!$user->hasRight('hrm', 'evaluation', 'read')) {
1037 // the user can't view any evaluations
1038 return false;
1039 }
1040 // the user can only see their own evaluations or their subordinates'
1041 return in_array($useridtocheck, $childids);
1042 }
1043 }
1044
1045 // For some object, we also have to check it is public or owned by user
1046 // Param $object must be the full object and not a simple id to have this test possible.
1047 if (in_array($feature, $checkuser) && is_object($object) && !empty($objectid)) {
1048 $useridtocheck = $object->fk_user;
1049 if (!empty($useridtocheck) && $useridtocheck > 0 && $useridtocheck != $user->id && empty($user->admin)) {
1050 return false;
1051 }
1052 }
1053
1054 if ($sql) {
1055 $resql = $db->query($sql);
1056 if ($resql) {
1057 $obj = $db->fetch_object($resql);
1058 if (!$obj || $obj->nb < count(explode(',', $objectid))) { // error if we found 0 or less record than the nb of ids provided
1059 return false;
1060 }
1061 } else {
1062 dol_syslog("Bad forged sql in security.lib.php::checkUserAccessToObject", LOG_WARNING);
1063 return false;
1064 }
1065 }
1066 }
1067
1068 dol_syslog("security.lib.php::checkUserAccessToObject::return True", LOG_DEBUG);
1069 return true;
1070}
1071
1072
1084function httponly_accessforbidden($message = '1', $http_response_code = 403, $stringalreadysanitized = 0)
1085{
1086 top_httphead();
1087 http_response_code($http_response_code);
1088
1089 if ($stringalreadysanitized) {
1090 print $message;
1091 } else {
1092 print htmlentities($message);
1093 }
1094
1095 exit(1);
1096}
1097
1111function accessforbidden($message = '', $printheader = 1, $printfooter = 1, $showonlymessage = 0, $params = null)
1112{
1113 global $conf, $db, $user, $langs, $hookmanager;
1114 global $action, $object;
1115
1116 if (!is_object($langs)) {
1117 include_once DOL_DOCUMENT_ROOT.'/core/class/translate.class.php';
1118 $langs = new Translate('', $conf);
1119 $langs->setDefaultLang();
1120 }
1121
1122 $langs->loadLangs(array("main", "errors"));
1123
1124 if ($printheader && !defined('NOHEADERNOFOOTER')) {
1125 if (function_exists("llxHeader")) {
1126 llxHeader('');
1127 } elseif (function_exists("llxHeaderVierge")) {
1128 llxHeaderVierge('');
1129 }
1130 print '<div style="padding: 20px">';
1131 }
1132 print '<div class="error">';
1133 if (empty($message)) {
1134 print $langs->trans("ErrorForbidden");
1135 } else {
1136 print $langs->trans($message);
1137 }
1138 print '</div>';
1139 print '<br>';
1140 if (empty($showonlymessage)) {
1141 if (empty($hookmanager)) {
1142 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
1143 $hookmanager = new HookManager($db);
1144 // Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
1145 $hookmanager->initHooks(array('main'));
1146 }
1147
1148 $parameters = array('message' => $message, 'params' => $params);
1149 $reshook = $hookmanager->executeHooks('getAccessForbiddenMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
1150 print $hookmanager->resPrint;
1151 if (empty($reshook)) {
1152 $langs->loadLangs(array("errors"));
1153 if ($user->login) {
1154 print $langs->trans("CurrentLogin").': <span class="error">'.$user->login.'</span><br>';
1155 print $langs->trans("ErrorForbidden2", $langs->transnoentitiesnoconv("Home"), $langs->transnoentitiesnoconv("Users"));
1156 print $langs->trans("ErrorForbidden4");
1157 } else {
1158 print $langs->trans("ErrorForbidden3");
1159 }
1160 }
1161 }
1162 if ($printfooter && !defined('NOHEADERNOFOOTER') && function_exists("llxFooter")) {
1163 print '</div>';
1164 llxFooter();
1165 }
1166
1167 // End PHP
1168 exit(0);
1169}
1170
1171
1179{
1180 $max = getDolGlobalString('MAIN_UPLOAD_DOC'); // In Kb
1181
1182 $maxphp = @ini_get('upload_max_filesize'); // In unknown
1183 if (preg_match('/k$/i', $maxphp)) {
1184 $maxphp = preg_replace('/k$/i', '', $maxphp);
1185 $maxphp = (int) ((float) $maxphp * 1);
1186 }
1187 if (preg_match('/m$/i', $maxphp)) {
1188 $maxphp = preg_replace('/m$/i', '', $maxphp);
1189 $maxphp = (int) ((float) $maxphp * 1024);
1190 }
1191 if (preg_match('/g$/i', $maxphp)) {
1192 $maxphp = preg_replace('/g$/i', '', $maxphp);
1193 $maxphp = (int) ((float) $maxphp * 1024 * 1024);
1194 }
1195 if (preg_match('/t$/i', $maxphp)) {
1196 $maxphp = preg_replace('/t$/i', '', $maxphp);
1197 $maxphp = (int) ((float) $maxphp * 1024 * 1024 * 1024);
1198 }
1199 $maxphp2 = @ini_get('post_max_size'); // In unknown
1200 if (preg_match('/k$/i', $maxphp2)) {
1201 $maxphp2 = preg_replace('/k$/i', '', $maxphp2);
1202 $maxphp2 = (int) ((float) $maxphp2) * 1;
1203 }
1204 if (preg_match('/m$/i', $maxphp2)) {
1205 $maxphp2 = preg_replace('/m$/i', '', $maxphp2);
1206 $maxphp2 = (int) ((float) $maxphp2 * 1024);
1207 }
1208 if (preg_match('/g$/i', $maxphp2)) {
1209 $maxphp2 = preg_replace('/g$/i', '', $maxphp2);
1210 $maxphp2 = (int) ((float) $maxphp2 * 1024 * 1024);
1211 }
1212 if (preg_match('/t$/i', $maxphp2)) {
1213 $maxphp2 = preg_replace('/t$/i', '', $maxphp2);
1214 $maxphp2 = (int) ((float) $maxphp2 * 1024 * 1024 * 1024);
1215 }
1216 // Now $max and $maxphp and $maxphp2 are in Kb
1217 $maxmin = $max;
1218 $maxphptoshow = $maxphptoshowparam = '';
1219 if ($maxphp > 0) {
1220 $maxmin = min($maxmin, $maxphp);
1221 $maxphptoshow = $maxphp;
1222 $maxphptoshowparam = 'upload_max_filesize';
1223 }
1224 if ($maxphp2 > 0) {
1225 $maxmin = min($maxmin, $maxphp2);
1226 if ($maxphp2 < $maxphp) {
1227 $maxphptoshow = $maxphp2;
1228 $maxphptoshowparam = 'post_max_size';
1229 }
1230 }
1231 //var_dump($maxphp.'-'.$maxphp2);
1232 //var_dump($maxmin);
1233
1234 return array('max' => $max, 'maxmin' => $maxmin, 'maxphptoshow' => $maxphptoshow, 'maxphptoshowparam' => $maxphptoshowparam);
1235}
1236
1244function checkIPInCidr($ip, $cidr)
1245{
1246 list($network, $prefix) = explode('/', $cidr, 2);
1247
1248 // Convert IPs to binary format
1249 $ip_bin = @inet_pton($ip);
1250 $net_bin = @inet_pton($network);
1251 if ($ip_bin === false || $net_bin === false) {
1252 return -1;
1253 }
1254
1255 // Require same address IPvX family
1256 if (strlen($ip_bin) !== strlen($net_bin)) {
1257 return -1;
1258 }
1259
1260 // Comparison boundaries
1261 $total_bits = strlen($ip_bin) * 8;
1262 $prefix = max(0, min((int) $prefix, $total_bits));
1263 $full_bytes = intdiv($prefix, 8);
1264 $rem_bits = $prefix % 8;
1265
1266 // Compare full bytes and partial bytes
1267 if ($full_bytes > 0) {
1268 if (substr($ip_bin, 0, $full_bytes) !== substr($net_bin, 0, $full_bytes)) {
1269 return 0;
1270 }
1271 }
1272 if ($rem_bits > 0) {
1273 $mask = (0xFF << (8 - $rem_bits)) & 0xFF;
1274 $ip_byte = ord($ip_bin[$full_bytes]);
1275 $net_byte = ord($net_bin[$full_bytes]);
1276 if (($ip_byte & $mask) !== ($net_byte & $mask)) {
1277 return 0;
1278 }
1279 }
1280 return 1;
1281}
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.