TheoChevalier / typolib

Home of the Typolib’ project
http://typolib.theochevalier.fr
0 stars 6 forks source link

sanitizeFileName() might return an empty string #121

Closed TheoChevalier closed 9 years ago

TheoChevalier commented 9 years ago

https://github.com/TheoChevalier/typolib/blob/master/app/classes/Typolib/Typolib/Utils.php#L13 -> we're removing any special character. But if $name is string with only special characters, I guess we'll be trying to mkdir a folder with an empty name. We shouldn't. Instead, we should either generate random chars, or throw an exception.

Once this issue is fixed, we need to update the unit test, to cover this case.

TheoChevalier commented 9 years ago

Also, let's make it more readable: let's replace spaces with _ first.

First, we can use a str_replace like this:

$name = strreplace(' ', '', $name);

Then we can run our regex:

pregreplace('/[^a-zA-Z0-9-.]/', '', $name)

TheoChevalier commented 9 years ago

Last thing, instead of removing special characters, I'd prefer we replace then. We would reduce probability of getting an existing folder name. Currently: "Mon code!!!" -> "Mon_code" "Mon code" -> "Mon_code"

If we do replacements (with # character, for instance):

"Mon code!!!" -> "Mon_code###" "Mon code" -> "Mon_code"

-> different name, we can create the code an we won't have to throw an exception.