foreach loops

This commit is contained in:
Roland Gruber 2017-09-17 12:33:33 +02:00
parent 5cdcc495f4
commit e468e10a29
2 changed files with 49 additions and 48 deletions

View File

@ -189,13 +189,13 @@ if (isset($_POST['delete'])) {
foreach ($moduleNames as $singlemodule) { foreach ($moduleNames as $singlemodule) {
$success = true; $success = true;
$messages = $modules[$singlemodule]->preDeleteActions(); $messages = $modules[$singlemodule]->preDeleteActions();
for ($i = 0; $i < sizeof($messages); $i++) { foreach ($messages as $message) {
$errors[] = $messages[$i]; $errors[] = $message;
if ($messages[$i][0] == 'ERROR') { if ($message[0] == 'ERROR') {
$success = false; $success = false;
$allOk = false; $allOk = false;
} }
elseif ($messages[$i][0] == 'WARN') { elseif ($message[0] == 'WARN') {
$allOk = false; $allOk = false;
} }
} }
@ -214,20 +214,21 @@ if (isset($_POST['delete'])) {
// merge changes // merge changes
$DNs = array_keys($temp); $DNs = array_keys($temp);
$attributes = array_merge_recursive($temp, $attributes); $attributes = array_merge_recursive($temp, $attributes);
for ($i=0; $i<count($DNs); $i++) { foreach ($DNs as $dn) {
$ops = array_keys($temp[$DNs[$i]]); $ops = array_keys($temp[$dn]);
for ($j=0; $j<count($ops); $j++) { foreach ($ops as $op) {
$attrs = array_keys($temp[$DNs[$i]][$ops[$j]]); $attrs = array_keys($temp[$dn][$op]);
for ($k=0; $k<count($attrs); $k++) foreach ($attrs as $attribute) {
$attributes[$DNs[$i]][$ops[$j]][$attrs[$k]] = array_unique($attributes[$DNs[$i]][$ops[$j]][$attrs[$k]]); $attributes[$dn][$op][$attribute] = array_unique($attributes[$dn][$op][$attribute]);
}
} }
} }
} }
} }
$DNs = array_keys($attributes); $DNs = array_keys($attributes);
for ($i=0; $i<count($DNs); $i++) { foreach ($DNs as $dn) {
if (isset($attributes[$DNs[$i]]['errors'])) { if (isset($attributes[$dn]['errors'])) {
foreach ($attributes[$DNs[$i]]['errors'] as $singleerror) { foreach ($attributes[$dn]['errors'] as $singleerror) {
$errors[] = $singleerror; $errors[] = $singleerror;
if ($singleerror[0] == 'ERROR') { if ($singleerror[0] == 'ERROR') {
$stopprocessing = true; $stopprocessing = true;
@ -237,28 +238,28 @@ if (isset($_POST['delete'])) {
} }
if (!$stopprocessing) { if (!$stopprocessing) {
// modify attributes // modify attributes
if (isset($attributes[$DNs[$i]]['modify']) && !$stopprocessing) { if (isset($attributes[$dn]['modify']) && !$stopprocessing) {
$success = @ldap_mod_replace($_SESSION['ldap']->server(), $DNs[$i], $attributes[$DNs[$i]]['modify']); $success = @ldap_mod_replace($_SESSION['ldap']->server(), $dn, $attributes[$dn]['modify']);
if (!$success) { if (!$success) {
$errors[] = array ('ERROR', sprintf(_('Was unable to modify attributes from DN: %s.'), $DNs[$i]), getDefaultLDAPErrorString($_SESSION['ldap']->server())); $errors[] = array ('ERROR', sprintf(_('Was unable to modify attributes from DN: %s.'), $dn), getDefaultLDAPErrorString($_SESSION['ldap']->server()));
$stopprocessing = true; $stopprocessing = true;
$allOk = false; $allOk = false;
} }
} }
// add attributes // add attributes
if (isset($attributes[$DNs[$i]]['add']) && !$stopprocessing) { if (isset($attributes[$dn]['add']) && !$stopprocessing) {
$success = @ldap_mod_add($_SESSION['ldap']->server(), $DNs[$i], $attributes[$DNs[$i]]['add']); $success = @ldap_mod_add($_SESSION['ldap']->server(), $dn, $attributes[$dn]['add']);
if (!$success) { if (!$success) {
$errors[] = array ('ERROR', sprintf(_('Was unable to add attributes to DN: %s.'), $DNs[$i]), getDefaultLDAPErrorString($_SESSION['ldap']->server())); $errors[] = array ('ERROR', sprintf(_('Was unable to add attributes to DN: %s.'), $dn), getDefaultLDAPErrorString($_SESSION['ldap']->server()));
$stopprocessing = true; $stopprocessing = true;
$allOk = false; $allOk = false;
} }
} }
// remove attributes // remove attributes
if (isset($attributes[$DNs[$i]]['remove']) && !$stopprocessing) { if (isset($attributes[$dn]['remove']) && !$stopprocessing) {
$success = @ldap_mod_del($_SESSION['ldap']->server(), $DNs[$i], $attributes[$DNs[$i]]['remove']); $success = @ldap_mod_del($_SESSION['ldap']->server(), $dn, $attributes[$dn]['remove']);
if (!$success) { if (!$success) {
$errors[] = array ('ERROR', sprintf(_('Was unable to remove attributes from DN: %s.'), $DNs[$i]), getDefaultLDAPErrorString($_SESSION['ldap']->server())); $errors[] = array ('ERROR', sprintf(_('Was unable to remove attributes from DN: %s.'), $dn), getDefaultLDAPErrorString($_SESSION['ldap']->server()));
$stopprocessing = true; $stopprocessing = true;
$allOk = false; $allOk = false;
} }
@ -279,9 +280,9 @@ if (isset($_POST['delete'])) {
if (!$stopprocessing) { if (!$stopprocessing) {
foreach ($moduleNames as $singlemodule) { foreach ($moduleNames as $singlemodule) {
$messages = $modules[$singlemodule]->postDeleteActions(); $messages = $modules[$singlemodule]->postDeleteActions();
for ($i = 0; $i < sizeof($messages); $i++) { foreach ($messages as $message) {
$errors[] = $messages[$i]; $errors[] = $message;
if (($messages[$i][0] == 'ERROR') || ($messages[$i][0] == 'WARN')) { if (($message[0] == 'ERROR') || ($message[0] == 'WARN')) {
$allOk = false; $allOk = false;
} }
} }

View File

@ -54,19 +54,19 @@ if (!empty($_POST)) {
// check if user already pressed button // check if user already pressed button
if (isset($_POST['add_suff']) || isset($_POST['cancel'])) { if (isset($_POST['add_suff']) || isset($_POST['cancel'])) {
if (isset($_POST['add_suff'])) { if (isset($_POST['add_suff'])) {
$fail = array(); $failedDNs = array();
$errors = array(); $error = array();
$new_suff = $_POST['new_suff']; $newSuffixes = $_POST['new_suff'];
$new_suff = str_replace("\\", "", $new_suff); $newSuffixes = str_replace("\\", "", $newSuffixes);
$new_suff = str_replace("'", "", $new_suff); $newSuffixes = str_replace("'", "", $newSuffixes);
$new_suff = explode(";", $new_suff); $newSuffixes = explode(";", $newSuffixes);
// add entries // add entries
for ($i = 0; $i < sizeof($new_suff); $i++) { foreach ($newSuffixes as $newSuffix) {
// check if entry is already present // check if entry is already present
$info = @ldap_read($_SESSION['ldap']->server(), escapeDN($new_suff[$i]), "objectclass=*", array('dn'), 0, 0, 0, LDAP_DEREF_NEVER); $info = @ldap_read($_SESSION['ldap']->server(), escapeDN($newSuffix), "objectclass=*", array('dn'), 0, 0, 0, LDAP_DEREF_NEVER);
$res = @ldap_get_entries($_SESSION['ldap']->server(), $info); $res = @ldap_get_entries($_SESSION['ldap']->server(), $info);
if ($res) continue; if ($res) continue;
$suff = $new_suff[$i]; $suff = $newSuffix;
// generate DN and attributes // generate DN and attributes
$tmp = explode(",", $suff); $tmp = explode(",", $suff);
$name = explode("=", $tmp[0]); $name = explode("=", $tmp[0]);
@ -78,7 +78,7 @@ if (isset($_POST['add_suff']) || isset($_POST['cancel'])) {
$attr['objectClass'] = 'organization'; $attr['objectClass'] = 'organization';
$dn = $suff; $dn = $suff;
if (!@ldap_add($_SESSION['ldap']->server(), $dn, $attr)) { if (!@ldap_add($_SESSION['ldap']->server(), $dn, $attr)) {
$fail[] = $suff; $failedDNs[] = $suff;
$error[] = ldap_error($_SESSION['ldap']->server()); $error[] = ldap_error($_SESSION['ldap']->server());
continue; continue;
} }
@ -117,7 +117,7 @@ if (isset($_POST['add_suff']) || isset($_POST['cancel'])) {
$attr['ou'] = $headarray[1]; $attr['ou'] = $headarray[1];
$dn = $subsuffs[$k]; $dn = $subsuffs[$k];
if (!@ldap_add($_SESSION['ldap']->server(), $dn, $attr)) { if (!@ldap_add($_SESSION['ldap']->server(), $dn, $attr)) {
$fail[] = $suff; $failedDNs[] = $suff;
$error[] = ldap_error($_SESSION['ldap']->server()); $error[] = ldap_error($_SESSION['ldap']->server());
break; break;
} }
@ -132,7 +132,7 @@ if (isset($_POST['add_suff']) || isset($_POST['cancel'])) {
} }
$dn = $subsuffs[$k]; $dn = $subsuffs[$k];
if (!@ldap_add($_SESSION['ldap']->server(), $dn, $attr)) { if (!@ldap_add($_SESSION['ldap']->server(), $dn, $attr)) {
$fail[] = $suff; $failedDNs[] = $suff;
$error[] = ldap_error($_SESSION['ldap']->server()); $error[] = ldap_error($_SESSION['ldap']->server());
break; break;
} }
@ -141,7 +141,7 @@ if (isset($_POST['add_suff']) || isset($_POST['cancel'])) {
} }
} }
else { else {
$fail[] = $suff; $failedDNs[] = $suff;
$error[] = ldap_error($_SESSION['ldap']->server()); $error[] = ldap_error($_SESSION['ldap']->server());
} }
} }
@ -151,10 +151,10 @@ if (isset($_POST['add_suff']) || isset($_POST['cancel'])) {
include 'main_header.php'; include 'main_header.php';
// print error/success messages // print error/success messages
if (isset($_POST['add_suff'])) { if (isset($_POST['add_suff'])) {
if (sizeof($fail) > 0) { if (sizeof($failedDNs) > 0) {
// print error messages // print error messages
for ($i = 0; $i < sizeof($fail); $i++) { for ($i = 0; $i < sizeof($failedDNs); $i++) {
StatusMessage("ERROR", _("Failed to create entry!") . "<br>" . htmlspecialchars($error[$i]), htmlspecialchars($fail[$i])); StatusMessage("ERROR", _("Failed to create entry!") . "<br>" . htmlspecialchars($error[$i]), htmlspecialchars($failedDNs[$i]));
} }
include 'main_footer.php'; include 'main_footer.php';
} }
@ -173,10 +173,10 @@ if (isset($_POST['add_suff']) || isset($_POST['cancel'])) {
} }
// first show of page // first show of page
$new_suff = $_GET['suffs']; $newSuffixes = $_GET['suffs'];
$new_suff = str_replace("\\", "", $new_suff); $newSuffixes = str_replace("\\", "", $newSuffixes);
$new_suff = str_replace("'", "", $new_suff); $newSuffixes = str_replace("'", "", $newSuffixes);
$new_suff = explode(";", $new_suff); $newSuffixes = explode(";", $newSuffixes);
include 'main_header.php'; include 'main_header.php';
echo '<div class="user-bright smallPaddingContent">'; echo '<div class="user-bright smallPaddingContent">';
@ -186,15 +186,15 @@ include 'main_header.php';
$container->addElement(new htmlOutputText(_("You can setup the LDAP suffixes for all account types in your LAM server profile on tab \"Account types\".")), true); $container->addElement(new htmlOutputText(_("You can setup the LDAP suffixes for all account types in your LAM server profile on tab \"Account types\".")), true);
$container->addElement(new htmlSpacer(null, '10px'), true); $container->addElement(new htmlSpacer(null, '10px'), true);
// print missing suffixes // print missing suffixes
for ($i = 0; $i < sizeof($new_suff); $i++) { foreach ($newSuffixes as $newSuffix) {
$container->addElement(new htmlOutputText($new_suff[$i]), true); $container->addElement(new htmlOutputText($newSuffix), true);
} }
$container->addElement(new htmlSpacer(null, '10px'), true); $container->addElement(new htmlSpacer(null, '10px'), true);
$buttonContainer = new htmlTable(); $buttonContainer = new htmlTable();
$buttonContainer->addElement(new htmlButton('add_suff', _("Create"))); $buttonContainer->addElement(new htmlButton('add_suff', _("Create")));
$buttonContainer->addElement(new htmlButton('cancel', _("Cancel"))); $buttonContainer->addElement(new htmlButton('cancel', _("Cancel")));
$buttonContainer->addElement(new htmlHiddenInput('new_suff', implode(";", $new_suff))); $buttonContainer->addElement(new htmlHiddenInput('new_suff', implode(";", $newSuffixes)));
$container->addElement($buttonContainer); $container->addElement($buttonContainer);
addSecurityTokenToMetaHTML($container); addSecurityTokenToMetaHTML($container);