summaryrefslogtreecommitdiff
path: root/modules/gallery/helpers
diff options
context:
space:
mode:
authorshadlaws <shad@shadlaws.com>2013-03-13 10:07:58 +0100
committershadlaws <shad@shadlaws.com>2013-03-13 10:07:58 +0100
commit8d0e1b4c4d456d9d2d94c29412629374d0b26d35 (patch)
tree6a50901bdc6552d5dc42e58fc338015e763d8f3e /modules/gallery/helpers
parent8b457bf39b51495706a6be501f93f80bbd6d1fef (diff)
#2059 - Add album name sanitizing similar to photo/movie filename sanitizing.
- added legal_file::sanitize_dirname(), analogous to sanitize_filename. - revised item model to use new function when adding or updating an album. - added some legal_file unit tests. - revised some item model unit tests.
Diffstat (limited to 'modules/gallery/helpers')
-rw-r--r--modules/gallery/helpers/legal_file.php29
1 files changed, 28 insertions, 1 deletions
diff --git a/modules/gallery/helpers/legal_file.php b/modules/gallery/helpers/legal_file.php
index f8547011..9f02fe70 100644
--- a/modules/gallery/helpers/legal_file.php
+++ b/modules/gallery/helpers/legal_file.php
@@ -298,7 +298,7 @@ class legal_file_Core {
$filename = str_replace("/", "_", $filename);
$filename = str_replace("\\", "_", $filename);
- // Remove extra dots from the filename. This will also remove extraneous underscores.
+ // Remove extra dots from the filename. Also removes extraneous and leading/trailing underscores.
$filename = legal_file::smash_extensions($filename);
// It's possible that the filename has no base (e.g. ".jpg") - if so, give it a generic one.
@@ -308,4 +308,31 @@ class legal_file_Core {
return $filename;
}
+
+ /**
+ * Sanitize a directory name for an album. This returns a completely legal and valid
+ * directory name.
+ *
+ * @param string $dirname (with no parent directory)
+ * @return string sanitized dirname
+ */
+ static function sanitize_dirname($dirname) {
+ // It should be a dirname without a parent directory - remove all slashes (and backslashes).
+ $dirname = str_replace("/", "_", $dirname);
+ $dirname = str_replace("\\", "_", $dirname);
+
+ // Remove extraneous and leading/trailing underscores.
+ $dirname = preg_replace("/[_]+/", "_", $dirname);
+ $dirname = trim($dirname, "_");
+
+ // Remove any trailing dots.
+ $dirname = rtrim($dirname, ".");
+
+ // It's possible that the dirname is now empty - if so, give it a generic one.
+ if (empty($dirname)) {
+ $dirname = "album";
+ }
+
+ return $dirname;
+ }
}