From 772b9c31276219ae39758aa6c80c6c2a3a4070ac Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Mon, 22 Dec 2014 20:21:54 +0000 Subject: [PATCH] refactoring --- lam/lib/config.inc | 33 ++++++++++++++++++++++ lam/templates/config/profmanage.php | 44 ++++++++++------------------- 2 files changed, 48 insertions(+), 29 deletions(-) diff --git a/lam/lib/config.inc b/lam/lib/config.inc index 5a798be6..7e246832 100644 --- a/lam/lib/config.inc +++ b/lam/lib/config.inc @@ -143,6 +143,39 @@ function getConfigProfiles() { return $ret; } +/** + * Creates a new server profile. + * + * @param String $name profile name + * @param String $password profile password + * @param String $template name of template file + * @return mixed Boolean TRUE if creation was ok, error message if not + */ +function createConfigProfile($name, $password, $template) { + if (!preg_match("/^[a-z0-9_-]+$/i", $name) || !preg_match("/^[a-z0-9\\._-]+$/i", $template) || in_array($name, getConfigProfiles())) { + return _("Profile name is invalid!"); + } + // check if template exists + if (!is_file("../../config/" . $template)) { + return "The file config/$template was not found. Please restore it."; + } + // create new profile file + $path = "../../config/" . $name . ".conf"; + @copy("../../config/$template", $path); + @chmod ($path, 0600); + $file = is_file($path); + if ($file) { + // load as config and write new password + $conf = new LAMConfig($name); + $conf->set_Passwd($password); + $conf->save(); + } + else { + return _("Unable to create new profile!"); + } + return true; +} + /** * Deletes the given server profile. * diff --git a/lam/templates/config/profmanage.php b/lam/templates/config/profmanage.php index a5cc85e0..5a02b4eb 100644 --- a/lam/templates/config/profmanage.php +++ b/lam/templates/config/profmanage.php @@ -55,37 +55,23 @@ if (isset($_POST['action'])) { } // add new profile elseif ($_POST['action'] == "add") { - if (preg_match("/^[a-z0-9_-]+$/i", $_POST['addprofile']) && !in_array($_POST['addprofile'], getConfigProfiles())) { - // check profile password - if ($_POST['addpassword'] && $_POST['addpassword2'] && ($_POST['addpassword'] == $_POST['addpassword2'])) { - // check if lam.conf.sample exists - if (!is_file("../../config/lam.conf.sample")) { - $error = "The file config/lam.conf.sample was not found. Please restore it."; - } - else { - // create new profile file - @copy("../../config/lam.conf.sample", "../../config/" . $_POST['addprofile'] . ".conf"); - @chmod ("../../config/" . $_POST['addprofile'] . ".conf", 0600); - $file = is_file("../../config/" . $_POST['addprofile'] . ".conf"); - if ($file) { - // load as config and write new password - $conf = new LAMConfig($_POST['addprofile']); - $conf->set_Passwd($_POST['addpassword']); - $conf->save(); - $_SESSION['conf_isAuthenticated'] = $_POST['addprofile']; - $_SESSION['conf_config'] = $conf; - $_SESSION['conf_messages'][] = array('INFO', _("Created new profile."), $_POST['addprofile']); - metaRefresh('confmain.php'); - exit; - } - else { - $error = _("Unable to create new profile!"); - } - } + // check profile password + if ($_POST['addpassword'] && $_POST['addpassword2'] && ($_POST['addpassword'] == $_POST['addpassword2'])) { + $result = createConfigProfile($_POST['addprofile'], $_POST['addpassword'], 'lam.conf.sample'); + if ($result === true) { + $_SESSION['conf_isAuthenticated'] = $_POST['addprofile']; + $_SESSION['conf_config'] = new LAMConfig($_POST['addprofile']); + $_SESSION['conf_messages'][] = array('INFO', _("Created new profile."), $_POST['addprofile']); + metaRefresh('confmain.php'); + exit; + } + else { + $error = $result; } - else $error = _("Profile passwords are different or empty!"); } - else $error = _("Profile name is invalid!"); + else { + $error = _("Profile passwords are different or empty!"); + } } // rename profile elseif ($_POST['action'] == "rename") {