40 if ($mode ==
'user') {
41 $t = getenv(
'APACHE_RUN_USER');
43 if ($mode ==
'group') {
44 $t = getenv(
'APACHE_RUN_GROUP');
64 if ($entitytotest ==
'') {
68 dol_syslog(
"checkLoginPassEntity usertotest=".$usertotest.
" entitytotest=".$entitytotest.
" authmode=".implode(
',', $authmode));
74 foreach ($authmode as $mode) {
75 if ($test && $mode && !$login) {
78 $authfile =
'functions_'.$mode.
'.php';
81 $dirlogin = array_merge(array(
"/core/login"), (array)
$conf->modules_parts[
'login']);
82 foreach ($dirlogin as $reldir) {
87 $tmpnewauthfile = $newdir.(preg_match(
'/\/$/', $newdir) ?
'' :
'/').$authfile;
88 if (is_file($tmpnewauthfile)) {
89 $fullauthfile = $tmpnewauthfile;
95 $result = include_once $fullauthfile;
97 if ($fullauthfile && $result) {
99 $function =
'check_user_password_'.$mode;
100 $login = call_user_func($function, $usertotest, $passwordtotest, $entitytotest,
$context);
101 if ($login && $login !=
'--bad-login-validity--') {
104 $conf->authmode = $mode;
111 dol_syslog(
"Authentication KO - failed to load file '".$authfile.
"'", LOG_ERR);
114 $langs->loadLangs(array(
'other',
'main',
'errors'));
116 $_SESSION[
"dol_loginmesg"] = (empty($_SESSION[
"dol_loginmesg"]) ?
'' : $_SESSION[
"dol_loginmesg"].
', ').$langs->transnoentitiesnoconv(
"ErrorFailedToLoadLoginFileForMode", $mode);
126if (!function_exists(
'dol_loginfunction')) {
138 global $dolibarr_main_demo, $dolibarr_main_force_https;
139 global
$db, $hookmanager;
141 $langs->loadLangs(array(
"main",
"other",
"help",
"admin"));
144 $hookmanager->initHooks(array(
'mainloginpage'));
146 $main_authentication =
$conf->file->main_authentication;
148 $session_name = session_name();
150 $dol_url_root = DOL_URL_ROOT;
153 $appli = constant(
'DOL_APPLICATION_TITLE');
154 $title = $appli.(getDolGlobalString(
'MAIN_OPTIMIZEFORTEXTBROWSER') ?
'' :
' '.constant(
'DOL_VERSION'));
157 if ($customapplication) {
158 if (preg_match(
'/^\+/', $customapplication)) {
159 $title .= $customapplication;
161 $title = $customapplication;
164 $titletruedolibarrversion = constant(
'DOL_VERSION');
186 if (!empty(
$conf->modules_parts[
'tpl'])) {
187 $dirtpls = array_merge(
$conf->modules_parts[
'tpl'], array(
'/core/tpl/'));
188 foreach ($dirtpls as $reldir) {
190 if (file_exists($tmp)) {
191 $template_dir = preg_replace(
'/login\.tpl\.php$/',
'', $tmp);
196 $template_dir = DOL_DOCUMENT_ROOT.
"/core/tpl/";
201 $prefix = dol_getprefix(
'');
202 $sessiontimeout =
'DOLSESSTIMEOUT_'.$prefix;
208 if (
GETPOST(
'urlfrom',
'alpha')) {
209 $_SESSION[
"urlfrom"] =
GETPOST(
'urlfrom',
'alpha');
211 unset($_SESSION[
"urlfrom"]);
214 if (!
GETPOST(
"username",
'alpha')) {
215 $focus_element =
'username';
217 $focus_element =
'password';
222 if (!empty($dolibarr_main_demo)) {
223 $tab = explode(
',', $dolibarr_main_demo);
224 $demologin = $tab[0];
225 $demopassword = $tab[1];
229 $parameters = array(
'entity' =>
GETPOSTINT(
'entity'),
'switchentity' =>
GETPOSTINT(
'switchentity'));
230 $reshook = $hookmanager->executeHooks(
'getLoginPageOptions', $parameters);
231 $morelogincontent = $hookmanager->resPrint;
234 $parameters = array(
'entity' =>
GETPOSTINT(
'entity'),
'switchentity' =>
GETPOSTINT(
'switchentity'));
235 $reshook = $hookmanager->executeHooks(
'getLoginPageExtraOptions', $parameters);
236 $moreloginextracontent = $hookmanager->resPrint;
239 $parameters = array(
'entity' =>
GETPOSTINT(
'entity'),
'switchentity' =>
GETPOSTINT(
'switchentity'));
240 $reshook = $hookmanager->executeHooks(
'redirectAfterConnection', $parameters);
241 $php_self = $hookmanager->resPrint;
244 $login = (!empty($hookmanager->resArray[
'username']) ? $hookmanager->resArray[
'username'] : (
GETPOST(
"username",
"alpha") ?
GETPOST(
"username",
"alpha") : $demologin));
245 $password = $demopassword;
249 $urllogo = DOL_URL_ROOT.
'/theme/common/login_logo.png';
251 if (!empty(
$mysoc->logo_small) && is_readable(
$conf->mycompany->dir_output.
'/logos/thumbs/'.
$mysoc->logo_small)) {
252 $urllogo = DOL_URL_ROOT.
'/viewimage.php?cache=1&modulepart=mycompany&file='.urlencode(
'logos/thumbs/'.
$mysoc->logo_small);
253 } elseif (!empty(
$mysoc->logo) && is_readable(
$conf->mycompany->dir_output.
'/logos/'.
$mysoc->logo)) {
254 $urllogo = DOL_URL_ROOT.
'/viewimage.php?cache=1&modulepart=mycompany&file='.urlencode(
'logos/'.
$mysoc->logo);
256 } elseif (!empty(
$mysoc->logo_squarred_small) && is_readable(
$conf->mycompany->dir_output.
'/logos/thumbs/'.
$mysoc->logo_squarred_small)) {
257 $urllogo = DOL_URL_ROOT.
'/viewimage.php?cache=1&modulepart=mycompany&file='.urlencode(
'logos/thumbs/'.
$mysoc->logo_squarred_small);
258 } elseif (is_readable(DOL_DOCUMENT_ROOT.
'/theme/dolibarr_logo.svg')) {
259 $urllogo = DOL_URL_ROOT.
'/theme/dolibarr_logo.svg';
284 $substitutionarray = getCommonSubstitutionArray($langs);
295 $dol_loginmesg = (!empty($_SESSION[
"dol_loginmesg"]) ? $_SESSION[
"dol_loginmesg"] :
'');
297 $favicon = DOL_URL_ROOT.
'/theme/dolibarr_256x256_color.png';
298 if (!empty(
$mysoc->logo_squarred_mini)) {
299 $favicon = DOL_URL_ROOT.
'/viewimage.php?cache=1&modulepart=mycompany&file='.urlencode(
'logos/thumbs/'.
$mysoc->logo_squarred_mini);
305 $jquerytheme =
'base';
311 $dol_hide_topmenu =
GETPOSTINT(
'dol_hide_topmenu');
312 $dol_hide_leftmenu =
GETPOSTINT(
'dol_hide_leftmenu');
313 $dol_optimize_smallscreen =
GETPOSTINT(
'dol_optimize_smallscreen');
314 $dol_no_mouse_hover =
GETPOSTINT(
'dol_no_mouse_hover');
315 $dol_use_jmobile =
GETPOSTINT(
'dol_use_jmobile');
318 include $template_dir.
'login.tpl.php';
323 $_SESSION[
"dol_loginmesg"] =
'';
358 $salt .= chr(mt_rand(64, 126));
361 $result = $saltprefix.$salt.$saltsuffix;
374 dol_syslog(
"encodedecode_dbpassconf level=".$level, LOG_DEBUG);
377 $passwd_crypted =
'';
379 if ($fp = fopen(DOL_DOCUMENT_ROOT.
'/conf/conf.php',
'r')) {
381 $buffer = fgets($fp, 4096);
387 if (preg_match(
'/^[^#]*dolibarr_main_db_encrypted_pass[\s]*=[\s]*(.*)/i', $buffer, $reg)) {
388 $val = trim($reg[1]);
389 $val = preg_replace(
'/^["\']/',
'', $val);
390 $val = preg_replace(
'/["\'][\s;]*$/',
'', $val);
392 $passwd_crypted = $val;
399 } elseif (preg_match(
'/^[^#]*dolibarr_main_db_pass[\s]*=[\s]*(.*)/i', $buffer, $reg)) {
400 $val = trim($reg[1]);
401 $val = preg_replace(
'/^["\']/',
'', $val);
402 $val = preg_replace(
'/["\'][\s;]*$/',
'', $val);
403 if (preg_match(
'/crypted:/i', $buffer)) {
406 $val = preg_replace(
'/crypted:/i',
'', $val);
407 $passwd_crypted = $val;
410 } elseif (preg_match(
'/^dolcrypt:([^:]+):(.*)$/i', $val, $reg)) {
414 $passwd_crypted = $reg[1].
':'.$reg[2];
424 $passwd_crypted = preg_replace(
'/^dolcrypt:/',
'',
dolEncrypt($val));
433 $config .=
'$dolibarr_main_db_pass=\''.$passwd.
'\';
'."\n";
436 $config .= '$dolibarr_main_db_pass=\
''.$mode.$passwd_crypted.
'\';
'."\n";
439 //print 'passwd =
'.$passwd.' - passwd_crypted =
'.$passwd_crypted;
447 // Write new conf file
448 $file = DOL_DOCUMENT_ROOT.'/
conf/
conf.php
';
449 if ($fp = @fopen($file, 'w
')) {
450 fwrite($fp, $config);
455 // It's config file, so we
set read permission
for creator only.
461 dol_syslog(
"encodedecode_dbpassconf Failed to open conf.php file for writing", LOG_WARNING);
465 dol_syslog(
"encodedecode_dbpassconf Failed to read conf.php", LOG_ERR);
483 $generated_password =
'';
486 $lowercase =
"qwertyuiopasdfghjklzxcvbnm";
487 $uppercase =
"ASDFGHJKLZXCVBNMQWERTYUIOP";
488 $numbers =
"1234567890";
490 $nbofchar = round($length / 3);
491 $nbofcharlast = ($length - 2 * $nbofchar);
493 if (function_exists(
'random_int')) {
494 $max = strlen($lowercase) - 1;
495 for ($x = 0; $x < $nbofchar; $x++) {
496 $tmp = random_int(0, $max);
497 $randomCode .= $lowercase[$tmp];
499 $max = strlen($uppercase) - 1;
500 for ($x = 0; $x < $nbofchar; $x++) {
501 $tmp = random_int(0, $max);
502 $randomCode .= $uppercase[$tmp];
504 $max = strlen($numbers) - 1;
505 for ($x = 0; $x < $nbofcharlast; $x++) {
506 $tmp = random_int(0, $max);
507 $randomCode .= $numbers[$tmp];
511 $max = strlen($lowercase) - 1;
512 for ($x = 0; $x < $nbofchar; $x++) {
513 $tmp = mt_rand(0, $max);
514 $randomCode .= $lowercase[$tmp];
516 $max = strlen($uppercase) - 1;
517 for ($x = 0; $x < $nbofchar; $x++) {
518 $tmp = mt_rand(0, $max);
519 $randomCode .= $uppercase[$tmp];
521 $max = strlen($numbers) - 1;
522 for ($x = 0; $x < $nbofcharlast; $x++) {
523 $tmp = mt_rand(0, $max);
524 $randomCode .= $numbers[$tmp];
529 $passwordArray = str_split($randomCode);
530 for ($i = count($passwordArray) - 1; $i > 0; $i--) {
531 $j = random_int(0, $i);
532 $tmp = $passwordArray[$i];
533 $passwordArray[$i] = $passwordArray[$j];
534 $passwordArray[$j] = $tmp;
536 $generated_password = implode(
'', $passwordArray);
539 $nomfichier = $nomclass.
".class.php";
541 require_once DOL_DOCUMENT_ROOT.
"/core/modules/security/generate/".$nomfichier;
542 $genhandler =
new $nomclass(
$db,
$conf, $langs, $user);
543 '@phan-var-force ModeleGenPassword $genhandler';
544 $generated_password = $genhandler->getNewGeneratedPassword();
549 if (is_array($replaceambiguouschars) && count($replaceambiguouschars) > 0) {
551 $max = strlen($numbers) - 1;
552 if (function_exists(
'random_int')) {
553 $tmp = random_int(0, $max);
554 $generated_password = str_replace($replaceambiguouschars, $numbers[$tmp], $generated_password);
556 $tmp = mt_rand(0, $max);
557 $generated_password = str_replace($replaceambiguouschars, $numbers[$tmp], $generated_password);
561 return $generated_password;
579 if (!empty(
$conf->use_javascript_ajax)) {
580 $out .=
"\n".
'<!-- Js code to suggest a security key -->';
581 $out .=
'<script nonce="'.getNonce().
'" type="text/javascript">';
582 $out .=
'jQuery(document).ready(function () {
583 jQuery("#'.dol_escape_js($htmlnameofbutton).
'").click(function() {
584 var currenttoken = jQuery("meta[name=anti-csrf-currenttoken]").attr("content");
585 console.log("dolJSToSetRandomPassword: We click on the button '.
dol_escape_js($htmlnameofbutton).
' to suggest a key. anti-csrf-currenttoken is "+currenttoken+". We will fill '.
dol_escape_js($htmlname).
'");
586 jQuery.get( "'.DOL_URL_ROOT.
'/core/ajax/security.php", {
587 action: \'getrandompassword\',
588 generic: '.($generic ?
'1' :
'0').
',
592 if (jQuery("input#'.
dol_escape_js($htmlname).
'").attr("type") == "password") {
593 jQuery("input#'.
dol_escape_js($htmlname).
'").attr("type", "text");
614 return '<!-- code to manage the eye hide/show -->
615<span id="'.$htmlname.
'" tabindex="-1"><span class="fa fa-eye"></span></span>
616<script nonce="'.getNonce().
'">
617 $(document).ready(function () {
618 $(\'#'.$htmlname.
'\').on(\
'click\', function (e) {
620 if (event.detail === 0) return false; // Ignore keyboard "clicks"
621 console.log("We click on '.$htmlname.
'");
622 const $passwordInput = $(\'#'.$htmlnameofinput.
'\');
624 if ($passwordInput.is(\
'[type=password]\')) {
625 $passwordInput.attr(\'type\', \'text\');
626 jQuery(\'#'.$htmlname.
' .fa-eye\').attr(\'class\', \'fa fa-eye-slash\');
628 $passwordInput.attr(\'type\', \'password\');
629 jQuery(\'#'.$htmlname.
' .fa-eye-slash\').attr(\'class\', \'fa fa-eye\');
632 return false; // This prevents the click from reloading the page
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dolSetCookie(string $cookiename, string $cookievalue, int $expire=-1)
Set a cookie.
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into JavaScript code.
dol_htmlcleanlastbr($stringtodecode)
This function remove all ending and br at end.
dol_htmloutput_events($disabledoutputofmessages=0)
Print formatted messages to output (Used to show messages on html output).
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the "subst...
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
conf($dolibarr_main_document_root)
Load conf file (file must exists)
$context
@method int call_trigger(string $triggerName, ?User $user)
showEyeForField($htmlname, $htmlnameofinput)
Output the eye picto to show/hide a password HTML field.
dolJSToSetRandomPassword($htmlname, $htmlnameofbutton='generate_token', $generic=1)
Output javascript to autoset a generated password using default module into a HTML element.
dol_getwebuser($mode)
Return user/group account of web server.
encodedecode_dbpassconf($level=0)
Encode or decode database password in config file.
checkLoginPassEntity($usertotest, $passwordtotest, $entitytotest, $authmode, $context='')
Return a login if login/pass was successful.
getRandomPassword($generic=false, $replaceambiguouschars=null, $length=32)
Return a generated password using default module.
if(!function_exists( 'dol_loginfunction')) makesalt($type=CRYPT_SALT_LENGTH)
Initialise the salt for the crypt function.
dol_decode($chain, $key='1')
Decode a base 64 encoded + specific delta change.
dolDecrypt($chain, $key='', $patterntotest='')
Decode a string with a symmetric encryption.
dolEncrypt($chain, $key='', $ciphering='', $forceseed='', $obfuscationmode='dolcrypt')
Encode a string with a symmetric encryption.