Work:FixPerm Script HTTP
From Zoelife4U Wiki
Written by Joe Crown
New Interface
Be sure to notice the new code interface and click on View Plain now, if you try to paste the below code, it will show line numbers !! Click Help for more information
Note, both versions merged into one. We'll leave the older CLI version up for a while to test this one is as fast on the command line as that one ;)
HTTP Version
Place in $HOME/public_html/ and run from the web browser like http://domain.com/fixperm.php
<?php
// This code is licensed under the GPL Version 2.0 only
// Make sure you name the file with the .php extension
// or one that is run as php.
// This script now allows its file name to be anything,
// so long as it is run through the php parser.
// This is Version 1.8.7
// Put this in the folder that you want it to fix permissions
// on & all of the sub-folders & files in those folders
// Start the timer
function timeabused(){
list($utime, $time) = explode(" ", microtime());
return ((float)$utime + (float)$time);
}
$scriptStart = timeabused();
// Set some default settings to override any lower settings
// to prevent crashes on large accounts
ini_set('register_globals', 'Off');
ini_set('max_execution_time', '600');
ini_set('max_input_time', '600');
ini_set('memory_limit', '260M');
ini_set('upload_max_filesize', '256M');
ini_set('post_max_size', '257M');
define (highFilesHTML, 100000);
define (highFoldersHTML, 30000);
define (highFilesCLI, 300000);
define (highFoldersCLI, 30000);
// This will stop the permissions from being changed
// with a large number of files or folders
function killfortomany($numfolders, $numfiles){
$totalnumfolders = shell_exec ("find ./ -type d | wc -l");
$totalnumfiles = shell_exec ("find ./ -type f | wc -l");
if (($totalnumfolders >= $numfolders) or ($totalnumfiles >= $numfiles)){
return "TMF";
}else{
return "LNF";
}
}
// Make sure the folder & files are current & that the
// stats are correct before checking if it is safe to run
clearstatcache();
// Let's figure out if we're on the command line or web server..
function clihttp() {
if($_ENV['SHELL']) {
$d = "CLI";
}elseif($_POST['d'] == "yes") {
$d = "HTTP";
}
return $d;
}
// This function finds all files & folders in the starting directory
function filelist ($startdir="./"){
$ignoredDirectory[] = ".";
$ignoredDirectory[] = "..";
global $directorylist; // I do not know of a way to have it work without this line I know it sucks :P
if (is_dir($startdir)){
if ($dh = opendir($startdir)){
while (($file = readdir($dh)) !== false){
if (!(array_search($file,$ignoredDirectory) > -1)){
if (filetype($startdir . $file) == "dir"){
$directorylist[$startdir . $file]['name'] = $file;
$directorylist[$startdir . $file]['path'] = $startdir;
filelist($startdir . $file . "/");
} else {
$directorylist[$startdir . $file]['name'] = $file;
$directorylist[$startdir . $file]['path'] = $startdir;
}
}
}
closedir($dh);
}
}
return($directorylist);
}
// This function is to fix the permissions
// Its output is in HTML
function fixpermshtml(){
$files = filelist();
$j = 0;
foreach ($files as $list){
$filesandfolders[] = $list['path'] . $list['name'];
}
$j = count($filesandfolders);
for ($i = 0; $i < $j; $i++){
if (is_dir($filesandfolders[$i])){
chmod($filesandfolders[$i], 0755);
print("<p><strong>$filesandfolders[$i]:</strong> 0755</p>\n");
} else {
if (file_exists($filesandfolders[$i])){
if ((substr($filesandfolders[$i], -9) == '.ftpquota')){
chmod($filesandfolders[$i], 0600);
print("<p><strong>$filesandfolders[$i]:</strong> 0600</p>\n");
} elseif ((substr($filesandfolders[$i], -3) == '.pl') or (substr($filesandfolders[$i], -3) == '.pm') or (substr($filesandfolders[$i], -4) == '.cgi') or (substr($filesandfolders[$i], -3) == '.py')){
chmod($filesandfolders[$i], 0755);
print("<p><strong>$filesandfolders[$i]:</strong> 0755</p>\n");
$file_result = `file $filesandfolders[$i]`;
if (!preg_match("/ELF/", $file_result) || !preg_match("/SYSV/", $file_result)){
system("dos2unix $filesandfolders[$i]");
}
} elseif ((substr($filesandfolders[$i], -13) == 'wp-config.php') or (substr($filesandfolders[$i], -13) == 'configure.php')){
chmod($filesandfolders[$i], 0444);
print("<p><strong>$filesandfolders[$i]:</strong> 0444</p>\n");
} else{
chmod($filesandfolders[$i], 0644);
print("<p><strong>$filesandfolders[$i]:</strong> 0644</p>\n");
}
}
}
}
}
// This function is to fix the permissions
// Its output is for the CLI
function fixpermscmd(){
echo "<p>Fixing permissions on Files & Folders.";
echo "\nSetting folders to 755 & files to 644. ";
system ('find ./ -type d -exec chmod 755 {} \;');
system ('find ./ -type f -exec chmod 644 {} \;');
echo "\nDone.";
echo "\nSetting .cgi & .pl files to 755. ";
system ('find ./ -name \*.cgi -exec chmod 755 {} \;');
system ('find ./ -name \*.pl -exec chmod 755 {} \;');
system ('find ./ -name \*.pm -exec chmod 755 {} \;');
system ('find ./ -name \*.py -exec chmod 755 {} \;');
echo "\nDone.";
echo "\nAlso setting .ftpquota files to 600. ";
system ('find ./ -name .ftpquota -exec chmod 600 {} \;');
echo "\nDone.";
}
// This gives you an idea of the number of
// files & folders before running the script
// in HTTP/HTML mode
function numfilesandfolders(){
print "Number of folders ";
system ("find ./ -type d | wc -l");
print "<br/>Number of files ";
system ("find ./ -type f | wc -l");
print "<br/>Please note that a large number of Folders & files will slow this down.";
}
// This tells you how much memory the script has used
function memoryabused(){
$d = clihttp();
if (is_callable(memory_get_usage)){
if ($d == "CLI"){
print "Memory usage in bytes " . memory_get_usage() . "\n";
}else{
print "<p>Memory usage in bytes " . memory_get_usage() . "</p>";
}
} else {
if ($d == "CLI"){
print "I can't get the Memory usage.\n";
}else{
print "<p>I can't get the Memory usage.</p>";
}
}
}
// This spits out the HTML form
function showform(){
print "<br/>\n";
print "<form method='post' action=''>\n";
print "Set all folder permissions to 755, files to 644, & .cgi/.pl files to 755?<br/>\n";
print "<input type='submit' value='yes' name='d'></p>\n";
print "</form>\n";
}
// Now we finally start doing something
$d = clihttp();
if ($d) {
if($d=="CLI"){
print"Fixing permissions on Files & Folders.\n";
print"Setting folders to 755 & files to 644.\n";
print"Setting .cgi, .pl, .pm, & .py files to 755.\n";
print"Also setting .ftpquota files to 600.\n\n";
$f = killfortomany(highFoldersCLI, highFilesCLI);
if($f == "LNF"){
fixpermscmd();
}else{
print "The account has to many files to dare running it in this folder.";
}
print"\n";
memoryabused();
$scriptEnd = timeabused();
echo "Script executed in ".bcsub($scriptEnd, $scriptStart, 4)." seconds.";
}elseif($d="HTTP"){
print"<html><head><title>File & Folder Permissions fixer</title>\n";
print"<!-- This code is licensed under the GPL Version 2.0 only --></head><body><p>\n";
print"Fixing permissions on Files & Folders.\n";
print"<br/>Setting folders to 755 & files to 644.\n";
print"<br/>Setting .cgi, .pl, .pm, & .py files to 755.\n";
print"<br/>Also setting .ftpquota files to 600.\n";
$f = killfortomany(highFoldersHTML, highFilesHTML);
if($f == "LNF"){
fixpermshtml();
}else{
print "<br/>The account has to many files to dare running it in this folder.\n";
}
memoryabused();
print"<br/>";
$scriptEnd = timeabused();
echo "Script executed in ".bcsub($scriptEnd, $scriptStart, 4)." seconds.";
print"</p></body></html>";
}
} else {
print"<html><head><title>File & Folder Permissions fixer</title>\n";
print"<!-- This code is licensed under the GPL Version 2.0 only --></head><body><p>\n";
numfilesandfolders();
showform();
memoryabused();
print"</p></body></html>";
}
?>