File indexing completed on 2024-06-16 05:30:10

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Ldap
0017  * @subpackage Filter
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * Zend_Ldap_Filter_Abstract provides a base implementation for filters.
0025  *
0026  * @category   Zend
0027  * @package    Zend_Ldap
0028  * @subpackage Filter
0029  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0030  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0031  */
0032 abstract class Zend_Ldap_Filter_Abstract
0033 {
0034     /**
0035      * Returns a string representation of the filter.
0036      *
0037      * @return string
0038      */
0039     abstract public function toString();
0040 
0041     /**
0042      * Returns a string representation of the filter.
0043      * @see toString()
0044      *
0045      * @return string
0046      */
0047     public function __toString()
0048     {
0049         return $this->toString();
0050     }
0051 
0052     /**
0053      * Negates the filter.
0054      *
0055      * @return Zend_Ldap_Filter_Abstract
0056      */
0057     public function negate()
0058     {
0059         /**
0060          * Zend_Ldap_Filter_Not
0061          */
0062         // require_once 'Zend/Ldap/Filter/Not.php';
0063         return new Zend_Ldap_Filter_Not($this);
0064     }
0065 
0066     /**
0067      * Creates an 'and' filter.
0068      *
0069      * @param  Zend_Ldap_Filter_Abstract $filter,...
0070      * @return Zend_Ldap_Filter_And
0071      */
0072     public function addAnd($filter)
0073     {
0074         /**
0075          * Zend_Ldap_Filter_And
0076          */
0077         // require_once 'Zend/Ldap/Filter/And.php';
0078         $fa = func_get_args();
0079         $args = array_merge(array($this), $fa);
0080         return new Zend_Ldap_Filter_And($args);
0081     }
0082 
0083     /**
0084      * Creates an 'or' filter.
0085      *
0086      * @param  Zend_Ldap_Filter_Abstract $filter,...
0087      * @return Zend_Ldap_Filter_Or
0088      */
0089     public function addOr($filter)
0090     {
0091         /**
0092          * Zend_Ldap_Filter_Or
0093          */
0094         // require_once 'Zend/Ldap/Filter/Or.php';
0095         $fa = func_get_args();
0096         $args = array_merge(array($this), $fa);
0097         return new Zend_Ldap_Filter_Or($args);
0098     }
0099 
0100     /**
0101      * Escapes the given VALUES according to RFC 2254 so that they can be safely used in LDAP filters.
0102      *
0103      * Any control characters with an ACII code < 32 as well as the characters with special meaning in
0104      * LDAP filters "*", "(", ")", and "\" (the backslash) are converted into the representation of a
0105      * backslash followed by two hex digits representing the hexadecimal value of the character.
0106      * @see Net_LDAP2_Util::escape_filter_value() from Benedikt Hallinger <beni@php.net>
0107      * @link http://pear.php.net/package/Net_LDAP2
0108      * @author Benedikt Hallinger <beni@php.net>
0109      *
0110      * @param  string|array $values Array of values to escape
0111      * @return array Array $values, but escaped
0112      */
0113     public static function escapeValue($values = array())
0114     {
0115         /**
0116          * @see Zend_Ldap_Converter
0117          */
0118         // require_once 'Zend/Ldap/Converter.php';
0119 
0120         if (!is_array($values)) $values = array($values);
0121         foreach ($values as $key => $val) {
0122             // Escaping of filter meta characters
0123             $val = str_replace(array('\\', '*', '(', ')'), array('\5c', '\2a', '\28', '\29'), $val);
0124             // ASCII < 32 escaping
0125             $val = Zend_Ldap_Converter::ascToHex32($val);
0126             if (null === $val) $val = '\0';  // apply escaped "null" if string is empty
0127             $values[$key] = $val;
0128         }
0129         return (count($values) == 1) ? $values[0] : $values;
0130     }
0131 
0132     /**
0133      * Undoes the conversion done by {@link escapeValue()}.
0134      *
0135      * Converts any sequences of a backslash followed by two hex digits into the corresponding character.
0136      * @see Net_LDAP2_Util::escape_filter_value() from Benedikt Hallinger <beni@php.net>
0137      * @link http://pear.php.net/package/Net_LDAP2
0138      * @author Benedikt Hallinger <beni@php.net>
0139      *
0140      * @param  string|array $values Array of values to escape
0141      * @return array Array $values, but unescaped
0142      */
0143     public static function unescapeValue($values = array())
0144     {
0145         /**
0146          * @see Zend_Ldap_Converter
0147          */
0148         // require_once 'Zend/Ldap/Converter.php';
0149 
0150         if (!is_array($values)) $values = array($values);
0151         foreach ($values as $key => $value) {
0152             // Translate hex code into ascii
0153             $values[$key] = Zend_Ldap_Converter::hex32ToAsc($value);
0154         }
0155         return (count($values) == 1) ? $values[0] : $values;
0156     }
0157 }