| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
die( 'You are not allowed to call this page directly.' ); |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Parses serialized strings without using the unsafe unserialize function. |
| 9 |
* |
| 10 |
* @since 6.2 |
| 11 |
*/ |
| 12 |
class FrmSerializedStringParserHelper { |
| 13 |
|
| 14 |
/** |
| 15 |
* @var FrmSerializedStringParserHelper|null |
| 16 |
*/ |
| 17 |
private static $instance; |
| 18 |
|
| 19 |
/** |
| 20 |
* Get a singleton instance of the parser. |
| 21 |
* |
| 22 |
* @return FrmSerializedStringParserHelper |
| 23 |
*/ |
| 24 |
public static function get() { |
| 25 |
if ( ! isset( self::$instance ) ) { |
| 26 |
self::$instance = new self(); |
| 27 |
} |
| 28 |
return self::$instance; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Private constructor to enforce the use of FrmSerializedStringParserHelper::get. |
| 33 |
*/ |
| 34 |
private function __construct() {} |
| 35 |
|
| 36 |
/** |
| 37 |
* Parse a string containing a serialized data structure. |
| 38 |
* This is the initial entry point into the recursive parser. |
| 39 |
* |
| 40 |
* @param string $string |
| 41 |
* @return mixed |
| 42 |
*/ |
| 43 |
public function parse( $string ) { |
| 44 |
return $this->do_parse( new FrmStringReaderHelper( $string ) ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* This is the recursive parser. |
| 49 |
* |
| 50 |
* @param FrmStringReaderHelper $string |
| 51 |
* @return mixed |
| 52 |
*/ |
| 53 |
private function do_parse( $string ) { |
| 54 |
$val = null; |
| 55 |
|
| 56 |
// May be : or ; as a terminator, depending on what the data type is. |
| 57 |
$type = substr( $string->read( 2 ), 0, 1 ); |
| 58 |
|
| 59 |
switch ( $type ) { |
| 60 |
case 'a': |
| 61 |
// Associative array: a:length:{[index][value]...} |
| 62 |
$count = (int) $string->read_until( ':' ); |
| 63 |
|
| 64 |
// Eat the opening "{" of the array. |
| 65 |
$string->read( 1 ); |
| 66 |
|
| 67 |
$val = array(); |
| 68 |
for ( $i = 0; $i < $count; $i++ ) { |
| 69 |
$array_key = $this->do_parse( $string ); |
| 70 |
$array_value = $this->do_parse( $string ); |
| 71 |
|
| 72 |
if ( ! is_array( $array_key ) ) { |
| 73 |
$val[ $array_key ] = $array_value; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// Eat "}" terminating the array. |
| 78 |
$string->read( 1 ); |
| 79 |
break; |
| 80 |
|
| 81 |
case 's': |
| 82 |
$len = (int) $string->read_until( ':' ); |
| 83 |
$val = $string->read( $len + 2 ); |
| 84 |
|
| 85 |
// Eat the separator. |
| 86 |
$string->read( 1 ); |
| 87 |
break; |
| 88 |
|
| 89 |
case 'i': |
| 90 |
$val = (int) $string->read_until( ';' ); |
| 91 |
break; |
| 92 |
|
| 93 |
case 'd': |
| 94 |
$val = (float) $string->read_until( ';' ); |
| 95 |
break; |
| 96 |
|
| 97 |
case 'b': |
| 98 |
// Boolean is 0 or 1. |
| 99 |
$bool = $string->read( 2 ); |
| 100 |
$val = substr( $bool, 0, 1 ) == '1'; |
| 101 |
break; |
| 102 |
|
| 103 |
default: |
| 104 |
// Includes case 'N' and case 'O'. |
| 105 |
// Treat a serialized object or anything unexpected as Null. |
| 106 |
$val = null; |
| 107 |
break; |
| 108 |
}//end switch |
| 109 |
|
| 110 |
return $val; |
| 111 |
} |
| 112 |
} |
| 113 |
|