Projeto

Geral

Perfil

find_pregs.php

Script auxiliar - Alexandre Rocha Wendling Cassol, 15/06/2022 13:10 h

Baixar (3,18 KB)

 
1
<?php
2
$stdin = file_get_contents( "php://stdin" );
3
define( 'FOUT', $argv[1] );
4
define( 'DEBUG', $argv[2] === '1' );
5
file_put_contents( FOUT, '<?php'.PHP_EOL );
6
$i = 0;
7
function getArg( $str ) {
8
        $str = trim( $str );
9
        if ( DEBUG ) echo PHP_EOL."®".$str."®".PHP_EOL.PHP_EOL;
10
        $d = substr( $str, 0, 1 );
11
        $c = substr( $str, 1 );
12
        if ( $d === '\'' || $d === '"' ) {
13
                $p = explode( $d, preg_replace( '#'.preg_quote( '\\'.$d ).'#', '®', preg_replace( '#'.preg_quote( '\\\\' ).'#', 'ŧ', $c ) ), 2 );
14
                foreach( $p as $k => $v ) $p[$k] = preg_replace( array( '#®#', '#ŧ#' ), array( '\\'.$d, '\\\\\\' ), $v );
15
                $d2 = substr( trim( $p[1] ), 0, 1 );
16
                if ( $d === '"' ) {
17
                        if ( DEBUG ) print_r( $p[0] );
18
                        preg_match_all( '/\$([a-zA-Z0-9_]+(->[a-zA-Z0-9_]+)*)(\[[^\]]*\])*/', $p[0], $matches );
19
                        if ( DEBUG ) print_r( array( 'subject' => $p[0], 'matches' => $matches ) );
20
                        foreach( $matches[0] as $match ) {
21
                                if ( preg_match( '/\[/', $match ) ) {
22
                                        $p[0] = preg_replace( '/®/', preg_replace( '/\[[^\]]*\]/', '', $match ), preg_replace( '/'.preg_quote( $match ).'/', '®', $p[0] ) );
23
                                        $match = preg_replace( '/\[[^\]]*\]/', '', $match );
24
                                }
25
                                if ( preg_match( '/->/', $match ) ) {
26
                                        $p[0] = preg_replace( '/®/', preg_replace( '/->/', '_', $match ), preg_replace( '/'.preg_quote( $match ).'/', '®', $p[0] ) );
27
                                        $match = preg_replace( '/->/', '_', $match );
28
                                }
29
                                file_put_contents( FOUT, $match.'=\'\'; ', FILE_APPEND );
30
                        }
31
                }
32
                if ( DEBUG ) print_r( array( $str, $p, $d2 ) );
33
                return $d.$p[0].$d.( $d2 === '.'? $d2.getArg( substr( trim( $p[1] ), 1 ) ) : '');
34
        }
35
        if ( $d == "$" ) {
36
                $p = trim( preg_replace( '#^[a-zA-Z0-9_]*(\->[a-zA-Z0-9_]*)?(\[[^\]]*\])*#' ,'', $c ) );
37
                $d2 = substr( $p, 0, 1 );
38
                $c2 = substr( $p, 1 );
39
                $var = '$v'.($GLOBALS['i']++);
40
                if ( DEBUG ) print_r( array( $c, $p, $d2, $c2 ) );
41
                file_put_contents( FOUT, $var.'=\'\'; ', FILE_APPEND );
42
                return $var.($d2==="."?$d2.getArg( $c2 ):"");
43
        }
44
        if ( ord( $d )  === 17 ) return getArg( $c );
45
        if ( ord( $d )  === 35 ) return getArg( preg_replace( '/^[^\x11]*/', '', $c ) );
46
        if ( ord( $d )  === 47 && ord( substr( $c, 0, 1 ) ) === 47 ) return getArg( preg_replace( '/^[^\x11]*/', '', $c ) );
47
        if ( preg_match( '#^array_keys\(#', $str ) ) return 'array_keys(array())';
48
        if ( preg_match( '#^array\(#', $str ) ) return 'array()';
49
        if ( DEBUG ) echo '// ERROR ('.ord($d).') <'.preg_replace( '/\?\>/', '', $str ).'>';
50
        file_put_contents( FOUT, '// ERROR ('.ord($d).') <'.preg_replace( '/\?\>/', '', $str ).'>', FILE_APPEND );
51
        return "ERROR";
52
}
53

    
54
foreach ( explode( "\n", $stdin ) as $line ) {
55
        if ( $line === '' ) continue;
56
        list( $fl, $ln, $buf ) = explode( ':', $line, 3 );
57
        foreach ( array_filter( preg_split( '/preg_/', substr( $buf, stripos( $buf, 'preg_' ) ) ) ) as $preg ) {
58
                list( $fnc, $args ) = explode( '(', $preg, 2 );
59
                $fnc = trim( $fnc );
60
                $arg = getArg( $args );
61
                $extras = ',\'\'';
62
                if ( $fnc === 'replace' || $fnc === 'filter' ) $extras .= ',\'\'';
63
                if ( $fnc === 'grep' ) $extras = ',array()';
64
                if ( $fnc === 'replace_callback' ) $extras = ',function(){return \'\';},\'\'';
65
                file_put_contents( FOUT, 'preg_'.$fnc.'('.$arg.$extras.');                                           // '.$fl.':'.$ln.PHP_EOL, FILE_APPEND );
66
        }
67
}