+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2016 |
+ | Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
/**
*
* @package CRM
- * @copyright CiviCRM LLC (c) 2004-2016
+ * @copyright CiviCRM LLC (c) 2004-2017
*/
/**
*/
public static function cleanDir($target, $rmdir = TRUE, $verbose = TRUE) {
static $exceptions = array('.', '..');
- if ($target == '' || $target == '/') {
+ if ($target == '' || $target == '/' || !$target) {
throw new Exception("Overly broad deletion");
}
* The directory where the file should be saved.
* @param string $contents
* Optional: the contents of the file.
+ * @param string $fileName
*
* @return string
* The filename saved, or FALSE on failure.
*/
- public static function createFakeFile($dir, $contents = 'delete me') {
+ public static function createFakeFile($dir, $contents = 'delete me', $fileName = NULL) {
$dir = self::addTrailingSlash($dir);
- $file = 'delete-this-' . CRM_Utils_String::createRandom(10, CRM_Utils_String::ALPHANUMERIC);
- $success = file_put_contents($dir . $file, $contents);
+ if (!$fileName) {
+ $fileName = 'delete-this-' . CRM_Utils_String::createRandom(10, CRM_Utils_String::ALPHANUMERIC);
+ }
+ $success = file_put_contents($dir . $fileName, $contents);
- return ($success === FALSE) ? FALSE : $file;
+ return ($success === FALSE) ? FALSE : $fileName;
}
/**
}
/**
+ * Make a valid file name.
+ *
* @param string $name
*
* @return string
}
/**
- * @param $path
- * @param $ext
+ * Copies a file
+ *
+ * @param $filePath
+ * @return mixed
+ */
+ public static function duplicate($filePath) {
+ $oldName = pathinfo($filePath, PATHINFO_FILENAME);
+ $uniqID = md5(uniqid(rand(), TRUE));
+ $newName = preg_replace('/(_[\w]{32})$/', '', $oldName) . '_' . $uniqID;
+ $newPath = str_replace($oldName, $newName, $filePath);
+ copy($filePath, $newPath);
+ return $newPath;
+ }
+
+ /**
+ * Get files for the extension.
+ *
+ * @param string $path
+ * @param string $ext
*
* @return array
*/
return TRUE;
}
+ /**
+ * Format file.
+ *
+ * @param array $param
+ * @param string $fileName
+ * @param array $extraParams
+ */
public static function formatFile(&$param, $fileName, $extraParams = array()) {
if (empty($param[$fileName])) {
return;
return self::getFileURL($path, $mimeType);
}
+
+ /**
+ * Get file icon class for specific MIME Type
+ *
+ * @param string $mimeType
+ * @return string
+ */
+ public static function getIconFromMimeType($mimeType) {
+ if (!isset(Civi::$statics[__CLASS__]['mimeIcons'])) {
+ Civi::$statics[__CLASS__]['mimeIcons'] = json_decode(file_get_contents(__DIR__ . '/File/mimeIcons.json'), TRUE);
+ }
+ $iconClasses = Civi::$statics[__CLASS__]['mimeIcons'];
+ foreach ($iconClasses as $text => $icon) {
+ if (strpos($mimeType, $text) === 0) {
+ return $icon;
+ }
+ }
+ return $iconClasses['*'];
+ }
+
}