Reputation: 11392
I'm developing a web application using codeigniter and I want to dynamically generate a CSS and a JS file.
By that I mean, depending on which modules a user uses on the site, it should include different CSS and JS files. Something like this for the CSS:
foreach($modules as $module){
include('path/to/module/css');
}
I'll save this inside a class called 'modules' and inside a function called 'get_css()'. So the codeigniter link will be: http://somesite.com/modules/get_css
But I want the server to think that this is a css file and I would also like for it to not be inside my application (ie: application/controllers) as this could be a security threat. But instead be in the root directory.
I'm supposing I have to do something with htaccess, and maybe create another file that gets the content from that page? I have actually no idea how to do either, and/or if it's the correct way.
Thanks for you help.
Upvotes: 1
Views: 351
Reputation: 1441
You could make your get_css() method retrieve the contents of the CSS files, merge them together and then output a cached CSS file to disk. It would be helpful to use a hash to name the files so you only generate merged CSS files for each unique combination of modules. So you'd merge the contents of all the files and name it as:
$output_filename = md5( $module_a_name . $module_b_name ... ) . '.css' ;
if (! file_exists( $output_filename ) {
//generate merged CSS file
}
// Call back to your template layer to include $output_file in the page header
By checking if that file already exists on the server, you can prevent the re-work of generating the files, and then simply output a style include in your template pointing at the resulting file. No .htaccess changes necessary, just let files just get served in the same way as your other static content.
I'm by no means a security expert, but as long as PHP is configured to write these files no a non-executable directory and you don't use user input to grab the source CSS files you should be fine. You'll also need to clear the cached CSS files when you update the source CSS files.
Upvotes: 3