jan*_*ane 13 javascript php ajax jquery
我想使用post从活动目录中检索信息(例如缩略图).
<?php
/**
* Get a list of users from Active Directory.
*/
$ldap_password = $_POST['password'];
$ldap_username = $_POST['username'];
$server = 'ldap://xxxxxxxxxxxxxxxxxxxxxx';
$domain = 'xxxxxxxxxxxxxxxxx';
$port = 389;
$ldap_connection = ldap_connect($server, $port);
if (FALSE === $ldap_connection){
// Uh-oh, something is wrong...
}
// We have to set this option for the version of Active Directory we are using.
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version');
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0); // We need this for doing an LDAP search.
if (TRUE === ldap_bind($ldap_connection, $ldap_username.$domain, $ldap_password)){
$ldap_base_dn = "OU=Employees,OU=Accounts,OU=xxxxx,DC=xxxxxx,DC=xxxxxxx,DC=com";
$search_filter = '(&(objectCategory=person)(samaccountname=*))';
$attributes = array();
$attributes[] = 'givenname';
$attributes[] = 'mail';
$attributes[] = 'samaccountname';
$attributes[] = 'sn';
$result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter, $attributes);
$maxPageSize = 1000;
if (FALSE !== $result){
$entries = ldap_get_entries($ldap_connection, $result);
for ($x=0; $x<$entries['count']; $x++){
if (!empty($entries[$x]['givenname'][0]) &&
!empty($entries[$x]['mail'][0]) &&
!empty($entries[$x]['samaccountname'][0]) &&
!empty($entries[$x]['sn'][0]) &&
'Shop' !== $entries[$x]['sn'][0] &&
'Account' !== $entries[$x]['sn'][0]){
$ad_users[strtoupper(trim($entries[$x]['samaccountname'][0]))] = array('email' => strtolower(trim($entries[$x]['mail'][0])),'first_name' => trim($entries[$x]['givenname'][0]),'last_name' => trim($entries[$x]['sn'][0]));
}
}
}
ldap_unbind($ldap_connection); // Clean up after ourselves.
}
$message .= "Retrieved ". count($ad_users) ." Active Directory users\n";
?>
Run Code Online (Sandbox Code Playgroud)
我尝试使用http://localhost:8666/web1/activedirectory.php以查看它是否返回任何内容,但它返回以下错误,因为结果> 1000.
警告:ldap_search():返回部分搜索结果:第28行的C:\ xampp\htdocs\web1\activedirectory.php中超出了Sizelimit
注意:未定义的变量:第46行的C:\ xampp\htdocs\web1\activedirectory.php中的消息
下面是我要将.php文件链接到上面文件的jquery :
$('.leaderboard li').on('click', function () {
$.ajax({
url: "../popupData/activedirectory.php", // php file with link to the active directory.
type: "POST",
data: {id:$(this).find('.parent-div').data('id')},
success: function(data){
console.log(data);
data = JSON.parse(data);
$('#popup').fadeIn();
//call for the thumbnail photo
// etc ..
},
error: function(){
alert('failed, possible script does not exist');
}
});
});
Run Code Online (Sandbox Code Playgroud)
ibr*_*rir 11
第一个问题:
您必须附加一个img元素,而不是像这样设置文本:
$('#imagesofBadges').append('<img src="' + data[0].BadgeImage + '"/>');
Run Code Online (Sandbox Code Playgroud)
第二个问题:
附加图像时添加一个类属性,以便您可以使用该类名使用jQuery获取它们,如下所示:
var $img = $('<img src="' + data[0].BadgeImage + '"/>'); // create the image
$img.addClass('badge-image'); // add the class .badge-image to it
$('#imagesofBadges').append($img); // append it
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用如下选择器获取这些图像:
$('#imagesofBadges .badge-image'); // will fetch all the elements that have the class .badge-image that are inside #imagesofBadges.
Run Code Online (Sandbox Code Playgroud)
编辑:
如果你想#imagesofBadges在追加新图片之前删除里面的所有图片,请使用:
// fetch all the images inside #imagesofBadges and remove them
$('#imagesofBadges img').remove();
// append the new image
$('#imagesofBadges').append('<img src="' + data[0].BadgeImage + '"/>');
Run Code Online (Sandbox Code Playgroud)
首先,你得到的错误与POST,AJAX或PHP无关.它是由LDAP查询过于通用引起的:
ldap_search(): Partial search results returned: Size limit exceeded
Run Code Online (Sandbox Code Playgroud)
每个jQuery调用只能返回一个图像,因此您需要逐个检索缩略图,并且每个调用只需要搜索并返回一条记录(即READ,而不是SEARCH).
这意味着您需要在PHP调用中发送您的用户ID,以便脚本可以知道要返回哪个缩略图...这样就可以了.
但PHP 没有使用该信息.您搜索所有名称,这意味着它无论如何都无法工作,但它甚至没有启动,因为LDAP搜索会破坏.
$search_filter = '(&(objectCategory=person)(samaccountname=*))';
Run Code Online (Sandbox Code Playgroud)
在上面的行中,您需要添加一些过滤器.例如,您在排行榜中获得的SAM帐户名称是否为ID?如果是这样,你可以做类似的事情
$kid = 'NonExistingAccount';
if (preg_match('#SAM_NAME_MATCHING_REGEX#', $_POST['id'])) {
$kid = $_POST['id'];
}
$search_filter = "(&(objectCategory=person)(samaccountname={$kid}))";
Run Code Online (Sandbox Code Playgroud)
并确保只检索一条记录.此时你可以去提取图像(如果内存服务的是位图格式),并将其转换为适合jQuery的形式:
$bitmap = $entries[0]['picture'];
// Some error checking would probably be good
$gd = imageCreateFromString($bitmap);
// Here you'll probably want to resize your image. Create
// another GD object with ImageCreateTrueColor and use imageCopyResampled
// with the appropriate size.
// Then, inform jQuery that a PNG is coming along
header('Content-Type: image/png');
// and send the PNG
imagePNG($gd);
// and nothing else (shouldn't matter, but you never know, and anyway...).
exit();
Run Code Online (Sandbox Code Playgroud)
您有一个包含多个元素(例如,您的员工)的HTML部分.
非常重要:此部分将由PHP使用LDAP搜索生成,以便您获得所需的信息.您可能需要对LDAP搜索进行分页,以避免返回太多结果(和错误),如前所述.
但是,一旦执行此操作,您将拥有每个用户的可分辨名称.
<ul class="leaderboard">
...
<li data-dn="CN=Jeff Smith,OU=Sales,DC=Fabrikam,DC=COM">
<img class="placeholder" />
<span class="cn">Jeff Smith</span>
<span class="ou">Sales</span>
</li>
...
</ul>
Run Code Online (Sandbox Code Playgroud)
在上面,您从LDAP搜索中读取"Jeff Smith"等.但你不能轻易地在PHP中放置一个图像,HTML不允许它(好吧,它确实如此,因为这个答案与你的类似问题显示,但你说不喜欢),所以你把它放在一个占位符而不是使用CSS适当的大小.
您还可以在图像中放置一个动画GIF,上面写着"loading ...".
为了提高效率,您可以在data-dn属性中保存每个结果的DN.
无论是在click或onload()上,还是在页面更改时有AJAX分页,都可以检索所有图像.这与您已有的代码非常相似.
$('.leaderboard li').on('click', function () {
// Get the image. This returns nothing if it has already been loaded.
var img = $(this).find('img.placeholder');
if (img.length === 0) { return; }
var dn = $(this).attr('data-dn');
// Load image from its DN.
// See https://stackoverflow.com/questions/4285042/asychronously-load-images-with-jquery
img.src = 'load-image.php?dn=' + dn;
});
Run Code Online (Sandbox Code Playgroud)
该load-image.php脚本将接收$_GET['dn']并将要求使用提供的DN 执行LDAP读取,并检索适当的图像属性.
然后你只需输出它header()和image*()你喜欢的功能(例如imageJPEG()或imagePNG()).
您可以在AJAX中执行此操作并发送编码为base64的图像(上面的链接中的代码),但它更复杂,并且您只需要感兴趣,当您需要时,保存只发送一个呼叫而不是二十个二十个图像的时间会立即丢失如果您的Web服务器对base64进行gz编码(或者当它没有增加33%时),则将编码为base64而不是二进制的JPEG发送,大小增加5-10%.