php 如何替换ldap_sort?

yiytaume  于 2023-01-24  发布在  PHP
关注(0)|答案(1)|浏览(140)

我正在将一个应用程序移植到PHP 8,遇到了ldap_sort不再可用的问题。我可以从我的ldap服务器检索数据,并且可以按sn排序,然后按givenname排序,但我在每个术语上都收到了警告。我假设这与一些ldap记录中的不完整数据有关,但不确定。我的代码如下,我收到的警告是:

警告:试图访问第50行C:\inetpub\wwwroot\inc\queries_php\ldap\qryldap.php中int类型值的数组偏移量
警告:试图访问第51行C:\inetpub\wwwroot\inc\queries_php\ldap\qryldap.php中int类型值的数组偏移量

其中第50行是以$lastname开头的,第51行是$givenname。我正在努力找出一种方法来循环遍历结果,以防止显示警告。请问有什么想法吗?
卡卢姆

function QueryLDAP() {
    // -------------- CHANGE VARIABLES TO SUIT YOUR ENVIRONMENT  --------------
    //LDAP server address
        $server = "ldap://192.168.1.1";

    //domain user to connect to LDAP
        $user = "administrator@company.com";
        
    //user password
            $psw = "p@55w0rd!";

    //FQDN path where search will be performed. OU - organizational unit / DC - domain component
        $dn = "OU=Users,DC=Company,DC=local";
    //Search query. CN - common name (CN=* will return all objects)
        $search = "(&(objectCategory=person)(objectClass=user)(company=Company))";
        $attributes = array("objectSid", "sAMAccountName", "givenname", "sn", "cn", "mail", "department", "telephonenumber", "mobile", "company", "title", "facsimileTelephoneNumber", "description");

    // connecting to LDAP server
        $ds=ldap_connect($server);
        $r=ldap_bind($ds, $user , $psw); 

    // performing search
        $sr=ldap_search($ds, $dn, $search, $attributes);

    // Sort by lastname
    //  ldap_sort($ds, $sr, 'sn');  ldap_sort deprecated in PHP8 - see below!!

    $GLOBALS['ldap'] = ldap_get_entries($ds, $sr);  // allows this variable to be used in functions

    // Sort the array by lastname, firstname ascending:
    //  from example 3, https://www.php.net/manual/en/function.array-multisort.php
        foreach ($GLOBALS['ldap'] as $key => $row) {
            $lastname[$key]  = $row['sn'];
            $firstname[$key] = $row['givenname'];
        }
        array_multisort($lastname, SORT_ASC, $firstname, SORT_ASC, $GLOBALS['ldap']);

    // close connection
        ldap_close($ds);
}
n6lpvg4x

n6lpvg4x1#

ldap_get_entries返回一个带有附加字段'count'的数组。这个'count'字段包含一个整数值,显然不是数组。由于这个原因,ldap_get_entries()的结果实际上不是一个多数组。
要将这些条目作为多数组进行操作,一个快速的方法是保存计数字段,删除它,然后排序,一旦完成,您可以添加计数字段,如果不需要,也可以不添加。
这就是我如何处理https://github.com/ltb-project/ltb-ldap/blob/main/src/Ltb/Ldap.php
对于ldap_sort的删除,还有许多其他解决方案:What is a replacement for LDAP_sort() method which is deprecated?

相关问题