Location: PHPKode > projects > CourierAdmin > srv/www/htdocs/courieradmin/edit_user.php
<?php
/*
Courieradmin V1, Administration of Courier Mailserver
Copyright (C) 2003  Daniel Heule

This file is part of Courieradmin.

Courieradmin is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

Courieradmin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Courieradmin; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
require_once("common.php");
require_once("read_courier_config.php");
if(!$CourierAdmin_UserManager) exit;
if($_SESSION['user']['right_manageusers'] != 1) exit;
if(!isset($_REQUEST['user_type']) && $CourierAdmin_UserDB) $_REQUEST['user_type'] = "UserDB";
if(!isset($_REQUEST['user_type']) && $CourierAdmin_UserMysql) $_REQUEST['user_type'] = "MYSQL";
if(!isset($_REQUEST['user_type']) && $CourierAdmin_UserPGSQL) $_REQUEST['user_type'] = "PGSQL";
if(!isset($_REQUEST['user_type']) && $CourierAdmin_UserLDAP) $_REQUEST['user_type'] = "LDAP";
if(!isset($_REQUEST['user_type'])) exit;
if(!isset($_REQUEST['Act_user_edit'])) $_REQUEST['Act_user_edit'] = "Add New User to Courier";
$_SESSION['edit_user_type'] = $_REQUEST['user_type'];
if(!isset($_SESSION['edit_user_issaved'])) $_SESSION['edit_user_issaved'] = 0;
if(!isset($_SESSION['edit_user_data'])) $_SESSION['edit_user_data'] = array();
$titel_text = "User im LDAP Verzeichniss bearbeiten:";
$titel_einschub = "";
//print_r($_REQUEST);
//print_r($ldapuconfig);
?>
<html>
<head>
<title>User Editieren, Courieradmin (c) by Nerve</title>
</head>
<body>
<p><a href="list_user.php">Zur&uuml;ck zur Anzeige der User !</a></p>
<table width=100%>
<?php
$retval = "";
switch ($_REQUEST['user_type'])
{
  case "UserDB":
  print "i ist gleich 0";
  break;
  case "MYSQL":
  print "i ist gleich 1";
  break;
  case "PGSQL":
  print "i ist gleich 2";
  break;
  case "LDAP":
  $info = array();
  if($_REQUEST['Act_user_edit'] == "Add New User to Courier")
  {
    $info = $CourierAdmin_user_ldap_default;
  }
  if($_REQUEST['Act_user_edit'] == "Save User")
  {
    //print_r($_REQUEST);
    if(isset($_REQUEST['user_ldap_dn']))
    {
      unset($a);
      $a_dn = $_REQUEST['user_ldap_rdn'].",".$ldapuconfig['LDAP_BASEDN'];
      $dn_split = ldap_explode_dn ($_REQUEST['user_ldap_dn'], 0);
      if(!strcasecmp($dn_split[0], $_REQUEST['user_ldap_rdn']))
      {
        $dn_eq = 1;
      }
      else
      {
        $dn_eq = 0;
      }
      
      if(isset($_SESSION['edit_user_data'][0]) && isset($_SESSION['edit_user_data'][0]['objectclass']))
      {
        if(isset($_SESSION['edit_user_data'][0]['objectclass']['count']))
        {
          for($c = 0; $c < $_SESSION['edit_user_data'][0]['objectclass']['count']; $c++)
          {
            $a['objectclass'][] = $_SESSION['edit_user_data'][0]['objectclass'][$c];
          }
        }
        else
        {
         $a['objectclass'] = $_SESSION['edit_user_data'][0]['objectclass'];
        }
      }
      else
      {
        $a['objectclass'] = $CourierAdmin_user_ldap_default[0]['objectclass'];
      }
      $all_ok = 1;
      foreach($CourierAdmin_user_ldap_must as $param)
      {
        $name = "user_ldap_".$param;
        if(!isset($_REQUEST[$name]) && !is_array($_REQUEST[$name])) $all_ok = 0;
        else
        {
          foreach($_REQUEST[$name] as $val)
          {
            if($val != "")
            {
              $a[$param][] = $val;
            }
          }
        }
      }
      foreach($CourierAdmin_user_ldap_may as $param)
      {
        $name = "user_ldap_".$param;
        if(!isset($_REQUEST[$name])) $all_ok = 0;
        else
        {
          foreach($_REQUEST[$name] as $val)
          {
            if($val != "")
            {
              $a[$param][] = $val;
            }
          }
        }
      }
      if($all_ok)
      {
        $ds=ldap_connect($ldapuconfig['LDAP_SERVER']);  // muss ein gültiger LDAP Server sein
        if ($ds)
        {
          if($r=ldap_bind($ds,$ldapuconfig['LDAP_BINDDN'],$ldapuconfig['LDAP_BINDPW']))
          {
            if(!$dn_eq)
            {
              if(ldap_add($ds, $a_dn, $a))
              {
                if(!ldap_delete($ds, $_REQUEST['user_ldap_dn'])) $retval = "Fehler beim Verschieben ,,, alter eintrag konnte nicht gelöscht werden! ".ldap_error($ds)."<br>\n";
                else $retval = "Eintrag erfolgreich verschoben !";
              }
              sleep(1);
            }
            
            if(@ldap_modify($ds,$a_dn,$a))
            {
              $sr=ldap_read($ds,$a_dn,"(objectclass=CourierMailAccount)");
              $info = ldap_get_entries($ds, $sr);
              if($info['count'] == 1)
              {
                // User Modifiziert !!!!!
               $_SESSION['edit_user_data'] = $info;
               $retval = "LDAP Update erfolgreich !";
              }
            }
            else
            {
              $retval .= "Fehler beim Modifizieren des LDAP eintrages! ".ldap_error($ds);
              $a['dn'] = $a_dn;
              $info[0] = $a;
            }

                
          }
          
        }
        else
        {
          echo "Verbindung zum LDAP Server nicht möglich<br>";
        }

      }
    }
    else
    {
      $ds=ldap_connect($ldapuconfig['LDAP_SERVER']);  // muss ein gültiger LDAP Server sein
      if ($ds)
      {
        if($r=ldap_bind($ds,$ldapuconfig['LDAP_BINDDN'],$ldapuconfig['LDAP_BINDPW']))
        {
          unset($a);
          $a_dn = $_REQUEST['user_ldap_rdn'].",".$ldapuconfig['LDAP_BASEDN'];
          $a['objectclass'] = $CourierAdmin_user_ldap_default[0]['objectclass'];
          $all_ok = 1;
          foreach($CourierAdmin_user_ldap_must as $param)
          {
            $name = "user_ldap_".$param;
            if(!isset($_REQUEST[$name]) && !is_array($_REQUEST[$name])) $all_ok = 0;
            else
            {
              foreach($_REQUEST[$name] as $val)
              {
                if($val != "")
                {
                  $a[$param][] = $val;
                }
              }
            }
          }
          foreach($CourierAdmin_user_ldap_may as $param)
          {
            $name = "user_ldap_".$param;
            if(!isset($_REQUEST[$name])) $all_ok = 0;
            else
            {
              foreach($_REQUEST[$name] as $val)
              {
                if($val != "")
                {
                  $a[$param][] = $val;
                }
              }
            }
          }
          if($all_ok)
          {
            if(@ldap_add($ds, $a_dn, $a))
            {
              $retval = "Einfügen ins LDAP Verzeichniss erfolgreich !";
              $sr=ldap_read($ds,$a_dn,"(objectclass=CourierMailAccount)");
              $info = ldap_get_entries($ds, $sr);
              $_SESSION['edit_user_data'] = $info;
              unset($output);
              exec("sudo -u ".$CourierAdmin_sh_virtual_user." ".$CourierAdmin_path."/bin/sudo_virtual mdir_create ".$info[0]['homedirectory'][0],$output);
              //print_r($output);
              foreach($output as $line)
              {
                $retval .= "<br>\n".$line;
              }
            }
            else
            {
              $retval = ldap_error($ds);
              $a['dn'] = $a_dn;
              $info[0] = $a;
            }
          }
          else
          {
            $retval = "Bitte eingaben nochmals überprüfen !";
            $a['dn'] = $a_dn;
            $info[0] = $a;
          }
         }
      }
      else $retval = ldap_error($ds);
    }
  }
  if($_REQUEST['Act_user_edit'] == "Edit User" && isset($_REQUEST['user_ldap_dn']))
  {
    $ds=ldap_connect($ldapuconfig['LDAP_SERVER']);  // muss ein gültiger LDAP Server sein
    if ($ds)
    {
      if($r=ldap_bind($ds,$ldapuconfig['LDAP_BINDDN'],$ldapuconfig['LDAP_BINDPW']))
      {
        //$sr=ldap_read($ds,$ldapuconfig['LDAP_BASEDN'],"(objectclass=CourierMailAccount)");
        $sr=ldap_read($ds,$_REQUEST['user_ldap_dn'],"(objectclass=CourierMailAccount)");
        $info = ldap_get_entries($ds, $sr);
        $_SESSION['edit_user_data'] = $info;
        //print_r($info);
      }
      // ldap_close($ds);
    }
    else
    {
      echo "Verbindung zum LDAP Server nicht möglich<br>";
    }
  }
  if($_REQUEST['Act_user_edit'] == "Delete User" && isset($_REQUEST['user_ldap_dn']))
  {
    $ds=ldap_connect($ldapuconfig['LDAP_SERVER']);  // muss ein gültiger LDAP Server sein
    if ($ds)
    {
      if($r=ldap_bind($ds,$ldapuconfig['LDAP_BINDDN'],$ldapuconfig['LDAP_BINDPW']))
      {
        //$sr=ldap_read($ds,$ldapuconfig['LDAP_BASEDN'],"(objectclass=CourierMailAccount)");
        $sr=ldap_read($ds,$_REQUEST['user_ldap_dn'],"(objectclass=CourierMailAccount)");
        $info = ldap_get_entries($ds, $sr);
        $_SESSION['edit_user_data'] = $info;
        $titel_text = "User im LDAP Verzeichniss löschen:";
        $titel_einschub = "<tr><td colspan = 2><input type=submit value=\"User wirklich loeschen !\" name=\"Act_user_edit\"></td></tr>\n<tr><td colspan=2><hr></td></tr>\n";
        //print_r($info);
      }
      // ldap_close($ds);
    }
    else
    {
      echo "Verbindung zum LDAP Server nicht möglich<br>";
    }
  }



  //print_r($info);
  print("<form method=\"post\"  action=\"".$self."\" target=\"_self\">");
  print("<tr><td colspan=2><h1>".$titel_text."</h1></td></tr>\n");
  print("<tr><td colspan=2><hr></td></tr>\n");
  print($titel_einschub);
  if($retval != "")
  {
    print("<tr><td colspan=2><h3><font color=\"#FF0000\">".$retval."</font></h3></td></tr>\n");
    print("<tr><td colspan=2><hr></td></tr>\n");
  }
  if(isset($info[0]))
  {
    if(isset($info[0]['dn']))
    {
      print("<tr><td><h3>DN:&nbsp;</h3></td><td><h3>".$info[0]['dn']."</h3><input type=hidden name=\"user_ldap_dn\" value=\"".$info[0]['dn']."\"></td></tr>\n");
      print("<tr><td colspan=2><hr></td></tr>\n");
    }
    else
    {
      print("<tr><td><h3>Neue DN: (z.b.)&nbsp;</h3></td><td><h3>".$CourierAdmin_user_ldap_RDN_default.",".$ldapuconfig['LDAP_BASEDN']."</h3></td></tr>\n");
      print("<tr><td colspan=2><hr></td></tr>\n");
    }
    $ldap_dn = @ldap_explode_dn($info[0]['dn'],0);
    if(!isset($ldap_dn[0]) || $ldap_dn[0] == "") $ldap_dn[0] = $CourierAdmin_user_ldap_RDN_default;
    print("<tr><td><b><font color=\"#FF0000\">RDN:</font></b></td><td><input type=\"text\" size=\"40\" name=\"user_ldap_rdn\" value=\"".$ldap_dn[0]."\"></td></tr>");
    print("<tr><td colspan=2><hr></td></tr>\n");
    $k = array_keys($info[0]['objectclass']);
    sort($k);
    foreach($k as $c)
    {
      if(!is_numeric($c)) continue;
      if($c == 0)
      {
        print("<tr><td><b><font color=\"#FF0000\">objectclass:</font></b></td><td>".$info[0]['objectclass'][$c]."</td></tr>");
      }
      else
      {
        print("<tr><td>&nbsp;</td><td>".$info[0]['objectclass'][$c]."</td></tr>");
      }
    }
    print("<tr><td colspan=2><hr></td></tr>\n");
  }
  foreach($CourierAdmin_user_ldap_must as $attrib)
  {
    if(!strcasecmp($attrib, $ldapuconfig['LDAP_CLEARPW']))
    {
      if(isset($info[0]) && isset($info[0][$attrib]))
      {
        $k = array_keys($info[0][$attrib]);
        sort($k);
        foreach($k as $c)
        {
          if(!is_numeric($c)) continue;
          if($c == 0)
          {
            print("<tr><td><b><font color=\"#FF0000\">".$attrib.":</font></b></td><td><input type=\"password\" size=\"40\" name=\"user_ldap_".$attrib."[]\" value=\"".$info[0][$attrib][$c]."\"></td></tr>");
          }
          else
          {
            print("<tr><td>&nbsp;</td><td><input type=\"password\" size=\"40\" name=\"user_ldap_".$attrib."[]\" value=\"".$info[0][$attrib][$c]."\"></td></tr>");
          }
        }
        print("<tr><td>&nbsp;</td><td><input type=\"password\" size=\"40\" name=\"user_ldap_".$attrib."[]\" value=\"\"></td></tr>");
      }
      else
      {
        print("<tr><td><b><font color=\"#FF0000\">".$attrib.":</font></b></td><td><input type=\"password\" size=\"40\" name=\"user_ldap_".$attrib."[]\" value=\"\"></td></tr>");
      }
    }
    else
    {
      if(isset($info[0]) && isset($info[0][$attrib]))
      {
        $k = array_keys($info[0][$attrib]);
        sort($k);
        foreach($k as $c)
        {
          if(!is_numeric($c)) continue;
          if($c == 0)
          {
            print("<tr><td><b><font color=\"#FF0000\">".$attrib.":</font></b></td><td><input type=\"text\" size=\"40\" name=\"user_ldap_".$attrib."[]\" value=\"".$info[0][$attrib][$c]."\"></td></tr>");
          }
          else
          {
            print("<tr><td>&nbsp;</td><td><input type=\"text\" size=\"40\" name=\"user_ldap_".$attrib."[]\" value=\"".$info[0][$attrib][$c]."\"></td></tr>");
          }
        }
        print("<tr><td>&nbsp;</td><td><input type=\"text\" size=\"40\" name=\"user_ldap_".$attrib."[]\" value=\"\"></td></tr>");
      }
      else
      {
        print("<tr><td><b><font color=\"#FF0000\">".$attrib.":</font></b></td><td><input type=\"text\" size=\"40\" name=\"user_ldap_".$attrib."[]\" value=\"\"></td></tr>");
      }
    }
    print("<tr><td colspan=2><hr></td></tr>\n");
  }
  foreach($CourierAdmin_user_ldap_may as $attrib)
  {
    if(!strcasecmp($attrib, $ldapuconfig['LDAP_CLEARPW']))
    {
      if(isset($info[0]) && isset($info[0][$attrib]))
      {
        $k = array_keys($info[0][$attrib]);
        sort($k);
        foreach($k as $c)
        {
          if(!is_numeric($c)) continue;
          if($c == 0)
          {
            print("<tr><td><b><font color=\"#0000FF\">".$attrib.":</font></b></td><td><input type=\"password\" size=\"40\" name=\"user_ldap_".$attrib."[]\" value=\"".$info[0][$attrib][$c]."\"></td></tr>");
          }
          else
          {
            print("<tr><td>&nbsp;</td><td><input type=\"password\" size=\"40\" name=\"user_ldap_".$attrib."[]\" value=\"".$info[0][$attrib][$c]."\"></td></tr>");
          }
        }
        print("<tr><td>&nbsp;</td><td><input type=\"password\" size=\"40\" name=\"user_ldap_".$attrib."[]\" value=\"\"></td></tr>");
      }
      else
      {
        print("<tr><td><b><font color=\"#0000FF\">".$attrib.":</font></b></td><td><input type=\"password\" size=\"40\" name=\"user_ldap_".$attrib."[]\" value=\"\"></td></tr>");
      }
    }
    else
    {
      if(isset($info[0]) && isset($info[0][$attrib]))
      {
        $k = array_keys($info[0][$attrib]);
        sort($k);
        foreach($k as $c)
        {
          if(!is_numeric($c)) continue;
          if($c == 0)
          {
            print("<tr><td><b><font color=\"#0000FF\">".$attrib.":</font></b></td><td><input type=\"text\" size=\"40\" name=\"user_ldap_".$attrib."[]\" value=\"".$info[0][$attrib][$c]."\"></td></tr>");
          }
          else
          {
            print("<tr><td>&nbsp;</td><td><input type=\"text\" size=\"40\" name=\"user_ldap_".$attrib."[]\" value=\"".$info[0][$attrib][$c]."\"></td></tr>");
          }
        }
        print("<tr><td>&nbsp;</td><td><input type=\"text\" size=\"40\" name=\"user_ldap_".$attrib."[]\" value=\"\"></td></tr>");
      }
      else
      {
        print("<tr><td><b><font color=\"#0000FF\">".$attrib.":</font></b></td><td><input type=\"text\" size=\"40\" name=\"user_ldap_".$attrib."[]\" value=\"\"></td></tr>");
      }
    }
    print("<tr><td colspan=2><hr></td></tr>\n");
  }
  print("<tr><td colspan = 2><input type=hidden name=\"user_type\" value=\"LDAP\"><input type=submit value=\"Save User\" name=\"Act_user_edit\"></td></tr>");
  print("</form>");
  //print_r($info);








  break;
  default:
  echo "Nicht implementiert: ".$_REQUEST['user_type'];
}

?>
</table>
<p><a href="list_user.php">Zur&uuml;ck zur Anzeige der User !</a></p>
</body>
</html>                                                  
Return current item: CourierAdmin