web/lib/Zend/Ldap/Converter.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    12  * obtain it through the world-wide-web, please send an email
    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.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Ldap
    16  * @package    Zend_Ldap
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    17  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @version    $Id: Converter.php 22996 2010-09-22 17:01:46Z sgehrig $
    19  * @version    $Id: Converter.php 24593 2012-01-05 20:35:02Z matthew $
    20  */
    20  */
    21 
    21 
    22 /**
    22 /**
    23  * Zend_Ldap_Converter is a collection of useful LDAP related conversion functions.
    23  * Zend_Ldap_Converter is a collection of useful LDAP related conversion functions.
    24  *
    24  *
    25  * @category   Zend
    25  * @category   Zend
    26  * @package    Zend_Ldap
    26  * @package    Zend_Ldap
    27  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    27  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    28  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    28  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    29  */
    29  */
    30 class Zend_Ldap_Converter
    30 class Zend_Ldap_Converter
    31 {
    31 {
    32     const STANDARD         = 0;
    32     const STANDARD         = 0;
    71     {
    71     {
    72         $string = preg_replace("/\\\([0-9A-Fa-f]{2})/e", "''.chr(hexdec('\\1')).''", $string);
    72         $string = preg_replace("/\\\([0-9A-Fa-f]{2})/e", "''.chr(hexdec('\\1')).''", $string);
    73         return $string;
    73         return $string;
    74     }
    74     }
    75 
    75 
    76 	/**
    76     /**
    77      * Convert any value to an LDAP-compatible value.
    77      * Convert any value to an LDAP-compatible value.
    78      *
    78      *
    79      * By setting the <var>$type</var>-parameter the conversion of a certain
    79      * By setting the <var>$type</var>-parameter the conversion of a certain
    80      * type can be forced
    80      * type can be forced
    81      *
    81      *
    82      * @todo write more tests
    82      * @todo write more tests
    83      *
    83      *
    84      * @param	mixed 	$value 	The value to convert
    84      * @param    mixed     $value     The value to convert
    85      * @param	int   	$ytpe  	The conversion type to use
    85      * @param    int       $ytpe      The conversion type to use
    86      * @return	string
    86      * @return    string
    87      * @throws	Zend_Ldap_Converter_Exception
    87      * @throws    Zend_Ldap_Converter_Exception
    88      */
    88      */
    89     public static function toLdap($value, $type = self::STANDARD)
    89     public static function toLdap($value, $type = self::STANDARD)
    90     {
    90     {
    91         try {
    91         try {
    92             switch ($type) {
    92             switch ($type) {
   130      *
   130      *
   131      * The date-entity <var>$date</var> can be either a timestamp, a
   131      * The date-entity <var>$date</var> can be either a timestamp, a
   132      * DateTime Object, a string that is parseable by strtotime() or a Zend_Date
   132      * DateTime Object, a string that is parseable by strtotime() or a Zend_Date
   133      * Object.
   133      * Object.
   134      *
   134      *
   135      * @param	integer|string|DateTimt|Zend_Date		$date	The date-entity
   135      * @param    integer|string|DateTimt|Zend_Date        $date    The date-entity
   136      * @param	boolean									$asUtc	Whether to return the LDAP-compatible date-string
   136      * @param    boolean                                    $asUtc    Whether to return the LDAP-compatible date-string
   137      *                          								as UTC or as local value
   137      *                                                          as UTC or as local value
   138      * @return	string
   138      * @return    string
   139      * @throws	InvalidArgumentException
   139      * @throws    InvalidArgumentException
   140      */
   140      */
   141     public static function toLdapDateTime($date, $asUtc = true)
   141     public static function toLdapDateTime($date, $asUtc = true)
   142     {
   142     {
   143         if (!($date instanceof DateTime)) {
   143         if (!($date instanceof DateTime)) {
   144             if (is_int($date)) {
   144             if (is_int($date)) {
   168      *
   168      *
   169      * This converts a boolean value of TRUE, an integer-value of 1 and a
   169      * This converts a boolean value of TRUE, an integer-value of 1 and a
   170      * case-insensitive string 'true' to an LDAP-compatible 'TRUE'. All other
   170      * case-insensitive string 'true' to an LDAP-compatible 'TRUE'. All other
   171      * other values are converted to an LDAP-compatible 'FALSE'.
   171      * other values are converted to an LDAP-compatible 'FALSE'.
   172      *
   172      *
   173      * @param	boolean|integer|string		$value	The boolean value to encode
   173      * @param    boolean|integer|string        $value    The boolean value to encode
   174      * @return	string
   174      * @return    string
   175      */
   175      */
   176     public static function toLdapBoolean($value)
   176     public static function toLdapBoolean($value)
   177     {
   177     {
   178         $return = 'FALSE';
   178         $return = 'FALSE';
   179         if (!is_scalar($value)) {
   179         if (!is_scalar($value)) {
   186     }
   186     }
   187 
   187 
   188     /**
   188     /**
   189      * Serialize any value for storage in LDAP
   189      * Serialize any value for storage in LDAP
   190      *
   190      *
   191      * @param	mixed		$value	The value to serialize
   191      * @param    mixed        $value    The value to serialize
   192      * @return	string
   192      * @return    string
   193      */
   193      */
   194     public static function toLdapSerialize($value)
   194     public static function toLdapSerialize($value)
   195     {
   195     {
   196         return serialize($value);
   196         return serialize($value);
   197     }
   197     }
   200      * Convert an LDAP-compatible value to a corresponding PHP-value.
   200      * Convert an LDAP-compatible value to a corresponding PHP-value.
   201      *
   201      *
   202      * By setting the <var>$type</var>-parameter the conversion of a certain
   202      * By setting the <var>$type</var>-parameter the conversion of a certain
   203      * type can be forced
   203      * type can be forced
   204      * .
   204      * .
   205      * @param	string	$value 			The value to convert
   205      * @param    string    $value             The value to convert
   206      * @param	int		$ytpe  			The conversion type to use
   206      * @param    int        $ytpe              The conversion type to use
   207      * @param	boolean	$dateTimeAsUtc	Return DateTime values in UTC timezone
   207      * @param    boolean    $dateTimeAsUtc    Return DateTime values in UTC timezone
   208      * @return	mixed
   208      * @return    mixed
   209      * @throws	Zend_Ldap_Converter_Exception
   209      * @throws    Zend_Ldap_Converter_Exception
   210      */
   210      */
   211     public static function fromLdap($value, $type = self::STANDARD, $dateTimeAsUtc = true)
   211     public static function fromLdap($value, $type = self::STANDARD, $dateTimeAsUtc = true)
   212     {
   212     {
   213         switch ($type) {
   213         switch ($type) {
   214             case self::BOOLEAN:
   214             case self::BOOLEAN:
   217             case self::GENERALIZED_TIME:
   217             case self::GENERALIZED_TIME:
   218                 return self::fromLdapDateTime($value);
   218                 return self::fromLdapDateTime($value);
   219                 break;
   219                 break;
   220             default:
   220             default:
   221                 if (is_numeric($value)) {
   221                 if (is_numeric($value)) {
   222                     return (float)$value;
   222                     // prevent numeric values to be treated as date/time
       
   223                     return $value;
   223                 } else if ('TRUE' === $value || 'FALSE' === $value) {
   224                 } else if ('TRUE' === $value || 'FALSE' === $value) {
   224                     return self::fromLdapBoolean($value);
   225                     return self::fromLdapBoolean($value);
   225                 }
   226                 }
   226                 if (preg_match('/^\d{4}[\d\+\-Z\.]*$/', $value)) {
   227                 if (preg_match('/^\d{4}[\d\+\-Z\.]*$/', $value)) {
   227                     return self::fromLdapDateTime($value, $dateTimeAsUtc);
   228                     return self::fromLdapDateTime($value, $dateTimeAsUtc);
   237     /**
   238     /**
   238      * Convert an LDAP-Generalized-Time-entry into a DateTime-Object
   239      * Convert an LDAP-Generalized-Time-entry into a DateTime-Object
   239      *
   240      *
   240      * CAVEAT: The DateTime-Object returned will alwasy be set to UTC-Timezone.
   241      * CAVEAT: The DateTime-Object returned will alwasy be set to UTC-Timezone.
   241      *
   242      *
   242      * @param	string		$date	The generalized-Time
   243      * @param    string        $date    The generalized-Time
   243      * @param	boolean		$asUtc	Return the DateTime with UTC timezone
   244      * @param    boolean        $asUtc    Return the DateTime with UTC timezone
   244      * @return	DateTime
   245      * @return    DateTime
   245      * @throws	InvalidArgumentException if a non-parseable-format is given
   246      * @throws    InvalidArgumentException if a non-parseable-format is given
   246      */
   247      */
   247     public static function fromLdapDateTime($date, $asUtc = true)
   248     public static function fromLdapDateTime($date, $asUtc = true)
   248     {
   249     {
   249         $datepart = array ();
   250         $datepart = array ();
   250         if (!preg_match('/^(\d{4})/', $date, $datepart) ) {
   251         if (!preg_match('/^(\d{4})/', $date, $datepart) ) {
   263             'hour'   => 0,
   264             'hour'   => 0,
   264             'minute' => 0,
   265             'minute' => 0,
   265             'second' => 0,
   266             'second' => 0,
   266             'offdir' => '+',
   267             'offdir' => '+',
   267             'offsethours' => 0,
   268             'offsethours' => 0,
   268 			'offsetminutes' => 0
   269             'offsetminutes' => 0
   269         );
   270         );
   270 
   271 
   271         $length = strlen($date);
   272         $length = strlen($date);
   272 
   273 
   273         // Check for month.
   274         // Check for month.
   361     }
   362     }
   362 
   363 
   363     /**
   364     /**
   364      * Convert an LDAP-compatible boolean value into a PHP-compatible one
   365      * Convert an LDAP-compatible boolean value into a PHP-compatible one
   365      *
   366      *
   366      * @param	string		$value		The value to convert
   367      * @param    string        $value        The value to convert
   367      * @return	boolean
   368      * @return    boolean
   368      * @throws	InvalidArgumentException
   369      * @throws    InvalidArgumentException
   369      */
   370      */
   370     public static function fromLdapBoolean($value)
   371     public static function fromLdapBoolean($value)
   371     {
   372     {
   372         if ( 'TRUE' === $value ) {
   373         if ( 'TRUE' === $value ) {
   373             return true;
   374             return true;
   379     }
   380     }
   380 
   381 
   381     /**
   382     /**
   382      * Unserialize a serialized value to return the corresponding object
   383      * Unserialize a serialized value to return the corresponding object
   383      *
   384      *
   384      * @param	string		$value	The value to convert
   385      * @param    string        $value    The value to convert
   385      * @return	mixed
   386      * @return    mixed
   386      * @throws	UnexpectedValueException
   387      * @throws    UnexpectedValueException
   387      */
   388      */
   388     public static function fromLdapUnserialize($value)
   389     public static function fromLdapUnserialize($value)
   389     {
   390     {
   390         $v = @unserialize($value);
   391         $v = @unserialize($value);
   391         if (false===$v && $value != 'b:0;') {
   392         if (false===$v && $value != 'b:0;') {