Created
August 3, 2017 19:51
-
-
Save nylen/866603c7a910d63f59f6bfbd917bc10f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
/* | |
Usage: | |
$ ag --php --skip-vcs-ignores register_rest_route | tee scans/register_rest_route.txt | |
$ scans/register_rest_route-strings.php | |
*/ | |
$f = fopen( dirname( __FILE__ ) . '/register_rest_route.txt', 'r' ); | |
if ( ! $f ) { | |
die(); | |
} | |
while ( ( $line = fgets( $f ) ) !== false ) { | |
preg_match( '#^(?P<path>[^:]+):(?P<line>\d+):(?P<code>.*)$#', $line, $matches ); | |
if ( ! $matches ) { | |
error_log( "bad line: $line" ); | |
break; | |
} | |
$tokens = token_get_all( '<?php ' . $matches['code'] ); | |
$inside_string = false; | |
foreach ( $tokens as $token ) { | |
if ( is_array( $token ) ) { | |
$token_name = token_name( $token[0] ); | |
if ( | |
$token_name === 'T_CONSTANT_ENCAPSED_STRING' || | |
( $token_name === 'T_ENCAPSED_AND_WHITESPACE' && $inside_string ) | |
) { | |
$value = $token[1]; | |
if ( preg_match( '#\((?!\?P)#', $value ) ) { | |
echo "$line"; | |
} | |
} | |
} else if ( $token === '"' ) { | |
$inside_string = ! $inside_string; | |
} | |
} | |
} | |
fclose( $f ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment