<?php
// Seksyen Pengurusan Data
// Zahar Wahab
// Last Update : 28 Mei 2015

if(phpCAS::getUser()){
$dn = "ou=People,dc=upm,dc=edu,dc=my";
$ldaphost = "172.16.245.30";  // your ldap servers
$ldapport = 389;                 // your ldap server's port number
$ldaprdn  = 'uid=upmidsso,ou=Systems,dc=upm,dc=edu,dc=my';     // ldap rdn or dn
$ldappass = 'T3r841kD4r1L4d4ng';  // associated password

$ds = ldap_connect($ldaphost, $ldapport)
          or die("Could not connect to $ldaphost");

if (ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3)) {
} else {
    echo "Failed to set protocol version to 3";
}

if ($ds) {
    $ldapbind = ldap_bind($ds, $ldaprdn, $ldappass);

    if ($ldapbind) {
    } else {
        echo "LDAP bind failed...";
    }

}
$filter = "uid=".phpCAS::getUser();
$justthese = array("ou", "sn", "givenname", "mail", "displayName","cn","upmEduPersonActive","upmedupersontype","employeenumber","upmedupersontype","upmedupersoncollege","upmedupersonicnumber");
$sr=ldap_search($ds, $dn, $filter);
if (!$sr) {
    echo "LDAP-Errno: " . ldap_errno($ds) . "<br />\n";
    echo "LDAP-Error: " . ldap_error($ds) . "<br />\n";
    die("Argh!<br />\n");
}

$info = ldap_get_entries($ds, $sr);
for ($i=0; $i<$info["count"]; $i++) {
//echo ($i+1)." ".$info[$i]["cn"][0]."<br>";
//}
$_SESSION['fullname'] = $info[$i]["cn"][0];
$_SESSION['employeenumber'] = $info[$i]["employeenumber"][0];
$_SESSION['upmedupersonicnumber'] = $info[$i]["upmedupersonicnumber"][0];
$_SESSION['hidsource'] = $info[$i]["upmedupersontype"][0];
//echo $_SESSION['sumber']."test".$_SESSION['employeenumber'];
}
}
?>
