The BuddyPress Group Documents plugin allows groups a handy way for users to share documents with fellow members of a BP group. It’s crucial to the work that is done on the CUNY Academic Commons. But, by default, the plugin stores documents in a subdirectory of your WP uploads folder (usually /wp-content/blogs.dir/ on multisite). That means that documents are available directly, to anyone who has the URL, regardless of the public/private/hidden status of groups. This isn’t a problem from within BuddyPress, since URLs for the documents only appear inside of the protected group interface. But if the URL is shared, then the document becomes publicly available. Worse, if someone posts the URL of a document in a public place, search engine bots will find it, and the contents of the document could end up in Google.
I wrote a few helper functions to change this behavior. The strategy is this: Move the files so that they are not accessible via URL, ie in a directory above the web root. (In my case, it’s a directory called bp-group-documents, just above my web root.) Then, catch requests of a certain type (I’ve chosen to go with a URL parameter get_group_doc=), and check them to see whether the current user has the adequate permission to access the document in question. Finally, make sure that all of the URLs and paths that BPGD uses to upload and display documents are filtered to the updated versions. I’ve provided my code below – use and modify at your pleasure. You should be able to place it in your plugins/bp-custom.php file, and then move your existing docs from their current location (probably something like wp-content/blogs.dir/1/files/group-documents) to the new directory.
I also added a line to my .htaccess file to ensure that requests to the old URLs are redirected to the new, hardened URL. That line is this:
RewriteRule ^wp-content/blogs.dir/1/files/group-documents/(.*) /?get_group_doc=$1 [R,L]
Obviously, you may have to modify it for different file paths.
EDITED Feb 8, 2011 to include the code for creating directories when none exist
I just had this issue come up yesterday. Thanks for posting! Do you know if there is also a way to restrict the group documents by role? We post homework for our students as well as the answers for the parents. Of course we don’t want the students to have the answers. 🙂
Hi Kate. Yes, it’s possible to do this, but it will take some custom coding to make it work. First, it depends on how you are distinguishing teachers and parents from students. Are you using a piece of user_meta? Or a BP xprofile field? Or have you defined roles with custom permissions, so that you can use current_user_can()? In any case, you’ll need a conditional statement that tests for this, and if the property does not exist, then return false. Keep in mind that this is only a fix for the download script in this post – the listing of documents on the group’s Documents tab is handled by a different method. You’d have to dig into the code to find out what it is; I am afraid I don’t know it well enough to tell you off the top of my head. If I were you, I would look at the code that the plugin uses to display by category, and repurpose it for your purposes.
This is fantastically useful. I take it that the bp-group-documents folder needs world write access to allow Apache to write to it, yes?
What would it take for all uploads to wp-content/blogs.dir to be stored outside the web root? Would that not make the whole system more secure? Certainly moodle stores it’s data in this way by default.
I know this is a fairly basic function, but I am not much of a coder…any suggestions on someone who could do this for a SUNY group?
The test you use on line 033 to see if a group with the provided ID exists doesn’t appear to work.
When a new BP_Groups_Group instance is created (as you do on line 031) by passing in an non existent ID, the returned BP_Groups_Group object’s ID property still contains the ID you passed in, even though the group doesn’t actually exist.
Testing the ‘date_created’ property instead appears to work, or do you have any other suggestion?
Thanks for pointing this out, Chris. I’ve been following the BP Trac ticket that you opened regarding the issue, and I’ll update this post accordingly when that’s been settled.
Merci Boone pour le plugin 🙂
You know that this plugin was delated from WP directory and it’s now supported here:
I tried your code and it seems work but with some files not, sometimes visitors can download the file and sometimes visitors and members are redirected to group home without any error message!
I am using Bp 1.6.1 and WP 3.4.1 and the plugin above.