<?php
#########Konfiguration#######################################
$verz = "upload/";                          // relatives Uploadverzeichnis (relativ zum 'Standort' dieser Datei, wohin die Dateien kopiert werden sollen
$trennzeichen_excel = ";";                  // Das Trennzeichen das Excel f&uuml;r CVS-Dateien nimmt ist je nach Excelversion unterschiedlich: 
                                            // Entweder $trennzeichen_excel = ";"; --> Semikolon
                                            //     oder $trennzeichen_excel = ","; --> Komma
#############################################################

############
##
## 13.09.2011:   Änderung des Passwortformats von dd.mm.jjjj auf ddmmjjjj weil auf der Loginseite das so angegeben ist.
##
############
include("kurs/datenbankanbindung.php");     // f&uuml;gt die Datenbankanbindung ein: Sys:\php\includes\kurs\datenbankanbindung.php
function utf8_fopen_read($fileName) { 
    $fc = iconv('windows-1250', 'utf-8', file_get_contents($fileName)); 
    $handle=fopen("php://memory", "rw"); 
    fwrite($handle, $fc); 
    fseek($handle, 0); 
    return $handle; 
} 

setcookie("ck_verz","$verz");  // Verzeichnis in Cookie speichern
$user_admin=$_COOKIE["user_admin"];  //Cookie laden

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


$limitedext = array(".csv"); //Erlaubte Dateieindung beim Upload

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

switch($action){
default:

$heute  = mktime(0, 0, 0, date("m"), date("d"), date("Y"));  // heutiges Datum
$heute_format = date("Y-m-d", $heute);  // ISO-8601 Format wie in der Datenbank: Formatierung f&uuml;r Vergleiche erforderlich

function deleteFiles($verz){
  $handle = opendir($verz);
  while ($file = readdir ($handle)) {
    if($file != "." && $file != "..") {

      $datei = $verz.$file;

      if(@unlink($datei)){
          $error = TRUE;
      }
      else
      {
          $error = FALSE;
      }
    }
  }
  closedir($handle);
  return $error;
}

deleteFiles($verz);  // Dateien werden im Uploadverzeichnis gel&ouml;scht, falls noch welche darin w&auml;ren


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>CSV Upload</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\" allign=\"center\"  width=\"50%\" 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\">CSV Upload</font><br>
                    <span class=\"sh\">Import eines Studienjahrgangs</span>
                    <img height=\"5\" src=\"../../images/blank.gif\" width=\"1\"></font></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>
                    <form action='".$_SERVER['PHP_SELF'] . "?action=csvimp" ."' enctype=\"multipart/form-data\" method='post'>
                        <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-width:0; border-collapse: collapse\" bordercolor=\"#111111\" width=\"100%\" id=\"AutoNumber1\" height=\"142\">
                            <tr>
                                <td width=\"60%\" style=\"border-style: none; border-width: medium\" >
                                  <b>Datei w&auml;hlen:</b><br>Erlaubte Dateiendungen: *.csv
                                </td>
                                <td width=\"40%\"align=\"left\" style=\"border-style: none; border-width: medium\" >
                                  <p>
                                  <input name=\"file\" type=\"file\" size=\"30\"></p>
                                </td>
                             </tr>
                             <tr>
                                <td width=\"60%\" style=\"border-style: none; border-width: medium\">
                                </td>
                                <td width=\"40%\"align=\"right\" style=\"border-style: none; border-width: medium\">
                                  <p>
                                  <input type=\"submit\" value=\"Abschicken\" name=\"B1\" size=\"20\">
                                </td>
                            </tr>
                        </table>
                    </form>
                    </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>";



break;
case "csvimp":

// http://www.tutorials.de/tutorials65420.html
$tempname = $_FILES['file']['tmp_name'];
$name = $_FILES['file']['name'];

$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];
$einheit = "Byte";  // F&uuml;r die Efolgsmeldung.

