From 8e9ae8e53efe5741a1ed650156059b95ea795263 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 27 Feb 2016 14:49:38 +1100 Subject: [PATCH] Clean up from #1631 - Files in the media library can now contain two dots - Retina Images Support --- modules/cms/classes/MediaLibrary.php | 29 ++++++-------- modules/cms/widgets/MediaManager.php | 56 ++++++++++++++++------------ 2 files changed, 44 insertions(+), 41 deletions(-) diff --git a/modules/cms/classes/MediaLibrary.php b/modules/cms/classes/MediaLibrary.php index 42fa3e968..76bfa8aee 100644 --- a/modules/cms/classes/MediaLibrary.php +++ b/modules/cms/classes/MediaLibrary.php @@ -381,35 +381,30 @@ class MediaLibrary $path = str_replace('\\', '/', $path); $path = '/'.trim($path, '/'); - if ($normalizeOnly) + if ($normalizeOnly) { return $path; + } $regexDirectorySeparator = preg_quote(DIRECTORY_SEPARATOR, '/'); $regexDot = preg_quote('.', '/'); $regex = [ - - /** - * Checks for parent or current directory reference at beginning of path - */ + // Checks for parent or current directory reference at beginning of path '(^'.$regexDot.'+?'.$regexDirectorySeparator.')', - /** - * Check for parent or current directory reference in middle of path - */ - '('.$regexDirectorySeparator.$regexDot.'+?'.$regexDirectorySeparator.')', + // Check for parent or current directory reference in middle of path + '('.$regexDirectorySeparator.$regexDot.'+?'.$regexDirectorySeparator.')', - /** - * Check for parent or current directory reference at end of path - */ - '('.$regexDirectorySeparator.$regexDot.'+?$)', + // Check for parent or current directory reference at end of path + '('.$regexDirectorySeparator.$regexDot.'+?$)', ]; - /** - * Now, let's combine everything to one regex + /* + * Combine everything to one regex */ $regex = '/'.implode('|', $regex).'/'; - if (preg_match($regex, $path) !== 0 || strpos($path, '//') !== false) - throw new ApplicationException(Lang::get('cms::lang.media.invalid_path', ['path'=>$path])); + if (preg_match($regex, $path) !== 0 || strpos($path, '//') !== false) { + throw new ApplicationException(Lang::get('cms::lang.media.invalid_path', compact('path'))); + } return $path; } diff --git a/modules/cms/widgets/MediaManager.php b/modules/cms/widgets/MediaManager.php index d84e07acb..bbeda53d4 100644 --- a/modules/cms/widgets/MediaManager.php +++ b/modules/cms/widgets/MediaManager.php @@ -945,25 +945,6 @@ class MediaManager extends WidgetBase } } - /** - * Creates a slug form the string. A modified version of Laravel's Str::slug - * with the main difference that it accepts @-signs - * @param string - * @return string - */ - public static function slug($string) - { - $title = Str::ascii($title); - // Convert all dashes/underscores into separator - $flip = $separator == '-' ? '_' : '-'; - $title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title); - // Remove all characters that are not the separator, letters, numbers, whitespace or @. - $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s@]+!u', '', mb_strtolower($title)); - // Replace all separator characters and whitespace by a single separator - $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title); - return trim($title, $separator); - } - protected function checkUploadPostback() { $fileName = null; @@ -989,15 +970,14 @@ class MediaManager extends WidgetBase $fileName = File::name($fileName).'.'.$extension; /* - * File name contains non-latin characters, attempt to slug the value - */ + * File name contains non-latin characters, attempt to slug the value + */ if (!$this->validateFileName($fileName)) { - $fileNameSlug = static::slug(File::name($fileName), '-'); - $fileName = $fileNameSlug.'.'.$extension; + $fileNameClean = $this->cleanFileName(File::name($fileName)); + $fileName = $fileNameClean . '.' . $extension; } // See mime type handling in the asset manager - if (!$uploadedFile->isValid()) { throw new ApplicationException($uploadedFile->getErrorMessage()); } @@ -1018,6 +998,11 @@ class MediaManager extends WidgetBase } } + /** + * Validate a proposed media item file name. + * @param string + * @return string + */ protected function validateFileName($name) { if (!preg_match('/^[0-9a-z@\.\s_\-]+$/i', $name)) { @@ -1031,6 +1016,29 @@ class MediaManager extends WidgetBase return true; } + /** + * Creates a slug form the string. A modified version of Str::slug + * with the main difference that it accepts @-signs + * @param string + * @return string + */ + protected function cleanFileName($name) + { + $title = Str::ascii($title); + + // Convert all dashes/underscores into separator + $flip = $separator = '-'; + $title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title); + + // Remove all characters that are not the separator, letters, numbers, whitespace or @. + $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s@]+!u', '', mb_strtolower($title)); + + // Replace all separator characters and whitespace by a single separator + $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title); + + return trim($title, $separator); + } + // // Cropping //