| 1 | # | ||
| 2 | # execute PHP built-in functions | ||
| 3 | # | ||
| 4 | package PHP::Decode::Func; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use List::Util qw(min max shuffle); | ||
| 9 | use MIME::Base64; | ||
| 10 | use Compress::Zlib; | ||
| 11 | use Digest::MD5 qw(md5 md5_hex); | ||
| 12 | eval "use Digest::SHA1 qw(sha1 sha1_hex); 1" or eval "use Digest::SHA qw(sha1 sha1_hex); 1" or die "Digest::SHA/SHA1 required"; | ||
| 13 | use HTML::Entities; | ||
| 14 | use URI::Escape; | ||
| 15 | use File::Basename; | ||
| 16 | use PHP::Decode::Array qw(is_int_index); | ||
| 17 | use PHP::Decode::Op; | ||
| 18 | use PHP::Decode::Parser qw(:all); | ||
| 19 | use PHP::Decode::Transformer; | ||
| 20 | |||
| 21 | our $VERSION = '0.132'; | ||
| 22 | |||
| 23 | # if block contains just a sequence of strings, then return joined string | ||
| 24 | # | ||
| 25 | sub _joinable_str { | ||
| 26 | my ($parser, $s, $val) = @_; | ||
| 27 | |||
| 28 | if (is_block($s)) { | ||
| 29 | my ($type, $a) = @{$parser->{strmap}{$s}}; | ||
| 30 | foreach my $stmt (@$a) { | ||
| 31 | $val = &_joinable_str($parser, $stmt, $val); | ||
| 32 | last if (!defined $val); | ||
| 33 | } | ||
| 34 | return $val; | ||
| 35 | } | ||
| 36 | if (is_strval($s)) { | ||
| 37 | return $val . $parser->{strmap}{$s}; | ||
| 38 | } | ||
| 39 | if (is_null($s)) { | ||
| 40 | return ''; | ||
| 41 | } | ||
| 42 | return; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub parsing_func { | ||
| 46 | my ($ctx) = @_; | ||
| 47 | |||
| 48 | if ($ctx->{infunction}) { | ||
| 49 | if (!$ctx->{incall}) { | ||
| 50 | return 0; | ||
| 51 | } | ||
| 52 | return 1; | ||
| 53 | } | ||
| 54 | return 0; | ||
| 55 | } | ||
| 56 | |||
| 57 | # see: https://gist.github.com/BaiGang/1321793 | ||
| 58 | # | ||
| 59 | sub levenshtein { | ||
| 60 | my ($str1, $str2) = @_; | ||
| 61 | my ($len1, $len2) = (length $str1, length $str2); | ||
| 62 | my %mat; | ||
| 63 | |||
| 64 | if ($len1 == 0) { | ||
| 65 | return $len2; | ||
| 66 | } | ||
| 67 | if ($len2 == 0) { | ||
| 68 | return $len1; | ||
| 69 | } | ||
| 70 | for (my $i = 0; $i <= $len1; ++$i) { | ||
| 71 | $mat{0}{$i} = $i; | ||
| 72 | $mat{1}{$i} = 0; | ||
| 73 | } | ||
| 74 | my @ar1 = split //, $str1; | ||
| 75 | my @ar2 = split //, $str2; | ||
| 76 | |||
| 77 | for (my $j = 1; $j <= $len2; ++$j) { | ||
| 78 | my $p = $j % 2; | ||
| 79 | my $q = ($j + 1) % 2; | ||
| 80 | $mat{$p}{0} = $j; | ||
| 81 | for (my $i = 1; $i <= $len1; ++$i) { | ||
| 82 | my $cost = 0; | ||
| 83 | if ($ar1[$i-1] ne $ar2[$j-1]) { | ||
| 84 | $cost = 1; | ||
| 85 | } | ||
| 86 | $mat{$p}{$i} = min($cost + $mat{$q}{$i-1}, | ||
| 87 | $mat{$p}{$i-1} + 1, $mat{$q}{$i} + 1); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | return $mat{$len2%2}{$len1}; | ||
| 91 | } | ||
| 92 | |||
| 93 | # see: Perl-only CRC32 function | ||
| 94 | # http://billauer.co.il/blog/2011/05/perl-crc32-crc-xs-module/ | ||
| 95 | # | ||
| 96 | my $crc32a_table; | ||
| 97 | my $crc32b_table; | ||
| 98 | |||
| 99 | sub mycrc32a { | ||
| 100 | my ($input, $init_value, $polynomial) = @_; | ||
| 101 | |||
| 102 | # hash('crc32', $str); | ||
| 103 | # == perl -I./Digest-CRC-0.22/lib -e 'use Digest::CRC; | ||
| 104 | # $ctx = Digest::CRC->new(width=>32, init=>0xffffffff, xorout=>0xffffffff, refout=>0, poly=>0x4C11DB7, refin=>0, cont=>0); | ||
| 105 | # $ctx->add("test"); | ||
| 106 | # print $digest = $ctx->hexdigest . "\n";' --> (need to read bytes backwards) | ||
| 107 | # | ||
| 108 | $init_value = 0 unless (defined $init_value); | ||
| 109 | $polynomial = 0x04c11db7 unless (defined $polynomial); | ||
| 110 | |||
| 111 | unless (defined $crc32a_table) { | ||
| 112 | $crc32a_table = []; | ||
| 113 | |||
| 114 | for (my $i=0; $i<256; $i++) { | ||
| 115 | my $x = $i << 24; | ||
| 116 | for (my $j=0; $j<8; $j++) { | ||
| 117 | if ($x & (1 << 31)) { | ||
| 118 | $x = ($x << 1) ^ $polynomial; | ||
| 119 | } else { | ||
| 120 | $x = $x << 1; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | $x = $x & 0xffffffff; | ||
| 124 | push @$crc32a_table, $x; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | my $crc = $init_value ^ 0xffffffff; | ||
| 128 | |||
| 129 | foreach my $x (unpack ('C*', $input)) { | ||
| 130 | $crc = (($crc << 8) & 0xffffff00) ^ $crc32a_table->[(($crc >> 24) & 0xff) ^ ($x & 0xff)]; | ||
| 131 | } | ||
| 132 | $crc = $crc ^ 0xffffffff; | ||
| 133 | |||
| 134 | # reverse byteorder | ||
| 135 | $crc = ($crc << 24) & 0xff000000 | ($crc << 8) & 0xff0000 | ($crc >> 8) & 0xff00 | ($crc >> 24) & 0xff; | ||
| 136 | return $crc; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub mycrc32b { | ||
| 140 | my ($input, $init_value, $polynomial) = @_; | ||
| 141 | |||
| 142 | # hash('crc32b', $str); | ||
| 143 | # == perl -I./Digest-CRC-0.22/lib -e 'use Digest::CRC; | ||
| 144 | # $ctx = Digest::CRC->new(width=>32, init=>0xffffffff, xorout=>0xffffffff, refout=>1, poly=>0x4C11DB7, refin=>1, cont=>0); | ||
| 145 | # $ctx->add("test"); | ||
| 146 | # print $digest = $ctx->hexdigest;' | ||
| 147 | # | ||
| 148 | $init_value = 0 unless (defined $init_value); | ||
| 149 | #$polynomial = _reflect(0x04c11db7,32) unless (defined $polynomial); | ||
| 150 | $polynomial = 0xedb88320 unless (defined $polynomial); | ||
| 151 | |||
| 152 | unless (defined $crc32b_table) { | ||
| 153 | $crc32b_table = []; | ||
| 154 | for (my $i=0; $i<256; $i++) { | ||
| 155 | my $x = $i; | ||
| 156 | for (my $j=0; $j<8; $j++) { | ||
| 157 | if ($x & 1) { | ||
| 158 | $x = ($x >> 1) ^ $polynomial; | ||
| 159 | } else { | ||
| 160 | $x = $x >> 1; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | push @$crc32b_table, $x; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | my $crc = $init_value ^ 0xffffffff; | ||
| 167 | |||
| 168 | foreach my $x (unpack ('C*', $input)) { | ||
| 169 | $crc = (($crc >> 8) & 0xffffff) ^ $crc32b_table->[($crc ^ $x) & 0xff]; | ||
| 170 | } | ||
| 171 | $crc = $crc ^ 0xffffffff; | ||
| 172 | return $crc; | ||
| 173 | } | ||
| 174 | |||
| 175 | # see: https://metacpan.org/dist/PHP-Strings/source/Strings.pm | ||
| 176 | # todo: might use more functions from PHP::Strings module | ||
| 177 | # | ||
| 178 | sub stripcslashes { | ||
| 179 | my ($str) = @_; | ||
| 180 | |||
| 181 | $str =~ s{ | ||
| 182 | \\([abfnrtv\\?'"]) | ||
| 183 | | | ||
| 184 | \\(\d\d\d) | ||
| 185 | | | ||
| 186 | \\(x[[:xdigit:]]{2}) | ||
| 187 | | | ||
| 188 | \\(x[[:xdigit:]]) | ||
| 189 | }{ | ||
| 190 | if ( $+ eq 'v' ) { | ||
| 191 | "\013"; | ||
| 192 | } elsif (length $+ == 1) { | ||
| 193 | eval qq{qq/\\$+/}; | ||
| 194 | } else { | ||
| 195 | chr oct "0$+"; | ||
| 196 | } | ||
| 197 | }exg ; | ||
| 198 | return $str; | ||
| 199 | } | ||
| 200 | |||
| 201 | sub dyn_replace { | ||
| 202 | my ($replace) = @_; | ||
| 203 | my @groups; | ||
| 204 | { | ||
| 205 | no strict 'refs'; | ||
| 206 | $groups[$_] = $$_ for 1 .. $#-; # the size of @- tells us the number of capturing groups | ||
| 207 | } | ||
| 208 | # For the e modifier preg_replace escapes [' " \ NULL] in the replacement string. | ||
| 209 | # see: https://www.php.net/manual/en/function.preg-replace.php | ||
| 210 | # | ||
| 211 | for (my $i=1; $i < scalar @groups; $i++) { | ||
| 212 | #print ">> dyn_replace $i: $groups[$i]\n"; | ||
| 213 | $groups[$i] =~ s/(["'\\\0])/\\$1/g; | ||
| 214 | } | ||
| 215 | $replace =~ s/\$(\d+)/$groups[$1]/g; | ||
| 216 | return $replace; | ||
| 217 | } | ||
| 218 | |||
| 219 | sub dyn_replace_eval { | ||
| 220 | my ($ctx, $replace) = @_; | ||
| 221 | my $parser = $ctx->{parser}; | ||
| 222 | my $res = dyn_replace($replace); | ||
| 223 | |||
| 224 | my $code = $parser->setstr($res); | ||
| 225 | my $parser2 = $parser->subparser(); | ||
| 226 | my $ctx2 = $ctx->subctx(parser => $parser2); | ||
| 227 | my $block = $ctx2->parse_eval($code); | ||
| 228 | my $k = $ctx2->exec_eval($block); | ||
| 229 | my $str = _joinable_str($parser, $k, ''); | ||
| 230 | unless (defined $str) { | ||
| 231 | # mark non resolvable code with {{{#stmtX}}} pattern | ||
| 232 | # | ||
| 233 | $str = '{{{' . $k . '}}}'; | ||
| 234 | } | ||
| 235 | $ctx->{log}->($ctx, 'replace', $replace, "$res -> eval($code) -> $str ($k)") if $ctx->{log}; | ||
| 236 | return $str; | ||
| 237 | } | ||
| 238 | |||
| 239 | sub dyn_result { | ||
| 240 | my ($str, $parser) = @_; | ||
| 241 | my @out; | ||
| 242 | my $k; | ||
| 243 | my $res; | ||
| 244 | |||
| 245 | # when the result contains non resolved statements, | ||
| 246 | # then create a chain of concatted expresions. | ||
| 247 | # | ||
| 248 | while ($str =~ /^(.*?)\{\{\{(#\w+\d+)\}\}\}(.*)$/) { | ||
| 249 | if ($1 ne '') { | ||
| 250 | $k = $parser->setstr($1); | ||
| 251 | push(@out, $k); | ||
| 252 | } | ||
| 253 | push(@out, $2); | ||
| 254 | $str = $3; | ||
| 255 | } | ||
| 256 | if (($str eq '') && (scalar @out > 0)) { | ||
| 257 | $res = pop(@out); | ||
| 258 | } else { | ||
| 259 | $res = $parser->setstr($str); | ||
| 260 | } | ||
| 261 | while (scalar @out > 0) { | ||
| 262 | my $op1 = pop(@out); | ||
| 263 | $res = $parser->setexpr('.', $op1, $res); | ||
| 264 | } | ||
| 265 | return $res; | ||
| 266 | } | ||
| 267 | |||
| 268 | sub preg_replace { | ||
| 269 | my ($ctx, $_pattern, $_replacement, $str, $mode, $limit) = @_; | ||
| 270 | my $parser = $ctx->{parser}; | ||
| 271 | my $pattern = $parser->get_strval($_pattern); | ||
| 272 | my $replacement = $parser->get_strval($_replacement); | ||
| 273 | my $cnt = 0; | ||
| 274 | my $res; | ||
| 275 | my $k; | ||
| 276 | |||
| 277 | if ($str eq '#null') { | ||
| 278 | $res = ''; | ||
| 279 | } else { | ||
| 280 | $res = $parser->get_strval($str); | ||
| 281 | } | ||
| 282 | unless (defined $pattern && defined $res) { | ||
| 283 | return; | ||
| 284 | } | ||
| 285 | my $m = ''; | ||
| 286 | my $modifier; | ||
| 287 | if ($mode eq 'preg') { | ||
| 288 | my ($delim) = $pattern =~ /^(.)/; | ||
| 289 | my $delim2; | ||
| 290 | |||
| 291 | # allow bracket delimiters | ||
| 292 | # https://php.net/regexp.reference.delimiters | ||
| 293 | # | ||
| 294 | if ($delim eq '[') { | ||
| 295 | $delim2 = ']'; | ||
| 296 | } elsif ($delim eq '(') { | ||
| 297 | $delim2 = ')'; | ||
| 298 | } elsif ($delim eq '{') { | ||
| 299 | $delim2 = '}'; | ||
| 300 | } elsif ($delim eq '<') { | ||
| 301 | $delim2 = '>'; | ||
| 302 | } else { | ||
| 303 | $delim2 = $delim;; | ||
| 304 | } | ||
| 305 | # TODO: need to handle escapes in pattern? | ||
| 306 | # https://php.net/manual/en/function.preg-quote.php | ||
| 307 | # | ||
| 308 | if ($pattern =~ m|^\Q$delim\E([^$delim2]*)\Q$delim2\E(\w*)$|) { | ||
| 309 | $pattern = $1; | ||
| 310 | $modifier = $2; | ||
| 311 | $m .= 'i' if ($modifier =~ /i/); | ||
| 312 | $m .= 'm' if ($modifier =~ /m/); | ||
| 313 | $m .= 's' if ($modifier =~ /s/); | ||
| 314 | $m .= 'x' if ($modifier =~ /x/); | ||
| 315 | } | ||
| 316 | } | ||
| 317 | |||
| 318 | # don't escape pattern here - it might contain regexp rules | ||
| 319 | # don't escape replacement here - special-chars should not get excaped | ||
| 320 | # | ||
| 321 | if ($limit == 0) { | ||
| 322 | # delete pattern | ||
| 323 | if ($mode eq 'preg') { | ||
| 324 | $res =~ s/$pattern//g; | ||
| 325 | } else { | ||
| 326 | $res =~ s/\Q$pattern\E//g; | ||
| 327 | } | ||
| 328 | $k = $parser->setstr($res); | ||
| 329 | } else { | ||
| 330 | unless (defined $replacement) { | ||
| 331 | return; | ||
| 332 | } | ||
| 333 | |||
| 334 | # the replacement might contain backreferences in the | ||
| 335 | # form '\1'. Convert them to '$1' for perl call. | ||
| 336 | # | ||
| 337 | if ($mode eq 'preg') { | ||
| 338 | $replacement =~ s/(\\([0-9]))/\$$2/g; | ||
| 339 | } | ||
| 340 | |||
| 341 | # When the replacement contains backreferences and | ||
| 342 | # it comes from data, it needs to get evaled first. | ||
| 343 | # (eval as string by /ee does not help in this case). | ||
| 344 | # | ||
| 345 | # http://stackoverflow.com/questions/1908159/perl-can-i-store-backreferences-not-their-values-in-variables | ||
| 346 | # | ||
| 347 | if (defined $modifier && $modifier =~ /e/) { | ||
| 348 | # eval-modifier deprecated since php-5.5 | ||
| 349 | # https://php.net/manual/en/reference.pcre.pattern.modifiers.php#reference.pcre.pattern.modifiers.eval | ||
| 350 | # | ||
| 351 | eval { $res =~ s/$pattern/dyn_replace_eval($ctx, $replacement)/eg; }; | ||
| 352 | if ($@) { | ||
| 353 | $ctx->{warn}->($ctx, 'replace', $str, "bad preg/e $pattern"); | ||
| 354 | return; | ||
| 355 | } | ||
| 356 | if (scalar @- > 0) { | ||
| 357 | $cnt++; | ||
| 358 | } | ||
| 359 | $k = dyn_result($res, $parser); | ||
| 360 | } else { | ||
| 361 | if ($limit != -1) { | ||
| 362 | while ($cnt < $limit) { | ||
| 363 | if ($mode eq 'preg') { | ||
| 364 | eval { $res =~ s/$pattern/dyn_replace($replacement)/e; }; | ||
| 365 | } else { | ||
| 366 | eval { $res =~ s/\Q$pattern\E/$replacement/; }; | ||
| 367 | } | ||
| 368 | if ($@) { | ||
| 369 | $ctx->{warn}->($ctx, 'replace', $str, "bad preg $pattern"); | ||
| 370 | return; | ||
| 371 | } | ||
| 372 | if (scalar @- == 0) { | ||
| 373 | # no match found in substitution | ||
| 374 | last; | ||
| 375 | } | ||
| 376 | $cnt++; | ||
| 377 | } | ||
| 378 | } else { | ||
| 379 | if ($mode eq 'preg') { | ||
| 380 | eval { $res =~ s/$pattern/dyn_replace($replacement)/eg; }; | ||
| 381 | } else { | ||
| 382 | # TODO: use quotemeta($replacement) here? | ||
| 383 | # | ||
| 384 | eval { $res =~ s/\Q$pattern\E/$replacement/g; }; | ||
| 385 | } | ||
| 386 | if ($@) { | ||
| 387 | $ctx->{warn}->($ctx, 'replace', $str, "bad preg $pattern"); | ||
| 388 | return; | ||
| 389 | } | ||
| 390 | if (scalar @- > 0) { | ||
| 391 | $cnt++; | ||
| 392 | } | ||
| 393 | } | ||
| 394 | $k = $parser->setstr($res); | ||
| 395 | } | ||
| 396 | } | ||
| 397 | $ctx->{log}->($ctx, 'replace', $str, "[%s]->[%s] %s -> %s", $parser->shortstr($pattern,40), $parser->shortstr($replacement,40), $parser->shortstr($str,40), $parser->shortstr($res,40)) if $ctx->{log}; | ||
| 398 | return ($k, $cnt); | ||
| 399 | } | ||
| 400 | |||
| 401 | sub preg_replace_subject { | ||
| 402 | my ($ctx, $pattern, $replacement, $str, $mode, $limit) = @_; | ||
| 403 | my $parser = $ctx->{parser}; | ||
| 404 | |||
| 405 | if (is_array($pattern)) { | ||
| 406 | my $arr = $parser->{strmap}{$pattern}; | ||
| 407 | my $keys = $arr->get_keys(); | ||
| 408 | my $rep = $replacement; | ||
| 409 | my $r_arr; | ||
| 410 | my $r_keys; | ||
| 411 | my $cnt = 0; | ||
| 412 | |||
| 413 | if (is_array($replacement)) { | ||
| 414 | $r_arr = $parser->{strmap}{$replacement}; | ||
| 415 | $r_keys = $r_arr->get_keys(); | ||
| 416 | } | ||
| 417 | foreach my $k (@$keys) { | ||
| 418 | my $val = $arr->val($k); | ||
| 419 | if (is_array($replacement)) { | ||
| 420 | my $rk = shift(@$r_keys); | ||
| 421 | $rep = $r_arr->val($rk); | ||
| 422 | } | ||
| 423 | #$ctx->{log}->($ctx, 'replace', $str, "preg $val -> $rep [$str]") if $ctx->{log}; | ||
| 424 | my ($r, $n) = preg_replace($ctx, $val, $rep, $str, $mode, $limit); | ||
| 425 | if ($limit != -1) { | ||
| 426 | $limit -= $cnt; | ||
| 427 | } | ||
| 428 | $cnt += $n; | ||
| 429 | $str = $r; | ||
| 430 | } | ||
| 431 | return ($str, $cnt); | ||
| 432 | } elsif (is_strval($pattern)) { | ||
| 433 | my ($r, $cnt) = preg_replace($ctx, $pattern, $replacement, $str, $mode, $limit); | ||
| 434 | return ($r, $cnt); | ||
| 435 | } | ||
| 436 | return; | ||
| 437 | } | ||
| 438 | |||
| 439 | use constant R_VOID => 0x0001; | ||
| 440 | use constant R_INT => 0x0002; | ||
| 441 | use constant R_FLOAT => 0x0004; | ||
| 442 | use constant R_BOOL => 0x0008; | ||
| 443 | use constant R_STR => 0x0010; | ||
| 444 | use constant R_ARRAY => 0x0020; | ||
| 445 | use constant R_OBJECT => 0x0040; | ||
| 446 | use constant R_CALL => 0x0080; | ||
| 447 | use constant R_MIXED => 0x0100; | ||
| 448 | use constant R_FIX => 0x0200; # string not usable by eval | ||
| 449 | use constant R_FIXSTR => (R_FIX|R_STR); | ||
| 450 | |||
| 451 | # https://php.net/manual/en/function.get-defined-functions.php | ||
| 452 | # list all: php -r '$x = get_defined_functions(); print_r($x);' | less | ||
| 453 | # list standard: php -r '$f=get_extension_funcs("standard"); print_r($f);' | less | ||
| 454 | # list per extension: php -r '$x = get_loaded_extensions(); foreach ($x as $e) { print ("$e: \n"); $f=get_extension_funcs($e); print_r($f); }' | less | ||
| 455 | # | ||
| 456 | my %php_funcs_core = ( | ||
| 457 | zend_version => { ret => R_FIXSTR }, | ||
| 458 | func_num_args => { ret => R_INT }, | ||
| 459 | func_get_arg => { ret => R_MIXED }, | ||
| 460 | func_get_args => { ret => R_ARRAY }, | ||
| 461 | strlen => { ret => R_INT }, | ||
| 462 | strcmp => { ret => R_INT }, | ||
| 463 | strncmp => { ret => R_INT }, | ||
| 464 | strcasecmp => { ret => R_INT }, | ||
| 465 | strncasecmp => { ret => R_INT }, | ||
| 466 | each => { param => ['#ref0'], ret => R_ARRAY }, | ||
| 467 | error_reporting => { ret => R_INT }, | ||
| 468 | define => { ret => R_BOOL }, | ||
| 469 | defined => { ret => R_BOOL }, | ||
| 470 | get_class => { ret => R_STR }, | ||
| 471 | get_called_class => { ret => R_STR }, | ||
| 472 | get_parent_class => { ret => R_STR }, | ||
| 473 | method_exists => { ret => R_BOOL }, | ||
| 474 | property_exists => { ret => R_BOOL }, | ||
| 475 | class_exists => { ret => R_BOOL }, | ||
| 476 | interface_exists => { ret => R_BOOL }, | ||
| 477 | function_exists => { ret => R_BOOL }, | ||
| 478 | class_alias => { ret => R_BOOL }, | ||
| 479 | get_included_files => { ret => R_ARRAY }, | ||
| 480 | get_required_files => { ret => R_ARRAY }, | ||
| 481 | is_subclass_of => { ret => R_BOOL }, | ||
| 482 | is_a => { ret => R_BOOL }, | ||
| 483 | get_class_vars => { ret => R_ARRAY }, | ||
| 484 | get_object_vars => { ret => R_ARRAY }, | ||
| 485 | get_class_methods => { ret => R_ARRAY }, | ||
| 486 | trigger_error => { ret => R_BOOL }, | ||
| 487 | user_error => { ret => R_BOOL }, | ||
| 488 | set_error_handler => { ret => R_CALL }, | ||
| 489 | restore_error_handler => { ret => R_BOOL }, | ||
| 490 | set_exception_handler => { ret => R_CALL }, | ||
| 491 | restore_exception_handler => { ret => R_BOOL }, | ||
| 492 | get_declared_classes => { ret => R_ARRAY }, | ||
| 493 | get_declared_interfaces => { ret => R_ARRAY }, | ||
| 494 | get_defined_functions => { ret => R_ARRAY }, | ||
| 495 | get_defined_vars => { ret => R_ARRAY }, | ||
| 496 | create_function => { ret => R_STR, callable => 1 }, | ||
| 497 | get_resource_type => { ret => R_FIXSTR }, | ||
| 498 | get_loaded_extensions => { ret => R_ARRAY }, | ||
| 499 | extension_loaded => { ret => R_BOOL }, | ||
| 500 | get_extension_funcs => { ret => R_ARRAY }, | ||
| 501 | get_defined_constants => { ret => R_ARRAY }, | ||
| 502 | debug_backtrace => { ret => R_ARRAY }, | ||
| 503 | debug_print_backtrace => { ret => R_VOID }, | ||
| 504 | gc_collect_cycles => { ret => R_INT }, | ||
| 505 | gc_enabled => { ret => R_BOOL }, | ||
| 506 | gc_enable => { ret => R_VOID }, | ||
| 507 | gc_disable => { ret => R_VOID }, | ||
| 508 | gc_status => { ret => R_ARRAY }, | ||
| 509 | ); | ||
| 510 | |||
| 511 | my %php_funcs_standard = ( | ||
| 512 | exit => { ret => R_VOID }, # is language construct | ||
| 513 | die => { ret => R_VOID }, # is language construct | ||
| 514 | unset => { ret => R_VOID }, # is language construct | ||
| 515 | constant => { ret => R_MIXED }, | ||
| 516 | bin2hex => { ret => R_FIXSTR }, | ||
| 517 | sleep => { ret => R_INT }, | ||
| 518 | usleep => { ret => R_VOID }, | ||
| 519 | time_nanosleep => { ret => R_ARRAY|R_BOOL }, | ||
| 520 | time_sleep_until => { ret => R_BOOL }, | ||
| 521 | strptime => { ret => R_STR }, | ||
| 522 | flush => { ret => R_VOID }, | ||
| 523 | wordwrap => { ret => R_STR }, | ||
| 524 | htmlspecialchars => { ret => R_STR }, | ||
| 525 | htmlentities => { ret => R_STR }, | ||
| 526 | html_entity_decode => { ret => R_STR }, | ||
| 527 | htmlspecialchars_decode => { ret => R_STR }, | ||
| 528 | get_html_translation_table => { ret => R_ARRAY }, | ||
| 529 | sha1 => { ret => R_FIXSTR }, | ||
| 530 | sha1_file => { ret => R_FIXSTR|R_BOOL }, | ||
| 531 | md5 => { ret => R_FIXSTR }, | ||
| 532 | md5_file => { ret => R_FIXSTR|R_BOOL }, | ||
| 533 | crc32 => { ret => R_INT }, | ||
| 534 | iptcparse => {}, | ||
| 535 | iptcembed => {}, | ||
| 536 | getimagesize => {}, | ||
| 537 | image_type_to_mime_type => {}, | ||
| 538 | image_type_to_extension => {}, | ||
| 539 | phpinfo => {}, | ||
| 540 | phpversion => {}, | ||
| 541 | phpcredits => {}, | ||
| 542 | php_logo_guid => {}, | ||
| 543 | php_real_logo_guid => {}, | ||
| 544 | php_egg_logo_guid => {}, | ||
| 545 | zend_logo_guid => {}, | ||
| 546 | php_sapi_name => {}, | ||
| 547 | php_uname => { ret => R_STR }, | ||
| 548 | php_ini_scanned_files => {}, | ||
| 549 | php_ini_loaded_file => {}, | ||
| 550 | strnatcmp => {}, | ||
| 551 | strnatcasecmp => {}, | ||
| 552 | substr_count => { ret => R_INT }, | ||
| 553 | strspn => {}, | ||
| 554 | strcspn => {}, | ||
| 555 | strtok => {}, | ||
| 556 | strtoupper => {}, | ||
| 557 | strtolower => {}, | ||
| 558 | strpos => {}, | ||
| 559 | stripos => {}, | ||
| 560 | strrpos => {}, | ||
| 561 | strripos => {}, | ||
| 562 | strrev => { ret => R_STR }, | ||
| 563 | hebrev => {}, | ||
| 564 | hebrevc => {}, | ||
| 565 | nl2br => {}, | ||
| 566 | basename => {}, | ||
| 567 | dirname => {}, | ||
| 568 | pathinfo => {}, | ||
| 569 | stripslashes => {}, | ||
| 570 | stripcslashes => {}, | ||
| 571 | strstr => {}, | ||
| 572 | stristr => {}, | ||
| 573 | strrchr => {}, | ||
| 574 | str_shuffle => {}, | ||
| 575 | str_word_count => {}, | ||
| 576 | str_split => { ret => R_ARRAY }, | ||
| 577 | strpbrk => {}, | ||
| 578 | substr_compare => {}, | ||
| 579 | strcoll => {}, | ||
| 580 | money_format => {}, | ||
| 581 | substr => {}, | ||
| 582 | substr_replace => {}, | ||
| 583 | quotemeta => {}, | ||
| 584 | ucfirst => {}, | ||
| 585 | lcfirst => {}, | ||
| 586 | ucwords => {}, | ||
| 587 | strtr => {}, | ||
| 588 | addslashes => {}, | ||
| 589 | addcslashes => {}, | ||
| 590 | rtrim => { ret => R_STR }, | ||
| 591 | str_replace => {}, | ||
| 592 | str_ireplace => {}, | ||
| 593 | str_repeat => {}, | ||
| 594 | count_chars => {}, | ||
| 595 | chunk_split => {}, | ||
| 596 | trim => { ret => R_STR }, | ||
| 597 | ltrim => { ret => R_STR }, | ||
| 598 | strip_tags => {}, | ||
| 599 | similar_text => {}, | ||
| 600 | explode => {}, | ||
| 601 | implode => {}, | ||
| 602 | join => {}, | ||
| 603 | setlocale => {}, | ||
| 604 | localeconv => {}, | ||
| 605 | nl_langinfo => {}, | ||
| 606 | soundex => {}, | ||
| 607 | levenshtein => {}, | ||
| 608 | chr => {}, | ||
| 609 | ord => {}, | ||
| 610 | parse_str => {}, | ||
| 611 | str_getcsv => {}, | ||
| 612 | str_pad => {}, | ||
| 613 | chop => {}, | ||
| 614 | strchr => {}, | ||
| 615 | sprintf => {}, | ||
| 616 | printf => {}, | ||
| 617 | vprintf => {}, | ||
| 618 | vsprintf => {}, | ||
| 619 | fprintf => {}, | ||
| 620 | vfprintf => {}, | ||
| 621 | sscanf => {}, | ||
| 622 | fscanf => {}, | ||
| 623 | parse_url => {}, | ||
| 624 | urlencode => { ret => R_STR }, | ||
| 625 | urldecode => { ret => R_STR }, | ||
| 626 | rawurlencode => { ret => R_STR }, | ||
| 627 | rawurldecode => { ret => R_STR }, | ||
| 628 | http_build_query => {}, | ||
| 629 | readlink => {}, | ||
| 630 | linkinfo => {}, | ||
| 631 | symlink => {}, | ||
| 632 | link => {}, | ||
| 633 | unlink => {}, | ||
| 634 | exec => { param => ['#str0', '#ref0', '#ref0'], ret => R_STR|R_BOOL }, | ||
| 635 | system => {}, | ||
| 636 | escapeshellcmd => {}, | ||
| 637 | escapeshellarg => {}, | ||
| 638 | passthru => {}, | ||
| 639 | shell_exec => {}, | ||
| 640 | proc_open => {}, | ||
| 641 | proc_close => {}, | ||
| 642 | proc_terminate => {}, | ||
| 643 | proc_get_status => {}, | ||
| 644 | proc_nice => {}, | ||
| 645 | rand => {}, | ||
| 646 | srand => {}, | ||
| 647 | getrandmax => {}, | ||
| 648 | mt_rand => {}, | ||
| 649 | mt_srand => {}, | ||
| 650 | mt_getrandmax => {}, | ||
| 651 | getservbyname => {}, | ||
| 652 | getservbyport => {}, | ||
| 653 | getprotobyname => {}, | ||
| 654 | getprotobynumber => {}, | ||
| 655 | getmyuid => {}, | ||
| 656 | getmygid => {}, | ||
| 657 | getmypid => {}, | ||
| 658 | getmyinode => {}, | ||
| 659 | getlastmod => {}, | ||
| 660 | base64_decode => { cmd => \&cmd_base64_decode, ret => R_STR }, | ||
| 661 | base64_encode => { cmd => \&cmd_base64_encode, ret => R_STR }, | ||
| 662 | convert_uuencode => {}, | ||
| 663 | convert_uudecode => {}, | ||
| 664 | abs => {}, | ||
| 665 | ceil => {}, | ||
| 666 | floor => {}, | ||
| 667 | round => {}, | ||
| 668 | sin => {}, | ||
| 669 | cos => {}, | ||
| 670 | tan => {}, | ||
| 671 | asin => {}, | ||
| 672 | acos => {}, | ||
| 673 | atan => {}, | ||
| 674 | atanh => {}, | ||
| 675 | atan2 => {}, | ||
| 676 | sinh => {}, | ||
| 677 | cosh => {}, | ||
| 678 | tanh => {}, | ||
| 679 | asinh => {}, | ||
| 680 | acosh => {}, | ||
| 681 | expm1 => {}, | ||
| 682 | log1p => {}, | ||
| 683 | pi => {}, | ||
| 684 | is_finite => {}, | ||
| 685 | is_nan => {}, | ||
| 686 | is_infinite => {}, | ||
| 687 | pow => { ret => R_INT|R_FLOAT }, | ||
| 688 | exp => {}, | ||
| 689 | log => {}, | ||
| 690 | log10 => {}, | ||
| 691 | sqrt => {}, | ||
| 692 | hypot => {}, | ||
| 693 | deg2rad => {}, | ||
| 694 | rad2deg => {}, | ||
| 695 | bindec => {}, | ||
| 696 | hexdec => {}, | ||
| 697 | octdec => {}, | ||
| 698 | decbin => {}, | ||
| 699 | decoct => {}, | ||
| 700 | dechex => {}, | ||
| 701 | base_convert => {}, | ||
| 702 | number_format => {}, | ||
| 703 | fmod => {}, | ||
| 704 | inet_ntop => {}, | ||
| 705 | inet_pton => {}, | ||
| 706 | ip2long => {}, | ||
| 707 | long2ip => {}, | ||
| 708 | getenv => { cmd => \&PHP::Decode::Transformer::cmd_getenv, ret => R_STR }, | ||
| 709 | putenv => {}, | ||
| 710 | getopt => {}, | ||
| 711 | sys_getloadavg => {}, | ||
| 712 | microtime => {}, | ||
| 713 | gettimeofday => {}, | ||
| 714 | getrusage => {}, | ||
| 715 | uniqid => {}, | ||
| 716 | quoted_printable_decode => {}, | ||
| 717 | quoted_printable_encode => {}, | ||
| 718 | convert_cyr_string => {}, | ||
| 719 | get_current_user => {}, | ||
| 720 | set_time_limit => {}, | ||
| 721 | get_cfg_var => {}, | ||
| 722 | magic_quotes_runtime => {}, | ||
| 723 | set_magic_quotes_runtime => {}, | ||
| 724 | get_magic_quotes_gpc => {}, | ||
| 725 | get_magic_quotes_runtime => {}, | ||
| 726 | import_request_variables => {}, | ||
| 727 | error_log => {}, | ||
| 728 | error_get_last => {}, | ||
| 729 | call_user_func => { callable => 1 }, | ||
| 730 | call_user_func_array => { callable => 1 }, | ||
| 731 | call_user_method => { callable => 1 }, | ||
| 732 | call_user_method_array => { callable => 1 }, | ||
| 733 | forward_static_call => { callable => 1 }, | ||
| 734 | forward_static_call_array => { callable => 1 }, | ||
| 735 | serialize => {}, | ||
| 736 | unserialize => {}, | ||
| 737 | var_dump => {}, | ||
| 738 | var_export => {}, | ||
| 739 | debug_zval_dump => {}, | ||
| 740 | print_r => {}, | ||
| 741 | memory_get_usage => {}, | ||
| 742 | memory_get_peak_usage => {}, | ||
| 743 | register_shutdown_function => { callable => 1 }, | ||
| 744 | register_tick_function => { callable => 1 }, | ||
| 745 | unregister_tick_function => {}, | ||
| 746 | highlight_file => {}, | ||
| 747 | show_source => {}, | ||
| 748 | highlight_string => {}, | ||
| 749 | php_strip_whitespace => {}, | ||
| 750 | ini_get => {}, | ||
| 751 | ini_get_all => {}, | ||
| 752 | ini_set => {}, | ||
| 753 | ini_alter => {}, | ||
| 754 | ini_restore => {}, | ||
| 755 | get_include_path => {}, | ||
| 756 | set_include_path => {}, | ||
| 757 | restore_include_path => {}, | ||
| 758 | setcookie => {}, | ||
| 759 | setrawcookie => {}, | ||
| 760 | header => {}, | ||
| 761 | header_remove => {}, | ||
| 762 | headers_sent => {}, | ||
| 763 | headers_list => {}, | ||
| 764 | connection_aborted => {}, | ||
| 765 | connection_status => {}, | ||
| 766 | ignore_user_abort => {}, | ||
| 767 | parse_ini_file => {}, | ||
| 768 | parse_ini_string => {}, | ||
| 769 | is_uploaded_file => {}, | ||
| 770 | move_uploaded_file => {}, | ||
| 771 | gethostbyaddr => {}, | ||
| 772 | gethostbyname => {}, | ||
| 773 | gethostbynamel => {}, | ||
| 774 | gethostname => {}, | ||
| 775 | dns_check_record => {}, | ||
| 776 | checkdnsrr => {}, | ||
| 777 | dns_get_mx => {}, | ||
| 778 | getmxrr => {}, | ||
| 779 | dns_get_record => {}, | ||
| 780 | intval => { ret => R_INT }, | ||
| 781 | floatval => { ret => R_FLOAT }, | ||
| 782 | doubleval => { ret => R_FLOAT }, | ||
| 783 | strval => { ret => R_STR }, | ||
| 784 | gettype => {}, | ||
| 785 | settype => {}, | ||
| 786 | empty => { ret => R_BOOL }, | ||
| 787 | isset => { ret => R_BOOL }, | ||
| 788 | is_null => { ret => R_BOOL }, | ||
| 789 | is_resource => { ret => R_BOOL }, | ||
| 790 | is_bool => { ret => R_BOOL }, | ||
| 791 | is_long => { ret => R_BOOL }, | ||
| 792 | is_float => { ret => R_BOOL }, | ||
| 793 | is_int => { ret => R_BOOL }, | ||
| 794 | is_integer => { ret => R_BOOL }, | ||
| 795 | is_double => { ret => R_BOOL }, | ||
| 796 | is_real => { ret => R_BOOL }, | ||
| 797 | is_numeric => { ret => R_BOOL }, | ||
| 798 | is_string => { ret => R_BOOL }, | ||
| 799 | is_array => { ret => R_BOOL }, | ||
| 800 | is_object => { ret => R_BOOL }, | ||
| 801 | is_scalar => { ret => R_BOOL }, | ||
| 802 | is_callable => { ret => R_BOOL }, | ||
| 803 | pclose => {}, | ||
| 804 | popen => {}, | ||
| 805 | readfile => {}, | ||
| 806 | rewind => {}, | ||
| 807 | rmdir => {}, | ||
| 808 | umask => {}, | ||
| 809 | fclose => {}, | ||
| 810 | feof => {}, | ||
| 811 | fgetc => {}, | ||
| 812 | fgets => {}, | ||
| 813 | fgetss => {}, | ||
| 814 | fread => {}, | ||
| 815 | fopen => {}, | ||
| 816 | fpassthru => {}, | ||
| 817 | ftruncate => {}, | ||
| 818 | fstat => {}, | ||
| 819 | fseek => {}, | ||
| 820 | ftell => {}, | ||
| 821 | fflush => {}, | ||
| 822 | fwrite => {}, | ||
| 823 | fputs => {}, | ||
| 824 | mkdir => {}, | ||
| 825 | rename => {}, | ||
| 826 | copy => {}, | ||
| 827 | tempnam => {}, | ||
| 828 | tmpfile => {}, | ||
| 829 | file => {}, | ||
| 830 | file_get_contents => {}, | ||
| 831 | file_put_contents => {}, | ||
| 832 | stream_select => {}, | ||
| 833 | stream_context_create => {}, | ||
| 834 | stream_context_set_params => {}, | ||
| 835 | stream_context_get_params => {}, | ||
| 836 | stream_context_set_option => {}, | ||
| 837 | stream_context_get_options => {}, | ||
| 838 | stream_context_get_default => {}, | ||
| 839 | stream_context_set_default => {}, | ||
| 840 | stream_filter_prepend => {}, | ||
| 841 | stream_filter_append => {}, | ||
| 842 | stream_filter_remove => {}, | ||
| 843 | stream_socket_client => {}, | ||
| 844 | stream_socket_server => {}, | ||
| 845 | stream_socket_accept => {}, | ||
| 846 | stream_socket_get_name => {}, | ||
| 847 | stream_socket_recvfrom => {}, | ||
| 848 | stream_socket_sendto => {}, | ||
| 849 | stream_socket_enable_crypto => {}, | ||
| 850 | stream_socket_shutdown => {}, | ||
| 851 | stream_socket_pair => {}, | ||
| 852 | stream_copy_to_stream => {}, | ||
| 853 | stream_get_contents => {}, | ||
| 854 | stream_supports_lock => {}, | ||
| 855 | fgetcsv => {}, | ||
| 856 | fputcsv => {}, | ||
| 857 | flock => {}, | ||
| 858 | get_meta_tags => {}, | ||
| 859 | stream_set_read_buffer => {}, | ||
| 860 | stream_set_write_buffer => {}, | ||
| 861 | set_file_buffer => {}, | ||
| 862 | set_socket_blocking => {}, | ||
| 863 | stream_set_blocking => {}, | ||
| 864 | socket_set_blocking => {}, | ||
| 865 | stream_get_meta_data => {}, | ||
| 866 | stream_get_line => {}, | ||
| 867 | stream_wrapper_register => {}, | ||
| 868 | stream_register_wrapper => {}, | ||
| 869 | stream_wrapper_unregister => {}, | ||
| 870 | stream_wrapper_restore => {}, | ||
| 871 | stream_get_wrappers => {}, | ||
| 872 | stream_get_transports => {}, | ||
| 873 | stream_resolve_include_path => {}, | ||
| 874 | stream_is_local => {}, | ||
| 875 | get_headers => {}, | ||
| 876 | stream_set_timeout => {}, | ||
| 877 | socket_set_timeout => {}, | ||
| 878 | socket_get_status => {}, | ||
| 879 | realpath => {}, | ||
| 880 | fnmatch => {}, | ||
| 881 | fsockopen => {}, | ||
| 882 | pfsockopen => {}, | ||
| 883 | pack => {}, | ||
| 884 | unpack => {}, | ||
| 885 | get_browser => {}, | ||
| 886 | crypt => {}, | ||
| 887 | opendir => {}, | ||
| 888 | closedir => {}, | ||
| 889 | chdir => {}, | ||
| 890 | chroot => {}, | ||
| 891 | getcwd => {}, | ||
| 892 | rewinddir => {}, | ||
| 893 | readdir => {}, | ||
| 894 | dir => {}, | ||
| 895 | scandir => {}, | ||
| 896 | glob => {}, | ||
| 897 | fileatime => {}, | ||
| 898 | filectime => {}, | ||
| 899 | filegroup => {}, | ||
| 900 | fileinode => {}, | ||
| 901 | filemtime => {}, | ||
| 902 | fileowner => {}, | ||
| 903 | fileperms => {}, | ||
| 904 | filesize => {}, | ||
| 905 | filetype => {}, | ||
| 906 | file_exists => {}, | ||
| 907 | is_writable => {}, | ||
| 908 | is_writeable => {}, | ||
| 909 | is_readable => {}, | ||
| 910 | is_executable => {}, | ||
| 911 | is_file => {}, | ||
| 912 | is_dir => {}, | ||
| 913 | is_link => {}, | ||
| 914 | stat => {}, | ||
| 915 | lstat => {}, | ||
| 916 | chown => {}, | ||
| 917 | chgrp => {}, | ||
| 918 | lchown => {}, | ||
| 919 | lchgrp => {}, | ||
| 920 | chmod => {}, | ||
| 921 | touch => {}, | ||
| 922 | clearstatcache => {}, | ||
| 923 | disk_total_space => {}, | ||
| 924 | disk_free_space => {}, | ||
| 925 | diskfreespace => {}, | ||
| 926 | realpath_cache_size => {}, | ||
| 927 | realpath_cache_get => {}, | ||
| 928 | mail => {}, | ||
| 929 | ezmlm_hash => {}, | ||
| 930 | openlog => {}, | ||
| 931 | syslog => {}, | ||
| 932 | closelog => {}, | ||
| 933 | define_syslog_variables => {}, | ||
| 934 | lcg_value => {}, | ||
| 935 | metaphone => {}, | ||
| 936 | ob_start => { cmd => \&PHP::Decode::Transformer::cmd_ob_start, ret => R_BOOL, callable => 1 }, | ||
| 937 | ob_flush => {}, | ||
| 938 | ob_clean => {}, | ||
| 939 | ob_end_flush => { cmd => \&PHP::Decode::Transformer::cmd_ob_end_flush, ret => R_BOOL, callable => 1 }, | ||
| 940 | ob_end_clean => { cmd => \&PHP::Decode::Transformer::cmd_ob_end_clean, ret => R_BOOL }, | ||
| 941 | ob_get_flush => {}, | ||
| 942 | ob_get_clean => {}, | ||
| 943 | ob_get_length => {}, | ||
| 944 | ob_get_level => {}, | ||
| 945 | ob_get_status => {}, | ||
| 946 | ob_get_contents => {}, | ||
| 947 | ob_implicit_flush => {}, | ||
| 948 | ob_list_handlers => {}, | ||
| 949 | ksort => { param => ['#ref0'], ret => R_BOOL }, | ||
| 950 | krsort => { param => ['#ref0'], ret => R_BOOL }, | ||
| 951 | natsort => { param => ['#ref0'], ret => R_BOOL }, | ||
| 952 | natcasesort => { param => ['#ref0'], ret => R_BOOL }, | ||
| 953 | asort => { param => ['#ref0'], ret => R_BOOL }, | ||
| 954 | arsort => { param => ['#ref0'], ret => R_BOOL }, | ||
| 955 | sort => { param => ['#ref0'], ret => R_BOOL }, | ||
| 956 | rsort => { param => ['#ref0'], ret => R_BOOL }, | ||
| 957 | usort => { param => ['#ref0'], ret => R_BOOL, callable => 1 }, | ||
| 958 | uasort => { param => ['#ref0'], ret => R_BOOL, callable => 1 }, | ||
| 959 | uksort => { param => ['#ref0'], ret => R_BOOL, callable => 1 }, | ||
| 960 | shuffle => { param => ['#ref0'], ret => R_BOOL }, | ||
| 961 | array_walk => { param => ['#ref0'], ret => R_BOOL, callable => 1 }, | ||
| 962 | array_walk_recursive => { param => ['#ref0'], ret => R_BOOL, callable => 1 }, | ||
| 963 | count => {}, | ||
| 964 | end => { param => ['#ref0'], ret => R_MIXED }, | ||
| 965 | prev => { param => ['#ref0'], ret => R_MIXED }, | ||
| 966 | next => { param => ['#ref0'], ret => R_MIXED }, | ||
| 967 | reset => { param => ['#ref0'], ret => R_MIXED }, | ||
| 968 | current => {}, | ||
| 969 | key => {}, | ||
| 970 | min => {}, | ||
| 971 | max => {}, | ||
| 972 | in_array => {}, | ||
| 973 | array_search => {}, | ||
| 974 | extract => { param => ['#ref0'], ret => R_INT }, | ||
| 975 | compact => {}, | ||
| 976 | array_fill => {}, | ||
| 977 | array_fill_keys => {}, | ||
| 978 | range => { ret => R_ARRAY }, | ||
| 979 | array_multisort => { param => ['#ref0'], ret => R_BOOL }, | ||
| 980 | array_push => { param => ['#ref0'], ret => R_INT }, | ||
| 981 | array_pop => { param => ['#ref0'], ret => R_MIXED }, | ||
| 982 | array_shift => { param => ['#ref0'], ret => R_MIXED }, | ||
| 983 | array_unshift => { param => ['#ref0'], ret => R_INT }, | ||
| 984 | array_splice => { param => ['#ref0'], ret => R_ARRAY }, | ||
| 985 | array_slice => {}, | ||
| 986 | array_merge => {}, | ||
| 987 | array_merge_recursive => {}, | ||
| 988 | array_replace => {}, | ||
| 989 | array_replace_recursive => {}, | ||
| 990 | array_keys => {}, | ||
| 991 | array_values => {}, | ||
| 992 | array_count_values => {}, | ||
| 993 | array_reverse => {}, | ||
| 994 | array_reduce => {}, | ||
| 995 | array_pad => {}, | ||
| 996 | array_flip => {}, | ||
| 997 | array_change_key_case => {}, | ||
| 998 | array_rand => {}, | ||
| 999 | array_unique => {}, | ||
| 1000 | array_intersect => {}, | ||
| 1001 | array_intersect_key => {}, | ||
| 1002 | array_intersect_ukey => { callable => 1 }, | ||
| 1003 | array_uintersect => { callable => 1 }, | ||
| 1004 | array_intersect_assoc => {}, | ||
| 1005 | array_uintersect_assoc => { callable => 1 }, | ||
| 1006 | array_intersect_uassoc => { callable => 1 }, | ||
| 1007 | array_uintersect_uassoc => { callable => 1 }, | ||
| 1008 | array_diff => {}, | ||
| 1009 | array_diff_key => {}, | ||
| 1010 | array_diff_ukey => { callable => 1 }, | ||
| 1011 | array_udiff => { callable => 1 }, | ||
| 1012 | array_diff_assoc => {}, | ||
| 1013 | array_udiff_assoc => { callable => 1 }, | ||
| 1014 | array_diff_uassoc => { callable => 1 }, | ||
| 1015 | array_udiff_uassoc => { callable => 1 }, | ||
| 1016 | array_sum => {}, | ||
| 1017 | array_product => {}, | ||
| 1018 | array_filter => { callable => 1 }, | ||
| 1019 | array_map => { callable => 1, ret => R_ARRAY }, | ||
| 1020 | array_chunk => {}, | ||
| 1021 | array_combine => {}, | ||
| 1022 | array_key_exists => {}, | ||
| 1023 | pos => {}, | ||
| 1024 | sizeof => {}, | ||
| 1025 | key_exists => {}, | ||
| 1026 | assert => {}, | ||
| 1027 | assert_options => {}, | ||
| 1028 | version_compare => {}, | ||
| 1029 | ftok => {}, | ||
| 1030 | str_rot13 => {}, | ||
| 1031 | stream_get_filters => {}, | ||
| 1032 | stream_filter_register => {}, | ||
| 1033 | stream_bucket_make_writeable => {}, | ||
| 1034 | stream_bucket_prepend => {}, | ||
| 1035 | stream_bucket_append => {}, | ||
| 1036 | stream_bucket_new => {}, | ||
| 1037 | output_add_rewrite_var => {}, | ||
| 1038 | output_reset_rewrite_vars => {}, | ||
| 1039 | sys_get_temp_dir => {}, | ||
| 1040 | ); | ||
| 1041 | |||
| 1042 | my %php_funcs_date = ( | ||
| 1043 | strtotime => {}, | ||
| 1044 | date => {}, | ||
| 1045 | idate => {}, | ||
| 1046 | gmdate => {}, | ||
| 1047 | mktime => {}, | ||
| 1048 | gmmktime => {}, | ||
| 1049 | checkdate => {}, | ||
| 1050 | strftime => {}, | ||
| 1051 | gmstrftime => {}, | ||
| 1052 | time => {}, | ||
| 1053 | localtime => {}, | ||
| 1054 | getdate => {}, | ||
| 1055 | date_create => {}, | ||
| 1056 | date_create_from_format => {}, | ||
| 1057 | date_parse => {}, | ||
| 1058 | date_parse_from_format => {}, | ||
| 1059 | date_get_last_errors => {}, | ||
| 1060 | date_format => {}, | ||
| 1061 | date_modify => {}, | ||
| 1062 | date_add => {}, | ||
| 1063 | date_sub => {}, | ||
| 1064 | date_timezone_get => {}, | ||
| 1065 | date_timezone_set => {}, | ||
| 1066 | date_offset_get => {}, | ||
| 1067 | date_diff => {}, | ||
| 1068 | date_time_set => {}, | ||
| 1069 | date_date_set => {}, | ||
| 1070 | date_isodate_set => {}, | ||
| 1071 | date_timestamp_set => {}, | ||
| 1072 | date_timestamp_get => {}, | ||
| 1073 | timezone_open => {}, | ||
| 1074 | timezone_name_get => {}, | ||
| 1075 | timezone_name_from_abbr => {}, | ||
| 1076 | timezone_offset_get => {}, | ||
| 1077 | timezone_transitions_get => {}, | ||
| 1078 | timezone_location_get => {}, | ||
| 1079 | timezone_identifiers_list => {}, | ||
| 1080 | timezone_abbreviations_list => {}, | ||
| 1081 | timezone_version_get => {}, | ||
| 1082 | date_interval_create_from_date_string => {}, | ||
| 1083 | date_interval_format => {}, | ||
| 1084 | date_default_timezone_set => {}, | ||
| 1085 | date_default_timezone_get => {}, | ||
| 1086 | date_sunrise => {}, | ||
| 1087 | date_sunset => {}, | ||
| 1088 | date_sun_info => {}, | ||
| 1089 | ); | ||
| 1090 | |||
| 1091 | my %php_funcs_pcre = ( | ||
| 1092 | preg_match => {}, | ||
| 1093 | preg_match_all => {}, | ||
| 1094 | preg_replace => { ret => R_STR|R_ARRAY }, | ||
| 1095 | preg_replace_callback => { callable => 1, ret => R_STR|R_ARRAY }, | ||
| 1096 | preg_replace_callback_array => { callable => 1, ret => R_STR|R_ARRAY }, | ||
| 1097 | preg_filter => {}, | ||
| 1098 | preg_split => { ret => R_ARRAY }, | ||
| 1099 | preg_quote => {}, | ||
| 1100 | preg_grep => {}, | ||
| 1101 | preg_last_error => {}, | ||
| 1102 | ); | ||
| 1103 | |||
| 1104 | my %php_funcs_posix = ( | ||
| 1105 | posix_kill => {}, | ||
| 1106 | posix_getpid => {}, | ||
| 1107 | posix_getppid => {}, | ||
| 1108 | posix_getuid => {}, | ||
| 1109 | posix_setuid => {}, | ||
| 1110 | posix_geteuid => {}, | ||
| 1111 | posix_seteuid => {}, | ||
| 1112 | posix_getgid => {}, | ||
| 1113 | posix_setgid => {}, | ||
| 1114 | posix_getegid => {}, | ||
| 1115 | posix_setegid => {}, | ||
| 1116 | posix_getgroups => {}, | ||
| 1117 | posix_getlogin => {}, | ||
| 1118 | posix_getpgrp => {}, | ||
| 1119 | posix_setsid => {}, | ||
| 1120 | posix_setpgid => {}, | ||
| 1121 | posix_getpgid => {}, | ||
| 1122 | posix_getsid => {}, | ||
| 1123 | posix_uname => {}, | ||
| 1124 | posix_times => {}, | ||
| 1125 | posix_ctermid => {}, | ||
| 1126 | posix_ttyname => {}, | ||
| 1127 | posix_isatty => {}, | ||
| 1128 | posix_getcwd => {}, | ||
| 1129 | posix_mkfifo => {}, | ||
| 1130 | posix_mknod => {}, | ||
| 1131 | posix_access => {}, | ||
| 1132 | posix_getgrnam => {}, | ||
| 1133 | posix_getgrgid => {}, | ||
| 1134 | posix_getpwnam => {}, | ||
| 1135 | posix_getpwuid => {}, | ||
| 1136 | posix_getrlimit => {}, | ||
| 1137 | posix_get_last_error => {}, | ||
| 1138 | posix_errno => {}, | ||
| 1139 | posix_strerror => {}, | ||
| 1140 | posix_initgroups => {}, | ||
| 1141 | ); | ||
| 1142 | |||
| 1143 | my %php_funcs_curl = ( | ||
| 1144 | curl_init => {}, | ||
| 1145 | curl_copy_handle => {}, | ||
| 1146 | curl_version => {}, | ||
| 1147 | curl_setopt => {}, | ||
| 1148 | curl_setopt_array => {}, | ||
| 1149 | curl_exec => {}, | ||
| 1150 | curl_getinfo => {}, | ||
| 1151 | curl_error => {}, | ||
| 1152 | curl_errno => {}, | ||
| 1153 | curl_close => {}, | ||
| 1154 | curl_multi_init => {}, | ||
| 1155 | curl_multi_add_handle => {}, | ||
| 1156 | curl_multi_remove_handle => {}, | ||
| 1157 | curl_multi_select => {}, | ||
| 1158 | curl_multi_exec => {}, | ||
| 1159 | curl_multi_getcontent => {}, | ||
| 1160 | curl_multi_info_read => {}, | ||
| 1161 | curl_multi_close => {}, | ||
| 1162 | ); | ||
| 1163 | |||
| 1164 | my %php_funcs_zlib = ( | ||
| 1165 | readgzfile => {}, | ||
| 1166 | gzrewind => {}, | ||
| 1167 | gzclose => {}, | ||
| 1168 | gzeof => {}, | ||
| 1169 | gzgetc => {}, | ||
| 1170 | gzgets => {}, | ||
| 1171 | gzgetss => {}, | ||
| 1172 | gzread => {}, | ||
| 1173 | gzopen => {}, | ||
| 1174 | gzpassthru => {}, | ||
| 1175 | gzseek => {}, | ||
| 1176 | gztell => {}, | ||
| 1177 | gzwrite => {}, | ||
| 1178 | gzputs => {}, | ||
| 1179 | gzfile => {}, | ||
| 1180 | gzcompress => {}, | ||
| 1181 | gzuncompress => {}, | ||
| 1182 | gzdeflate => {}, | ||
| 1183 | gzinflate => {}, | ||
| 1184 | gzdecode => {}, | ||
| 1185 | gzencode => {}, | ||
| 1186 | ob_gzhandler => {}, | ||
| 1187 | zlib_get_coding_type => {}, | ||
| 1188 | ); | ||
| 1189 | |||
| 1190 | my %php_funcs_mysql = ( | ||
| 1191 | mysql_connect => {}, | ||
| 1192 | mysql_pconnect => {}, | ||
| 1193 | mysql_close => {}, | ||
| 1194 | mysql_select_db => {}, | ||
| 1195 | mysql_query => {}, | ||
| 1196 | mysql_unbuffered_query => {}, | ||
| 1197 | mysql_db_query => {}, | ||
| 1198 | mysql_list_dbs => {}, | ||
| 1199 | mysql_list_tables => {}, | ||
| 1200 | mysql_list_fields => {}, | ||
| 1201 | mysql_list_processes => {}, | ||
| 1202 | mysql_error => {}, | ||
| 1203 | mysql_errno => {}, | ||
| 1204 | mysql_affected_rows => {}, | ||
| 1205 | mysql_insert_id => {}, | ||
| 1206 | mysql_result => {}, | ||
| 1207 | mysql_num_rows => {}, | ||
| 1208 | mysql_num_fields => {}, | ||
| 1209 | mysql_fetch_row => {}, | ||
| 1210 | mysql_fetch_array => {}, | ||
| 1211 | mysql_fetch_assoc => {}, | ||
| 1212 | mysql_fetch_object => {}, | ||
| 1213 | mysql_data_seek => {}, | ||
| 1214 | mysql_fetch_lengths => {}, | ||
| 1215 | mysql_fetch_field => {}, | ||
| 1216 | mysql_field_seek => {}, | ||
| 1217 | mysql_free_result => {}, | ||
| 1218 | mysql_field_name => {}, | ||
| 1219 | mysql_field_table => {}, | ||
| 1220 | mysql_field_len => {}, | ||
| 1221 | mysql_field_type => {}, | ||
| 1222 | mysql_field_flags => {}, | ||
| 1223 | mysql_escape_string => {}, | ||
| 1224 | mysql_real_escape_string => {}, | ||
| 1225 | mysql_stat => {}, | ||
| 1226 | mysql_thread_id => {}, | ||
| 1227 | mysql_client_encoding => {}, | ||
| 1228 | mysql_ping => {}, | ||
| 1229 | mysql_get_client_info => {}, | ||
| 1230 | mysql_get_host_info => {}, | ||
| 1231 | mysql_get_proto_info => {}, | ||
| 1232 | mysql_get_server_info => {}, | ||
| 1233 | mysql_info => {}, | ||
| 1234 | mysql_set_charset => {}, | ||
| 1235 | mysql => {}, | ||
| 1236 | mysql_fieldname => {}, | ||
| 1237 | mysql_fieldtable => {}, | ||
| 1238 | mysql_fieldlen => {}, | ||
| 1239 | mysql_fieldtype => {}, | ||
| 1240 | mysql_fieldflags => {}, | ||
| 1241 | mysql_selectdb => {}, | ||
| 1242 | mysql_freeresult => {}, | ||
| 1243 | mysql_numfields => {}, | ||
| 1244 | mysql_numrows => {}, | ||
| 1245 | mysql_listdbs => {}, | ||
| 1246 | mysql_listtables => {}, | ||
| 1247 | mysql_listfields => {}, | ||
| 1248 | mysql_db_name => {}, | ||
| 1249 | mysql_dbname => {}, | ||
| 1250 | mysql_tablename => {}, | ||
| 1251 | mysql_table_name => {}, | ||
| 1252 | ); | ||
| 1253 | |||
| 1254 | my %php_funcs_hash = ( | ||
| 1255 | hash => { ret => R_STR }, | ||
| 1256 | hash_file => {}, | ||
| 1257 | hash_hmac => {}, | ||
| 1258 | hash_hmac_file => {}, | ||
| 1259 | hash_init => {}, | ||
| 1260 | hash_update => {}, | ||
| 1261 | hash_update_stream => {}, | ||
| 1262 | hash_update_file => {}, | ||
| 1263 | hash_final => {}, | ||
| 1264 | hash_copy => {}, | ||
| 1265 | hash_algos => {}, | ||
| 1266 | hash_hmac_algos => {}, | ||
| 1267 | hash_pbkdf2 => {}, | ||
| 1268 | hash_equals => {}, | ||
| 1269 | hash_hkdf => {}, | ||
| 1270 | mhash_get_block_size => {}, | ||
| 1271 | mhash_get_hash_name => {}, | ||
| 1272 | mhash_keygen_s2k => {}, | ||
| 1273 | mhash_count => {}, | ||
| 1274 | mhash => {}, | ||
| 1275 | ); | ||
| 1276 | |||
| 1277 | my %php_funcs_libxml = ( | ||
| 1278 | libxml_set_streams_context => {}, | ||
| 1279 | libxml_use_internal_errors => {}, | ||
| 1280 | libxml_get_last_error => {}, | ||
| 1281 | libxml_get_errors => {}, | ||
| 1282 | libxml_clear_errors => {}, | ||
| 1283 | libxml_disable_entity_loader => {}, | ||
| 1284 | libxml_set_external_entity_loader => {}, | ||
| 1285 | ); | ||
| 1286 | |||
| 1287 | my %php_funcs_filter = ( | ||
| 1288 | filter_has_var => { ret => R_BOOL }, | ||
| 1289 | filter_input => { ret => R_MIXED }, | ||
| 1290 | filter_var => { ret => R_MIXED }, | ||
| 1291 | filter_input_array => { ret => R_MIXED }, | ||
| 1292 | filter_var_array => { ret => R_ARRAY|R_BOOL }, | ||
| 1293 | filter_list => { ret => R_ARRAY }, | ||
| 1294 | filter_id => { ret => R_INT|R_BOOL }, | ||
| 1295 | ); | ||
| 1296 | |||
| 1297 | my %php_funcs_json = ( | ||
| 1298 | json_encode => { ret => R_STR }, | ||
| 1299 | json_decode => { ret => R_MIXED }, | ||
| 1300 | json_last_error => { ret => R_INT }, | ||
| 1301 | json_last_error_msg => { ret => R_STR }, | ||
| 1302 | ); | ||
| 1303 | |||
| 1304 | my %php_funcs_spl = ( | ||
| 1305 | class_implements => {}, | ||
| 1306 | class_parents => {}, | ||
| 1307 | class_uses => {}, | ||
| 1308 | spl_autoload => {}, | ||
| 1309 | spl_autoload_call => {}, | ||
| 1310 | spl_autoload_extensions => {}, | ||
| 1311 | spl_autoload_functions => {}, | ||
| 1312 | spl_autoload_register => {}, | ||
| 1313 | spl_autoload_unregister => {}, | ||
| 1314 | spl_classes => {}, | ||
| 1315 | spl_object_hash => {}, | ||
| 1316 | spl_object_id => {}, | ||
| 1317 | iterator_apply => {}, | ||
| 1318 | iterator_count => {}, | ||
| 1319 | iterator_to_array => {}, | ||
| 1320 | ); | ||
| 1321 | |||
| 1322 | my %php_funcs_session = ( | ||
| 1323 | session_name => { ret => R_STR }, | ||
| 1324 | session_module_name => { ret => R_STR }, | ||
| 1325 | session_save_path => { ret => R_STR }, | ||
| 1326 | session_id => { ret => R_STR }, | ||
| 1327 | session_create_id => { ret => R_STR }, | ||
| 1328 | session_regenerate_id => { ret => R_BOOL }, | ||
| 1329 | session_decode => { ret => R_BOOL }, | ||
| 1330 | session_encode => { ret => R_STR }, | ||
| 1331 | session_destroy => { ret => R_BOOL }, | ||
| 1332 | session_unset => { ret => R_BOOL }, | ||
| 1333 | session_gc => { ret => R_INT }, | ||
| 1334 | session_get_cookie_params => { ret => R_BOOL }, | ||
| 1335 | session_write_close => { ret => R_BOOL }, | ||
| 1336 | session_abort => { ret => R_BOOL }, | ||
| 1337 | session_reset => { ret => R_BOOL }, | ||
| 1338 | session_status => { ret => R_INT }, | ||
| 1339 | session_register_shutdown => { ret => R_VOID }, | ||
| 1340 | session_commit => { ret => R_BOOL }, | ||
| 1341 | session_set_save_handler => { ret => R_BOOL }, | ||
| 1342 | session_cache_limiter => { ret => R_STR }, | ||
| 1343 | session_cache_expire => { ret => R_INT }, | ||
| 1344 | session_set_cookie_params => { ret => R_ARRAY }, | ||
| 1345 | session_start => { ret => R_BOOL }, | ||
| 1346 | ); | ||
| 1347 | |||
| 1348 | my %php_funcs_pdo = ( | ||
| 1349 | pdo_drivers => {}, | ||
| 1350 | ); | ||
| 1351 | |||
| 1352 | my %php_funcs_xml = ( | ||
| 1353 | xml_parser_create => {}, | ||
| 1354 | xml_parser_create_ns => {}, | ||
| 1355 | xml_set_object => {}, | ||
| 1356 | xml_set_element_handler => {}, | ||
| 1357 | xml_set_character_data_handler => {}, | ||
| 1358 | xml_set_processing_instruction_handler => {}, | ||
| 1359 | xml_set_default_handler => {}, | ||
| 1360 | xml_set_unparsed_entity_decl_handler => {}, | ||
| 1361 | xml_set_notation_decl_handler => {}, | ||
| 1362 | xml_set_external_entity_ref_handler => {}, | ||
| 1363 | xml_set_start_namespace_decl_handler => {}, | ||
| 1364 | xml_set_end_namespace_decl_handler => {}, | ||
| 1365 | xml_parse => {}, | ||
| 1366 | xml_parse_into_struct => {}, | ||
| 1367 | xml_get_error_code => {}, | ||
| 1368 | xml_error_string => {}, | ||
| 1369 | xml_get_current_line_number => {}, | ||
| 1370 | xml_get_current_column_number => {}, | ||
| 1371 | xml_get_current_byte_index => {}, | ||
| 1372 | xml_parser_free => {}, | ||
| 1373 | xml_parser_set_option => {}, | ||
| 1374 | xml_parser_get_option => {}, | ||
| 1375 | ); | ||
| 1376 | |||
| 1377 | my %php_funcs_calendar = ( | ||
| 1378 | cal_days_in_month => {}, | ||
| 1379 | cal_from_jd => {}, | ||
| 1380 | cal_info => {}, | ||
| 1381 | cal_to_jd => {}, | ||
| 1382 | easter_date => {}, | ||
| 1383 | easter_days => {}, | ||
| 1384 | frenchtojd => {}, | ||
| 1385 | gregoriantojd => {}, | ||
| 1386 | jddayofweek => {}, | ||
| 1387 | jdmonthname => {}, | ||
| 1388 | jdtofrench => {}, | ||
| 1389 | jdtogregorian => {}, | ||
| 1390 | jdtojewish => {}, | ||
| 1391 | jdtojulian => {}, | ||
| 1392 | jdtounix => {}, | ||
| 1393 | jewishtojd => {}, | ||
| 1394 | juliantojd => {}, | ||
| 1395 | unixtojd => {}, | ||
| 1396 | ); | ||
| 1397 | |||
| 1398 | my %php_funcs_ctype = ( | ||
| 1399 | ctype_alnum => { ret => R_BOOL }, | ||
| 1400 | ctype_alpha => { ret => R_BOOL }, | ||
| 1401 | ctype_cntrl => { ret => R_BOOL }, | ||
| 1402 | ctype_digit => { ret => R_BOOL }, | ||
| 1403 | ctype_lower => { ret => R_BOOL }, | ||
| 1404 | ctype_graph => { ret => R_BOOL }, | ||
| 1405 | ctype_print => { ret => R_BOOL }, | ||
| 1406 | ctype_punct => { ret => R_BOOL }, | ||
| 1407 | ctype_space => { ret => R_BOOL }, | ||
| 1408 | ctype_upper => { ret => R_BOOL }, | ||
| 1409 | ctype_xdigit => { ret => R_BOOL }, | ||
| 1410 | ); | ||
| 1411 | |||
| 1412 | my %php_funcs_gettext = ( | ||
| 1413 | textdomain => { ret => R_STR }, | ||
| 1414 | gettext => { ret => R_STR }, | ||
| 1415 | '_' => { ret => R_STR }, | ||
| 1416 | dgettext => { ret => R_STR }, | ||
| 1417 | dcgettext => { ret => R_STR }, | ||
| 1418 | bindtextdomain => { ret => R_STR }, | ||
| 1419 | ngettext => { ret => R_STR}, | ||
| 1420 | dngettext => { ret => R_STR }, | ||
| 1421 | dcngettext => { ret => R_STR }, | ||
| 1422 | bind_textdomain_codeset => { ret => R_STR }, | ||
| 1423 | ); | ||
| 1424 | |||
| 1425 | my %php_funcs_iconv = ( | ||
| 1426 | iconv_strlen => { ret => R_INT }, | ||
| 1427 | iconv_substr => { ret => R_STR }, | ||
| 1428 | iconv_strpos => { ret => R_INT }, | ||
| 1429 | iconv_strrpos => { ret => R_INT }, | ||
| 1430 | iconv_mime_encode => { ret => R_STR }, | ||
| 1431 | iconv_mime_decode => { ret => R_STR }, | ||
| 1432 | iconv_mime_decode_headers => { ret => R_STR|R_ARRAY }, | ||
| 1433 | iconv => { ret => R_STR }, | ||
| 1434 | iconv_set_encoding => { ret => R_BOOL }, | ||
| 1435 | iconv_get_encoding => { ret => R_STR|R_ARRAY }, | ||
| 1436 | ob_iconv_handler => { callable => 1, ret => R_STR }, | ||
| 1437 | ); | ||
| 1438 | |||
| 1439 | my %php_funcs_socket = ( | ||
| 1440 | socket_select => {}, | ||
| 1441 | socket_create_listen => {}, | ||
| 1442 | socket_accept => {}, | ||
| 1443 | socket_set_nonblock => {}, | ||
| 1444 | socket_set_block => {}, | ||
| 1445 | socket_listen => {}, | ||
| 1446 | socket_close => {}, | ||
| 1447 | socket_write => {}, | ||
| 1448 | socket_read => { ret => R_STR }, | ||
| 1449 | socket_getsockname => {}, | ||
| 1450 | socket_getpeername => {}, | ||
| 1451 | socket_create => {}, | ||
| 1452 | socket_connect => {}, | ||
| 1453 | socket_strerror => {}, | ||
| 1454 | socket_bind => {}, | ||
| 1455 | socket_recv => {}, | ||
| 1456 | socket_send => {}, | ||
| 1457 | socket_recvfrom => {}, | ||
| 1458 | socket_sendto => {}, | ||
| 1459 | socket_get_option => {}, | ||
| 1460 | socket_getopt => {}, | ||
| 1461 | socket_set_option => {}, | ||
| 1462 | socket_setopt => {}, | ||
| 1463 | socket_create_pair => {}, | ||
| 1464 | socket_shutdown => {}, | ||
| 1465 | socket_last_error => {}, | ||
| 1466 | socket_clear_error => {}, | ||
| 1467 | socket_import_stream => {}, | ||
| 1468 | socket_export_stream => {}, | ||
| 1469 | socket_sendmsg => {}, | ||
| 1470 | socket_recvmsg => {}, | ||
| 1471 | socket_cmsg_space => {}, | ||
| 1472 | socket_addrinfo_lookup => {}, | ||
| 1473 | socket_addrinfo_connect => {}, | ||
| 1474 | socket_addrinfo_bind => {}, | ||
| 1475 | socket_addrinfo_explain => {}, | ||
| 1476 | ); | ||
| 1477 | |||
| 1478 | my %php_funcs_tokenizer = ( | ||
| 1479 | token_get_all => {}, | ||
| 1480 | token_name => {}, | ||
| 1481 | ); | ||
| 1482 | |||
| 1483 | # https://developer.wordpress.org/reference/functions/ | ||
| 1484 | # | ||
| 1485 | my %php_funcs_wordpress = ( | ||
| 1486 | get_option => { ret => R_MIXED }, | ||
| 1487 | update_option => { ret => R_BOOL }, | ||
| 1488 | add_post_meta => { ret => R_INT }, | ||
| 1489 | get_categories => { ret => R_ARRAY }, | ||
| 1490 | wp_add_post_tags => { ret => R_ARRAY }, | ||
| 1491 | wp_create_category => { ret => R_INT }, | ||
| 1492 | get_posts => { ret => R_MIXED }, | ||
| 1493 | get_post => { ret => R_MIXED }, | ||
| 1494 | wp_insert_post => { ret => R_INT }, | ||
| 1495 | wp_update_post => { ret => R_INT }, | ||
| 1496 | wp_delete_post => { ret => R_MIXED }, | ||
| 1497 | wp_set_post_tags => { ret => R_ARRAY }, | ||
| 1498 | get_post_meta => { ret => R_MIXED }, | ||
| 1499 | add_post_meta => { ret => R_INT }, | ||
| 1500 | update_post_meta => { ret => R_INT }, | ||
| 1501 | delete_post_meta => { ret => R_BOOL }, | ||
| 1502 | get_users => { ret => R_ARRAY }, | ||
| 1503 | get_user_by => { ret => R_MIXED }, | ||
| 1504 | get_userdata => { ret => R_MIXED }, | ||
| 1505 | wp_insert_user => { ret => R_INT }, | ||
| 1506 | wp_update_user => { ret => R_INT }, | ||
| 1507 | wp_delete_user => { ret => R_BOOL }, | ||
| 1508 | get_user_meta => { ret => R_MIXED }, | ||
| 1509 | add_user_meta => { ret => R_INT }, | ||
| 1510 | update_user_meta => { ret => R_INT }, | ||
| 1511 | delete_user_meta => { ret => R_BOOL }, | ||
| 1512 | get_user_setting => { ret => R_MIXED }, | ||
| 1513 | delete_user_setting => { ret => R_BOOL }, | ||
| 1514 | wp_schedule_single_event => { ret => R_BOOL }, | ||
| 1515 | wp_clear_scheduled_hook => { ret => R_INT }, | ||
| 1516 | wp_unschedule_event => { ret => R_BOOL }, | ||
| 1517 | wp_upload_dir => { ret => R_ARRAY }, | ||
| 1518 | ); | ||
| 1519 | |||
| 1520 | my %php_funcs = ( | ||
| 1521 | Core => \%php_funcs_core, | ||
| 1522 | standard => \%php_funcs_standard, | ||
| 1523 | date => \%php_funcs_date, | ||
| 1524 | pcre => \%php_funcs_pcre, | ||
| 1525 | posix => \%php_funcs_posix, | ||
| 1526 | curl => \%php_funcs_curl, | ||
| 1527 | zlib => \%php_funcs_zlib, | ||
| 1528 | mysql => \%php_funcs_mysql, | ||
| 1529 | hash => \%php_funcs_hash, | ||
| 1530 | libxml => \%php_funcs_libxml, | ||
| 1531 | filter => \%php_funcs_filter, | ||
| 1532 | json => \%php_funcs_json, | ||
| 1533 | spl => \%php_funcs_spl, | ||
| 1534 | session => \%php_funcs_session, | ||
| 1535 | pdo => \%php_funcs_pdo, | ||
| 1536 | xml => \%php_funcs_xml, | ||
| 1537 | calendar => \%php_funcs_calendar, | ||
| 1538 | ctype => \%php_funcs_ctype, | ||
| 1539 | gettext => \%php_funcs_gettext, | ||
| 1540 | iconv => \%php_funcs_iconv, | ||
| 1541 | socket => \%php_funcs_socket, | ||
| 1542 | tokenizer=> \%php_funcs_tokenizer, | ||
| 1543 | #wordpress=> \%php_funcs_wordpress, | ||
| 1544 | ); | ||
| 1545 | |||
| 1546 | sub get_php_func { | ||
| 1547 | my ($cmd) = @_; | ||
| 1548 | |||
| 1549 | $cmd = lc($cmd); | ||
| 1550 | |||
| 1551 | foreach my $e (keys %php_funcs) { | ||
| 1552 | if (exists $php_funcs{$e}{$cmd}) { | ||
| 1553 | my $f = $php_funcs{$e}{$cmd}; | ||
| 1554 | return $f; | ||
| 1555 | } | ||
| 1556 | } | ||
| 1557 | return | ||
| 1558 | } | ||
| 1559 | |||
| 1560 | sub func_may_call_callbacks { | ||
| 1561 | my ($cmd) = @_; | ||
| 1562 | |||
| 1563 | $cmd = lc($cmd); | ||
| 1564 | |||
| 1565 | if ($cmd =~ /^(eval|include|include_once|require|require_once)$/) { | ||
| 1566 | return 1; | ||
| 1567 | } | ||
| 1568 | my $f = get_php_func($cmd); | ||
| 1569 | if (defined $f) { | ||
| 1570 | if (exists $f->{callable}) { | ||
| 1571 | return 1; | ||
| 1572 | } | ||
| 1573 | return 0; | ||
| 1574 | } | ||
| 1575 | return 1; | ||
| 1576 | } | ||
| 1577 | |||
| 1578 | sub func_may_return_string { | ||
| 1579 | my ($cmd) = @_; | ||
| 1580 | |||
| 1581 | $cmd = lc($cmd); | ||
| 1582 | |||
| 1583 | if ($cmd =~ /^(include|include_once|require|require_once)$/) { | ||
| 1584 | return 0; | ||
| 1585 | } | ||
| 1586 | my $f = get_php_func($cmd); | ||
| 1587 | if (defined $f) { | ||
| 1588 | if (exists $f->{ret}) { | ||
| 1589 | unless ($f->{ret} & (R_STR|R_MIXED)) { | ||
| 1590 | return 0; | ||
| 1591 | } | ||
| 1592 | if ($f->{ret} & R_FIX) { | ||
| 1593 | return 0; | ||
| 1594 | } | ||
| 1595 | } | ||
| 1596 | return 1; | ||
| 1597 | } | ||
| 1598 | return 1; | ||
| 1599 | } | ||
| 1600 | |||
| 1601 | sub cmd_base64_decode { | ||
| 1602 | my ($ctx, $cmd, $args) = @_; | ||
| 1603 | my $parser = $ctx->{parser}; | ||
| 1604 | |||
| 1605 | if (scalar @$args >= 1) { | ||
| 1606 | my $s = $parser->get_strval($$args[0]); | ||
| 1607 | my $php50_compat; | ||
| 1608 | my $strict = 0; | ||
| 1609 | if (scalar @$args > 1) { | ||
| 1610 | $strict = $parser->get_strval($$args[1]); | ||
| 1611 | } | ||
| 1612 | if (defined $s) { | ||
| 1613 | # older php-versions (< php5.1) treated blanks in base64 string as '+': | ||
| 1614 | # https://php.net/manual/en/function.base64-decode.php | ||
| 1615 | # | ||
| 1616 | # perl ignores any character not part of the 65-character base64 subset. | ||
| 1617 | # https://perldoc.perl.org/MIME::Base64 | ||
| 1618 | # | ||
| 1619 | if ($php50_compat) { | ||
| 1620 | $s =~ s/ /+/g; # incompatible with ignore-rule | ||
| 1621 | } | ||
| 1622 | if ($strict && !($s =~ /^[A-Za-z0-9\/+]*=*$/)) { | ||
| 1623 | $ctx->{warn}->($ctx, 'cmd', $cmd, "$$args[0] contains non-strict chars"); | ||
| 1624 | } | ||
| 1625 | my $decoded; | ||
| 1626 | { | ||
| 1627 | # suppress decode_base64() warnings (premature end of data, etc.) | ||
| 1628 | local $^W = 0; | ||
| 1629 | $decoded = decode_base64($s); | ||
| 1630 | } | ||
| 1631 | if (defined $decoded) { | ||
| 1632 | #$decoded = decode_input($decoded); # might convert wide strings back to internal perl utf-8 encoding | ||
| 1633 | #$ctx->{log}->($ctx, 'cmd', $cmd, "%d:[%s]->%d:[%s]", length($s), $parser->shortstr($s,40), length($decoded), $parser->shortstr($decoded,40)) if $ctx->{log}; | ||
| 1634 | return $parser->setstr($decoded); | ||
| 1635 | } | ||
| 1636 | } | ||
| 1637 | } | ||
| 1638 | return; | ||
| 1639 | } | ||
| 1640 | |||
| 1641 | sub cmd_base64_encode { | ||
| 1642 | my ($ctx, $cmd, $args) = @_; | ||
| 1643 | my $parser = $ctx->{parser}; | ||
| 1644 | |||
| 1645 | if (scalar @$args == 1) { | ||
| 1646 | my $s = $parser->get_strval($$args[0]); | ||
| 1647 | if (defined $s) { | ||
| 1648 | # might convert from internal perl wide string representation to utf-8 stream | ||
| 1649 | # see: https://perldoc.perl.org/MIME::Base64 | ||
| 1650 | # | ||
| 1651 | #my $bytestr = encode('utf-8', $s); | ||
| 1652 | #my $encoded = encode_base64($bytestr,''); | ||
| 1653 | my $encoded = encode_base64($s,''); | ||
| 1654 | if (defined $encoded) { | ||
| 1655 | $ctx->{log}->($ctx, 'cmd', $cmd, "%d:[%s]->%d:[%s]", length($s), $parser->shortstr($s,40), length($encoded), $parser->shortstr($encoded,40)) if $ctx->{log}; | ||
| 1656 | return $parser->setstr($encoded); | ||
| 1657 | } | ||
| 1658 | } | ||
| 1659 | } | ||
| 1660 | return; | ||
| 1661 | } | ||
| 1662 | |||
| 1663 | sub get_refvar_val { | ||
| 1664 | my ($ctx, $var) = @_; | ||
| 1665 | my $parser = $ctx->{parser}; | ||
| 1666 | |||
| 1667 | unless ($ctx->{infunction} && !$ctx->{incall}) { | ||
| 1668 | if (is_variable($var)) { | ||
| 1669 | my $val = $ctx->getvar($var, 1); | ||
| 1670 | if (!defined $val && $ctx->is_superglobal($var)) { | ||
| 1671 | return; # unknown contents | ||
| 1672 | } | ||
| 1673 | return (1, $val); # valid | ||
| 1674 | } elsif ($var =~ /^(\#elem\d+)$/) { | ||
| 1675 | my ($v, $i) = @{$parser->{strmap}{$var}}; | ||
| 1676 | my ($basevar, $has_index, $idxstr) = $ctx->resolve_variable($var, 0); | ||
| 1677 | if ($has_index) { | ||
| 1678 | my $basestr = $ctx->exec_statement($basevar, 0); | ||
| 1679 | if (defined $basestr && is_array($basestr) && defined $idxstr) { | ||
| 1680 | $idxstr = $parser->setstr('') if is_null($idxstr); # null maps to '' array index | ||
| 1681 | my $arr = $parser->{strmap}{$basestr}; | ||
| 1682 | my $val = $arr->get($idxstr); | ||
| 1683 | return (1, $val); # valid | ||
| 1684 | } | ||
| 1685 | } | ||
| 1686 | } | ||
| 1687 | } | ||
| 1688 | return; | ||
| 1689 | } | ||
| 1690 | |||
| 1691 | sub set_refvar_val { | ||
| 1692 | my ($ctx, $var, $val) = @_; | ||
| 1693 | my $parser = $ctx->{parser}; | ||
| 1694 | |||
| 1695 | if (is_variable($var)) { | ||
| 1696 | $ctx->setvar($var, $val, 1); | ||
| 1697 | } elsif ($var =~ /^(\#elem\d+)$/) { | ||
| 1698 | my $sub = $parser->setexpr('=', $var, $val); | ||
| 1699 | my $had_assigns = $ctx->have_assignments(); | ||
| 1700 | my $k = $ctx->exec_statement($sub); | ||
| 1701 | if (!$had_assigns) { | ||
| 1702 | # remove any pending assignments, to avoid variable | ||
| 1703 | # insertion for the 'sub'-assignmenmt. | ||
| 1704 | # The full array assignment is inserted after the loop | ||
| 1705 | # | ||
| 1706 | $ctx->discard_pending_assignments(); | ||
| 1707 | } | ||
| 1708 | } | ||
| 1709 | return; | ||
| 1710 | } | ||
| 1711 | |||
| 1712 | # check for php numeric string | ||
| 1713 | # https://www.php.net/manual/en/language.types.numeric-strings.php | ||
| 1714 | # | ||
| 1715 | sub is_numeric_str { | ||
| 1716 | my ($s) = @_; | ||
| 1717 | return $s =~ /^\s*[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?\s*$/; | ||
| 1718 | } | ||
| 1719 | |||
| 1720 | # integer steps from $start to $end (might be descending) | ||
| 1721 | # | ||
| 1722 | sub range_steps { | ||
| 1723 | my ($start, $end, $step) = @_; | ||
| 1724 | my $dir = ($start <= $end) ? $step : -$step; | ||
| 1725 | my $cnt = int(abs($end - $start) / $step); | ||
| 1726 | |||
| 1727 | return map { $start + $_ * $dir } (0 .. $cnt); | ||
| 1728 | } | ||
| 1729 | |||
| 1730 | sub exec_cmd { | ||
| 1731 | my ($ctx, $cmd, $args) = @_; | ||
| 1732 | my $parser = $ctx->{parser}; | ||
| 1733 | my $res; | ||
| 1734 | my $to_num = 0; | ||
| 1735 | |||
| 1736 | # some dynamic function names are created from mixed-style strings | ||
| 1737 | # see: https://www.php.net/manual/en/functions.user-defined.php | ||
| 1738 | # function names are case-insensitive for the ASCII characters A to Z. | ||
| 1739 | # | ||
| 1740 | $cmd = lc($cmd); | ||
| 1741 | |||
| 1742 | $ctx->{log}->($ctx, 'cmd', $cmd, "args (%s)", join(', ', @$args)) if $ctx->{log}; | ||
| 1743 | |||
| 1744 | my $f = get_php_func($cmd); | ||
| 1745 | |||
| 1746 | if (defined $f && exists $f->{cmd}) { | ||
| 1747 | my $k = &{$f->{cmd}}(@_); | ||
| 1748 | return $k; | ||
| 1749 | } | ||
| 1750 | |||
| 1751 | if ($cmd eq 'unescape') { | ||
| 1752 | if (scalar @$args == 1) { | ||
| 1753 | my $s = $parser->get_strval($$args[0]); | ||
| 1754 | if (defined $s) { | ||
| 1755 | $res = $s; | ||
| 1756 | $res =~ s/%([0-9a-fA-F]{2})/chr(hex($1))/ge; | ||
| 1757 | } | ||
| 1758 | } | ||
| 1759 | } elsif ($cmd eq 'str_rot13') { | ||
| 1760 | if (scalar @$args == 1) { | ||
| 1761 | my $s = $parser->get_strval($$args[0]); | ||
| 1762 | if (defined $s) { | ||
| 1763 | $res = $s; | ||
| 1764 | $res =~ tr/A-Za-z/N-ZA-Mn-za-m/; | ||
| 1765 | } | ||
| 1766 | } | ||
| 1767 | } elsif ($cmd eq 'strrev') { | ||
| 1768 | if (scalar @$args == 1) { | ||
| 1769 | my $s = $parser->get_strval($$args[0]); | ||
| 1770 | if (defined $s) { | ||
| 1771 | $res = reverse $s; | ||
| 1772 | } | ||
| 1773 | } | ||
| 1774 | } elsif ($cmd eq 'strtoupper') { | ||
| 1775 | if (scalar @$args == 1) { | ||
| 1776 | my $s = $parser->get_strval($$args[0]); | ||
| 1777 | if (defined $s) { | ||
| 1778 | $res = uc($s); | ||
| 1779 | } | ||
| 1780 | } | ||
| 1781 | } elsif ($cmd eq 'strtolower') { | ||
| 1782 | if (scalar @$args == 1) { | ||
| 1783 | my $s = $parser->get_strval($$args[0]); | ||
| 1784 | if (defined $s) { | ||
| 1785 | $res = lc($s); | ||
| 1786 | } | ||
| 1787 | } | ||
| 1788 | } elsif ($cmd eq 'ucfirst') { | ||
| 1789 | if (scalar @$args == 1) { | ||
| 1790 | my $s = $parser->get_strval($$args[0]); | ||
| 1791 | if (defined $s) { | ||
| 1792 | $res = ucfirst($s); | ||
| 1793 | } | ||
| 1794 | } | ||
| 1795 | } elsif ($cmd eq 'lcfirst') { | ||
| 1796 | if (scalar @$args == 1) { | ||
| 1797 | my $s = $parser->get_strval($$args[0]); | ||
| 1798 | if (defined $s) { | ||
| 1799 | $res = lcfirst($s); | ||
| 1800 | } | ||
| 1801 | } | ||
| 1802 | } elsif ($cmd eq 'dirname') { | ||
| 1803 | if (scalar @$args == 1) { | ||
| 1804 | my $s = $parser->get_strval($$args[0]); | ||
| 1805 | if (defined $s) { | ||
| 1806 | $res = dirname($s); | ||
| 1807 | } | ||
| 1808 | } | ||
| 1809 | } elsif ($cmd eq 'basename') { | ||
| 1810 | if (scalar @$args == 1) { | ||
| 1811 | my $s = $parser->get_strval($$args[0]); | ||
| 1812 | if (defined $s) { | ||
| 1813 | $res = basename($s); | ||
| 1814 | } | ||
| 1815 | } | ||
| 1816 | } elsif ($cmd eq 'strlen') { | ||
| 1817 | if (scalar @$args == 1) { | ||
| 1818 | my $val = $$args[0]; | ||
| 1819 | if (is_array($val)) { | ||
| 1820 | $ctx->{warn}->($ctx, 'cmd', $cmd, "len of array $val taken -> 0"); | ||
| 1821 | $res = 0; | ||
| 1822 | $to_num = 1; | ||
| 1823 | } elsif (is_null($val)) { | ||
| 1824 | $res = 0; | ||
| 1825 | $to_num = 1; | ||
| 1826 | } elsif (is_strval($val)) { | ||
| 1827 | my $s = $parser->get_strval($val); | ||
| 1828 | if (defined $s) { | ||
| 1829 | $res = length($s); | ||
| 1830 | $to_num = 1; | ||
| 1831 | } | ||
| 1832 | } | ||
| 1833 | } | ||
| 1834 | } elsif ($cmd eq 'chr') { | ||
| 1835 | if (scalar @$args == 1) { | ||
| 1836 | my $s = $parser->get_strval($$args[0]); | ||
| 1837 | if (defined $s) { | ||
| 1838 | $res = chr(int($s) & 0xff); | ||
| 1839 | } | ||
| 1840 | } | ||
| 1841 | } elsif ($cmd eq 'ord') { | ||
| 1842 | if (scalar @$args == 1) { | ||
| 1843 | my $s = $parser->get_strval($$args[0]); | ||
| 1844 | if (defined $s) { | ||
| 1845 | $res = ord($s); | ||
| 1846 | $to_num = 1; | ||
| 1847 | } | ||
| 1848 | } | ||
| 1849 | } elsif ($cmd eq 'hexdec') { | ||
| 1850 | if (scalar @$args == 1) { | ||
| 1851 | my $s = $parser->get_strval($$args[0]); | ||
| 1852 | if (defined $s) { | ||
| 1853 | $res = hex($s); | ||
| 1854 | $to_num = 1; | ||
| 1855 | } | ||
| 1856 | } | ||
| 1857 | } elsif ($cmd eq 'octdec') { | ||
| 1858 | if (scalar @$args == 1) { | ||
| 1859 | my $s = $parser->get_strval($$args[0]); | ||
| 1860 | if (defined $s) { | ||
| 1861 | $res = oct($s); | ||
| 1862 | $to_num = 1; | ||
| 1863 | } | ||
| 1864 | } | ||
| 1865 | } elsif ($cmd eq 'bindec') { | ||
| 1866 | if (scalar @$args == 1) { | ||
| 1867 | my $s = $parser->get_strval($$args[0]); | ||
| 1868 | if (defined $s) { | ||
| 1869 | $res = unpack('N', pack('B32', substr('0' x 32 . $s, -32))); | ||
| 1870 | $to_num = 1; | ||
| 1871 | } | ||
| 1872 | } | ||
| 1873 | } elsif ($cmd eq 'dechex') { | ||
| 1874 | if (scalar @$args == 1) { | ||
| 1875 | my $s = $parser->get_strval($$args[0]); | ||
| 1876 | if (defined $s) { | ||
| 1877 | $res = sprintf("%x", $s); | ||
| 1878 | } | ||
| 1879 | } | ||
| 1880 | } elsif ($cmd eq 'octhex') { | ||
| 1881 | if (scalar @$args == 1) { | ||
| 1882 | my $s = $parser->get_strval($$args[0]); | ||
| 1883 | if (defined $s) { | ||
| 1884 | $res = sprintf("%o", $s); | ||
| 1885 | } | ||
| 1886 | } | ||
| 1887 | } elsif ($cmd eq 'binhex') { | ||
| 1888 | if (scalar @$args == 1) { | ||
| 1889 | my $s = $parser->get_strval($$args[0]); | ||
| 1890 | if (defined $s) { | ||
| 1891 | $res = sprintf("%b", $s); | ||
| 1892 | } | ||
| 1893 | } | ||
| 1894 | } elsif ($cmd eq 'bin2hex') { | ||
| 1895 | if (scalar @$args == 1) { | ||
| 1896 | my $s = $parser->get_strval($$args[0]); | ||
| 1897 | if (defined $s) { | ||
| 1898 | $res = unpack('H*', $s); | ||
| 1899 | } | ||
| 1900 | } | ||
| 1901 | } elsif ($cmd eq 'hex2bin') { | ||
| 1902 | if (scalar @$args == 1) { | ||
| 1903 | my $s = $parser->get_strval($$args[0]); | ||
| 1904 | if (defined $s) { | ||
| 1905 | $res = pack('H*', $s); | ||
| 1906 | } | ||
| 1907 | } | ||
| 1908 | } elsif ($cmd eq 'floor') { | ||
| 1909 | if (scalar @$args == 1) { | ||
| 1910 | my $s = $parser->get_strval($$args[0]); | ||
| 1911 | if (defined $s) { | ||
| 1912 | $s = 0 if ($s eq ''); | ||
| 1913 | $res = int($s); | ||
| 1914 | $to_num = 1; | ||
| 1915 | } | ||
| 1916 | } | ||
| 1917 | } elsif ($cmd eq 'ceil') { | ||
| 1918 | if (scalar @$args == 1) { | ||
| 1919 | my $s = $parser->get_strval($$args[0]); | ||
| 1920 | if (defined $s) { | ||
| 1921 | $s = 0 if ($s eq ''); | ||
| 1922 | $res = int($s + 0.99); | ||
| 1923 | $to_num = 1; | ||
| 1924 | } | ||
| 1925 | } | ||
| 1926 | } elsif ($cmd eq 'intval') { | ||
| 1927 | if (scalar @$args == 1) { | ||
| 1928 | my $s = $parser->get_strval($$args[0]); | ||
| 1929 | if (defined $s) { | ||
| 1930 | $s = 0 if ($s eq ''); | ||
| 1931 | $res = int(PHP::Decode::Op::to_num($s)); | ||
| 1932 | $to_num = 1; | ||
| 1933 | } | ||
| 1934 | } | ||
| 1935 | } elsif ($cmd eq 'strval') { | ||
| 1936 | if (scalar @$args == 1) { | ||
| 1937 | my $s = $parser->get_strval($$args[0]); | ||
| 1938 | if (defined $s) { | ||
| 1939 | $res = "$s"; | ||
| 1940 | } | ||
| 1941 | } | ||
| 1942 | } elsif ($cmd eq 'boolval') { | ||
| 1943 | if (scalar @$args == 1) { | ||
| 1944 | my $s = $parser->get_strval($$args[0]); | ||
| 1945 | if (defined $s) { | ||
| 1946 | $s = 0 if ($s eq ''); | ||
| 1947 | if ($s eq '0') { | ||
| 1948 | $res = 0; | ||
| 1949 | } else { | ||
| 1950 | $res = 1; | ||
| 1951 | } | ||
| 1952 | $to_num = 1; | ||
| 1953 | } | ||
| 1954 | } | ||
| 1955 | } elsif ($cmd eq 'floatval') { | ||
| 1956 | if (scalar @$args == 1) { | ||
| 1957 | my $s = $parser->get_strval($$args[0]); | ||
| 1958 | if (defined $s) { | ||
| 1959 | $s = 0 if ($s eq ''); | ||
| 1960 | $res = $s + 0; | ||
| 1961 | $to_num = 1; | ||
| 1962 | } | ||
| 1963 | } | ||
| 1964 | } elsif ($cmd eq 'pow') { | ||
| 1965 | if (scalar @$args == 2) { | ||
| 1966 | my $s = $parser->get_strval($$args[0]); | ||
| 1967 | my $e = $parser->get_strval($$args[1]); | ||
| 1968 | if (defined $s && defined $e) { | ||
| 1969 | $s = 0 if ($s eq ''); | ||
| 1970 | $e = 0 if ($e eq ''); | ||
| 1971 | $res = $s ** $e; | ||
| 1972 | $to_num = 1; | ||
| 1973 | } | ||
| 1974 | } | ||
| 1975 | } elsif ($cmd eq 'gzinflate') { | ||
| 1976 | if (scalar @$args >= 1) { | ||
| 1977 | my $s = $parser->get_strval($$args[0]); | ||
| 1978 | my $maxlen = '0'; | ||
| 1979 | if (scalar @$args > 1) { | ||
| 1980 | $maxlen = $parser->get_strval($$args[1]); | ||
| 1981 | $maxlen = 0 if ($maxlen eq ''); | ||
| 1982 | } | ||
| 1983 | if (defined $s && (defined $maxlen && ($maxlen eq '0'))) { | ||
| 1984 | my ($i, $istatus) = Compress::Zlib::inflateInit(-WindowBits => -(MAX_WBITS)); | ||
| 1985 | my ($output, $ostatus) = $i->inflate($s); | ||
| 1986 | if (defined $output) { | ||
| 1987 | $res = $output; | ||
| 1988 | } | ||
| 1989 | } | ||
| 1990 | } | ||
| 1991 | } elsif ($cmd eq 'gzdecode') { | ||
| 1992 | if (scalar @$args >= 1) { | ||
| 1993 | my $s = $parser->get_strval($$args[0]); | ||
| 1994 | my $maxlen = '0'; | ||
| 1995 | if (scalar @$args > 1) { | ||
| 1996 | $maxlen = $parser->get_strval($$args[1]); | ||
| 1997 | $maxlen = 0 if ($maxlen eq ''); | ||
| 1998 | } | ||
| 1999 | if (defined $s && (defined $maxlen && ($maxlen eq '0'))) { | ||
| 2000 | # http://www.faqs.org/rfcs/rfc1952.html | ||
| 2001 | # | ||
| 2002 | if (substr($s, 0, 3) eq "\x1f\x8b\x08") { | ||
| 2003 | my $off = 10; | ||
| 2004 | my $flag = ord(substr($s, 3, 1)); | ||
| 2005 | if ($flag > 0 ) { | ||
| 2006 | if ($flag & 4) { | ||
| 2007 | my ($xlen) = unpack('v', substr($s, $off, 2)); | ||
| 2008 | $off = $off + 2 + $xlen; | ||
| 2009 | } | ||
| 2010 | if ($flag & 8) { | ||
| 2011 | $off = index($s, "\0", $off) + 1; | ||
| 2012 | } | ||
| 2013 | if ($flag & 16) { | ||
| 2014 | $off = index($s, "\0", $off) + 1; | ||
| 2015 | } | ||
| 2016 | if ($flag & 2) { | ||
| 2017 | $off = $off + 2; | ||
| 2018 | } | ||
| 2019 | } | ||
| 2020 | my $b = substr($s, $off, -8); | ||
| 2021 | |||
| 2022 | my ($i, $istatus) = Compress::Zlib::inflateInit(-WindowBits => -(MAX_WBITS)); | ||
| 2023 | my ($output, $ostatus) = $i->inflate($b); | ||
| 2024 | if (defined $output) { | ||
| 2025 | $res = $output; | ||
| 2026 | } | ||
| 2027 | } | ||
| 2028 | } | ||
| 2029 | } | ||
| 2030 | } elsif ($cmd eq 'gzuncompress') { | ||
| 2031 | if (scalar @$args == 1) { | ||
| 2032 | my $s = $parser->get_strval($$args[0]); | ||
| 2033 | if (defined $s) { | ||
| 2034 | $res = Compress::Zlib::uncompress($s); | ||
| 2035 | } | ||
| 2036 | } | ||
| 2037 | } elsif ($cmd eq 'crc32') { | ||
| 2038 | if (scalar @$args == 1) { | ||
| 2039 | my $s = $parser->get_strval($$args[0]); | ||
| 2040 | if (defined $s) { | ||
| 2041 | $res = mycrc32b($s); | ||
| 2042 | $to_num = 1; | ||
| 2043 | } | ||
| 2044 | } | ||
| 2045 | } elsif ($cmd eq 'hash') { | ||
| 2046 | if (scalar @$args >= 2) { | ||
| 2047 | my $a = $parser->get_strval($$args[0]); | ||
| 2048 | my $s = $parser->get_strval($$args[1]); | ||
| 2049 | my $raw = '0'; | ||
| 2050 | if (scalar @$args > 2) { | ||
| 2051 | $raw = $parser->get_strval($$args[2]); | ||
| 2052 | } | ||
| 2053 | if (defined $a && defined $s && defined $raw) { | ||
| 2054 | my $output; | ||
| 2055 | if ($a eq 'crc32') { | ||
| 2056 | $output = pack('N', mycrc32a($s)); | ||
| 2057 | } elsif ($a eq 'crc32b') { | ||
| 2058 | $output = pack('N', mycrc32b($s)); | ||
| 2059 | } elsif ($a eq 'sha1') { | ||
| 2060 | $output = sha1($s); | ||
| 2061 | } elsif ($a eq 'md5') { | ||
| 2062 | $output = md5($s); | ||
| 2063 | } | ||
| 2064 | if (defined $output) { | ||
| 2065 | if ($raw eq '0') { | ||
| 2066 | #$res = sprintf("%x", $output); | ||
| 2067 | $res = unpack('H*', ''.$output); | ||
| 2068 | } else { | ||
| 2069 | $res = $output; | ||
| 2070 | } | ||
| 2071 | } | ||
| 2072 | } | ||
| 2073 | } | ||
| 2074 | } elsif ($cmd eq 'rawurldecode') { | ||
| 2075 | if (scalar @$args == 1) { | ||
| 2076 | my $s = $parser->get_strval($$args[0]); | ||
| 2077 | if (defined $s) { | ||
| 2078 | # no '+'->' ' conversion here | ||
| 2079 | $res = URI::Escape::uri_unescape($s); | ||
| 2080 | } | ||
| 2081 | } | ||
| 2082 | } elsif ($cmd eq 'rawurlencode') { | ||
| 2083 | if (scalar @$args == 1) { | ||
| 2084 | my $s = $parser->get_strval($$args[0]); | ||
| 2085 | if (defined $s) { | ||
| 2086 | # no ' '->'+' conversion here | ||
| 2087 | # default safe RFC3986 characters are: "A-Za-z0-9\-\._~" | ||
| 2088 | $res = URI::Escape::uri_escape($s); | ||
| 2089 | } | ||
| 2090 | } | ||
| 2091 | } elsif ($cmd eq 'urldecode') { | ||
| 2092 | if (scalar @$args == 1) { | ||
| 2093 | my $s = $parser->get_strval($$args[0]); | ||
| 2094 | if (defined $s) { | ||
| 2095 | $s =~ s/\+/ /g; | ||
| 2096 | $res = URI::Escape::uri_unescape($s); | ||
| 2097 | } | ||
| 2098 | } | ||
| 2099 | } elsif ($cmd eq 'urlencode') { | ||
| 2100 | if (scalar @$args == 1) { | ||
| 2101 | my $s = $parser->get_strval($$args[0]); | ||
| 2102 | if (defined $s) { | ||
| 2103 | # default safe characters are here: "A-Za-z0-9\-\._" | ||
| 2104 | # see: php/ext/standard/url.c | ||
| 2105 | $res = URI::Escape::uri_escape($s, "^A-Za-z0-9\-\._ "); | ||
| 2106 | $res =~ s/ /\+/g; | ||
| 2107 | } | ||
| 2108 | } | ||
| 2109 | } elsif ($cmd eq 'trim') { | ||
| 2110 | if (scalar @$args == 1) { | ||
| 2111 | my $s = $parser->get_strval($$args[0]); | ||
| 2112 | if (defined $s) { | ||
| 2113 | $res = $s; | ||
| 2114 | $res =~ s/^\s+|\s+$//g; | ||
| 2115 | } | ||
| 2116 | } | ||
| 2117 | } elsif ($cmd eq 'ltrim') { | ||
| 2118 | if (scalar @$args == 1) { | ||
| 2119 | my $s = $parser->get_strval($$args[0]); | ||
| 2120 | if (defined $s) { | ||
| 2121 | $res = $s; | ||
| 2122 | $res =~ s/^\s+//; | ||
| 2123 | } | ||
| 2124 | } | ||
| 2125 | } elsif (($cmd eq 'rtrim') || ($cmd eq 'chop')) { | ||
| 2126 | if (scalar @$args == 1) { | ||
| 2127 | my $s = $parser->get_strval($$args[0]); | ||
| 2128 | if (defined $s) { | ||
| 2129 | $res = $s; | ||
| 2130 | $res =~ s/\s+$//; | ||
| 2131 | } | ||
| 2132 | } | ||
| 2133 | } elsif ($cmd eq 'stripslashes') { | ||
| 2134 | if (scalar @$args == 1) { | ||
| 2135 | my $s = $parser->get_strval($$args[0]); | ||
| 2136 | if (defined $s) { | ||
| 2137 | $res = $s; | ||
| 2138 | $res =~ s/\\(\'|\"|\\)/$1/g; | ||
| 2139 | } | ||
| 2140 | } | ||
| 2141 | } elsif ($cmd eq 'stripcslashes') { | ||
| 2142 | if (scalar @$args == 1) { | ||
| 2143 | my $s = $parser->get_strval($$args[0]); | ||
| 2144 | if (defined $s) { | ||
| 2145 | $res = stripcslashes($s); | ||
| 2146 | } | ||
| 2147 | } | ||
| 2148 | } elsif ($cmd eq 'strpos') { | ||
| 2149 | if (scalar @$args >= 2) { | ||
| 2150 | my $s = $parser->get_strval($$args[0]); | ||
| 2151 | my $p = $parser->get_strval($$args[1]); | ||
| 2152 | my $off = 0; | ||
| 2153 | if (scalar @$args == 3) { | ||
| 2154 | $off = $parser->get_strval($$args[2]); | ||
| 2155 | $off = 0 if ($off eq ''); | ||
| 2156 | } | ||
| 2157 | if (defined $s && defined $p) { | ||
| 2158 | $res = index($s, $p, $off); | ||
| 2159 | $to_num = 1; | ||
| 2160 | } | ||
| 2161 | } | ||
| 2162 | } elsif ($cmd eq 'substr') { | ||
| 2163 | if (scalar @$args >= 2) { | ||
| 2164 | my $s = $parser->get_strval($$args[0]); | ||
| 2165 | my $off = $parser->get_strval($$args[1]); | ||
| 2166 | if (defined $s && defined $off) { | ||
| 2167 | $off = 0 if ($off eq ''); | ||
| 2168 | if (scalar @$args == 3) { | ||
| 2169 | my $len = $parser->get_strval($$args[2]); | ||
| 2170 | if (defined $len) { | ||
| 2171 | $len = 0 if ($len eq ''); | ||
| 2172 | $res = substr($s, $off, $len); | ||
| 2173 | } | ||
| 2174 | } else { | ||
| 2175 | $res = substr($s, $off); | ||
| 2176 | } | ||
| 2177 | } | ||
| 2178 | } | ||
| 2179 | } elsif ($cmd eq 'substr_count') { | ||
| 2180 | if (scalar @$args >= 2) { | ||
| 2181 | my $s = $parser->get_strval($$args[0]); | ||
| 2182 | my $p = $parser->get_strval($$args[1]); | ||
| 2183 | if (defined $s) { | ||
| 2184 | my $sub = $s; | ||
| 2185 | if (scalar @$args > 2) { | ||
| 2186 | my $off = $parser->get_strval($$args[2]); | ||
| 2187 | if (scalar @$args > 3) { | ||
| 2188 | my $len = $parser->get_strval($$args[3]); | ||
| 2189 | $sub = substr($s, $off, $len); | ||
| 2190 | } else { | ||
| 2191 | $sub = substr($s, $off); | ||
| 2192 | } | ||
| 2193 | } | ||
| 2194 | $res = () = $sub =~ /\Q$p\E/g; | ||
| 2195 | $to_num=1; | ||
| 2196 | } | ||
| 2197 | } | ||
| 2198 | } elsif ($cmd eq 'strstr') { | ||
| 2199 | if (scalar @$args >= 2) { | ||
| 2200 | my $s = $parser->get_strval($$args[0]); | ||
| 2201 | my $p = $parser->get_strval($$args[1]); | ||
| 2202 | if (defined $s && defined $p) { | ||
| 2203 | my $off = index($s, $p); | ||
| 2204 | if (defined $off) { | ||
| 2205 | my $before = 0; | ||
| 2206 | if (scalar @$args > 2) { | ||
| 2207 | $before = $parser->get_strval($$args[2]); | ||
| 2208 | } | ||
| 2209 | if ($before) { | ||
| 2210 | $res = substr($s, 0, $off); | ||
| 2211 | } else { | ||
| 2212 | $res = substr($s, $off); | ||
| 2213 | } | ||
| 2214 | } | ||
| 2215 | } | ||
| 2216 | } | ||
| 2217 | } elsif ($cmd eq 'strtr') { | ||
| 2218 | # https://www.php.net/manual/en/function.strtr.php | ||
| 2219 | # | ||
| 2220 | # if from and to have different lengths, the extra characters in | ||
| 2221 | # the longer of the two are ignored. The length of string will be | ||
| 2222 | # the same as the return value's. | ||
| 2223 | # | ||
| 2224 | if (scalar @$args == 3) { | ||
| 2225 | my $str = $parser->get_strval($$args[0]); | ||
| 2226 | my $from = $parser->get_strval($$args[1]); | ||
| 2227 | my $to = $parser->get_strval($$args[2]); | ||
| 2228 | my $org = $str; | ||
| 2229 | |||
| 2230 | if (defined $str && defined $from && defined $to) { | ||
| 2231 | # for from > to perl would skip characters missing in 'to', | ||
| 2232 | # for to > from perl would substitue the last char with the full suffix. | ||
| 2233 | # | ||
| 2234 | if (length($from) > length($to)) { | ||
| 2235 | $to .= substr($from, length($from)-1); | ||
| 2236 | } elsif (length($to) > length($from)) { | ||
| 2237 | $from .= substr($to, length($to)-1); | ||
| 2238 | } | ||
| 2239 | # tr needs eval to use variables | ||
| 2240 | # | ||
| 2241 | eval "\$str =~ tr/\Q$from\E/\Q$to\E/;"; | ||
| 2242 | unless ($@) { | ||
| 2243 | $ctx->{log}->($ctx, 'cmd', $cmd, "[%s]->[%s] [%s]->[%s]", $parser->shortstr($from,40), $parser->shortstr($to,40), $parser->shortstr($org,40), $parser->shortstr($str,40)) if $ctx->{log}; | ||
| 2244 | $res = $str; | ||
| 2245 | } | ||
| 2246 | } | ||
| 2247 | } | ||
| 2248 | } elsif ($cmd eq 'strcmp') { | ||
| 2249 | # https://php.net/manual/en/function.strcmp.php | ||
| 2250 | # TODO: perl cmp does just return -1,0,1 | ||
| 2251 | # | ||
| 2252 | if (scalar @$args == 2) { | ||
| 2253 | my $s = $parser->get_strval($$args[0]); | ||
| 2254 | my $d = $parser->get_strval($$args[1]); | ||
| 2255 | if (defined $s && defined $d) { | ||
| 2256 | $res = $s cmp $d; | ||
| 2257 | $to_num = 1; | ||
| 2258 | } | ||
| 2259 | } | ||
| 2260 | } elsif ($cmd eq 'strncmp') { | ||
| 2261 | # https://php.net/manual/en/function.strncmp.php | ||
| 2262 | # TODO: perl cmp does just return -1,0,1 | ||
| 2263 | # | ||
| 2264 | if (scalar @$args == 3) { | ||
| 2265 | my $s = $parser->get_strval($$args[0]); | ||
| 2266 | my $d = $parser->get_strval($$args[1]); | ||
| 2267 | my $len = $parser->get_strval($$args[2]); | ||
| 2268 | if (defined $s && defined $d && defined $len) { | ||
| 2269 | $len = 0 if ($len eq ''); | ||
| 2270 | $res = substr($s,0,$len) cmp substr($d,0,$len); | ||
| 2271 | $to_num = 1; | ||
| 2272 | } | ||
| 2273 | } | ||
| 2274 | } elsif ($cmd eq 'strcasecmp') { | ||
| 2275 | # https://php.net/manual/en/function.strcasecmp.php | ||
| 2276 | # TODO: perl cmp does just return -1,0,1 | ||
| 2277 | # | ||
| 2278 | if (scalar @$args == 2) { | ||
| 2279 | my $s = $parser->get_strval($$args[0]); | ||
| 2280 | my $d = $parser->get_strval($$args[1]); | ||
| 2281 | if (defined $s && defined $d) { | ||
| 2282 | $res = lc($s) cmp lc($d); | ||
| 2283 | $to_num = 1; | ||
| 2284 | } | ||
| 2285 | } | ||
| 2286 | } elsif ($cmd eq 'strncasecmp') { | ||
| 2287 | # https://php.net/manual/en/function.strncasecmp.php | ||
| 2288 | # TODO: perl cmp does just return -1,0,1 | ||
| 2289 | # | ||
| 2290 | if (scalar @$args == 3) { | ||
| 2291 | my $s = $parser->get_strval($$args[0]); | ||
| 2292 | my $d = $parser->get_strval($$args[1]); | ||
| 2293 | my $len = $parser->get_strval($$args[2]); | ||
| 2294 | if (defined $s && defined $d && defined $len) { | ||
| 2295 | $len = 0 if ($len eq ''); | ||
| 2296 | $res = lc(substr($s,0,$len)) cmp lc(substr($d,0,$len)); | ||
| 2297 | $to_num = 1; | ||
| 2298 | } | ||
| 2299 | } | ||
| 2300 | } elsif ($cmd eq 'str_repeat') { | ||
| 2301 | # https://php.net/manual/en/function.str-repeat.php | ||
| 2302 | # | ||
| 2303 | if (scalar @$args == 2) { | ||
| 2304 | my $s = $parser->get_strval($$args[0]); | ||
| 2305 | my $m = $parser->get_strval($$args[1]); | ||
| 2306 | |||
| 2307 | if (defined $s && defined $m) { | ||
| 2308 | $m = 0 if ($m eq ''); | ||
| 2309 | if ($m > $ctx->{max_repeat}) { # limit maxsize | ||
| 2310 | $ctx->{warn}->($ctx, 'cmd', $cmd, "$s x $m memory exhaution"); | ||
| 2311 | } else { | ||
| 2312 | $res = $s x $m; | ||
| 2313 | } | ||
| 2314 | } | ||
| 2315 | } | ||
| 2316 | } elsif ($cmd eq 'str_replace') { | ||
| 2317 | if (scalar @$args >= 3) { | ||
| 2318 | my $pattern = $$args[0]; | ||
| 2319 | my $replacement = $$args[1]; | ||
| 2320 | my $subject = $$args[2]; | ||
| 2321 | my $limit = -1; | ||
| 2322 | if (scalar @$args == 4) { | ||
| 2323 | $limit = $parser->get_strval($$args[3]); | ||
| 2324 | $limit = 0 if ($limit eq ''); | ||
| 2325 | } | ||
| 2326 | if (defined $pattern && defined $replacement && defined $subject) { | ||
| 2327 | if (is_array($subject)) { | ||
| 2328 | my $arr = $parser->{strmap}{$subject}; | ||
| 2329 | my $keys = $arr->get_keys(); | ||
| 2330 | my $newarr = $parser->newarr(); | ||
| 2331 | |||
| 2332 | foreach my $k (@$keys) { | ||
| 2333 | my $val = $arr->val($k); | ||
| 2334 | my ($r, $cnt) = preg_replace_subject($ctx, $pattern, $replacement, $val, 'str', $limit); | ||
| 2335 | $newarr->set(undef, $r); | ||
| 2336 | if ($limit != -1) { | ||
| 2337 | $limit -= $cnt; | ||
| 2338 | } | ||
| 2339 | } | ||
| 2340 | return $newarr->{name}; | ||
| 2341 | } elsif (is_strval($subject)) { | ||
| 2342 | my ($r, $cnt) = preg_replace_subject($ctx, $pattern, $replacement, $subject, 'str', $limit); | ||
| 2343 | return $r; | ||
| 2344 | } elsif ($subject eq '#null') { | ||
| 2345 | my ($r, $cnt) = preg_replace_subject($ctx, $pattern, $replacement, $subject, 'str', $limit); | ||
| 2346 | return $r; | ||
| 2347 | } | ||
| 2348 | } | ||
| 2349 | } | ||
| 2350 | } elsif ($cmd eq 'ereg_replace') { | ||
| 2351 | if (scalar @$args == 3) { | ||
| 2352 | my $pattern = $parser->get_strval($$args[0]); | ||
| 2353 | my $replacement = $parser->get_strval($$args[1]); | ||
| 2354 | my $str = $parser->get_strval($$args[2]); | ||
| 2355 | |||
| 2356 | # todo: use posix regex here | ||
| 2357 | # | ||
| 2358 | if (defined $pattern && defined $replacement && defined $str) { | ||
| 2359 | $res = $str; | ||
| 2360 | $res =~ s/\Q$pattern\E/$replacement/g; | ||
| 2361 | $ctx->{log}->($ctx, 'cmd', $cmd, "[%s]->[%s] %s -> %s", $parser->shortstr($pattern,40), $parser->shortstr($replacement,40), $parser->shortstr($str,40), $parser->shortstr($res,40)) if $ctx->{log}; | ||
| 2362 | } | ||
| 2363 | } | ||
| 2364 | } elsif ($cmd eq 'preg_replace') { | ||
| 2365 | if (scalar @$args >= 3) { | ||
| 2366 | my $pattern = $$args[0]; | ||
| 2367 | my $replacement = $$args[1]; | ||
| 2368 | my $subject = $$args[2]; | ||
| 2369 | my $limit = -1; | ||
| 2370 | if (scalar @$args == 4) { | ||
| 2371 | $limit = $parser->get_strval($$args[3]); | ||
| 2372 | $limit = 0 if ($limit eq ''); | ||
| 2373 | } | ||
| 2374 | |||
| 2375 | # preg_replace uses pattern '/xxx/' instead of 'xxx' | ||
| 2376 | # | ||
| 2377 | if (defined $pattern && defined $replacement && defined $subject) { | ||
| 2378 | if (is_array($subject)) { | ||
| 2379 | my $arr = $parser->{strmap}{$subject}; | ||
| 2380 | my $keys = $arr->get_keys(); | ||
| 2381 | my $newarr = $parser->newarr(); | ||
| 2382 | |||
| 2383 | foreach my $k (@$keys) { | ||
| 2384 | my $val = $arr->val($k); | ||
| 2385 | my ($r, $cnt) = preg_replace_subject($ctx, $pattern, $replacement, $val, 'preg', $limit); | ||
| 2386 | $newarr->set(undef, $r); | ||
| 2387 | if ($limit != -1) { | ||
| 2388 | $limit -= $cnt; | ||
| 2389 | } | ||
| 2390 | } | ||
| 2391 | return $newarr->{name}; | ||
| 2392 | } elsif (is_strval($subject)) { | ||
| 2393 | my ($r, $cnt) = preg_replace_subject($ctx, $pattern, $replacement, $subject, 'preg', $limit); | ||
| 2394 | return $r; | ||
| 2395 | } elsif ($subject eq '#null') { | ||
| 2396 | my ($r, $cnt) = preg_replace_subject($ctx, $pattern, $replacement, $subject, 'preg', $limit); | ||
| 2397 | return $r; | ||
| 2398 | } | ||
| 2399 | } | ||
| 2400 | } | ||
| 2401 | } elsif ($cmd eq 'preg_quote') { | ||
| 2402 | if (scalar @$args == 1) { | ||
| 2403 | my $s = $parser->get_strval($$args[0]); | ||
| 2404 | if (defined $s) { | ||
| 2405 | # escape: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : - | ||
| 2406 | $s =~ s/%([\.\\\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:\-])/\\$1/ge; | ||
| 2407 | $res = $s; | ||
| 2408 | } | ||
| 2409 | } | ||
| 2410 | } elsif ($cmd eq 'md5') { | ||
| 2411 | if (scalar @$args == 1) { | ||
| 2412 | my $s = $parser->get_strval($$args[0]); | ||
| 2413 | if (defined $s) { | ||
| 2414 | $res = md5_hex($s); | ||
| 2415 | } | ||
| 2416 | } | ||
| 2417 | } elsif ($cmd eq 'sha1') { | ||
| 2418 | if (scalar @$args == 1) { | ||
| 2419 | my $s = $parser->get_strval($$args[0]); | ||
| 2420 | if (defined $s) { | ||
| 2421 | $res = sha1_hex($s); | ||
| 2422 | } | ||
| 2423 | } | ||
| 2424 | } elsif ($cmd eq 'levenshtein') { | ||
| 2425 | if (scalar @$args == 2) { | ||
| 2426 | my $s1 = $parser->get_strval($$args[0]); | ||
| 2427 | my $s2 = $parser->get_strval($$args[1]); | ||
| 2428 | if (defined $s1 && defined $s2) { | ||
| 2429 | $res = levenshtein($s1, $2); | ||
| 2430 | } | ||
| 2431 | } | ||
| 2432 | } elsif ($cmd eq 'str_split') { | ||
| 2433 | if (scalar @$args >= 1) { | ||
| 2434 | my $s1 = $parser->get_strval($$args[0]); | ||
| 2435 | my $s2 = '1'; | ||
| 2436 | if (scalar @$args == 2) { | ||
| 2437 | $s2 = $parser->get_strval($$args[1]); | ||
| 2438 | } | ||
| 2439 | if (defined $s1 && defined $s2) { | ||
| 2440 | my $arr = $parser->newarr(); | ||
| 2441 | # don't include delimter in split result | ||
| 2442 | my @parts = $s1 =~ /.{1,$s2}/g; | ||
| 2443 | |||
| 2444 | foreach my $p (@parts) { | ||
| 2445 | $ctx->{log}->($ctx, 'cmd', $cmd, "[$s2] part: $p") if $ctx->{log}; | ||
| 2446 | my $k = $parser->setstr($p); | ||
| 2447 | $arr->set(undef, $k); | ||
| 2448 | } | ||
| 2449 | return $arr->{name}; | ||
| 2450 | } | ||
| 2451 | } | ||
| 2452 | } elsif ($cmd eq 'preg_split') { | ||
| 2453 | if (scalar @$args >= 2) { | ||
| 2454 | my $s1 = $parser->get_strval($$args[0]); | ||
| 2455 | my $s2 = $parser->get_strval($$args[1]); | ||
| 2456 | my $limit = '-1'; | ||
| 2457 | my $flags = 0; | ||
| 2458 | if (scalar @$args > 2) { | ||
| 2459 | $limit = $parser->get_strval($$args[2]); | ||
| 2460 | $limit = 0 if ($limit eq ''); | ||
| 2461 | } | ||
| 2462 | if (scalar @$args > 3) { | ||
| 2463 | $flags = $parser->get_strval($$args[3]); | ||
| 2464 | } | ||
| 2465 | if (defined $s1 && defined $s2 && ($flags == 0)) { | ||
| 2466 | my $arr = $parser->newarr(); | ||
| 2467 | my ($pattern) = $s1 =~ /^\/(.*)\/$/; | ||
| 2468 | my @parts = split /$pattern/, $s2, $limit; | ||
| 2469 | |||
| 2470 | foreach my $p (@parts) { | ||
| 2471 | $ctx->{log}->($ctx, 'cmd', $cmd, "[$s2] '$s1' part: $p") if $ctx->{log}; | ||
| 2472 | my $k = $parser->setstr($p); | ||
| 2473 | $arr->set(undef, $k); | ||
| 2474 | } | ||
| 2475 | return $arr->{name}; | ||
| 2476 | } | ||
| 2477 | } | ||
| 2478 | } elsif ($cmd eq 'explode') { | ||
| 2479 | if (scalar @$args >= 2) { | ||
| 2480 | my $s1 = $parser->get_strval($$args[0]); | ||
| 2481 | my $s2 = $parser->get_strval($$args[1]); | ||
| 2482 | my $limit = '-1'; | ||
| 2483 | if (scalar @$args == 3) { | ||
| 2484 | $limit = $parser->get_strval($$args[2]); | ||
| 2485 | } | ||
| 2486 | if (defined $s1 && defined $s2) { | ||
| 2487 | my $arr = $parser->newarr(); | ||
| 2488 | my @parts = split /\Q$s1\E/, $s2, $limit; | ||
| 2489 | |||
| 2490 | foreach my $p (@parts) { | ||
| 2491 | $ctx->{log}->($ctx, 'cmd', $cmd, "[$s2] '$s1': part: $p") if $ctx->{log}; | ||
| 2492 | my $k = $parser->setstr($p); | ||
| 2493 | $arr->set(undef, $k); | ||
| 2494 | } | ||
| 2495 | return $arr->{name}; | ||
| 2496 | } | ||
| 2497 | } | ||
| 2498 | } elsif (($cmd eq 'implode') || ($cmd eq 'join')) { | ||
| 2499 | # https://php.net/manual/en/function.implode.php | ||
| 2500 | # 'join' is an alias for implode() | ||
| 2501 | # | ||
| 2502 | if (scalar @$args >= 1) { | ||
| 2503 | my $basestr = $$args[0]; | ||
| 2504 | my $glue = ''; | ||
| 2505 | |||
| 2506 | if (scalar @$args == 2) { | ||
| 2507 | if (is_array($$args[0])) { | ||
| 2508 | $ctx->{warn}->($ctx, 'cmd', $cmd, "reversed arguments $$args[0] $$args[1]"); | ||
| 2509 | $basestr = $$args[0]; | ||
| 2510 | $glue = $parser->get_strval($$args[1]); | ||
| 2511 | } else { | ||
| 2512 | $glue = $parser->get_strval($$args[0]); | ||
| 2513 | $basestr = $$args[1]; | ||
| 2514 | } | ||
| 2515 | } | ||
| 2516 | if (defined $glue && defined $basestr) { | ||
| 2517 | if (is_array($basestr)) { | ||
| 2518 | my $arr = $parser->{strmap}{$basestr}; | ||
| 2519 | my $keys = $arr->get_keys(); | ||
| 2520 | |||
| 2521 | my @vals = (); | ||
| 2522 | my $failed = 0; | ||
| 2523 | foreach my $k (@$keys) { | ||
| 2524 | my $val = $arr->val($k); | ||
| 2525 | if (is_strval($val)) { | ||
| 2526 | push (@vals, $parser->{strmap}->{$val}); | ||
| 2527 | } else { | ||
| 2528 | my $v = $ctx->exec_statement($val); | ||
| 2529 | if (is_strval($v)) { | ||
| 2530 | push (@vals, $parser->{strmap}->{$v}); | ||
| 2531 | } else { | ||
| 2532 | $ctx->{warn}->($ctx, 'cmd', $cmd, "$basestr: key $k not str ($val -> $v)"); | ||
| 2533 | $failed = 1; | ||
| 2534 | last; | ||
| 2535 | } | ||
| 2536 | } | ||
| 2537 | } | ||
| 2538 | $res = join($glue, @vals) unless ($failed); | ||
| 2539 | } | ||
| 2540 | } | ||
| 2541 | } | ||
| 2542 | } elsif ($cmd eq 'range') { | ||
| 2543 | # https://www.php.net/manual/en/function.range.php | ||
| 2544 | # | ||
| 2545 | if (scalar @$args >= 2) { | ||
| 2546 | my $s1 = $parser->get_strval($$args[0]); | ||
| 2547 | my $s2 = $parser->get_strval($$args[1]); | ||
| 2548 | my $step = '1'; | ||
| 2549 | if (scalar @$args == 3) { | ||
| 2550 | $step = $parser->get_strval($$args[2]); | ||
| 2551 | $step = 0 if (!defined $step || ($step eq '')); | ||
| 2552 | } | ||
| 2553 | if (defined $s1 && defined $s2 && ($step =~ /^-?\d+$/) && ($step != 0)) { | ||
| 2554 | my @parts; | ||
| 2555 | |||
| 2556 | # php ranges might be descending and use abs(step). | ||
| 2557 | # for non-numeric strings only the first char is used. | ||
| 2558 | # (float ranges are not supported) | ||
| 2559 | # | ||
| 2560 | if (($s1 =~ /^-?\d+$/) && ($s2 =~ /^-?\d+$/)) { | ||
| 2561 | @parts = range_steps($s1, $s2, abs($step)); | ||
| 2562 | } elsif (($s1 ne '') && ($s2 ne '') && !is_numeric_str($s1) && !is_numeric_str($s2)) { | ||
| 2563 | @parts = map { chr } range_steps(ord($s1), ord($s2), abs($step)); | ||
| 2564 | } else { | ||
| 2565 | return; | ||
| 2566 | } | ||
| 2567 | my $arr = $parser->newarr(); | ||
| 2568 | |||
| 2569 | foreach my $p (@parts) { | ||
| 2570 | $ctx->{log}->($ctx, 'cmd', $cmd, "['$s1', '$s2']: part: $p") if $ctx->{log}; | ||
| 2571 | my $k = $parser->setstr($p); | ||
| 2572 | $arr->set(undef, $k); | ||
| 2573 | } | ||
| 2574 | return $arr->{name}; | ||
| 2575 | } | ||
| 2576 | } | ||
| 2577 | } elsif (($cmd eq 'count') || ($cmd eq 'sizeof')) { | ||
| 2578 | # https://php.net/manual/en/function.count.php | ||
| 2579 | # todo: support arg2: COUNT_NORMAL/COUNT_RECURSIVE | ||
| 2580 | # | ||
| 2581 | if (scalar @$args == 1) { | ||
| 2582 | my $basestr = $$args[0]; | ||
| 2583 | if (defined $basestr) { | ||
| 2584 | if (is_array($basestr)) { | ||
| 2585 | my $arr = $parser->{strmap}{$basestr}; | ||
| 2586 | my $keys = $arr->get_keys(); | ||
| 2587 | $res = scalar @$keys; | ||
| 2588 | $to_num = 1; | ||
| 2589 | } | ||
| 2590 | } | ||
| 2591 | } | ||
| 2592 | } elsif (($cmd eq 'htmlspecialchars') || ($cmd eq 'htmlentities')) { | ||
| 2593 | if (scalar @$args >= 1) { | ||
| 2594 | my $s = $parser->get_strval($$args[0]); | ||
| 2595 | if (defined $s) { | ||
| 2596 | $res = HTML::Entities::encode_entities($s); | ||
| 2597 | } | ||
| 2598 | } | ||
| 2599 | } elsif (($cmd eq 'htmlspecialchars_decode') || ($cmd eq 'html_entity_decode')) { | ||
| 2600 | # just support 1 argument version without flags & charset for now | ||
| 2601 | # | ||
| 2602 | if (scalar @$args == 1) { | ||
| 2603 | my $s = $parser->get_strval($$args[0]); | ||
| 2604 | if (defined $s) { | ||
| 2605 | $res = HTML::Entities::decode_entities($s); | ||
| 2606 | } | ||
| 2607 | } | ||
| 2608 | } elsif ($cmd eq 'pack') { | ||
| 2609 | if (scalar @$args >= 1) { | ||
| 2610 | my $s = $parser->get_strval($$args[0]); | ||
| 2611 | my @param = (); | ||
| 2612 | my $i; | ||
| 2613 | |||
| 2614 | if (defined $s) { | ||
| 2615 | for ($i=1; $i < scalar @$args; $i++) { | ||
| 2616 | my $p = $parser->get_strval($args->[$i]); | ||
| 2617 | last unless (defined $p); | ||
| 2618 | push(@param, $p); | ||
| 2619 | } | ||
| 2620 | if ($i == scalar @$args) { | ||
| 2621 | eval { $res = pack($s, @param); }; | ||
| 2622 | if ($@) { | ||
| 2623 | $ctx->{warn}->($ctx, 'cmd', $cmd, "bad param $s"); | ||
| 2624 | } | ||
| 2625 | } | ||
| 2626 | } | ||
| 2627 | } | ||
| 2628 | } elsif ($cmd eq 'unpack') { | ||
| 2629 | if (scalar @$args == 2) { | ||
| 2630 | my $s = $parser->get_strval($$args[0]); | ||
| 2631 | my $d = $parser->get_strval($$args[1]); | ||
| 2632 | |||
| 2633 | if (defined $s && defined $d) { | ||
| 2634 | my @values; | ||
| 2635 | eval { @values = unpack($s, $d); }; | ||
| 2636 | if ($@) { | ||
| 2637 | $ctx->{warn}->($ctx, 'cmd', $cmd, "bad param $s"); | ||
| 2638 | } | ||
| 2639 | |||
| 2640 | my $arr = $parser->newarr(); | ||
| 2641 | |||
| 2642 | foreach my $p (@values) { | ||
| 2643 | $ctx->{log}->($ctx, 'cmd', $cmd, "[$s]: $p") if $ctx->{log}; | ||
| 2644 | my $k = $parser->setstr($p); | ||
| 2645 | $arr->set(undef, $k); | ||
| 2646 | } | ||
| 2647 | return $arr->{name}; | ||
| 2648 | } | ||
| 2649 | } | ||
| 2650 | } elsif ($cmd eq 'sprintf') { | ||
| 2651 | if (scalar @$args >= 1) { | ||
| 2652 | my $s = $parser->get_strval($$args[0]); | ||
| 2653 | my @param = (); | ||
| 2654 | my $i; | ||
| 2655 | |||
| 2656 | if (defined $s) { | ||
| 2657 | for ($i=1; $i < scalar @$args; $i++) { | ||
| 2658 | my $p = $parser->get_strval($args->[$i]); | ||
| 2659 | last unless (defined $p); | ||
| 2660 | push(@param, $p); | ||
| 2661 | } | ||
| 2662 | if ($i == scalar @$args) { | ||
| 2663 | eval { $res = sprintf($s, @param); }; | ||
| 2664 | if ($@) { | ||
| 2665 | $ctx->{warn}->($ctx, 'cmd', $cmd, "bad param $s"); | ||
| 2666 | } | ||
| 2667 | } | ||
| 2668 | } | ||
| 2669 | } | ||
| 2670 | } elsif ($cmd eq 'array_push') { | ||
| 2671 | # https://php.net/manual/en/function.array-push.php | ||
| 2672 | # | ||
| 2673 | if (scalar @$args >= 1) { | ||
| 2674 | my $var = $$args[0]; # ref var | ||
| 2675 | my ($is_valid, $val) = get_refvar_val($ctx, $var); | ||
| 2676 | |||
| 2677 | if ($is_valid) { | ||
| 2678 | if (!defined $val || is_null($val) || is_array($val)) { | ||
| 2679 | my $arr; | ||
| 2680 | if (defined $val && is_array($val)) { | ||
| 2681 | $arr = $parser->{strmap}{$val}; | ||
| 2682 | $arr = $arr->copy(); # recursive copy | ||
| 2683 | } else { | ||
| 2684 | $arr = $parser->newarr(); | ||
| 2685 | } | ||
| 2686 | for (my $i=1; $i < scalar @$args; $i++) { | ||
| 2687 | $arr->set(undef, $$args[$i]); | ||
| 2688 | } | ||
| 2689 | set_refvar_val($ctx, $var, $arr->{name}); | ||
| 2690 | my $keys = $arr->get_keys(); | ||
| 2691 | |||
| 2692 | $res = scalar keys @$keys; # returns elem count | ||
| 2693 | $to_num = 1; | ||
| 2694 | } | ||
| 2695 | } | ||
| 2696 | } | ||
| 2697 | } elsif ($cmd eq 'array_pop') { | ||
| 2698 | # https://php.net/manual/en/function.array-pop.php | ||
| 2699 | # | ||
| 2700 | if (scalar @$args == 1) { | ||
| 2701 | my $var = $$args[0]; # ref var | ||
| 2702 | my ($is_valid, $val) = get_refvar_val($ctx, $var); | ||
| 2703 | |||
| 2704 | if ($is_valid) { | ||
| 2705 | if (!defined $val || is_array($val)) { | ||
| 2706 | if (defined $val && is_array($val)) { | ||
| 2707 | my $arr = $parser->{strmap}{$val}; | ||
| 2708 | my $keys = $arr->get_keys(); | ||
| 2709 | if (scalar @$keys == 0) { | ||
| 2710 | $res = '#null'; | ||
| 2711 | } else { | ||
| 2712 | my $k = pop @$keys; | ||
| 2713 | $res = $arr->val($k); | ||
| 2714 | $arr = $arr->copy($keys); | ||
| 2715 | set_refvar_val($ctx, $var, $arr->{name}); | ||
| 2716 | } | ||
| 2717 | } else { | ||
| 2718 | $res = '#null'; | ||
| 2719 | } | ||
| 2720 | return $res; | ||
| 2721 | } | ||
| 2722 | } | ||
| 2723 | } | ||
| 2724 | } elsif ($cmd eq 'array_unshift') { | ||
| 2725 | # https://php.net/manual/en/function.array-unshift.php | ||
| 2726 | # | ||
| 2727 | if (scalar @$args >= 1) { | ||
| 2728 | my $var = $$args[0]; # ref var | ||
| 2729 | my ($is_valid, $val) = get_refvar_val($ctx, $var); | ||
| 2730 | |||
| 2731 | if ($is_valid) { | ||
| 2732 | if (!defined $val || is_null($val) || is_array($val)) { | ||
| 2733 | my $newarr = $parser->newarr(); | ||
| 2734 | |||
| 2735 | for (my $i=1; $i < scalar @$args; $i++) { | ||
| 2736 | $newarr->set(undef, $$args[$i]); # prepend new elems | ||
| 2737 | } | ||
| 2738 | if (defined $val && is_array($val)) { | ||
| 2739 | my $arr = $parser->{strmap}{$val}; | ||
| 2740 | my $keys = $arr->get_keys(); | ||
| 2741 | |||
| 2742 | foreach my $k (@$keys) { | ||
| 2743 | my $oldval = $arr->val($k); | ||
| 2744 | if (is_int_index($k)) { | ||
| 2745 | $newarr->set(undef, $oldval); # renumber int key | ||
| 2746 | } else { | ||
| 2747 | $newarr->set($k, $oldval); | ||
| 2748 | } | ||
| 2749 | } | ||
| 2750 | } | ||
| 2751 | set_refvar_val($ctx, $var, $newarr->{name}); | ||
| 2752 | |||
| 2753 | my $keys = $newarr->get_keys(); | ||
| 2754 | $res = scalar keys @$keys; # returns elem count | ||
| 2755 | $to_num = 1; | ||
| 2756 | } | ||
| 2757 | } | ||
| 2758 | } | ||
| 2759 | } elsif ($cmd eq 'array_shift') { | ||
| 2760 | # https://php.net/manual/en/function.array-shift.php | ||
| 2761 | # | ||
| 2762 | if (scalar @$args == 1) { | ||
| 2763 | my $var = $$args[0]; # ref var | ||
| 2764 | my ($is_valid, $val) = get_refvar_val($ctx, $var); | ||
| 2765 | |||
| 2766 | if ($is_valid) { | ||
| 2767 | if (!defined $val || is_array($val)) { | ||
| 2768 | if (defined $val && is_array($val)) { | ||
| 2769 | my $arr = $parser->{strmap}{$val}; | ||
| 2770 | my $keys = $arr->get_keys(); | ||
| 2771 | if (scalar @$keys == 0) { | ||
| 2772 | $res = '#null'; | ||
| 2773 | } else { | ||
| 2774 | my $k = shift @$keys; | ||
| 2775 | $res = $arr->val($k); | ||
| 2776 | |||
| 2777 | my $newarr = $parser->newarr(); | ||
| 2778 | foreach my $k (@$keys) { | ||
| 2779 | my $oldval = $arr->val($k); | ||
| 2780 | if (is_int_index($k)) { | ||
| 2781 | $newarr->set(undef, $oldval); # renumber int key | ||
| 2782 | } else { | ||
| 2783 | $newarr->set($k, $oldval); | ||
| 2784 | } | ||
| 2785 | } | ||
| 2786 | set_refvar_val($ctx, $var, $newarr->{name}); | ||
| 2787 | } | ||
| 2788 | } else { | ||
| 2789 | $res = '#null'; | ||
| 2790 | } | ||
| 2791 | return $res; | ||
| 2792 | } | ||
| 2793 | } | ||
| 2794 | } | ||
| 2795 | } elsif ($cmd eq 'each') { | ||
| 2796 | # https://php.net/manual/en/function.each.php | ||
| 2797 | # | ||
| 2798 | if (scalar @$args == 1) { | ||
| 2799 | my $var = $$args[0]; # ref var | ||
| 2800 | my ($is_valid, $val) = get_refvar_val($ctx, $var); | ||
| 2801 | |||
| 2802 | if ($is_valid) { | ||
| 2803 | if (defined $val && is_array($val)) { | ||
| 2804 | my $arr = $parser->{strmap}{$val}; | ||
| 2805 | my $pos = $arr->get_pos(); | ||
| 2806 | my $keys = $arr->get_keys(); | ||
| 2807 | if ($pos >= scalar @$keys) { | ||
| 2808 | $res = 0; | ||
| 2809 | $to_num = 1; | ||
| 2810 | } else { | ||
| 2811 | my $k = $keys->[$pos]; | ||
| 2812 | my $v = $arr->val($k); | ||
| 2813 | my $newarr = $parser->newarr(); | ||
| 2814 | if (is_int_index($k)) { | ||
| 2815 | $k = $parser->setnum($k); | ||
| 2816 | } | ||
| 2817 | $newarr->set(undef, $k); | ||
| 2818 | $newarr->set(undef, $v); | ||
| 2819 | $arr->set_pos($pos+1); | ||
| 2820 | return $newarr->{name}; | ||
| 2821 | } | ||
| 2822 | } else { | ||
| 2823 | $res = 0; | ||
| 2824 | $to_num = 1; | ||
| 2825 | } | ||
| 2826 | } | ||
| 2827 | } | ||
| 2828 | } elsif ($cmd eq 'reset') { | ||
| 2829 | # https://php.net/manual/en/function.reset.php | ||
| 2830 | # | ||
| 2831 | if (scalar @$args == 1) { | ||
| 2832 | my $var = $$args[0]; # ref var | ||
| 2833 | my ($is_valid, $val) = get_refvar_val($ctx, $var); | ||
| 2834 | |||
| 2835 | if ($is_valid) { | ||
| 2836 | if (defined $val && is_array($val)) { | ||
| 2837 | my $arr = $parser->{strmap}{$val}; | ||
| 2838 | my $keys = $arr->get_keys(); | ||
| 2839 | $arr->set_pos(0); | ||
| 2840 | if (scalar @$keys == 0) { | ||
| 2841 | $res = 0; | ||
| 2842 | $to_num = 1; | ||
| 2843 | } else { | ||
| 2844 | my $k = shift @$keys; | ||
| 2845 | $res = $arr->val($k); | ||
| 2846 | return $res; | ||
| 2847 | } | ||
| 2848 | } else { | ||
| 2849 | $res = 0; | ||
| 2850 | $to_num = 1; | ||
| 2851 | } | ||
| 2852 | } | ||
| 2853 | } | ||
| 2854 | } elsif ($cmd eq 'next') { | ||
| 2855 | # https://php.net/manual/en/function.next.php | ||
| 2856 | # | ||
| 2857 | if (scalar @$args == 1) { | ||
| 2858 | my $var = $$args[0]; # ref var | ||
| 2859 | my ($is_valid, $val) = get_refvar_val($ctx, $var); | ||
| 2860 | |||
| 2861 | if ($is_valid) { | ||
| 2862 | if (defined $val && is_array($val)) { | ||
| 2863 | my $arr = $parser->{strmap}{$val}; | ||
| 2864 | my $pos = $arr->get_pos(); | ||
| 2865 | my $keys = $arr->get_keys(); | ||
| 2866 | if (($pos+1) >= scalar @$keys) { | ||
| 2867 | $res = 0; | ||
| 2868 | $to_num = 1; | ||
| 2869 | } else { | ||
| 2870 | my $k = $keys->[$pos+1]; | ||
| 2871 | $res = $arr->val($k); | ||
| 2872 | $arr->set_pos($pos+1); | ||
| 2873 | return $res; | ||
| 2874 | } | ||
| 2875 | } else { | ||
| 2876 | $res = 0; | ||
| 2877 | $to_num = 1; | ||
| 2878 | } | ||
| 2879 | } | ||
| 2880 | } | ||
| 2881 | } elsif ($cmd eq 'prev') { | ||
| 2882 | # https://php.net/manual/en/function.prev.php | ||
| 2883 | # | ||
| 2884 | if (scalar @$args == 1) { | ||
| 2885 | my $var = $$args[0]; # ref var | ||
| 2886 | my ($is_valid, $val) = get_refvar_val($ctx, $var); | ||
| 2887 | |||
| 2888 | if ($is_valid) { | ||
| 2889 | if (defined $val && is_array($val)) { | ||
| 2890 | my $arr = $parser->{strmap}{$val}; | ||
| 2891 | my $pos = $arr->get_pos(); | ||
| 2892 | my $keys = $arr->get_keys(); | ||
| 2893 | if (($pos == 0) || (scalar @$keys == 0)) { | ||
| 2894 | $res = 0; | ||
| 2895 | $to_num = 1; | ||
| 2896 | } else { | ||
| 2897 | my $k = $keys->[$pos-1]; | ||
| 2898 | $res = $arr->val($k); | ||
| 2899 | $arr->set_pos($pos-1); | ||
| 2900 | return $res; | ||
| 2901 | } | ||
| 2902 | } else { | ||
| 2903 | $res = 0; | ||
| 2904 | $to_num = 1; | ||
| 2905 | } | ||
| 2906 | } | ||
| 2907 | } | ||
| 2908 | } elsif ($cmd eq 'end') { | ||
| 2909 | # https://php.net/manual/en/function.end.php | ||
| 2910 | # | ||
| 2911 | if (scalar @$args == 1) { | ||
| 2912 | my $var = $$args[0]; # ref var | ||
| 2913 | my ($is_valid, $val) = get_refvar_val($ctx, $var); | ||
| 2914 | |||
| 2915 | if ($is_valid) { | ||
| 2916 | if (defined $val && is_array($val)) { | ||
| 2917 | my $arr = $parser->{strmap}{$val}; | ||
| 2918 | my $keys = $arr->get_keys(); | ||
| 2919 | if (scalar @$keys == 0) { | ||
| 2920 | $res = 0; | ||
| 2921 | $to_num = 1; | ||
| 2922 | } else { | ||
| 2923 | my $pos = scalar @$keys - 1; | ||
| 2924 | my $k = $keys->[$pos]; | ||
| 2925 | $res = $arr->val($k); | ||
| 2926 | $arr->set_pos($pos); | ||
| 2927 | return $res; | ||
| 2928 | } | ||
| 2929 | } else { | ||
| 2930 | $res = 0; | ||
| 2931 | $to_num = 1; | ||
| 2932 | } | ||
| 2933 | } | ||
| 2934 | } | ||
| 2935 | } elsif (($cmd eq 'current') || ($cmd eq 'pos')) { | ||
| 2936 | # https://php.net/manual/en/function.current.php | ||
| 2937 | # | ||
| 2938 | if (scalar @$args == 1) { | ||
| 2939 | my $var = $$args[0]; | ||
| 2940 | |||
| 2941 | unless ($ctx->{infunction} && !$ctx->{incall}) { | ||
| 2942 | if (is_array($var)) { | ||
| 2943 | my $arr = $parser->{strmap}{$var}; | ||
| 2944 | my $pos = $arr->get_pos(); | ||
| 2945 | my $keys = $arr->get_keys(); | ||
| 2946 | if ($pos >= scalar @$keys) { | ||
| 2947 | $res = 0; | ||
| 2948 | $to_num = 1; | ||
| 2949 | } else { | ||
| 2950 | my $k = $keys->[$pos]; | ||
| 2951 | $res = $arr->val($k); | ||
| 2952 | return $res; | ||
| 2953 | } | ||
| 2954 | } else { | ||
| 2955 | $res = 0; | ||
| 2956 | $to_num = 1; | ||
| 2957 | } | ||
| 2958 | } | ||
| 2959 | } | ||
| 2960 | } elsif ($cmd eq 'key') { | ||
| 2961 | # https://php.net/manual/en/function.key.php | ||
| 2962 | # | ||
| 2963 | if (scalar @$args == 1) { | ||
| 2964 | my $var = $$args[0]; | ||
| 2965 | |||
| 2966 | unless ($ctx->{infunction} && !$ctx->{incall}) { | ||
| 2967 | if (is_array($var)) { | ||
| 2968 | my $arr = $parser->{strmap}{$var}; | ||
| 2969 | my $pos = $arr->get_pos(); | ||
| 2970 | my $keys = $arr->get_keys(); | ||
| 2971 | if ($pos >= scalar @$keys) { | ||
| 2972 | $res = '#null'; | ||
| 2973 | return $res; | ||
| 2974 | } else { | ||
| 2975 | my $k = $keys->[$pos]; | ||
| 2976 | if (is_int_index($k)) { | ||
| 2977 | $k = $parser->setnum($k); | ||
| 2978 | } | ||
| 2979 | return $k; | ||
| 2980 | } | ||
| 2981 | } else { | ||
| 2982 | $res = '#null'; | ||
| 2983 | return $res; | ||
| 2984 | } | ||
| 2985 | } | ||
| 2986 | } | ||
| 2987 | } elsif ($cmd eq 'array_map') { | ||
| 2988 | # https://php.net/manual/en/function.array-map.php | ||
| 2989 | # | ||
| 2990 | if (scalar @$args == 2) { | ||
| 2991 | my $name = $$args[0]; | ||
| 2992 | my $param = $$args[1]; | ||
| 2993 | if ((is_strval($name) || is_variable($name)) && is_array($param)) { | ||
| 2994 | my $arr = $parser->{strmap}{$param}; | ||
| 2995 | my $keys = $arr->get_keys(); | ||
| 2996 | |||
| 2997 | if (is_strval($name)) { | ||
| 2998 | $name = $parser->{strmap}{$name}; | ||
| 2999 | } | ||
| 3000 | if (defined $keys) { | ||
| 3001 | my $newarr = $parser->newarr(); | ||
| 3002 | foreach my $k (@$keys) { | ||
| 3003 | my $val = $arr->val($k); | ||
| 3004 | my $c = $parser->setcall($name, [$val]); | ||
| 3005 | my $v = $ctx->exec_statement($c); | ||
| 3006 | $newarr->set($k, $v); | ||
| 3007 | } | ||
| 3008 | return $newarr->{name}; | ||
| 3009 | } | ||
| 3010 | } | ||
| 3011 | } | ||
| 3012 | } elsif ($cmd eq 'array_walk') { | ||
| 3013 | # https://php.net/manual/en/function.array-walk.php | ||
| 3014 | # | ||
| 3015 | if (scalar @$args >= 2) { | ||
| 3016 | my $var = $$args[0]; # ref var | ||
| 3017 | my $name = $$args[1]; # callable | ||
| 3018 | my $arg; | ||
| 3019 | if (is_strval($name)) { | ||
| 3020 | $name = $parser->{strmap}->{$name}; | ||
| 3021 | } | ||
| 3022 | if (scalar @$args > 2) { | ||
| 3023 | $arg = $$args[2]; | ||
| 3024 | } | ||
| 3025 | my ($is_valid, $val) = get_refvar_val($ctx, $var); | ||
| 3026 | |||
| 3027 | if ($is_valid) { | ||
| 3028 | if (defined $val && is_array($val)) { | ||
| 3029 | # todo: need only to create new array if handler has ref-param | ||
| 3030 | # | ||
| 3031 | my $arr = $parser->{strmap}{$val}; | ||
| 3032 | my $keys = $arr->get_keys(); | ||
| 3033 | my $newarr = $parser->newarr(); | ||
| 3034 | foreach my $k (@$keys) { | ||
| 3035 | my $oldval = $arr->val($k); | ||
| 3036 | my $k2 = $k; | ||
| 3037 | if (is_int_index($k)) { | ||
| 3038 | $k2 = $parser->setnum($k); | ||
| 3039 | } | ||
| 3040 | my $v = $var.'__walktmp'; | ||
| 3041 | $ctx->setvar($v, $oldval, 1); | ||
| 3042 | my $c = $parser->setcall($name, defined $arg ? [$v, $k2, $arg] : [$v, $k2]); | ||
| 3043 | my $r = $ctx->exec_statement($c, 1); | ||
| 3044 | my $n = $ctx->getvar($v, 1); | ||
| 3045 | $newarr->set($k, $n); | ||
| 3046 | } | ||
| 3047 | set_refvar_val($ctx, $var, $newarr->{name}); | ||
| 3048 | } | ||
| 3049 | $res = 1; | ||
| 3050 | $to_num = 1; | ||
| 3051 | } | ||
| 3052 | } | ||
| 3053 | } elsif ($cmd eq 'array_rand') { | ||
| 3054 | # https://php.net/manual/en/function.array-rand.php | ||
| 3055 | # | ||
| 3056 | if (scalar @$args >= 1) { | ||
| 3057 | my $param = $$args[0]; | ||
| 3058 | my $num = 1; | ||
| 3059 | if (scalar @$args == 2) { | ||
| 3060 | $num = $parser->get_strval($$args[1]); | ||
| 3061 | } | ||
| 3062 | unless ($ctx->{infunction} && !$ctx->{incall}) { | ||
| 3063 | # don't precalc rand() in function body | ||
| 3064 | # | ||
| 3065 | if (is_array($param)) { | ||
| 3066 | my $arr = $parser->{strmap}{$param}; | ||
| 3067 | my $keys = $arr->get_keys(); | ||
| 3068 | if (!defined $keys || ($num > scalar @$keys)) { | ||
| 3069 | $res = 0; | ||
| 3070 | $to_num = 1; | ||
| 3071 | } elsif ($num == 1) { | ||
| 3072 | my $i = int(rand(scalar @$keys)); | ||
| 3073 | my $k = $keys->[$i]; # return key | ||
| 3074 | if (is_int_index($k)) { | ||
| 3075 | $res = $k; | ||
| 3076 | $to_num = 1; | ||
| 3077 | } else { | ||
| 3078 | $res = $parser->get_strval($k); | ||
| 3079 | } | ||
| 3080 | } else { | ||
| 3081 | # keep random keys uniq & in order | ||
| 3082 | my @randidx = shuffle(0 .. $#$keys); | ||
| 3083 | @randidx = sort { $a <=> $b } @randidx[0..$num-1]; | ||
| 3084 | |||
| 3085 | my $newarr = $parser->newarr(); | ||
| 3086 | foreach (my $i=0; $i < $num; $i++) { | ||
| 3087 | my $k = $keys->[$randidx[$i]]; | ||
| 3088 | if (is_int_index($k)) { | ||
| 3089 | $k = $parser->setnum($k); | ||
| 3090 | } | ||
| 3091 | $newarr->set(undef, $k); # append key | ||
| 3092 | } | ||
| 3093 | return $newarr->{name}; | ||
| 3094 | } | ||
| 3095 | } | ||
| 3096 | } | ||
| 3097 | } | ||
| 3098 | } elsif ($cmd eq 'round') { | ||
| 3099 | if (scalar @$args >= 1) { | ||
| 3100 | my $s = $parser->get_strval($$args[0]); | ||
| 3101 | my $precision = 0; | ||
| 3102 | if (scalar @$args == 2) { | ||
| 3103 | $precision = $parser->get_strval($$args[1]); | ||
| 3104 | } | ||
| 3105 | if (defined $s && defined $precision) { | ||
| 3106 | if ($precision == 0) { | ||
| 3107 | $res = int($s + 0.5); | ||
| 3108 | } else { | ||
| 3109 | $res = sprintf "%0.*f", $precision, $s; | ||
| 3110 | } | ||
| 3111 | $to_num = 1; | ||
| 3112 | } | ||
| 3113 | } | ||
| 3114 | } elsif (($cmd eq 'rand') || ($cmd eq 'mt_rand')) { | ||
| 3115 | my $min = 0; | ||
| 3116 | my $max = 32767; | ||
| 3117 | if (scalar @$args == 2) { | ||
| 3118 | $min = $parser->get_strval($$args[0]); | ||
| 3119 | $max = $parser->get_strval($$args[1]); | ||
| 3120 | } | ||
| 3121 | if (defined $min && defined $max) { | ||
| 3122 | # don't precalc rand() in function body | ||
| 3123 | # | ||
| 3124 | unless ($ctx->{infunction} && !$ctx->{incall}) { | ||
| 3125 | my $range = int($max) - int($min); | ||
| 3126 | $res = int(rand($range)) + int($min); | ||
| 3127 | $to_num = 1; | ||
| 3128 | } | ||
| 3129 | } | ||
| 3130 | } elsif ($cmd eq 'empty') { | ||
| 3131 | # https://php.net/manual/en/function.empty.php | ||
| 3132 | # (https://php.net/manual/en/types.comparisons.php) | ||
| 3133 | # | ||
| 3134 | if (scalar @$args >= 1) { | ||
| 3135 | my $val = $$args[0]; | ||
| 3136 | if (is_array($val)) { | ||
| 3137 | my $arr = $parser->{strmap}{$val}; | ||
| 3138 | $res = $arr->empty() ? 1 : 0; | ||
| 3139 | $to_num = 1; | ||
| 3140 | } elsif (is_null($val)) { | ||
| 3141 | $res = 1; | ||
| 3142 | $to_num = 1; | ||
| 3143 | } elsif (is_strval($val)) { | ||
| 3144 | my $s = $parser->get_strval($val); | ||
| 3145 | if (!defined $s) { | ||
| 3146 | $res = 1; | ||
| 3147 | } elsif ($s eq '0') { | ||
| 3148 | $res = 1; | ||
| 3149 | } elsif (($val =~ /^(\#str\d+)$/) && ($s eq '')) { | ||
| 3150 | $res = 1; | ||
| 3151 | } else { | ||
| 3152 | $res = 0; | ||
| 3153 | } | ||
| 3154 | $to_num = 1; | ||
| 3155 | } elsif (is_variable($val)) { | ||
| 3156 | my $v = $ctx->getvar($val); | ||
| 3157 | #$ctx->{log}->($ctx, 'cmd', $cmd, "$val -> $v: (tainted: %s)", $ctx->{tainted}) if $ctx->{log}; | ||
| 3158 | if (!defined $v) { | ||
| 3159 | unless (&parsing_func($ctx) || $ctx->{tainted}) { | ||
| 3160 | unless ($ctx->{skipundef}) { | ||
| 3161 | $res = 1; | ||
| 3162 | $to_num = 1; | ||
| 3163 | } | ||
| 3164 | } | ||
| 3165 | } elsif ($v eq '#unresolved') { | ||
| 3166 | $ctx->{warn}->($ctx, 'cmd', $cmd, "$val is #unresolved"); | ||
| 3167 | } | ||
| 3168 | } elsif ($val =~ /^(\#elem\d+)$/) { | ||
| 3169 | my ($v, $i) = @{$parser->{strmap}->{$val}}; | ||
| 3170 | my ($basevar, $has_index, $idxstr) = $ctx->resolve_variable($val, 0); | ||
| 3171 | if (defined $basevar) { | ||
| 3172 | if ($has_index) { | ||
| 3173 | my $basestr = $ctx->exec_statement($basevar, 0); | ||
| 3174 | if (defined $basestr && is_array($basestr) && defined $idxstr) { | ||
| 3175 | $idxstr = $parser->setstr('') if is_null($idxstr); # null maps to '' array index | ||
| 3176 | my $arr = $parser->{strmap}{$basestr}; | ||
| 3177 | my $arrval = $arr->get($idxstr); | ||
| 3178 | if (defined $arrval) { | ||
| 3179 | my $s = $parser->get_strval($arrval); | ||
| 3180 | if (!defined $s) { | ||
| 3181 | $res = 1; | ||
| 3182 | } elsif ($s eq '0') { | ||
| 3183 | $res = 1; | ||
| 3184 | } elsif (($arrval =~ /^(\#str\d+)$/) && ($s eq '')) { | ||
| 3185 | $res = 1; | ||
| 3186 | } else { | ||
| 3187 | $res = 0; | ||
| 3188 | } | ||
| 3189 | } elsif (!$ctx->is_superglobal($basevar)) { | ||
| 3190 | $res = 1; | ||
| 3191 | } | ||
| 3192 | $to_num = 1; | ||
| 3193 | } elsif (!defined $basestr && (!$ctx->is_superglobal($basevar) || ($basevar =~ /^\$GLOBALS$/))) { | ||
| 3194 | unless ($ctx->{tainted}) { | ||
| 3195 | $ctx->{warn}->($ctx, 'cmd', $cmd, "$val %s[%s] is #unresolved", $basevar, defined $i ? $i : ''); | ||
| 3196 | } | ||
| 3197 | } | ||
| 3198 | } else { | ||
| 3199 | my $v = $ctx->getvar($basevar); | ||
| 3200 | if (!defined $v) { | ||
| 3201 | unless (&parsing_func($ctx) || $ctx->{tainted}) { | ||
| 3202 | unless ($ctx->{skipundef}) { | ||
| 3203 | $res = 1; | ||
| 3204 | $to_num = 1; | ||
| 3205 | } | ||
| 3206 | } | ||
| 3207 | } elsif ($v eq '#unresolved') { | ||
| 3208 | $ctx->{warn}->($ctx, 'cmd', $cmd, "$val is #unresolved"); | ||
| 3209 | } | ||
| 3210 | } | ||
| 3211 | } | ||
| 3212 | } | ||
| 3213 | } | ||
| 3214 | } elsif ($cmd eq 'is_array') { | ||
| 3215 | # https://www.php.net/manual/en/function.is-array.php | ||
| 3216 | # | ||
| 3217 | if (scalar @$args == 1) { | ||
| 3218 | my $val = $$args[0]; | ||
| 3219 | if (is_array($val)) { | ||
| 3220 | $res = 1; | ||
| 3221 | $to_num = 1; | ||
| 3222 | } elsif ($ctx->is_superglobal($val)) { | ||
| 3223 | $res = 1; | ||
| 3224 | $to_num = 1; | ||
| 3225 | } elsif (is_null($val)) { | ||
| 3226 | $res = 0; | ||
| 3227 | $to_num = 1; | ||
| 3228 | } elsif (is_strval($val)) { | ||
| 3229 | $res = 0; | ||
| 3230 | $to_num = 1; | ||
| 3231 | } | ||
| 3232 | } | ||
| 3233 | } elsif ($cmd eq 'is_null') { | ||
| 3234 | # https://php.net/manual/en/function.is-null.php (similar to $a === null) | ||
| 3235 | # (https://php.net/manual/en/types.comparisons.php) | ||
| 3236 | # | ||
| 3237 | if (scalar @$args >= 1) { | ||
| 3238 | my $val = $$args[0]; | ||
| 3239 | if (is_array($val)) { | ||
| 3240 | $res = 0; | ||
| 3241 | $to_num = 1; | ||
| 3242 | } elsif (is_null($val)) { | ||
| 3243 | $res = 1; | ||
| 3244 | $to_num = 1; | ||
| 3245 | } elsif (is_strval($val)) { | ||
| 3246 | my $s = $parser->get_strval($val); | ||
| 3247 | if (!defined $s) { | ||
| 3248 | $res = 1; | ||
| 3249 | } else { | ||
| 3250 | $res = 0; | ||
| 3251 | } | ||
| 3252 | $to_num = 1; | ||
| 3253 | } elsif (is_variable($val)) { | ||
| 3254 | my $v = $ctx->getvar($val); | ||
| 3255 | if (!defined $v) { | ||
| 3256 | unless (&parsing_func($ctx) || $ctx->{tainted}) { | ||
| 3257 | unless ($ctx->{skipundef}) { | ||
| 3258 | if ($ctx->is_superglobal($val)) { | ||
| 3259 | # superglobals always exist as empty array, | ||
| 3260 | # Since it is unknown which fields exist in a real environment, | ||
| 3261 | # don't create superglobal array on the fly here. | ||
| 3262 | # | ||
| 3263 | $res = 0; | ||
| 3264 | } else { | ||
| 3265 | $res = 1; | ||
| 3266 | } | ||
| 3267 | $to_num = 1; | ||
| 3268 | } | ||
| 3269 | } | ||
| 3270 | } elsif ($v eq '#unresolved') { | ||
| 3271 | $ctx->{warn}->($ctx, 'cmd', $cmd, "$val is #unresolved"); | ||
| 3272 | } | ||
| 3273 | } elsif ($val =~ /^(\#elem\d+)$/) { | ||
| 3274 | my ($v, $i) = @{$parser->{strmap}->{$val}}; | ||
| 3275 | my ($basevar, $has_index, $idxstr) = $ctx->resolve_variable($val, 0); | ||
| 3276 | if (defined $basevar) { | ||
| 3277 | my $basestr = $ctx->exec_statement($basevar, 0); | ||
| 3278 | if (defined $basestr && is_array($basestr) && defined $idxstr) { | ||
| 3279 | $idxstr = $parser->setstr('') if is_null($idxstr); # null maps to '' array index | ||
| 3280 | my $arr = $parser->{strmap}{$basestr}; | ||
| 3281 | my $arrval = $arr->get($idxstr); | ||
| 3282 | if (defined $arrval) { | ||
| 3283 | $res = 0; | ||
| 3284 | } elsif (!$ctx->is_superglobal($basevar)) { | ||
| 3285 | $res = 1; | ||
| 3286 | } | ||
| 3287 | $to_num = 1; | ||
| 3288 | } elsif (!defined $basestr && (!$ctx->is_superglobal($basevar) || ($basevar =~ /^\$GLOBALS$/))) { | ||
| 3289 | unless ($ctx->{tainted}) { | ||
| 3290 | $ctx->{warn}->($ctx, 'cmd', $cmd, "$val %s[%s] is #unresolved", $basevar, defined $i ? $i : ''); | ||
| 3291 | } | ||
| 3292 | } | ||
| 3293 | } | ||
| 3294 | } | ||
| 3295 | } | ||
| 3296 | } elsif ($cmd eq 'is_null_weak') { | ||
| 3297 | # https://php.net/manual/en/types.comparisons.php (similar to $a == null) | ||
| 3298 | # | ||
| 3299 | if (scalar @$args >= 1) { | ||
| 3300 | my $val = $$args[0]; | ||
| 3301 | if (is_array($val)) { | ||
| 3302 | $res = 1; | ||
| 3303 | $to_num = 1; | ||
| 3304 | } elsif (is_null($val)) { | ||
| 3305 | $res = 1; | ||
| 3306 | $to_num = 1; | ||
| 3307 | } elsif (is_strval($val)) { | ||
| 3308 | my $s = $parser->get_strval($val); | ||
| 3309 | if (!defined $s) { | ||
| 3310 | $res = 1; | ||
| 3311 | } elsif (($val =~ /^(\#num\d+)$/) && ($s == 0)) { | ||
| 3312 | $res = 1; | ||
| 3313 | } elsif ($s eq '0') { | ||
| 3314 | $res = 0; | ||
| 3315 | } elsif (($val =~ /^(\#str\d+)$/) && ($s eq '')) { | ||
| 3316 | $res = 1; | ||
| 3317 | } elsif ($val =~ /^(\#null)$/) { | ||
| 3318 | $res = 1; | ||
| 3319 | } else { | ||
| 3320 | $res = 0; | ||
| 3321 | } | ||
| 3322 | $to_num = 1; | ||
| 3323 | } elsif (is_variable($val)) { | ||
| 3324 | my $v = $ctx->getvar($val); | ||
| 3325 | if (!defined $v) { | ||
| 3326 | unless (&parsing_func($ctx) || $ctx->{tainted}) { | ||
| 3327 | unless ($ctx->{skipundef}) { | ||
| 3328 | if ($ctx->is_superglobal($val)) { | ||
| 3329 | # superglobals always exist as empty array, | ||
| 3330 | # Since it is unknown which fields exist in a real environment, | ||
| 3331 | # don't create superglobal array on the fly here. | ||
| 3332 | # | ||
| 3333 | $res = 0; | ||
| 3334 | } else { | ||
| 3335 | $res = 1; | ||
| 3336 | } | ||
| 3337 | $to_num = 1; | ||
| 3338 | } | ||
| 3339 | } | ||
| 3340 | } elsif ($v eq '#unresolved') { | ||
| 3341 | $ctx->{warn}->($ctx, 'cmd', $cmd, "$val is #unresolved"); | ||
| 3342 | } | ||
| 3343 | } elsif ($val =~ /^(\#elem\d+)$/) { | ||
| 3344 | my ($v, $i) = @{$parser->{strmap}->{$val}}; | ||
| 3345 | my ($basevar, $has_index, $idxstr) = $ctx->resolve_variable($val, 0); | ||
| 3346 | if (defined $basevar) { | ||
| 3347 | my $basestr = $ctx->exec_statement($basevar, 0); | ||
| 3348 | if (defined $basestr && is_array($basestr) && defined $idxstr) { | ||
| 3349 | $idxstr = $parser->setstr('') if is_null($idxstr); # null maps to '' array index | ||
| 3350 | my $arr = $parser->{strmap}{$basestr}; | ||
| 3351 | my $arrval = $arr->get($idxstr); | ||
| 3352 | if (defined $arrval) { | ||
| 3353 | my $s = $parser->get_strval($arrval); | ||
| 3354 | if (!defined $s) { | ||
| 3355 | $res = 1; | ||
| 3356 | } elsif (($arrval =~ /^(\#num\d+)$/) && ($s == 0)) { | ||
| 3357 | $res = 1; | ||
| 3358 | } elsif ($arrval eq '0') { | ||
| 3359 | $res = 1; | ||
| 3360 | } elsif (($arrval =~ /^(\#str\d+)$/) && ($s eq '')) { | ||
| 3361 | $res = 1; | ||
| 3362 | } elsif ($arrval =~ /^(\#null)$/) { | ||
| 3363 | $res = 1; | ||
| 3364 | } else { | ||
| 3365 | $res = 0; | ||
| 3366 | } | ||
| 3367 | } elsif (!$ctx->is_superglobal($basevar)) { | ||
| 3368 | $res = 1; | ||
| 3369 | } | ||
| 3370 | $to_num = 1; | ||
| 3371 | } elsif (!defined $basestr && (!$ctx->is_superglobal($basevar) || ($basevar =~ /^\$GLOBALS$/))) { | ||
| 3372 | unless ($ctx->{tainted}) { | ||
| 3373 | $ctx->{warn}->($ctx, 'cmd', $cmd, "$val %s[%s] is #unresolved", $basevar, defined $i ? $i : ''); | ||
| 3374 | } | ||
| 3375 | } | ||
| 3376 | } | ||
| 3377 | } | ||
| 3378 | } | ||
| 3379 | } elsif ($cmd eq 'isset') { | ||
| 3380 | my $inv = &exec_cmd($ctx, 'is_null', $args); | ||
| 3381 | if (defined $inv && is_strval($inv)) { | ||
| 3382 | if ($parser->{strmap}{$inv} eq '0') { | ||
| 3383 | $res = 1; | ||
| 3384 | } else { | ||
| 3385 | $res = 0; | ||
| 3386 | } | ||
| 3387 | $to_num = 1; | ||
| 3388 | } | ||
| 3389 | } elsif ($cmd eq 'define') { | ||
| 3390 | if (scalar @$args == 2) { | ||
| 3391 | my $key = $parser->get_strval($$args[0]); | ||
| 3392 | my $val = $parser->get_strval($$args[1]); | ||
| 3393 | if (defined $key && defined $val) { | ||
| 3394 | if (defined $ctx->{defines}) { | ||
| 3395 | $ctx->{defines}{$key} = $$args[1]; | ||
| 3396 | $ctx->{log}->($ctx, 'cmd', $cmd, "$key = $val") if $ctx->{log}; | ||
| 3397 | # no result here | ||
| 3398 | return '#noreturn'; | ||
| 3399 | } | ||
| 3400 | } | ||
| 3401 | } | ||
| 3402 | } elsif ($cmd eq 'function_exists') { | ||
| 3403 | # https://php.net/manual/en/function.function-exists.php | ||
| 3404 | # | ||
| 3405 | if (scalar @$args == 1) { | ||
| 3406 | my $fn = $parser->get_strval($$args[0]); | ||
| 3407 | if (defined $fn) { | ||
| 3408 | my $lfn = lc($fn); | ||
| 3409 | |||
| 3410 | # php functions are always global | ||
| 3411 | # - so a user defined func is valid also in tainted mode | ||
| 3412 | # | ||
| 3413 | unless ($ctx->{skipundef}) { | ||
| 3414 | if ($ctx->getfun($fn)) { | ||
| 3415 | $res = 1; | ||
| 3416 | } | ||
| 3417 | } | ||
| 3418 | unless (defined $res) { | ||
| 3419 | if (get_php_func($lfn)) { | ||
| 3420 | $res = 1; | ||
| 3421 | } | ||
| 3422 | } | ||
| 3423 | unless (defined $res) { | ||
| 3424 | if ($ctx->{tainted}) { | ||
| 3425 | $ctx->{warn}->($ctx, 'cmd', $cmd, "$fn not found but ctx tainted -> skip execution"); | ||
| 3426 | return; | ||
| 3427 | } else { | ||
| 3428 | $res = 0; | ||
| 3429 | } | ||
| 3430 | } | ||
| 3431 | $to_num = 1; | ||
| 3432 | } | ||
| 3433 | } | ||
| 3434 | } elsif ($cmd eq 'create_function') { | ||
| 3435 | # https://php.net/manual/en/function.create-function.php | ||
| 3436 | # (see also: anonymous functions) | ||
| 3437 | # | ||
| 3438 | # This is basically what PHP does internally: | ||
| 3439 | # function create_function($args, $code) { | ||
| 3440 | # // create a random $functionName | ||
| 3441 | # eval('function ' . $functionName . '($args){$code}'); | ||
| 3442 | # return $functionName; | ||
| 3443 | # } | ||
| 3444 | # | ||
| 3445 | # It's why you can do something like this (which works on the same principle as SQL injection): | ||
| 3446 | # $code = 'print "I print repeatedly.\n"; } print "I print once.\n"; if (false) {'; | ||
| 3447 | # $function = create_function('', $code); | ||
| 3448 | # call_user_func($function); | ||
| 3449 | # call_user_func($function); | ||
| 3450 | # | ||
| 3451 | # // I print once. | ||
| 3452 | # // I print repeatedly. | ||
| 3453 | # // I print repeatedly. | ||
| 3454 | # | ||
| 3455 | if (scalar @$args == 2) { | ||
| 3456 | my $params = $parser->get_strval($$args[0]); | ||
| 3457 | my $code = $parser->get_strval($$args[1]); | ||
| 3458 | if (defined $args && defined $code) { | ||
| 3459 | my @arglist; | ||
| 3460 | foreach my $p (split(/,/, $params)) { | ||
| 3461 | my $stmt = $parser->read_statement([$p], undef); | ||
| 3462 | push(@arglist, $stmt); | ||
| 3463 | } | ||
| 3464 | # eval with new parser and without global & local varmap | ||
| 3465 | # | ||
| 3466 | my $codestr = $parser->setstr('function ('.$params.') {'.$code.'}'); | ||
| 3467 | my $parser2 = $parser->subparser(); | ||
| 3468 | my $ctx2 = $ctx->subctx(globals => {}, varmap => {}, parser => $parser2, tainted => 1, varhist => {}); | ||
| 3469 | my $block = $ctx2->parse_eval($codestr); | ||
| 3470 | my $stmt = $ctx2->exec_eval($block); | ||
| 3471 | $stmt = $ctx->exec_statement($stmt); | ||
| 3472 | return $stmt; | ||
| 3473 | } | ||
| 3474 | } | ||
| 3475 | } elsif ($cmd eq 'call_user_func') { | ||
| 3476 | # https://php.net/manual/en/function.call-user-func.php | ||
| 3477 | # | ||
| 3478 | if (scalar @$args >= 1) { | ||
| 3479 | my $name = $$args[0]; | ||
| 3480 | if (is_strval($name) || is_variable($name)) { | ||
| 3481 | if (is_strval($name)) { | ||
| 3482 | $name = $parser->{strmap}->{$name}; | ||
| 3483 | } | ||
| 3484 | my @arglist = @$args[1..@$args-1]; | ||
| 3485 | my $call = $parser->setcall($name, \@arglist); | ||
| 3486 | return $call; | ||
| 3487 | } | ||
| 3488 | } | ||
| 3489 | } elsif ($cmd eq 'call_user_func_array') { | ||
| 3490 | # https://php.net/manual/en/function.call-user-func-array.php | ||
| 3491 | # | ||
| 3492 | if (scalar @$args == 2) { | ||
| 3493 | my $name = $$args[0]; | ||
| 3494 | my $param = $$args[1]; | ||
| 3495 | if ((is_strval($name) || is_variable($name)) && is_array($param)) { | ||
| 3496 | my $arr = $parser->{strmap}{$param}; | ||
| 3497 | my $keys = $arr->get_keys(); | ||
| 3498 | if (is_strval($name)) { | ||
| 3499 | $name = $parser->{strmap}->{$name}; | ||
| 3500 | } | ||
| 3501 | if (@$keys) { | ||
| 3502 | my @arglist = (); | ||
| 3503 | foreach my $k (@$keys) { | ||
| 3504 | my $val = $arr->val($k); | ||
| 3505 | my $v = $ctx->exec_statement($val); | ||
| 3506 | push(@arglist, $v); | ||
| 3507 | } | ||
| 3508 | my $call = $parser->setcall($name, \@arglist); | ||
| 3509 | return $call; | ||
| 3510 | } | ||
| 3511 | } | ||
| 3512 | } | ||
| 3513 | } elsif ($cmd eq 'file') { | ||
| 3514 | # https://php.net/manual/en/function.file.php | ||
| 3515 | # - TODO: also URL possible | ||
| 3516 | # | ||
| 3517 | if (scalar @$args >= 1) { | ||
| 3518 | my $filename = $parser->get_strval($$args[0]); | ||
| 3519 | my $flags = 0; | ||
| 3520 | |||
| 3521 | if (scalar @$args > 1) { | ||
| 3522 | $flags = $parser->get_strval($$args[1]); | ||
| 3523 | } | ||
| 3524 | if (defined $filename) { | ||
| 3525 | my ($fh) = $parser->newfh($filename, 'r'); | ||
| 3526 | if (defined $fh) { | ||
| 3527 | my $buf = $parser->{strmap}->{$fh}{buf}; | ||
| 3528 | $parser->{strmap}->{$fh}{buf} = ''; | ||
| 3529 | # split in lines and keep trailing newlines | ||
| 3530 | # | ||
| 3531 | my @lines; | ||
| 3532 | if ($flags =~ /FILE_IGNORE_NEW_LINES/) { | ||
| 3533 | @lines = split(/\n/, $buf); | ||
| 3534 | } else { | ||
| 3535 | @lines = split(/^/, $buf); | ||
| 3536 | } | ||
| 3537 | my $arr = $parser->newarr(); | ||
| 3538 | |||
| 3539 | foreach my $p (@lines) { | ||
| 3540 | $ctx->{log}->($ctx, 'cmd', $cmd, "[$filename] line: $p") if $ctx->{log}; | ||
| 3541 | my $k = $parser->setstr($p); | ||
| 3542 | $arr->set(undef, $k); | ||
| 3543 | } | ||
| 3544 | return $arr->{name}; | ||
| 3545 | } | ||
| 3546 | } | ||
| 3547 | } | ||
| 3548 | } elsif ($cmd eq 'file_get_contents') { | ||
| 3549 | # https://php.net/manual/en/function.file-get-contents.php | ||
| 3550 | # | ||
| 3551 | if (scalar @$args == 1) { | ||
| 3552 | my $filename = $parser->get_strval($$args[0]); | ||
| 3553 | |||
| 3554 | if (defined $filename) { | ||
| 3555 | my ($fh) = $parser->newfh($filename, 'r'); | ||
| 3556 | if (defined $fh) { | ||
| 3557 | my $buf = $parser->{strmap}->{$fh}{buf}; | ||
| 3558 | $parser->{strmap}->{$fh}{buf} = ''; | ||
| 3559 | $res = $buf; | ||
| 3560 | } | ||
| 3561 | } | ||
| 3562 | } | ||
| 3563 | } elsif ($cmd eq 'reset') { | ||
| 3564 | # https://php.net/manual/en/function.reset.php | ||
| 3565 | # reset next()-pointer & return first array element | ||
| 3566 | # | ||
| 3567 | if (scalar @$args == 1) { | ||
| 3568 | my $basestr = $$args[0]; | ||
| 3569 | |||
| 3570 | if (is_array($basestr)) { | ||
| 3571 | my $arr = $parser->{strmap}{$basestr}; | ||
| 3572 | my $keys = $arr->get_keys(); | ||
| 3573 | |||
| 3574 | #my @vals = map { $arr->val($_) } @$keys; | ||
| 3575 | $res = $arr->val($keys->[0]); | ||
| 3576 | } | ||
| 3577 | } | ||
| 3578 | } elsif ($cmd eq 'fopen') { | ||
| 3579 | if (scalar @$args >= 2) { | ||
| 3580 | my $filename = $parser->get_strval($$args[0]); | ||
| 3581 | my $mode = $parser->get_strval($$args[1]); | ||
| 3582 | if (defined $filename && defined $mode) { | ||
| 3583 | my ($fh) = $parser->newfh($filename, $mode); | ||
| 3584 | return $fh; | ||
| 3585 | } | ||
| 3586 | } | ||
| 3587 | } elsif ($cmd eq 'fclose') { | ||
| 3588 | if (scalar @$args == 1) { | ||
| 3589 | my $fh = $$args[0]; | ||
| 3590 | if ($fh =~ /^(\#fh\d+)$/) { | ||
| 3591 | $parser->{strmap}->{$fh}{pos} = 0; | ||
| 3592 | $parser->{strmap}->{$fh}{buf} = ''; | ||
| 3593 | } | ||
| 3594 | } | ||
| 3595 | } elsif ($cmd eq 'fread') { | ||
| 3596 | if (scalar @$args == 2) { | ||
| 3597 | my $fh = $$args[0]; | ||
| 3598 | my $cnt = $parser->get_strval($$args[1]); | ||
| 3599 | if (($fh =~ /^(\#fh\d+)$/) && defined $cnt) { | ||
| 3600 | my $pos = $parser->{strmap}->{$fh}{pos}; | ||
| 3601 | my $buf = $parser->{strmap}->{$fh}{buf}; | ||
| 3602 | if ($cnt > length($buf) - $pos) { | ||
| 3603 | $cnt = length($buf) - $pos; | ||
| 3604 | } | ||
| 3605 | $res = substr($buf, $pos, $cnt); | ||
| 3606 | $pos += $cnt; | ||
| 3607 | $parser->{strmap}->{$fh}{pos} = $pos; | ||
| 3608 | } | ||
| 3609 | } | ||
| 3610 | } elsif ($cmd eq 'fgets') { | ||
| 3611 | if (scalar @$args >= 1) { | ||
| 3612 | my $fh = $$args[0]; | ||
| 3613 | my $cnt; | ||
| 3614 | if (scalar @$args == 2) { | ||
| 3615 | $cnt = $parser->get_strval($$args[1]); | ||
| 3616 | } | ||
| 3617 | if ($fh =~ /^(\#fh\d+)$/) { | ||
| 3618 | my $pos = $parser->{strmap}->{$fh}{pos}; | ||
| 3619 | my $buf = $parser->{strmap}->{$fh}{buf}; | ||
| 3620 | $res = ''; | ||
| 3621 | |||
| 3622 | while ($pos < length($buf)) { | ||
| 3623 | my $ch = substr($buf, $pos, 1); | ||
| 3624 | if ($ch eq "\n") { | ||
| 3625 | $pos++; | ||
| 3626 | last; | ||
| 3627 | } | ||
| 3628 | last if (defined $cnt && (length($res) >= $cnt)); | ||
| 3629 | $res .= $ch; | ||
| 3630 | $pos++; | ||
| 3631 | } | ||
| 3632 | $parser->{strmap}->{$fh}{pos} = $pos; | ||
| 3633 | } | ||
| 3634 | } | ||
| 3635 | } | ||
| 3636 | |||
| 3637 | if (defined $res) { | ||
| 3638 | my $k; | ||
| 3639 | if ($to_num) { | ||
| 3640 | $k = $parser->setnum($res); | ||
| 3641 | } else { | ||
| 3642 | $k = $parser->setstr($res); | ||
| 3643 | } | ||
| 3644 | return $k; | ||
| 3645 | } | ||
| 3646 | return; | ||
| 3647 | } | ||
| 3648 | |||
| 3649 | 1; | ||
| 3650 | |||
| 3651 | __END__ |