Buscar texto en todos los archivos php de una carpeta y sub carpetas  


 Buscar texto en los archivos php de un directorio y subdirectorios para eso teneis que crear un archivo php y pegar el siguiente codigo (Ejemplo buscar.php) ese archivo lo colocas en la raiz de tu proyecto y lo abres en el navegador:

ejemplo : dev.mipagina.com/buscar.php


<?php
// increase memory limit
ini_set('memory_limit', '128M');
// no time limit
set_time_limit(0);
// partial string to search first line for

$hack_str = ' texto a buscar';//en esta parte podeis poner el texto a buscar

// Change $the_dir to the relative path you'd like to start searching/fixing in.
// You can use this if the script is timing out (or just move the script into subdirectories).
$the_dir = './';

function get_infected_files( $dir ) {
    global $hack_str;
    $dir = rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
    $d = opendir($dir);
    $files = array();
    if ( $d ) {
        while ( $f = readdir($d) ) {
            $path = $dir . $f;
               
            if ( is_dir($path) ) {
                if ( $f != '.' && $f != '..' ) {
                    $more_files = get_infected_files($path);
                    if ( count($more_files) > 0 ) {
                        $files[] = $more_files;
                    }
                }
            }
            else {
                if ( strpos($f, '.php') !== false ) {
                    $contents = explode("\n", file_get_contents($path));
                    if ( strpos($contents[0], $hack_str, 0) !== false ) {
                        $files[] = $path;
                    }
                }
            }
        }
    }
    return $files;
}

function print_files( $files ) {
    if ( count($files) > 0 ) {
        foreach ( $files as $file ) {
            if ( is_array($file) ) {
                print_files($file);
            }
            else {
                echo $file . '<br />';
            }
        }
    }
}

function fix_files( $files ) {
    global $hack_str;
    foreach ( $files as $file ) {
        if ( is_array($file) ) {
            fix_files($file);
        }
        else {
            $contents = explode("\n", file_get_contents($file));
            unset($contents[0]);
            $f = fopen($file, 'w');
            if ( $f ) {
                $the_content = implode($contents, "\n");
                $the_content = preg_replace('/^\\s/', '', $the_content); // remove any leading whitespace.
                fwrite($f, $the_content, strlen($the_content));
                fclose($f);
                echo "Removed first line containing <code>" .  htmlentities($hack_str) ."</code>from $file...<br />";
            }
        }
    }
}

function get_count( $files ) {
    $count = count($files);
    foreach ( $files as $file ) {
        if ( is_array($file) ) {
            $count--; // remove this because it's a directory
            $count += get_count($file);
        }
        else {
            $count ++;
        }
    }
    return $count / 2;
}

?>

<?php
$files = get_infected_files($the_dir);
?>

<h2><?php echo get_count($files); ?> Infected Files in <?php echo $the_dir; ?></h2>

<?php
if ( count($files) > 0 ) :
    print_files($files);
?>
<?php endif; ?> ------------------..............------------------