| 1 |
<?php |
| 2 |
|
| 3 |
namespace LAM\INIT; |
| 4 |
|
| 5 |
use htmlButton; |
| 6 |
use htmlJavaScript; |
| 7 |
use htmlOutputText; |
| 8 |
use htmlResponsiveInputField; |
| 9 |
use htmlResponsiveRow; |
| 10 |
use htmlStatusMessage; |
| 11 |
use htmlTitle; |
| 12 |
use LAMCfgMain; |
| 13 |
use LAMException; |
| 14 |
|
| 15 |
/* |
| 16 |
|
| 17 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 18 |
Copyright (C) 2025 Roland Gruber |
| 19 |
|
| 20 |
This program is free software; you can redistribute it and/or modify |
| 21 |
it under the terms of the GNU General Public License as published by |
| 22 |
the Free Software Foundation; either version 2 of the License, or |
| 23 |
(at your option) any later version. |
| 24 |
|
| 25 |
This program is distributed in the hope that it will be useful, |
| 26 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 27 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 28 |
GNU General Public License for more details. |
| 29 |
|
| 30 |
You should have received a copy of the GNU General Public License |
| 31 |
along with this program; if not, write to the Free Software |
| 32 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 33 |
|
| 34 |
*/ |
| 35 |
|
| 36 |
/** |
| 37 |
* Password dialog for initial configuration. |
| 38 |
* |
| 39 |
* @author Roland Gruber |
| 40 |
* @package main |
| 41 |
*/ |
| 42 |
|
| 43 |
/** security functions */ |
| 44 |
include_once(__DIR__ . "/../lib/security.inc"); |
| 45 |
/** access to configuration settings */ |
| 46 |
include_once(__DIR__ . "/../lib/config.inc"); |
| 47 |
/** status messages */ |
| 48 |
include_once(__DIR__ . "/../lib/status.inc"); |
| 49 |
|
| 50 |
// set session save path |
| 51 |
if (isFileBasedSession()) { |
| 52 |
session_save_path(__DIR__ . '/../sess'); |
| 53 |
} |
| 54 |
|
| 55 |
lam_start_session(); |
| 56 |
|
| 57 |
$cfgMain = new LAMCfgMain(); |
| 58 |
if ($cfgMain->hasPasswordSet()) { |
| 59 |
logNewMessage(LOG_ERR, 'Invalid attempt to set initial config password'); |
| 60 |
die(); |
| 61 |
} |
| 62 |
|
| 63 |
setlanguage(); |
| 64 |
|
| 65 |
if (!empty($_POST)) { |
| 66 |
validateSecurityToken(); |
| 67 |
} |
| 68 |
|
| 69 |
$message = null; |
| 70 |
|
| 71 |
// check if user already pressed button |
| 72 |
if (isset($_POST['changePassword'])) { |
| 73 |
// check new password |
| 74 |
$password1 = $_POST['password1']; |
| 75 |
$password2 = $_POST['password2']; |
| 76 |
if ($password1 == '') { |
| 77 |
$message = new htmlStatusMessage('ERROR', _('No password was entered!')); |
| 78 |
printContent($message); |
| 79 |
exit(); |
| 80 |
} |
| 81 |
// check if passwords match |
| 82 |
if ($password1 != $password2) { |
| 83 |
$message = new htmlStatusMessage('ERROR', _('Passwords are different!')); |
| 84 |
printContent($message); |
| 85 |
exit(); |
| 86 |
} |
| 87 |
// check password strength |
| 88 |
$pwdPolicyResult = isValidConfigurationPassword($password1); |
| 89 |
if (!$pwdPolicyResult) { |
| 90 |
$message = new htmlStatusMessage('ERROR', _('Please enter at least 8 characters including letters, a number and a symbol.')); |
| 91 |
printContent($message); |
| 92 |
exit(); |
| 93 |
} |
| 94 |
// set new password |
| 95 |
$cfgMain->setPassword($password1); |
| 96 |
try { |
| 97 |
$cfgMain->save(); |
| 98 |
metaRefresh('config/mainlogin.php'); |
| 99 |
exit(); |
| 100 |
} |
| 101 |
catch (LAMException $e) { |
| 102 |
$message = new htmlStatusMessage('ERROR', $e->getTitle(), $e->getMessage()); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
printContent($message); |
| 107 |
|
| 108 |
/** |
| 109 |
* Displays the content area |
| 110 |
* |
| 111 |
* @param htmlStatusMessage|null $message status message |
| 112 |
* @param bool $showPasswordInputs show password input fields |
| 113 |
*/ |
| 114 |
function printContent(?htmlStatusMessage $message = null, bool $showPasswordInputs = true): void { |
| 115 |
echo ' |
| 116 |
<!DOCTYPE html> |
| 117 |
<html><head> |
| 118 |
<meta name="robots" content="noindex, nofollow"> |
| 119 |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> |
| 120 |
<meta http-equiv="pragma" content="no-cache"> |
| 121 |
<meta http-equiv="cache-control" content="no-cache"> |
| 122 |
'; |
| 123 |
printHeaderContents('LDAP Account Manager', '..'); |
| 124 |
echo '</head> |
| 125 |
<body>'; |
| 126 |
printJsIncludes('..'); |
| 127 |
|
| 128 |
echo '<div class="smallPaddingContent">'; |
| 129 |
echo "<form action=\"setInitialPassword.php\" method=\"post\">\n"; |
| 130 |
addSecurityTokenToSession(); |
| 131 |
$container = new htmlResponsiveRow(); |
| 132 |
if ($message !== null) { |
| 133 |
$container->addVerticalSpacer('1rem'); |
| 134 |
$container->add($message); |
| 135 |
} |
| 136 |
$container->addVerticalSpacer('2rem'); |
| 137 |
if ($showPasswordInputs) { |
| 138 |
$container->add(new htmlTitle(_('Initial configuration'))); |
| 139 |
$container->addVerticalSpacer('3rem'); |
| 140 |
$container->add(new htmlOutputText(_("Please enter your new LAM main configuration password.")), 12, 12, 12, 'text-center'); |
| 141 |
$container->addVerticalSpacer('2rem'); |
| 142 |
$pwdInput1 = new htmlResponsiveInputField(_('New password'), 'password1', ''); |
| 143 |
$pwdInput1->setIsPassword(true, true, true); |
| 144 |
$container->add($pwdInput1); |
| 145 |
$pwdInput2 = new htmlResponsiveInputField(_('Repeat password'), 'password2', ''); |
| 146 |
$pwdInput2->setIsPassword(true); |
| 147 |
$pwdInput2->setSameValueFieldID('password1'); |
| 148 |
$container->add($pwdInput2); |
| 149 |
$container->addVerticalSpacer('1rem'); |
| 150 |
$container->add(new htmlButton('changePassword', _("Submit")), 12, 12, 12, 'text-center'); |
| 151 |
addSecurityTokenToMetaHTML($container); |
| 152 |
$container->add(new htmlJavaScript('checkFieldsHaveSameValues("password1", "password2");')); |
| 153 |
} |
| 154 |
|
| 155 |
parseHtml(null, $container, [], false, 'user'); |
| 156 |
|
| 157 |
echo '</form><br> |
| 158 |
</div> |
| 159 |
<br><br> |
| 160 |
</body> |
| 161 |
</html>'; |
| 162 |
} |