From a5f36bd154742278dd3cb5fe631968f021e3f8d2 Mon Sep 17 00:00:00 2001
From: Earl Miles <merlin@logrus.com>
Date: Wed, 6 May 2009 00:10:01 +0000
Subject: [PATCH] #447064: Make the CSS cache directory creation more robust,
 plus add hook_requirements as well.

---
 ctools.install   | 31 +++++++++++++++++++++++++++++++
 includes/css.inc |  7 ++++++-
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/ctools.install b/ctools.install
index 6b63e9b9..446c1f3c 100644
--- a/ctools.install
+++ b/ctools.install
@@ -6,6 +6,37 @@
  * Contains install and update functions for ctools.
  */
 
+/**
+ * Use requirements to ensure that the CTools CSS cache directory can be
+ * created.
+ */
+function ctools_requirements($phase) {
+  $requirements = array();
+  if ($phase == 'runtime') {
+    $path = file_create_path('ctools/css');
+    if (!file_check_directory($path)) {
+      $path = file_directory_path() . '/ctools';
+      file_check_directory($path, FILE_CREATE_DIRECTORY);
+      $path .= '/css';
+      file_check_directory($path, FILE_CREATE_DIRECTORY);
+    }
+
+    $requirements['ctools_css_cache'] = array(
+      'title' => t('CTools CSS Cache'),
+      'severity' => REQUIREMENT_OK,
+      'value' => t('Exists'),
+    );
+
+    if (!file_check_directory($path)) {
+      $requirements['ctools_css_cache']['description'] = t('The CTools CSS cache directory could not be created due to a misconfigured files directory. Please ensure that the files directory is corretly configured and that the webserver has permission to create directories.');
+      $requirements['ctools_css_cache']['severity'] = REQUIREMENT_ERROR;
+      $requirements['ctools_css_cache']['value'] = t('Unable to create');
+    }
+  }
+
+  return $requirements;
+}
+
 /**
  * Implementation of hook_install()
  */
diff --git a/includes/css.inc b/includes/css.inc
index 313188a1..c8c19804 100644
--- a/includes/css.inc
+++ b/includes/css.inc
@@ -147,13 +147,18 @@ function ctools_css_cache($css, $filter = TRUE) {
 
   // Create the css/ within the files folder.
   $path = file_create_path('ctools/css');
-  if (!$path) {
+  if (!file_check_directory($path)) {
     $path = file_directory_path() . '/ctools';
     file_check_directory($path, FILE_CREATE_DIRECTORY);
     $path .= '/css';
     file_check_directory($path, FILE_CREATE_DIRECTORY);
   }
 
+  if (!file_check_directory($path)) {
+    drupal_set_message(t('Unable to create CTools CSS cache directory. Check the permissions on your files directory.'), 'error');
+    return;
+  }
+
   // @todo Is this slow? Does it matter if it is?
   $filename = $path . '/' . md5($css) . '.css';
 
-- 
GitLab