renamed profile management functions

This commit is contained in:
Roland Gruber 2004-10-06 20:00:17 +00:00
parent 921a0e1639
commit 6289fa7cc4
6 changed files with 50 additions and 297 deletions

View File

@ -234,9 +234,13 @@ class baseModule {
if (is_array($this->meta['profile_checks'])) { if (is_array($this->meta['profile_checks'])) {
$identifiers = array_keys($this->meta['profile_checks']); $identifiers = array_keys($this->meta['profile_checks']);
for ($i = 0; $i < sizeof($identifiers); $i++) { for ($i = 0; $i < sizeof($identifiers); $i++) {
// check if option is required // empty input
if ($this->meta['profile_checks'][$identifiers[$i]]['required'] && ($options[$identifiers[$i]][0] == '')) { if (($options[$identifiers[$i]][0] == '') || !isset($options[$identifiers[$i]][0])) {
$messages[] = $this->meta['profile_checks'][$identifiers[$i]]['required_message']; // 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']) { switch ($this->meta['profile_checks'][$identifiers[$i]]['type']) {
// check by regular expression (from account.inc) // check by regular expression (from account.inc)

View File

@ -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 * @return array profile names
*/ */
function getUserProfiles() { function getAccountProfiles($scope) {
$dir = @dir(substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/users"); $dir = @dir(substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles");
$ret = array(); $ret = array();
$pos = 0; $pos = 0;
if ($dir) { if ($dir) {
while ($entry = $dir->read()){ $entry = $dir->read();
$ext = substr($entry, strlen($entry)-4, 4); while ($entry){
$name = substr($entry, 0, strlen($entry)-4); // check if filename ends with .<scope>
if ($ext == ".pru") { if (strrpos($entry, '.')) {
$ret[$pos] = $name; $pos = strrpos($entry, '.');
$pos ++; if (substr($entry, $pos + 1) == $scope) {
$name = substr($entry, 0, $pos);
$ret[] = $name;
}
} }
$entry = $dir->read();
} }
} }
return $ret; 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 * @param string $profile name of the profile (without .<scope> extension)
*/ * @param string $scope account type
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)
* @return array hash array (attribute => value) * @return array hash array (attribute => value)
*/ */
function loadUserProfile($profile) { function loadAccountProfile($profile, $scope) {
if (!eregi("^[0-9a-z_\\-]+$", $profile)) return false; if (!eregi("^[0-9a-z_-]+$", $profile) || !eregi("^[a-z]+$", $scope)) return false;
$settings = array(); $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) { if (is_file($file) == True) {
$file = @fopen($file, "r"); $file = @fopen($file, "r");
if ($file) { if ($file) {
@ -136,102 +98,23 @@ function loadUserProfile($profile) {
} }
/** /**
* Loads an group profile * Saves an hash array (attribute => value) to an account 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
* *
* file is created, if needed * file is created, if needed
* *
* @param string $profile name of the user profile (without .pru) * @param string $profile name of the account profile (without .<scope> extension)
* @param array $attributes hash array (attribute => value) * @param array $attributes hash array (attribute => value)
* @param string $scope account type
* @return boolean true, if saving succeeded * @return boolean true, if saving succeeded
*/ */
function saveUserProfile($attributes, $profile) { function saveAccountProfile($attributes, $profile, $scope) {
if (!$_SESSION['loggedIn'] == true) return false; if (!$_SESSION['loggedIn'] == true) return false;
// check profile name // 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)) { if (!is_array($attributes)) {
return false; 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"); $file = @fopen($path, "w");
if ($file) { if ($file) {
// write attributes // 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 $file name of profile (Without .<scope> extension)
* * @param string $scope account type
* @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)
* @return boolean true if profile was deleted * @return boolean true if profile was deleted
*/ */
function delUserProfile($file) { function delAccountProfile($file, $scope) {
if (!$_SESSION['loggedIn'] == true) return false; if (!$_SESSION['loggedIn'] == true) return false;
if (!eregi("^[0-9a-z\\-_]+$", $file)) return false; if (!eregi("^[0-9a-z\\-_]+$", $file) || !eregi("^[0-9a-z\\-_]+$", $scope)) return false;
$prof = substr(__FILE__, 0, strlen(__FILE__) - 16) . "config/profiles/users/".$file.".pru"; $prof = substr(__FILE__, 0, strlen(__FILE__) - 16) . "config/profiles/" . $file . "." . $scope;
if (is_file($prof)) { if (is_file($prof)) {
return @unlink($prof); return @unlink($prof);
} }
else return false; 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;
}
?> ?>

View File

@ -108,24 +108,10 @@ if (sizeof($errors) > 0) {
} }
else { // input data is valid, save profile else { // input data is valid, save profile
// save profile // save profile
if ($_POST['accounttype'] == "user") { if (saveAccountProfile($options, $_POST['profname'], $_POST['accounttype'])) {
if (saveUserProfile($options, $_POST['profname'])) { echo StatusMessage("INFO", _("Profile was saved."), $_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']);
} }
else StatusMessage("ERROR", _("Unable to save profile!"), $_POST['profname']);
echo ("<br><p><a href=\"profilemain.php\">" . _("Back to Profile Editor") . "</a></p>"); echo ("<br><p><a href=\"profilemain.php\">" . _("Back to Profile Editor") . "</a></p>");
} }

View File

@ -55,31 +55,11 @@ echo ("</head>\n<body>\n<p><br></p>\n");
// check if admin has submited delete operation // check if admin has submited delete operation
if ($_POST['submit']) { if ($_POST['submit']) {
// delete user profile // delete profile
if ($_POST['type'] == "user") { if (!delAccountProfile($_POST['del'], $_POST['type'])) {
if (!delUserProfile($_POST['del'])) { StatusMessage("ERROR", _("Unable to delete profile!"), $_POST['del'] . "." . $_POST['type']);
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']);
} }
else StatusMessage("INFO", _("Deleted profile:"), $_POST['del'] . "." . $_POST['type']);
echo ("<br><a href=\"profilemain.php\">" . _("Back to Profile Editor") . "</a>"); echo ("<br><a href=\"profilemain.php\">" . _("Back to Profile Editor") . "</a>");
echo ("</body></html>\n"); echo ("</body></html>\n");
exit; exit;

View File

@ -96,21 +96,21 @@ if ($_POST['forward'] == "yes") {
} }
// get list of user profiles and generate entries for dropdown box // get list of user profiles and generate entries for dropdown box
$usrprof = getUserProfiles(); $usrprof = getAccountProfiles('user');
$userprofiles = ""; $userprofiles = "";
for ($i = 0; $i < sizeof($usrprof); $i++) { for ($i = 0; $i < sizeof($usrprof); $i++) {
$userprofiles = $userprofiles . "<option>" . $usrprof[$i] . "</option>\n"; $userprofiles = $userprofiles . "<option>" . $usrprof[$i] . "</option>\n";
} }
// get list of group profiles and generate entries for dropdown box // get list of group profiles and generate entries for dropdown box
$grpprof = getGroupProfiles(); $grpprof = getAccountProfiles('group');
$groupprofiles = ""; $groupprofiles = "";
for ($i = 0; $i < sizeof($grpprof); $i++) { for ($i = 0; $i < sizeof($grpprof); $i++) {
$groupprofiles = $groupprofiles . "<option>" . $grpprof[$i] . "</option>\n"; $groupprofiles = $groupprofiles . "<option>" . $grpprof[$i] . "</option>\n";
} }
// get list of host profiles and generate entries for dropdown box // get list of host profiles and generate entries for dropdown box
$hstprof = getHostProfiles(); $hstprof = getAccountProfiles('host');
$hostprofiles = ""; $hostprofiles = "";
for ($i = 0; $i < sizeof($hstprof); $i++) { for ($i = 0; $i < sizeof($hstprof); $i++) {
$hostprofiles = $hostprofiles . "<option>" . $hstprof[$i] . "</option>\n"; $hostprofiles = $hostprofiles . "<option>" . $hstprof[$i] . "</option>\n";

View File

@ -67,9 +67,7 @@ $options = getProfileOptions($type);
// load old profile if needed // load old profile if needed
$old_options = array(); $old_options = array();
if ($_GET['edit']) { if ($_GET['edit']) {
if ($type == "user") $old_options = loadUserProfile($_GET['edit']); $old_options = loadAccountProfile($_GET['edit'], $type);
elseif ($type == "group") $old_options = loadGroupProfile($_GET['edit']);
elseif ($type == "host") $old_options = loadHostProfile($_GET['edit']);
} }
// display formular // display formular