Converter.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Ldap
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * Zend_Ldap_Converter is a collection of useful LDAP related conversion functions.
  23. *
  24. * @category Zend
  25. * @package Zend_Ldap
  26. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Ldap_Converter
  30. {
  31. /**
  32. * Converts all ASCII chars < 32 to "\HEX"
  33. *
  34. * @see Net_LDAP2_Util::asc2hex32() from Benedikt Hallinger <beni@php.net>
  35. * @link http://pear.php.net/package/Net_LDAP2
  36. * @author Benedikt Hallinger <beni@php.net>
  37. *
  38. * @param string $string String to convert
  39. * @return string
  40. */
  41. public static function ascToHex32($string)
  42. {
  43. for ($i = 0; $i<strlen($string); $i++) {
  44. $char = substr($string, $i, 1);
  45. if (ord($char)<32) {
  46. $hex = dechex(ord($char));
  47. if (strlen($hex) == 1) $hex = '0' . $hex;
  48. $string = str_replace($char, '\\' . $hex, $string);
  49. }
  50. }
  51. return $string;
  52. }
  53. /**
  54. * Converts all Hex expressions ("\HEX") to their original ASCII characters
  55. *
  56. * @see Net_LDAP2_Util::hex2asc() from Benedikt Hallinger <beni@php.net>,
  57. * heavily based on work from DavidSmith@byu.net
  58. * @link http://pear.php.net/package/Net_LDAP2
  59. * @author Benedikt Hallinger <beni@php.net>, heavily based on work from DavidSmith@byu.net
  60. *
  61. * @param string $string String to convert
  62. * @return string
  63. */
  64. public static function hex32ToAsc($string)
  65. {
  66. $string = preg_replace("/\\\([0-9A-Fa-f]{2})/e", "''.chr(hexdec('\\1')).''", $string);
  67. return $string;
  68. }
  69. }