Set cache folder for dompdf
authorWilliam Mortada <williammortada@thirdsectordesign.org>
Tue, 21 Nov 2023 10:22:52 +0000 (10:22 +0000)
committerWilliam Mortada <williammortada@thirdsectordesign.org>
Wed, 22 Nov 2023 11:07:53 +0000 (11:07 +0000)
CRM/Utils/PDF/Utils.php

index decc1b39eabda89fa1fe5132c2ba646926adbb65..847af31e82da51a26617d8023b43e82883928976 100644 (file)
@@ -258,8 +258,35 @@ class CRM_Utils_PDF_Utils {
         $settings[$setting] = $value;
       }
     }
+
+    // core#4791 - Set cache dir to prevent files being generated in font dir
+    $cacheDir = self::getCacheDir($settings);
+    if ($cacheDir !== "") {
+      $settings['font_cache'] = $cacheDir;
+    }
     $options->set($settings);
     return $options;
   }
 
+  /**
+   * Get location of cache folder.
+   *
+   * @param array $settings
+   * @return string
+   */
+  private static function getCacheDir(array $settings): string {
+    // Use subfolder of custom font dir if it is writable
+    if (isset($settings['font_dir']) && is_writable($settings['font_dir'])) {
+      $cacheDir = $settings['font_dir'] . DIRECTORY_SEPARATOR . 'font_cache';
+    }
+    else {
+      $cacheDir = Civi::paths()->getPath('[civicrm.files]/upload/font_cache');
+    }
+    // Try to create dir if it doesn't exist or return empty string
+    if ((!is_dir($cacheDir)) && (!mkdir($cacheDir))) {
+      $cacheDir = "";
+    }
+    return $cacheDir;
+  }
+
 }