通过PHP设置thumbnailPhoto属性

时间:2011-11-29 14:13:49

标签: php active-directory ldap

在我的工作中,我们最近升级到Lync(办公室通讯器的花哨版本),并且有一个照片字段,可以在电子邮件和聊天中显示您的照片。

问题是,这个“默认公司图片”存储在thumbnailPhoto属性中用户帐户的活动目录对象中。

使用ADSI我可以玩设置不同的值,但不幸的是,除了更新这张照片之外我在网上找不到任何东西。

我已经在PHP和Active Directory中完成了其他工作,如果有人能够通过PHP设置这张照片,我很乐意看到你是如何做到的。

提前谢谢!

1 个答案:

答案 0 :(得分:0)

我在Google上搜索了此问题的解决方案。 由于找不到适合自己需求的产品,因此我分享了我的解决方案。

属性图像是包含图像的base64字符串。

if(!isset($_POST["username"]))
    exit(json_encode(array("error" => true, "errormsg" => "No username attr set")));
$username = preg_replace("[^a-zA-Z0-9]", "", $_POST["username"]);

if(!isset($_POST["image"]))
    exit(json_encode(array("error" => true, "errormsg" => "No image attr set")));
$image = base64_decode($_POST["image"]);

// Set LDAP Information
$ds = ldap_connect("ldap://IP_ADDRESS");
$r = ldap_bind($ds, "DOMAIN\USERNAME", "PASSWORD");

// Search for UPN in given OU
$search = "(&(objectCategory=user)(objectClass=user)(userPrincipalName=$username))";
$sr = ldap_search($ds, "OU=OU_NAME,DC=DC_NAME,DC=local", $search);
$data = ldap_get_entries($ds, $sr);

if($data["count"] == 1){
    // OPTIONAL: Show existing image
    /*echo "<strong>Distinguished Name: </strong>" . $data[0]["dn"] . "<br />";
    if (isset($data[0]["thumbnailphoto"]) && isset($data[0]["thumbnailphoto"][0])) {
        echo '<img src="data:image/jpeg;base64,' . base64_encode($data[0]["thumbnailphoto"][0]) . '" height=200/><br>';
    }
    else {
        echo "<strong>Photo not set</strong><br />";
    }*/

    if(ldap_mod_replace($ds, $data[0]["dn"], array("thumbnailPhoto" => $image))){
        exit(json_encode(array("error" => false, "errormsg" => "thumbnailphoto for user $username was set")));
    }else{
        exit(json_encode(array("error" => true, "errormsg" => "thumbnailphoto for user $username could not be set")));
    }
}elseif($data["count"] == 0){
    exit(json_encode(array("error" => true, "errormsg" => "No user found for $username")));
}elseif($data["count"] > 1){
    exit(json_encode(array("error" => true, "errormsg" => "Multiple users found for $username")));
}
ldap_close($ds);