News:

Wondering if this will always be free?  See why free is better.

Main Menu

regexp for allowed filenames to upload

Started by katzenellenbogen, January 08, 2005, 07:05:56 AM

Previous topic - Next topic

katzenellenbogen

I'd like to be able to use regular expressions to restrict the possible filenames when users are allowed to make uploads.
This feature should especially apply to the part left of the Dot in filenames.
Any chances this is going to be implemented?

Grudge

IMHO, if you are competant enough to know how to construct a regex for this, then you are competant enough to "hack" it into the source files. Options in the admin centre exist as a means of helping people who cannot delve into the source code setup the forum as they wish. Anything that is really advanced such as this has to be added at the code level as it's just too much to ask for a "Attachment filename regex" box on the UI. If we were to add this we'd have to add boxes for regex'ing usernames, emails, locations, signatures etc etc
I'm only a half geek really...

katzenellenbogen

QuoteIMHO, if you are competant enough to know how to construct a regex for this, then you are competant enough to "hack" it into the source files.
As for me I can handle regular expressions quite well, but it won't make me a programmer able to add this feature to the php source files :(

QuoteIf we were to add this we'd have to add boxes for regex'ing usernames, emails, locations, signatures etc etc
In my opinion this would not be the case as it would only make real sense in the uploading part... If not regexp's, there could also be input fields "only allow file names starting with.." which would make it easier to handle for the forum admin... It would be very useful to keep some sort of order in filenames; in my case it is the forum of a social scienes uni faculty. Sometimes my fellow students really use stupid filenames when uploading scriptures... I have already defined a naming standard for the different lectures, dates etc., but many can't follow/understand it. So it would be great if I could overlook it by my original suggestion :)



Grudge

Well, I can tell you how to "hack this" in quite easily. In Post.php (Sources directory), find this line:

if (!empty($modSettings['attachmentSizeLimit']) && $_FILES['attachment']['size'][$n] > $modSettings['attachmentSizeLimit'] * 1024)
fatal_lang_error('smf122', false, array($modSettings['attachmentSizeLimit']));


And above it put something like:

if (!preg_match('~REGEXHERE~', $_FILES['attachment']['name'][$n]))
    fatal_error('File does not follow naming convention');


May need a little tweaking but that should do it...
I'm only a half geek really...

katzenellenbogen

Grudge, thank you for the tip.
Unfortunately I can not yet get it to work. In Post.php I inserted something as following:

if (!preg_match('smf*', $_FILES['attachment']['name'][$n]))
        fatal_error('File does not follow naming convention');


If I understand right it should now be possible to upload a file named "smfw". But instead I get errors (not relevant how the file name of file to be uploaded looks like):
Header says:
QuoteNotice: Undefined index: in /home/www/htdocs/fasala.de/smf/Sources/Load.php(1036) : eval()'d code on line 147
Text box says:
Quote2: Delimiter must not be alphanumeric or backslash

I thought the case could be the single quotes in the actual regexp, instead of doubled quotes (as suggested on official php-site. But also with "smf*" it did not work.
Any ideas?

Peter Duggan

Quote from: katzenellenbogen on January 11, 2005, 01:06:17 PM
Text box says:
Quote2: Delimiter must not be alphanumeric or backslash

It thinks the 's' of 'smf' is your opening delimiter. So look at Grudge's example, where he's used tildas as delimiters, or choose another acceptable delimiter of your own.

[Unknown]

if (!preg_match('~^smf~i', $_FILES['attachment']['name'][$n]))
        fatal_error('File does not follow naming convention');


You would have to use perl-style syntax here.  The basics are:
  - it starts and ends with a character. (here, ~...)
  - you can add i to the very end to be case insensitive.. (example: ~smf~i)
  - to match the start and end, you use ^ and $ respectively - example: ~^smf.*$~i
  - a single . matches any single character.  It can be followed by * or +, where * means "0 characters or more" and + means "1 or more".  Meaning, ~^abc.+$~ would not match "abc", but would match "abc2"...

-[Unknown]

katzenellenbogen

Many thanks for being so helpful, folks!
It is now working as I originally wished it to.

Advertisement: