function folderPermissions($folder)
{
    global $chmod, $f;
    $curdir = getcwd();
    chdir($folder);
    $files = safe_glob('*.*');
    chdir($curdir);
    foreach ($files as $file) {
        $path = $folder . '/' . $file;
        if (is_dir($path)) {
            if ($file != '.' && $file != '..') {
                @chmod($path, $chmod);
                clearstatcache();
                if ((fileperms($path) & 0777) == $chmod) {
                    if (!folderPermissions($path)) {
                        return false;
                    }
                } else {
                    return false;
                }
            }
        } else {
            @chmod($path, 0666 & $chmod);
            clearstatcache();
            if ((fileperms($path) & 0777) != (0666 & $chmod)) {
                return false;
            }
        }
    }
    return true;
}
function folderPermissions($folder)
{
    $files = array();
    if (($dir = opendir($folder)) !== false) {
        while (($file = readdir($dir)) !== false) {
            if ($file != '.' && $file != '..') {
                $files[] = $file;
            }
        }
        closedir($dir);
    }
    foreach ($files as $file) {
        $path = $folder . '/' . $file;
        if (is_dir($path)) {
            @chmod($path, FOLDER_MOD);
            clearstatcache();
            if (checkPermissions(fileperms($path) & 0777, FOLDER_MOD)) {
                if (!folderPermissions($path)) {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            @chmod($path, FILE_MOD);
            clearstatcache();
            if (!checkPermissions(fileperms($path) & 0777, FILE_MOD)) {
                return false;
            }
        }
    }
    return true;
}