Php56.php 3.8 KB

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