$extlimit = "yes"; //Do you want to limit the extensions of files uploaded
$ext = strrchr($name,'.');
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {

$err[] = "
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../../styles_pc.css\" type=\"text/css\">
<title>Falscher Dateityp</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=\"98%\" background=\"../../images/box_top.gif\" valign=\"top\">
                    <img height=\"10\" src=\"../../images/blank.gif\" width=\"1\"><br>
                    <span class=\"sh\">Falscher Dateityp</span>
                    <img height=\"5\" src=\"../../images/blank.gif\" width=\"1\"></font></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>
                     <br>
                     <p align=\"center\">Es d&uuml;rfen nur csv-Dateien hochgeladen werden!
                     <p>
                     <p align=\"center\"><p align=\"center\"><a href=\"javascript:history.back()\">
                    <img border=\"0\" src=\"../../images/zurueck.gif\" width=\"77\" height=\"22\"></a>
                     </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\">
                      <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>";
}

if($size > "9900000") {
$err[] = "
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../../styles_pc.css\" type=\"text/css\">
<title>Datei zu gro&szlig;</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=\"98%\" background=\"../../images/box_top.gif\" valign=\"top\">
                    <img height=\"10\" src=\"../../images/blank.gif\" width=\"1\"><br>
                    <span class=\"sh\">Datei zu gro&szlig;</span>
                    <img height=\"5\" src=\"../../images/blank.gif\" width=\"1\"></font></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>
                     <br>
                     <p align=\"center\">Die Datei welche Sie hochladen wollen, ist zu gross!<br>Maximale Dateigroesse betr&auml;gt 9.900 KB!
                     <p>
                     <p align=\"center\"><p align=\"center\"><a href=\"javascript:history.back()\">
                    <img border=\"0\" src=\"../../images/zurueck.gif\" width=\"77\" height=\"22\"></a>
                     </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\">
                      <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>";
}
$name = str_replace (array("&auml;", "&ouml;", "&uuml;", "&szlig;", "&Auml;", "&Ouml;", "&Uuml;"," "), array("ae", "oe", "ue", "ss", "Ae", "Oe", "Ue", "_"), $name);
$heute  = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));  // heutiges Datum & Zeit: http://de2.php.net/manual/de/function.date.php
$heuteformat = date("H-i-s_Y-m-d", $heute);  // Formatierung, wie es vor dem Dateinamen erscheinen soll
$datum_db = date("Y-m-d H:i:s", $heute);  // Formatierung, wie es vor dem Dateinamen erscheinen soll
$neu = $heuteformat."_".$name;
setcookie("ck_neu","$neu");

