Aleksander Machniak
2014-08-05 db25c43e97f85121e9b14f0b4a2f2341e3a56966
Merge pull request #207 from kanarip/dev/net_ldap3

Merge Net_LDAP3
2 files added
2 files modified
1 files deleted
3843 ■■■■ changed files
program/lib/Net/LDAP3.php 2618 ●●●●● patch | view | raw | blame | history
program/lib/Net/LDAP3/Result.php 139 ●●●●● patch | view | raw | blame | history
program/lib/Roundcube/rcube_ldap.php 32 ●●●● patch | view | raw | blame | history
program/lib/Roundcube/rcube_ldap_generic.php 924 ●●●●● patch | view | raw | blame | history
program/lib/Roundcube/rcube_ldap_result.php 130 ●●●●● patch | view | raw | blame | history
program/lib/Net/LDAP3.php
New file
@@ -0,0 +1,2618 @@
<?php
/*
 +-----------------------------------------------------------------------+
 | Net/LDAP3.php                                                         |
 |                                                                       |
 | Based on code created by the Roundcube Webmail team.                  |
 |                                                                       |
 | Copyright (C) 2006-2014, The Roundcube Dev Team                       |
 | Copyright (C) 2012-2014, Kolab Systems AG                             |
 |                                                                       |
 | Licensed under the GNU General Public License version 3 or            |
 | any later version with exceptions for plugins.                        |
 | See the README file for a full license statement.                     |
 |                                                                       |
 | PURPOSE:                                                              |
 |   Provide advanced functionality for accessing LDAP directories       |
 |                                                                       |
 +-----------------------------------------------------------------------+
 | Authors: Thomas Bruederli <roundcube@gmail.com>                       |
 |          Aleksander Machniak <machniak@kolabsys.com>                  |
 |          Jeroen van Meeuwen <vanmeeuwen@kolabsys.com>                 |
 +-----------------------------------------------------------------------+
*/
require_once __DIR__ . '/LDAP3/Result.php';
/**
 * Model class to access a LDAP directories
 *
 * @package Net_LDAP3
 */
