Regular expressions aren't the fastest things in the world, so it's best to avoid them if possible. I think this might be such a case here where you don't need them, but it depends how you set things up.
PHP Code:
if (strpos($file_edit, PHP_FILE_TREE_PATH) === 0)){ // if PHP_FILE_TREE_PATH is at the beginning of $file_edit... readfile($file_edit); }else{ echo 'Hack attempt detected'; }
Some notes: if you are using PHP 5, you can replace strpos with stripos (case insensitive). If not, you will want to do a strtolower() before you execute strpos().
On a side note, I actually don't like this way of validating which files can be edited. It just doesn't seem very secure. What if someone enters "/a/valid/directory/../../private_directory/file.php"? I don't know what would happen there. I'd personally prefer to use a database table to list specific files. It would likely be more secure because the users could only choose files listed in the table, and you could add more functionality, such as group file permissions.
__________________
The interlocking pieces of web development: usability, performance, accessibility, and standards.
Last edited by frost : 09-22-2007 at 01:08 AM.
|