If(empty($err)) {
  move_uploaded_file("$tempname", "$verz$neu");  // Unterverzeichnisse wie folgt: copy("$tempname", "pdf/$neu");

  $row = 1;                                                              // Anzahl der Arrays
  $handle = utf8_fopen_read ("$verz$neu","r");                                      // Datei zum Lesen &ouml;ffnen

  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>Datens&auml;tze pr&uuml;fen</title>
   </head>
   <body>
    <table border=\"0\" style=\"border-collapse: collapse\" width='150%'  cellpadding=\"0\" height=\"90%\">
      <tr>
          <td>
            <div align=\"center\">
              <table cellspacing=\"0\" cellpadding=\"0\"  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\">Datens&auml;tze pr&uuml;fen</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\">
                          <tr>
                            <td width=\"4%\" 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=\"1\">uid</font></b>
                            </td>
                            <td width=\"10%\" align=\"left\" 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=\"1\">Nachname</font></b>
                            </td>
                            <td width=\"10%\" align=\"left\" 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=\"1\">Vorname</font></b>
                            </td>
                            <td width=\"1%\" align=\"left\" 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=\"1\">Matrikelnr.</font></b>
                            </td>
                            <td width=\"2%\" align=\"left\" 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=\"1\">KFZ</font></b>
                            </td>
                            <td width=\"2%\" align=\"left\" 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=\"1\">HS</font></b>
                            </td>
                            <td width=\"2%\" align=\"left\" 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=\"1\">PLZ</font></b>
                            </td>
                            <td width=\"13%\" align=\"left\" 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=\"1\">Ort</font></b>
                            </td>
                            <td width=\"14%\" align=\"left\" 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=\"1\">Stra&szlig;e</font></b>
                            </td>
                            <td width=\"2%\" align=\"left\" 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=\"1\">Stg</font></b>
                            </td>
                            <td width=\"11%\" align=\"left\" 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=\"1\">StgText</font></b>
                            </td>
                            <td width=\"2%\" align=\"left\" 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=\"1\">AG</font></b>
                            </td>
                            <td width=\"2%\" align=\"left\" 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=\"1\">Jahr</font></b>
                            </td>
                            <td width=\"3%\" align=\"left\" 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=\"1\">Passwort</font></b>
                            </td>
                            <td width=\"44%\" align=\"left\" 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=\"1\">Mail</font></b>
                            </td>
                          </tr>";


                          while ( ($data = fgetcsv ($handle, 1000, $trennzeichen_excel)) !== FALSE )                  // Daten werden aus der Datei in ein Array $data gelesen
                          {
                            $num = count ($data);                                                     // Felder im Array $data werden gez&auml;hlt
                            //print "<p> $num fields in line $row: <br>\n";
                            print "\n";
                            $row++;                                                                   // Anzahl der Arrays wird
                                                                                                      // inkrementiert
                                                                                                    
                           $passwd = str_replace(".", "", $data[13]);

                            print "<tr>";
                            //   for ($c=0; $c < $num; $c++) {                                        // FOR-Schleife: Oder Anzahl der zu importierenden Felder ($num)
                            print "<td><font size=\"1\">".strtolower($data[0]) . "</td>\n";       // uid
                            print "<td><font size=\"1\">".$data[1] . "</td>\n";                   // Nacname
                            print "<td><font size=\"1\">".$data[2] . "</td>\n";                   // Vorname
                            print "<td><font size=\"1\">".$data[3] . "</td>\n";                   // Matrikelnr
                            print "<td><font size=\"1\">".$data[4] . "</td>\n";                   // KFZ
                            print "<td><font size=\"1\">".$data[5] . "</td>\n";                   // HS
                            print "<td><font size=\"1\">".$data[6] . "</td>\n";                   // PLZ
                            print "<td><font size=\"1\">".$data[7] . "</td>\n";                   // ORT
                            print "<td><font size=\"1\">".$data[8] . "</td>\n";                   // Stra&szlig;e
                            print "<td><font size=\"1\">".$data[9] . "</td>\n";                   // STG
                            print "<td><font size=\"1\">".$data[10] . "</td>\n";                   // StgText
                            print "<td><font size=\"1\">".strtoupper($data[11]) . "</td>\n";      // AG
                            print "<td><font size=\"1\">".$data[12] . "</td>\n";                  // Jahrgang
                            #print "<td><font size=\"1\">".strtolower($data[12]) . "</td>\n";      // Passwort in Kleinbuchstaben
                            print "<td><font size=\"1\">".$passwd . "</td>\n";      // Passwort
                            print "<td><font size=\"1\">".$data[14] . "</td>\n";                  // Mail
                            //   }
                            print "</tr>";
                            $ln_inserts[]++;                                                      //z&auml;hlt die Anzahl der importierenden DS
                          }
                        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>
              <br>";
              $zahl=count($ln_inserts);

              echo"

              <table border=\"0\" style=\"border-collapse: collapse\" width='40%'  cellpadding=\"0\">
              <tr>
                <td>
                   <div align=\"center\">
                     <table cellspacing=\"0\" cellpadding=\"0\"  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\">Sollen die $zahl Datens&auml;tze importiert werden?</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>
                          <form action='".$_SERVER['PHP_SELF'] . "?action=csvimp2" ."' enctype=\"multipart/form-data\" method='post'>
                          <table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#E7E8ED\">
                          <tr>
                            <td width=\"70%\" 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=\"1\">Auswahl</font></b>
                            </td>
                            <td width=\"30%\" align=\"left\" 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=\"1\">Markieren</font></b>
                            </td>
                          </tr>
                          <tr>
                            <td width=\"70%\"><font size=\"1\">Datens&auml;tze nicht importieren
                            </td>
                            <td width=\"30%\"><font size=\"1\"><input type=\"radio\" name=\"import\" value=\"0\" checked>
                            </td>
                          </tr>
                          <tr>
                            <td width=\"70%\"><font size=\"1\">Datens&auml;tze importieren
                            </td>
                            <td width=\"30%\"><font size=\"1\"><input type=\"radio\" name=\"import\" value=\"1\">
                            </td>
                          </tr>
                          <tr>
                            <td width=\"70%\">
                              &nbsp;
                            </td>
                            <td width=\"30%\">
                              &nbsp;
                            </td>
                          </tr>
                          <tr>
                            <td width=\"70%\">
                              &nbsp;
                            </td width=\"30%\">
                            <td>
                              <input type=\"submit\" value=\"Abschicken\" name=\"B1\" size=\"20\">
                            </td>
                          </tr>
                          </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>
  ";
}else {

  foreach($err as $error)
  echo "$error";
  exit;
} //Ende if error
fclose ($handle);