class Net_LDAP3
{
    public $conn;
    public $vlv_active = false;
    private $attribute_level_rights_map = array(
        "r" => "read",
        "s" => "search",
        "w" => "write",
        "o" => "delete",
        "c" => "compare",
        "W" => "write",
        "O" => "delete"
    );
    private $entry_level_rights_map = array(
        "a" => "add",
        "d" => "delete",
        "n" => "modrdn",
        "v" => "read"
    );
    /*
     * Manipulate configuration through the config_set and config_get methods.
     * Available options:
     *       'debug'           => false,
     *       'hosts'           => array(),
     *       'port'            => 389,
     *       'use_tls'         => false,
     *       'ldap_version'    => 3,        // using LDAPv3
     *       'auth_method'     => '',       // SASL authentication method (for proxy auth), e.g. DIGEST-MD5
     *       'numsub_filter'   => '(objectClass=organizationalUnit)', // with VLV, we also use numSubOrdinates to query the total number of records. Set this filter to get all numSubOrdinates attributes for counting
     *       'referrals'       => false,    // Sets the LDAP_OPT_REFERRALS option. Mostly used in multi-domain Active Directory setups
     *       'network_timeout' => 10,       // The timeout (in seconds) for connect + bind arrempts. This is only supported in PHP >= 5.3.0 with OpenLDAP 2.x
     *       'sizelimit'       => 0,        // Enables you to limit the count of entries fetched. Setting this to 0 means no limit.
     *       'timelimit'       => 0,        // Sets the number of seconds how long is spend on the search. Setting this to 0 means no limit.
     *       'vlv'             => false,    // force VLV off
     *       'config_root_dn'  => 'cn=config',  // Root DN to read config (e.g. vlv indexes) from
     *       'service_bind_dn' => 'uid=kolab-service,ou=Special Users,dc=example,dc=org',
     *       'service_bind_pw' => 'Welcome2KolabSystems',
     *       'root_dn'         => 'dc=example,dc=org',
     */
    protected $config = array(
        'sizelimit' => 0,
        'timelimit' => 0,
    );
    protected $debug_level = false;
    protected $list_page   = 1;
    protected $page_size   = 10;
    protected $cache;
    // Use public method config_set('log_hook', $callback) to have $callback be
    // call_user_func'ed instead of the local log functions.
    protected $_log_hook;
    // Use public method config_set('config_get_hook', $callback) to have
    // $callback be call_user_func'ed instead of the local config_get function.
    protected $_config_get_hook;
    // Use public method config_set('config_set_hook', $callback) to have
    // $callback be call_user_func'ed instead of the local config_set function.
    protected $_config_set_hook;
    // Not Yet Implemented
    // Intended to allow hooking in for the purpose of caching.
    protected $_result_hook;
    // Runtime. These are not the variables you're looking for.
    protected $_current_bind_dn;
    protected $_current_bind_pw;
    protected $_current_host;
    protected $_supported_control = array();
    protected $_vlv_indexes_and_searches;
    /**
     * Constructor
     *
     * @param array $config Configuration parameters that have not already
     *                      been initialized. For configuration parameters
     *                      that have in fact been set, use the config_set()
     *                      method after initialization.
     */
    public function __construct($config = array())
    {
        if (!empty($config) && is_array($config)) {
            foreach ($config as $key => $value) {
                if (empty($this->config[$key])) {
                    $setter = 'config_set_' . $key;
                    if (method_exists($this, $setter)) {
                        $this->$setter($value);
                    }
                    else if (isset($this->$key)) {
                        $this->$key = $value;
                    }
                    else {
                        $this->config[$key] = $value;
                    }
                }
            }
        }
    }
    /**
     *  Add multiple entries to the directory information tree in one go.
     */
    public function add_entries($entries, $attributes = array())
    {
        // If $entries is an associative array, it's keys are DNs and its
        // values are the attributes for that DN.
        //
        // If $entries is a non-associative array, the attributes are expected
        // to be positional in $attributes.
        $result_set = array();
        if (array_keys($entries) == range(0, count($entries) - 1)) {
            // $entries is sequential
            if (count($entries) !== count($attributes)) {
                $this->_error("Wrong entry/attribute count in " . __FUNCTION__);
                return false;
            }
            for ($i = 0; $i < count($entries); $i++) {
                $result_set[$i] = $this->add_entry($entries[$i], $attributes[$i]);
            }
        }
        else {
            // $entries is associative
            foreach ($entries as $entry_dn => $entry_attributes) {
                if (array_keys($attributes) !== range(0, count($attributes)-1)) {
                    // $attributes is associative as well, let's merge these
                    //
                    // $entry_attributes takes precedence, so is in the second
                    // position in array_merge()
                    $entry_attributes = array_merge($attributes, $entry_attributes);
                }
                $result_set[$entry_dn] = $this->add_entry($entry_dn, $entry_attributes);
            }
        }
        return $result_set;
    }
    /**
     * Add an entry to the directory information tree.
     */
    public function add_entry($entry_dn, $attributes)
    {
        // TODO:
        // - Get entry rdn attribute value from entry_dn and see if it exists in
        //   attributes -> issue warning if so (but not block the operation).
        $this->_debug("Entry DN", $entry_dn);
        $this->_debug("Attributes", $attributes);
        foreach ($attributes as $attr_name => $attr_value) {
            if (empty($attr_value)) {
                unset($attributes[$attr_name]);
            }
        }
        $this->_debug("C: Add $entry_dn: " . json_encode($attributes));
        if (($add_result = ldap_add($this->conn, $entry_dn, $attributes)) == false) {
            $this->_debug("S: " . ldap_error($this->conn));
            $this->_debug("S: Adding entry $entry_dn failed. " . ldap_error($this->conn));
            return false;
        }
        $this->_debug("LDAP: S: OK");
        return true;
    }
    /**
     * Add replication agreements and initialize the consumer(s) for
     * $domain_root_dn.
     *
     * Searches the configured replicas for any of the current domain/config
     * databases, and uses this information to configure the additional
     * replication for the (new) domain database (at $domain_root_dn).
     *
     * Very specific to Netscape-based directory servers, and currently also
     * very specific to multi-master replication.
     */
    public function add_replication_agreements($domain_root_dn)
    {
        $replica_hosts = $this->list_replicas();
        if (empty($replica_hosts)) {
            return;
        }
        $result = $this->search($this->config_get('config_root_dn'), "(&(objectclass=nsDS5Replica)(nsDS5ReplicaType=3))", "sub");
        if (!$result) {
            $this->_debug("No replication configuration found.");
            return;
        }
        // Preserve the number of replicated databases we have, because the replication ID
        // can be calculated from the number of databases replicated, and the number of
        // servers.
        $num_replica_dbs = $result->count();
        $replicas        = $result->entries(true);
        $max_replica_agreements = 0;
        foreach ($replicas as $replica_dn => $replica_attrs) {
            $result = $this->search($replica_dn, "(objectclass=nsDS5ReplicationAgreement)", "sub");
            if ($result) {
                if ($max_replica_agreements < $result->count()) {
                    $max_replica_agreements    = $result->count();
                    $max_replica_agreements_dn = $replica_dn;
                }
            }
        }
        $max_repl_id = $num_replica_dbs * count($replica_hosts);
        $this->_debug("The current maximum replication ID is $max_repl_id");
        $this->_debug("The current maximum number of replication agreements for any database is $max_replica_agreements (for $max_replica_agreements_dn)");
        $this->_debug("With " . count($replica_hosts) . " replicas, the next is " . ($max_repl_id + 1) . " and the last one is " . ($max_repl_id + count($replica_hosts)));
        // Then add the replication agreements
        foreach ($replica_hosts as $num => $replica_host) {
            $ldap = new Net_LDAP3($this->config);
            $ldap->config_set('hosts', array($replica_host));
            $ldap->connect();
            $ldap->bind($this->_current_bind_dn, $this->_current_bind_pw);
            $replica_attrs = array(
                'cn' => 'replica',
                'objectclass' => array(
                    'top',
                    'nsds5replica',
                    'extensibleobject',
                ),
                'nsDS5ReplicaBindDN'     => $ldap->get_entry_attribute($replica_dn, "nsDS5ReplicaBindDN"),
                'nsDS5ReplicaId'         => ($max_repl_id + $num + 1),
                'nsDS5ReplicaRoot'       => $domain_root_dn,
                'nsDS5ReplicaType'       => $ldap->get_entry_attribute($replica_dn, "nsDS5ReplicaType"),
                'nsds5ReplicaPurgeDelay' => $ldap->get_entry_attribute($replica_dn, "nsds5ReplicaPurgeDelay"),
                'nsDS5Flags'             => $ldap->get_entry_attribute($replica_dn, "nsDS5Flags")
            );
            $new_replica_dn = 'cn=replica,cn="' . $domain_root_dn . '",cn=mapping tree,cn=config';
            $this->_debug("Adding $new_replica_dn to $replica_host with attributes: " . var_export($replica_attrs, true));
            $result = $ldap->add_entry($new_replica_dn, $replica_attrs);
            if (!$result) {
                $this->_error("Could not add replication configuration to database for $domain_root_dn on $replica_host");
                continue;
            }
            $result = $ldap->search($replica_dn, "(objectclass=nsDS5ReplicationAgreement)", "sub");
            if (!$result) {
                $this->_error("Host $replica_host does not have any replication agreements");
                continue;
            }
            $entries = $result->entries(true);
            $replica_agreement_tpl_dn = key($entries);
            $this->_debug("Using " . var_export($replica_agreement_tpl_dn, true) . " as the template for new replication agreements");
            foreach ($replica_hosts as $replicate_to_host) {
                // Skip the current server
                if ($replicate_to_host == $replica_host) {
                    continue;
                }
                $this->_debug("Adding a replication agreement for $domain_root_dn to $replicate_to_host on " . $replica_host);
                $attrs = array(
                    'objectclass',
                    'nsDS5ReplicaBindDN',
                    'nsDS5ReplicaCredentials',
                    'nsDS5ReplicaTransportInfo',
                    'nsDS5ReplicaBindMethod',
                    'nsDS5ReplicaHost',
                    'nsDS5ReplicaPort'
                );
                $replica_agreement_attrs = $ldap->get_entry_attributes($replica_agreement_tpl_dn, $attrs);
                $replica_agreement_attrs['cn'] = array_shift(explode('.', $replicate_to_host)) . str_replace(array('dc=',','), array('_',''), $domain_root_dn);
                $replica_agreement_attrs['nsDS5ReplicaRoot'] = $domain_root_dn;
                $replica_agreement_dn = "cn=" . $replica_agreement_attrs['cn'] . "," . $new_replica_dn;
                $this->_debug("Adding $replica_agreement_dn to $replica_host with attributes: " . var_export($replica_agreement_attrs, true));
                $result = $ldap->add_entry($replica_agreement_dn, $replica_agreement_attrs);
                if (!$result) {
                    $this->_error("Failed adding $replica_agreement_dn");
                }
            }
        }
        $server_id = implode('', array_diff($replica_hosts, $this->_server_id_not));
        $this->_debug("About to trigger consumer initialization for replicas on current 'parent': $server_id");
        $result = $this->search($this->config_get('config_root_dn'), "(&(objectclass=nsDS5ReplicationAgreement)(nsds5replicaroot=$domain_root_dn))", "sub");
        if ($result) {
            foreach ($result->entries(true) as $agreement_dn => $agreement_attrs) {
                $this->modify_entry_attributes(
                    $agreement_dn,
                    array(
                        'replace' => array(
                            'nsds5BeginReplicaRefresh' => 'start',
                        ),
                    )
                );
            }
        }
    }
    public function attribute_details($attributes = array())
    {
        $schema = $this->init_schema();
        if (!$schema) {
            return array();
        }
        $attribs = $schema->getAll('attributes');
        $attributes_details = array();
        foreach ($attributes as $attribute) {
            if (array_key_exists($attribute, $attribs)) {
                $attrib_details = $attribs[$attribute];
                if (!empty($attrib_details['sup'])) {
                    foreach ($attrib_details['sup'] as $super_attrib) {
                        $_attrib_details = $attribs[$super_attrib];
                        if (is_array($_attrib_details)) {
                            $attrib_details = array_merge($_attrib_details, $attrib_details);
                        }
                    }
                }
            }
            else if (array_key_exists(strtolower($attribute), $attribs)) {
                $attrib_details = $attribs[strtolower($attribute)];
                if (!empty($attrib_details['sup'])) {
                    foreach ($attrib_details['sup'] as $super_attrib) {
                        $_attrib_details = $attribs[$super_attrib];
                        if (is_array($_attrib_details)) {
                            $attrib_details = array_merge($_attrib_details, $attrib_details);
                        }
                    }
                }
            }
            else {
                $this->_warning("LDAP: No schema details exist for attribute $attribute (which is strange)");
            }
            // The relevant parts only, please
            $attributes_details[$attribute] = array(
                'type'        => !empty($attrib_details['single-value']) ? 'text' : 'list',
                'description' => $attrib_details['desc'],
                'syntax'      => $attrib_details['syntax'],
                'max-length'  => $attrib_details['max-length'] ?: false,
            );
        }
        return $attributes_details;
    }
    public function attributes_allowed($objectclasses = array())
    {
        $this->_debug("Listing allowed_attributes for objectclasses", $objectclasses);
        if (!is_array($objectclasses) || empty($objectclasses)) {
            return false;
        }
        $schema = $this->init_schema();
        if (!$schema) {
            return false;
        }
        $may          = array();
        $must         = array();
        $superclasses = array();
        foreach ($objectclasses as $objectclass) {
            $superclass = $schema->superclass($objectclass);
            if (!empty($superclass)) {
                $superclasses = array_merge($superclass, $superclasses);
            }
            $_may  = $schema->may($objectclass);
            $_must = $schema->must($objectclass);
            if (is_array($_may)) {
                $may = array_merge($may, $_may);
            }
            if (is_array($_must)) {
                $must = array_merge($must, $_must);
            }
        }
        $may          = array_unique($may);
        $must         = array_unique($must);
        $superclasses = array_unique($superclasses);
        return array('may' => $may, 'must' => $must, 'super' => $superclasses);
    }
    public function classes_allowed()
    {
        $schema = $this->init_schema();
        if (!$schema) {
            return false;
        }
        $list    = $schema->getAll('objectclasses');
        $classes = array();
        foreach ($list as $class) {
            $classes[] = $class['name'];
        }
        return $classes;
    }
    /**
     * Bind connection with DN and password
     *
     * @param string $dn   Bind DN
     * @param string $pass Bind password
     *
     * @return boolean True on success, False on error
     */
    public function bind($bind_dn, $bind_pw)
    {
        if (!$this->conn) {
            return false;
        }
        if ($bind_dn == $this->_current_bind_dn) {
            return true;
        }
        $this->_debug("C: Bind [dn: $bind_dn] [pass: $bind_pw]");
        if (@ldap_bind($this->conn, $bind_dn, $bind_pw)) {
            $this->_debug("S: OK");
            $this->_current_bind_dn = $bind_dn;
            $this->_current_bind_pw = $bind_pw;
            return true;
        }
        $this->_debug("S: ".ldap_error($this->conn));
        $this->_error("Bind failed for dn=$bind_dn: ".ldap_error($this->conn));
        return false;
    }
    /**
     * Close connection to LDAP server
     */
    public function close()
    {
        if ($this->conn) {
            $this->_debug("C: Close");
            ldap_unbind($this->conn);
            $this->_current_bind_dn = null;
            $this->_current_bind_pw = null;
            $this->conn             = null;
        }
    }
    /**
     * Get the value of a configuration item.
     *
     * @param string $key     Configuration key
     * @param mixed  $default Default value to return
     */
    public function config_get($key, $default = null)
    {
        if (!empty($this->_config_get_hook)) {
            return call_user_func_array($this->_config_get_hook, array($key, $value));
        }
        else if (method_exists($this, "config_get_{$key}")) {
            return call_user_func(array($this, "config_get_$key"), $value);
        }
        else if (!isset($this->config[$key])) {
            return $default;
        }
        else {
            return $this->config[$key];
        }
    }
    /**
     * Set a configuration item to value.
     *
     * @param string $key   Configuration key
     * @param mixed  $value Configuration value
     */
    public function config_set($key, $value = null)
    {
        if (is_array($key)) {
            foreach ($key as $k => $v) {
                $this->config_set($k, $v);
            }
            return;
        }
        if (!empty($this->_config_set_hook)) {
            call_user_func($this->_config_set_hook, array($key, $value));
        }
        else if (method_exists($this, "config_set_{$key}")) {
            call_user_func_array(array($this, "config_set_$key"), array($value));
        }
        else if (isset($this->$key)) {
            $this->$key = $value;
        }
        else {
            // 'host' option is deprecated
            if ($key == 'host') {
                $this->config['hosts'] = (array) $value;
            }
            else {
                $this->config[$key] = $value;
            }
        }
    }
    /**
     * Establish a connection to the LDAP server
     */
    public function connect($host = null)
    {
        if (!function_exists('ldap_connect')) {
            $this->_error("No ldap support in this PHP installation");
            return false;
        }
        if (is_resource($this->conn)) {
            $this->_debug("Connection already exists");
            return true;
        }
        $hosts = !empty($host) ? $host : $this->config_get('hosts', array());
        $port  = $this->config_get('port', 389);
        foreach ((array) $hosts as $host) {
            $this->_debug("C: Connect [$host:$port]");
            if ($lc = @ldap_connect($host, $port)) {
                if ($this->config_get('use_tls', false) === true) {
                    if (!ldap_start_tls($lc)) {
                        $this->_debug("S: Could not start TLS. " . ldap_error($lc));
                        continue;
                    }
                }
                $this->_debug("S: OK");
                $ldap_version = $this->config_get('ldap_version', 3);
                $timeout      = $this->config_get('network_timeout');
                $referrals    = $this->config_get('referrals');
                ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, $ldap_version);
                if ($timeout) {
                    ldap_set_option($lc, LDAP_OPT_NETWORK_TIMEOUT, $timeout);
                }
                if ($referrals !== null) {
                    ldap_set_option($lc, LDAP_OPT_REFERRALS, (bool) $referrals);
                }
                $this->_current_host = $host;
                $this->conn          = $lc;
                break;
            }
            $this->_debug("S: NOT OK");
        }
        if (!is_resource($this->conn)) {
            $this->_error("Could not connect to LDAP");
            return false;
        }
        return true;
    }
    /**
     *   Shortcut to ldap_delete()
     */
    public function delete_entry($entry_dn)
    {
        $this->_debug("LDAP: C: Delete $entry_dn");
        if (ldap_delete($this->conn, $entry_dn) === false) {
            $this->_debug("LDAP: S: " . ldap_error($this->conn));
            return false;
        }
        $this->_debug("LDAP: S: OK");
        return true;
    }
    /**
     * Deletes specified entry and all entries in the tree
     */
    public function delete_entry_recursive($entry_dn)
    {
        // searching for sub entries, but not scope sub, just one level
        $result = $this->search($entry_dn, '(objectclass=*)', 'one');
        if ($result) {
            $entries = $result->entries(true);
            foreach (array_keys($entries) as $sub_dn) {
                if (!$this->delete_entry_recursive($sub_dn)) {
                    return false;
                }
            }
        }
        if (!$this->delete_entry($entry_dn)) {
            return false;
        }
        return true;
    }
    public function effective_rights($subject)
    {
        $effective_rights_control_oid = "1.3.6.1.4.1.42.2.27.9.5.2";
        $supported_controls = $this->supported_controls();
        if (!in_array($effective_rights_control_oid, $supported_controls)) {
            $this->_debug("LDAP: No getEffectiveRights control in supportedControls");
            return false;
        }
        $attributes = array(
            'attributeLevelRights' => array(),
            'entryLevelRights' => array(),
        );
        $output   = array();
        $entry_dn = $this->entry_dn($subject);
        if (!$entry_dn) {
            $entry_dn = $this->config_get($subject . "_base_dn");
        }
        if (!$entry_dn) {
            $entry_dn = $this->config_get("base_dn");
        }
        if (!$entry_dn) {
            $entry_dn = $this->config_get("root_dn");
        }
        $this->_debug("effective_rights for subject $subject resolves to entry dn $entry_dn");
        $moz_ldapsearch = "/usr/lib64/mozldap/ldapsearch";
        if (!is_file($moz_ldapsearch)) {
            $moz_ldapsearch = "/usr/lib/mozldap/ldapsearch";
        }
        if (!is_file($moz_ldapsearch)) {
            $moz_ldapsearch = null;
        }
        if (empty($moz_ldapsearch)) {
            $this->_error("Mozilla LDAP C SDK binary ldapsearch not found, cannot get effective rights on subject $subject");
            return null;
        }
        $command = array(
            $moz_ldapsearch,
            '-x',
            '-h',
            $this->_ldap_server,
            '-p',
            $this->_ldap_port,
            '-b',
            escapeshellarg($entry_dn),
            '-D',
            escapeshellarg($this->_current_bind_dn),
            '-w',
            escapeshellarg($this->_current_bind_pw),
            '-J',
            escapeshellarg(implode(':', array(
                $effective_rights_control_oid,          // OID
                'true',                                 // Criticality
                'dn:' . $this->_current_bind_dn // User DN
            ))),
            '-s',
            'base',
            '"(objectclass=*)"',
            '"*"',
        );
        $command = implode(' ', $command);
        $this->_debug("LDAP: Executing command: $command");
        exec($command, $output, $return_code);
        $this->_debug("LDAP: Command output:" . var_export($output, true));
        $this->_debug("Return code: " . $return_code);
        if ($return_code) {
            $this->_error("Command $moz_ldapsearch returned error code: $return_code");
            return null;
        }
        $lines = array();
        foreach ($output as $line_num => $line) {
            if (substr($line, 0, 1) == " ") {
                $lines[count($lines)-1] .= trim($line);
            }
            else {
                $lines[] = trim($line);
            }
        }
        foreach ($lines as $line) {
            $line_components = explode(':', $line);
            $attribute_name  = array_shift($line_components);
            $attribute_value = trim(implode(':', $line_components));
            switch ($attribute_name) {
                case "attributeLevelRights":
                    $attributes[$attribute_name] = $this->parse_attribute_level_rights($attribute_value);
                    break;
                case "dn":
                    $attributes[$attribute_name] = $attribute_value;
                    break;
                case "entryLevelRights":
                    $attributes[$attribute_name] = $this->parse_entry_level_rights($attribute_value);
                    break;
                default:
                    break;
            }
        }
        return $attributes;
    }
    /**
     * Resolve entry data to entry DN
     *
     * @param string $subject    Entry string (e.g. entry DN or unique attribute value)
     * @param array  $attributes Additional attributes
     * @param string $base_dn    Optional base DN
     *
     * @return string Entry DN string
     */
    public function entry_dn($subject, $attributes = array(), $base_dn = null)
    {
        $this->_debug("entry_dn on subject $subject");
        $is_dn = ldap_explode_dn($subject, 1);
        if (is_array($is_dn) && array_key_exists("count", $is_dn) && $is_dn["count"] > 0) {
            $this->_debug("$subject is a dn");
            return $subject;
        }
        $this->_debug("$subject is not a dn");
        if (strlen($subject) < 32 || preg_match('/[^a-fA-F0-9-]/', $subject)) {
            $this->_debug("$subject is not a unique identifier");
            return;
        }
        $unique_attr = $this->config_get('unique_attribute', 'nsuniqueid');
        $this->_debug("Using unique_attribute " . var_export($unique_attr, true) . " at " . __FILE__ . ":" . __LINE__);
        $attributes  = array_merge(array($unique_attr => $subject), (array)$attributes);
        $subject     = $this->entry_find_by_attribute($attributes, $base_dn);
        if (!empty($subject)) {
            return key($subject);
        }
    }
    public function entry_find_by_attribute($attributes, $base_dn = null)
    {
        $this->_debug("Net_LDAP3::entry_find_by_attribute(\$attributes, \$base_dn) called with base_dn", $base_dn, "and attributes", $attributes);
        if (empty($attributes) || !is_array($attributes)) {
            return false;
        }
        if (empty($attributes[key($attributes)])) {
            return false;
        }
        $filter = count($attributes) ? "(&" : "";
        foreach ($attributes as $key => $value) {
            $filter .= "(" . $key . "=" . $value . ")";
        }
        $filter .= count($attributes) ? ")" : "";
        if (empty($base_dn)) {
            $base_dn = $this->config_get('root_dn');
            $this->_debug("Using base_dn from domain " . $this->domain . ": " . $base_dn);
        }
        $result = $this->search($base_dn, $filter, 'sub', array_keys($attributes));
        if ($result && $result->count() > 0) {
            $this->_debug("Results found: " . implode(', ', array_keys($result->entries(true))));
            return $result->entries(true);
        }
        else {
            $this->_debug("No result");
            return false;
        }
    }
    public function find_user_groups($member_dn)
    {
        $groups  = array();
        $root_dn = $this->config_get('root_dn');
        // TODO: Do not query for both, it's either one or the other
        $entries = $this->search($root_dn, "(|" .
            "(&(objectclass=groupofnames)(member=$member_dn))" .
            "(&(objectclass=groupofuniquenames)(uniquemember=$member_dn))" .
            ")"
        );
        if ($entries) {
            $groups = array_keys($entries->entries(true));
        }
        return $groups;
    }
    public function get_entry_attribute($subject_dn, $attribute)
    {
        $entry = $this->get_entry_attributes($subject_dn, (array)$attribute);
        return $entry[strtolower($attribute)];
    }
    public function get_entry_attributes($subject_dn, $attributes)
    {
        // @TODO: use get_entry?
        $result = $this->search($subject_dn, '(objectclass=*)', 'base', $attributes);
        if (!$result) {
            return array();
        }
        $entries  = $result->entries(true);
        $entry_dn = key($entries);
        $entry    = $entries[$entry_dn];
        return $entry;
    }
    /**
     * Get a specific LDAP entry, identified by its DN
     *
     * @param string $dn         Record identifier
     * @param array  $attributes Attributes to return
     *
     * @return array Hash array
     */
    public function get_entry($dn, $attributes = array())
    {
        $rec = null;
        if ($this->conn && $dn) {
            $this->_debug("C: Read [dn: $dn] [(objectclass=*)]");
            if ($ldap_result = @ldap_read($this->conn, $dn, '(objectclass=*)', $attributes)) {
                $this->_debug("S: OK");
                if ($entry = ldap_first_entry($this->conn, $ldap_result)) {
                    $rec = ldap_get_attributes($this->conn, $entry);
                }
            }
            else {
                $this->_debug("S: ".ldap_error($this->conn));
            }
            if (!empty($rec)) {
                $rec['dn'] = $dn; // Add in the dn for the entry.
            }
        }
        return $rec;
    }
    public function list_replicas()
    {
        $this->_debug("Finding replicas for this server.");
        // Search any host that is a replica for the current host
        $replica_hosts = $this->config_get('replica_hosts', array());
        $root_dn       = $this->config_get('config_root_dn');
        if (!empty($replica_hosts)) {
            return $replica_hosts;
        }
        $ldap = new Net_LDAP3($this->config);
        $ldap->connect();
        $ldap->bind($this->_current_bind_dn, $this->_current_bind_pw);
        $result = $ldap->search($root_dn, '(objectclass=nsds5replicationagreement)', 'sub', array('nsds5replicahost'));
        if (!$result) {
            $this->_debug("No replicas configured");
            return $replica_hosts;
        }
        $this->_debug("Replication agreements found: " . var_export($result->entries(true), true));
        foreach ($result->entries(true) as $dn => $attrs) {
            if (!in_array($attrs['nsds5replicahost'], $replica_hosts)) {
                $replica_hosts[] = $attrs['nsds5replicahost'];
            }
        }
        // $replica_hosts now holds the IDs of servers we are currently NOT
        // connected to. We might need this later in order to set
        $this->_server_id_not = $replica_hosts;
        $this->_debug("So far, we have the following replicas: " . var_export($replica_hosts, true));
        $ldap->close();
        foreach ($replica_hosts as $replica_host) {
            $ldap->config_set('hosts', array($replica_host));
            $ldap->connect();
            $ldap->bind($this->_current_bind_dn, $this->_current_bind_pw);
            $result = $ldap->search($root_dn, '(objectclass=nsds5replicationagreement)', 'sub', array('nsds5replicahost'));
            if (!$result) {
                $this->_debug("No replicas configured");
            }
            foreach ($result->entries(true) as $dn => $attrs) {
                if (!in_array($attrs['nsds5replicahost'], $replica_hosts)) {
                    $replica_hosts[] = $attrs['nsds5replicahost'];
                }
            }
            $ldap->close();
        }
        $this->config_set('replica_hosts', $replica_hosts);
        return $replica_hosts;
    }
    public function login($username, $password, $domain = null)
    {
        $this->_debug("Net_LDAP3::login(\$username = '" . $username . "', \$password = '****', \$domain = '" . $domain . "')");
        $_bind_dn = $this->config_get('service_bind_dn');
        $_bind_pw = $this->config_get('service_bind_pw');
        if (empty($_bind_dn)) {
            $this->_debug("No valid service bind dn found.");
            return null;
        }
        if (empty($_bind_pw)) {
            $this->_debug("No valid service bind password found.");
            return null;
        }
        $bound = $this->bind($_bind_dn, $_bind_pw);
        if (!$bound) {
            $this->_debug("Could not bind with service bind credentials.");
            return null;
        }
        $entry_dn = $this->entry_dn($username);
        if (!empty($entry_dn)) {
            $bound = $this->bind($entry_dn, $password);
            if (!$bound) {
                $this->_error("Could not bind with " . $entry_dn);
                return null;
            }
            return $entry_dn;
        }
        $base_dn = $this->config_get('root_dn');
        if (empty($base_dn)) {
            $this->_debug("Could not get a valid base dn to search.");
            return null;
        }
        $localpart = $username;
        if (empty($domain) ) {
            if (count(explode('@', $username)) > 1) {
                $_parts    = explode('@', $username);
                $localpart = $_parts[0];
                $domain    = $_parts[1];
            }
            else {
                $localpart = $username;
                $domain    = '';
            }
        }
        $realm  = $domain;
        $filter = $this->config_get("login_filter", null);
        if (empty($filter)) {
            $filter = $this->config_get("filter", null);
        }
        if (empty($filter)) {
            $filter = "(&(|(mail=%s)(mail=%U@%d)(alias=%s)(alias=%U@%d)(uid=%s))(objectclass=inetorgperson))";
        }
        $this->_debug("Net::LDAP3::login() original filter: " . $filter);
        $replace_patterns = array(
            '/%s/' => $username,
            '/%d/' => $domain,
            '/%U/' => $localpart,
            '/%r/' => $realm
        );
        $filter = preg_replace(array_keys($replace_patterns), array_values($replace_patterns), $filter);
        $this->_debug("Net::LDAP3::login() actual filter: " . $filter);
        $result = $this->search($base_dn, $filter, 'sub');
        if (!$result) {
            $this->_debug("Could not search $base_dn with $filter");
            return null;
        }
        if ($result->count() > 1) {
            $this->_debug("Multiple entries found.");
            return null;
        }
        else if ($result->count() < 1) {
            $this->_debug("No entries found.");
            return null;
        }
        $entries  = $result->entries();
        $entry    = self::normalize_result($entries);
        $entry_dn = key($entry);
        $bound = $this->bind($entry_dn, $password);
        if (!$bound) {
            $this->_debug("Could not bind with " . $entry_dn);
            return null;
        }
        return $entry_dn;
    }
    public function list_group_members($dn, $entry = null, $recurse = true)
    {
        $this->_debug("Called list_group_members(" . $dn . ")");
        if (is_array($entry) && in_array('objectclass', $entry)) {
            if (!in_array(array('groupofnames', 'groupofuniquenames', 'groupofurls'), $entry['objectclass'])) {
                $this->_debug("Called list_group_members on a non-group!");
                return array();
            }
        }
        else {
            $entry = $this->get_entry($dn, array('member', 'uniquemember', 'memberurl', 'objectclass'));
            if (!$entry) {
                return array();
            }
        }
        $group_members = array();
        foreach ((array)$entry['objectclass'] as $objectclass) {
            switch (strtolower($objectclass)) {
                case "groupofnames":
                case "kolabgroupofnames":
                    $group_members = array_merge($group_members, $this->list_group_member($dn, $entry['member'], $recurse));
                    break;
                case "groupofuniquenames":
                case "kolabgroupofuniquenames":
                    $group_members = array_merge($group_members, $this->list_group_uniquemember($dn, $entry['uniquemember'], $recurse));
                    break;
                case "groupofurls":
                    $group_members = array_merge($group_members, $this->list_group_memberurl($dn, $entry['memberurl'], $recurse));
                    break;
            }
        }
        return array_values(array_filter($group_members));
    }
    public function modify_entry($subject_dn, $old_attrs, $new_attrs)
    {
        $this->_debug("OLD ATTRIBUTES", $old_attrs);
        $this->_debug("NEW ATTRIBUTES", $new_attrs);
        // TODO: Get $rdn_attr - we have type_id in $new_attrs
        $dn_components  = ldap_explode_dn($subject_dn, 0);
        $rdn_components = explode('=', $dn_components[0]);
        $rdn_attr       = $rdn_components[0];
        $this->_debug("Net_LDAP3::modify_entry() using rdn attribute: " . $rdn_attr);
        $mod_array = array(
            'add'       => array(), // For use with ldap_mod_add()
            'del'       => array(), // For use with ldap_mod_del()
            'replace'   => array(), // For use with ldap_mod_replace()
            'rename'    => array(), // For use with ldap_rename()
        );
        // This is me cheating. Remove this special attribute.
        if (array_key_exists('ou', $old_attrs) || array_key_exists('ou', $new_attrs)) {
            $old_ou = $old_attrs['ou'];
            $new_ou = $new_attrs['ou'];
            unset($old_attrs['ou']);
            unset($new_attrs['ou']);
        }
        else {
            $old_ou = null;
            $new_ou = null;
        }
        // Compare each attribute value of the old attrs with the corresponding value
        // in the new attrs, if any.
        foreach ($old_attrs as $attr => $old_attr_value) {
            if (is_array($old_attr_value)) {
                if (count($old_attr_value) == 1) {
                    $old_attrs[$attr] = $old_attr_value[0];
                    $old_attr_value = $old_attrs[$attr];
                }
            }
            if (array_key_exists($attr, $new_attrs)) {
                if (is_array($new_attrs[$attr])) {
                    if (count($new_attrs[$attr]) == 1) {
                        $new_attrs[$attr] = $new_attrs[$attr][0];
                    }
                }
                if (is_array($old_attrs[$attr]) && is_array($new_attrs[$attr])) {
                    $_sort1 = $new_attrs[$attr];
                    sort($_sort1);
                    $_sort2 = $old_attr_value;
                    sort($_sort2);
                }
                else {
                    $_sort1 = true;
                    $_sort2 = false;
                }
                if ($new_attrs[$attr] !== $old_attr_value && $_sort1 !== $_sort2) {
                    $this->_debug("Attribute $attr changed from " . var_export($old_attr_value, true) . " to " . var_export($new_attrs[$attr], true));
                    if ($attr === $rdn_attr) {
                        $this->_debug("This attribute is the RDN attribute. Let's see if it is multi-valued, and if the original still exists in the new value.");
                        if (is_array($old_attrs[$attr])) {
                            if (!is_array($new_attrs[$attr])) {
                                if (in_array($new_attrs[$attr], $old_attrs[$attr])) {
                                    // TODO: Need to remove all $old_attrs[$attr] values not equal to $new_attrs[$attr], and not equal to the current $rdn_attr value [0]
                                    $this->_debug("old attrs. is array, new attrs. is not array. new attr. exists in old attrs.");
                                    $rdn_attr_value = array_shift($old_attrs[$attr]);
                                    $_attr_to_remove = array();
                                    foreach ($old_attrs[$attr] as $value) {
                                        if (strtolower($value) != strtolower($new_attrs[$attr])) {
                                            $_attr_to_remove[] = $value;
                                        }
                                    }
                                    $this->_debug("Adding to delete attribute $attr values:" . implode(', ', $_attr_to_remove));
                                    $mod_array['del'][$attr] = $_attr_to_remove;
                                    if (strtolower($new_attrs[$attr]) !== strtolower($rdn_attr_value)) {
                                        $this->_debug("new attrs is not the same as the old rdn value, issuing a rename");
                                        $mod_array['rename']['dn'] = $subject_dn;
                                        $mod_array['rename']['new_rdn'] = $rdn_attr . '=' . $new_attrs[$attr][0];
                                    }
                                }
                                else {
                                    $this->_debug("new attrs is not the same as any of the old rdn value, issuing a full rename");
                                    $mod_array['rename']['dn'] = $subject_dn;
                                    $mod_array['rename']['new_rdn'] = $rdn_attr . '=' . $new_attrs[$attr];
                                }
                            }
                            else {
                                // TODO: See if the rdn attr. value is still in $new_attrs[$attr]
                                if (in_array($old_attrs[$attr][0], $new_attrs[$attr])) {
                                    $this->_debug("Simply replacing attr $attr as rdn attr value is preserved.");
                                    $mod_array['replace'][$attr] = $new_attrs[$attr];
                                }
                                else {
                                    // TODO: This fails.
                                    $mod_array['rename']['dn'] = $subject_dn;
                                    $mod_array['rename']['new_rdn'] = $rdn_attr . '=' . $new_attrs[$attr][0];
                                    $mod_array['del'][$attr] = $old_attrs[$attr][0];
                                }
                            }
                        }
                        else {
                            if (!is_array($new_attrs[$attr])) {
                                $this->_debug("Renaming " . $old_attrs[$attr] . " to " . $new_attrs[$attr]);
                                $mod_array['rename']['dn'] = $subject_dn;
                                $mod_array['rename']['new_rdn'] = $rdn_attr . '=' . $new_attrs[$attr];
                            }
                            else {
                                $this->_debug("Adding to replace");
                                // An additional attribute value is being supplied. Just replace and continue.
                                $mod_array['replace'][$attr] = $new_attrs[$attr];
                                continue;
                            }
                        }
                    }
                    else {
                        if (!isset($new_attrs[$attr]) || $new_attrs[$attr] === '' || (is_array($new_attrs[$attr]) && empty($new_attrs[$attr]))) {
                            switch ($attr) {
                                case "userpassword":
                                    break;
                                default:
                                    $this->_debug("Adding to del: $attr");
                                    $mod_array['del'][$attr] = (array)($old_attr_value);
                                    break;
                            }
                        }
                        else {
                            $this->_debug("Adding to replace: $attr");
                            $mod_array['replace'][$attr] = (array)($new_attrs[$attr]);
                        }
                    }
                }
                else {
                    $this->_debug("Attribute $attr unchanged");
                }
            }
            else {
                // TODO: Since we're not shipping the entire object back and forth, and only post
                // part of the data... we don't know what is actually removed (think modifiedtimestamp, etc.)
                $this->_debug("Group attribute $attr not mentioned in \$new_attrs..., but not explicitly removed... by assumption");
            }
        }
        foreach ($new_attrs as $attr => $value) {
            // OU's parent base dn
            if ($attr == 'base_dn') {
                continue;
            }
            if (is_array($value)) {
                if (count($value) == 1) {
                    $new_attrs[$attr] = $value[0];
                    $value = $new_attrs[$attr];
                }
            }
            if (array_key_exists($attr, $old_attrs)) {
                if (is_array($old_attrs[$attr])) {
                    if (count($old_attrs[$attr]) == 1) {
                        $old_attrs[$attr] = $old_attrs[$attr][0];
                    }
                }
                if (is_array($new_attrs[$attr]) && is_array($old_attrs[$attr])) {
                    $_sort1 = $old_attrs[$attr];
                    sort($_sort1);
                    $_sort2 = $value;
                    sort($_sort2);
                }
                else {
                    $_sort1 = true;
                    $_sort2 = false;
                }
                if ($value === null || $value === '' || (is_array($value) && empty($value))) {
                    if (!array_key_exists($attr, $mod_array['del'])) {
                        switch ($attr) {
                            case 'userpassword':
                                break;
                            default:
                                $this->_debug("Adding to del(2): $attr");
                                $mod_array['del'][$attr] = (array)($old_attrs[$attr]);
                                break;
                        }
                    }
                }
                else {
                    if (!($old_attrs[$attr] === $value) && !($attr === $rdn_attr) && !($_sort1 === $_sort2)) {
                        if (!array_key_exists($attr, $mod_array['replace'])) {
                            $this->_debug("Adding to replace(2): $attr");
                            $mod_array['replace'][$attr] = $value;
                        }
                    }
                }
            }
            else {
                if (!empty($value)) {
                    $mod_array['add'][$attr] = $value;
                }
            }
        }
        if (empty($old_ou)) {
            $subject_dn_components = ldap_explode_dn($subject_dn, 0);
            unset($subject_dn_components["count"]);
            $subject_rdn = array_shift($subject_dn_components);
            $old_ou      = implode(',', $subject_dn_components);
        }
        // object is an organizational unit
        if (strpos($subject_dn, 'ou=' . $old_ou) === 0) {
            $root = substr($subject_dn, strlen($old_ou) + 4); // remove ou=*,
            if ((!empty($new_attrs['base_dn']) && strtolower($new_attrs['base_dn']) !== strtolower($root))
                || (strtolower($old_ou) !== strtolower($new_ou))
            ) {
                if (!empty($new_attrs['base_dn'])) {
                    $root = $new_attrs['base_dn'];
                }
                $mod_array['rename']['new_parent'] = $root;
                $mod_array['rename']['dn']         = $subject_dn;
                $mod_array['rename']['new_rdn']    = 'ou=' . $new_ou;
            }
        }
        // not OU object, but changed ou attribute
        else if ((!empty($old_ou) && !empty($new_ou)) && strtolower($old_ou) !== strtolower($new_ou)) {
            $mod_array['rename']['new_parent'] = $new_ou;
            if (empty($mod_array['rename']['dn']) || empty($mod_array['rename']['new_rdn'])) {
                $mod_array['rename']['dn']      = $subject_dn;
                $mod_array['rename']['new_rdn'] = $rdn_attr . '=' . $new_attrs[$rdn_attr];
            }
        }
        $this->_debug($mod_array);
        $result = $this->modify_entry_attributes($subject_dn, $mod_array);
        if ($result) {
            return $mod_array;
        }
    }
    /**
     * Bind connection with (SASL-) user and password
     *
     * @param string $authc Authentication user
     * @param string $pass  Bind password
     * @param string $authz Autorization user
     *
     * @return boolean True on success, False on error
     */
    public function sasl_bind($authc, $pass, $authz=null)
    {
        if (!$this->conn) {
            return false;
        }
        if (!function_exists('ldap_sasl_bind')) {
            $this->_error("Unable to bind: ldap_sasl_bind() not exists");
            return false;
        }
        if (!empty($authz)) {
            $authz = 'u:' . $authz;
        }
        $method = $this->config_get('auth_method');
        if (empty($method)) {
            $method = 'DIGEST-MD5';
        }
        $this->_debug("C: Bind [mech: $method, authc: $authc, authz: $authz] [pass: $pass]");
        if (ldap_sasl_bind($this->conn, null, $pass, $method, null, $authc, $authz)) {
            $this->_debug("S: OK");
            return true;
        }
        $this->_debug("S: ".ldap_error($this->conn));
        $this->_error("Bind failed for authcid=$authc ".ldap_error($this->conn));
        return false;
    }
    /**
     * Execute LDAP search
     *
     * @param string $base_dn    Base DN to use for searching
     * @param string $filter     Filter string to query
     * @param string $scope      The LDAP scope (list|sub|base)
     * @param array  $attrs      List of entry attributes to read
     * @param array  $prop       Hash array with query configuration properties:
     *   - sort:   array of sort attributes (has to be in sync with the VLV index)
     *   - search: search string used for VLV controls
     * @param bool   $count_only Set to true if only entry count is requested
     *
     * @return mixed Net_LDAP3_Result object or number of entries (if $count_only=true) or False on failure
     */
    public function search($base_dn, $filter = '(objectclass=*)', $scope = 'sub', $attrs = array('dn'), $props = array(), $count_only = false)
    {
        if (!$this->conn) {
            $this->_debug("No active connection for " . __CLASS__ . "::" . __FUNCTION__);
            return false;
        }
        $this->_debug("C: Search base dn: [$base_dn] scope [$scope] with filter [$filter]");
        // make sure attributes list is not empty
        if (empty($attrs)) {
            $attrs = array('dn');
        }
        if (!$count_only && ($sort = $this->find_vlv($base_dn, $filter, $scope, $props['sort']))) {
            // when using VLV, we get the total count by...
            // ...either reading numSubOrdinates attribute
            if (($sub_filter = $this->config_get('numsub_filter')) &&
                ($result_count = @$ns_function($this->conn, $base_dn, $sub_filter, array('numSubOrdinates'), 0, 0, 0))
            ) {
                $counts = ldap_get_entries($this->conn, $result_count);
                for ($vlv_count = $j = 0; $j < $counts['count']; $j++) {
                    $vlv_count += $counts[$j]['numsubordinates'][0];
                }
                $this->_debug("D: total numsubordinates = " . $vlv_count);
            }
            // ...or by fetching all records dn and count them
            else if (!function_exists('ldap_parse_virtuallist_control')) {
                $vlv_count = $this->search($base_dn, $filter, $scope, array('dn'), $props, true);
            }
            $this->vlv_active = $this->_vlv_set_controls($sort, $this->list_page, $this->page_size,
                $this->_vlv_search($sort, $props['search']));
        }
        else {
            $this->vlv_active = false;
        }
        $function  = self::scope_to_function($scope, $ns_function);
        $sizelimit = (int) $this->config['sizelimit'];
        $timelimit = (int) $this->config['timelimit'];
        $this->_debug("Using function $function on scope $scope (\$ns_function is $ns_function)");
        if ($this->vlv_active) {
            if (!empty($this->additional_filter)) {
                $filter = "(&" . $filter . $this->additional_filter . ")";
                $this->_debug("C: (With VLV) Setting a filter (with additional filter) of " . $filter);
            }
            else {
                $this->_debug("C: (With VLV) Setting a filter (without additional filter) of " . $filter);
            }
        }
        else {
            if (!empty($this->additional_filter)) {
                $filter = "(&" . $filter . $this->additional_filter . ")";
            }
            $this->_debug("C: (Without VLV) Setting a filter of " . $filter);
        }
        $this->_debug("Executing search with return attributes: " . var_export($attrs, true));
        $ldap_result = @$function($this->conn, $base_dn, $filter, $attrs, 0, $sizelimit, $timelimit);
        if (!$ldap_result) {
            $this->_debug("$function failed for dn=$base_dn: ".ldap_error($this->conn));
            return false;
        }
        // when running on a patched PHP we can use the extended functions
        // to retrieve the total count from the LDAP search result
        if ($this->vlv_active && function_exists('ldap_parse_virtuallist_control')) {
            if (ldap_parse_result($this->conn, $ldap_result, $errcode, $matcheddn, $errmsg, $referrals, $serverctrls)) {
                ldap_parse_virtuallist_control($this->conn, $serverctrls, $last_offset, $vlv_count, $vresult);
                $this->_debug("S: VLV result: last_offset=$last_offset; content_count=$vlv_count");
            }
            else {
                $this->_debug("S: ".($errmsg ? $errmsg : ldap_error($this->conn)));
            }
        }
        else if ($this->debug) {
            $this->_debug("S: ".ldap_count_entries($this->conn, $ldap_result)." record(s) found");
        }
        $result = new Net_LDAP3_Result($this->conn, $base_dn, $filter, $scope, $ldap_result);
        $result->set('offset', $last_offset);
        $result->set('count', $vlv_count);
        $result->set('vlv', true);
        return $count_only ? $result->count() : $result;
    }
    /**
     * Similar to Net_LDAP3::search() but using a search array with multiple
     * keys and values that to continue to use the VLV but with an original
     * filter adding the search stuff to an additional filter.
     *
     * @see Net_LDAP3::search()
     */
    public function search_entries($base_dn, $filter = '(objectclass=*)', $scope = 'sub', $attrs = array('dn'), $props = array())
    {
        $this->_debug("Net_LDAP3::search_entries with search " . var_export($props, true));
        if (is_array($props['search']) && array_key_exists('params', $props['search'])) {
            $_search = $this->search_filter($props['search']);
            $this->_debug("C: Search filter: $_search");
            if (!empty($_search)) {
                $this->additional_filter = $_search;
            }
            else {
                $this->additional_filter = "(|";
                foreach ($props['search'] as $attr => $value) {
                    $this->additional_filter .= "(" . $attr . "=" . $this->_fuzzy_search_prefix() . $value . $this->_fuzzy_search_suffix() . ")";
                }
                $this->additional_filter .= ")";
            }
            $this->_debug("C: Setting an additional filter " . $this->additional_filter);
        }
        $search = $this->search($base_dn, $filter, $scope, $attrs, $props);
        $this->additional_filter = null;
        if (!$search) {
            $this->_debug("Net_LDAP3: Search did not succeed!");
            return false;
        }
        return $search;
    }
    /**
     * Create LDAP search filter string according to defined parameters.
     */
    public function search_filter($search)
    {
        if (empty($search) || !is_array($search) || empty($search['params'])) {
            return null;
        }
        $operators = array('=', '~=', '>=', '<=');
        $filter    = '';
        foreach ((array) $search['params'] as $field => $param) {
            switch ((string)$param['type']) {
                case 'prefix':
                    $prefix = '';
                    $suffix = '*';
                    break;
                case 'suffix':
                    $prefix = '*';
                    $suffix = '';
                    break;
                case 'exact':
                case '=':
                case '~=':
                case '>=':
                case '<=':
                    $prefix = '';
                    $suffix = '';
                    break;
                case 'exists':
                    $prefix = '*';
                    $suffix = '';
                    $param['value'] = '';
                    break;
                case 'both':
                default:
                    $prefix = '*';
                    $suffix = '*';
                    break;
            }
            $operator = $param['type'] && in_array($param['type'], $operators) ? $param['type'] : '=';
            if (is_array($param['value'])) {
                $val_filter = array();
                foreach ($param['value'] as $val) {
                    $value = self::quote_string($val);
                    $val_filter[] = "(" . $field . $operator . $prefix . $value . $suffix . ")";
                }
                $filter .= "(|" . implode($val_filter, '') . ")";
            }
            else {
                $value = self::quote_string($param['value']);
                $filter .= "(" . $field . $operator . $prefix . $value . $suffix . ")";
            }
        }
        // join search parameters with specified operator ('OR' or 'AND')
        if (count($search['params']) > 1) {
            $filter = '(' . ($search['operator'] == 'AND' ? '&' : '|') . $filter . ')';
        }
        return $filter;
    }
    /**
     * Set properties for VLV-based paging
     *
     * @param number $page Page number to list (starting at 1)
     * @param number $size Number of entries to display on one page
     */
    public function set_vlv_page($page, $size = 10)
    {
        $this->list_page = $page;
        $this->page_size = $size;
    }
    /**
     * Turn an LDAP entry into a regular PHP array with attributes as keys.
     *
     * @param array $entry Attributes array as retrieved from ldap_get_attributes() or ldap_get_entries()
     *
     * @return array Hash array with attributes as keys
     */
    public static function normalize_entry($entry)
    {
        $rec = array();
        for ($i=0; $i < $entry['count']; $i++) {
            $attr = $entry[$i];
            for ($j=0; $j < $entry[$attr]['count']; $j++) {
                $rec[$attr][$j] = $entry[$attr][$j];
            }
        }
        return $rec;
    }
    /**
     * Normalize a ldap result by converting entry attribute arrays into single values
     */
    public static function normalize_result($_result)
    {
        if (!is_array($_result)) {
            return array();
        }
        $result = array();
        for ($x = 0; $x < $_result['count']; $x++) {
            $dn = $_result[$x]['dn'];
            $result[$dn] = array();
            for ($y = 0; $y < $_result[$x]['count']; $y++) {
                $attr = $_result[$x][$y];
                if ($_result[$x][$attr]['count'] == 1) {
                    switch ($attr) {
                        case 'objectclass':
                            $result[$dn][$attr] = array(strtolower($_result[$x][$attr][0]));
                            break;
                        default:
                            $result[$dn][$attr] = $_result[$x][$attr][0];
                            break;
                    }
                }
                else {
                    $result[$dn][$attr] = array();
                    for ($z = 0; $z < $_result[$x][$attr]['count']; $z++) {
                        switch ($attr) {
                            case 'objectclass':
                                $result[$dn][$attr][] = strtolower($_result[$x][$attr][$z]);
                                break;
                            default:
                                $result[$dn][$attr][] = $_result[$x][$attr][$z];
                                break;
                        }
                    }
                }
            }
        }
        return $result;
    }
    public static function scopeint2str($scope)
    {
        switch ($scope) {
            case 2:
                return 'sub';
                break;
            case 1:
                return 'one';
                break;
            case 0:
                return 'base';
                break;
            default:
                $this->_debug("Scope $scope is not a valid scope integer");
                break;
        }
    }
    /**
     * Choose the right PHP function according to scope property
     *
     * @param string $scope         The LDAP scope (sub|base|list)
     * @param string $ns_function   Function to be used for numSubOrdinates queries
     * @return string  PHP function to be used to query directory
     */
    public static function scope_to_function($scope, &$ns_function = null)
    {
        switch ($scope) {
            case 'sub':
                $function = $ns_function  = 'ldap_search';
                break;
            case 'base':
                $function = $ns_function = 'ldap_read';
                break;
            case 'one':
            case 'list':
            default:
                $function    = 'ldap_list';
                $ns_function = 'ldap_read';
                break;
        }
        return $function;
    }
    private function config_set_config_get_hook($callback)
    {
        $this->_config_get_hook = $callback;
    }
    private function config_set_config_set_hook($callback)
    {
        $this->_config_set_hook = $callback;
    }
    /**
     * Sets the debug level both for this class and the ldap connection.
     */
    private function config_set_debug($value)
    {
        $this->config['debug'] = (bool) $value;
        if ((int)($value) > 0) {
            ldap_set_option(null, LDAP_OPT_DEBUG_LEVEL, (int)($value));
        }
    }
    /**
     *  Sets a log hook that is called with every log message in this module.
     */
    private function config_set_log_hook($callback)
    {
        $this->_log_hook = $callback;
    }
    /**
     * Find a matching VLV
     */
    protected function find_vlv($base_dn, $filter, $scope, $sort_attrs = null)
    {
        if ($scope == 'base') {
            return false;
        }
        $vlv_indexes = $this->find_vlv_indexes_and_searches();
        if (empty($vlv_indexes)) {
            return false;
        }
        $this->_debug("Existing vlv index and search information", $vlv_indexes);
        $filter = strtolower($filter);
        foreach ($vlv_indexes as $vlv_index) {
            if (!empty($vlv_index[$base_dn])) {
                $this->_debug("Found a VLV for base_dn: " . $base_dn);
                if ($vlv_index[$base_dn]['filter'] == $filter) {
                    if ($vlv_index[$base_dn]['scope'] == $scope) {
                        $this->_debug("Scope and filter matches");
                        // Not passing any sort attributes means you don't care
                        if (!empty($sort_attrs)) {
                            $sort_attrs = (array) $sort_attrs;
                            foreach ($vlv_index[$base_dn]['sort'] as $sss_config) {
                                if (count(array_intersect($sort_attrs, $sss_config)) == count($sort_attrs)) {
                                    return $sort_attrs;
                                }
                            }
                            $this->_error("The requested sorting does not match any server-side sorting configuration");
                            return false;
                        }
                        else {
                            return $vlv_index[$base_dn]['sort'][0];
                        }
                    }
                    else {
                        $this->_debug("Scope does not match. VLV: " . var_export($vlv_index[$base_dn]['scope'], true)
                            . " while looking for " . var_export($scope, true));
                        return false;
                    }
                }
                else {
                    $this->_debug("Filter does not match");
                }
            }
        }
        return false;
    }
    /**
     * Return VLV indexes and searches including necessary configuration
     * details.
     */
    protected function find_vlv_indexes_and_searches()
    {
        if ($this->config['vlv'] === false) {
            return false;
        }
        if (is_array($this->config['vlv'])) {
            return $this->config['vlv'];
        }
        if ($this->_vlv_indexes_and_searches !== null) {
            return $this->_vlv_indexes_and_searches;
        }
        $this->_vlv_indexes_and_searches = array();
        $config_root_dn = $this->config_get('config_root_dn');
        if (empty($config_root_dn)) {
            return array();
        }
        if ($this->cache && ($cached_config = $this->cache->get('vlvconfig'))) {
            $this->_vlv_indexes_and_searches = $cached_config;
            return $this->_vlv_indexes_and_searches;
        }
        $this->_debug("No VLV information available yet, refreshing");
        $search_filter = '(objectclass=vlvsearch)';
        $search_result = ldap_search($this->conn, $config_root_dn, $search_filter, array('*'), 0, 0, 0);
        if ($search_result === false) {
            $this->_debug("Search for '$search_filter' on '$config_root_dn' failed:".ldap_error($this->conn));
            return;
        }
        $vlv_searches = new Net_LDAP3_Result($this->conn, $config_root_dn, $search_filter, 'sub', $search_result);
        if ($vlv_searches->count() < 1) {
            $this->_debug("Empty result from search for '(objectclass=vlvsearch)' on '$config_root_dn'");
            return;
        }
        $index_filter = '(objectclass=vlvindex)';
        foreach ($vlv_searches->entries(true) as $vlv_search_dn => $vlv_search_attrs) {
            // The attributes we are interested in are as follows:
            $_vlv_base_dn = $vlv_search_attrs['vlvbase'];
            $_vlv_scope   = $vlv_search_attrs['vlvscope'];
            $_vlv_filter  = $vlv_search_attrs['vlvfilter'];
            // Multiple indexes may exist
            $index_result = ldap_search($this->conn, $vlv_search_dn, $index_filter, array('*'), 0, 0, 0);
            if ($index_result === false) {
                $this->_debug("Search for '$index_filter' on '$vlv_search_dn' failed:".ldap_error($this->conn));
                continue;
            }
            $vlv_indexes = new Net_LDAP3_Result($this->conn, $vlv_search_dn, $index_filter, 'sub', $index_result);
            $vlv_indexes = $vlv_indexes->entries(true);
            // Reset this one for each VLV search.
            $_vlv_sort = array();
            foreach ($vlv_indexes as $vlv_index_dn => $vlv_index_attrs) {
                $_vlv_sort[] = explode(' ', $vlv_index_attrs['vlvsort']);
            }
            $this->_vlv_indexes_and_searches[] = array(
                    $_vlv_base_dn => array(
                            'scope'  => self::scopeint2str($_vlv_scope),
                            'filter' => strtolower($_vlv_filter),
                            'sort'   => $_vlv_sort,
                        ),
                );
        }
        // cache this
        if ($this->cache) {
            $this->cache->set('vlvconfig', $this->_vlv_indexes_and_searches);
        }
        return $this->_vlv_indexes_and_searches;
    }
    private function init_schema()
    {
        // use PEAR include if autoloading failed
        if (!class_exists('Net_LDAP2')) {
            require_once('Net/LDAP2.php');
        }
        $port = $this->config_get('port', 389);
        $tls  = $this->config_get('use_tls', false);
        foreach ((array) $this->config_get('hosts') as $host) {
            $this->_debug("C: Connect [$host:$port]");
            $_ldap_cfg = array(
                'host'   => $host,
                'port'   => $port,
                'tls'    => $tls,
                'version' => 3,
                'binddn' => $this->config_get('service_bind_dn'),
                'bindpw' => $this->config_get('service_bind_pw')
            );
            $_ldap_schema_cache_cfg = array(
                'path' => "/tmp/" . $host . ":" . ($port ? $port : '389') . "-Net_LDAP2_Schema.cache",
                'max_age' => 86400,
            );
            $_ldap = Net_LDAP2::connect($_ldap_cfg);
            if (!is_a($_ldap, 'Net_LDAP2_Error')) {
                $this->_debug("S: OK");
                break;
            }
            $this->_debug("S: NOT OK");
            $this->_debug($_ldap->getMessage());
        }
        if (is_a($_ldap, 'Net_LDAP2_Error')) {
            return null;
        }
        $_ldap_schema_cache = new Net_LDAP2_SimpleFileSchemaCache($_ldap_schema_cache_cfg);
        $_ldap->registerSchemaCache($_ldap_schema_cache);
        // TODO: We should learn what LDAP tech. we're running against.
        // Perhaps with a scope base objectclass recognize rootdse entry
        $schema_root_dn = $this->config_get('schema_root_dn');
        if (!$schema_root_dn) {
            $_schema = $_ldap->schema();
        }
        return $_schema;
    }
    private function list_group_member($dn, $members, $recurse = true)
    {
        $this->_debug("Called list_group_member(" . $dn . ")");
        $members       = (array) $members;
        $group_members = array();
        // remove possible 'count' item
        unset($members['count']);
        // Use the member attributes to return an array of member ldap objects
        // NOTE that the member attribute is supposed to contain a DN
        foreach ($members as $member) {
            $member_entry = $this->get_entry($member, array('member', 'uniquemember', 'memberurl', 'objectclass'));
            if (empty($member_entry)) {
                continue;
            }
            $group_members[$member] = $member;
            if ($recurse) {
                // Nested groups
                $group_group_members = $this->list_group_members($member, $member_entry);
                if ($group_group_members) {
                    $group_members = array_merge($group_group_members, $group_members);
                }
            }
        }
        return array_filter($group_members);
    }
    private function list_group_uniquemember($dn, $uniquemembers, $recurse = true)
    {
        $this->_debug("Called list_group_uniquemember(" . $dn . ")", $entry);
        $uniquemembers = (array)($uniquemembers);
        $group_members = array();
        // remove possible 'count' item
        unset($uniquemembers['count']);
        foreach ($uniquemembers as $member) {
            $member_entry = $this->get_entry($member, array('member', 'uniquemember', 'memberurl', 'objectclass'));
            if (empty($member_entry)) {
                continue;
            }
            $group_members[$member] = $member;
            if ($recurse) {
                // Nested groups
                $group_group_members = $this->list_group_members($member, $member_entry);
                if ($group_group_members) {
                    $group_members = array_merge($group_group_members, $group_members);
                }
            }
        }
        return array_filter($group_members);
    }
    private function list_group_memberurl($dn, $memberurls, $recurse = true)
    {
        $this->_debug("Called list_group_memberurl(" . $dn . ")");
        $group_members = array();
        $memberurls    = (array) $memberurls;
        $attributes    = array('member', 'uniquemember', 'memberurl', 'objectclass');
        // remove possible 'count' item
        unset($memberurls['count']);
        foreach ($memberurls as $url) {
            $ldap_uri = $this->parse_memberurl($url);
            $result   = $this->search($ldap_uri[3], $ldap_uri[6], 'sub', $attributes);
            if (!$result) {
                continue;
            }
            foreach ($result->entries(true) as $entry_dn => $_entry) {
                $group_members[$entry_dn] = $entry_dn;
                $this->_debug("Found " . $entry_dn);
                if ($recurse) {
                    // Nested group
                    $group_group_members = $this->list_group_members($entry_dn, $_entry);
                    if ($group_group_members) {
                        $group_members = array_merge($group_members, $group_group_members);
                    }
                }
            }
        }
        return array_filter($group_members);
    }
    /**
     * memberUrl attribute parser
     *
     * @param string $url URL string
     *
     * @return array URL elements
     */
    private function parse_memberurl($url)
    {
        preg_match('/(.*):\/\/(.*)\/(.*)\?(.*)\?(.*)\?(.*)/', $url, $matches);
        return $matches;
    }
    private function modify_entry_attributes($subject_dn, $attributes)
    {
        // Opportunities to set false include failed ldap commands.
        $result = true;
        if (is_array($attributes['rename']) && !empty($attributes['rename'])) {
            $olddn  = $attributes['rename']['dn'];
            $newrdn = $attributes['rename']['new_rdn'];
            if (!empty($attributes['rename']['new_parent'])) {
                $new_parent = $attributes['rename']['new_parent'];
            }
            else {
                $new_parent = null;
            }
            $this->_debug("LDAP: C: Rename $olddn to $newrdn,$new_parent");
            $result = ldap_rename($this->conn, $olddn, $newrdn, $new_parent, true);
            if ($result) {
                $this->_debug("LDAP: S: OK");
                if ($new_parent) {
                    $subject_dn = $newrdn . ',' . $new_parent;
                }
                else {
                    $old_parent_dn_components = ldap_explode_dn($olddn, 0);
                    unset($old_parent_dn_components["count"]);
                    $old_rdn       = array_shift($old_parent_dn_components);
                    $old_parent_dn = implode(",", $old_parent_dn_components);
                    $subject_dn    = $newrdn . ',' . $old_parent_dn;
                }
            }
            else {
                $this->_debug("LDAP: S: " . ldap_error($this->conn));
                $this->_warning("LDAP: Failed to rename $olddn to $newrdn,$new_parent");
                return false;
            }
        }
        if (is_array($attributes['replace']) && !empty($attributes['replace'])) {
            $this->_debug("LDAP: C: Mod-Replace $subject_dn: " . json_encode($attributes['replace']));
            $result = ldap_mod_replace($this->conn, $subject_dn, $attributes['replace']);
            if ($result) {
                $this->_debug("LDAP: S: OK");
            }
            else {
                $this->_debug("LDAP: S: " . ldap_error($this->conn));
                $this->_warning("LDAP: Failed to replace attributes on $subject_dn: " . json_encode($attributes['replace']));
                return false;
            }
        }
        if (is_array($attributes['del']) && !empty($attributes['del'])) {
            $this->_debug("LDAP: C: Mod-Delete $subject_dn: " . json_encode($attributes['del']));
            $result = ldap_mod_del($this->conn, $subject_dn, $attributes['del']);
            if ($result) {
                $this->_debug("LDAP: S: OK");
            }
            else {
                $this->_debug("LDAP: S: " . ldap_error($this->conn));
                $this->_warning("LDAP: Failed to delete attributes on $subject_dn: " . json_encode($attributes['del']));
                return false;
            }
        }
        if (is_array($attributes['add']) && !empty($attributes['add'])) {
            $this->_debug("LDAP: C: Mod-Add $subject_dn: " . json_encode($attributes['add']));
            $result = ldap_mod_add($this->conn, $subject_dn, $attributes['add']);
            if ($result) {
                $this->_debug("LDAP: S: OK");
            }
            else {
                $this->_debug("LDAP: S: " . ldap_error($this->conn));
                $this->_warning("LDAP: Failed to add attributes on $subject_dn: " . json_encode($attributes['add']));
                return false;
            }
        }
        return true;
    }
    private function parse_attribute_level_rights($attribute_value)
    {
        $attribute_value  = str_replace(", ", ",", $attribute_value);
        $attribute_values = explode(",", $attribute_value);
        $attribute_value  = array();
        foreach ($attribute_values as $access_right) {
            $access_right_components = explode(":", $access_right);
            $access_attribute        = strtolower(array_shift($access_right_components));
            $access_value            = array_shift($access_right_components);
            $attribute_value[$access_attribute] = array();
            for ($i = 0; $i < strlen($access_value); $i++) {
                $method = $this->attribute_level_rights_map[substr($access_value, $i, 1)];
                if (!in_array($method, $attribute_value[$access_attribute])) {
                    $attribute_value[$access_attribute][] = $method;
                }
            }
        }
        return $attribute_value;
    }
    private function parse_entry_level_rights($attribute_value)
    {
        $_attribute_value = array();
        for ($i = 0; $i < strlen($attribute_value); $i++) {
            $method = $this->entry_level_rights_map[substr($attribute_value, $i, 1)];
            if (!in_array($method, $_attribute_value)) {
                $_attribute_value[] = $method;
            }
        }
        return $_attribute_value;
    }
    private function supported_controls()
    {
        if (!empty($this->supported_controls)) {
            return $this->supported_controls;
        }
        $this->_info("Obtaining supported controls");
        if ($result = $this->search('', '(objectclass=*)', 'base', array('supportedcontrol'))) {
            $result  = $result->entries(true);
            $control = $result['']['supportedcontrol'];
        }
        else {
            $control = array();
        }
        $this->_info("Obtained " . count($control) . " supported controls");
        $this->supported_controls = $control;
        return $control;
    }
    protected function _alert()
    {
        $this->__log(LOG_ALERT, func_get_args());
    }
    protected function _critical()
    {
        $this->__log(LOG_CRIT, func_get_args());
    }
    protected function _debug()
    {
        $this->__log(LOG_DEBUG, func_get_args());
    }
    protected function _emergency()
    {
        $this->__log(LOG_EMERG, func_get_args());
    }
    protected function _error()
    {
        $this->__log(LOG_ERR, func_get_args());
    }
    protected function _info()
    {
        $this->__log(LOG_INFO, func_get_args());
    }
    protected function _notice()
    {
        $this->__log(LOG_NOTICE, func_get_args());
    }
    protected function _warning()
    {
        $this->__log(LOG_WARNING, func_get_args());
    }
    /**
     *  Log a message.
     */
    private function __log($level, $args)
    {
        $msg = array();
        foreach ($args as $arg) {
            $msg[] = !is_string($arg) ? var_export($arg, true) : $arg;
        }
        if (!empty($this->_log_hook)) {
            call_user_func_array($this->_log_hook, array($level, $msg));
            return;
        }
        if ($this->debug_level > 0) {
            syslog($level, implode("\n", $msg));
        }
    }
    /**
     * Add BER sequence with correct length and the given identifier
     */
    private static function _ber_addseq($str, $identifier)
    {
        $len = dechex(strlen($str)/2);
        if (strlen($len) % 2 != 0) {
            $len = '0'.$len;
        }
        return $identifier . $len . $str;
    }
    /**
     * Returns BER encoded integer value in hex format
     */
    private static function _ber_encode_int($offset)
    {
        $val    = dechex($offset);
        $prefix = '';
        // check if bit 8 of high byte is 1
        if (preg_match('/^[89abcdef]/', $val)) {
            $prefix = '00';
        }
        if (strlen($val)%2 != 0) {
            $prefix .= '0';
        }
        return $prefix . $val;
    }
    /**
     * Quotes attribute value string
     *
     * @param string $str Attribute value
     * @param bool   $dn  True if the attribute is a DN
     *
     * @return string Quoted string
     */
    public static function quote_string($str, $is_dn = false)
    {
        // take firt entry if array given
        if (is_array($str)) {
            $str = reset($str);
        }
        if ($is_dn) {
            $replace = array(
                ',' => '\2c',
                '=' => '\3d',
                '+' => '\2b',
                '<' => '\3c',
                '>' => '\3e',
                ';' => '\3b',
                "\\"=> '\5c',
                '"' => '\22',
                '#' => '\23'
            );
        }
        else {
            $replace = array(
                '*' => '\2a',
                '(' => '\28',
                ')' => '\29',
                "\\" => '\5c',
                '/' => '\2f'
            );
        }
        return strtr($str, $replace);
    }
    /**
     * create ber encoding for sort control
     *
     * @param array List of cols to sort by
     * @return string BER encoded option value
     */
    private static function _sort_ber_encode($sortcols)
    {
        $str = '';
        foreach (array_reverse((array)$sortcols) as $col) {
            $ber_val = self::_string2hex($col);
            // 30 = ber sequence with a length of octet value
            // 04 = octet string with a length of the ascii value
            $oct = self::_ber_addseq($ber_val, '04');
            $str = self::_ber_addseq($oct, '30') . $str;
        }
        // now tack on sequence identifier and length
        $str = self::_ber_addseq($str, '30');
        return pack('H'.strlen($str), $str);
    }
    /**
     * Returns ascii string encoded in hex
     */
    private static function _string2hex($str)
    {
        $hex = '';
        for ($i=0; $i < strlen($str); $i++)
            $hex .= dechex(ord($str[$i]));
        return $hex;
    }
    /**
     * Generate BER encoded string for Virtual List View option
     *
     * @param integer List offset (first record)
     * @param integer Records per page
     * @return string BER encoded option value
     */
    private static function _vlv_ber_encode($offset, $rpp, $search = '')
    {
        // This string is ber-encoded, php will prefix this value with:
        // 04 (octet string) and 10 (length of 16 bytes)
        // the code behind this string is broken down as follows:
        // 30 = ber sequence with a length of 0e (14) bytes following
        // 02 = type integer (in two's complement form) with 2 bytes following (beforeCount): 01 00 (ie 0)
        // 02 = type integer (in two's complement form) with 2 bytes following (afterCount):  01 18 (ie 25-1=24)
        // a0 = type context-specific/constructed with a length of 06 (6) bytes following
        // 02 = type integer with 2 bytes following (offset): 01 01 (ie 1)
        // 02 = type integer with 2 bytes following (contentCount):  01 00
        // whith a search string present:
        // 81 = type context-specific/constructed with a length of 04 (4) bytes following (the length will change here)
        // 81 indicates a user string is present where as a a0 indicates just a offset search
        // 81 = type context-specific/constructed with a length of 06 (6) bytes following
        // the following info was taken from the ISO/IEC 8825-1:2003 x.690 standard re: the
        // encoding of integer values (note: these values are in
        // two-complement form so since offset will never be negative bit 8 of the
        // leftmost octet should never by set to 1):
        // 8.3.2: If the contents octets of an integer value encoding consist
        // of more than one octet, then the bits of the first octet (rightmost) and bit 8
        // of the second (to the left of first octet) octet:
        // a) shall not all be ones; and
        // b) shall not all be zero
        if ($search) {
            $search  = preg_replace('/[^-[:alpha:] ,.()0-9]+/', '', $search);
            $ber_val = self::_string2hex($search);
            $str     = self::_ber_addseq($ber_val, '81');
        }
        else {
            // construct the string from right to left
            $str = "020100"; # contentCount
            // returns encoded integer value in hex format
            $ber_val = self::_ber_encode_int($offset);
            // calculate octet length of $ber_val
            $str = self::_ber_addseq($ber_val, '02') . $str;
            // now compute length over $str
            $str = self::_ber_addseq($str, 'a0');
        }
        // now tack on records per page
        $str = "020100" . self::_ber_addseq(self::_ber_encode_int($rpp-1), '02') . $str;
        // now tack on sequence identifier and length
        $str = self::_ber_addseq($str, '30');
        return pack('H'.strlen($str), $str);
    }
    private function _fuzzy_search_prefix()
    {
        switch ($this->config_get("fuzzy_search", 2)) {
            case 2:
                return "*";
                break;
            case 1:
            case 0:
            default:
                return "";
                break;
        }
    }
    private function _fuzzy_search_suffix()
    {
        switch ($this->config_get("fuzzy_search", 2)) {
            case 2:
                return "*";
                break;
            case 1:
                return "*";
            case 0:
            default:
                return "";
                break;
        }
    }
    /**
     * Return the search string value to be used in VLV controls
     */
    private function _vlv_search($sort, $search)
    {
        if (!empty($this->additional_filter)) {
            $this->_debug("Not setting a VLV search filter because we already have a filter");
            return;
        }
        if (empty($search)) {
            return;
        }
        $search_suffix = $this->_fuzzy_search_suffix();
        foreach ($search as $attr => $value) {
            if (!in_array(strtolower($attr), $sort)) {
                $this->_debug("Cannot use VLV search using attribute not indexed: $attr (not in " . var_export($sort, true) . ")");
                return;
            }
            else {
                return $value . $search_suffix;
            }
        }
    }
    /**
     * Set server controls for Virtual List View (paginated listing)
     */
    private function _vlv_set_controls($sort, $list_page, $page_size, $search = null)
    {
        $sort_ctrl = array(
            'oid'   => "1.2.840.113556.1.4.473",
            'value' => self::_sort_ber_encode($sort)
        );
        if (!empty($search)) {
            $this->_debug("_vlv_set_controls to include search: " . var_export($search, true));
        }
        $vlv_ctrl  = array(
            'oid' => "2.16.840.1.113730.3.4.9",
            'value' => self::_vlv_ber_encode(
                    $offset = ($list_page-1) * $page_size + 1,
                    $page_size,
                    $search
            ),
            'iscritical' => true
        );
        $this->_debug("C: set controls sort=" . join(' ', unpack('H'.(strlen($sort_ctrl['value'])*2), $sort_ctrl['value']))
            . " (" . implode(',', (array) $sort) . ");"
            . " vlv=" . join(' ', (unpack('H'.(strlen($vlv_ctrl['value'])*2), $vlv_ctrl['value']))) . " ($offset/$page_size)");
        if (!ldap_set_option($this->conn, LDAP_OPT_SERVER_CONTROLS, array($sort_ctrl, $vlv_ctrl))) {
            $this->_debug("S: ".ldap_error($this->conn));
            $this->set_error(self::ERROR_SEARCH, 'vlvnotsupported');
            return false;
        }
        return true;
    }
}
program/lib/Net/LDAP3/Result.php
New file
@@ -0,0 +1,139 @@
<?php
/*
 +-----------------------------------------------------------------------+
 | Net/LDAP3/Result.php                                                  |
 |                                                                       |
 | Based on code created by the Roundcube Webmail team.                  |
 |                                                                       |
 | Copyright (C) 2006-2014, The Roundcube Dev Team                       |
 | Copyright (C) 2012-2014, Kolab Systems AG                             |
 |                                                                       |
 | Licensed under the GNU General Public License version 3 or            |
 | any later version with exceptions for plugins.                        |
 | See the README file for a full license statement.                     |
 |                                                                       |
 | PURPOSE:                                                              |
 |   Provide advanced functionality for accessing LDAP directories       |
 |                                                                       |
 +-----------------------------------------------------------------------+
 | Authors: Thomas Bruederli <roundcube@gmail.com>                       |
 |          Jeroen van Meeuwen <vanmeeuwen@kolabsys.com>                 |
 +-----------------------------------------------------------------------+
*/
/**
 * Model class representing an LDAP search result
 *
 * @package LDAP
 */
