Skip to content

Commit

Permalink
seperate file and folder name clean up and checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremytubbs committed Nov 18, 2015
1 parent fbd6561 commit 8e45923
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions src/Deepzoom.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ public function makeTiles($image, $file = NULL, $folder = NULL)

// set filename or use path filename
$filename = $file !== NULL ? $file : pathinfo($image)['filename'];
$filename = $this->cleanupFilename($filename);

// set folder or use path filename
$foldername = $folder !== NULL ? $folder : pathinfo($image)['filename'];
$foldername = $this->cleanupFolderName($foldername);

// check for spaces in names
$check = $this->checkFileFolderNames($filename, $folder);
$check = $this->checkJsonFilename($filename);
if ($check != 'ok') return $check;

$folder = $foldername.'/'.$filename.'_files';
Expand Down Expand Up @@ -244,33 +246,49 @@ public function createJSONP($filename, $height, $width)
}

/**
* @param $file
* @param $folder
* @param $string
* @return string
*/
public function cleanupFilename($string)
{
// trim space
$string = trim($string);
// replace strings and dashes with underscore
return str_replace(['/\s/', '-', ' '], '_', $string);
}

/**
* @param $string
* @return string
*/
public function cleanupFolderName($string)
{
// trim space
$string = trim($string);
// replace strings and dashes with dash
return str_replace(['/\s/', ' '], '-', $string);
}

/**
* @param $string
* @return array|string
*/
public function checkFileFolderNames($file, $folder) {
// check for space
if (preg_match('/\s/',$file) || preg_match('/\s/',$folder)) {
return [
'status' => 'error',
'message' => 'Filename and folder name must not contain spaces.'
];
}
// check for special characters
$specialCharRegex = '/[\'^£$%&*()}{@#~?><>,|=_+¬-]/';
if (preg_match($specialCharRegex, $file) || preg_match($specialCharRegex, $folder)) {
public function checkJsonFilename($string) {
// for JSONP filename cannot contain special characters
$specialCharRegex = '/[\'^£%&*()}{@#~?><> ,|=+¬-]/';
if (preg_match($specialCharRegex, $string)) {
return [
'status' => 'error',
'message' => 'Filename and folder name must not contain special characters.'
'message' => 'JSONP filename name must not contain special characters.'
];
}
// for JSONP filename cannot start with a number
$fileFirstChar = substr($file, 0, 1);
$stringFirstChar = substr($string, 0, 1);
// if numeric add 'a' to begining of filename
if (is_numeric($fileFirstChar)) {
if (is_numeric($stringFirstChar)) {
return [
'status' => 'error',
'message' => 'Filenames must not start with a numeric value.'
'message' => 'JSONP filenames must not start with a numeric value.'
];
}
return 'ok';
Expand Down

0 comments on commit 8e45923

Please sign in to comment.