Merge pull request #5243 from totten/4.5-dompdf
[civicrm-core.git] / CRM / Utils / File.php
index 82f18f5f45ce244b5a6b80143ddcdf02462ff426..255a0c118f332d7ff07326858eda407807214375 100644 (file)
@@ -146,8 +146,8 @@ class CRM_Utils_File {
       throw new Exception("Overly broad deletion");
     }
 
-    if ($sourcedir = @opendir($target)) {
-      while (FALSE !== ($sibling = readdir($sourcedir))) {
+    if ($dh = @opendir($target)) {
+      while (FALSE !== ($sibling = readdir($dh))) {
         if (!in_array($sibling, $exceptions)) {
           $object = $target . DIRECTORY_SEPARATOR . $sibling;
 
@@ -161,7 +161,7 @@ class CRM_Utils_File {
         }
       }
       }
-      closedir($sourcedir);
+      closedir($dh);
 
       if ($rmdir) {
         if (rmdir($target)) {
@@ -182,19 +182,20 @@ class CRM_Utils_File {
    * @param $destination
    */
   static function copyDir($source, $destination) {
-    $dir = opendir($source);
-    @mkdir($destination);
-    while (FALSE !== ($file = readdir($dir))) {
-      if (($file != '.') && ($file != '..')) {
-        if (is_dir($source . DIRECTORY_SEPARATOR . $file)) {
-          CRM_Utils_File::copyDir($source . DIRECTORY_SEPARATOR . $file, $destination . DIRECTORY_SEPARATOR . $file);
-        }
-        else {
-          copy($source . DIRECTORY_SEPARATOR . $file, $destination . DIRECTORY_SEPARATOR . $file);
+    if ($dh = opendir($source)) {
+      @mkdir($destination);
+      while (FALSE !== ($file = readdir($dh))) {
+        if (($file != '.') && ($file != '..')) {
+          if (is_dir($source . DIRECTORY_SEPARATOR . $file)) {
+            CRM_Utils_File::copyDir($source . DIRECTORY_SEPARATOR . $file, $destination . DIRECTORY_SEPARATOR . $file);
+          }
+          else {
+            copy($source . DIRECTORY_SEPARATOR . $file, $destination . DIRECTORY_SEPARATOR . $file);
+          }
         }
       }
+      closedir($dh);
     }
-    closedir($dir);
   }
 
   /**
@@ -400,14 +401,15 @@ class CRM_Utils_File {
    */
   static function getFilesByExtension($path, $ext) {
     $path  = self::addTrailingSlash($path);
-    $dh    = opendir($path);
     $files = array();
-    while (FALSE !== ($elem = readdir($dh))) {
-      if (substr($elem, -(strlen($ext) + 1)) == '.' . $ext) {
-        $files[] .= $path . $elem;
+    if ($dh = opendir($path)) {
+      while (FALSE !== ($elem = readdir($dh))) {
+        if (substr($elem, -(strlen($ext) + 1)) == '.' . $ext) {
+          $files[] .= $path . $elem;
+        }
       }
+      closedir($dh);
     }
-    closedir($dh);
     return $files;
   }
 
@@ -618,8 +620,7 @@ HTACCESS;
           }
         }
       }
-      $dh = opendir($subdir);
-      if ($dh) {
+      if ($dh = opendir($subdir)) {
         while (FALSE !== ($entry = readdir($dh))) {
           $path = $subdir . DIRECTORY_SEPARATOR . $entry;
           if ($entry{0} == '.') {
@@ -689,68 +690,5 @@ HTACCESS;
     }
     return TRUE;
   }
-
-  /**
-   * Create a static file (e.g. css or js) in the dynamic resource directory
-   * Note: if the file already exists it will be overwritten
-   * @param string $fileName
-   * @param string $contents
-   */
-  static function addDynamicResource($fileName, $contents) {
-    // First ensure the directory exists
-    $path = self::dynamicResourcePath();
-    if (!is_dir($path)) {
-      self::createDir($path);
-      self::restrictBrowsing($path);
-    }
-    file_put_contents("$path/$fileName", $contents);
-  }
-
-  /**
-   * Get the path of a dynamic resource file
-   * With no fileName supplied, returns the path of the directory
-   * @param string $fileName
-   * @return string
-   */
-  static function dynamicResourcePath($fileName = NULL) {
-    $config = CRM_Core_Config::singleton();
-    // FIXME: Use self::baseFilePath once url issue has been resolved
-    // Windows PHP accepts any mix of "/" or "\"; simpler if we only deal with one of those
-    $imageUploadDir = str_replace('\\', '/', $config->imageUploadDir);
-    $path = self::addTrailingSlash(str_replace('/persist/contribute', '', $imageUploadDir), '/') . 'dynamic';
-    if ($fileName !== NULL) {
-      $path .= "/$fileName";
-    }
-    return $path;
-  }
-
-  /**
-   * Get the URL of a dynamic resource file
-   * @param string $fileName
-   * @return string
-   */
-  static function dynamicResourceUrl($fileName, $addCacheCode = TRUE) {
-    $config = CRM_Core_Config::singleton();
-    // FIXME: Need a better way of getting the url of the baseFilePath
-    $url = self::addTrailingSlash(str_replace('/persist/contribute', '', $config->imageUploadURL), '/') . 'dynamic/' . $fileName;
-    if ($addCacheCode) {
-      return $url . '?r=' . CRM_Core_Resources::singleton()->getCacheCode();
-    }
-    return $url;
-  }
-
-  /**
-   * Delete all files from the dynamic resource directory
-   * Change the cache code to force browsers to reload new resources
-   */
-  static function flushDynamicResources() {
-    $files = glob(self::dynamicResourcePath('*'));
-    foreach ($files ? $files : array() as $file) {
-      if (is_file($file)) {
-        unlink($file);
-      }
-    }
-    CRM_Core_Resources::singleton()->resetCacheCode();
-  }
 }