1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Formulaire de recherche</title>
</head>
<body bgcolor="#DDDDDD">
<table bgcolor="white" style="border-collapse:collapse;" width="375" align="center">
<tr>
<td width="350" style="border-width:1; border-color:black; border-style:dashed;" height="179">
<br>
<p align="center"><font face="Verdana"><span style="font-size:10pt;"> <u>Formulaire de recherche :</u><br> </span></font></p>
<form action="index.php" name="search" method="POST">
<table cellspacing="0" style="border-collapse:collapse;">
<tr>
<td width="165" style="border-width:1; border-top-color:rgb(153,153,153); border-right-color:black; border-bottom-color:black; border-left-color:rgb(153,153,153); border-style:none;">
<p align="right"><font face="Verdana"><span style="font-size:8pt;">Chemin d'accès :</span></font></p>
</td>
<td width="165" style="border-width:1; border-top-color:rgb(153,153,153); border-right-color:rgb(153,153,153); border-bottom-color:black; border-left-color:black; border-style:none;"> <p><font face="Verdana"><span style="font-size:8pt;"><input type="text" name="path" value="C:/wamp/www/script_replace" size="35"></span></font></p>
</td>
</tr>
<tr>
<td width="165" style="border-width:1; border-top-color:black; border-right-color:black; border-bottom-color:black; border-left-color:rgb(153,153,153); border-style:none;">
<p align="right"><font face="Verdana"><span style="font-size:8pt;">Trouver :</span></font></p>
</td>
<td width="165" style="border-width:1; border-top-color:black; border-right-color:rgb(153,153,153); border-bottom-color:black; border-left-color:black; border-style:none;"> <p><font face="Verdana"><span style="font-size:8pt;"><input type="text" name="find" size="25"></span></font></p>
</td>
</tr>
<tr>
<td width="165" style="border-width:1; border-top-color:black; border-right-color:black; border-bottom-color:rgb(153,153,153); border-left-color:rgb(153,153,153); border-style:none;">
<p align="right"><font face="Verdana"><span style="font-size:8pt;">Remplacer :</span></font></p>
</td>
<td width="165" style="border-width:1; border-top-color:black; border-right-color:rgb(153,153,153); border-bottom-color:rgb(153,153,153); border-left-color:black; border-style:none;"> <p><font face="Verdana"><span style="font-size:8pt;"><input type="text" name="replace"></span></font></p>
</td>
</tr>
</table>
<p align="center"> <a href="javascript:window.document.search.submit()">Envoyer</a> </p>
</form>
<?php
if(isset($_POST['find']) & isset($_POST['replace']))
{
if(!isset($_POST['find']) || (isset($_POST['find']) && $_POST['find']=="")) //si le champs de texte comprenant la chaine à rechercher n'est pas rempli faire :
{
echo'<script language="javascript">';
echo'alert("Chaîne à rechercher inconnue!");';
echo'javascript:history.back(1);';
echo'</script>';
}
// Répertoire racine du scan (au format UNIX même sous Windows)
$root_dir = $_POST['path'];
// Extensions des fichiers concernés par le remplacement
$ext = array('php', 'php3', 'inc');
// Chaine à chercher récupérée du formulaire
$find = $_POST['find'];
// Chaine à remplacer récupérée aussi du formulaire
$replace = $_POST['replace'];
############################################################################################
//Fonction qui scanne un répertoire et ses sous-répertoires et modifie les fichiers concernés
//si la chaîne est trouvée :
function scan_dir($path, $find, $replace) {
global $change, $ext;
if(!$test=opendir($path)) // si le nouveau chemin d'accès saisi par l'utilisateur est mal spécifié : erreur
{
echo'<script language="javascript">';
echo'alert("Chemin d\'accès inconnu !");';
echo'javascript:history.back(1);';
echo'</script>';
}
if(false !== $dp = opendir($path)) //retourne un pointeur sur un dossier pour être utilisé avec les fonctions closedir, readdir et rewinddir.
{
clearstatcache(); //Efface le cache de stat
while($file = readdir($dp)) //retourne le nom du fichier suivant dans le dossier identifié par dir_handle
{
$f = $path . '/' . $file; //chemin d'accès + nom du fichier courant.
if($file != '.' and $file != '..' and $f != $_SERVER['SCRIPT_FILENAME']) // SCRIPT_FILENAME : Le chemin absolu vers le fichier contenant le script en cours d'exécution
{
if(is_dir($f)) //Indique si le fichier $f est un dossier
{
scan_dir($f, $find, $replace); // rappel récursif si on a un nouveau dossier
} else {
$info_file = pathinfo($f); //retourne un tableau associatif, contenant les informations sur le chemin path . Les éléments suivants sont retournés : dirname , basename et extension
foreach($ext as $extension) {
if(isset($info_file['extension']) and 0 === strcmp($extension, $info_file['extension'])) //strcmp ret 0 si c'est égal
{
$old_content = file_get_contents($f); //Lit tout un fichier dans une chaîne
$new_content = str_replace($find, $replace, $old_content); // Remplace toutes les occurrences dans une chaîne
if(strcmp($old_content, $new_content)) //si le fichier texte a changé
{
$change = true;
if(false !== ($fp = fopen($f, 'w'))) // remplacement des données avec le new_content.
{
fwrite($fp, $new_content);
fclose($fp);
}
echo '<li>' . $f . '</li>';
}
break;
}
}
}
}
}
closedir($dp);
}
return;
}
//Présentation des résultats
echo '<br><br><p align="center"><font face="Verdana"><span style="font-size:10pt;"><u>Résultats du traitement : </u></span></p>';
echo "<span style=\"font-size:8pt;\">  Chaine recherchée : ".$_POST['find']."<br>";
echo "  Remplacer par : ".$_POST['replace']."<br><br>";
echo "<b>  Liste des fichiers modifiés : </b><br> </span></font>";
echo '<h5><ul>';
//On scan le répertoire et ses sous-répertoires
$change = false;
scan_dir($root_dir, $find, $replace);
if(!$change) {
echo '<li>Aucun fichier mis à jour !</li>';
}
echo '</ul></h5>';
}
?>
<p> </p></td></tr></table><p> </p>
</body>
</html> |
Partager