class Net_LDAP3_Result implements Iterator
{
    protected $conn;
    protected $base_dn;
    protected $filter;
    protected $scope;
    private $count;
    private $current;
    private $iteratorkey = 0;
    /**
     * Default constructor
     *
     * @param resource $conn      LDAP link identifier
     * @param string   $base_dn   Base DN used to get this result
     * @param string   $filter    Filter query used to get this result
     * @param string   $scope     Scope of the result
     * @param resource $result    LDAP result entry identifier
     */
    function __construct($conn, $base_dn, $filter, $scope, $result)
    {
        $this->conn    = $conn;
        $this->base_dn = $base_dn;
        $this->filter  = $filter;
        $this->scope   = $scope;
        $this->result  = $result;
    }
    public function get($property, $default = null)
    {
        if (isset($this->$property)) {
            return $this->$property;
        } else {
            return $default;
        }
    }
    public function set($property, $value)
    {
        $this->$property = $value;
    }
    /**
     *
     */
    public function sort($attr)
    {
        return ldap_sort($this->conn, $this->result, $attr);
    }
    /**
     *
     */
    public function count()
    {
        if (!isset($this->count))
            $this->count = ldap_count_entries($this->conn, $this->result);
        return $this->count;
    }
    /**
     *
     */
    public function entries($normalize = false)
    {
        $entries = ldap_get_entries($this->conn, $this->result);
        if ($normalize) {
            return Net_LDAP3::normalize_result($entries);
        }
        return $entries;
    }
    /***  Implement PHP 5 Iterator interface to make foreach work  ***/
    function current()
    {
        $attrib       = ldap_get_attributes($this->conn, $this->current);
        $attrib['dn'] = ldap_get_dn($this->conn, $this->current);
        return $attrib;
    }
    function key()
    {
        return $this->iteratorkey;
    }
    function rewind()
    {
        $this->iteratorkey = 0;
        $this->current = ldap_first_entry($this->conn, $this->result);
    }
    function next()
    {
        $this->iteratorkey++;
        $this->current = ldap_next_entry($this->conn, $this->current);
    }
    function valid()
    {
        return (bool)$this->current;
    }
}
program/lib/Roundcube/rcube_ldap.php
@@ -232,8 +232,7 @@
        // initialize ldap wrapper object
        $this->ldap = new rcube_ldap_generic($this->prop);
        $this->ldap->set_cache($this->cache);
        $this->ldap->set_debug($this->debug);
        $this->ldap->config_set(array('cache' => $this->cache, 'debug' => $this->debug));
        $this->_connect();
    }
