diff --git a/lam/lib/baseModule.inc b/lam/lib/baseModule.inc index 11ff4b9b..f843c75b 100644 --- a/lam/lib/baseModule.inc +++ b/lam/lib/baseModule.inc @@ -234,9 +234,13 @@ class baseModule { if (is_array($this->meta['profile_checks'])) { $identifiers = array_keys($this->meta['profile_checks']); for ($i = 0; $i < sizeof($identifiers); $i++) { - // check if option is required - if ($this->meta['profile_checks'][$identifiers[$i]]['required'] && ($options[$identifiers[$i]][0] == '')) { - $messages[] = $this->meta['profile_checks'][$identifiers[$i]]['required_message']; + // empty input + if (($options[$identifiers[$i]][0] == '') || !isset($options[$identifiers[$i]][0])) { + // check if option is required + if ($this->meta['profile_checks'][$identifiers[$i]]['required']) { + $messages[] = $this->meta['profile_checks'][$identifiers[$i]]['required_message']; + } + return $messages; } switch ($this->meta['profile_checks'][$identifiers[$i]]['type']) { // check by regular expression (from account.inc) diff --git a/lam/lib/profiles.inc b/lam/lib/profiles.inc index 4dc85441..d93c9cfc 100644 --- a/lam/lib/profiles.inc +++ b/lam/lib/profiles.inc @@ -30,81 +30,43 @@ $Id$ /** -* Returns an array of string with all available user profiles (without .pru) +* Returns an array of string with all available profiles for the given account type * +* @param string $scope account type * @return array profile names */ -function getUserProfiles() { - $dir = @dir(substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/users"); +function getAccountProfiles($scope) { + $dir = @dir(substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles"); $ret = array(); $pos = 0; if ($dir) { - while ($entry = $dir->read()){ - $ext = substr($entry, strlen($entry)-4, 4); - $name = substr($entry, 0, strlen($entry)-4); - if ($ext == ".pru") { - $ret[$pos] = $name; - $pos ++; + $entry = $dir->read(); + while ($entry){ + // check if filename ends with . + if (strrpos($entry, '.')) { + $pos = strrpos($entry, '.'); + if (substr($entry, $pos + 1) == $scope) { + $name = substr($entry, 0, $pos); + $ret[] = $name; + } } + $entry = $dir->read(); } } return $ret; } /** -* Returns an array of String with all available group profiles (without .prg) +* Loads an profile of the given account type * -* @return array profile names -*/ -function getGroupProfiles() { - $dir = @dir(substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/groups"); - $ret = array(); - $pos = 0; - if ($dir) { - while ($entry = $dir->read()){ - $ext = substr($entry, strlen($entry)-4, 4); - $name = substr($entry, 0, strlen($entry)-4); - if ($ext == ".prg") { - $ret[$pos] = $name; - $pos ++; - } - } - } - return $ret; -} - -/** -* Returns an array of String with all available host profiles (without .prh) -* -* @return array profile names -*/ -function getHostProfiles() { - $dir = @dir(substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/hosts"); - $ret = array(); - $pos = 0; - if ($dir) { - while ($entry = $dir->read()){ - $ext = substr($entry, strlen($entry)-4, 4); - $name = substr($entry, 0, strlen($entry)-4); - if ($ext == ".prh") { - $ret[$pos] = $name; - $pos ++; - } - } - } - return $ret; -} - -/** -* Loads an user profile -* -* @param string $profile name of the profile (without .pru) +* @param string $profile name of the profile (without . extension) +* @param string $scope account type * @return array hash array (attribute => value) */ -function loadUserProfile($profile) { - if (!eregi("^[0-9a-z_\\-]+$", $profile)) return false; +function loadAccountProfile($profile, $scope) { + if (!eregi("^[0-9a-z_-]+$", $profile) || !eregi("^[a-z]+$", $scope)) return false; $settings = array(); - $file = substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/users/" . $profile . ".pru"; + $file = substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/" . $profile . "." . $scope; if (is_file($file) == True) { $file = @fopen($file, "r"); if ($file) { @@ -136,102 +98,23 @@ function loadUserProfile($profile) { } /** -* Loads an group profile -* -* @param string $profile name of the profile (without .prg) -* @return array hash array (attribute => value) -*/ -function loadGroupProfile($profile) { - if (!eregi("^[0-9a-z_\\-]+$", $profile)) return false; - $settings = array(); - $file = substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/groups/" . $profile . ".prg"; - if (is_file($file) == True) { - $file = @fopen($file, "r"); - if ($file) { - while (!feof($file)) { - $line = fgets($file, 1024); - if (($line == "\n")||($line[0] == "#")) continue; // ignore comments - // search keywords - $parts = array(); - $parts = split(": ", $line); - if (sizeof($parts) != 2) continue; // ignore malformed settings - else { - $option = $parts[0]; - $value = $parts[1]; - // remove line ends - $value = chop($value); - $settings[$option] = explode("+::+", $value); - } - } - fclose($file); - } - else { - StatusMessage("ERROR", "", _("Unable to load profile!") . " " . $file); - } - } - else { - StatusMessage("ERROR", "", _("Unable to load profile!") . " " . $file); - } - return $settings; -} - -/** -* Loads an host profile -* -* @param string $profile name of the profile (without .prh) -* @return array hash array (attribute => value) -*/ -function loadHostProfile($profile) { - if (!eregi("^[0-9a-z_\\-]+$", $profile)) return false; - $settings = array(); - $file = substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/hosts/" . $profile . ".prh"; - if (is_file($file) == True) { - $file = @fopen($file, "r"); - if ($file) { - while (!feof($file)) { - $line = fgets($file, 1024); - if (($line == "\n")||($line[0] == "#")) continue; // ignore comments - // search keywords - $parts = array(); - $parts = split(": ", $line); - if (sizeof($parts) != 2) continue; // ignore malformed settings - else { - $option = $parts[0]; - $value = $parts[1]; - // remove line ends - $value = chop($value); - $settings[$option] = explode("+::+", $value); - } - } - fclose($file); - } - else { - StatusMessage("ERROR", "", _("Unable to load profile!") . " " . $file); - } - } - else { - StatusMessage("ERROR", "", _("Unable to load profile!") . " " . $file); - } - return $settings; -} - -/** -* Saves an hash array (attribute => value) to an user profile +* Saves an hash array (attribute => value) to an account profile * * file is created, if needed * -* @param string $profile name of the user profile (without .pru) +* @param string $profile name of the account profile (without . extension) * @param array $attributes hash array (attribute => value) +* @param string $scope account type * @return boolean true, if saving succeeded */ -function saveUserProfile($attributes, $profile) { +function saveAccountProfile($attributes, $profile, $scope) { if (!$_SESSION['loggedIn'] == true) return false; // check profile name - if (!eregi("^[0-9a-z_-]+$", $profile)) return false; + if (!eregi("^[0-9a-z_-]+$", $profile) || !eregi("^[a-z]+$", $scope)) return false; if (!is_array($attributes)) { return false; } - $path = substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/users/" . $profile . ".pru"; + $path = substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/" . $profile . "." . $scope; $file = @fopen($path, "w"); if ($file) { // write attributes @@ -255,119 +138,21 @@ function saveUserProfile($attributes, $profile) { } /** -* Saves an hash array (attribute => value) to an group profile +* Deletes an account profile * -* file is created, if needed -* -* @param string $profile name of the group profile (without .prg) -* @param array $attributes hash array (attribute => value) -* @return boolean true, if saving succeeded -*/ -function saveGroupProfile($attributes, $profile) { - if (!$_SESSION['loggedIn'] == true) return false; - // check profile name - if (!eregi("^[0-9a-z_-]+$", $profile)) return false; - if (!is_array($attributes)) { - return false; - } - $path = substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/groups/" . $profile . ".prg"; - $file = @fopen($path, "w"); - if ($file) { - // write attributes - $keys = array_keys($attributes); - for ($i = 0; $i < sizeof($keys); $i++) { - $line = $keys[$i] . ": " . implode("+::+", $attributes[$keys[$i]]) . "\n"; - fputs($file, $line); - } - // close file - fclose($file); - } - else { - return false; - } - return true; -} - -/** -* Saves an hash array (attribute => value) to an host profile -* -* file is created, if needed -* -* @param string $profile name of the host profile (without .prh) -* @param array $attributes hash array (attribute => value) -* @return boolean true, if saving succeeded -*/ -function saveHostProfile($attributes, $profile) { - if (!$_SESSION['loggedIn'] == true) return false; - // check profile name - if (!eregi("^[0-9a-z_-]+$", $profile)) return false; - if (!is_array($attributes)) { - return false; - } - $path = substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/hosts/" . $profile . ".prh"; - $file = @fopen($path, "w"); - if ($file) { - // write attributes - $keys = array_keys($attributes); - for ($i = 0; $i < sizeof($keys); $i++) { - $line = $keys[$i] . ": " . implode("+::+", $attributes[$keys[$i]]) . "\n"; - fputs($file, $line); - } - // close file - fclose($file); - } - else { - return false; - } - return true; -} - -/** -* Deletes a user profile -* -* @param string $file name of profile (Without .pru) +* @param string $file name of profile (Without . extension) +* @param string $scope account type * @return boolean true if profile was deleted */ -function delUserProfile($file) { +function delAccountProfile($file, $scope) { if (!$_SESSION['loggedIn'] == true) return false; - if (!eregi("^[0-9a-z\\-_]+$", $file)) return false; - $prof = substr(__FILE__, 0, strlen(__FILE__) - 16) . "config/profiles/users/".$file.".pru"; + if (!eregi("^[0-9a-z\\-_]+$", $file) || !eregi("^[0-9a-z\\-_]+$", $scope)) return false; + $prof = substr(__FILE__, 0, strlen(__FILE__) - 16) . "config/profiles/" . $file . "." . $scope; if (is_file($prof)) { return @unlink($prof); } else return false; } -/** -* Deletes a group profile -* -* @param string $file name of profile (Without .prg) -* @return boolean true if profile was deleted -*/ -function delGroupProfile($file) { - if (!$_SESSION['loggedIn'] == true) return false; - if (!eregi("^[0-9a-z\\-_]+$", $file)) return false; - $prof = substr(__FILE__, 0, strlen(__FILE__) - 16) . "config/profiles/groups/".$file.".prg"; - if (is_file($prof)) { - return @unlink($prof); - } - else return false; -} - -/** -* Deletes a host profile -* -* @param string $file name of profile (Without .prh) -* @return boolean true if profile was deleted -*/ -function delHostProfile($file) { - if (!$_SESSION['loggedIn'] == true) return false; - if (!eregi("^[0-9a-z\\-_]+$", $file)) return false; - $prof = substr(__FILE__, 0, strlen(__FILE__) - 16) . "config/profiles/hosts/".$file.".prh"; - if (is_file($prof)) { - return @unlink($prof); - } - else return false; -} ?> diff --git a/lam/templates/profedit/profilecreate.php b/lam/templates/profedit/profilecreate.php index c6e3e1e6..f095a39a 100644 --- a/lam/templates/profedit/profilecreate.php +++ b/lam/templates/profedit/profilecreate.php @@ -108,24 +108,10 @@ if (sizeof($errors) > 0) { } else { // input data is valid, save profile // save profile - if ($_POST['accounttype'] == "user") { - if (saveUserProfile($options, $_POST['profname'])) { - echo StatusMessage("INFO", _("Profile was saved."), $_POST['profname']); - } - else StatusMessage("ERROR", _("Unable to save profile!"), $_POST['profname']); - } - elseif ($_POST['accounttype'] == "group") { - if (saveGroupProfile($options, $_POST['profname'])) { - echo StatusMessage("INFO", _("Profile was saved."), $_POST['profname']); - } - else StatusMessage("ERROR", _("Unable to save profile!"), $_POST['profname']); - } - elseif ($_POST['accounttype'] == "host") { - if (saveHostProfile($options, $_POST['profname'])) { - echo StatusMessage("INFO", _("Profile was saved."), $_POST['profname']); - } - else StatusMessage("ERROR", _("Unable to save profile!"), $_POST['profname']); + if (saveAccountProfile($options, $_POST['profname'], $_POST['accounttype'])) { + echo StatusMessage("INFO", _("Profile was saved."), $_POST['profname']); } + else StatusMessage("ERROR", _("Unable to save profile!"), $_POST['profname']); echo ("

" . _("Back to Profile Editor") . "

"); } diff --git a/lam/templates/profedit/profiledelete.php b/lam/templates/profedit/profiledelete.php index 2754be2a..95939fcd 100644 --- a/lam/templates/profedit/profiledelete.php +++ b/lam/templates/profedit/profiledelete.php @@ -55,31 +55,11 @@ echo ("\n\n


\n"); // check if admin has submited delete operation if ($_POST['submit']) { - // delete user profile - if ($_POST['type'] == "user") { - if (!delUserProfile($_POST['del'])) { - StatusMessage("ERROR", "", _("Unable to delete profile!") . " " . $_POST['del']); - } - else StatusMessage("INFO", "", _("Deleted profile:") . " " . $_POST['del']); - } - // delete group profile - elseif ($_POST['type'] == "group") { - if (!delGroupProfile($_POST['del'])) { - StatusMessage("ERROR", "", _("Unable to delete profile!") . " " . $_POST['del']); - } - else StatusMessage("INFO", "", _("Deleted profile:") . " " . $_POST['del']); - } - // delete host profile - elseif ($_POST['type'] == "host") { - if (!delHostProfile($_POST['del'])) { - StatusMessage("ERROR", "", _("Unable to delete profile!") . " " . $_POST['del']); - } - else StatusMessage("INFO", "", _("Deleted profile:") . " " . $_POST['del']); - } - // wrong profile type - else { - StatusMessage("ERROR", "", _("Wrong or missing type!") . " " . $_POST['type']); + // delete profile + if (!delAccountProfile($_POST['del'], $_POST['type'])) { + StatusMessage("ERROR", _("Unable to delete profile!"), $_POST['del'] . "." . $_POST['type']); } + else StatusMessage("INFO", _("Deleted profile:"), $_POST['del'] . "." . $_POST['type']); echo ("
" . _("Back to Profile Editor") . ""); echo ("\n"); exit; diff --git a/lam/templates/profedit/profilemain.php b/lam/templates/profedit/profilemain.php index 105bdb7e..a2590b63 100644 --- a/lam/templates/profedit/profilemain.php +++ b/lam/templates/profedit/profilemain.php @@ -96,21 +96,21 @@ if ($_POST['forward'] == "yes") { } // get list of user profiles and generate entries for dropdown box -$usrprof = getUserProfiles(); +$usrprof = getAccountProfiles('user'); $userprofiles = ""; for ($i = 0; $i < sizeof($usrprof); $i++) { $userprofiles = $userprofiles . "\n"; } // get list of group profiles and generate entries for dropdown box -$grpprof = getGroupProfiles(); +$grpprof = getAccountProfiles('group'); $groupprofiles = ""; for ($i = 0; $i < sizeof($grpprof); $i++) { $groupprofiles = $groupprofiles . "\n"; } // get list of host profiles and generate entries for dropdown box -$hstprof = getHostProfiles(); +$hstprof = getAccountProfiles('host'); $hostprofiles = ""; for ($i = 0; $i < sizeof($hstprof); $i++) { $hostprofiles = $hostprofiles . "\n"; diff --git a/lam/templates/profedit/profilepage.php b/lam/templates/profedit/profilepage.php index 2f9bf6c2..3ff75e35 100644 --- a/lam/templates/profedit/profilepage.php +++ b/lam/templates/profedit/profilepage.php @@ -67,9 +67,7 @@ $options = getProfileOptions($type); // load old profile if needed $old_options = array(); if ($_GET['edit']) { - if ($type == "user") $old_options = loadUserProfile($_GET['edit']); - elseif ($type == "group") $old_options = loadGroupProfile($_GET['edit']); - elseif ($type == "host") $old_options = loadHostProfile($_GET['edit']); + $old_options = loadAccountProfile($_GET['edit'], $type); } // display formular