note 74107 modified in function.chmod by bobby

From: Date: Mon, 26 Mar 2007 14:45:29 +0000
Subject: note 74107 modified in function.chmod by bobby
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-124815@lists.php.net to get a copy of this message
<?php error_reporting(E_ERROR | E_PARSE); /* Makes is so Directories are not browseable to the public, removing only the Public = Read permission, while leaving the other chmod permissions for the file in tact. If you have exectue already on, and read off, public viewers will only be able to view files through links, but not browse around to see what's inside of directories and see what you've got laying around. */ //------------------------------------------------------- // Get file mode // Get file permissions supported by chmod function getmod($filename) { $val = 0; $perms = fileperms($filename); // Owner; User $val += (($perms & 0x0100) ? 0x0100 : 0x0000); //Read $val += (($perms & 0x0080) ? 0x0080 : 0x0000); //Write $val += (($perms & 0x0040) ? 0x0040 : 0x0000); //Execute // Group $val += (($perms & 0x0020) ? 0x0020 : 0x0000); //Read $val += (($perms & 0x0010) ? 0x0010 : 0x0000); //Write $val += (($perms & 0x0008) ? 0x0008 : 0x0000); //Execute // Global; World $val += (($perms & 0x0004) ? 0x0004 : 0x0000); //Read $val += (($perms & 0x0002) ? 0x0002 : 0x0000); //Write $val += (($perms & 0x0001) ? 0x0001 : 0x0000); //Execute // Misc $val += (($perms & 0x40000) ? 0x40000 : 0x0000); //temporary file (01000000) $val += (($perms & 0x80000) ? 0x80000 : 0x0000); //compressed file (02000000) $val += (($perms & 0x100000) ? 0x100000 : 0x0000); //sparse file (04000000) $val += (($perms & 0x0800) ? 0x0800 : 0x0000); //Hidden file (setuid bit) (04000) $val += (($perms & 0x0400) ? 0x0400 : 0x0000); //System file (setgid bit) (02000) $val += (($perms & 0x0200) ? 0x0200 : 0x0000); //Archive bit (sticky bit) (01000) return $val; } //------------------------------------------------------- // Find out if file has mode function hasmod($perms, $permission) { # User Read = 0400 (256), Write = 0200 (128), Execute = 0100 (64) # Group Read = 0040 (32), Write = 0020 (16), Execute = 0010 (8) # Public Read = 0004 (4), Write = 0002 (2), Execute = 0001 (1) return (($perms & $permission) == $permission); } //------------------------------------------------------- // Take the read option off of all the subdirectories of the included path function pathlock($dir, $listall = false, $testrun = true) { echo "START @ " . date("F j, Y, h:i:s A") . "<br><br>"; echo ($testrun ? '**Test Run Activated (no changes will be made).**<br><br>' : '**Live Run Activated.**<br><br>'); echo $dir . " is our directory.<br><br>\n"; echo "[...IN PROGRESS...]<br><br>"; $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_dir($current_file)) { // BEG ADD PATH $mode = getmod($current_file); //Get the mode $HasPubRead = hasmod($mode,4); if ($HasPubRead || $listall) { // Can the public read this dir? //====================================== $ch = true; $take = 0; // Change the mode: if ($HasPubRead) { $take = 4; // Value for Public Read. 4 is the same in octal and decimal. if (!$testrun) { $ch = chmod($current_file, $mode-$take); } } echo $current_file . ",current=" . decoct($mode) . (($mode!==$mode-$take) ? ",new=" . decoct($mode-$take) : '') . ($ch ? '' : ',FAILED') . "<br>\n"; } // end if hasmod // END ADD PATH $stack[] = $current_file; } // if if_dir } //if ($file !== '.' AND $file !== '..') } //while (($file = readdir($dh)) !== false) } //if ($dh = opendir($current_dir)) } // while ($stack) echo "<br>COMPLETE @ " . date("F j, Y, h:i:s A") . "<br>\n"; return; //return $path_list; } // end function //------------------------------------------------------- //listall Show all folders, even one's we're not going to process? //testrun Do a test run without making any changes pathlock($_SERVER["DOCUMENT_ROOT"],false,true); // listall?=false, testrun?=true ?> --was-- <?php error_reporting(E_ERROR | E_PARSE); /* Makes is so Directories are not browseable to the public, removing only the Public = Read permission, while leaving the other chmod permissions for the file in tact. If you have exectue already on, and read off, public viewers will only be able to view files through links, but not browse around to see what's inside of directories and see what you've got laying around. */ //------------------------------------------------------- // Get file mode function getmod($filename) { $val = 0; $perms = fileperms($filename); // User $val += (($perms & 0x0100) ? 0x0100 : 0x0000); //Read $val += (($perms & 0x0080) ? 0x0080 : 0x0000); //Write $val += (($perms & 0x0040) ? 0x0040 : 0x0000); //Execute // Group $val += (($perms & 0x0020) ? 0x0020 : 0x0000); //Read $val += (($perms & 0x0010) ? 0x0010 : 0x0000); //Write $val += (($perms & 0x0008) ? 0x0008 : 0x0000); //Execute // World $val += (($perms & 0x0004) ? 0x0004 : 0x0000); //Read $val += (($perms & 0x0002) ? 0x0002 : 0x0000); //Write $val += (($perms & 0x0001) ? 0x0001 : 0x0000); //Execute return $val; } //------------------------------------------------------- // Find out if file has mode function hasmod($perms, $permission) { # Public Read = 0004 (4) , Write = 0002 (2), Execute = 0001 (1) # Group Read = 0040 (32) , Write = 0020 (16), Execute = 0010 (8) # World Read 0400 (256), Write 0200 (128), Execute 0100 (64) return (($perms & $permission) == $permission); } //------------------------------------------------------- // Take the read option off of all the subdirectories of the included path function pathlock($dir, $listall = false, $testrun = true) { echo "START @ " . date("F j, Y, h:i:s A") . "<br><br>"; echo ($testrun ? '**Test Run Activated (no changes will be made).**<br><br>' : '**Live Run Activated.**<br><br>'); echo $dir . " is our directory.<br><br>\n"; echo "[...IN PROGRESS...]<br><br>"; $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_dir($current_file)) { // BEG ADD PATH $mode = getmod($current_file); //Get the mode $HasPubRead = hasmod($mode,4); if ($HasPubRead || $listall) { // Can the public read this dir? //====================================== $ch = true; $take = 0; // Change the mode: if ($HasPubRead) { $take = 4; // Value for Public Read. 4 is the same in octal and decimal. if (!$testrun) { $ch = chmod($current_file, $mode-$take); } } echo $current_file . ",current=" . decoct($mode) . (($mode!==$mode-$take) ? ",new=" . decoct($mode-$take) : '') . ($ch ? '' : ',FAILED') . "<br>\n"; } // end if hasmod // END ADD PATH $stack[] = $current_file; } // if if_dir } //if ($file !== '.' AND $file !== '..') } //while (($file = readdir($dh)) !== false) } //if ($dh = opendir($current_dir)) } // while ($stack) echo "<br>COMPLETE @ " . date("F j, Y, h:i:s A") . "<br>\n"; return; //return $path_list; } // end function //------------------------------------------------------- //listall Show all folders, even one's we're not going to process? //testrun Do a test run without making any changes pathlock($_SERVER["DOCUMENT_ROOT"],false,true); // listall?=false, testrun?=true ?> http://php.net/manual/en/function.chmod.php

« previous php.notes (#124815) next »