<?php
$user_admin=$_COOKIE["user_admin"];
include("kurs/datenbankanbindung.php");     // f&uuml;gt die Datenbankanbindung ein: Sys:\php\includes\kurs\datenbankanbindung.php


// Rechte&uuml;berpr&uuml;fung
$db = dbconnect();
if ($user_admin == ""){ require("index.php"); exit;} //Wenn man nicht angemeldet ist, darf man nicht auf die Seite
$result = $db->query("SELECT 1 FROM pros_admin_rechte, pros_admin_rechte_zuord , pros_admin where pros_admin_rechte.pros_admin_rolle = pros_admin_rechte_zuord.pros_admin_rolle AND pros_admin_rechte_zuord.faid = pros_admin.faid AND pros_admin.user = '$user_admin' AND pros_admin_rechte_zuord.pros_admin_rolle = 'a_ldapsuch'");
$row = $result->fetch_array();
if ($row[0] != 1){ include("kurs/rechte.php"); exit;}
// Rechte&uuml;berpr&uuml;fung ende

// Grundlegende Abfolge bei LDAP ist verbinden, binden, suchen,
// interpretieren des Sucheergebnisses, Verbindung schlie&szlig;en

   if (isset($_GET['action']))
      $action = $_GET['action'];
   else
      $action = "";

   switch($action){
   default:
echo"

<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
<title>Suchen</title>
</head>
<body onload=\"if(document.login)document.login.suchstring.focus();return false;\">
<table border=\"0\" style=\"border-collapse: collapse\" width=\"100%\" cellpadding=\"0\" height=\"100%\" id=\"table1\">
    <tr>
        <td>
        <div align=\"center\">
            <table cellspacing=\"0\" cellpadding=\"0\" width=\"550\" border=\"0\">
                <tr>
                    <td width=\"1%\" background=\"../images/box_e1.gif\">
                      <img height=\"40\" src=\"../images/blank.gif\" width=\"22\">
                    </td>
                    <td width=\"95%\" background=\"../images/box_top.gif\">
                      <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                      <font class=\"hd\">LDAP Suche</font><br>
                      <font class=\"sh\">In der Benutzerverwaltung suchen</font>
                      <img height=\"5\" src=\"../images/blank.gif\" width=\"1\">
                    </td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"3%\" background=\"../images/box_top.gif\" valign=\"top\">
                    </td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e2.gif\">
                      <img height=\"40\" src=\"../images/blank.gif\" width=\"22\">
                    </td>
                </tr>
                <tr>
                    <td width=\"1%\" background=\"../images/box_l.gif\">
                      <img height=\"1\" src=\"../images/blank.gif\" width=\"95%\">
                     </td>
                    <td colspan=\"2\"><br>
                      <p></p>
                      <p></p>
                      <p></p>
                      <p></p>
                       <form action="; echo $_SERVER['PHP_SELF'] . "?action=suchen"; echo" method=\"POST\" name=\"login\">
                        <input type=\"hidden\" name=\"tparam\" value=\"945180225\">
                        <table cellspacing=\"1\" cellpadding=\"1\" width=\"100%\" border=\"0\">
                            <tr>
                              <td class=\"formdesc\" width=\"60%\">
                                Suchbegriff:
                              </td>
                              <td width=\"40%\">
                                <input type=\"text\" name=\"suchstring\" size=\"40\" tabindex=\"2\"><br>
                              </td>
                            </tr>
                            <tr>
                              <td class=\"formdesc\" width=\"60%\">
                                Suchpfad:
                              </td>
                              <td width=\"40%\">
                                <input type=\"radio\" name=\"suchpfad\" value=\"sn\" checked>Nachnamen<br>
                                <input type=\"radio\" name=\"suchpfad\" value=\"cn\">UIDs
                              </td>
                            </tr>
                            <tr>
                              <td align=\"center\" colspan=\"2\">
                                <br>
                                <font size=\"1\">Es k&ouml;nnen auch nur Teile des Begriffs eingegeben werden: Begri*</font>
                                <p>
                              </td>
                            </tr>
                            <tr>
                              <td align='center' colspan=\"2\">
                                <input title=\"Suchen\" type=\"image\" src=\"../images/suchen.gif\" border=\"0\">
                              </td>
                           </tr>
                              </td>
                            </tr>
                        </table>
                      </form>
                      <p><br>
                      </p>
                    </td>
                    <td width=\"1%\" background=\"../images/box_r.gif\">
                      <img height=\"1\" src=\"../images/blank.gif\" width=\"22\">
                    </td>
                </tr>
                <tr>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"1%\" background=\"../images/box_e3.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"98%\" background=\"../images/box_bottom.gif\" colspan=\"2\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"8\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e4.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"> </td>
                </tr>
            </table>
        </div>
        </td>
    </tr>
</table>
</body>
</html>
";
break;
case "suchen":
if($_POST['suchstring'] != "")
{
  $suchstring=$_POST['suchstring'];
  $suchpfad=$_POST['suchpfad'];
  setcookie("suchpfad","$suchpfad");
  setcookie("suchstring","$suchstring");
}

if($suchstring == "")
{
 $suchpfad=$_COOKIE["suchpfad"];
 $suchstring=$_COOKIE["suchstring"];
}


$nummer = 0 + $_GET['nummer'];

if(empty($_GET['sort']))
{ 
  $sort = "desc"; 
}else{
  NULL;
}


if(isset($_GET['sort0']))
{
  if($sort0 == "asc") 
  { 
    $sort = "desc"; 
  } else { 
    $sort = "asc"; 
  }
}

if(isset($_GET['sort1']))
{
  if($sort1 == "asc") 
  { 
    $sort = "desc"; 
  } else { 
    $sort = "asc"; 
  }
}

if(isset($_GET['sort3']))
{
  if($sort3 == "asc") 
  { 
    $sort = "desc"; 
  } else { 
    $sort = "asc"; 
  }
}

if(isset($_GET['sort4']))
{
  if($sort4 == "asc") 
  { 
    $sort = "desc"; 
  } else { 
    $sort = "asc"; 
  }
}

if(isset($_GET['sort5']))
{
  if($sort5 == "asc") 
  { 
    $sort = "desc"; 
  } else { 
    $sort = "asc"; 
  }
}



$ds=@ldap_connect("141.10.102.1", "389");  // muss ein g&uuml;ltiger LDAP Server
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);

