dolibarr  16.0.5
functions_dolibarr.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2015 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2007-2015 Regis Houssin <regis.houssin@inodbox.com>
4  * Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
5  * Copyright (C) 2022 Harry Winner Kamdem <harry@sense.africa>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
37 function check_user_password_dolibarr($usertotest, $passwordtotest, $entitytotest = 1)
38 {
39  global $db, $conf, $langs;
40 
41  // Force master entity in transversal mode
42  $entity = $entitytotest;
43  if (!empty($conf->multicompany->enabled) && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) {
44  $entity = 1;
45  }
46 
47  $login = '';
48 
49  if (!empty($usertotest)) {
50  require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
51  dol_syslog("functions_dolibarr::check_user_password_dolibarr usertotest=".$usertotest." passwordtotest=".preg_replace('/./', '*', $passwordtotest)." entitytotest=".$entitytotest);
52 
53  // If test username/password asked, we define $test=false if ko and $login var to login if ok, set also $_SESSION["dol_loginmesg"] if ko
54  $table = MAIN_DB_PREFIX."user";
55  $usernamecol1 = 'login';
56  $usernamecol2 = 'email';
57  $entitycol = 'entity';
58 
59  $sql = "SELECT rowid, login, entity, pass, pass_crypted, datestartvalidity, dateendvalidity";
60  $sql .= " FROM ".$table;
61  $sql .= " WHERE (".$usernamecol1." = '".$db->escape($usertotest)."'";
62  if (preg_match('/@/', $usertotest)) {
63  $sql .= " OR ".$usernamecol2." = '".$db->escape($usertotest)."'";
64  }
65  $sql .= ") AND ".$entitycol." IN (0,".($entity ? ((int) $entity) : 1).")";
66  $sql .= " AND statut = 1";
67  // Note: Test on validity is done later
68  // Order is required to firstly found the user into entity, then the superadmin.
69  // For the case (TODO: we must avoid that) a user has renamed its login with same value than a user in entity 0.
70  $sql .= " ORDER BY entity DESC";
71 
72  $resql = $db->query($sql);
73  if ($resql) {
74  $obj = $db->fetch_object($resql);
75  if ($obj) {
76  $now = dol_now();
77  if ($obj->datestartvalidity && $db->jdate($obj->datestartvalidity) > $now) {
78  // Load translation files required by the page
79  $langs->loadLangs(array('main', 'errors'));
80  $_SESSION["dol_loginmesg"] = $langs->transnoentitiesnoconv("ErrorLoginDateValidity");
81  return '--bad-login-validity--';
82  }
83  if ($obj->dateendvalidity && $db->jdate($obj->dateendvalidity) < dol_get_first_hour($now)) {
84  // Load translation files required by the page
85  $langs->loadLangs(array('main', 'errors'));
86  $_SESSION["dol_loginmesg"] = $langs->transnoentitiesnoconv("ErrorLoginDateValidity");
87  return '--bad-login-validity--';
88  }
89 
90  $passclear = $obj->pass;
91  $passcrypted = $obj->pass_crypted;
92  $passtyped = $passwordtotest;
93 
94  $passok = false;
95 
96  // Check crypted password
97  $cryptType = '';
98  if (!empty($conf->global->DATABASE_PWD_ENCRYPTED)) {
99  $cryptType = $conf->global->DATABASE_PWD_ENCRYPTED;
100  }
101 
102  // By default, we use default setup for encryption rule
103  if (!in_array($cryptType, array('auto'))) {
104  $cryptType = 'auto';
105  }
106  // Check crypted password according to crypt algorithm
107  if ($cryptType == 'auto') {
108  if ($passcrypted && dol_verifyHash($passtyped, $passcrypted, '0')) {
109  $passok = true;
110  dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentification ok - hash ".$cryptType." of pass is ok");
111  }
112  }
113 
114  // For compatibility with very old versions
115  if (!$passok) {
116  if ((!$passcrypted || $passtyped)
117  && ($passclear && ($passtyped == $passclear))) {
118  $passok = true;
119  dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentification ok - found pass in database");
120  }
121  }
122 
123  // Password ok ?
124  if ($passok) {
125  $login = $obj->login;
126  } else {
127  sleep(1); // Anti brut force protection
128  dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentication KO bad password for '".$usertotest."', cryptType=".$cryptType, LOG_NOTICE);
129 
130  // Load translation files required by the page
131  $langs->loadLangs(array('main', 'errors'));
132 
133  $_SESSION["dol_loginmesg"] = $langs->transnoentitiesnoconv("ErrorBadLoginPassword");
134  }
135 
136  // We must check entity
137  if ($passok && !empty($conf->multicompany->enabled)) { // We must check entity
138  global $mc;
139 
140  if (!isset($mc)) {
141  $conf->multicompany->enabled = false; // Global not available, disable $conf->multicompany->enabled for safety
142  } else {
143  $ret = $mc->checkRight($obj->rowid, $entitytotest);
144  if ($ret < 0) {
145  dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentication KO entity '".$entitytotest."' not allowed for user '".$obj->rowid."'", LOG_NOTICE);
146 
147  $login = ''; // force authentication failure
148  if ($mc->db->lasterror()) {
149  $_SESSION["dol_loginmesg"] = $mc->db->lasterror();
150  }
151  }
152  }
153  }
154  } else {
155  dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentication KO user not found for '".$usertotest."'", LOG_NOTICE);
156  sleep(1);
157 
158  // Load translation files required by the page
159  $langs->loadLangs(array('main', 'errors'));
160 
161  $_SESSION["dol_loginmesg"] = $langs->transnoentitiesnoconv("ErrorBadLoginPassword");
162  }
163  } else {
164  dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentication KO db error for '".$usertotest."' error=".$db->lasterror(), LOG_ERR);
165  sleep(1);
166  $_SESSION["dol_loginmesg"] = $db->lasterror();
167  }
168  }
169 
170  return $login;
171 }
dol_verifyHash
dol_verifyHash($chain, $hash, $type='0')
Compute a hash and compare it to the given one For backward compatibility reasons,...
Definition: security.lib.php:151
dol_get_first_hour
dol_get_first_hour($date, $gm='tzserver')
Return GMT time for first hour of a given GMT date (it removes hours, min and second part)
Definition: date.lib.php:611
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
check_user_password_dolibarr
check_user_password_dolibarr($usertotest, $passwordtotest, $entitytotest=1)
Check validity of user/password/entity If test is ko, reason must be filled into $_SESSION["dol_login...
Definition: functions_dolibarr.php:37
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742