From 714d45f4904ea45d8208853c45b17ed1f0d0342e Mon Sep 17 00:00:00 2001 From: anarcat Date: Fri, 24 Jul 2009 10:02:24 -0400 Subject: [PATCH] #203204 - port d7 patch to d6 --- includes/file.inc | 55 ++++++++++++++++++++++++++++++++++++-------- modules/color/color.module | 4 +- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/includes/file.inc b/includes/file.inc index 6464110..b1fda3a 100644 --- a/includes/file.inc +++ b/includes/file.inc @@ -102,7 +102,7 @@ function file_check_directory(&$directory, $mode = 0, $form_item = NULL) { if (!is_dir($directory)) { if (($mode & FILE_CREATE_DIRECTORY) && @mkdir($directory)) { drupal_set_message(t('The directory %directory has been created.', array('%directory' => $directory))); - @chmod($directory, 0775); // Necessary for non-webserver users. + drupal_chmod($directory); } else { if ($form_item) { @@ -114,7 +114,7 @@ function file_check_directory(&$directory, $mode = 0, $form_item = NULL) { // Check to see if the directory is writable. if (!is_writable($directory)) { - if (($mode & FILE_MODIFY_PERMISSIONS) && @chmod($directory, 0775)) { + if (($mode & FILE_MODIFY_PERMISSIONS) && drupal_chmod($directory)) { drupal_set_message(t('The permissions of directory %directory have been changed to make it writable.', array('%directory' => $directory))); } else { @@ -126,9 +126,8 @@ function file_check_directory(&$directory, $mode = 0, $form_item = NULL) { if ((file_directory_path() == $directory || file_directory_temp() == $directory) && !is_file("$directory/.htaccess")) { $htaccess_lines = "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006\nOptions None\nOptions +FollowSymLinks"; - if (($fp = fopen("$directory/.htaccess", 'w')) && fputs($fp, $htaccess_lines)) { - fclose($fp); - chmod($directory .'/.htaccess', 0664); + if (file_put_contents("$directory/.htaccess", $htaccess_lines)) { + drupal_chmod("$directory/.htaccess"); } else { $variables = array('%directory' => $directory, '!htaccess' => '
'. nl2br(check_plain($htaccess_lines))); @@ -260,11 +259,8 @@ function file_copy(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME) { return 0; } - // Give everyone read access so that FTP'd users or - // non-webserver users can see/read these files, - // and give group write permissions so group members - // can alter files uploaded by the webserver. - @chmod($dest, 0664); + // Set the permissions on the new file. + drupal_chmod($destination); } if (isset($file) && is_object($file)) { @@ -575,6 +571,9 @@ function file_save_upload($source, $validators = array(), $dest = FALSE, $replac return 0; } + // Set the permissions on the new file. + drupal_chmod($file->filepath); + // If we made it this far it's safe to record this file in the database. $file->uid = $user->uid; $file->status = FILE_STATUS_TEMPORARY; @@ -1354,5 +1353,41 @@ function file_get_mimetype($filename, $mapping = NULL) { } /** + * Set the permissions on a file or directory. + * + * This function will use the 'file_chmod_directory' and 'file_chmod_file' + * variables for the default modes for directories and uploaded/generated files. + * By default these will give everyone read access so that users accessing the + * files with a user account without the webserver group (e.g. via FTP) can read + * these files, and give group write permissions so webserver group members + * (e.g. a vhost account) can alter files uploaded and owned by the webserver. + * + * @param $path + * String containing the path to a file or directory. + * @param $mode + * Integer value for the permissions. Consult PHP chmod() documentation for + * more information. + * @return + * TRUE for success, FALSE in the event of an error. + */ +function drupal_chmod($path, $mode = NULL) { + if (!isset($mode)) { + if (is_dir($path)) { + $mode = variable_get('file_chmod_directory', 0775); + } + else { + $mode = variable_get('file_chmod_file', 0664); + } + } + + if (@chmod($path, $mode)) { + return TRUE; + } + + watchdog('file', 'The file permissions could not be set on %path.', array('%path' => $path), WATCHDOG_ERROR); + return FALSE; +} + +/** * @} End of "defgroup file". */ diff --git a/modules/color/color.module b/modules/color/color.module index db9eeac..7efd321 100644 --- a/modules/color/color.module +++ b/modules/color/color.module @@ -446,7 +446,7 @@ function _color_save_stylesheet($file, $style, &$paths) { $paths['files'][] = $file; // Set standard file permissions for webserver-generated files. - @chmod($file, 0664); + drupal_chmod($file); } /** @@ -504,7 +504,7 @@ function _color_render_images($theme, &$info, &$paths, $palette) { $paths['files'][] = $image; // Set standard file permissions for webserver-generated files - @chmod(realpath($image), 0664); + drupal_chmod($image); // Build before/after map of image paths. $paths['map'][$file] = $base; -- 1.5.6.5