Integration of membership plugin and owncloud

I needed a way to distribute products automatically to subscribed users. In the beginning I had several ideas in mind, including git and owncloud. There already is an existing 3rd party app for owncloud to authenticate users by a WP user base but as I only want to let paid members access files I had to "expand" the script.
If you are looking for an integration of a cloud service with the membership plugin, here is the (my) solution:

1) Install owncloud (http://www.ownlcoud.org) on your server and configure it
2) Get the cloudpress plugin for owncloud from here: https://github.com/EELV-fr/Owncloud-Cloudpress
3) Rename the unzipped directory to "user_wordpress"
4) Open the "user_wordpress.php" file in a text editor and replace the "checkPassword" function (about line 67) with the following one:

` /* Check if the password is correct */
public function checkPassword($uid, $password){
if (!$this->db_conn) {
$this->connectdb();
}
if (!$this->db_conn) {
return false;
}
$query = 'SELECT user_login,user_pass,ID FROM '. self::$params['wordpress_db_prefix'] .'users WHERE user_login = "' . str_replace('"','""',$uid) . '"';
$query .= ' AND user_status = 0';
$result = mysql_query($query);
if ($result && mysql_num_rows($result)>0) {
$row = mysql_fetch_assoc($result);
$hash = $row['user_pass'];
$wplogin=$row['user_login'];
$wpuid=$row['ID'];
require_once('apps/user_wordpress/class-phpass.php');
$wp_hasher = new PasswordHash(8, TRUE);
$check = $wp_hasher->CheckPassword($password, $hash);

if ($check==true) {
if(self::$params['wordpress_global_group']!=''){
if(!OC_Group::groupExists(self::$params['wordpress_global_group'])){
OC_Group::createGroup(self::$params['wordpress_global_group']);
}

if( OC_Group::inGroup( $uid, self::$params['wordpress_global_group'] )){
// Do nothing
}
else{
OC_Group::addToGroup( $uid, self::$params['wordpress_global_group'] );
}
}
$query = 'SELECT * FROM '. self::$params['wordpress_db_prefix'] .'m_membership_relationships WHERE user_id = '.$wpuid;
$result = mysql_query($query);
if ($result && mysql_num_rows($result)>0) {
$this->setUserInfos($uid);
return $wplogin;

}

}
}
return false;
}`

5) Upload the directory to your owncloud installation in the "apps" directory
6) Enable the cloudpress app in the owncloud administration.

PS: I know the code above could be more elegant by using mysql joins, so feel free to post "better" versions of the code :wink:

With the changes above, only membership users will be able to join your cloud and do what you want them to do. You could - as I do - share a file directory with them or give them access to other tools like the calendar, server space, contacts, ...