source: trunk/library/Zend/Ldap/Filter/Abstract.php @ 5146

Revision 5146, 4.9 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2305 - Enviando alteracoes, desenvolvidas internamente na Prognus. Library: adicionando arquivos.

Line 
1<?php
2/**
3 * Zend Framework
4 *
5 * LICENSE
6 *
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
14 *
15 * @category   Zend
16 * @package    Zend_Ldap
17 * @subpackage Filter
18 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
20 * @version    $Id: Abstract.php 20096 2010-01-06 02:05:09Z bkarwin $
21 */
22
23/**
24 * Zend_Ldap_Filter_Abstract provides a base implementation for filters.
25 *
26 * @category   Zend
27 * @package    Zend_Ldap
28 * @subpackage Filter
29 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
30 * @license    http://framework.zend.com/license/new-bsd     New BSD License
31 */
32abstract class Zend_Ldap_Filter_Abstract
33{
34    /**
35     * Returns a string representation of the filter.
36     *
37     * @return string
38     */
39    abstract public function toString();
40
41    /**
42     * Returns a string representation of the filter.
43     * @see toString()
44     *
45     * @return string
46     */
47    public function __toString()
48    {
49        return $this->toString();
50    }
51
52    /**
53     * Negates the filter.
54     *
55     * @return Zend_Ldap_Filter_Abstract
56     */
57    public function negate()
58    {
59        /**
60         * Zend_Ldap_Filter_Not
61         */
62        require_once 'Zend/Ldap/Filter/Not.php';
63        return new Zend_Ldap_Filter_Not($this);
64    }
65
66    /**
67     * Creates an 'and' filter.
68     *
69     * @param  Zend_Ldap_Filter_Abstract $filter,...
70     * @return Zend_Ldap_Filter_And
71     */
72    public function addAnd($filter)
73    {
74        /**
75         * Zend_Ldap_Filter_And
76         */
77        require_once 'Zend/Ldap/Filter/And.php';
78        $fa = func_get_args();
79        $args = array_merge(array($this), $fa);
80        return new Zend_Ldap_Filter_And($args);
81    }
82
83    /**
84     * Creates an 'or' filter.
85     *
86     * @param  Zend_Ldap_Filter_Abstract $filter,...
87     * @return Zend_Ldap_Filter_Or
88     */
89    public function addOr($filter)
90    {
91        /**
92         * Zend_Ldap_Filter_Or
93         */
94        require_once 'Zend/Ldap/Filter/Or.php';
95        $fa = func_get_args();
96        $args = array_merge(array($this), $fa);
97        return new Zend_Ldap_Filter_Or($args);
98    }
99
100    /**
101     * Escapes the given VALUES according to RFC 2254 so that they can be safely used in LDAP filters.
102     *
103     * Any control characters with an ACII code < 32 as well as the characters with special meaning in
104     * LDAP filters "*", "(", ")", and "\" (the backslash) are converted into the representation of a
105     * backslash followed by two hex digits representing the hexadecimal value of the character.
106     * @see Net_LDAP2_Util::escape_filter_value() from Benedikt Hallinger <beni@php.net>
107     * @link http://pear.php.net/package/Net_LDAP2
108     * @author Benedikt Hallinger <beni@php.net>
109     *
110     * @param  string|array $values Array of values to escape
111     * @return array Array $values, but escaped
112     */
113    public static function escapeValue($values = array())
114    {
115        /**
116         * @see Zend_Ldap_Converter
117         */
118        require_once 'Zend/Ldap/Converter.php';
119
120        if (!is_array($values)) $values = array($values);
121        foreach ($values as $key => $val) {
122            // Escaping of filter meta characters
123            $val = str_replace(array('\\', '*', '(', ')'), array('\5c', '\2a', '\28', '\29'), $val);
124            // ASCII < 32 escaping
125            $val = Zend_Ldap_Converter::ascToHex32($val);
126            if (null === $val) $val = '\0';  // apply escaped "null" if string is empty
127            $values[$key] = $val;
128        }
129        return (count($values) == 1) ? $values[0] : $values;
130    }
131
132    /**
133     * Undoes the conversion done by {@link escapeValue()}.
134     *
135     * Converts any sequences of a backslash followed by two hex digits into the corresponding character.
136     * @see Net_LDAP2_Util::escape_filter_value() from Benedikt Hallinger <beni@php.net>
137     * @link http://pear.php.net/package/Net_LDAP2
138     * @author Benedikt Hallinger <beni@php.net>
139     *
140     * @param  string|array $values Array of values to escape
141     * @return array Array $values, but unescaped
142     */
143    public static function unescapeValue($values = array())
144    {
145        /**
146         * @see Zend_Ldap_Converter
147         */
148        require_once 'Zend/Ldap/Converter.php';
149
150        if (!is_array($values)) $values = array($values);
151        foreach ($values as $key => $value) {
152            // Translate hex code into ascii
153            $values[$key] = Zend_Ldap_Converter::hex32ToAsc($value);
154        }
155        return (count($values) == 1) ? $values[0] : $values;
156    }
157}
Note: See TracBrowser for help on using the repository browser.