Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a replacement for LDAP_sort() method which is deprecated?

Tags:

php

ldap

As the title says, when I try to use ldap_sort() method it returns deprecated method

Why ? Is there any equivalent ?

Also is it to possible to sort A-Z in the filter ?

like image 843
Srithovic Avatar asked Jun 29 '17 12:06

Srithovic


2 Answers

ldap_sort is based on a C-function that is deprecated for years by now. And as ldap_sort usually does not do what most people think it does we decided to also deprecate the PHP function.

Especially when the result can also be achieved with userland-code. Have a look at https://github.com/zendframework/zend-ldap/blob/master/src/Collection/DefaultIterator.php#L379-L403 to see how we solved this issue within zend-ldap. You can use any sort-function to get the sorting you prefer and are not bound to the sorting-algorithm that is implemented within ldap_sort but no one really knows how it sorts.

When you sort using your user land-function you can implement exactly the sorting you need for your special case. Sort by multiple attributes, sort case-insensitive, etc. etc.

The alternative is to use server-side sorting which

a) needs us to implement LDAP-COntrols in PHPs C-code (which isn'T finished at the moment) and b) needs the server to implement that

This functionality currently isn't available in PHP as we didn't (yet) manage to finish that feature. It is the better solution but as it's not necessarily available on the server you might still want to do the sorting on the client side. And when you search multiple times using paged results you will still need to sort the results by hand.

So ldap_sort is deprecated for reasons but it still is available. And it will be available in all the PHP 7.x branches. But you should use a different sorting-solution. We hope to be able to ship server-sided sorting in PHP 8 (No idea when that will be due though) but that might not help you as it depends on the servers capabilities.

So fetch your results, add them to an array and implement a sort-function for that array is the easiest thing you can do and that sorting algorithm does exactly what you want!

Disclaimer: I'm the one that implemented the DEPRECATED-Warning

like image 200
heiglandreas Avatar answered Oct 16 '22 15:10

heiglandreas


You can see, As Per PHP ldap_sort Documentation :

Sort the result of a LDAP search, returned by ldap_search().

As this function sorts the returned values on the client side it is possible that you might not get the expected results in case you reach the sizelimit either of the server or defined within ldap_search().

Warning : This feature has been DEPRECATED as of PHP 7.0.0. Relying on this feature is highly discouraged.

Also, As Per PHP Compatibility DeprecatedFunctionsSniff Doc in Github :

    'ldap_sort' => array(
        '7.0' => false,
        'alternative' => null,
    ),

In this testcase, it's written as ldap_sort will be Deprecated from PHP 7.0 version & It has alternative = NULL Means No Alternative

To answer your other question as

Also is it to possible to sort A-Z in the filter ?

The LDAP protocol defined in the RFC 2891 the LDAP Control Extension for Server Side Sorting of Search Results. You will have to check on the LDAP implementation you use (as it is not tagged on the question) if this control is implemented

like image 29
LuFFy Avatar answered Oct 16 '22 15:10

LuFFy