Skip to content

Commit

Permalink
Prevent adding of guid to directories (issue #8) + fix coding standar…
Browse files Browse the repository at this point in the history
…ds filesluggy.class.php.
  • Loading branch information
joeke committed Nov 8, 2016
1 parent f358447 commit 11b1b02
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 28 deletions.
2 changes: 1 addition & 1 deletion _build/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 4 additions & 0 deletions core/components/filesluggy/docs/changelog.txt
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
106 changes: 79 additions & 27 deletions core/components/filesluggy/model/filesluggy/filesluggy.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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']);


/**
*
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 11b1b02

Please sign in to comment.