Zugang B: Admin

Erstellt: 08.03.2022 Lesedauer ≤ 1 Min.

Stellt administrative Funktionen für die Benutzerverwaltung bereit.

Ergänzt die Benutzerkonten-Verwaltung des Administrators mit zusätzlichen Funktionen zum Anlegen, Löschen und Vergabe eines neuen Passworts anderer Benutzerkonten.

Es wird im Verzeichnis /user/ erwartet, in dem alle Daten für die Zugangskontrolle abgelegt werden.

Das Skript

Stand: 21.10.2022

Ladefehler: $$$ file a $$$

<?php
if (isset($_SESSION["benutzername"])) {
  
// sieht nur der Admin    
  
if ($_SESSION["benutzername"] === $admin) {
?>
    <h4>Administrator-Funktionen</h4>
    <blockquote><small>Neues Passwort setzen: Benutzer löschen und neu anlegen.</small></blockquote><br/>
    <form method="post">
      <legend>Benutzerdaten bearbeiten:</legend>
      <div>
        <label>E-Mail-Adresse:</label>
        <input type="text" name="username" autofocus="autofocus" />
      </div>
      <div>
        <input type="checkbox" name="add" value="add"><label>Neu anlegen (mit optionaler Info)</label>
        <input type="checkbox" name="pi" value="pi" checked=true><label>Passwort-Info-E-Mail schicken</label>
        <input type="checkbox" name="del" value="del"><label>Löschen (ohne Info)</label>
        <input type="checkbox" name="cld" value="cld"><label>Loginlist aufräumen (Doubletten entfernen: 1 Eintrag „gewinnt“)</label>
      </div>
      <div>
        <input type="submit" name="admin" value="Aktion ausführen">
      </div>
    </form>
<?php   
    
if (isset($_POST["admin"])) {    
      
$admindata explode("\n",file_get_contents($thefile));
      
      if ((isset(
$_POST["add"])) AND (isset($_POST["username"]))) {
        
$uname $_POST["username"];
        if (
strpos(implode('',$admindata),$uname) === false) {
          
$nepw     random_str(8);
          
$neuw password_hash($nepw,PASSWORD_DEFAULT);      
          
$admindata[] = $uname ":" $neuw ;
          If (
file_put_contents($thefile,implode("\n",$admindata), LOCK_EX)) { 
            If (isset(
$_POST["pi"])) {
              
$msg str_replace("??email??"$uname$msg);                   
              
$msg str_replace("??pword??"$nepw$msg);    
              if (
mail($uname,$betreff,$msg,implode("\r\n",$headers))) {  
                echo 
"<blockquote><p>An »{$uname}« wurde eine Passwort-Mitteilung geschickt.</p></blockquote><br/>";
              }
              else {
                echo 
'<blockquote><blockquote><blockquote><p>Der Mail-Versand wurde abgelehnt. In ca. 1 h nochmal versuchen (vermutlich Spamlimit erreicht).</p></blockquote></blockquote></blockquote><br/>';       
              }
            }
            else {
                echo 
"<blockquote><p>Änderung <em>ohne Benachrichtigung des Benutzers</em> durchgeführt.</p></blockquote><br/>";
            }
          }  
          else {
?> 
            <br/><blockquote><blockquote><blockquote>           
            <p>Bitte Anlegen wiederholen.<br/>Sollte diese Meldung mehrfach kommen, gibt es ein „Timing-Problem“    .</p>
              </blockquote></blockquote></blockquote><br/>
<?php      
          
}
        }
        else {
?>
            <blockquote><blockquote><blockquote>
            <p>Diesen Benutzer gibt es bereits. Keine Neuanlage.</p>
            </blockquote></blockquote></blockquote><br/>
<?php         
        
}
      }    
      
      if ((isset(
$_POST["del"])) AND (isset($_POST["username"]))) {
        
$admuser $_POST["username"];    
        
// foreach($admindata as &$data) {
        
foreach($admindata as $key => $data) {
          
//$data = preg_replace('/\s+/','',$data); // Leerzeichen weg
          
if (strlen($data)<4) continue;
          list(
$ruser,$rpx) = explode(":"$data2);    
          if (
$ruser <> $admuser) continue; 
          if (
$ruser === $admuser) {
            
// $data = 'rm_' . random_str(20).":rm_". random_str(20);
            
unset($admindata[$key]);
            If (
file_put_contents($thefile,implode("\n",$admindata), LOCK_EX)) { 
              echo 
"<blockquote><p>»{$ruser}« wurde entfernt.</p></blockquote><br/>";          
            }
            else {
?> 
              <blockquote><blockquote><blockquote>           
              <p>Bitte Löschen wiederholen.<br/>Sollte diese Meldung mehrfach kommen, gibt es ein „Timing-Problem“.</p>
              </blockquote></blockquote></blockquote><br/>
<?php      
            
}
            break;        
          }      
        }            
      }    
      
      if ((isset(
$_POST["cld"]))){
          
$stop false;
          foreach(
$admindata as $key => $data) {
            
$ack false;
            if (
strlen($data)<4) continue;
            list(
$ruser,$rpx) = explode(":"$data2);
              foreach(
$admindata  as $count => &$doppelt) {                  
                  if (
$count <= $key) continue;
                  if (
strlen($doppelt)<4) continue;
                  list(
$xuser,$xpx) = explode(":"$doppelt2);
                  if (
$ruser === $xuser) {
                      unset(
$admindata[$count]);
                      
$ack true;
                  }  
              }
              If (
$ack === true) {
                if (
file_put_contents($thefile,implode("\n",$admindata), LOCK_EX)) { 
                    echo 
"<blockquote><p>Doubletten von {$ruser} wurden entfernt.</p></blockquote><br/>";                    
                }
                else {
                    echo 
"<blockquote><p>Update der Benutzerdatei gescheitert. Vorgang wiederholen.</p></blockquote><br/>";                  
                    
$stop true;
                    Break;                          
                }
              }
              if (
$stop === true) Break;
            }                       
      }    
    }  
// Admin-Post
    
echo "<br/><h4>Aktuelles Konto ({$admin})</h4>";
  } 
// Admintest
// Sessiontest          
?>