| 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 array|bool|float|int|string|null |
| 52 |
*/ |
| 53 |
private function do_parse( $string ) { |
| 54 |
// May be : or ; as a terminator, depending on what the data type is. |
| 55 |
$type = $string->read( 1 ); |
| 56 |
$string->skip_next_character(); |
| 57 |
|
| 58 |
switch ( $type ) { |
| 59 |
case 'a': |
| 60 |
return $this->parse_array( $string ); |
| 61 |
|
| 62 |
case 's': |
| 63 |
return $this->parse_string( $string ); |
| 64 |
|
| 65 |
case 'i': |
| 66 |
return $this->parse_int( $string ); |
| 67 |
|
| 68 |
case 'd': |
| 69 |
return $this->parse_float( $string ); |
| 70 |
|
| 71 |
case 'b': |
| 72 |
return $this->parse_bool( $string ); |
| 73 |
} |
| 74 |
|
| 75 |
// Includes case 'N' and case 'O'. |
| 76 |
// Treat a serialized object or anything unexpected as Null. |
| 77 |
return null; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @param FrmStringReaderHelper $string |
| 82 |
* @return array |
| 83 |
*/ |
| 84 |
private function parse_array( $string ) { |
| 85 |
// Associative array: a:length:{[index][value]...} |
| 86 |
$count = (int) $string->read_until( ':' ); |
| 87 |
|
| 88 |
// Eat the opening "{" of the array. |
| 89 |
$string->skip_next_character(); |
| 90 |
|
| 91 |
$val = array(); |
| 92 |
for ( $i = 0; $i < $count; $i++ ) { |
| 93 |
$array_key = $this->do_parse( $string ); |
| 94 |
$array_value = $this->do_parse( $string ); |
| 95 |
|
| 96 |
if ( ! is_array( $array_key ) ) { |
| 97 |
$val[ $array_key ] = $array_value; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// Eat "}" terminating the array. |
| 102 |
$string->skip_next_character(); |
| 103 |
|
| 104 |
return $val; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* @param FrmStringReaderHelper $string |
| 109 |
* @return string |
| 110 |
*/ |
| 111 |
private function parse_string( $string ) { |
| 112 |
$len = (int) $string->read_until( ':' ); |
| 113 |
$val = $string->read( $len + 2 ); |
| 114 |
|
| 115 |
// Eat the separator. |
| 116 |
$string->skip_next_character(); |
| 117 |
|
| 118 |
return $val; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @param FrmStringReaderHelper $string |
| 123 |
* @return int |
| 124 |
*/ |
| 125 |
private function parse_int( $string ) { |
| 126 |
return (int) $string->read_until( ';' ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* @param FrmStringReaderHelper $string |
| 131 |
* @return float |
| 132 |
*/ |
| 133 |
private function parse_float( $string ) { |
| 134 |
return (float) $string->read_until( ';' ); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* @param FrmStringReaderHelper $string |
| 139 |
* @return bool |
| 140 |
*/ |
| 141 |
private function parse_bool( $string ) { |
| 142 |
// Boolean is 0 or 1. |
| 143 |
$val = $string->read( 1 ) === '1'; |
| 144 |
$string->skip_next_character(); |
| 145 |
return $val; |
| 146 |
} |
| 147 |
} |
| 148 |
|