@@ -320,8 +319,7 @@
                            // we need to use a separate LDAP connection
                            if (!empty($this->prop['vlv'])) {
                                $ldap = new rcube_ldap_generic($this->prop);
                                $ldap->set_debug($this->debug);
                                $ldap->set_cache($this->cache);
                                $ldap->config_set(array('cache' => $this->cache, 'debug' => $this->debug));
                                if (!$ldap->connect($host)) {
                                    continue;
                                }
@@ -335,7 +333,7 @@
                        $res = $ldap->search($search_base_dn, $search_filter, 'sub', $search_attribs);
                        if ($res) {
                            $res->rewind();
                            $replaces['%dn'] = $res->get_dn();
                            $replaces['%dn'] = key($res->entries(TRUE));
                            // add more replacements from 'search_bind_attrib' config
                            if ($search_bind_attrib) {
@@ -1066,7 +1064,7 @@
            }
        }
        if (!$this->ldap->add($dn, $newentry)) {
        if (!$this->ldap->add_entry($dn, $newentry)) {
            $this->set_error(self::ERROR_SAVING, 'errorsaving');
            return false;
        }
@@ -1078,7 +1076,7 @@
                'objectClass' => (array) $this->prop['sub_fields'][$xidx],
            );
            $this->ldap->add($xdn, $xf);
            $this->ldap->add_entry($xdn, $xf);
        }
        $dn = self::dn_encode($dn);
