Net_LDAP
[ class tree: Net_LDAP ] [ index: Net_LDAP ] [ all elements ]

Source for file Util.php

Documentation is available at Util.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +--------------------------------------------------------------------------+
  4. // | Net_LDAP                                                                 |
  5. // +--------------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                    |
  7. // +--------------------------------------------------------------------------+
  8. // | This library is free software; you can redistribute it and/or            |
  9. // | modify it under the terms of the GNU Lesser General Public               |
  10. // | License as published by the Free Software Foundation; either             |
  11. // | version 2.1 of the License, or (at your option) any later version.       |
  12. // |                                                                          |
  13. // | This library is distributed in the hope that it will be useful,          |
  14. // | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
  15. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        |
  16. // | Lesser General Public License for more details.                          |
  17. // |                                                                          |
  18. // | You should have received a copy of the GNU Lesser General Public         |
  19. // | License along with this library; if not, write to the Free Software      |
  20. // | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA |
  21. // +--------------------------------------------------------------------------+
  22. // | Authors: Benedikt Hallinger                                              |
  23. // +--------------------------------------------------------------------------+
  24. //
  25. // $Id: Util.php,v 1.10 2007/02/26 12:07:44 beni Exp $
  26. require_once "PEAR.php";
  27.  
  28.  
  29. /**
  30.  * Utility Class for Net_LDAP
  31.  *
  32.  * This class servers some functionality to the other classes of Net_LDAP but most of
  33.  * the methods can be used separately as well.
  34.  *
  35.  * @package Net_LDAP
  36.  * @author Benedikt Hallinger <beni@php.net>
  37.  * @version $Revision: 1.10 $
  38.  */
  39. class Net_LDAP_Util extends PEAR
  40. {
  41.     /**
  42.      * Private empty Constructur
  43.      *
  44.      * @access private
  45.      */
  46.     function Net_LDAP_Util()
  47.     {
  48.          // We do nothing here, since all methods can be called statically.
  49.          // In Net_LDAP <= 0.7, we needed a instance of Util, because
  50.          // it was possible to do utf8 encoding and decoding, but this
  51.          // has been moved to the LDAP class.
  52.     }
  53.  
  54.     /**
  55.      * Wrapper function for PHPs ldap_explode_dn()
  56.      *
  57.      * PHPs ldap_explode_dn() does not escape DNs so it will fail
  58.      * if the parameter $dn is something like <kbd>"<foobar>"</kbd> or contains
  59.      * Umlauts.
  60.      * This method ensures, that the DN is properly escaped and encoded.
  61.      *
  62.      * It is taken from http://php.net/ldap_explode_dn and slightly modified.
  63.      *
  64.      * @author DavidSmith@byu.net
  65.      * @param string $dn           The DN that should be split
  66.      * @param string $only_values  Return just values, no attribute names ('foo' instead of 'cn=foo')
  67.      * @static
  68.      * @deprecated This method gets superseeded by ldap_explode_dn(), if it is done
  69.      */
  70.     function ldap_explode_dn_escaped($dn$only_values = 0)
  71.     {
  72.         $dn addcslashes$dn"<>" );
  73.         $result ldap_explode_dn$dn$only_values );
  74.         if (!$result{
  75.             return PEAR::raiseError("Error exploding DN: invalid DN!");
  76.         }
  77.         if (isset($result["count"])) {
  78.             unset($result["count"]);
  79.         }
  80.         //translate hex code into ascii again
  81.         foreach$result as $key => $value )
  82.             $result[$keypreg_replace("/\\\([0-9A-Fa-f]{2})/e""''.chr(hexdec('\\1')).''"$value);
  83.         return $result;
  84.      }
  85.  
  86.     /**
  87.     * Explodes the given DN into its elements
  88.     *
  89.     * {@link http://www.ietf.org/rfc/rfc2253.txt RFC 2253} says, a Distinguished Name is a sequence
  90.     * of Relative Distinguished Names (RDNs), which themselves
  91.     * are sets of Attributes. For each RDN a array is constructed where the RDN part is stored.
  92.     *
  93.     * For example, the DN 'OU=Sales+CN=J. Smith,DC=example,DC=net' is exploded to:
  94.     * <kbd>array( [0] => 'OU=Sales', [1] => 'CN=J. Smith', [2] => 'DC=example', [3] => 'DC=net' )</kbd>
  95.     *
  96.     * [NOT IMPLEMENTED] DNs might also contain values, which are the bytes of the BER encoding of
  97.     * the X.500 AttributeValue rather than some LDAP string syntax. These values are hex-encoded
  98.     * and prefixed with a #. To distinguish such BER values, ldap_explode_dn uses references to
  99.     * the actual values, e.g. '1.3.6.1.4.1.1466.0=#04024869,DC=example,DC=com' is exploded to:
  100.     * [ { '1.3.6.1.4.1.1466.0' => "\004\002Hi" }, { 'DC' => 'example' }, { 'DC' => 'com' } ];
  101.     *
  102.     * [NOT IMPLEMENTED] It also performs the following operations on the given DN:
  103.     *   - Unescape "\" followed by ",", "+", """, "\", "<", ">", ";", "#", "=", " ", or a hexpair
  104.     *     and and strings beginning with "#".
  105.     *   - Removes the leading 'OID.' characters if the type is an OID instead of a name.
  106.     *
  107.     * OPTIONS is a list of name/value pairs, valid options are:
  108.     *   casefold    Controls case folding of attribute types names.
  109.     *               Attribute values are not affected by this option.
  110.     *               The default is to uppercase. Valid values are:
  111.     *               lower        Lowercase attribute types names.
  112.     *               upper        Uppercase attribute type names. This is the default.
  113.     *               none         Do not change attribute type names.
  114.     *   reverse     If TRUE, the RDN sequence is reversed.
  115.     *   onlyvalues  If TRUE, then only attributes values are returned ('foo' instead of 'cn=foo')
  116.     *
  117.     * @todo Escaping stuff needs to be implemented completely: http://www.ietf.org/rfc/rfc2253.txt
  118.     * @todo OID stuff needs to be implemented
  119.     * @todo The return value is currently php-like. Maybe we should return exactly the structure perl would return
  120.     * @todo ldap_explode_dn does not (un)escape anything thus we should maybe code our own exploding mechanism. We could use unescape_dn_value() to do that
  121.     * @static
  122.     * @author beni@php.net, based on work from DavidSmith@byu.net
  123.     * @param string $dn      The escaped DN that should be exploded
  124.     * @param array  $options  Options to use
  125.     * @return array    Parts of the exploded DN
  126.     */
  127.     function ldap_explode_dn($dn$options = array('casefold' => 'upper'))
  128.     {
  129.         $options['onlyvalues'== true ? $options['onlyvalues'= 1 : $options['onlyvalues'= 0;
  130.         !isset($options['reverse']$options['reverse'= false : $options['reverse'= true;
  131.         if (!isset($options['casefold'])) $options['casefold''upper';
  132.  
  133.         // Escaping of DN
  134.         $dn addcslashes$dn"<>" );
  135.         $dn_array ldap_explode_dn$dn$options['onlyvalues');
  136.         if (!$dn_array{
  137.             return PEAR::raiseError("Error exploding DN: invalid DN!");
  138.         }
  139.         if (isset($dn_array["count"])) {
  140.             unset($dn_array["count"]);
  141.         }
  142.  
  143.         //translate hex code into ascii again and apply case folding
  144.         foreach $dn_array as $key => $value {
  145.             $value preg_replace("/\\\([0-9A-Fa-f]{2})/e""''.chr(hexdec('\\1')).''"$value);
  146.             if ($options['casefold'== 'upper'$value preg_replace("/^(\w+)=/e""''.strtoupper('\\1').''"$value);
  147.             if ($options['casefold'== 'lower'$value preg_replace("/^(\w+)=/e""''.strtolower('\\1').''"$value);
  148.         }
  149.  
  150.         if ($options['reverse']{
  151.             return array_reverse($dn_array);
  152.         else {
  153.             return $dn_array;
  154.         }
  155.     }
  156.  
  157.     /**
  158.     * escape_dn_value ( VALUES )
  159.     *
  160.     * Comment taken from CPAN:
  161.     * Escapes the given VALUES according to RFC 2253 so that they can be safely used in LDAP DNs.
  162.     * The characters ",", "+", """, "\", "<", ">", ";", "#", "=" with a special meaning in RFC 2252
  163.     * are preceeded by ba backslash. Control characters with an ASCII code < 32 are represented as \hexpair.
  164.     * Finally all leading and trailing spaces are converted to sequences of \20.
  165.     *
  166.     * Returns the converted list in list mode and the first element in scalar mode.
  167.     *
  168.     * @todo implement me!
  169.     * @static
  170.     * @param array $values    A array containing the DN values that should be escaped
  171.     * @return array           The array $values, but escaped
  172.     */
  173.     function escape_dn_value($values = array())
  174.     {
  175.         PEAR::raiseError("Not implemented!");
  176.     }
  177.  
  178.     /**
  179.     * Undoes the conversion done by escape_dn_value().
  180.     *
  181.     * Any escape sequence starting with a baskslash - hexpair or special character -
  182.     * will be transformed back to the corresponding character.
  183.     *
  184.     * Returns the converted list in list mode and the first element in scalar mode.
  185.     *
  186.     * @todo implement me!
  187.     * @param array $values    Array of DN Values
  188.     * @return array           Same as $values, but unescaped
  189.     * @static
  190.     */
  191.     function unescape_dn_value($values = array())
  192.     {
  193.         PEAR::raiseError("Not implemented!");
  194.     }
  195.  
  196.     /**
  197.     * Returns the given DN in a canonical form
  198.     *
  199.     * Returns undef if DN is not a valid Distinguished Name.
  200.     * Note: The empty string "" is a valid DN.) DN can either be a string or reference to an array of
  201.     * hashes as returned by ldap_explode_dn, which is useful when constructing a DN.
  202.     *
  203.     * It performs the following operations on the given DN:
  204.     *     - Removes the leading 'OID.' characters if the type is an OID instead of a name.
  205.     *     - Escapes all RFC 2253 special characters (",", "+", """, "\", "<", ">", ";", "#", "=", " "), slashes ("/"), and any other character where the ASCII code is < 32 as \hexpair.
  206.     *     - Converts all leading and trailing spaces in values to be \20.
  207.     *     - If an RDN contains multiple parts, the parts are re-ordered so that the attribute type names are in alphabetical order.
  208.     *
  209.     * OPTIONS is a list of name/value pairs, valid options are:
  210.     *     casefold    Controls case folding of attribute type names.
  211.     *                 Attribute values are not affected by this option. The default is to uppercase.
  212.     *                 Valid values are:
  213.     *                 lower        Lowercase attribute type names.
  214.     *                 upper        Uppercase attribute type names. This is the default.
  215.     *                 none         Do not change attribute type names.
  216.     *     mbcescape   If TRUE, characters that are encoded as a multi-octet UTF-8 sequence will be escaped as \(hexpair){2,*}.
  217.     *     reverse     If TRUE, the RDN sequence is reversed.
  218.     *     separator   Separator to use between RDNs. Defaults to comma (',').
  219.     *
  220.     * @todo implement me!
  221.     * @static
  222.     * @param string $dn      The DN
  223.     * @param array  $option  Options to use
  224.     * @return string    The canonical DN
  225.     */
  226.     function canonical_dn($dn$options = array('casefold' => 'upper'))
  227.     {
  228.         PEAR::raiseError("Not implemented!");
  229.     }
  230.  
  231.     /**
  232.     * Escapes the given VALUES according to RFC 2254 so that they can be safely used in LDAP filters.
  233.     *
  234.     * Any control characters with an ACII code < 32 as well as the characters with special meaning in
  235.     * LDAP filters "*", "(", ")", and "\" (the backslash) are converted into the representation of a
  236.     * backslash followed by two hex digits representing the hexadecimal value of the character.
  237.     *
  238.     * @todo NULL escaping seems to never apply
  239.     * @todo The "ASCII escaping" Part needs some work
  240.     * @static
  241.     * @param array $values    Array of values to escape
  242.     * @return array           Array $values, but escaped
  243.     */
  244.     function escape_filter_value($values = array())
  245.     {
  246.         $escaped = array();
  247.         foreach ($values as $val{
  248.             // ASCII < 32 escaping
  249.             // [TODO]
  250.  
  251.             // Escaping of meta characters
  252.             $val str_replace('*''\0x2a'$val);
  253.             $val str_replace('(''\0x28'$val);
  254.             $val str_replace(')''\0x29'$val);
  255.             $val str_replace('\\''\0x5c'$val);
  256.             $val str_replace(null'\0x2a'$val)// null escaping seems to never apply. This probably needs some work!
  257.  
  258.             if ($val === null$val '\0x2a';  // apply escaped "null" if string is empty
  259.  
  260.             array_push($escaped$val);
  261.         }
  262.  
  263.         return $escaped;
  264.     }
  265.  
  266.     /**
  267.     * Undoes the conversion done by {@link escape_filter_value()}.
  268.     *
  269.     * Converts any sequences of a backslash followed by two hex digits into the corresponding character.
  270.     *
  271.     * Returns the converted list in list mode and the first element in scalar mode.
  272.     *
  273.     * @todo implement me!
  274.     * @static
  275.     * @param array $values    Array of values to escape
  276.     * @return array           Array $values, but unescaped
  277.     */
  278.     function unescape_filter_value($values = array())
  279.     {
  280.         PEAR::raiseError("Not implemented!");
  281.     }
  282.  
  283. }
  284.  
  285. ?>

Documentation generated on Mon, 11 Mar 2019 15:01:40 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.