dolibarr 18.0.6
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
38function check_user_password_dolibarr($usertotest, $passwordtotest, $entitytotest = 1)
39{
40 global $db, $conf, $langs;
41
42 // Force master entity in transversal mode
43 $entity = $entitytotest;
44 if (isModEnabled('multicompany') && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) {
45 $entity = 1;
46 }
47
48 $login = '';
49
50 if (!empty($usertotest)) {
51 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
52 dol_syslog("functions_dolibarr::check_user_password_dolibarr usertotest=".$usertotest." passwordtotest=".preg_replace('/./', '*', $passwordtotest)." entitytotest=".$entitytotest);
53
54 // 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
55 $table = MAIN_DB_PREFIX."user";
56 $usernamecol1 = 'login';
57 $usernamecol2 = 'email';
58 $entitycol = 'entity';
59
60 $sql = "SELECT rowid, login, entity, pass, pass_crypted, datestartvalidity, dateendvalidity, flagdelsessionsbefore";
61 $sql .= " FROM ".$table;
62 $sql .= " WHERE (".$usernamecol1." = '".$db->escape($usertotest)."'";
63 if (preg_match('/@/', $usertotest)) {
64 $sql .= " OR ".$usernamecol2." = '".$db->escape($usertotest)."'";
65 }
66 $sql .= ") AND ".$entitycol." IN (0,".($entity ? ((int) $entity) : 1).")";
67 $sql .= " AND statut = 1";
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 // Note: Test on validity is done later natively with isNotIntoValidityDateRange() by core after calling checkLoginPassEntity() that call this method
73
74 $resql = $db->query($sql);
75 if ($resql) {
76 $obj = $db->fetch_object($resql);
77 if ($obj) {
78 $passclear = $obj->pass;
79 $passcrypted = $obj->pass_crypted;
80 $passtyped = $passwordtotest;
81
82 $passok = false;
83
84 // Check crypted password
85 $cryptType = '';
86 if (!empty($conf->global->DATABASE_PWD_ENCRYPTED)) {
87 $cryptType = $conf->global->DATABASE_PWD_ENCRYPTED;
88 }
89
90 // By default, we use default setup for encryption rule
91 if (!in_array($cryptType, array('auto'))) {
92 $cryptType = 'auto';
93 }
94 // Check crypted password according to crypt algorithm
95 if ($cryptType == 'auto') {
96 if ($passcrypted && dol_verifyHash($passtyped, $passcrypted, '0')) {
97 $passok = true;
98 dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentification ok - hash ".$cryptType." of pass is ok");
99 }
100 }
101
102 // For compatibility with very old versions
103 if (!$passok) {
104 if ((!$passcrypted || $passtyped)
105 && ($passclear && ($passtyped == $passclear))) {
106 $passok = true;
107 dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentification ok - found old pass in database", LOG_WARNING);
108 }
109 }
110
111 // Password ok ?
112 if ($passok) {
113 $login = $obj->login;
114 } else {
115 dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentication KO bad password for '".$usertotest."', cryptType=".$cryptType, LOG_NOTICE);
116 sleep(1); // Anti brut force protection. Must be same delay when login is not valid
117
118 // Load translation files required by the page
119 $langs->loadLangs(array('main', 'errors'));
120
121 $_SESSION["dol_loginmesg"] = $langs->transnoentitiesnoconv("ErrorBadLoginPassword");
122 }
123
124 // We must check entity
125 if ($passok && isModEnabled('multicompany')) { // We must check entity
126 global $mc;
127
128 if (!isset($mc)) {
129 !isModEnabled('multicompany'); // Global not available, disable $conf->multicompany->enabled for safety
130 } else {
131 $ret = $mc->checkRight($obj->rowid, $entitytotest);
132 if ($ret < 0) {
133 dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentication KO entity '".$entitytotest."' not allowed for user '".$obj->rowid."'", LOG_NOTICE);
134
135 $login = ''; // force authentication failure
136 if ($mc->db->lasterror()) {
137 $_SESSION["dol_loginmesg"] = $mc->db->lasterror();
138 }
139 }
140 }
141 }
142 } else {
143 dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentication KO user not found for '".$usertotest."'", LOG_NOTICE);
144 sleep(1); // Anti brut force protection. Must be same delay when password is not valid
145
146 // Load translation files required by the page
147 $langs->loadLangs(array('main', 'errors'));
148
149 $_SESSION["dol_loginmesg"] = $langs->transnoentitiesnoconv("ErrorBadLoginPassword");
150 }
151 } else {
152 dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentication KO db error for '".$usertotest."' error=".$db->lasterror(), LOG_ERR);
153 sleep(1);
154 $_SESSION["dol_loginmesg"] = $db->lasterror();
155 }
156 }
157
158 return $login;
159}
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
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...
dol_verifyHash($chain, $hash, $type='0')
Compute a hash and compare it to the given one For backward compatibility reasons,...