refactoring

This commit is contained in:
Roland Gruber 2020-05-16 11:10:37 +02:00
parent 0a72bc9635
commit 5151d96592
1 changed files with 62 additions and 26 deletions

View File

@ -92,10 +92,25 @@ function loadAccountProfile($profile, $typeId, $serverProfileName) {
logNewMessage(LOG_NOTICE, "Invalid account profile name: $serverProfileName:$profile:$typeId"); logNewMessage(LOG_NOTICE, "Invalid account profile name: $serverProfileName:$profile:$typeId");
return false; return false;
} }
$settings = array();
$file = substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/" . $serverProfileName . '/' . $profile . "." . $typeId; $file = substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/" . $serverProfileName . '/' . $profile . "." . $typeId;
if (is_file($file)) { try {
$file = @fopen($file, "r"); return readAccountProfileFile($file);
} catch (LAMException $e) {
StatusMessage('ERROR', $e->getTitle(), $e->getMessage());
}
}
/**
* Reads an account profile from the given file name.
*
* @param string $fileName file name
* @return array hash array (attribute => value)
* @throws LAMException error reading file
*/
function readAccountProfileFile($fileName) {
$settings = array();
if (is_file($fileName)) {
$file = @fopen($fileName, "r");
if ($file) { if ($file) {
while (!feof($file)) { while (!feof($file)) {
$line = fgets($file, 1024); $line = fgets($file, 1024);
@ -114,15 +129,16 @@ function loadAccountProfile($profile, $typeId, $serverProfileName) {
} }
} }
fclose($file); fclose($file);
}
else {
StatusMessage("ERROR", "", _("Unable to load profile!") . " " . $file);
}
}
else {
StatusMessage("ERROR", "", _("Unable to load profile!") . " " . $file);
}
return $settings; return $settings;
}
else {
throw new LAMException(_("Unable to load profile!"), $fileName);
}
}
else {
throw new LAMException(_("Unable to load profile!"), $fileName);
}
return array();
} }
/** /**
@ -254,7 +270,34 @@ function copyAccountProfileToTemplates($sourceType, $sourceProfileName) {
* Installs template profiles to the current server profile. * Installs template profiles to the current server profile.
*/ */
function installProfileTemplates() { function installProfileTemplates() {
$templatePath = dirname(__FILE__) . '/../config/templates/profiles'; $allTemplates = getProfileTemplateNames();
$basePath = dirname(__FILE__) . '/../config/profiles/' . $_SESSION['config']->getName();
if (!file_exists($basePath)) {
mkdir($basePath, 0700, true);
}
$typeManager = new \LAM\TYPES\TypeManager();
foreach ($typeManager->getConfiguredTypes() as $type) {
if (empty($allTemplates[$type->getScope()])) {
continue;
}
foreach ($allTemplates[$type->getScope()] as $templateName) {
$path = $basePath . '/' . $templateName . '.' . $type->getId();
if (!is_file($path)) {
$template = getProfileTemplateFileName($type->getScope(), $templateName);
logNewMessage(LOG_DEBUG, 'Copy template ' . $template . ' to ' . $path);
@copy($template, $path);
}
}
}
}
/**
* Returns a list of all global profile templates.
*
* @return array names (array('user' => array('default', 'extra')))
*/
function getProfileTemplateNames() {
$templatePath = __DIR__ . '/../config/templates/profiles';
$templateDir = @dir($templatePath); $templateDir = @dir($templatePath);
$allTemplates = array(); $allTemplates = array();
if ($templateDir) { if ($templateDir) {
@ -269,24 +312,17 @@ function installProfileTemplates() {
$entry = $templateDir->read(); $entry = $templateDir->read();
} }
} }
$basePath = dirname(__FILE__) . '/../config/profiles/' . $_SESSION['config']->getName(); return $allTemplates;
if (!file_exists($basePath)) { }
mkdir($basePath, 0700, true);
} /**
$typeManager = new \LAM\TYPES\TypeManager(); * Returns the file name of a global template.
foreach ($typeManager->getConfiguredTypes() as $type) { *
if (empty($allTemplates[$type->getScope()])) { * @param string $scope e.g. user
continue; * @param string $name profile name
} * @return string file name
foreach ($allTemplates[$type->getScope()] as $templateName) { */
$path = $basePath . '/' . $templateName . '.' . $type->getId(); function getProfileTemplateFileName($scope, $name) {
if (!is_file($path)) { return __DIR__ . '/../config/templates/profiles' . '/' . $name . '.' . $scope;
$template = $templatePath . '/' . $templateName . '.' . $type->getScope();
logNewMessage(LOG_DEBUG, 'Copy template ' . $template . ' to ' . $path);
@copy($template, $path);
}
}
}
} }
?>