<?
//-- Telefon Rehberi --
// modify_res.php
// LDAP uzerinde degisiklik yapip sonuclari kullaniciya bildirir.
// Yazan: Mustafa Hadi Dilek, Subat 2000
//-------------
  
require("common.php");
  
  if (!
ValidateHOST()) {
    
CommonHeader("LDAP Değişiklik Sonuçları");
    echo 
"Üzgünüz. LDAP bilgileri ancak Ulakbim içindeki bilgisayarlardan değiştirilebilir...";
    
CommonFooter($footer_links);
    exit;
  }
  
  
$footer_links["rehber.php"] ="Ulakbim Telefon Rehberi";
  
$footer_links["modify.php"] ="LDAP Kaydı Değiştirme";
  
$footer_links["login.php"]  ="Başka Bir Kullanıcıya Geç";
  
  if (!isset(
$submit_change) && !isset($submit_cancel)) {
    @
header("Location: login.php?empty=YES");
    exit;
  }  
  
  if (!isset(
$HTTP_COOKIE_VARS["user"]) || !isset($HTTP_COOKIE_VARS["pwd"])) {
    @
header("Location: login.php?enablecookie=YES");
    exit;
  }

  
$user $HTTP_COOKIE_VARS["user"];
  
$pwd  $HTTP_COOKIE_VARS["pwd"];
  
  if (
$user=="" || $pwd=="") {
    @
header("Location: login.php?empty=YES");
    exit;
  }

  if(!empty(
$submit_cancel)) {
      echo 
"Değişiklik iptal edildi...\n";
      
CommonFooter($footer_links);
      exit;
  }
  elseif(!empty(
$submit_change)) {
    
$my_con = @ldap_connect(_LDAP_HOST,_LDAP_PORT);
    
      if (!
$my_con) {
      
CommonHeader("LDAP Degisiklik Sonuclari");
      echo 
"Hata. LDAP sunucu baglantısı saglanamadı.<BR>Sorun yetkililere iletildi...";
      
mail(getenv("SERVER_ADMIN"), // Target address
        
"Ulakbim Telefon Rehberi'nde hata!!!",  // Subject
        
"LDAP sunucusuna erisim basarisiz. Script: "._FILE_."\nErisim parametreleri:\nSunucu: "._LDAP_HOST."\nPort:" ._LDAP_PORT."\nSunucu calismiyor olabilir ya da erisim parametrelerinde hata var..."// Message body
        
"From: ".getenv("SERVER_ADMIN")."\nX-Mailer: PHP/" phpversion());
           
        
CommonFooter($footer_links);
        exit;
      }
      
      
$dn "uid=".$user.","._ORG;
           
      if(!(
$r = @ldap_bind($my_con$dn$pwd))) {
        @
header("Location: login.php?unauth=YES");
        exit;
      }
        
      
$base_dn    $dn;
      
$filter     "objectClass=*";                 // tum entryleri getirir...
      
$search_res ldap_read($my_con$base_dn$filter);
      
$info       ldap_get_attributes($my_con,ldap_first_entry($my_con,$search_res));
      
      
reset($ldap_attr_str);
      
      while(list(
$key$val)=each($ldap_attr_str)) {
      if (
in_array($key,$ldap_attr_changeable)) {
        if (
in_array($keyarray_keys($info))) {        
          $
$key trim($$key);
          
          if (empty($
$key)) $$key="-";
          
          if (
is_array(@$info[$key])) {
           if (@
$info[$key][0] != $$key$changes[$key] = $$key;
          }
          else {
            if (@
$info[$key] != $$key$changes[$key] = $$key;
          }
        }
      }
    }
    
    
CommonHeader("LDAP Degişiklik Sonuçları");
    
    if (
is_array(@$changes)) {
        if (
in_array("givenname",array_keys(@$changes)) || in_array("sn",array_keys(@$changes))) {
          if (
in_array("givenname",array_keys(@$changes)))
              
$changes["cn"] = $changes["givenname"];
            else
            
$changes["cn"] = @$info["givenname"][0];

             
$changes["cn"] .= " ";  
          
          if (
in_array("sn",array_keys(@$changes)))
              
$changes["cn"] .= $changes["sn"];
            else
               
$changes["cn"] .= @$info["sn"][0];
      }

         
my_ldap_modify($dn$pwd$dn$changes);
        
        
reset($changes);
      while(list(
$key,$val)=each($changes))
        echo 
$ldap_attr_str[$key]." <B>".$changes[$key]."</B> olarak degistirildi...<BR>\n";
    }
    else
      echo 
"Kaydınızda degişiklik yapılmadı...<BR>\n";
    
    
ldap_close($my_con);
  } 
// elseif

  
CommonFooter($footer_links);
?>