LDAPAccountManager/lam/lib/persistence.inc

653 lines
18 KiB
PHP
Raw Normal View History

2020-04-12 10:39:52 +00:00
<?php
namespace LAM\PERSISTENCE;
2020-05-13 18:47:12 +00:00
use LAM\PDF\PDFStructure;
2020-05-12 18:58:56 +00:00
use LAM\PDF\PDFStructureReader;
2020-05-13 18:47:12 +00:00
use LAM\PDF\PDFStructureWriter;
2020-04-12 19:51:19 +00:00
use LAMCfgMain;
2020-04-19 18:40:40 +00:00
use LAMConfig;
2020-04-12 19:51:19 +00:00
use LAMException;
2020-06-01 08:52:55 +00:00
use selfServiceProfile;
2020-05-12 18:58:56 +00:00
use function LAM\PDF\getAvailableLogos;
use function LAM\PDF\getPDFStructures;
2020-05-30 10:35:22 +00:00
use function LAM\PDF\getPdfTemplateLogoBinary;
use function LAM\PDF\getPdfTemplateLogoNames;
use function LAM\PDF\getPdfTemplateNames;
2020-05-13 18:47:12 +00:00
use function LAM\PDF\uploadPDFLogo;
2020-05-03 08:32:35 +00:00
use function LAM\PROFILES\getAccountProfiles;
use function LAM\PROFILES\getProfileTemplateNames;
use function LAM\PROFILES\installTemplateAccountProfile;
2020-05-03 08:32:35 +00:00
use function LAM\PROFILES\loadAccountProfile;
use function LAM\PROFILES\loadTemplateAccountProfile;
2020-05-03 08:32:35 +00:00
use function LAM\PROFILES\saveAccountProfile;
2020-04-12 19:51:19 +00:00
2020-04-12 10:39:52 +00:00
/*
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2020 Roland Gruber
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* This file includes functions to manage the persistence of LAM's configuration files.
*
* @package configuration
* @author Roland Gruber
*/
include_once __DIR__ . '/config.inc';
2020-05-03 08:32:35 +00:00
include_once __DIR__ . '/profiles.inc';
2020-04-12 10:39:52 +00:00
/**
2020-04-12 19:51:19 +00:00
* Exporter for LAM's configuration data.
2020-04-12 10:39:52 +00:00
*/
class ConfigDataExporter {
/**
* Exports LAM's configuration data in JSON format.
* @throws LAMException error during export
2020-04-12 10:39:52 +00:00
*/
public function exportAsJson() {
2020-04-13 13:40:33 +00:00
$mainCfg = $this->_getMainConfiguration();
2020-04-12 10:39:52 +00:00
$jsonData = array();
2020-04-13 13:40:33 +00:00
$jsonData['mainConfig'] = $this->_getMainConfigData($mainCfg);
$jsonData['certificates'] = $this->_getCertificates($mainCfg);
2020-05-03 08:32:35 +00:00
$serverProfileNames = getConfigProfiles();
$serverProfiles = array();
foreach ($serverProfileNames as $serverProfileName) {
$serverProfiles[$serverProfileName] = new \LAMConfig($serverProfileName);
}
$jsonData['serverProfiles'] = $this->_getServerProfiles($serverProfiles);
$jsonData['accountProfiles'] = $this->_getAccountProfiles($serverProfiles);
2020-05-30 10:35:22 +00:00
$jsonData['accountProfileTemplates'] = $this->_getAccountProfileTemplates();
2020-05-12 18:58:56 +00:00
$jsonData['pdfProfiles'] = $this->_getPdfProfiles($serverProfiles);
2020-05-30 10:35:22 +00:00
$jsonData['pdfProfileTemplates'] = $this->_getPdfProfileTemplates();
2020-06-01 08:52:55 +00:00
$jsonData['selfServiceProfiles'] = $this->_getSelfServiceProfiles();
2020-04-13 13:40:33 +00:00
/**
* TODO
*
2020-04-19 18:40:40 +00:00
* webauthn
2020-04-13 13:40:33 +00:00
*/
2020-04-12 10:39:52 +00:00
return json_encode($jsonData);
}
2020-04-13 13:40:33 +00:00
/**
* Returns the main configuration.
*
* @return LAMCfgMain main config
*/
public function _getMainConfiguration() {
return new LAMCfgMain();
}
2020-04-12 10:39:52 +00:00
/**
* Internal function to read master configuration.
*
2020-04-13 13:40:33 +00:00
* @param LAMCfgMain $mainCfg main config
2020-04-12 10:39:52 +00:00
* @return array data
*/
2020-04-13 13:40:33 +00:00
public function _getMainConfigData($mainCfg) {
2020-04-12 10:39:52 +00:00
return $mainCfg->exportData();
}
2020-04-13 13:40:33 +00:00
/**
* Returns the certificate file content.
*
* @param LAMCfgMain $mainCfg main config
* @return array data
*/
public function _getCertificates($mainCfg) {
return $mainCfg->exportCertificates();
}
2020-04-19 18:40:40 +00:00
/**
* Returns the content of the server profiles.
*
2020-05-03 08:32:35 +00:00
* @param array $serverProfiles list of server profiles (name => object)
2020-04-19 18:40:40 +00:00
* @return array $data
*/
2020-05-03 08:32:35 +00:00
public function _getServerProfiles($serverProfiles) {
2020-04-19 18:40:40 +00:00
$data = array();
2020-05-03 08:32:35 +00:00
foreach ($serverProfiles as $profileName => $serverProfile) {
2020-04-19 18:40:40 +00:00
$data[$profileName] = $serverProfile->exportData();
}
return $data;
}
2020-05-03 08:32:35 +00:00
/**
* Returns the content of the account profiles.
*
* @param array $serverProfiles list of server profiles (name => object)
* @return array $data
*/
public function _getAccountProfiles($serverProfiles) {
$data = array();
foreach ($serverProfiles as $profileName => $serverProfile) {
foreach ($serverProfile->get_ActiveTypes() as $typeId) {
$accountProfileNames = getAccountProfiles($typeId, $profileName);
foreach ($accountProfileNames as $accountProfileName) {
$accountProfile = loadAccountProfile($accountProfileName, $typeId, $profileName);
$data[$profileName][$typeId][$accountProfileName] = $accountProfile;
}
}
}
return $data;
}
/**
* Returns the content of the account profile templates.
*
* @return array $data
* @throws LAMException error reading template
*/
2020-05-30 10:35:22 +00:00
public function _getAccountProfileTemplates() {
$data = array();
$accountProfileTemplateNames = getProfileTemplateNames();
foreach ($accountProfileTemplateNames as $scope => $templateNames) {
foreach ($templateNames as $templateName) {
$accountProfileTemplate = loadTemplateAccountProfile($templateName, $scope);
$data[$scope][$templateName] = $accountProfileTemplate;
}
}
return $data;
}
2020-05-12 18:58:56 +00:00
/**
* Returns the content of the PDF profiles.
*
* @param array $serverProfiles list of server profiles (name => object)
* @return array $data
*/
public function _getPdfProfiles($serverProfiles) {
$data = array();
foreach ($serverProfiles as $profileName => $serverProfile) {
foreach ($serverProfile->get_ActiveTypes() as $typeId) {
$pdfProfileNames = getPDFStructures($typeId, $profileName);
$reader = new PDFStructureReader($profileName);
foreach ($pdfProfileNames as $pdfProfileName) {
$pdfStructure = $reader->read($typeId, $pdfProfileName);
$data[$profileName]['structures'][$typeId][$pdfProfileName] = $pdfStructure->export();
}
}
$logoData = getAvailableLogos($profileName);
foreach ($logoData as $logo) {
$logoFileName = $logo['filename'];
$logoPath = __DIR__ . '/../config/pdf/' . $profileName . '/logos/' . $logoFileName;
$handle = fopen($logoPath, 'r');
$logoBinary = fread($handle, 100000000);
fclose($handle);
$data[$profileName]['logos'][$logoFileName] = base64_encode($logoBinary);
}
}
return $data;
}
2020-05-30 10:35:22 +00:00
/**
* Returns the content of the account profile templates.
*
* @return array $data
* @throws LAMException error reading template
*/
public function _getPdfProfileTemplates() {
$data = array();
$pdfTemplateNames = getPdfTemplateNames();
$reader = new PDFStructureReader(\LAM\PDF\GLOBAL_PROFILE);
foreach ($pdfTemplateNames as $scope => $templateNames) {
foreach ($templateNames as $templateName) {
$pdfStructure = $reader->read($scope, $templateName);
$data['structures'][$scope][$templateName] = $pdfStructure->export();
}
}
$logoNames = getPdfTemplateLogoNames();
foreach ($logoNames as $logoName) {
$data['logos'][$logoName] = base64_encode(getPdfTemplateLogoBinary($logoName));
}
return $data;
}
2020-06-01 08:52:55 +00:00
/**
* Returns the content of the self service profiles.
*
* @return array data
*/
public function _getSelfServiceProfiles() {
$data = array();
$profileTypes = getSelfServiceProfiles();
foreach ($profileTypes as $profileType => $profileNames) {
foreach ($profileNames as $profileName) {
$profile = loadSelfServiceProfile($profileName, $profileType);
if ($profile === false) {
continue;
}
$data[$profileType][$profileName] = $profile->export();
}
}
return $data;
}
2020-04-12 10:39:52 +00:00
}
2020-04-12 19:51:19 +00:00
/**
* Importer for LAM's configuration data.
*/
class ConfigDataImporter {
/**
* Returns a list of possible import objects.
*
* @param string $json JSON data
* @return ImporterStep[] steps
* @throws LAMException if invalid format
*/
public function getPossibleImportSteps($json) {
2020-04-19 18:40:40 +00:00
$data = json_decode($json, true);
2020-04-12 19:51:19 +00:00
if ($data === null) {
throw new LAMException(_('Unable to read import file.'));
}
$steps = array();
foreach ($data as $key => $value) {
switch ($key) {
case 'mainConfig':
$steps[] = new ImporterStep(_('General settings'), 'mainConfig', $value);
break;
2020-04-13 13:40:33 +00:00
case 'certificates':
$steps[] = new ImporterStep(_('SSL certificates'), 'certificates', $value);
break;
2020-04-19 18:40:40 +00:00
case 'serverProfiles':
$mainStep = new ImporterStep(_('Server profiles'), 'serverProfiles', $value);
foreach ($value as $profileName => $profileData) {
$mainStep->addSubStep(new ImporterStep($profileName, 'serverProfile_' . $profileName, $profileData));
}
$steps[] = $mainStep;
break;
2020-05-03 08:32:35 +00:00
case 'accountProfiles':
$mainStep = new ImporterStep(_('Account profiles'), 'accountProfiles', $value);
foreach ($value as $profileName => $profileData) {
$mainStep->addSubStep(new ImporterStep($profileName, 'accountProfile_' . $profileName, $profileData));
}
$steps[] = $mainStep;
break;
case 'accountProfileTemplates':
$steps[] = new ImporterStep(_('Account profiles') . ' - ' . _('Global templates'), 'accountProfileTemplates', $value);
break;
2020-05-13 18:47:12 +00:00
case 'pdfProfiles':
$mainStep = new ImporterStep(_('PDF structures'), 'pdfProfiles', $value);
foreach ($value as $profileName => $profileData) {
$mainStep->addSubStep(new ImporterStep($profileName, 'pdfProfile_' . $profileName, $profileData));
}
$steps[] = $mainStep;
break;
2020-05-30 10:35:22 +00:00
case 'pdfProfileTemplates':
$steps[] = new ImporterStep(_('PDF structures') . ' - ' . _('Global templates'), 'pdfProfileTemplates', $value);
break;
2020-06-01 08:52:55 +00:00
case 'selfServiceProfiles':
$steps[] = new ImporterStep(_('Self service profiles'), 'selfServiceProfiles', $value);
break;
2020-04-12 19:51:19 +00:00
default:
logNewMessage(LOG_WARNING, 'Unknown import type: ' . $key);
}
}
if (empty($steps)) {
throw new LAMException(_('Unable to read import file.'));
}
return $steps;
}
/**
* Runs the actual import.
*
* @param ImporterStep[] $steps import steps
2020-04-13 13:40:33 +00:00
* @throws LAMException if error occurred
2020-04-12 19:51:19 +00:00
*/
public function runImport($steps) {
foreach ($steps as $step) {
if (!$step->isActive()) {
continue;
}
$key = $step->getKey();
switch ($key) {
case 'mainConfig':
2020-04-13 13:40:33 +00:00
$this->importMainConfig($step->getValue());
break;
case 'certificates':
$this->importCertificates($step->getValue());
2020-04-12 19:51:19 +00:00
break;
2020-04-26 06:55:09 +00:00
case 'serverProfiles':
$this->importServerProfiles($step);
break;
2020-05-03 08:32:35 +00:00
case 'accountProfiles':
$this->importAccountProfiles($step);
break;
case 'accountProfileTemplates':
$this->importAccountProfileTemplates($step);
break;
2020-05-13 18:47:12 +00:00
case 'pdfProfiles':
$this->importPdfProfiles($step);
break;
2020-05-30 10:35:22 +00:00
case 'pdfProfileTemplates':
$this->importPdfProfileTemplates($step);
break;
2020-06-01 08:52:55 +00:00
case 'selfServiceProfiles':
$this->importSelfServiceProfiles($step);
break;
2020-04-12 19:51:19 +00:00
default:
logNewMessage(LOG_WARNING, 'Unknown import type: ' . $key);
}
}
}
2020-04-13 13:40:33 +00:00
/**
* Imports the main configuration.
*
* @param array $data main config data
* @throws LAMException error during import
*/
private function importMainConfig($data) {
$cfgMain = new LAMCfgMain();
$cfgMain->importData($data);
$cfgMain->save();
}
/**
* Imports the SSL certificates.
*
* @param null|string $data file content
* @throws LAMException error during import
*/
private function importCertificates($data) {
$cfgMain = new LAMCfgMain();
$cfgMain->importCertificates($data);
}
2020-04-26 06:55:09 +00:00
/**
* Imports the server profiles.
*
* @param ImporterStep $step step
* @throws LAMException error during import
*/
private function importServerProfiles($step) {
2020-04-27 19:53:50 +00:00
$failedProfiles = array();
2020-04-26 06:55:09 +00:00
foreach ($step->getSubSteps() as $profileStep) {
if (!$profileStep->isActive()) {
continue;
}
$data = $profileStep->getValue();
$profileName = str_replace('serverProfile_', '', $profileStep->getKey());
$serverProfile = new LAMConfig($profileName);
$serverProfile->importData($data);
2020-04-27 19:53:50 +00:00
$result = $serverProfile->save();
if ($result === LAMConfig::SAVE_FAIL) {
$failedProfiles[] = $profileName;
}
}
if (!empty($failedProfiles)) {
throw new LAMException(_('Unable to save server profile.'), implode(', ', $failedProfiles));
2020-04-26 06:55:09 +00:00
}
}
2020-05-03 08:32:35 +00:00
/**
* Imports the account profiles.
*
* @param ImporterStep $step step
* @throws LAMException error during import
*/
private function importAccountProfiles($step) {
$failedProfiles = array();
foreach ($step->getSubSteps() as $profileStep) {
if (!$profileStep->isActive()) {
continue;
}
$data = $profileStep->getValue();
$serverProfileName = str_replace('accountProfile_', '', $profileStep->getKey());
$serverProfile = new LAMConfig($serverProfileName);
foreach ($data as $typeId => $accountProfiles) {
foreach ($accountProfiles as $accountProfileName => $accountProfileData) {
$result = saveAccountProfile($accountProfileData, $accountProfileName, $typeId, $serverProfile);
if (!$result) {
$failedProfiles[] = $serverProfileName . ':' . $typeId . ':' . $accountProfileName;
}
}
}
}
if (!empty($failedProfiles)) {
throw new LAMException(_('Unable to save account profile.'), implode(', ', $failedProfiles));
}
}
/**
* Imports the account profile templates.
*
* @param ImporterStep $step step
* @throws LAMException error during import
*/
private function importAccountProfileTemplates($step) {
$data = $step->getValue();
foreach ($data as $typeId => $accountProfileTemplates) {
foreach ($accountProfileTemplates as $accountProfileTemplateName => $accountProfileData) {
installTemplateAccountProfile($typeId, $accountProfileTemplateName, $accountProfileData);
}
}
}
2020-05-13 18:47:12 +00:00
/**
* Imports the PDF profiles.
*
* @param ImporterStep $step step
* @throws LAMException error during import
*/
private function importPdfProfiles($step) {
$failedProfiles = array();
foreach ($step->getSubSteps() as $profileStep) {
if (!$profileStep->isActive()) {
continue;
}
$data = $profileStep->getValue();
$serverProfileName = str_replace('pdfProfile_', '', $profileStep->getKey());
if (isset($data['structures'])) {
$writer = new PDFStructureWriter($serverProfileName);
foreach ($data['structures'] as $typeId => $pdfProfiles) {
foreach ($pdfProfiles as $pdfProfileName => $pdfProfileData) {
$structure = new PDFStructure();
$structure->import($pdfProfileData);
try {
$writer->write($typeId, $pdfProfileName, $structure);
}
catch (LAMException $e) {
2020-05-30 10:35:22 +00:00
logNewMessage(LOG_ERR, $e->getTitle() . ' ' . $e->getMessage());
2020-05-13 18:47:12 +00:00
$failedProfiles[] = $serverProfileName . ':' . $typeId . ':' . $pdfProfileName;
}
}
}
}
if (isset($data['logos'])) {
foreach ($data['logos'] as $logoFileName => $logoData) {
$tempFilePath = tempnam("/tmp", "lam");
$tempFile = fopen($tempFilePath, "w");
$logoBinary = base64_decode($logoData);
fwrite($tempFile, $logoBinary);
fclose($tempFile);
uploadPDFLogo($tempFilePath, $logoFileName, $serverProfileName);
unlink($tempFilePath);
}
}
}
if (!empty($failedProfiles)) {
throw new LAMException(_('Could not save PDF structure, access denied.'), implode(', ', $failedProfiles));
}
}
2020-05-30 10:35:22 +00:00
/**
* Imports the PDF profile templates.
*
* @param ImporterStep $step step
* @throws LAMException error during import
*/
private function importPdfProfileTemplates($step) {
$failedNames = array();
$data = $step->getValue();
if (isset($data['structures'])) {
$writer = new PDFStructureWriter(\LAM\PDF\GLOBAL_PROFILE);
foreach ($data['structures'] as $typeId => $pdfProfiles) {
foreach ($pdfProfiles as $pdfProfileName => $pdfProfileData) {
$structure = new PDFStructure();
$structure->import($pdfProfileData);
try {
$writer->write($typeId, $pdfProfileName, $structure);
}
catch (LAMException $e) {
$failedNames[] = $typeId . ':' . $pdfProfileName;
logNewMessage(LOG_ERR, $e->getTitle() . ' ' . $e->getMessage());
}
}
}
}
$failedLogos = array();
if (isset($data['logos'])) {
foreach ($data['logos'] as $logoFileName => $logoData) {
$tempFilePath = tempnam("/tmp", "lam");
$tempFile = fopen($tempFilePath, "w");
$logoBinary = base64_decode($logoData);
fwrite($tempFile, $logoBinary);
fclose($tempFile);
$message = uploadPDFLogo($tempFilePath, $logoFileName, \LAM\PDF\GLOBAL_PROFILE);
unlink($tempFilePath);
if ($message->getType() === 'ERROR') {
$failedLogos[] = $logoFileName;
}
}
}
if (!empty($failedNames)) {
throw new LAMException(_('Could not save PDF structure, access denied.'), implode(', ', $failedNames));
}
if (!empty($failedLogos)) {
throw new LAMException(_('Unable to upload logo file.'), implode(', ', $failedLogos));
}
}
2020-06-01 08:52:55 +00:00
/**
* Imports the self service profiles.
*
* @param ImporterStep $step importer step
* @throws LAMException error saving profiles
*/
private function importSelfServiceProfiles($step) {
$failedNames = array();
$data = $step->getValue();
foreach ($data as $typeId => $profileData) {
foreach ($profileData as $profileName => $currentProfileData) {
$profile = selfServiceProfile::import($currentProfileData);
$result = saveSelfServiceProfile($profileName, $typeId, $profile);
if (!$result) {
$failedNames[] = $profileName;
}
}
}
if (!empty($failedNames)) {
throw new LAMException(_('Unable to save profile!'), implode(', ', $failedNames));
}
}
2020-04-12 19:51:19 +00:00
}
/**
* Step of the import process.
*/
class ImporterStep {
private $label;
private $key;
private $value;
private $active = false;
2020-04-19 18:40:40 +00:00
private $subSteps = array();
2020-04-12 19:51:19 +00:00
/**
* Constructor.
*
* @param string $label label
* @param string $key key
* @param array $value value
*/
2020-04-26 06:55:09 +00:00
public function __construct($label, $key, $value) {
2020-04-12 19:51:19 +00:00
$this->label = $label;
$this->key = $key;
2020-04-26 06:55:09 +00:00
$this->value = $value;
2020-04-12 19:51:19 +00:00
}
/**
* Returns the label.
*
* @return string label
*/
public function getLabel() {
return $this->label;
}
/**
* Returns the key.
*
* @return string key
*/
public function getKey() {
return $this->key;
}
/**
* Returns if this step should be executed.
*
* @return bool active
*/
public function isActive(): bool {
return $this->active;
}
/**
* Sets if this step should be executed.
*
* @param bool $active active
*/
public function setActive(bool $active) {
$this->active = $active;
}
/**
* Returns the value.
*
* @return string value
*/
public function getValue() {
return $this->value;
}
2020-04-19 18:40:40 +00:00
/**
* Adds a sub-step.
*
* @param ImporterStep $subStep sub-step
*/
public function addSubStep($subStep) {
$this->subSteps[] = $subStep;
}
/**
* Returns the sub-steps.
*
* @return ImporterStep[] sub-steps
*/
public function getSubSteps() {
return $this->subSteps;
}
2020-04-12 19:51:19 +00:00
}