@@ -1221,7 +1219,7 @@
        if (!empty($subdeldata)) {
            foreach ($subdeldata as $fld => $val) {
                $subdn = $fld.'='.rcube_ldap_generic::quote_string($val).','.$dn;
                if (!$this->ldap->delete($subdn)) {
                if (!$this->ldap->delete_entry($subdn)) {
                    return false;
                }
            }
@@ -1265,7 +1263,7 @@
                    $fld => $val,
                    'objectClass' => (array) $this->prop['sub_fields'][$fld],
                );
                $this->ldap->add($subdn, $xf);
                $this->ldap->add_entry($subdn, $xf);
            }
        }
@@ -1295,7 +1293,7 @@
            if ($this->sub_filter) {
                if ($entries = $this->ldap->list_entries($dn, $this->sub_filter)) {
                    foreach ($entries as $entry) {
                        if (!$this->ldap->delete($entry['dn'])) {
                        if (!$this->ldap->delete_entry($entry['dn'])) {
                            $this->set_error(self::ERROR_SAVING, 'errorsaving');
                            return false;
                        }
@@ -1304,12 +1302,12 @@
            }
            // Delete the record.
            if (!$this->ldap->delete($dn)) {
            if (!$this->ldap->delete_entry($dn)) {
                $this->set_error(self::ERROR_SAVING, 'errorsaving');
                return false;
            }
            // remove contact from all groups where he was member
            // remove contact from all groups where he was a member
            if ($this->groups) {
                $dn = self::dn_encode($dn);
                $group_ids = $this->get_record_groups($dn);
@@ -1342,7 +1340,7 @@
        if ($with_groups && $this->groups && ($groups = $this->_fetch_groups()) && count($groups)) {
            foreach ($groups as $group) {
                $this->ldap->delete($group['dn']);
                $this->ldap->delete_entry($group['dn']);
            }
            if ($this->cache) {
@@ -1567,7 +1565,7 @@
        $this->debug = $dbg;
        if ($this->ldap) {
            $this->ldap->set_debug($dbg);
            $this->ldap->config_set('debug', $dbg);
        }
    }
@@ -1666,7 +1664,7 @@
            }
            $ldap = clone $this->ldap;
            $ldap->set_config($this->prop['groups']);
            $ldap->config_set($this->prop['groups']);
            $ldap->set_vlv_page($vlv_page+1, $page_size);
        }
@@ -1790,7 +1788,7 @@
            $member_attr => '',
        );
        if (!$this->ldap->add($new_dn, $new_entry)) {
        if (!$this->ldap->add_entry($new_dn, $new_entry)) {
            $this->set_error(self::ERROR_SAVING, 'errorsaving');
            return false;
        }
@@ -1813,7 +1811,7 @@
        $group_cache = $this->_fetch_groups();
        $del_dn      = $group_cache[$group_id]['dn'];
        if (!$this->ldap->delete($del_dn)) {
        if (!$this->ldap->delete_entry($del_dn)) {
            $this->set_error(self::ERROR_SAVING, 'errorsaving');
            return false;
        }
program/lib/Roundcube/rcube_ldap_generic.php
@@ -5,8 +5,8 @@
 | Roundcube/rcube_ldap_generic.php                                      |
 |                                                                       |
 | This file is part of the Roundcube Webmail client                     |
 | Copyright (C) 2006-2013, The Roundcube Dev Team                       |
 | Copyright (C) 2012-2013, Kolab Systems AG                             |
 | Copyright (C) 2006-2014, The Roundcube Dev Team                       |
 | Copyright (C) 2012-2014, Kolab Systems AG                             |
 |                                                                       |
 | Licensed under the GNU General Public License version 3 or            |
 | any later version with exceptions for skins & plugins.                |
@@ -21,464 +21,124 @@
 +-----------------------------------------------------------------------+
*/
/*
  LDAP connection properties
  --------------------------
  $prop = array(
      'host'            => '<ldap-server-address>',
      // or
      'hosts'           => array('directory.verisign.com'),
      'port'            => 389,
      'use_tls'         => true|false,
      'ldap_version'    => 3,             // using LDAPv3
      'auth_method'     => '',            // SASL authentication method (for proxy auth), e.g. DIGEST-MD5
      'attributes'      => array('dn'),   // List of attributes to read from the server
      'vlv'             => false,         // Enable Virtual List View to more efficiently fetch paginated data (if server supports it)
      'config_root_dn'  => 'cn=config',   // Root DN to read config (e.g. vlv indexes) from
      'numsub_filter'   => '(objectClass=organizationalUnit)',   // with VLV, we also use numSubOrdinates to query the total number of records. Set this filter to get all numSubOrdinates attributes for counting
      'sizelimit'       => '0',           // Enables you to limit the count of entries fetched. Setting this to 0 means no limit.
      'timelimit'       => '0',           // Sets the number of seconds how long is spend on the search. Setting this to 0 means no limit.
      'network_timeout' => 10,            // The timeout (in seconds) for connect + bind arrempts. This is only supported in PHP >= 5.3.0 with OpenLDAP 2.x
      'referrals'       => true|false,    // Sets the LDAP_OPT_REFERRALS option. Mostly used in multi-domain Active Directory setups
  );
*/
/**
 * Model class to access an LDAP directories
 *
 * @package    Framework
 * @subpackage LDAP
 */
class rcube_ldap_generic
class rcube_ldap_generic extends Net_LDAP3
{
    const UPDATE_MOD_ADD = 1;
    const UPDATE_MOD_DELETE = 2;
    const UPDATE_MOD_REPLACE = 4;
    const UPDATE_MOD_FULL = 7;
    public $conn;
    public $vlv_active = false;
    /** private properties */
    protected $cache = null;
    protected $config = array();
    protected $attributes = array('dn');
    protected $entries = null;
    protected $result = null;
    protected $debug = false;
    protected $list_page = 1;
    protected $page_size = 10;
    protected $vlv_config = null;
    /**
    * Object constructor
    *
    * @param array $p LDAP connection properties
    */
    function __construct($p)
    function __construct($config = null)
    {
        $this->config = $p;
        parent::__construct($config);
        if (is_array($p['attributes']))
            $this->attributes = $p['attributes'];
        if (!is_array($p['hosts']) && !empty($p['host']))
            $this->config['hosts'] = array($p['host']);
        $this->config_set('log_hook', array($this, 'log'));
    }
    /**
     * Activate/deactivate debug mode
     *
     * @param boolean $dbg True if LDAP commands should be logged
     * Establish a connection to the LDAP server
     */
    public function set_debug($dbg = true)
    {
        $this->debug = $dbg;
    }
    /**
     * Set connection options
     *
     * @param mixed $opt Option name as string or hash array with multiple options
     * @param mixed $val Option value
     */
    public function set_config($opt, $val = null)
    {
        if (is_array($opt))
            $this->config = array_merge($this->config, $opt);
        else
            $this->config[$opt] = $value;
    }
    /**
     * Enable caching by passing an instance of rcube_cache to be used by this object
     *
     * @param object rcube_cache Instance or False to disable caching
     */
    public function set_cache($cache_engine)
    {
        $this->cache = $cache_engine;
    }
    /**
     * Set properties for VLV-based paging
     *
     * @param  number $page  Page number to list (starting at 1)
     * @param  number $size  Number of entries to display on one page
     */
    public function set_vlv_page($page, $size = 10)
    {
        $this->list_page = $page;
        $this->page_size = $size;
    }
    /**
    * Establish a connection to the LDAP server
    */
    public function connect($host = null)
    {
        if (!function_exists('ldap_connect')) {
            rcube::raise_error(array('code' => 100, 'type' => 'ldap',
                'file' => __FILE__, 'line' => __LINE__,
                'message' => "No ldap support in this installation of PHP"),
                true);
            return false;
        }
        // Net_LDAP3 does not support IDNA yet
        // also parse_host() here is very Roundcube specific
        $host = rcube_utils::idn_to_ascii(rcube_utils::parse_host($host));
        if (is_resource($this->conn) && $this->config['host'] == $host)
            return true;
        if (empty($this->config['ldap_version']))
            $this->config['ldap_version'] = 3;
        // iterate over hosts if none specified
        if (!$host) {
            if (!is_array($this->config['hosts']))
                $this->config['hosts'] = array($this->config['hosts']);
            foreach ($this->config['hosts'] as $host) {
                if (!empty($host) && $this->connect($host)) {
                    return true;
                }
            }
            return false;
        }
        // open connection to the given $host
        $host     = rcube_utils::idn_to_ascii(rcube_utils::parse_host($host));
        $hostname = $host . ($this->config['port'] ? ':'.$this->config['port'] : '');
        $this->_debug("C: Connect to $hostname [{$this->config['name']}]");
        if ($lc = @ldap_connect($host, $this->config['port'])) {
            if ($this->config['use_tls'] === true) {
                if (!ldap_start_tls($lc)) {
                    return false;
                }
            }
            $this->_debug("S: OK");
            ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, $this->config['ldap_version']);
            $this->config['host'] = $host;
            $this->conn = $lc;
            if (!empty($this->config['network_timeout']))
                ldap_set_option($lc, LDAP_OPT_NETWORK_TIMEOUT, $this->config['network_timeout']);
            if (isset($this->config['referrals']))
                ldap_set_option($lc, LDAP_OPT_REFERRALS, $this->config['referrals']);
            if (isset($this->config['dereference']))
                ldap_set_option($lc, LDAP_OPT_DEREF, $this->config['dereference']);
        }
        else {
            $this->_debug("S: NOT OK");
        }
        if (!is_resource($this->conn)) {
            rcube::raise_error(array('code' => 100, 'type' => 'ldap',
                'file' => __FILE__, 'line' => __LINE__,
                'message' => "Could not connect to any LDAP server, last tried $hostname"),
                true);
            return false;
        }
        return true;
    }
    /**
     * Bind connection with (SASL-) user and password
     *
     * @param string $authc Authentication user
     * @param string $pass  Bind password
     * @param string $authz Autorization user
     *
     * @return boolean True on success, False on error
     */
    public function sasl_bind($authc, $pass, $authz=null)
    {
        if (!$this->conn) {
            return false;
        }
        if (!function_exists('ldap_sasl_bind')) {
            rcube::raise_error(array('code' => 100, 'type' => 'ldap',
                'file' => __FILE__, 'line' => __LINE__,
                'message' => "Unable to bind: ldap_sasl_bind() not exists"),
                true);
            return false;
        }
        if (!empty($authz)) {
            $authz = 'u:' . $authz;
        }
        if (!empty($this->config['auth_method'])) {
            $method = $this->config['auth_method'];
        }
        else {
            $method = 'DIGEST-MD5';
        }
        $this->_debug("C: SASL Bind [mech: $method, authc: $authc, authz: $authz, pass: **** [" . strlen($pass) . "]");
        if (ldap_sasl_bind($this->conn, NULL, $pass, $method, NULL, $authc, $authz)) {
            $this->_debug("S: OK");
            return true;
        }
        $this->_debug("S: ".ldap_error($this->conn));
        rcube::raise_error(array(
            'code' => ldap_errno($this->conn), 'type' => 'ldap',
            'file' => __FILE__, 'line' => __LINE__,
            'message' => "SASL Bind failed for authcid=$authc ".ldap_error($this->conn)),
            true);
        return false;
    }
    /**
     * Bind connection with DN and password
     *
     * @param string $dn   Bind DN
     * @param string $pass Bind password
     *
     * @return boolean True on success, False on error
     */
    public function bind($dn, $pass)
    {
        if (!$this->conn) {
            return false;
        }
        $this->_debug("C: Bind $dn, pass: **** [" . strlen($pass) . "]");
        if (@ldap_bind($this->conn, $dn, $pass)) {
            $this->_debug("S: OK");
            return true;
        }
        $this->_debug("S: ".ldap_error($this->conn));
        rcube::raise_error(array(
            'code' => ldap_errno($this->conn), 'type' => 'ldap',
            'file' => __FILE__, 'line' => __LINE__,
            'message' => "Bind failed for dn=$dn: ".ldap_error($this->conn)),
            true);
        return false;
    }
    /**
     * Close connection to LDAP server
     */
    public function close()
    {
        if ($this->conn) {
            $this->_debug("C: Close");
            ldap_unbind($this->conn);
            $this->conn = null;
        }
    }
    /**
     * Return the last result set
     *
     * @return object rcube_ldap_result Result object
     */
    function get_result()
    {
        return $this->result;
        return parent::connect($host);
    }
    /**
     * Get a specific LDAP entry, identified by its DN
     *
     * @param string $dn Record identifier
     * @return array     Hash array
     *
     * @return array Hash array
     */
    function get_entry($dn)
    {
        $rec = null;
        if ($this->conn && $dn) {
            $this->_debug("C: Read $dn [(objectclass=*)]");
            if ($ldap_result = @ldap_read($this->conn, $dn, '(objectclass=*)', $this->attributes)) {
                $this->_debug("S: OK");
                if ($entry = ldap_first_entry($this->conn, $ldap_result)) {
                    $rec = ldap_get_attributes($this->conn, $entry);
                }
            }
            else {
                $this->_debug("S: ".ldap_error($this->conn));
            }
            if (!empty($rec)) {
                $rec['dn'] = $dn; // Add in the dn for the entry.
            }
        }
        return $rec;
        return parent::get_entry($dn, $this->attributes);
    }
    /**
     * Execute the LDAP search based on the stored credentials
     *
     * @param string $base_dn  The base DN to query
     * @param string $filter   The LDAP filter for search
     * @param string $scope    The LDAP scope (list|sub|base)
     * @param array  $attrs    List of entry attributes to read
     * @param array  $prop     Hash array with query configuration properties:
     *   - sort: array of sort attributes (has to be in sync with the VLV index)
     *   - search: search string used for VLV controls
     * @param boolean $count_only Set to true if only entry count is requested
     *
     * @return mixed  rcube_ldap_result object or number of entries (if count_only=true) or false on error
     * Prints debug/error info to the log
     */
    public function search($base_dn, $filter = '', $scope = 'sub', $attrs = array('dn'), $prop = array(), $count_only = false)
    public function log($level, $msg)
    {
        if (!$this->conn) {
            return false;
        }
        $msg = implode("\n", $msg);
        if (empty($filter)) {
            $filter = '(objectclass=*)';
        }
        $this->_debug("C: Search $base_dn for $filter");
        $function = self::scope2func($scope, $ns_function);
        // find available VLV index for this query
        if (!$count_only && ($vlv_sort = $this->_find_vlv($base_dn, $filter, $scope, $prop['sort']))) {
            // when using VLV, we get the total count by...
            // ...either reading numSubOrdinates attribute
            if (($sub_filter = $this->config['numsub_filter']) &&
                ($result_count = @$ns_function($this->conn, $base_dn, $sub_filter, array('numSubOrdinates'), 0, 0, 0))
            ) {
                $counts = ldap_get_entries($this->conn, $result_count);
                for ($vlv_count = $j = 0; $j < $counts['count']; $j++)
                    $vlv_count += $counts[$j]['numsubordinates'][0];
                $this->_debug("D: total numsubordinates = " . $vlv_count);
        switch ($level) {
        case LOG_DEBUG:
        case LOG_INFO:
        case LOG_NOTICE:
            if ($this->config['debug']) {
                rcube::write_log('ldap', $msg);
            }
            // ...or by fetching all records dn and count them
            else if (!function_exists('ldap_parse_virtuallist_control')) {
                $vlv_count = $this->search($base_dn, $filter, $scope, array('dn'), $prop, true);
            }
            break;
            $this->vlv_active = $this->_vlv_set_controls($vlv_sort, $this->list_page, $this->page_size, $prop['search']);
        case LOG_EMERGE:
        case LOG_ALERT:
        case LOG_CRIT:
            rcube::raise_error($msg, true, true);
            break;
        case LOG_ERR:
        case LOG_WARNING:
            rcube::raise_error($msg, true, false);
            break;
        }
        else {
            $this->vlv_active = false;
        }
        // only fetch dn for count (should keep the payload low)
        if ($ldap_result = @$function($this->conn, $base_dn, $filter,
            $attrs, 0, (int)$this->config['sizelimit'], (int)$this->config['timelimit'])
        ) {
            // when running on a patched PHP we can use the extended functions
            // to retrieve the total count from the LDAP search result
            if ($this->vlv_active && function_exists('ldap_parse_virtuallist_control')) {
                if (ldap_parse_result($this->conn, $ldap_result, $errcode, $matcheddn, $errmsg, $referrals, $serverctrls)) {
                    ldap_parse_virtuallist_control($this->conn, $serverctrls, $last_offset, $vlv_count, $vresult);
                    $this->_debug("S: VLV result: last_offset=$last_offset; content_count=$vlv_count");
                }
                else {
                    $this->_debug("S: ".($errmsg ? $errmsg : ldap_error($this->conn)));
                }
            }
            else if ($this->debug) {
                $this->_debug("S: ".ldap_count_entries($this->conn, $ldap_result)." record(s) found");
            }
            $this->result = new rcube_ldap_result($this->conn, $ldap_result, $base_dn, $filter, $vlv_count);
            return $count_only ? $this->result->count() : $this->result;
        }
        else {
            $this->_debug("S: ".ldap_error($this->conn));
        }
        return false;
    }
    /**
     * Modify an LDAP entry on the server
     *
     * @param string $dn      Entry DN
     * @param array  $params  Hash array of entry attributes
     * @param int    $mode    Update mode (UPDATE_MOD_ADD | UPDATE_MOD_DELETE | UPDATE_MOD_REPLACE)
     * @deprecated
     */
    public function modify($dn, $parms, $mode = 255)
    public function set_debug($dbg = true)
    {
        // TODO: implement this
        return false;
        $this->config['debug'] = (bool) $dbg;
    }
    /**
     * Wrapper for ldap_add()
     *
     * @see ldap_add()
     * @deprecated
     */
    public function set_cache($cache_engine)
    {
        $this->config['cache'] = $cache_engine;
    }
    /**
     * @deprecated
     */
    public static function scope2func($scope, &$ns_function = null)
    {
        return self::scope_to_function($scope, $ns_function);
    }
    /**
     * @deprecated
     */
    public function set_config($opt, $val = null)
    {
        $this->config_set($opt, $val);
    }
    /**
     * @deprecated
     */
    public function add($dn, $entry)
    {
        $this->_debug("C: Add $dn: ".print_r($entry, true));
        $res = ldap_add($this->conn, $dn, $entry);
        if ($res === false) {
            $this->_debug("S: ".ldap_error($this->conn));
            return false;
        }
        $this->_debug("S: OK");
        return true;
        return $this->add_entry($dn, $entry);
    }
    /**
     * Wrapper for ldap_delete()
     *
     * @see ldap_delete()
     * @deprecated
     */
    public function delete($dn)
    {
        $this->_debug("C: Delete $dn");
        $res = ldap_delete($this->conn, $dn);
        if ($res === false) {
            $this->_debug("S: ".ldap_error($this->conn));
            return false;
        }
        $this->_debug("S: OK");
        return true;
        return $this->delete_entry($dn);
    }
    /**
@@ -595,9 +255,6 @@
        $this->_debug("C: Read $dn [{$filter}]");
        if ($this->conn && $dn) {
            if (!$attributes)
                $attributes = $this->attributes;
            $result = @ldap_read($this->conn, $dn, $filter, $attributes, 0, (int)$this->config['sizelimit'], (int)$this->config['timelimit']);
            if ($result === false) {
                $this->_debug("S: ".ldap_error($this->conn));
@@ -610,452 +267,7 @@
        return false;
    }
    /**
     * Choose the right PHP function according to scope property
     *
     * @param string $scope         The LDAP scope (sub|base|list)
     * @param string $ns_function   Function to be used for numSubOrdinates queries
     * @return string  PHP function to be used to query directory
     */
    public static function scope2func($scope, &$ns_function = null)
    {
        switch ($scope) {
          case 'sub':
            $function = $ns_function  = 'ldap_search';
            break;
          case 'base':
            $function = $ns_function = 'ldap_read';
            break;
          default:
            $function = 'ldap_list';
            $ns_function = 'ldap_read';
            break;
        }
        return $function;
    }
    /**
     * Convert the given scope integer value to a string representation
     */
    public static function scopeint2str($scope)
    {
        switch ($scope) {
            case 2:  return 'sub';
            case 1:  return 'one';
            case 0:  return 'base';
            default: $this->_debug("Scope $scope is not a valid scope integer");
        }
        return '';
    }
    /**
     * Escapes the given value according to RFC 2254 so that it can be safely used in LDAP filters.
     *
     * @param string $val Value to quote
     * @return string The escaped value
     */
    public static function escape_value($val)
    {
        return strtr($str, array('*'=>'\2a', '('=>'\28', ')'=>'\29',
            '\\'=>'\5c', '/'=>'\2f'));
    }
    /**
     * Escapes a DN value according to RFC 2253
     *
     * @param string $dn DN value o quote
     * @return string The escaped value
     */
    public static function escape_dn($dn)
    {
        return strtr($str, array(','=>'\2c', '='=>'\3d', '+'=>'\2b',
            '<'=>'\3c', '>'=>'\3e', ';'=>'\3b', '\\'=>'\5c',
            '"'=>'\22', '#'=>'\23'));
    }
    /**
     * Normalize a LDAP result by converting entry attributes arrays into single values
     *
     * @param array $result LDAP result set fetched with ldap_get_entries()
     * @return array        Hash array with normalized entries, indexed by their DNs
     */
    public static function normalize_result($result)
    {
        if (!is_array($result)) {
            return array();
        }
        $entries  = array();
        for ($i = 0; $i < $result['count']; $i++) {
            $key = $result[$i]['dn'] ? $result[$i]['dn'] : $i;
            $entries[$key] = self::normalize_entry($result[$i]);
        }
        return $entries;
    }
    /**
     * Turn an LDAP entry into a regular PHP array with attributes as keys.
     *
     * @param array $entry Attributes array as retrieved from ldap_get_attributes() or ldap_get_entries()
     *
     * @return array       Hash array with attributes as keys
     */
    public static function normalize_entry($entry)
    {
        if (!isset($entry['count'])) {
            return $entry;
        }
        $rec = array();
        for ($i=0; $i < $entry['count']; $i++) {
            $attr = $entry[$i];
            if ($entry[$attr]['count'] == 1) {
                switch ($attr) {
                    case 'objectclass':
                        $rec[$attr] = array(strtolower($entry[$attr][0]));
                        break;
                    default:
                        $rec[$attr] = $entry[$attr][0];
                        break;
                }
            }
            else {
                for ($j=0; $j < $entry[$attr]['count']; $j++) {
                    $rec[$attr][$j] = $entry[$attr][$j];
                }
            }
        }
        return $rec;
    }
    /**
     * Set server controls for Virtual List View (paginated listing)
     */
    private function _vlv_set_controls($sort, $list_page, $page_size, $search = null)
    {
        $sort_ctrl = array('oid' => "1.2.840.113556.1.4.473",  'value' => self::_sort_ber_encode((array)$sort));
        $vlv_ctrl  = array('oid' => "2.16.840.1.113730.3.4.9", 'value' => self::_vlv_ber_encode(($offset = ($list_page-1) * $page_size + 1), $page_size, $search), 'iscritical' => true);
        $this->_debug("C: Set controls sort=" . join(' ', unpack('H'.(strlen($sort_ctrl['value'])*2), $sort_ctrl['value'])) . " ($sort[0]);"
            . " vlv=" . join(' ', (unpack('H'.(strlen($vlv_ctrl['value'])*2), $vlv_ctrl['value']))) . " ($offset/$page_size; $search)");
        if (!ldap_set_option($this->conn, LDAP_OPT_SERVER_CONTROLS, array($sort_ctrl, $vlv_ctrl))) {
            $this->_debug("S: ".ldap_error($this->conn));
            $this->set_error(self::ERROR_SEARCH, 'vlvnotsupported');
            return false;
        }
        return true;
    }
    /**
     * Returns unified attribute name (resolving aliases)
     */
    private static function _attr_name($namev)
    {
        // list of known attribute aliases
        static $aliases = array(
            'gn' => 'givenname',
            'rfc822mailbox' => 'email',
            'userid' => 'uid',
            'emailaddress' => 'email',
            'pkcs9email' => 'email',
        );
        list($name, $limit) = explode(':', $namev, 2);
        $suffix = $limit ? ':'.$limit : '';
        return (isset($aliases[$name]) ? $aliases[$name] : $name) . $suffix;
    }
    /**
     * Quotes attribute value string
     *
     * @param string $str Attribute value
     * @param bool   $dn  True if the attribute is a DN
     *
     * @return string Quoted string
     */
    public static function quote_string($str, $dn=false)
    {
        // take firt entry if array given
        if (is_array($str))
            $str = reset($str);
        if ($dn)
            $replace = array(','=>'\2c', '='=>'\3d', '+'=>'\2b', '<'=>'\3c',
                '>'=>'\3e', ';'=>'\3b', '\\'=>'\5c', '"'=>'\22', '#'=>'\23');
        else
            $replace = array('*'=>'\2a', '('=>'\28', ')'=>'\29', '\\'=>'\5c',
                '/'=>'\2f');
        return strtr($str, $replace);
    }
    /**
     * Prints debug info to the log
     */
    private function _debug($str)
    {
        if ($this->debug && class_exists('rcube')) {
            rcube::write_log('ldap', $str);
        }
    }
    /*****************  Virtual List View (VLV) related utility functions  **************** */
    /**
     * Return the search string value to be used in VLV controls
     */
    private function _vlv_search($sort, $search)
    {
        foreach ($search as $attr => $value) {
            if (!in_array(strtolower($attr), $sort)) {
                $this->_debug("d: Cannot use VLV search using attribute not indexed: $attr (not in " . var_export($sort, true) . ")");
                return null;
            } else {
                return $value;
            }
        }
    }
    /**
     * Find a VLV index matching the given query attributes
     *
     * @return string Sort attribute or False if no match
     */
    private function _find_vlv($base_dn, $filter, $scope, $sort_attrs = null)
    {
        if (!$this->config['vlv'] || $scope == 'base') {
            return false;
        }
        // get vlv config
        $vlv_config = $this->_read_vlv_config();
        if ($vlv = $vlv_config[$base_dn]) {
            $this->_debug("D: Found a VLV for $base_dn");
            if ($vlv['filter'] == strtolower($filter) || stripos($filter, '(&'.$vlv['filter'].'(') === 0) {
                $this->_debug("D: Filter matches");
                if ($vlv['scope'] == $scope) {
                    // Not passing any sort attributes means you don't care
                    if (empty($sort_attrs) || in_array($sort_attrs, $vlv['sort'])) {
                        return $vlv['sort'][0];
                    }
                }
                else {
                    $this->_debug("D: Scope does not match");
                }
            }
            else {
                $this->_debug("D: Filter does not match");
            }
        }
        else {
            $this->_debug("D: No VLV for $base_dn");
        }
        return false;
    }
    /**
     * Return VLV indexes and searches including necessary configuration
     * details.
     */
    private function _read_vlv_config()
    {
        if (empty($this->config['vlv']) || empty($this->config['config_root_dn'])) {
            return array();
        }
        // return hard-coded VLV config
        else if (is_array($this->config['vlv'])) {
            return $this->config['vlv'];
        }
        // return cached result
        if (is_array($this->vlv_config)) {
            return $this->vlv_config;
        }
        if ($this->cache && ($cached_config = $this->cache->get('vlvconfig'))) {
            $this->vlv_config = $cached_config;
            return $this->vlv_config;
        }
        $this->vlv_config = array();
        $config_root_dn = $this->config['config_root_dn'];
        $ldap_result = ldap_search($this->conn, $config_root_dn, '(objectclass=vlvsearch)', array('*'), 0, 0, 0);
        $vlv_searches = new rcube_ldap_result($this->conn, $ldap_result, $config_root_dn, '(objectclass=vlvsearch)');
        if ($vlv_searches->count() < 1) {
            $this->_debug("D: Empty result from search for '(objectclass=vlvsearch)' on '$config_root_dn'");
            return array();
        }
        foreach ($vlv_searches->entries(true) as $vlv_search_dn => $vlv_search_attrs) {
            // Multiple indexes may exist
            $ldap_result = ldap_search($this->conn, $vlv_search_dn, '(objectclass=vlvindex)', array('*'), 0, 0, 0);
            $vlv_indexes = new rcube_ldap_result($this->conn, $ldap_result, $vlv_search_dn, '(objectclass=vlvindex)');
            // Reset this one for each VLV search.
            $_vlv_sort = array();
            foreach ($vlv_indexes->entries(true) as $vlv_index_dn => $vlv_index_attrs) {
                $_vlv_sort[] = explode(' ', $vlv_index_attrs['vlvsort']);
            }
            $this->vlv_config[$vlv_search_attrs['vlvbase']] = array(
                'scope'  => self::scopeint2str($vlv_search_attrs['vlvscope']),
                'filter' => strtolower($vlv_search_attrs['vlvfilter']),
                'sort'   => $_vlv_sort,
            );
        }
        // cache this
        if ($this->cache)
            $this->cache->set('vlvconfig', $this->vlv_config);
        $this->_debug("D: Refreshed VLV config: " . var_export($this->vlv_config, true));
        return $this->vlv_config;
    }
    /**
     * Generate BER encoded string for Virtual List View option
     *
     * @param integer List offset (first record)
     * @param integer Records per page
     *
     * @return string BER encoded option value
     */
    private static function _vlv_ber_encode($offset, $rpp, $search = '')
    {
        /*
            this string is ber-encoded, php will prefix this value with:
            04 (octet string) and 10 (length of 16 bytes)
            the code behind this string is broken down as follows:
            30 = ber sequence with a length of 0e (14) bytes following
            02 = type integer (in two's complement form) with 2 bytes following (beforeCount): 01 00 (ie 0)
            02 = type integer (in two's complement form) with 2 bytes following (afterCount):  01 18 (ie 25-1=24)
            a0 = type context-specific/constructed with a length of 06 (6) bytes following
            02 = type integer with 2 bytes following (offset): 01 01 (ie 1)
            02 = type integer with 2 bytes following (contentCount):  01 00
            with a search string present:
            81 = type context-specific/constructed with a length of 04 (4) bytes following (the length will change here)
            81 indicates a user string is present where as a a0 indicates just a offset search
            81 = type context-specific/constructed with a length of 06 (6) bytes following
            The following info was taken from the ISO/IEC 8825-1:2003 x.690 standard re: the
            encoding of integer values (note: these values are in
            two-complement form so since offset will never be negative bit 8 of the
            leftmost octet should never by set to 1):
            8.3.2: If the contents octets of an integer value encoding consist
            of more than one octet, then the bits of the first octet (rightmost)
            and bit 8 of the second (to the left of first octet) octet:
                a) shall not all be ones; and
                b) shall not all be zero
        */
        if ($search) {
            $search = preg_replace('/[^-[:alpha:] ,.()0-9]+/', '', $search);
            $ber_val = self::_string2hex($search);
            $str = self::_ber_addseq($ber_val, '81');
        }
        else {
            // construct the string from right to left
            $str = "020100"; # contentCount
            $ber_val = self::_ber_encode_int($offset);  // returns encoded integer value in hex format
            // calculate octet length of $ber_val
            $str = self::_ber_addseq($ber_val, '02') . $str;
            // now compute length over $str
            $str = self::_ber_addseq($str, 'a0');
        }
        // now tack on records per page
        $str = "020100" . self::_ber_addseq(self::_ber_encode_int($rpp-1), '02') . $str;
        // now tack on sequence identifier and length
        $str = self::_ber_addseq($str, '30');
        return pack('H'.strlen($str), $str);
    }
    /**
     * create ber encoding for sort control
     *
     * @param array List of cols to sort by
     * @return string BER encoded option value
     */
    private static function _sort_ber_encode($sortcols)
    {
        $str = '';
        foreach (array_reverse((array)$sortcols) as $col) {
            $ber_val = self::_string2hex($col);
            // 30 = ber sequence with a length of octet value
            // 04 = octet string with a length of the ascii value
            $oct = self::_ber_addseq($ber_val, '04');
            $str = self::_ber_addseq($oct, '30') . $str;
        }
        // now tack on sequence identifier and length
        $str = self::_ber_addseq($str, '30');
        return pack('H'.strlen($str), $str);
    }
    /**
     * Add BER sequence with correct length and the given identifier
     */
    private static function _ber_addseq($str, $identifier)
    {
        $len = dechex(strlen($str)/2);
        if (strlen($len) % 2 != 0)
            $len = '0'.$len;
        return $identifier . $len . $str;
    }
    /**
     * Returns BER encoded integer value in hex format
     */
    private static function _ber_encode_int($offset)
    {
        $val = dechex($offset);
        $prefix = '';
        // check if bit 8 of high byte is 1
        if (preg_match('/^[89abcdef]/', $val))
            $prefix = '00';
        if (strlen($val)%2 != 0)
            $prefix .= '0';
        return $prefix . $val;
    }
    /**
     * Returns ascii string encoded in hex
     */
    private static function _string2hex($str)
    {
        $hex = '';
        for ($i=0; $i < strlen($str); $i++) {
            $hex .= dechex(ord($str[$i]));
        }
        return $hex;
    }
}
// for backward compat.
class rcube_ldap_result extends Net_LDAP3_Result {}
program/lib/Roundcube/rcube_ldap_result.php
File was deleted