*/
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;
}
/**
}
}
+ /**
+ * 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.
*
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['*'];
+ }
+
}