/** * ldap 备份 * @param int $cardid * @param string $username * @param string $password 未加密密码 * @return null */ function ldapBack($cardid,$username,$password){ $connect = ldap_connect(LDAP_HOST,LDAP_PORT); if(!$connect){ $log = Logger::getLogger(); $log->fatal('备份LDAP连接失败!'); } ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3); @$back_bind = ldap_bind($connect, LDAP_ADMIN_USER, LDAP_ADMIN_PW); $add_entry = array(); $add_entry['objectclass'][] = 'top'; $add_entry['objectclass'][] = 'person'; $add_entry['objectclass'][] = 'inetOrgPerson'; $add_entry['cn'] = $username; $add_entry['uid'] = $cardid; $add_entry['sn'] = $username; $add_entry['userpassword'] = "{MD5}".base64_encode(md5($password,true)); $dn = "uid={$cardid},ou=people,dc=buct,dc=edu,dc=cn"; //查询ldap中是否已经存在相应数据 $base_dn = "DC=buct,DC=edu,DC=cn"; $filter = "(uid=$cardid)"; $read = ldap_search($connect, $base_dn, $filter); $info = ldap_get_entries($connect, $read); if(empty($info['count'])) {//不存在数据,使用添加方法添加数据 $boo = ldap_add($connect,$dn,$add_entry); }else {//数据已经存在,使用编辑方法修改数据 $boo = ldap_modify($connect,$dn,$add_entry); } //$boo = ldap_modify($connect,$dn,$add_entry); ldap_close($connect); $_data = array(); $_data['cardid'] = $info[0]['uid'][0]; $_data['name'] = $info[0]['sn'][0]; return $_data; }