From 5c5aaddc78e44c648472eb5b7980a2ab769a5ef4 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Sun, 20 Aug 2017 17:26:27 +0200 Subject: [PATCH] Applied adapted Drupal core patch #572516 for sites with mixed public and private files --- config/default/default.settings.php | 5 +++++ includes/common.inc | 20 ++++++++++---------- includes/file.inc | 25 ++++++++++++++----------- includes/locale.inc | 4 ++-- modules/locale/locale.module | 2 +- modules/system/system.admin.inc | 6 +++--- 6 files changed, 35 insertions(+), 27 deletions(-) diff --git a/config/default/default.settings.php b/config/default/default.settings.php index 1fa42b1..1c57fed 100644 --- a/config/default/default.settings.php +++ b/config/default/default.settings.php @@ -52,6 +52,11 @@ $conf = array( // Site main values: 'site_name' => 'My SuiteDesk site', +// Files directories (private, public and temp): + 'file_directory_path' => '../files/default', + 'file_dirpublic_path' => 'files/default', + 'file_directory_temp' => '/tmp', + // Social links: # 'social_website' => 'http://www.example.com', # 'social_rss' => 'http://www.example.com/feed', diff --git a/includes/common.inc b/includes/common.inc index 25db3ba..bbe8364 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -1926,8 +1926,8 @@ function drupal_get_css($css = NULL) { $no_theme_preprocess = ''; $preprocess_css = (variable_get('preprocess_css', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update')); - $directory = file_directory_path(); - $is_writable = is_dir($directory) && is_writable($directory) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC); + $directory = file_directory_path(TRUE); + $is_writable = is_dir($directory) && is_writable($directory); // A dummy query-string is added to filenames, to gain control over // browser-caching. The string changes on every update or full cache @@ -2002,7 +2002,7 @@ function drupal_build_css_cache($types, $filename) { $data = ''; // Create the css/ within the files folder. - $csspath = file_create_path('css'); + $csspath = file_create_path('css', TRUE); file_check_directory($csspath, FILE_CREATE_DIRECTORY); if (!file_exists($csspath .'/'. $filename)) { @@ -2028,7 +2028,7 @@ function drupal_build_css_cache($types, $filename) { $data = implode('', $matches[0]) . $data; // Create the CSS file. - file_save_data($data, $csspath .'/'. $filename, FILE_EXISTS_REPLACE); + file_save_data($data, $csspath .'/'. $filename, FILE_EXISTS_REPLACE, TRUE); } return $csspath .'/'. $filename; } @@ -2182,7 +2182,7 @@ function _drupal_load_stylesheet($matches) { * Delete all cached CSS files. */ function drupal_clear_css_cache() { - file_scan_directory(file_create_path('css'), '.*', array('.', '..', 'CVS'), 'file_delete', TRUE); + file_scan_directory(file_create_path('css', TRUE), '.*', array('.', '..', 'CVS'), 'file_delete', TRUE); } /** @@ -2333,8 +2333,8 @@ function drupal_get_js($scope = 'header', $javascript = NULL) { $no_preprocess = array('core' => '', 'module' => '', 'theme' => ''); $files = array(); $preprocess_js = (variable_get('preprocess_js', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update')); - $directory = file_directory_path(); - $is_writable = is_dir($directory) && is_writable($directory) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC); + $directory = file_directory_path(TRUE); + $is_writable = is_dir($directory) && is_writable($directory); // A dummy query-string is added to filenames, to gain control over // browser-caching. The string changes on every update or full cache @@ -2535,7 +2535,7 @@ function drupal_build_js_cache($files, $filename) { $contents = ''; // Create the js/ within the files folder. - $jspath = file_create_path('js'); + $jspath = file_create_path('js', TRUE); file_check_directory($jspath, FILE_CREATE_DIRECTORY); if (!file_exists($jspath .'/'. $filename)) { @@ -2548,7 +2548,7 @@ function drupal_build_js_cache($files, $filename) { } // Create the JS file. - file_save_data($contents, $jspath .'/'. $filename, FILE_EXISTS_REPLACE); + file_save_data($contents, $jspath .'/'. $filename, FILE_EXISTS_REPLACE, TRUE); } return $jspath .'/'. $filename; @@ -2558,7 +2558,7 @@ function drupal_build_js_cache($files, $filename) { * Delete all cached JS files. */ function drupal_clear_js_cache() { - file_scan_directory(file_create_path('js'), '.*', array('.', '..', 'CVS'), 'file_delete', TRUE); + file_scan_directory(file_create_path('js', TRUE), '.*', array('.', '..', 'CVS'), 'file_delete', TRUE); variable_set('javascript_parsed', array()); } diff --git a/includes/file.inc b/includes/file.inc index b0f90e8..bfc1b41 100644 --- a/includes/file.inc +++ b/includes/file.inc @@ -60,8 +60,8 @@ function file_create_url($path) { * appended if necessary, or FALSE if the path is invalid (i.e. outside the * configured 'files' or temp directories). */ -function file_create_path($dest = 0) { - $file_path = file_directory_path(); +function file_create_path($dest = 0, $public_files = FALSE) { + $file_path = file_directory_path($public_files); if (!$dest) { return $file_path; } @@ -292,8 +292,8 @@ function file_check_location($source, $directory = '') { * @return * TRUE for success, FALSE for failure. */ -function file_copy(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME) { - $dest = file_create_path($dest); +function file_copy(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME, $public_files = FALSE) { + $dest = file_create_path($dest, $public_files); $directory = $dest; $basename = file_check_path($directory); @@ -413,10 +413,10 @@ function file_destination($destination, $replace) { * @return * TRUE for success, FALSE for failure. */ -function file_move(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME) { +function file_move(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME, $public_files = FALSE) { $path_original = is_object($source) ? $source->filepath : $source; - if (file_copy($source, $dest, $replace)) { + if (file_copy($source, $dest, $replace, $public_files)) { $path_current = is_object($source) ? $source->filepath : $source; if ($path_original == $path_current || file_delete($path_original)) { @@ -874,7 +874,7 @@ function file_validate_image_resolution(&$file, $maximum_dimensions = 0, $minimu * * @return A string containing the resulting filename or 0 on error */ -function file_save_data($data, $dest, $replace = FILE_EXISTS_RENAME) { +function file_save_data($data, $dest, $replace = FILE_EXISTS_RENAME, $public_files = FALSE) { $temp = file_directory_temp(); // On Windows, tempnam() requires an absolute path, so we use realpath(). $file = tempnam(realpath($temp), 'file'); @@ -885,7 +885,7 @@ function file_save_data($data, $dest, $replace = FILE_EXISTS_RENAME) { fwrite($fp, $data); fclose($fp); - if (!file_move($file, $dest, $replace)) { + if (!file_move($file, $dest, $replace, $public_files)) { return 0; } @@ -895,7 +895,7 @@ function file_save_data($data, $dest, $replace = FILE_EXISTS_RENAME) { /** * Set the status of a file. * - * @param $file + * @param $file * A Drupal file object. * @param $status * A status value to set the file to. One of: @@ -924,7 +924,7 @@ function file_transfer($source, $headers) { if (ob_get_level()) { ob_end_clean(); } - + // IE cannot download private files because it cannot store files downloaded // over HTTPS in the browser cache. The problem can be solved by sending // custom headers to IE. See http://support.microsoft.com/kb/323308/en-us @@ -1150,7 +1150,10 @@ function file_directory_temp() { * * @return A string containing the path to Drupal's 'files' directory. */ -function file_directory_path() { +function file_directory_path($public_files = FALSE) { + if ($public_files) { + return variable_get('file_dirpublic_path', 'files'); + } return variable_get('file_directory_path', conf_path() .'/files'); } diff --git a/includes/locale.inc b/includes/locale.inc index 57bb693..eab303f 100644 --- a/includes/locale.inc +++ b/includes/locale.inc @@ -2181,7 +2181,7 @@ function _locale_rebuild_js($langcode = NULL) { // Construct the filepath where JS translation files are stored. // There is (on purpose) no front end to edit that variable. - $dir = file_create_path(variable_get('locale_js_directory', 'languages')); + $dir = file_create_path(variable_get('locale_js_directory', 'languages'), TRUE); // Delete old file, if we have no translations anymore, or a different file to be saved. $changed_hash = $language->javascript != $data_hash; @@ -2199,7 +2199,7 @@ function _locale_rebuild_js($langcode = NULL) { file_check_directory($dir, TRUE); // Save the file. - if (file_save_data($data, $dest)) { + if (file_save_data($data, $dest, FILE_EXISTS_REPLACE, TRUE)) { $language->javascript = $data_hash; // If we deleted a previous version of the file and we replace it with a // new one we have an update. diff --git a/modules/locale/locale.module b/modules/locale/locale.module index 092eb48..57eeca5 100644 --- a/modules/locale/locale.module +++ b/modules/locale/locale.module @@ -496,7 +496,7 @@ function locale_system_update($components) { function locale_update_js_files() { global $language; - $dir = file_create_path(variable_get('locale_js_directory', 'languages')); + $dir = file_create_path(variable_get('locale_js_directory', 'languages'), TRUE); $parsed = variable_get('javascript_parsed', array()); // The first three parameters are NULL in order to get an array with all diff --git a/modules/system/system.admin.inc b/modules/system/system.admin.inc index 4ab16b5..0043487 100644 --- a/modules/system/system.admin.inc +++ b/modules/system/system.admin.inc @@ -1391,11 +1391,11 @@ function system_performance_settings() { $form['bandwidth_optimizations'] = array( '#type' => 'fieldset', '#title' => t('Bandwidth optimizations'), - '#description' => t('

Drupal can automatically optimize external resources like CSS and JavaScript, which can reduce both the size and number of requests made to your website. CSS files can be aggregated and compressed into a single file, while JavaScript files are aggregated (but not compressed). These optional optimizations may reduce server load, bandwidth requirements, and page loading times.

These options are disabled if you have not set up your files directory, or if your download method is set to private.

') + '#description' => t('

Drupal can automatically optimize external resources like CSS and JavaScript, which can reduce both the size and number of requests made to your website. CSS files can be aggregated and compressed into a single file, while JavaScript files are aggregated (but not compressed). These optional optimizations may reduce server load, bandwidth requirements, and page loading times.

These options are disabled if you have not set up your files directory, or if your download method is set to private and directory %public_files does not exists or isn\'t writable.

', array('%public_files' => variable_get('file_dirpublic_path', 'files'))) ); - $directory = file_directory_path(); - $is_writable = is_dir($directory) && is_writable($directory) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC); + $directory = file_directory_path(0, TRUE); + $is_writable = is_dir($directory) && is_writable($directory); $form['bandwidth_optimizations']['preprocess_css'] = array( '#type' => 'radios', '#title' => t('Optimize CSS files'),