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
| <?php
define('DEFAULT_PATH', "C://wamp64/www/");
$encodingList = ['UTF-8', 'UTF-16LE', 'UTF-16BE', 'UTF-32LE', 'UTF-32BE', 'WINDOWS-1252'];
$optionList = '';
$errors = [];
foreach ($encodingList as $value)
{
if ( $value === 'WINDOWS-1252' ) // Cette valeur provoque le remplacement des caractères diacritiques par des points d'interrogation.
continue;
$optionList .= "<option value='$value'>$value</option>";
}
if ( isset($_POST['send']) and !empty($_POST['types']) )
{
if ( empty($_POST['targetCode']) or empty($_POST['path']) or empty($_POST['types']) )
{
$errors[] = "Vous n'avez pas saisi toutes les valeurs.";
goto fin;
}
$targetCode = trim(strip_tags($_POST['targetCode']));
$directory = trim(strip_tags($_POST['path']));
$directory = trim($directory, '/\\') . '/';
$extensions = $_POST['types'];
$backupDir = $directory.'/backup/';
$backupExt = 'bak';
$fileList = glob($directory . '*.{' . implode(',', $extensions) . '}', GLOB_BRACE);
if ( !file_exists($backupDir) )
mkdir($backupDir);
if ( $directory === DEFAULT_PATH )
{
$errors[] = "Le répertoire racine est interdit.";
goto fin;
}
foreach ($fileList as $file)
{
$content = file_get_contents($file);
$encoding = mb_detect_encoding($content, $encodingList, true);
$test = mb_detect_encoding($content);
echo 'code source: ', $encoding, '<br/>';
echo 'code cible: ', $targetCode, '<br/>';
echo 'code test: ', $test, '<br/>';
//if ( $encoding === 'UTF-8' )
if ( $encoding === $targetCode )
continue;
$filename = substr(strrchr("/$file", '/'), 1); // voir basename
//echo __line__, ': ', $filename, '<br/>';
//echo __line__, ': ', basename($file), '<br/>';
if ( copy($file, $backupDir . $filename . '.' . $backupExt) === false )
{
echo "Impossible de copier ", $file, PHP_EOL, "<br/>Procédure abandonnée.";
continue;
}
//file_put_contents($file, mb_convert_encoding($content, 'UTF-8', $encoding) );
file_put_contents( $file, mb_convert_encoding($content, $targetCode, $encoding) );
}
fin:
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Convertisseur d'encodage</title>
<link rel="stylesheet" media="screen" type="text/css" href="css/esthet.css" />
</head>
<body>
<h1>Convertisseur d'encodage</h1>
<?php if ( !empty($errors) )
echo "<p style='color:red;'>".implode('<br/>', $errors)."</p>";
?>
<form method="post" action="" enctype="multipart/form-data">
<label for="targetCode" class="xxL">Code cible</label><select name="targetCode" id="targetCode">
<?= $optionList; ?>
</select><br/>
<label for="path" class="xxL">Répertoire à convertir</label><input type="text" name="path" id="path" value="<?= DEFAULT_PATH; ?>" /><br/>
<label class="xxL">Types de fichiers à convertir :</label><br/>
<input style="margin-left:40px;" type="checkbox" name="types[1]" id="type1" value="php" checked /><label for="type1">php</label><br/>
<input style="margin-left:40px;" type="checkbox" name="types[2]" id="type2" value="asp" checked /><label for="type2">asp</label><br/>
<input style="margin-left:40px;" type="checkbox" name="types[3]" id="type3" value="js" checked /><label for="type3">js</label><br/>
<input style="margin-left:40px;" type="checkbox" name="types[4]" id="type4" value="html" checked /><label for="type4">html</label><br/>
<input style="margin-left:40px;" type="checkbox" name="types[5]" id="type5" value="css" checked /><label for="type5">css</label><br/>
<input style="margin-left:40px;" type="checkbox" name="types[6]" id="type6" value="csv" checked /><label for="type6">csv</label><br/>
<p><input type="submit" name="send" value="Exécuter" /></p>
</form>
</body>
</html> |