########################
##### Basic Config #####
########################
//only files containing links and images
$filetypes = array(
"txt",
"php",
"asp",
"htm",
"html"
);
//default 30 seconds is probably not long enough for a massive file overhaul like this
set_time_limit(120); //2 minutes
#########################
##### /Basic Config #####
#########################
?>
Win2Nix Case Converter
//we dont need no stinking errors
error_reporting(0);
//timing script
$timeparts = explode(' ',microtime());
$starttime = $timeparts[1].substr($timeparts[0],1);
//load form for first time usage
if (!isset($HTTP_POST_VARS['go'])) {
print "This script will seek out and convert the case of all HREF and SRC tags to lowercase. Furthermore it will also convert the directory and file names to match the converted HTML tags. Once you have this file in the document root of the website to be converted... Click the button below.
";
die(" | |
");
}
###########################################
##### Step 1 (edit contents of files) #####
###########################################
//get files and directories into an array
$list = list_files(".");
$altered = 0;
//edit file contents
foreach ($list['files'] as $v) {
$editfile = "";
$done = "";
//exclude 'this' file from the script
if (strtolower(basename($v)) != strtolower(basename($HTTP_SERVER_VARS['PHP_SELF']))) {
//get file extension for this file
$fext = substr($v, strrpos($v, '.') + 1);
//check if valid for editing
foreach ($filetypes as $f) {
if (strtolower($fext) == strtolower($f)) {
$editfile = $v;
//open and read the file
if ($editfile != "") {
if ($fp = fopen($editfile, "r")) {
//get contents of file
$line = fread($fp, filesize($editfile));
//search for html tags
$search = "/(href|src)(\s*\=\s*[(\")])((?!.*:\/\/)[^(\")]*)([^\>]*\>)/ie";
//replace found tags with
$replace = "\"$1\".\"$2\".strtolower(\"$3\").\"$4\"";
//set new variable
$modified = preg_replace($search, $replace, $line);
//add modified line to dump var 'done'
$done .= $modified;
//wipe file contents and replace with 'done'
if ($fp = fopen($editfile, "w")) {
if (fputs($fp, $done) === FALSE) {
print "Had a problem writing contents to ".$editfile;
} else {
$altered++;
}
} else {
$notwritable[] = $editfile;
}
} else {
$notreadable[] = $editfile;
}
}
}
}
}
}
###########################################
################# /Step 1 #################
###########################################
###########################################
#### Step 2 (rename files and folders) ####
###########################################
$renfiles = 0;
$rendirs = 0;
//rename files before changing directory structure
foreach ($list['files'] as $file) {
rename($file, strtolower($file));
$renfiles++;
}
//rename directories
foreach ($list['dirs'] as $dir) {
rename($dir, strtolower($dir));
$rendirs++;
}
###########################################
################# /Step 2 #################
###########################################
//timing script
$timeparts = explode(' ',microtime());
$endtime = $timeparts[1].substr($timeparts[0],1);
$time = bcsub($endtime,$starttime,6);
print "Altered the contents of $altered files
";
print "Renamed $renfiles files and $rendirs directories
";
print "Finished in $time seconds.
";
if ($notreadable != "") {
$permissions = 1;
print "";
foreach ($notreadable as $v) {
print "Could not read: $v ";
}
print " |
";
print "
";
}
if ($notwritable != "") {
print "";
$permissions = 1;
foreach ($notwritable as $v) {
print "Could not write: $v ";
}
print " |
";
print "
";
}
if (isset($permissions)) {
print "| ";
print "Please check your file permissions and run the script again.";
print " |
";
}
function list_files($dir) {
$file_list = '';
$stack[] = $dir;
while ($stack) {
$current_dir = array_pop($stack);
if ($dh = opendir($current_dir)) {
while (($file = readdir($dh)) !== false) {
if ($file !== '.' AND $file !== '..') {
$current_file = "{$current_dir}/{$file}";
if (is_file($current_file)) {
$file_list[] = "{$current_dir}/{$file}";
} elseif (is_dir($current_file)) {
$dir_list[] = $current_file;
$stack[] = $current_file;
}
}
}
}
}
$list = array("dirs" => $dir_list, "files" => $file_list);
return $list;
}
?>