<?php
$user_admin=$_COOKIE["user_admin"];
$jahrgang=$_COOKIE["jahrgang"];

if ($jahrgang == ""){ require("jahrgang.php"); exit;} //Wenn man kein Jahrgang ausgew&auml;hlt hat wird die Jahrgangsauswahlseite geladen

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_mailcomp'");
$row = $result->fetch_array();
if ($row[0] != 1){ include("kurs/rechte.php"); exit;}
// Rechte&uuml;berpr&uuml;fung ende


if ($jahrgang < 04){
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>Nicht unterst&uuml;tzter Jahrgang</title>
</head>
<body>
<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=\"10\" background=\"../images/box_e1.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"3%\" background=\"../images/box_top.gif\" valign=\"top\">
                    <img src=\"../images/i_login.gif\"></td>
                    <td width=\"95%\" background=\"../images/box_top.gif\">
                    <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                    <span class=\"sh\">Nicht unterst&uuml;tzter Jahrgang</span><br>
                    <img height=\"5\" src=\"../images/blank.gif\" width=\"1\"></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=\"22\"></td>
                    <td colspan=\"2\"><br>
                    <p align=\"center\">Eine Abfrage des LDAP Servers ist erst ab dem Jahrgang 04 m&ouml;glich<br>
                                        Die Mailadressen werden erst seit dem Jahrgang 04 in der Benutzerverwaltung gespeichert!
                    </p>
                    <p align=\"center\"><a href=\"hauptframe_jahr.php\">
                    <img border=\"0\" src=\"../images/zurueck.gif\" width=\"77\" height=\"22\"></a><br>
                    </p>
                    <p></p>
                    <p></p>
                    <p></p>
                    <p></p>
                    <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>
";
exit;
}


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

   
if ($ds) {
        
  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>Mailadressen pr&uuml;fen</title>
  </head>
  <body>
  <table border=\"0\" style=\"border-collapse: collapse\" width=\"100%\" cellpadding=\"0\" height=\"90%\">
      <tr>
          <td>
          <div align=\"center\">
              <table 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 nowrap=\"nowrap\" align=\"left\" width=\"98%\" background=\"../images/box_top.gif\" valign=\"top\">
                      <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                      <font class=\"hd\">Mailadressen pr&uuml;fen</font><br>
                      <font class=\"sh\">Aktive Studenten des Jahrgangs $jahrgang</font><br>
                      <img height=\"5\" src=\"../images/blank.gif\" width=\"1\"></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=\"22\"></td>
                      <td>
                        <table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#E7E8ED\" width=\"100%\">
                        <tr>
                          <td width=\"10%\" 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\">
                          <b><font size=\"2\">uid</font></b>
                          </td>
                          <td width=\"40%\" align=\"center\" 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\">
                          <b><font size=\"2\">Mail DB</font></b></td>
                          <td width=\"40%\" align=\"center\" 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\">
                          <b><font size=\"2\">Mail LDAP</font></b></td>
                          <td width=\"10%\" align=\"center\" 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\">
                          <b><font size=\"2\">Status</font></b></td>
                        </tr>";
                        $db = dbconnect();
                        
                        $ln_anzahl  = 0;  // Anzahl der gelesenen Datens&auml;tze
                        $ln_anzahl1 = 0;  // Anzahl der &uuml;bereinstimmend gefundenen Datens&auml;tze
                        $ln_anzahl2 = 0;  // Anzahl der nicht &uuml;bereinstimmend gefundenen Datens&auml;tze
                        $ln_anzahl3 = 0;  // Anzahl der notwendigen Updates in der DB
                        
                        $query = "SELECT uid, mail, Jahrgang 
                                    FROM stud 
                                   WHERE Jahrgang=\"$jahrgang\" 
                                     AND durchgefallen != 'Y' 
                                     AND hs='L'
                                    ORDER BY uid asc";
                        $result = $db->query($query)
                               or die ("Cannot execute query");
                        while ($row = $result->fetch_array()){
                        
                        $ln_anzahl = $ln_anzahl + 1;  //Anzahl der DS z&auml;hlen

                        // Suchen &uuml;ber uid
                        $sr=ldap_search($ds,"o=fhov", "cn=$row[uid]");

                        $info = ldap_get_entries($ds, $sr);
                        
                        $maildb = strtolower($row['mail']);
                        
                        for ($i=0; $i<$info["count"]; $i++) {
                          $mailldap = strtolower($info[$i]['mail'][0]);
                          $mailldap2 = strtolower($info[$i]['mail'][1]);
                        }
                        

                        if ($maildb == trim($maildb))  // Wenn String in DB ein Leerzeichen enth&auml;lt, entfernen und korigierten Wert in DB schreiben
                        {
                          NULL;
                        }else{
                          $maildb = trim($maildb);
                          $sql = $db->query ("UPDATE stud SET mail='$maildb' where uid=\"$row[uid]\"");
                            if ($sql)
                            {
                              $ln_anzahl3 = $ln_anzahl3 + 1;  //Anzahl der DS z&auml;hlen, wie oft ein update durchgef&uuml;hrt wurde
                            }else{
                              echo"$maildb: Update nicht erfolgt<br>";
                            }
                        }


                        echo"
                        <tr>
                          <td width=\"10%\" 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\">
                          <font size=\"2\">$row[uid]</font>
                          </td>
                          <td width=\"40%\" align=\"center\" 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\">
                          <font size=\"2\">$maildb</font></td>
                          <td width=\"40%\" align=\"center\" 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\">
                          <font size=\"2\">$mailldap<br>$mailldap2</font></td>
                          <td width=\"10%\" align=\"center\" 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\">
                            <font size=\"2\">";
                             if ($maildb == $mailldap OR $maildb == $mailldap2)
                             {
                                echo "<img border=\"0\" src=\"ok.gif\"><br>";
                                $ln_anzahl1 = $ln_anzahl1 + 1;
                              }else{
                                echo "<img border=\"0\" src=\"cross.jpg\"><br>";
                                $ln_anzahl2 = $ln_anzahl2 + 1;
                             }
                             echo"
                           </font>
                          </td>
                        </tr>";
                        $maildb = "";
                        $mailldap = "";
                        $mailldap2 = "";
                        }
                        echo "
                        </table>
                       </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\">
                      <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>
  
  <p>

  <html>
  <head>
  <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
  <link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
  <title>Nicht unterst&uuml;tzter Jahrgang</title>
  </head>
  <body>
  <table border=\"0\" style=\"border-collapse: collapse\" width=\"100%\" cellpadding=\"0\" id=\"table1\">
      <tr>
          <td>
          <div align=\"center\">
              <table cellspacing=\"0\" cellpadding=\"0\" width=\"549\" border=\"0\" height=\"114\">
					<tr>
                      <td background=\"../images/box_e1.gif\">
                      &nbsp;</td>
                      <td nowrap=\"nowrap\" align=\"left\" background=\"../images/box_top.gif\" valign=\"top\" colspan=\"2\">
                      <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                      <span class=\"sh\">Statistik</span></td>
                      <td nowrap=\"nowrap\" align=\"right\" background=\"../images/box_e2.gif\" height=\"28\">
                      <img height=\"10\" src=\"../images/blank.gif\"></td>
                  </tr>
                  <tr>
                      <td background=\"../images/box_l.gif\">
                        &nbsp;</td>
                      <td valign=\"top\">
                        &nbsp;</td>
                      <td>
                        &nbsp;</td>
                      <td background=\"../images/box_r.gif\" height=\"17\">
                      &nbsp;</td>
                  </tr>
                  <tr>
                      <td background=\"../images/box_l.gif\">
                        <img height=\"4\" src=\"../images/blank.gif\">
                      </td>
                      <td valign=\"top\">
                        $ln_anzahl</td>
                      <td>
                        Datens&auml;tze &uuml;berpr&uuml;ft
                      </td>
                      <td background=\"../images/box_r.gif\" height=\"17\">
                      </td>
                  </tr>
                  <tr>
                      <td background=\"../images/box_l.gif\">
                        <img height=\"1\" src=\"../images/blank.gif\">
                      </td>
                      <td>
                        $ln_anzahl1</td>
                      <td>
                        richtige E-Mail Adressen
                      </td>
                      <td background=\"../images/box_r.gif\" height=\"17\">
                      <img height=\"1\" src=\"../images/blank.gif\"></td>
                  </tr>
                  <tr>
                      <td background=\"../images/box_l.gif\">
                        <img height=\"1\" src=\"../images/blank.gif\">
                      </td>
                      <td>
                        $ln_anzahl2</td>
                      <td>
                        falsche E-Mail Adressen
                      </td>
                      <td background=\"../images/box_r.gif\" height=\"17\">
                      <img height=\"1\" src=\"../images/blank.gif\"></td>
                  </tr>
                  <tr>
                      <td background=\"../images/box_l.gif\">
                        <img height=\"1\" src=\"../images/blank.gif\">
                      </td>
                      <td>
                        $ln_anzahl3</td>
                      <td>
                        Mailadressen korrigiert, da sie ein Leerzeichen enthielten
                      </td>
                      <td background=\"../images/box_r.gif\" height=\"17\">
                      <img height=\"1\" src=\"../images/blank.gif\"></td>
                  </tr>
                  <tr>
                      <td nowrap=\"nowrap\" align=\"left\" background=\"../images/box_e3.gif\">
                      <img height=\"16\" src=\"../images/blank.gif\"></td>
                      <td nowrap=\"nowrap\" align=\"left\" width=\"77\" background=\"../images/box_bottom.gif\">
                      &nbsp;</td>
                      <td nowrap=\"nowrap\" align=\"left\" width=\"428\" background=\"../images/box_bottom.gif\">
                      <img height=\"16\" src=\"../images/blank.gif\" width=\"8\"></td>
                      <td nowrap=\"nowrap\" align=\"right\" background=\"../images/box_e4.gif\" height=\"18\">
                      <img height=\"16\" src=\"../images/blank.gif\"> </td>
                  </tr>
              </table>
          </div>
          </td>
      </tr>
  </table>
  </body>
  </html>
  ";

  ldap_close($ds);
   
} else {
  echo "<h4>Verbindung zum LDAP Server nicht m&ouml;glich</h4>"; 
} // Ende if
?>