Horde_Ldap::search PHP Метод

    public function search($base = null, $filter = null, $params = array())
    {
        if (is_null($base)) {
            $base = $this->_config['basedn'];
        }
        if ($base instanceof Horde_Ldap_Entry) {
            /* Fetch DN of entry, making searchbase relative to the entry. */
            $base = $base->dn();
        }
        if (is_null($filter)) {
            $filter = $this->_config['filter'];
        }
        if ($filter instanceof Horde_Ldap_Filter) {
            /* Convert Horde_Ldap_Filter to string representation. */
            $filter = (string) $filter;
        }
        /* Setting search parameters.  */
        $sizelimit = isset($params['sizelimit']) ? $params['sizelimit'] : 0;
        $timelimit = isset($params['timelimit']) ? $params['timelimit'] : 0;
        $attrsonly = isset($params['attrsonly']) ? $params['attrsonly'] : 0;
        $attributes = isset($params['attributes']) ? $params['attributes'] : array();
        /* Ensure $attributes to be an array in case only one attribute name
         * was given as string. */
        if (!is_array($attributes)) {
            $attributes = array($attributes);
        }
        /* Reorganize the $attributes array index keys sometimes there are
         * problems with not consecutive indexes. */
        $attributes = array_values($attributes);
        /* Scoping makes searches faster! */
        $scope = isset($params['scope']) ? $params['scope'] : $this->_config['scope'];
        switch ($scope) {
            case 'one':
                $search_function = 'ldap_list';
                break;
            case 'base':
                $search_function = 'ldap_read';
                break;
            default:
                $search_function = 'ldap_search';
        }
        /* Continue attempting the search operation until we get a success or a
         * definitive failure. */
        while (true) {
            $link = $this->getLink();
            $search = @call_user_func($search_function, $link, $base, $filter, $attributes, $attrsonly, $sizelimit, $timelimit);
            if ($errno = @ldap_errno($link)) {
                $err = $this->errorName($errno);
                if ($err == 'LDAP_NO_SUCH_OBJECT' || $err == 'LDAP_SIZELIMIT_EXCEEDED') {
                    return new Horde_Ldap_Search($search, $this, $attributes);
                }
                if ($err == 'LDAP_FILTER_ERROR') {
                    /* Bad search filter. */
                    throw new Horde_Ldap_Exception(ldap_err2str($errno) . ' ($filter)', $errno);
                }
                if ($err == 'LDAP_OPERATIONS_ERROR' && $this->_config['auto_reconnect']) {
                    $this->_link = false;
                    $this->_reconnect();
                } else {
                    $msg = "\nParameters:\nBase: {$base}\nFilter: {$filter}\nScope: {$scope}";
                    throw new Horde_Ldap_Exception(ldap_err2str($errno) . $msg, $errno);
                }
            } else {
                return new Horde_Ldap_Search($search, $this, $attributes);
            }
        }
    }

Usage Example

Пример #1
0
 /**
  * Retrieves user preferences from the backend.
  *
  * @throws Sam_Exception
  */
 public function retrieve()
 {
     $attrib = Horde_String::lower($this->_params['attribute']);
     try {
         $search = $this->_ldap->search($this->_params['basedn'], Horde_Ldap_Filter::create($this->_params['uid'], 'equals', $this->_user), array('attributes' => array($attrib)));
         $entry = $search->shiftEntry();
         if (!$entry) {
             throw new Sam_Exception(sprintf('LDAP user "%s" not found.', $this->_user));
         }
         foreach ($entry->getValue($attrib, 'all') as $attribute) {
             list($a, $v) = explode(' ', $attribute);
             $ra = $this->_mapOptionToAttribute($a);
             if (is_numeric($v)) {
                 if (strstr($v, '.')) {
                     $newoptions[$ra][] = (double) $v;
                 } else {
                     $newoptions[$ra][] = (int) $v;
                 }
             } else {
                 $newoptions[$ra][] = $v;
             }
         }
     } catch (Horde_Ldap_Exception $e) {
         throw new Sam_Exception($e);
     }
     /* Go through new options and pull single values out of their
      * arrays. */
     foreach ($newoptions as $k => $v) {
         if (count($v) > 1) {
             $this->_options[$k] = $v;
         } else {
             $this->_options[$k] = $v[0];
         }
     }
 }
All Usage Examples Of Horde_Ldap::search