CRM-15168 more metadata titles (& whitespace
[civicrm-core.git] / CRM / Utils / File.php
index 568fd3ce065ff1a36c45b2d801f554c3081f581b..82a18ccc0ce60048c3d76a2c97472511a6fd2b7e 100644 (file)
@@ -129,7 +129,12 @@ class CRM_Utils_File {
    * delete a directory given a path name, delete children directories
    * and files if needed
    *
-   * @param string $path  the path name
+   * @param $target
+   * @param bool $rmdir
+   * @param bool $verbose
+   *
+   * @throws Exception
+   * @internal param string $path the path name
    *
    * @return void
    * @access public
@@ -172,6 +177,10 @@ class CRM_Utils_File {
     }
   }
 
+  /**
+   * @param $source
+   * @param $destination
+   */
   static function copyDir($source, $destination) {
     $dir = opendir($source);
     @mkdir($destination);
@@ -237,6 +246,9 @@ class CRM_Utils_File {
   /**
    * Appends trailing slashed to paths
    *
+   * @param $name
+   * @param null $separator
+   *
    * @return string
    * @access public
    * @static
@@ -252,6 +264,13 @@ class CRM_Utils_File {
     return $name;
   }
 
+  /**
+   * @param $dsn
+   * @param $fileName
+   * @param null $prefix
+   * @param bool $isQueryString
+   * @param bool $dieOnErrors
+   */
   static function sourceSQLFile($dsn, $fileName, $prefix = NULL, $isQueryString = FALSE, $dieOnErrors = TRUE) {
     require_once 'DB.php';
 
@@ -294,6 +313,11 @@ class CRM_Utils_File {
     }
   }
 
+  /**
+   * @param $ext
+   *
+   * @return bool
+   */
   static function isExtensionSafe($ext) {
     static $extensions = NULL;
     if (!$extensions) {
@@ -345,6 +369,11 @@ class CRM_Utils_File {
     return $name;
   }
 
+  /**
+   * @param $name
+   *
+   * @return string
+   */
   static function makeFileName($name) {
     $uniqID   = md5(uniqid(rand(), TRUE));
     $info     = pathinfo($name);
@@ -362,6 +391,12 @@ class CRM_Utils_File {
     }
   }
 
+  /**
+   * @param $path
+   * @param $ext
+   *
+   * @return array
+   */
   static function getFilesByExtension($path, $ext) {
     $path  = self::addTrailingSlash($path);
     $dh    = opendir($path);
@@ -378,7 +413,8 @@ class CRM_Utils_File {
   /**
    * Restrict access to a given directory (by planting there a restrictive .htaccess file)
    *
-   * @param string $dir  the directory to be secured
+   * @param string $dir the directory to be secured
+   * @param bool $overwrite
    */
   static function restrictAccess($dir, $overwrite = FALSE) {
     // note: empty value for $dir can play havoc, since that might result in putting '.htaccess' to root dir
@@ -457,6 +493,11 @@ HTACCESS;
     return $_path;
   }
 
+  /**
+   * @param $directory
+   *
+   * @return string
+   */
   static function relativeDirectory($directory) {
     // Do nothing on windows
     if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
@@ -480,9 +521,15 @@ HTACCESS;
     return $directory;
   }
 
+  /**
+   * @param $directory
+   *
+   * @return string
+   */
   static function absoluteDirectory($directory) {
-    // Do nothing on windows - config will need to specify absolute path
-    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
+    // check if directory is already absolute, if so return immediately
+    // Note: Windows PHP accepts any mix of "/" or "\", so "C:\htdocs" or "C:/htdocs" would be a valid absolute path
+    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && preg_match(';^[a-zA-Z]:[/\\\\];', $directory)) {
       return $directory;
     }
 
@@ -500,6 +547,9 @@ HTACCESS;
   /**
    * Make a file path relative to some base dir
    *
+   * @param $directory
+   * @param $basePath
+   *
    * @return string
    */
   static function relativize($directory, $basePath) {
@@ -588,6 +638,8 @@ HTACCESS;
    *
    * @param string $parent
    * @param string $child
+   * @param bool $checkRealPath
+   *
    * @return bool
    */
   static function isChildPath($parent, $child, $checkRealPath = TRUE) {
@@ -616,6 +668,8 @@ HTACCESS;
    *
    * @param string $fromDir the directory which should be moved
    * @param string $toDir the new location of the directory
+   * @param bool $verbose
+   *
    * @return bool TRUE on success
    */
   static function replaceDir($fromDir, $toDir, $verbose = FALSE) {
@@ -634,5 +688,62 @@ 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) {
+    $config = CRM_Core_Config::singleton();
+    // FIXME: Need a better way of getting the url of the baseFilePath
+    return self::addTrailingSlash(str_replace('/persist/contribute', '', $config->imageUploadURL), '/') . 'dynamic/' . $fileName;
+  }
+
+  /**
+   * Delete all files from the dynamic resource directory
+   */
+  static function flushDynamicResources() {
+    $files = glob(self::dynamicResourcePath('*'));
+    foreach ($files ? $files : array() as $file) {
+      if (is_file($file)) {
+        unlink($file);
+      }
+    }
+  }
 }