<?php
include_once 'classes/TestProjektSmarty.class.php';
require_once("config.inc.php");
$templatename = substr(basename($_SERVER['PHP_SELF']),0,-3)."html";
$smarty = new Smarty();
require_once "functions.php";
require_once "language/german.inc.php";
require_once("func_rollenrechte.php");

// Rechteüberprüfung
  if(!rore($uid,'2','RE')){echo "Keine Rechte";exit;}
// Rechteüberprüfung ende


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


if($action == ''){
    
    
    
    # Verfügbare Professoren
    
    $ds = @ldap_connect($global_ldap_serverhsnet, $global_ldap_porthsnet);
    if ($ds) {
      $r                 = @ldap_bind($ds, $global_ldap_user, $global_ldap_pwd);
      $_ldap_dn          = "ou=Benutzer,ou=OUHochschulnetzwerk,dc=hsnet,dc=hs-ludwigsburg,dc=de";
      $filter            = "(&(objectclass=person)(cn=*)(sAMAccountName=*))";
      $sr                = @ldap_search($ds, $_ldap_dn, $filter);
      $result            = ldap_get_entries($ds,$sr);
      $anzahl            = ldap_count_entries($ds, $sr);

      for($i=0;$i<$anzahl;$i++){
        # AcitveDirectory oder OPEN LDAP
        if($global_ldap_art == 1){
         if($result[$i]['givenname'][0] != '' AND $result[$i]['sn'][0] != ''){
          $row['vorname']           = utf8_encode($result[$i]['givenname'][0]);
          $row['nachname']          = utf8_encode($result[$i]['sn'][0]);
         }
        }
        if($global_ldap_art == 2){
         if($result[$i]['givenname'][0] != '' AND $result[$i]['sn'][0] != ''){
          $row['vorname']           = $result[$i]['givenname'][0];
          $row['nachname']          = $result[$i]['sn'][0];
         }
        }

        $row['uid']               = $result[$i]['samaccountname'][0];
        #echo "$vorname $nachname -> $uid<br>";
        $table_data1[]            = $row;
      }

      $table_data1 = sortArrayByFields(
                                       $table_data1,
                                       array('nachname' => array(SORT_ASC, SORT_STRING))
                                       );

      # Bereits vorhandene UIDs löschen
      $query1 = "SELECT imtuid, uid
                   FROM dep_user
               ORDER BY nachname ASC";
    
      $result1 = $db->query ($query1)
                 or die ("Cannot execute query1");
      while ($row1 = $result1->fetch_array()){
       $table_data1 = removeElementWithValue($table_data1, "uid", $row1['uid']);
      }
      # Arrynummerierung wieder richtig machen nach Löschen von werte {[0],[3][4][7]} -> {[0],[1][2][3]}. Sonst spinnt loop in html
      $table_data1 = array_values($table_data1);
      
      # Übergabe des arrays in html
      $smarty->assign('table_data1', $table_data1);

    }else{
      echo "Keine Verbindung zu LDAP";
    }
    
      
    # Alle Administratoren
    $query2 = "SELECT imtuid, vorname, nachname, fakultaet, uid
                 FROM dep_user
             ORDER BY nachname ASC";
  
    $result2 = $db->query ($query2)
              or die ("Cannot execute query2");
    
    while ($row2 = $result2->fetch_array()){
      $table_data2[] = $row2;
    }
    $smarty->assign('table_data2', $table_data2);    
}

if($action == 'register'){
   
   $uid = $_POST['dozent'];
   if($uid == ''){
    echo "<meta http-equiv=\"refresh\" content=\"0; URL=".$_SERVER['PHP_SELF']."\">";
    exit;
   }
   
   $ds = @ldap_connect($global_ldap_serverhsnet, $global_ldap_porthsnet);
    #ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
    if ($ds) {
      # bind wird benötigt, sonst werden die Umlaute bei Namen  nicht richtig angezeigt ?!!?
      $r                 = @ldap_bind($ds, $global_ldap_user, $global_ldap_pwd);
      $_ldap_dn          = "ou=Benutzer,ou=OUHochschulnetzwerk,dc=hsnet,dc=hs-ludwigsburg,dc=de";
      $sr                = ldap_search($ds, $_ldap_dn, "sAMAccountName=$uid");
      $info              = ldap_get_entries($ds, $sr);
      $anzahl            = ldap_count_entries($ds, $sr);
      
      # AcitveDirectory oder OPEN LDAP
      if($global_ldap_art == 1){
        $vorname           = $info[0]['givenname'][0];
        $nachname          = $info[0]['sn'][0];
      }
      if($global_ldap_art == 2){
        $vorname           = $info[0]['givenname'][0];
        $nachname          = $info[0]['sn'][0];
      }
      
      $mail              = $info[0]['mail'][0];
      $datum             = date("Y-m-d H:i:s");

        $db = dbconnect();
        $sql1 = $db->query("INSERT INTO dep_user ( vorname
                                                 , nachname
                                                 , mail
                                                 , uid
                                                 , erstell_dat
                                                 )
                             VALUES
                                                 ( '$vorname'
                                                 , '$nachname'
                                                 , '$mail'
                                                 , '$uid'
                                                 , '$datum'
                                                 )
                           ");
       
  
  }
  echo "<meta http-equiv=\"refresh\" content=\"0; URL=".$_SERVER['PHP_SELF']."\">";

}


if($action == 'del'){
    $imtuid = $_GET['imtuid'];
  
    ## Muss man nochmals überdenken, wenn ein Prof noch Deputate erfasst hat.
   
    $db = dbconnect();
    $del1 = $db->query("DELETE FROM dep_rollen_user_zuord WHERE imtuid = $imtuid");
    $del1 = $db->query("DELETE FROM dep_userlog WHERE imtuid = $imtuid");
    $del1 = $db->query("DELETE FROM dep_user WHERE imtuid = $imtuid");
    
  echo "<meta http-equiv=\"refresh\" content=\"0; URL=".$_SERVER['PHP_SELF']."\">";
  
}

$smarty->assign('action', "$action");
$smarty->display("$template/$templatename");

?>