refactoring

This commit is contained in:
Roland Gruber 2014-12-22 20:21:54 +00:00
parent f862f9bd8e
commit 772b9c3127
2 changed files with 48 additions and 29 deletions

View File

@ -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.
*

View File

@ -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") {