From 11b1b025fcc4d49f9aaa51ee3a9b8ef24e0f0d79 Mon Sep 17 00:00:00 2001 From: Joeke Kloosterman Date: Tue, 8 Nov 2016 14:14:02 +0100 Subject: [PATCH] Prevent adding of guid to directories (issue #8) + fix coding standards filesluggy.class.php. --- _build/config.json | 2 +- core/components/filesluggy/docs/changelog.txt | 4 + .../model/filesluggy/filesluggy.class.php | 106 +++++++++++++----- 3 files changed, 84 insertions(+), 28 deletions(-) diff --git a/_build/config.json b/_build/config.json index bd8a022..bd63c2c 100644 --- a/_build/config.json +++ b/_build/config.json @@ -3,7 +3,7 @@ "lowCaseName": "filesluggy", "description": "A MODX extra to sanitize file- and foldernames upon upload and creation.", "author": "Sterc", - "version": "1.3.1", + "version": "1.3.2", "package":{ "menus": [], "elements": { diff --git a/core/components/filesluggy/docs/changelog.txt b/core/components/filesluggy/docs/changelog.txt index 44879bf..b307ba5 100644 --- a/core/components/filesluggy/docs/changelog.txt +++ b/core/components/filesluggy/docs/changelog.txt @@ -1,5 +1,9 @@ Changelog for FileSluggy, a MODX Revolution extra by Sterc. +FileSluggy 1.3.2 +============== +- Prevent adding of guid to directories (issue #8). + FileSluggy 1.3.1 ============== - Wrap baseName in realpath function (Issue #10). diff --git a/core/components/filesluggy/model/filesluggy/filesluggy.class.php b/core/components/filesluggy/model/filesluggy/filesluggy.class.php index af305a7..ecdc6ec 100644 --- a/core/components/filesluggy/model/filesluggy/filesluggy.class.php +++ b/core/components/filesluggy/model/filesluggy/filesluggy.class.php @@ -22,18 +22,68 @@ public function __construct(modX &$modx, array $config = array()) $config, $this->modx->getOption('core_path') . 'components/filesluggy/' ); - $charSet = $this->modx->getOption('charset_iconv', $config, $this->modx->getOption('filesluggy.charset_iconv', null, 'US-ASCII//TRANSLIT')); - $Encoding = strtoupper($this->modx->getOption('enc', $config, $this->modx->getOption('filesluggy.enc', null, 'UTF-8'))); - $RegExp = $this->modx->getOption('regexp', $config, $this->modx->getOption('filesluggy.regexp', null, '/[^\.A-Za-z0-9 _-]/')); - $AddGUID = (boolean) $this->modx->getOption('guid_use', $config, $this->modx->getOption('filesluggy.guid_use', null, 0)); - $fileNamePrefix = (string) $this->modx->getOption('filenamePrefix', $config, $this->modx->getOption('filesluggy.filename_prefix', null, '')); - $ignoreFilename = (boolean) $this->modx->getOption('ignoreFilename', $config, $this->modx->getOption('filesluggy.ignorefilename', null, 0)); // Replaces the whole file name with a guid. - $Delimiter = $this->modx->getOption('word_delimiter', $config, $this->modx->getOption('filesluggy.word_delimiter', null, '-')); - $fileTypes = $this->modx->getOption('allowed_file_types', $config, $this->modx->getOption('filesluggy.allowed_file_types', null, 'jpg,jpeg,png,gif,psd,ico,bmp,svg,doc,docx,pdf')); - $LowerCaseOnly = (boolean) $this->modx->getOption('lowercase_only', $config, $this->modx->getOption('filesluggy.lowercase_only', null, 1)); - $constrainMediaSource = $this->modx->getOption('constrain_mediasource', $config, $this->modx->getOption('filesluggy.constrain_mediasource', null, null)); + $charSet = $this->modx->getOption( + 'charset_iconv', + $config, + $this->modx->getOption('filesluggy.charset_iconv', null, 'US-ASCII//TRANSLIT') + ); + $Encoding = strtoupper( + $this->modx->getOption( + 'enc', + $config, + $this->modx->getOption('filesluggy.enc', null, 'UTF-8') + ) + ); + $RegExp = $this->modx->getOption( + 'regexp', + $config, + $this->modx->getOption('filesluggy.regexp', null, '/[^\.A-Za-z0-9 _-]/') + ); + $AddGUID = (boolean) $this->modx->getOption( + 'guid_use', + $config, + $this->modx->getOption('filesluggy.guid_use', null, 0) + ); + $fileNamePrefix = (string) $this->modx->getOption( + 'filenamePrefix', + $config, + $this->modx->getOption('filesluggy.filename_prefix', null, '') + ); + $ignoreFilename = (boolean) $this->modx->getOption( + 'ignoreFilename', + $config, + $this->modx->getOption('filesluggy.ignorefilename', null, 0) + ); // Replaces the whole file name with a guid. + $Delimiter = $this->modx->getOption( + 'word_delimiter', + $config, + $this->modx->getOption('filesluggy.word_delimiter', null, '-') + ); + $fileTypes = $this->modx->getOption( + 'allowed_file_types', + $config, + $this->modx->getOption( + 'filesluggy.allowed_file_types', + null, + 'jpg,jpeg,png,gif,psd,ico,bmp,svg,doc,docx,pdf' + ) + ); + $LowerCaseOnly = (boolean) $this->modx->getOption( + 'lowercase_only', + $config, + $this->modx->getOption('filesluggy.lowercase_only', null, 1) + ); + $constrainMediaSource = $this->modx->getOption( + 'constrain_mediasource', + $config, + $this->modx->getOption('filesluggy.constrain_mediasource', null, null) + ); $cultureKey = $this->modx->getOption('cultureKey', null, 'en'); - $sanitizeDir = $this->modx->getOption('sanitizeDir', $config, $this->modx->getOption('filesluggy.sanitizeDir', null, false)); + $sanitizeDir = $this->modx->getOption( + 'sanitizeDir', + $config, + $this->modx->getOption('filesluggy.sanitizeDir', null, false) + ); $this->SkipIconv = function_exists('iconv') ? false : true; $this->SkipMB = function_exists('mb_check_encoding') ? false : true; @@ -189,21 +239,23 @@ public function sanitizeName($filePath, $isdir = false) /** * Add Prefix and Guid to the filename */ - if (!empty($this->config['filenamePrefix'])) { - $newFilename .= $this->config['filenamePrefix'] . $this->config['wordDelimiter']; - } - - if ((boolean) $this->config['ignoreFilename'] && (boolean) $this->config['addGuid']) { - $newFilename .= uniqid() . $this->config['wordDelimiter']; - } elseif ((boolean) $this->config['ignoreFilename'] && (boolean) !$this->config['addGuid']) { - $newFilename .= uniqid() . $this->config['wordDelimiter']; - } elseif ((boolean) !$this->config['ignoreFilename'] && (boolean) $this->config['addGuid']) { - $newFilename .=uniqid() . $this->config['wordDelimiter'] . $fileName; - } else { + if ($isdir) { $newFilename .= $fileName; + } else { + if (!empty($this->config['filenamePrefix'])) { + $newFilename .= $this->config['filenamePrefix'] . $this->config['wordDelimiter']; + } + if ((boolean)$this->config['ignoreFilename'] && (boolean)$this->config['addGuid']) { + $newFilename .= uniqid() . $this->config['wordDelimiter']; + } elseif ((boolean)$this->config['ignoreFilename'] && (boolean)!$this->config['addGuid']) { + $newFilename .= uniqid() . $this->config['wordDelimiter']; + } elseif ((boolean)!$this->config['ignoreFilename'] && (boolean)$this->config['addGuid']) { + $newFilename .= uniqid() . $this->config['wordDelimiter'] . $fileName; + } else { + $newFilename .= $fileName; + } + $newFilename = trim($newFilename, $this->config['wordDelimiter']); } - $newFilename = trim($newFilename, $this->config['wordDelimiter']); - /** * @@ -264,10 +316,10 @@ public function sanitizeName($filePath, $isdir = false) if ($newFilename == $fileName) { $this->_FileNameSameAsOrginal = true; } - if($isdir){ - return $newFilename; + if (!$isdir) { + $newFilename .= '.' . $fileExt; } - return $newFilename . '.' . $fileExt; + return $newFilename; } /**