Projekt

Obecné

Profil

Stáhnout (3.79 KB) Statistiky
| Větev: | Revize:
1 cb15593b Cajova-Houba
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symfony\Polyfill\Php56;
13
14
use Symfony\Polyfill\Util\Binary;
15
16
/**
17
 * @internal
18
 */
19
final class Php56
20
{
21
    const LDAP_ESCAPE_FILTER = 1;
22
    const LDAP_ESCAPE_DN = 2;
23
24
    public static function hash_equals($knownString, $userInput)
25
    {
26
        if (!is_string($knownString)) {
27
            trigger_error('Expected known_string to be a string, '.gettype($knownString).' given', E_USER_WARNING);
28
29
            return false;
30
        }
31
32
        if (!is_string($userInput)) {
33
            trigger_error('Expected user_input to be a string, '.gettype($userInput).' given', E_USER_WARNING);
34
35
            return false;
36
        }
37
38
        $knownLen = Binary::strlen($knownString);
39
        $userLen = Binary::strlen($userInput);
40
41
        if ($knownLen !== $userLen) {
42
            return false;
43
        }
44
45
        $result = 0;
46
47
        for ($i = 0; $i < $knownLen; ++$i) {
48
            $result |= ord($knownString[$i]) ^ ord($userInput[$i]);
49
        }
50
51
        return 0 === $result;
52
    }
53
54
    /**
55
     * Stub implementation of the {@link ldap_escape()} function of the ldap
56
     * extension.
57
     *
58
     * Escape strings for safe use in LDAP filters and DNs.
59
     *
60
     * @author Chris Wright <ldapi@daverandom.com>
61
     *
62
     * @param string $subject
63
     * @param string $ignore
64
     * @param int    $flags
65
     *
66
     * @return string
67
     *
68
     * @see http://stackoverflow.com/a/8561604
69
     */
70
    public static function ldap_escape($subject, $ignore = '', $flags = 0)
71
    {
72
        static $charMaps = null;
73
74
        if (null === $charMaps) {
75
            $charMaps = array(
76
                self::LDAP_ESCAPE_FILTER => array('\\', '*', '(', ')', "\x00"),
77
                self::LDAP_ESCAPE_DN => array('\\', ',', '=', '+', '<', '>', ';', '"', '#', "\r"),
78
            );
79
80
            $charMaps[0] = array();
81
82
            for ($i = 0; $i < 256; ++$i) {
83
                $charMaps[0][chr($i)] = sprintf('\\%02x', $i);
84
            }
85
86
            for ($i = 0, $l = count($charMaps[self::LDAP_ESCAPE_FILTER]); $i < $l; ++$i) {
87
                $chr = $charMaps[self::LDAP_ESCAPE_FILTER][$i];
88
                unset($charMaps[self::LDAP_ESCAPE_FILTER][$i]);
89
                $charMaps[self::LDAP_ESCAPE_FILTER][$chr] = $charMaps[0][$chr];
90
            }
91
92
            for ($i = 0, $l = count($charMaps[self::LDAP_ESCAPE_DN]); $i < $l; ++$i) {
93
                $chr = $charMaps[self::LDAP_ESCAPE_DN][$i];
94
                unset($charMaps[self::LDAP_ESCAPE_DN][$i]);
95
                $charMaps[self::LDAP_ESCAPE_DN][$chr] = $charMaps[0][$chr];
96
            }
97
        }
98
99
        // Create the base char map to escape
100
        $flags = (int) $flags;
101
        $charMap = array();
102
103
        if ($flags & self::LDAP_ESCAPE_FILTER) {
104
            $charMap += $charMaps[self::LDAP_ESCAPE_FILTER];
105
        }
106
107
        if ($flags & self::LDAP_ESCAPE_DN) {
108
            $charMap += $charMaps[self::LDAP_ESCAPE_DN];
109
        }
110
111
        if (!$charMap) {
112
            $charMap = $charMaps[0];
113
        }
114
115
        // Remove any chars to ignore from the list
116
        $ignore = (string) $ignore;
117
118
        for ($i = 0, $l = strlen($ignore); $i < $l; ++$i) {
119
            unset($charMap[$ignore[$i]]);
120
        }
121
122
        // Do the main replacement
123
        $result = strtr($subject, $charMap);
124
125
        // Encode leading/trailing spaces if self::LDAP_ESCAPE_DN is passed
126
        if ($flags & self::LDAP_ESCAPE_DN) {
127
            if ($result[0] === ' ') {
128
                $result = '\\20'.substr($result, 1);
129
            }
130
131
            if ($result[strlen($result) - 1] === ' ') {
132
                $result = substr($result, 0, -1).'\\20';
133
            }
134
        }
135
136
        return $result;
137
    }
138
}