if ($ds) {

   $r=ldap_bind($ds);    // das ist ein "anonymer" bind, typischerweise nur Lese Zugriff

   // Suchen des Nachnamen-Eintrags
   $suchstring2=$suchstring;
   #$sr=ldap_search($ds,"o=fhov", "cn=$suchstring");
   $sr=ldap_search($ds,"o=fhov", "$suchpfad=$suchstring2");


   $anzahl = ldap_count_entries($ds,$sr);

   $info = ldap_get_entries($ds, $sr);
   
   // Funktion mit der Sortierung
   function multi_sort($input, $key, $type) {
    if(is_array($input) && is_numeric($key)) {
        $tmp_arr = array();
        $output = array();
        foreach($input as $i_key => $i_value) {
            $tmp_arr[$i_key] = $i_value[$key];
        }
        natcasesort($tmp_arr);
        reset($tmp_arr);
        foreach($tmp_arr as $tmp_key => $tmp_val) {
            $output[] = $input[$tmp_key];
        }
        if(strtolower($type) != "asc") return $output;
        else return array_reverse($output);
    }
   }
  
  for ($i=0; $i<$info["count"]; $i++) {
    
    //$dn enth&auml;lt dn-string ==> 'cn=test,ou=extern,o=fhov': Nachfolgende Zeilen nur ou extrahieren
    $dn = $info[$i]['dn'];
    $b = strpos ($dn, "ou=")+3; // Position bestimmen wo ou anf&auml;ngt, +3 damit "ou=" nicht mitangezeigt wird, also Positon um 3 nach recht verschoben wird
    $c = strpos ($dn, ",o=");   // Position bestimmen woe ,o= anf&auml;ngt
    $d = $c-$b;                 // beide Werte subrahieren, dann erh&auml;lt man die L&auml;nge von ou
    $dn = substr($dn, $b, $d);
    

	     
    $old_arr[$i][0] = $info[$i]['cn'][0];
    $old_arr[$i][1] = $info[$i]['sn'][0];
    $old_arr[$i][2] = $info[$i]['givenname'][0];
    $old_arr[$i][3] = $dn;
    
    if ($dn == 'DOZ')          // bei den DOZ steht oft sowas wie "Prof." in dem Feld AG --> AG ausblenden
    {
      $old_arr[$i][4] = '';
    }else{
      $old_arr[$i][4] = $info[$i]['generationqualifier'][0];
    };

    $old_arr[$i][5] = $info[$i]['mail'][0];
  }
  
  $new_arr =  multi_sort($old_arr, $nummer, "$sort");
  

  echo"
  <html>
  
  <head>
  <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
  <link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
  <title>Suchergebnisse</title>
  </head>
  
  <body>
    <table valign=\"top\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\" border=\"0\">
        <tr>
            <td width=\"10\" background=\"../images/box_e1.gif\">
            <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
            <td align=\"left\" width=\"98%\" background=\"../images/box_top.gif\" valign=\"top\">
            <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
            <font class=\"hd\">Suchergebnisse zu Begriff $suchstring</font><br>";
            if ($anzahl == 1){$einheit="Suchergebnis";}else{$einheit="Suchergebnisse";}
            echo"
            <font class=\"sh\">$anzahl $einheit</font><br>
            <img height=\"5\" src=\"../images/blank.gif\" width=\"1\"></td>
            <td align=\"right\" width=\"1%\" background=\"../images/box_e2.gif\">
            <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
        </tr>
        <tr>
            <td width=\"1%\" background=\"../images/box_l.gif\">
            <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
            <td>
            <table valign=\"top\" border=\"0\" cellpadding=\"4\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#E7E8ED\" width=\"100%\" id=\"AutoNumber1\">
            <tr>
            <td width=\"15%\">
              <b><a href=".$_SERVER['PHP_SELF'] ."?nummer=0&action=suchen&sort0=$sort>UID</a></b>
            </td>
            <td width=\"20%\">
              <b><a href=".$_SERVER['PHP_SELF'] ."?nummer=1&action=suchen&sort1=$sort>Name</a></b>
            </td>
            <td width=\"15%\">
              <b><a href=".$_SERVER['PHP_SELF'] ."?nummer=3&action=suchen&sort3=$sort>Gruppe</a></b>
            </td>
            <td width=\"15%\">
              <b><a href=".$_SERVER['PHP_SELF'] ."?nummer=4&action=suchen&sort4=$sort>AG</a></b>
            </td>
            <td width=\"35%\">
              <b><a href=".$_SERVER['PHP_SELF'] ."?nummer=5&action=suchen&sort5=$sort>Mail</a></b>
            </td>
            </tr>";

             for ($i=0; $i<$info["count"]; $i++) {
/*                echo "01 ".$info[$i]['generationqualifier'][0]."<br>";
	        echo "02 ".$info[$i]['givenname'][0]."<br>";
	        echo "03 ".$info[$i]['fullname'][0]."<br>";
	        echo "04 ".$info[$i]['language'][0]."<br>";
                echo "05 ".$info[$i]['allowunlimitedcredit'][0]."<br>";
                echo "06 ".$info[$i]['accountbalance'][0]."<br>";
                echo "07 ".$info[$i]['groupmembership'][0]."<br>";
                echo "08 ".$info[$i]['groupmembership'][1]."<br>";
                echo "08a".$info[$i]['groupmembership'][2]."<br>";
                echo "08b".$info[$i]['groupmembership'][3]."<br>";
                echo "09 ".$info[$i]['sn'][0]."<br>";
                echo "10 ".$info[$i]['securityequals'][0]."<br>";
                echo "11 ".$info[$i]['profile'][0]."<br>";
                echo "12 ".$info[$i]['passwordrequired'][0]."<br>";
                echo "13 ".$info[$i]['passwordminimumlength'][0]."<br>";
                echo "14 ".$info[$i]['passwordexpirationtime'][0]."<br>";
                echo "15 ".$info[$i]['passwordexpirationinterval'][0]."<br>";
                echo "16 ".$info[$i]['passwordallowchange'][0]."<br>";
                echo "17 ".$info[$i]['objectclass'][0]."<br>";
                echo "18 ".$info[$i]['logintime'][0]."<br>";
                echo "19 ".$info[$i]['loginintruderaddress'][0]."<br>";
                echo "20 ".$info[$i]['loginexpirationtime'][0]."<br>";
                echo "21 ".$info[$i]['ndshomedirectory'][0]."<br>";
                echo "22 ".$info[$i]['cn'][0]."<br>";
                echo "23 ".$info[$i]['acl'][0]."<br>";
                echo "24 ".$info[$i][dn]."<br>";
                echo "<p>";
                echo "<p>";*/

                
                
             $uid          = utf8_decode($new_arr[$i][0]);
             $sn           = utf8_decode($new_arr[$i][1]);
             $givenname    = utf8_decode($new_arr[$i][2]);
             $dn           = utf8_decode($new_arr[$i][3]);
             $ag           = utf8_decode($new_arr[$i][4]);
             $mail         = utf8_decode($new_arr[$i][5]);

             
             echo"
              <tr>
              <td width=\"15%\" style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
               $uid
              </td>
              <td width=\"20%\" style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
                $sn, $givenname
              </td>
              <td valign=\"middle\" width=\"15%\" style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
                $dn
              </td>
              <td valign=\"middle\" width=\"15%\" style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
                $ag
              </td>
              <td width=\"35%\" style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
                $mail
              </td>
                </td>
             </tr>";
             $uid = "";
             $sn = "";
             $givenname = "";
             $dn = "";
             $mail = "";
             $ag = "";
            }
            echo"
             </table>
            </td>
            <td width=\"1%\" background=\"../images/box_r.gif\">
            <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
        </tr>
        <tr>
            <td align=\"left\" width=\"1%\" background=\"../images/box_e3.gif\">
            <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"></td>
            <td align=\"left\" width=\"98%\" background=\"../images/box_bottom.gif\">
            <img height=\"16\" src=\"../images/blank.gif\" width=\"8\"></td>
            <td  align=\"right\" width=\"1%\" background=\"../images/box_e4.gif\">
            <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"> </td>
        </tr>
    </table>
  </body>
  </html>";


   ldap_close($ds);

} else {
   echo "<h4>Verbindung zum LDAP Server nicht m&ouml;glich</h4>";
}

} //ENDE SWITCH CASE
?>