<bdo id='HDEsN'></bdo><ul id='HDEsN'></ul>

    1. <small id='HDEsN'></small><noframes id='HDEsN'>

    2. <tfoot id='HDEsN'></tfoot>
      <i id='HDEsN'><tr id='HDEsN'><dt id='HDEsN'><q id='HDEsN'><span id='HDEsN'><b id='HDEsN'><form id='HDEsN'><ins id='HDEsN'></ins><ul id='HDEsN'></ul><sub id='HDEsN'></sub></form><legend id='HDEsN'></legend><bdo id='HDEsN'><pre id='HDEsN'><center id='HDEsN'></center></pre></bdo></b><th id='HDEsN'></th></span></q></dt></tr></i><div id='HDEsN'><tfoot id='HDEsN'></tfoot><dl id='HDEsN'><fieldset id='HDEsN'></fieldset></dl></div>
      <legend id='HDEsN'><style id='HDEsN'><dir id='HDEsN'><q id='HDEsN'></q></dir></style></legend>

      PHP ldap_add 函数以转义 DN 语法中的 ldap 特殊字符

      时间:2024-08-23

          <tbody id='9lspC'></tbody>

          <bdo id='9lspC'></bdo><ul id='9lspC'></ul>
              <i id='9lspC'><tr id='9lspC'><dt id='9lspC'><q id='9lspC'><span id='9lspC'><b id='9lspC'><form id='9lspC'><ins id='9lspC'></ins><ul id='9lspC'></ul><sub id='9lspC'></sub></form><legend id='9lspC'></legend><bdo id='9lspC'><pre id='9lspC'><center id='9lspC'></center></pre></bdo></b><th id='9lspC'></th></span></q></dt></tr></i><div id='9lspC'><tfoot id='9lspC'></tfoot><dl id='9lspC'><fieldset id='9lspC'></fieldset></dl></div>
            1. <tfoot id='9lspC'></tfoot>
            2. <small id='9lspC'></small><noframes id='9lspC'>

                <legend id='9lspC'><style id='9lspC'><dir id='9lspC'><q id='9lspC'></q></dir></style></legend>
                本文介绍了PHP ldap_add 函数以转义 DN 语法中的 ldap 特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试将一些用户添加到我的 Ldap DB,但是当我使用一些特殊字符(如,.")时出现一些错误(无效的 dn 语法).我需要一个转义所有字符的函数.我尝试了 preg_quote,但在某些情况下会出现一些错误.

                I'm trying to add some users to my Ldap DB but I get some errors (invalid dn syntax) when I use some special characters like ",.". I need a function that escape all characters. I try preg_quote but I get some errors in some cases.

                提前致谢

                代码:

                $user = 'Test , Name S.L';
                
                    if(!(ldap_add($ds, "cn=" . $user . ",".LDAP_DN_BASE, $info))) {
                
                            include 'error_new_account.php';
                
                    }
                

                推荐答案

                编辑 2013 年 1 月:添加了对转义 DN 字符串中的前导/尾随空格的支持,每 RFC 4514.感谢 Eugenio 指出这个问题.

                EDIT Jan 2013: added support for escaping leading/trailing spaces in DN strings, per RFC 4514. Thanks to Eugenio for pointing out this issue.

                编辑 2014:我将此功能添加到 PHP 5.6.下面的代码现在是早期 PHP 版本的同类替代品.

                EDIT 2014: I added this function to PHP 5.6. The code below is now a like-for-like drop-in replacement for earlier PHP versions.

                if (!function_exists('ldap_escape')) {
                    define('LDAP_ESCAPE_FILTER', 0x01);
                    define('LDAP_ESCAPE_DN',     0x02);
                
                    /**
                     * @param string $subject The subject string
                     * @param string $ignore Set of characters to leave untouched
                     * @param int $flags Any combination of LDAP_ESCAPE_* flags to indicate the
                     *                   set(s) of characters to escape.
                     * @return string
                     */
                    function ldap_escape($subject, $ignore = '', $flags = 0)
                    {
                        static $charMaps = array(
                            LDAP_ESCAPE_FILTER => array('\', '*', '(', ')', "x00"),
                            LDAP_ESCAPE_DN     => array('\', ',', '=', '+', '<', '>', ';', '"', '#'),
                        );
                
                        // Pre-process the char maps on first call
                        if (!isset($charMaps[0])) {
                            $charMaps[0] = array();
                            for ($i = 0; $i < 256; $i++) {
                                $charMaps[0][chr($i)] = sprintf('\%02x', $i);;
                            }
                
                            for ($i = 0, $l = count($charMaps[LDAP_ESCAPE_FILTER]); $i < $l; $i++) {
                                $chr = $charMaps[LDAP_ESCAPE_FILTER][$i];
                                unset($charMaps[LDAP_ESCAPE_FILTER][$i]);
                                $charMaps[LDAP_ESCAPE_FILTER][$chr] = $charMaps[0][$chr];
                            }
                
                            for ($i = 0, $l = count($charMaps[LDAP_ESCAPE_DN]); $i < $l; $i++) {
                                $chr = $charMaps[LDAP_ESCAPE_DN][$i];
                                unset($charMaps[LDAP_ESCAPE_DN][$i]);
                                $charMaps[LDAP_ESCAPE_DN][$chr] = $charMaps[0][$chr];
                            }
                        }
                
                        // Create the base char map to escape
                        $flags = (int)$flags;
                        $charMap = array();
                        if ($flags & LDAP_ESCAPE_FILTER) {
                            $charMap += $charMaps[LDAP_ESCAPE_FILTER];
                        }
                        if ($flags & LDAP_ESCAPE_DN) {
                            $charMap += $charMaps[LDAP_ESCAPE_DN];
                        }
                        if (!$charMap) {
                            $charMap = $charMaps[0];
                        }
                
                        // Remove any chars to ignore from the list
                        $ignore = (string)$ignore;
                        for ($i = 0, $l = strlen($ignore); $i < $l; $i++) {
                            unset($charMap[$ignore[$i]]);
                        }
                
                        // Do the main replacement
                        $result = strtr($subject, $charMap);
                
                        // Encode leading/trailing spaces if LDAP_ESCAPE_DN is passed
                        if ($flags & LDAP_ESCAPE_DN) {
                            if ($result[0] === ' ') {
                                $result = '\20' . substr($result, 1);
                            }
                            if ($result[strlen($result) - 1] === ' ') {
                                $result = substr($result, 0, -1) . '\20';
                            }
                        }
                
                        return $result;
                    }
                }
                

                所以你会这样做:

                $user = 'Test , Name S.L';
                $cn = ldap_escape($user, '', LDAP_ESCAPE_DN);
                if (!ldap_add($ds, "cn={$cn}," . LDAP_DN_BASE, $info)) {
                    include 'error_new_account.php';
                }
                

                这篇关于PHP ldap_add 函数以转义 DN 语法中的 ldap 特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 Ubuntu 服务器中使用 PHP 7.0 启用 LDAP? 下一篇:LDAP 问题,ldap_bind 无效的 dn 语法

                相关文章

              • <i id='PHdud'><tr id='PHdud'><dt id='PHdud'><q id='PHdud'><span id='PHdud'><b id='PHdud'><form id='PHdud'><ins id='PHdud'></ins><ul id='PHdud'></ul><sub id='PHdud'></sub></form><legend id='PHdud'></legend><bdo id='PHdud'><pre id='PHdud'><center id='PHdud'></center></pre></bdo></b><th id='PHdud'></th></span></q></dt></tr></i><div id='PHdud'><tfoot id='PHdud'></tfoot><dl id='PHdud'><fieldset id='PHdud'></fieldset></dl></div>

                  1. <small id='PHdud'></small><noframes id='PHdud'>

                    • <bdo id='PHdud'></bdo><ul id='PHdud'></ul>
                  2. <tfoot id='PHdud'></tfoot>

                  3. <legend id='PHdud'><style id='PHdud'><dir id='PHdud'><q id='PHdud'></q></dir></style></legend>