Check-in [0e6997be49]
Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | safe version of serialize() and unserialize() by Anthon Pang |
---|---|
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
0e6997be4973732c15ec97f35e53070b |
User & Date: | mario 2010-10-19 14:50:01 |
Context
2010-10-19
| ||
14:51 | switch to double quotes for test-up filter, and add TMPDIR alternative check-in: b92447158a user: mario tags: trunk | |
14:50 | safe version of serialize() and unserialize() by Anthon Pang check-in: 0e6997be49 user: mario tags: trunk | |
2010-07-03
| ||
08:46 | addition submitted by Anthon Pang: mysqli_set_charset(5.0.5) and E_DEPRECATED constant, also adds mysql_set_charset(5.2.3) check-in: 1191f907b3 user: mario tags: trunk | |
Changes
Added ext/contrib/serialize.php.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | <?php /* * @author anthon (dpt) pang (at) gmail (dot) com * @license Public Domain */ /* * Used to impose practical limits for safe_unserialize() */ define('MAX_SERIALIZED_INPUT_LENGTH', 4096); define('MAX_SERIALIZED_ARRAY_LENGTH', 256); define('MAX_SERIALIZED_ARRAY_DEPTH', 3); /** * safe_serialize implementation * * @param mixed $value * @return string * @throw Exception if $value is malformed or contains unsupported types (e.g., resources, objects) */ function _safe_serialize( $value ) { if(is_null($value)) { return 'N;'; } if(is_bool($value)) { return 'b:'.(int)$value.';'; } if(is_int($value)) { return 'i:'.$value.';'; } if(is_float($value)) { return 'd:'.$value.';'; } if(is_string($value)) { return 's:'.strlen($value).':"'.$value.'";'; } if(is_array($value)) { $out = ''; foreach($value as $k => $v) { $out .= _safe_serialize($k) . _safe_serialize($v); } return 'a:'.count($value).':{'.$out.'}'; } if(is_resource($value)) { // built-in returns 'i:0;' throw new Exception('safe_serialize: resources not supported'); } if(is_object($value) || gettype($value) == 'object') { throw new Exception('safe_serialize: objects not supported'); } throw new Exception('safe_serialize cannot serialize: '.gettype($value)); } /** * Safe serialize() replacement * - output a strict subset of PHP's native serialized representation * * @param mixed $value * @return string */ function safe_serialize( $value ) { // ensure we use the byte count for strings even when strlen() is overloaded by mb_strlen() if (function_exists('mb_internal_encoding') && (((int) ini_get('mbstring.func_overload')) & 2)) { $mbIntEnc = mb_internal_encoding(); mb_internal_encoding('ASCII'); } try { $out = _safe_serialize($value); } catch(Exception $e) { $out = false; } if (isset($mbIntEnc)) { mb_internal_encoding($mbIntEnc); } return $out; } /** * safe_unserialize implementation * * @param string $str * @return mixed * @throw Exception if $str is malformed or contains unsupported types (e.g., resources, objects) */ function _safe_unserialize($str) { if(strlen($str) > MAX_SERIALIZED_INPUT_LENGTH) { throw new Exception('safe_unserialize: input exceeds ' . MAX_SERIALIZED_INPUT_LENGTH); } if(empty($str) || !is_string($str)) { return false; } $stack = array(); $expected = array(); $state = 0; while($state != 1) { $type = isset($str[0]) ? $str[0] : ''; if($type == '}') { $str = substr($str, 1); } else if($type == 'N' && $str[1] == ';') { $value = null; $str = substr($str, 2); } else if($type == 'b' && preg_match('/^b:([01]);/', $str, $matches)) { $value = $matches[1] == '1' ? true : false; $str = substr($str, 4); } else if($type == 'i' && preg_match('/^i:(-?[0-9]+);(.*)/s', $str, $matches)) { $value = (int)$matches[1]; $str = $matches[2]; } else if($type == 'd' && preg_match('/^d:(-?[0-9]+\.?[0-9]*(E[+-][0-9]+)?);(.*)/s', $str, $matches)) { $value = (float)$matches[1]; $str = $matches[3]; } else if($type == 's' && preg_match('/^s:([0-9]+):"(.*)/s', $str, $matches) && substr($matches[2], (int)$matches[1], 2) == '";') { $value = substr($matches[2], 0, (int)$matches[1]); $str = substr($matches[2], (int)$matches[1] + 2); } else if($type == 'a' && preg_match('/^a:([0-9]+):{(.*)/s', $str, $matches) && $matches[1] < MAX_SERIALIZED_ARRAY_LENGTH) { $expectedLength = (int)$matches[1]; $str = $matches[2]; } else if($type == 'O') { throw new Exception('safe_unserialize: objects not supported'); } else { throw new Exception('safe_unserialize: unknown/malformed type: '.$type); } switch($state) { case 3: // in array, expecting value or another array if($type == 'a') { if(count($stack) >= MAX_SERIALIZED_ARRAY_DEPTH) { throw new Exception('safe_unserialize: array nesting exceeds ' . MAX_SERIALIZED_ARRAY_DEPTH); } $stack[] = &$list; $list[$key] = array(); $list = &$list[$key]; $expected[] = $expectedLength; $state = 2; break; } if($type != '}') { $list[$key] = $value; $state = 2; break; } throw new Exception('safe_unserialize: missing array value'); case 2: // in array, expecting end of array or a key if($type == '}') { if(count($list) < end($expected)) { throw new Exception('safe_unserialize: array size less than expected ' . $expected[0]); } unset($list); $list = &$stack[count($stack)-1]; array_pop($stack); // go to terminal state if we're at the end of the root array array_pop($expected); if(count($expected) == 0) { $state = 1; } break; } if($type == 'i' || $type == 's') { if(count($list) >= MAX_SERIALIZED_ARRAY_LENGTH) { throw new Exception('safe_unserialize: array size exceeds ' . MAX_SERIALIZED_ARRAY_LENGTH); } if(count($list) >= end($expected)) { throw new Exception('safe_unserialize: array size exceeds expected length'); } $key = $value; $state = 3; break; } throw new Exception('safe_unserialize: illegal array index type'); case 0: // expecting array or value if($type == 'a') { if(count($stack) >= MAX_SERIALIZED_ARRAY_DEPTH) { throw new Exception('safe_unserialize: array nesting exceeds ' . MAX_SERIALIZED_ARRAY_DEPTH); } $data = array(); $list = &$data; $expected[] = $expectedLength; $state = 2; break; } if($type != '}') { $data = $value; $state = 1; break; } throw new Exception('safe_unserialize: not in array'); } } if(!empty($str)) { throw new Exception('safe_unserialize: trailing data in input'); } return $data; } /** * Safe unserialize() replacement * - accepts a strict subset of PHP's native serialized representation * * @param string $str * @return mixed */ function safe_unserialize( $str ) { // ensure we use the byte count for strings even when strlen() is overloaded by mb_strlen() if (function_exists('mb_internal_encoding') && (((int) ini_get('mbstring.func_overload')) & 2)) { $mbIntEnc = mb_internal_encoding(); mb_internal_encoding('ASCII'); } try { $out = _safe_unserialize($str); } catch(Exception $e) { $out = ''; } if (isset($mbIntEnc)) { mb_internal_encoding($mbIntEnc); } return $out; } ?> |