From 5151d96592512ed4f6a7b5104b672899c888af64 Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Sat, 16 May 2020 11:10:37 +0200 Subject: [PATCH] refactoring --- lam/lib/profiles.inc | 82 +++++++++++++++++++++++++++++++------------- 1 file changed, 59 insertions(+), 23 deletions(-) diff --git a/lam/lib/profiles.inc b/lam/lib/profiles.inc index 6825843f..c09d2464 100644 --- a/lam/lib/profiles.inc +++ b/lam/lib/profiles.inc @@ -92,10 +92,25 @@ function loadAccountProfile($profile, $typeId, $serverProfileName) { logNewMessage(LOG_NOTICE, "Invalid account profile name: $serverProfileName:$profile:$typeId"); return false; } - $settings = array(); $file = substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/" . $serverProfileName . '/' . $profile . "." . $typeId; - if (is_file($file)) { - $file = @fopen($file, "r"); + try { + 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) { while (!feof($file)) { $line = fgets($file, 1024); @@ -114,15 +129,16 @@ function loadAccountProfile($profile, $typeId, $serverProfileName) { } } fclose($file); + return $settings; } else { - StatusMessage("ERROR", "", _("Unable to load profile!") . " " . $file); + throw new LAMException(_("Unable to load profile!"), $fileName); } } else { - StatusMessage("ERROR", "", _("Unable to load profile!") . " " . $file); + throw new LAMException(_("Unable to load profile!"), $fileName); } - return $settings; + return array(); } /** @@ -254,21 +270,7 @@ function copyAccountProfileToTemplates($sourceType, $sourceProfileName) { * Installs template profiles to the current server profile. */ function installProfileTemplates() { - $templatePath = dirname(__FILE__) . '/../config/templates/profiles'; - $templateDir = @dir($templatePath); - $allTemplates = array(); - if ($templateDir) { - $entry = $templateDir->read(); - while ($entry){ - $parts = explode('.', $entry); - if ((strlen($entry) > 3) && (sizeof($parts) == 2)) { - $name = $parts[0]; - $scope = $parts[1]; - $allTemplates[$scope][] = $name; - } - $entry = $templateDir->read(); - } - } + $allTemplates = getProfileTemplateNames(); $basePath = dirname(__FILE__) . '/../config/profiles/' . $_SESSION['config']->getName(); if (!file_exists($basePath)) { mkdir($basePath, 0700, true); @@ -281,7 +283,7 @@ function installProfileTemplates() { foreach ($allTemplates[$type->getScope()] as $templateName) { $path = $basePath . '/' . $templateName . '.' . $type->getId(); if (!is_file($path)) { - $template = $templatePath . '/' . $templateName . '.' . $type->getScope(); + $template = getProfileTemplateFileName($type->getScope(), $templateName); logNewMessage(LOG_DEBUG, 'Copy template ' . $template . ' to ' . $path); @copy($template, $path); } @@ -289,4 +291,38 @@ function installProfileTemplates() { } } -?> +/** + * 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); + $allTemplates = array(); + if ($templateDir) { + $entry = $templateDir->read(); + while ($entry){ + $parts = explode('.', $entry); + if ((strlen($entry) > 3) && (sizeof($parts) == 2)) { + $name = $parts[0]; + $scope = $parts[1]; + $allTemplates[$scope][] = $name; + } + $entry = $templateDir->read(); + } + } + return $allTemplates; +} + +/** + * Returns the file name of a global template. + * + * @param string $scope e.g. user + * @param string $name profile name + * @return string file name + */ +function getProfileTemplateFileName($scope, $name) { + return __DIR__ . '/../config/templates/profiles' . '/' . $name . '.' . $scope; +} +