I am currently building an administration panel. There is a page where a file containing HTML is loaded into a html editor (TinyMCE) and is saved to the same file that was loaded (to save the changes).
The file is simply a page that has dynamic links that are to be interchanged from an administration panel. If all of these pages were the same, then I would simply save core values to a database and then load specific sections, however that is not the case as there are many variations of the templates, it's rare that any two are the same.
Therefore, I wanted an application that clients could use to edit these pages themselves. How would I go about saving these files in a secure way that strips any malicious behavior?
Sanitising html when storing it in the db is generally a bad idea, if you get it wrong and you corrupt the data there is no going back, you'd have to get your users to re-input the content once you'd fixed any mistakes. You might think you've covered all bases but there will always be that user that says "When I do this...."
Instead just store the data in the database as it comes in (obviously filtering for sql injection) You don't want Mr O'reilly breaking your db.
You need to filter the content on the way out of the db, how you do this depends on your circumstances.
If you are just expecting your users to store text, then using
htmlspecialchars is sufficient this will change
< and so on
However if you want your users to be able to use tags then strip tags would be more appropriate
$allowable_tags = array( '<p>', '<h1>', '<h2>', '<h3>', '<strong>', '<em>', ); $content = $db->get($page_id); echo strip_tags($content,implode('',$allowable_tags));