| 1 |
<?php |
| 2 |
namespace LAM\CONFIG; |
| 3 |
use htmlButton; |
| 4 |
use htmlGroup; |
| 5 |
use htmlInputFileUpload; |
| 6 |
use htmlLink; |
| 7 |
use htmlOutputText; |
| 8 |
use htmlResponsiveInputCheckbox; |
| 9 |
use htmlResponsiveInputField; |
| 10 |
use htmlResponsiveRow; |
| 11 |
use htmlStatusMessage; |
| 12 |
use htmlSubTitle; |
| 13 |
use LAM\PERSISTENCE\ConfigDataExporter; |
| 14 |
use LAM\PERSISTENCE\ConfigDataImporter; |
| 15 |
use LAMCfgMain; |
| 16 |
use LAMException; |
| 17 |
use ZipArchive; |
| 18 |
|
| 19 |
/* |
| 20 |
|
| 21 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 22 |
Copyright (C) 2020 - 2026 Roland Gruber |
| 23 |
|
| 24 |
This program is free software; you can redistribute it and/or modify |
| 25 |
it under the terms of the GNU General Public License as published by |
| 26 |
the Free Software Foundation; either version 2 of the License, or |
| 27 |
(at your option) any later version. |
| 28 |
|
| 29 |
This program is distributed in the hope that it will be useful, |
| 30 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 31 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 32 |
GNU General Public License for more details. |
| 33 |
|
| 34 |
You should have received a copy of the GNU General Public License |
| 35 |
along with this program; if not, write to the Free Software |
| 36 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 37 |
|
| 38 |
*/ |
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* Import and export functions for LAM configuration. |
| 43 |
* |
| 44 |
* @package configuration |
| 45 |
* @author Roland Gruber |
| 46 |
*/ |
| 47 |
|
| 48 |
|
| 49 |
/** Access to persistence functions */ |
| 50 |
include_once(__DIR__ . '/../../lib/persistence.inc'); |
| 51 |
/** account modules */ |
| 52 |
include_once(__DIR__ . '/../../lib/modules.inc'); |
| 53 |
|
| 54 |
// start session |
| 55 |
if (isFileBasedSession()) { |
| 56 |
session_save_path("../../sess"); |
| 57 |
} |
| 58 |
lam_start_session(); |
| 59 |
|
| 60 |
setlanguage(); |
| 61 |
|
| 62 |
if (!isset($_SESSION['cfgMain'])) { |
| 63 |
$cfg = new LAMCfgMain(); |
| 64 |
$_SESSION['cfgMain'] = $cfg; |
| 65 |
} |
| 66 |
$cfg = &$_SESSION['cfgMain']; |
| 67 |
|
| 68 |
// export |
| 69 |
if (isset($_POST['exportConfig']) && $cfg->checkPassword($_SESSION["mainconf_password"])) { |
| 70 |
$exporter = new ConfigDataExporter(); |
| 71 |
if (!headers_sent()) { |
| 72 |
header('Content-Type: application/zip; charset=utf-8'); |
| 73 |
header('Content-disposition: attachment; filename=lam-config.zip'); |
| 74 |
} |
| 75 |
try { |
| 76 |
$zip = new ZipArchive(); |
| 77 |
$zipTmpFile = tmpfile(); |
| 78 |
if ($zipTmpFile === false) { |
| 79 |
throw new LAMException(_('Unable to create temporary file.')); |
| 80 |
} |
| 81 |
$metaData = stream_get_meta_data($zipTmpFile); |
| 82 |
if (empty($metaData['uri'])) { |
| 83 |
throw new LAMException(_('Unable to create temporary file.')); |
| 84 |
} |
| 85 |
$zipFile = $metaData['uri']; |
| 86 |
fclose($zipTmpFile); |
| 87 |
$zip->open($zipFile, ZipArchive::CREATE); |
| 88 |
$json = $exporter->exportAsJson(); |
| 89 |
$zip->addFromString('lam-config.json', $json); |
| 90 |
$zip->close(); |
| 91 |
$handle = fopen($zipFile, "r"); |
| 92 |
if ($handle === false) { |
| 93 |
throw new LAMException(_('Unable to create temporary file.')); |
| 94 |
} |
| 95 |
$fileSize = filesize($zipFile); |
| 96 |
if (($fileSize === false) || ($fileSize <= 0)) { |
| 97 |
throw new LAMException(_('Unable to create temporary file.')); |
| 98 |
} |
| 99 |
$contents = fread($handle, $fileSize); |
| 100 |
fclose($handle); |
| 101 |
unlink($zipFile); |
| 102 |
echo $contents; |
| 103 |
} |
| 104 |
catch (LAMException $e) { |
| 105 |
logNewMessage(LOG_ERR, $e->getTitle() . ' ' . $e->getMessage()); |
| 106 |
} |
| 107 |
exit; |
| 108 |
} |
| 109 |
|
| 110 |
echo $_SESSION['header']; |
| 111 |
printHeaderContents(_("Import and export configuration"), '../..'); |
| 112 |
|
| 113 |
?> |
| 114 |
</head> |
| 115 |
<body> |
| 116 |
<?php |
| 117 |
// include all JavaScript files |
| 118 |
printJsIncludes('../..'); |
| 119 |
?> |
| 120 |
<div id="lam-topnav" class="lam-header"> |
| 121 |
<div class="lam-header-left lam-menu-stay"> |
| 122 |
<a href="https://www.ldap-account-manager.org/" target="new_window"> |
| 123 |
<img class="align-middle" width="24" height="24" alt="help" src="../../graphics/logo.svg"> |
| 124 |
<span class="hide-on-mobile"> |
| 125 |
<?php |
| 126 |
echo getLAMVersionText(); |
| 127 |
?> |
| 128 |
</span> |
| 129 |
</a> |
| 130 |
</div> |
| 131 |
<?php |
| 132 |
if (is_dir(__DIR__ . '/../../docs/manual')) { |
| 133 |
?> |
| 134 |
<a class="lam-header-right lam-menu-icon hide-on-tablet" href="javascript:void(0);" class="icon" onclick="window.lam.topmenu.toggle();"> |
| 135 |
<img class="align-middle" width="16" height="16" alt="menu" src="../../graphics/menu.svg"> |
| 136 |
<span class="padding0"> </span> |
| 137 |
</a> |
| 138 |
<a class="lam-header-right lam-menu-entry" target="_blank" href="../../docs/manual/index.html"> |
| 139 |
<span class="padding0"> <?php echo _("Help") ?></span> |
| 140 |
</a> |
| 141 |
<?php |
| 142 |
} |
| 143 |
?> |
| 144 |
</div> |
| 145 |
<form action="confImportExport.php" method="post" autocomplete="off" enctype="multipart/form-data"> |
| 146 |
<br><br> |
| 147 |
<?php |
| 148 |
|
| 149 |
// check if user is logged in |
| 150 |
if (!isset($_POST['submitLogin']) && !isset($_SESSION["mainconf_password"])) { |
| 151 |
showLoginDialog(); |
| 152 |
exit(); |
| 153 |
} |
| 154 |
|
| 155 |
// check login |
| 156 |
if (isset($_POST['submitLogin']) && !checkLogin($cfg)) { |
| 157 |
exit(); |
| 158 |
} |
| 159 |
|
| 160 |
displayImportExport(); |
| 161 |
|
| 162 |
/** |
| 163 |
* Shows the login dialog for the configuration master password. |
| 164 |
* |
| 165 |
* @param htmlStatusMessage $message message to show if any error occurred |
| 166 |
*/ |
| 167 |
function showLoginDialog($message = null): void { |
| 168 |
$content = new htmlResponsiveRow(); |
| 169 |
$loginContent = new htmlResponsiveRow(); |
| 170 |
$loginContent->setCSSClasses(['maxrow fullwidth roundedShadowBox spacing5']); |
| 171 |
if ($message !== null) { |
| 172 |
$loginContent->add($message, 12); |
| 173 |
} |
| 174 |
$pwdInput = new htmlResponsiveInputField(_("Master password"), 'password', '', '236'); |
| 175 |
$pwdInput->setIsPassword(true); |
| 176 |
$pwdInput->setCSSClasses(['lam-initial-focus']); |
| 177 |
$loginContent->add($pwdInput, 12); |
| 178 |
$loginContent->addLabel(new htmlOutputText(' ', false)); |
| 179 |
$loginButton = new htmlButton('submitLogin', _("Ok")); |
| 180 |
$loginButton->setCSSClasses(['lam-primary']); |
| 181 |
$loginContent->addField($loginButton); |
| 182 |
|
| 183 |
$content->add($loginContent, 12); |
| 184 |
|
| 185 |
parseHtml(null, $content, [], false, null); |
| 186 |
renderBackLink(); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Renders the link back to login page. |
| 191 |
*/ |
| 192 |
function renderBackLink(): void { |
| 193 |
$content = new htmlResponsiveRow(); |
| 194 |
$content->addVerticalSpacer('2rem'); |
| 195 |
$content->add(new htmlLink(_('Back to login'), '../login.php'), 12); |
| 196 |
$content->addVerticalSpacer('1rem'); |
| 197 |
parseHtml(null, $content, [], false, null); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Checks the login password. |
| 202 |
* |
| 203 |
* @param LAMCfgMain $cfg main config |
| 204 |
* @return bool login ok |
| 205 |
*/ |
| 206 |
function checkLogin($cfg): bool { |
| 207 |
$password = $_POST['password']; |
| 208 |
if ($cfg->checkPassword($password)) { |
| 209 |
$_SESSION["mainconf_password"] = $password; |
| 210 |
return true; |
| 211 |
} |
| 212 |
showLoginDialog(new htmlStatusMessage('ERROR', _('The password is invalid! Please try again.'))); |
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Displays the import/export functions. |
| 218 |
*/ |
| 219 |
function displayImportExport(): void { |
| 220 |
$content = new htmlResponsiveRow(); |
| 221 |
|
| 222 |
$content->add(new htmlSubTitle(_('Export')), 12); |
| 223 |
$exportButton = new htmlButton('exportConfig', _('Export')); |
| 224 |
$exportButton->setCSSClasses(['lam-primary']); |
| 225 |
$content->add($exportButton); |
| 226 |
|
| 227 |
$content->add(new htmlSubTitle(_('Import')), 12); |
| 228 |
renderImportPart($content); |
| 229 |
|
| 230 |
parseHtml(null, $content, [], false, null); |
| 231 |
renderBackLink(); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Renders the import area. |
| 236 |
* |
| 237 |
* @param htmlResponsiveRow $content content where to add import part |
| 238 |
*/ |
| 239 |
function renderImportPart($content): void { |
| 240 |
$validUpload = false; |
| 241 |
$importSteps = []; |
| 242 |
if (isset($_POST['importConfig'])) { |
| 243 |
try { |
| 244 |
if (empty($_FILES['import-file']['tmp_name'])) { |
| 245 |
throw new LAMException(_('The file you uploaded is too large. Please check php.ini, upload_max_filesize setting.')); |
| 246 |
} |
| 247 |
$uploadFileName = $_FILES['import-file']['name']; |
| 248 |
$tmpFileName = $_FILES['import-file']['tmp_name']; |
| 249 |
if (preg_match('/\\.zip$/', $uploadFileName)) { |
| 250 |
$zip = new ZipArchive(); |
| 251 |
$zip->open($tmpFileName); |
| 252 |
$data = $zip->getFromName('lam-config.json'); |
| 253 |
if ($data === false) { |
| 254 |
throw new LAMException(_('Unable to read import file.')); |
| 255 |
} |
| 256 |
$zip->close(); |
| 257 |
} |
| 258 |
else { |
| 259 |
$handle = fopen($tmpFileName, "r"); |
| 260 |
if ($handle === false) { |
| 261 |
throw new LAMException(_('Unable to read import file.')); |
| 262 |
} |
| 263 |
$data = fread($handle, 100_000_000); |
| 264 |
if ($data === false) { |
| 265 |
throw new LAMException(_('Unable to read import file.')); |
| 266 |
} |
| 267 |
fclose($handle); |
| 268 |
} |
| 269 |
$importer = new ConfigDataImporter(); |
| 270 |
$importSteps = $importer->getPossibleImportSteps($data); |
| 271 |
$tmpFile = __DIR__ . '/../../tmp/internal/import_' . generateRandomText() . '.tmp'; |
| 272 |
$file = @fopen($tmpFile, "w"); |
| 273 |
if ($file) { |
| 274 |
fwrite($file, $data); |
| 275 |
fclose($file); |
| 276 |
chmod($tmpFile, 0600); |
| 277 |
} |
| 278 |
$_SESSION['configImportFile'] = $tmpFile; |
| 279 |
$validUpload = true; |
| 280 |
} |
| 281 |
catch (LAMException $e) { |
| 282 |
$content->add(new htmlStatusMessage('ERROR', htmlspecialchars($e->getTitle()), htmlspecialchars($e->getMessage()))); |
| 283 |
} |
| 284 |
} |
| 285 |
if (!isset($_POST['importConfigConfirm']) && !$validUpload) { |
| 286 |
$content->add(new htmlInputFileUpload('import-file'), 12); |
| 287 |
$submitButton = new htmlButton('importConfig', _('Submit')); |
| 288 |
$submitButton->setCSSClasses(['lam-secondary']); |
| 289 |
$content->add($submitButton); |
| 290 |
} |
| 291 |
elseif (isset($_POST['importConfig'])) { |
| 292 |
$content->add(new htmlOutputText(_('Import steps')), 12); |
| 293 |
foreach ($importSteps as $importStep) { |
| 294 |
$stepKey = 'step_' . $importStep->getKey(); |
| 295 |
$stepCheckbox = new htmlResponsiveInputCheckbox($stepKey, true, $importStep->getLabel()); |
| 296 |
$stepCheckbox->setLabelAfterCheckbox(); |
| 297 |
$stepCheckbox->setCSSClasses(['bold']); |
| 298 |
$subStepIds = []; |
| 299 |
$content->add($stepCheckbox); |
| 300 |
$content->addVerticalSpacer('0.3rem'); |
| 301 |
foreach ($importStep->getSubSteps() as $subStep) { |
| 302 |
$subStepKey = 'step_' . $subStep->getKey(); |
| 303 |
$subStepIds[] = $subStepKey; |
| 304 |
$subStepCheckbox = new htmlResponsiveInputCheckbox($subStepKey, true, $subStep->getLabel()); |
| 305 |
$subStepCheckbox->setLabelAfterCheckbox(); |
| 306 |
$content->add($subStepCheckbox); |
| 307 |
} |
| 308 |
$stepCheckbox->setTableRowsToShow($subStepIds); |
| 309 |
$content->addVerticalSpacer('1rem'); |
| 310 |
} |
| 311 |
$buttonGroup = new htmlGroup(); |
| 312 |
$importButton = new htmlButton('importConfigConfirm', _('Import')); |
| 313 |
$importButton->setCSSClasses(['lam-secondary']); |
| 314 |
$buttonGroup->addElement($importButton); |
| 315 |
$buttonGroup->addElement(new htmlButton('importCancel', _('Cancel'))); |
| 316 |
$content->add($buttonGroup); |
| 317 |
} |
| 318 |
elseif (isset($_POST['importConfigConfirm'])) { |
| 319 |
try { |
| 320 |
$handle = fopen($_SESSION['configImportFile'], "r"); |
| 321 |
if ($handle === false) { |
| 322 |
throw new LAMException(_('Unable to read import file.')); |
| 323 |
} |
| 324 |
$data = fread($handle, 100_000_000); |
| 325 |
if ($data === false) { |
| 326 |
throw new LAMException(_('Unable to read import file.')); |
| 327 |
} |
| 328 |
fclose($handle); |
| 329 |
$importer = new ConfigDataImporter(); |
| 330 |
$importSteps = $importer->getPossibleImportSteps($data); |
| 331 |
foreach ($importSteps as $importStep) { |
| 332 |
$importStep->setActive(isset($_POST['step_' . $importStep->getKey()])); |
| 333 |
foreach ($importStep->getSubSteps() as $subStep) { |
| 334 |
$subStep->setActive(isset($_POST['step_' . $subStep->getKey()])); |
| 335 |
} |
| 336 |
} |
| 337 |
$importer->runImport($importSteps); |
| 338 |
unlink($_SESSION['configImportFile']); |
| 339 |
$content->add(new htmlStatusMessage('INFO', _('Configuration import ended successful.'))); |
| 340 |
$content->add(new htmlButton('importNew', _('New import'))); |
| 341 |
} |
| 342 |
catch (LAMException $e) { |
| 343 |
$content->add(new htmlStatusMessage('ERROR', htmlspecialchars($e->getTitle()), htmlspecialchars($e->getMessage()))); |
| 344 |
$content->add(new htmlButton('importCancel', _('Back'))); |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
?> |
| 350 |
</form> |
| 351 |
</body> |
| 352 |
</html> |