Skip to content

Instantly share code, notes, and snippets.

@szbl
Created December 31, 2012 01:06
Show Gist options
  • Save szbl/4416559 to your computer and use it in GitHub Desktop.
Save szbl/4416559 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<title>JS Searcher</title>
<style type="text/css">
body { font: 14px HelveticaNeue,Helvetica,Arial,Sans-serif; }
textarea.code,pre { width:100%;height:100px;margin:1em;padding:1em;border:1px solid #ccc;background:#f7f7f7;font-family:Monaco,Courier,monospace; }
#summary { margin: 1em; border-collapse: collapse; border: 1px solid #d7d7d7; }
#summary td { border: 1px solid #d7d7d7; padding: .5em; }
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
jQuery( document ).ready(function($){
$( 'a.toggle-href' ).click(function(){
$( $( this ).attr( 'href' ) ).toggle();
return false;
});
$( '.hide' ).hide();
$( 'form' ).submit(function(){
return confirm( 'Are you ABSOLUTELY sure?' );
});
});
</script>
</head>
<body>
<?php
global $js_files_found, $js_files_infected, $js_files_cleaned, $js_files, $js_search_str, $init_dir;
if ( isset( $_POST ) && isset( $_POST['SHOULDIDOIT'] ) && $_POST['SHOULDIDOIT'] == 'DOIT' )
{
define( 'CHANGE_FILES', true );
}
$js_files_found = 0;
$js_files_infected = 0;
$js_files_cleaned = 0;
$js_files = array();
$js_search_str = 'document';
$init_dir = dirname( __FILE__ );
function list_js_files( $directory )
{
global $js_files_found, $js_files;
if ( !is_dir( $directory ) )
return false;
$directory = rtrim( $directory, DIRECTORY_SEPARATOR );
foreach ( glob( $directory . DIRECTORY_SEPARATOR . '*' ) as $file )
{
if ( is_dir( $file ) )
{
list_js_files( $file );
}
elseif ( strtolower( pathinfo( $file, PATHINFO_EXTENSION ) ) == 'js' )
{
$js_files[] = $file;
$js_files_found++;
do_cleanup( $file );
}
}
}
function do_cleanup( $file )
{
global $js_files_cleaned, $js_files_infected, $js_search_str;
/*
* Here we can search, replace, etc. on the file's contents.
*/
$contents = file_get_contents( $file );
if ( preg_match( '/' . preg_quote( $js_search_str, '/' ) . '/mi', $contents, $matches ) == 1 )
{
$js_files_infected++;
if ( defined( 'CHANGE_FILES' ) && CHANGE_FILES )
file_put_contents( $file, $contents );
$contents = preg_replace( '/' . preg_quote( $js_search_str, '/' ) . '/', '', $contents );
$js_files_cleaned++;
}
}
function do_summary()
{
global $js_files_found, $js_files_infected, $js_files_cleaned, $js_files, $js_search_str, $init_dir;
if ( count( $js_files ) > 0 ) : ?>
<h2>File Summary [<a href="#summary" class="toggle-href">Toggle Summary</a>]</h2>
<p>Searched for:</p>
<pre class="code"><?php echo $js_search_str; ?></pre>
<ul>
<li>Found <?php echo (int) $js_files_found; ?> files.</li>
<li>Found <?php echo (int) $js_files_infected; ?> are thought to be infected.</li>
<?php if ( defined( 'CHANGE_FILES' ) && CHANGE_FILES ) : ?>
<li>Cleaned <?php echo (int) $js_files_cleaned; ?> JavaScript files.</li>
<?php endif; ?>
</ul>
<table class="hide" id="summary">
<tbody>
<?php foreach ( $js_files as $file ) : ?>
<tr>
<td><code><a href="<?php
$href = str_replace( trim( $init_dir ) . DIRECTORY_SEPARATOR, '', $file );
if ( '/' != DIRECTORY_SEPARATOR )
$href = str_replace( DIRECTORY_SEPARATOR, '/', $href );
echo $href;
?>"><?php echo $file; ?></a></code></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php if ( !defined( 'CHANGE_FILES' ) || !CHANGE_FILES ) : ?>
<form method="post" action="">
<input type="hidden" value="DOIT" name="SHOULDIDOIT">
<input type="submit" value="Permanently Remove This Code From Files">
</form>
<?php endif; ?>
<?php endif;
}
list_js_files( $init_dir );
do_summary();
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment