PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.8.3
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.8.3
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / helpers / FrmSerializedStringParserHelper.php

FrmSerializedStringParserHelper.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.8.3, at classes/helpers/FrmSerializedStringParserHelper.php

113 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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