break;
case "csvimp2":

$neu=$_COOKIE["ck_neu"];  //CSV-Dateinamen um Datei l&ouml;schen zu k&ouml;nnen
$verz=$_COOKIE["ck_verz"];  //Uploadverzeichnis aus Cookie holen


if ($_POST['import'] == "0")  // DS sollen nicht importiert werden
{
  $dateiname="$verz$neu";
  if (@unlink($dateiname))  // Funktion l&ouml;scht Dateien und gibt true / false zur&uuml;ck
  {
  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>Import beendet</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\"></td>
                        <td width=\"95%\" background=\"../../images/box_top.gif\">
                        <img height=\"10\" src=\"../../images/blank.gif\" width=\"1\"><br>
                        <span class=\"sh\">Importvorgang abgebrochen</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\">Datems&auml;tze wurden nicht importiert!<br>Die CSV-Datei wurde vom Server gel&ouml;scht!
                        </p>
                        <p align=\"center\"><a href=\"javascript:window.close()\">
                        <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>";

  }else{

    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>Datei nicht gel&ouml;scht</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\"></td>
                        <td width=\"95%\" background=\"../../images/box_top.gif\">
                        <img height=\"10\" src=\"../../images/blank.gif\" width=\"1\"><br>
                        <span class=\"sh\">Importvorgang abgebrochen</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\">Datems&auml;tze wurden nicht importiert!<br>Die CSV-Datei ($neu) konnte <b>nicht</b> vom Server gel&ouml;scht werden!
                        </p>
                        <p align=\"center\"><a href=\"javascript:window.close()\">
                        <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>";
  } // End if

} elseif($_POST['import'] == "1") // DS sollen importiert werden
{
  $row = 1;                                                                   // Anzahl der Arrays
  $handle = utf8_fopen_read ("$verz$neu","r");                                          // Datei zum Lesen &ouml;ffnen
  while ( ($data = fgetcsv ($handle, 1000, $trennzeichen_excel)) !== FALSE )                  // Daten werden aus der Datei in ein Array $data gelesen
  {
    $num = count ($data);                                                     // Felder im Array $data werden gez&auml;hlt
    //print "<p> $num fields in line $row: <br>\n";
    print "\n";
    $row++;                                                                   // Anzahl der Arrays wird
                                                                              // inkrementiert
   #$pwd  = strtolower($data[13]);
   $pwd = str_replace(".", "", $data[13]);
   $passwd = md5($pwd);
   $uid  = strtolower($data[0]);
   
   $geb = preg_split('/\./', $data[13]);
   $tag = $geb[0];
   $mon = $geb[1];
   $jahr = $geb[2];
   $gebtag = "$jahr-$mon-$tag";
   //$passwd = mysqli_escape_string($db,$passwd);
   
                                                                              
    // ACHTUNG: AN DIESER ZEILE NICHTS &Auml;NDERN, da die erste Zeile des Strings rausgefiltert wird bei der Ausgabe der nichtimportierten DS
    $insert[] .= "INSERT INTO stud (uid, nachname, vorname, mtknr, pokfz, hs, poplz, poOrt, poStrasse, stg, stgText, AG, Jahrgang, Pwd, mail, geburtstag) VALUES ( lower('$data[0]')
                                     ,'$data[1]'
                                     ,'$data[2]'
                                     ,'$data[3]'
                                     ,'$data[4]'
                                     ,'$data[5]'
                                     ,'$data[6]'
                                     ,'$data[7]'
                                     ,'$data[8]'
                                     ,'$data[9]'
                                     ,'$data[10]'
                                     ,upper('$data[11]')
                                     ,'$data[12]'
                                     ,'$passwd'
                                     ,'$data[14]'
                                     ,'$gebtag'
                                   )
                 ";
  }

  $db = dbconnect();
  for ($i=0;$i<sizeof($insert);$i++)
  {
    $sql = $db->query("$insert[$i]");
    if (!$sql) {
      $unimported[]= $insert[$i];
    }
  }
  fclose ($handle);
  $dateiname="$verz$neu";
  if (@unlink($dateiname))  // Funktion l&ouml;scht Dateien und gibt true / false zur&uuml;ck
  {
  
$y = str_replace (array("INSERT INTO stud (uid, nachname, vorname, mtknr, pokfz, hs, poplz, poOrt, poStrasse, stg, stgText, AG, Jahrgang, Pwd, mail, geburtstag) VALUES ( lower('"), array(""), $unimported);

    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>Import beendet</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\"></td>
                        <td width=\"95%\" background=\"../../images/box_top.gif\">
                        <img height=\"10\" src=\"../../images/blank.gif\" width=\"1\"><br>
                        <span class=\"sh\">Importvorgang erfolgreich</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\">Der Importvorgang wurde erfolgreich beendet!<br>Die CSV-Datei wurde vom Server gel&ouml;scht!
                        </p>";
                          $num1 = count ($y);  // Anzahl der nicht importierten UIDs
                          if ($y != "")
                          {
                            echo"Es wurden $num1 Datens&auml;tze nicht importiert<br>
                                 Wahrscheinlich ist die UID bereits in der Datenbank vorhanden,<br>
                                 oder eines der Felder ist zu lang:
                                 <p>";
                          }
                          // Ausgabe der nicht importierten DS
                          for ($i=0;$i<sizeof($y);$i++)
                          {
                            $b = strpos ($y[$i], "'");
                            $e = substr($y[$i], 0, $b);
                            
                            print strtolower($e); // Ausgabe der nicht importierten UIDs
                            echo"<br>";
                          }
                        echo"
                        <p align=\"center\"><a href=\"javascript:window.close()\">
                        <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>";

  }else{

    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>Datei nicht gel&ouml;scht</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\"></td>
                        <td width=\"95%\" background=\"../../images/box_top.gif\">
                        <img height=\"10\" src=\"../../images/blank.gif\" width=\"1\"><br>
                        <span class=\"sh\">Importvorgang erfolgreich</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\">Der Importvorgang wurde erfolgreich beendet!<br>Die CSV-Datei ($neu) konnte <b>nicht</b> vom Server gel&ouml;scht werden!
                        </p>";
                          $num1 = count ($y);  // Anzahl der nicht importierten UIDs
                          if ($y != "")
                          {
                            echo"Es wurden $num1 Datens&auml;tze nicht importiert<br>
                                 Wahrscheinlich ist die UID bereits in der Datenbank vorhanden,<br>
                                 oder eines der Felder ist zu lang:
                                 <p>";
                          }
                          // Ausgabe der nicht importierten DS
                          for ($i=0;$i<sizeof($y);$i++)
                          {
                            $b = strpos ($y[$i], "'");
                            $e = substr($y[$i], 0, $b);
                            
                            print strtolower($e); // Ausgabe der nicht importierten UIDs
                            echo"<br>";
                          }
                        echo"
                        <p align=\"center\"><a href=\"javascript:window.close()\">
                        <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>";
  } // End if

}else { // Es wurde kein Radiobutton gew&auml;hlt! Technisch aber ausgeschlossen

  echo "sie m&uuml;ssen eine Auswahl treffen";

}

function deleteFiles($verz){
  $handle = opendir($verz);
  while ($file = readdir ($handle)) {
    if($file != "." && $file != "..") {

      $datei = $verz.$file;

      if(@unlink($datei)){
          $error = TRUE;
      }
      else
      {
          $error = FALSE;
      }
    }
  }
  closedir($handle);
  return $error;
}

deleteFiles($verz);  // Dateien werden im Uploadverzeichnis gel&ouml;scht, falls noch welche darin w&auml;ren

} // ende Action
?>