[pLog-svn] r2177 - in plog/trunk: class/dao class/dao/userdata config

oscar at devel.plogworld.net oscar at devel.plogworld.net
Mon Jun 6 21:37:24 GMT 2005


Author: oscar
Date: 2005-06-06 21:37:24 +0000 (Mon, 06 Jun 2005)
New Revision: 2177

Added:
   plog/trunk/class/dao/userdata/
   plog/trunk/class/dao/userdata/baseuserdataprovider.class.php
   plog/trunk/class/dao/userdata/phpbb2userdataprovider.class.php
   plog/trunk/class/dao/userdata/ploguserdataprovider.class.php
   plog/trunk/config/userdata.properties.php
Modified:
   plog/trunk/class/dao/users.class.php
Log:
let's keep breaking things... this time, one of the most interesting features of 1.1 (IMHO): pluggable
user-data backends. The current Users class is now nothing more than a proxy class for the real
user-data backend class that is doing the dirty job. By default we will create packages using
plog's own PlogUserDataProvider but users willing to integrate plog with other tools such as phpbb2,
ldap, mambo or kerberos will be able to easily switch by editing one configuration file.

Before you ask, the fact that this requires editing one config file is intentional. This is not a
feature that 98% of the common users will need to mess around with so there's not much point in
spending too much time making it pretty.

The idea behind this feature is that plog will be able to use external sources of user data, instead
of using its own database tables, dynamically (as opposed to synchronizing the user data every day,
hour or once in a while) Whenever a user is added/removed/updated in the host application, these
changes will be immediately seen by plog and viceversa.

This feature does *not* include single login mechanisms or anything like that yet.

At the code leve, user-data "providers" are classes that extend the BaseUserDataProvider base class and provide all the methods that the original Users class provided. Since all client classes of Users expect a certain interface, this change does not break anything anywhere in the code.

Please keep in mind that this is a first approach, and a very rough one. Nobody has even started
working on additional providers so we don't know what difficulties we can find when creating new
ones... I am planning to take a look at phpbb2 because it should be easy :), but so far the current
plog user data backend is working just fine.

Let me know if you have any questions...


Added: plog/trunk/class/dao/userdata/baseuserdataprovider.class.php
===================================================================
--- plog/trunk/class/dao/userdata/baseuserdataprovider.class.php	2005-06-06 21:28:28 UTC (rev 2176)
+++ plog/trunk/class/dao/userdata/baseuserdataprovider.class.php	2005-06-06 21:37:24 UTC (rev 2177)
@@ -0,0 +1,230 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/dao/model.class.php" );
+
+	/**
+	 * This is the base class that defines the methods that all user-data providers must implement.
+	 * It also provides some common methods that will be shared by all user-data providers and that therefore
+	 * should exist in the parent class rather than being copied many times in the child classes.
+	 *
+	 * \ingroup User_Data_Providers
+	 */
+	class BaseUserDataProvider extends Model
+	{
+	
+		var $_providerConfig;
+		
+		/**
+		 * Constructor of the class
+		 *
+		 * @param providerConfig a Properties object with information about the 
+		 * provider-specific parameters, which may vary depending on the provider
+		 */
+		function BaseUserDataProvider( $providerConfig )
+		{
+			$this->Model();
+			
+			$this->_providerConfig = $providerConfig;
+		}
+		
+		/**
+		 * Returns the config specific data
+		 *
+		 * @return a Properties object with the provider configuration, as it was defined
+		 * in the userdata.properties.php file
+		 */
+		function getProviderConfiguration()
+		{
+			return( $this->_providerConfig );
+		}
+		
+        /**
+         * Returns true if the user is in the database and the username
+         * and password match
+         *
+         * @param user Username of the user who we'd like to authenticate
+         * @param pass Password of the user
+         * @return true if user and password correct or false otherwise.
+         */
+        function authenticateUser( $user, $pass )
+        {
+        }
+
+        /**
+         * Returns all the information associated to the user given
+         *
+         * @param user Username of the user from who we'd like to get the information
+         * @param pass Password of the user we'd like to get the information
+         * @return Returns a UserInfo object with the requested information, or false otherwise.
+         */
+        function getUserInfo( $user, $pass )
+        {
+        }
+
+        /**
+         * Retrieves the user information but given only a username
+         *
+         * @param username The username of the user
+         * @return Returns a UserInfo object with the requested information, or false otherwise.
+         */
+        function getUserInfoFromUsername( $username )
+        {
+        }
+
+        /**
+         * Retrieves the user infromation but given only a userid
+         *
+         * @param userId User ID of the user from whom we'd like to get the information
+         * @return Returns a UserInfo object with the requested information, or false otherwise.
+         */
+        function getUserInfoFromId( $userid, $extendedInfo = false )
+        {
+        }
+
+        /**
+         * Returns an array of BlogInfo objects with the information of all the blogs to which
+         * a user belongs
+         *
+         * @param userId Identifier of the user
+         * @return An array of BlogInfo objects to whom the user belongs.
+         */
+        function getUsersBlogs( $userid, $status = BLOG_STATUS_ALL )
+        {
+            $usersBlogs = Array();
+            $blogs = new Blogs();
+            $ids = Array();
+
+            // check if the user is the owner of any blog
+            $prefix = $this->getPrefix();
+            $owner = "SELECT * FROM {$prefix}blogs WHERE owner_id = ".$userid;			
+			if( $status != BLOG_STATUS_ALL ) 
+				$owner .= " AND status = '".Db::qstr( $status )."'";
+            
+			$result = $this->Execute( $owner );
+
+            while( $row = $result->FetchRow($result)) {
+                $usersBlogs[] = $blogs->_fillBlogInformation( $row );
+                $ids[] = $row["id"];
+            }
+
+            // and now check to which other blogs he or she belongs
+            $otherBlogs = "SELECT b.* FROM {$prefix}blogs b, {$prefix}users_permissions p 
+                           WHERE p.user_id = '".Db::qstr($userid)."' AND b.id = p.blog_id";
+            if( !empty($usersBlogs)) {
+	         	$blogIds = implode( ",", $ids );
+	         	$otherBlogs .= " AND p.blog_id NOT IN (".$blogIds.")";
+            }
+            if( $status != BLOG_STATUS_ALL )
+            	$otherBlogs .= " AND b.status = '".Db::qstr( $status )."'";
+            	
+            $result = $this->Execute( $otherBlogs );
+            // now we know to which he or she belongs, so we only have
+            // to load the information about those blogs
+            while( $row = $result->FetchRow($result)) {
+                $usersBlogs[] = $blogs->_fillBlogInformation( $row );
+            }
+
+            return $usersBlogs;
+        }
+
+        /**
+         * Returns an array with all the users available in the database
+         *
+		 * @param status
+		 * @param includeExtraInfo
+         * @param page
+         * @param itemsPerPage
+         * @return An array containing all the users.
+         */
+        function getAllUsers( $status = USER_STATUS_ALL, $includeExtraInfo = false, $page = -1, $itemsPerPage = DEFAULT_ITEMS_PER_PAGE )
+        {
+        }
+
+        /**
+         * Updates the information related to a user
+         *
+         * @param userInfo An UserInfo object containing the <b>already udpated</b> information of the
+         * user we would like to update.
+         * @return Returns true if ok or false otherwise.
+         */
+        function updateUser( $userInfo )
+        {
+        }
+
+        /**
+         * Adds a user to the database.
+         *
+         * @param user An UserInfo object with the necessary information
+         * @return Returns the identifier assigned to the user, or false if there was any error. It will also modify the
+		 * UserInfo object passed by parameter and set its database id.
+         */
+        function addUser( &$user )
+        {
+        }
+
+        /**
+         * Returns an array with all the users that belong to the given
+         * blog.
+         *
+         * @param blogId The blog identifier.
+         * @param includeOwner Wether to include the owner of the blog or not.
+         * @return An array with the information about the users who belong in
+         * one way or another to that blog.
+         */
+        function getBlogUsers( $blogId, $includeOwner = true, $status = USER_STATUS_ALL )
+        {
+        }
+
+        /**
+         * disables a user
+         *
+         * @param userId The identifier of the user we are trying to disable
+         */
+        function disableUser( $userId )
+        {
+        }
+        
+        /**
+         * Removes users from the database
+         *
+         * @param userId The identifier of the user we are trying to remove
+         */
+        function deleteUser( $userId )
+        {
+        }        
+
+        /**
+         * returns the total number of users
+         *
+         * @return total number of users
+         */
+        function getNumUsers( $status = USER_STATUS_ALL )
+        {
+        }
+        
+        /**
+         * returns true if the given username exists
+         *
+         * @param userName
+         * @return true if it exists or false otherwise
+         */
+        function userExists( $userName )
+        {  
+        }
+
+        /**
+         * get the blogid of user own
+         */
+		function getUserBlogId( $username )
+		{
+		}
+
+        /**
+         * check if the email account has been registered
+         * @return true if the email account has been registered
+         */
+        function emailExists($email)
+        {
+        }		
+	}
+?>
\ No newline at end of file


Property changes on: plog/trunk/class/dao/userdata/baseuserdataprovider.class.php
___________________________________________________________________
Name: svn:executable
   + *

Added: plog/trunk/class/dao/userdata/phpbb2userdataprovider.class.php
===================================================================
--- plog/trunk/class/dao/userdata/phpbb2userdataprovider.class.php	2005-06-06 21:28:28 UTC (rev 2176)
+++ plog/trunk/class/dao/userdata/phpbb2userdataprovider.class.php	2005-06-06 21:37:24 UTC (rev 2177)
@@ -0,0 +1,156 @@
+<?php
+
+    include_once( PLOG_CLASS_PATH."class/dao/userdata/baseuserdataprovider.class.php" );
+    include_once( PLOG_CLASS_PATH."class/dao/userinfo.class.php" );
+    include_once( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" );
+    include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
+    include_once( PLOG_CLASS_PATH."class/dao/userstatus.class.php" );
+    
+    /**
+     * Model representing the users in our application. Provides the methods such as
+     * authentication and querying for users.
+	 *
+	 * \ingroup User_Data_Providers
+     */
+    class PhpBB2UserDataProvider extends BaseUserDataProvider
+    {
+
+        /**
+         * Initializes the model
+         */
+        function PhpBB2UserDataProvider( $providerConfig )
+        {
+            $this->BaseUserDataProvider( $providerConfig );
+        }
+
+        /**
+         * Returns true if the user is in the database and the username
+         * and password match
+         *
+         * @param user Username of the user who we'd like to authenticate
+         * @param pass Password of the user
+         * @return true if user and password correct or false otherwise.
+         */
+        function authenticateUser( $user, $pass )
+        {        
+        }
+
+        /**
+         * Returns all the information associated to the user given
+         *
+         * @param user Username of the user from who we'd like to get the information
+         * @param pass Password of the user we'd like to get the information
+         * @return Returns a UserInfo object with the requested information, or false otherwise.
+         */
+        function getUserInfo( $user, $pass )
+        {
+        }
+
+        /**
+         * Retrieves the user information but given only a username
+         *
+         * @param username The username of the user
+         * @return Returns a UserInfo object with the requested information, or false otherwise.
+         */
+        function getUserInfoFromUsername( $username )
+        {
+        }
+
+        /**
+         * Retrieves the user infromation but given only a userid
+         *
+         * @param userId User ID of the user from whom we'd like to get the information
+         * @return Returns a UserInfo object with the requested information, or false otherwise.
+         */
+        function getUserInfoFromId( $userid, $extendedInfo = false )
+        {
+        }
+
+        /**
+         * Returns an array with all the users available in the database
+         *
+		 * @param status
+		 * @param includeExtraInfo
+         * @param page
+         * @param itemsPerPage
+         * @return An array containing all the users.
+         */
+        function getAllUsers( $status = USER_STATUS_ALL, $includeExtraInfo = false, $page = -1, $itemsPerPage = DEFAULT_ITEMS_PER_PAGE )
+        {
+        }
+
+        /**
+         * Updates the information related to a user
+         *
+         * @param userInfo An UserInfo object containing the <b>already udpated</b> information of the
+         * user we would like to update.
+         * @return Returns true if ok or false otherwise.
+         */
+        function updateUser( $userInfo )
+        {
+        }
+
+        /**
+         * Adds a user to the database.
+         *
+         * @param user An UserInfo object with the necessary information
+         * @return Returns the identifier assigned to the user, or false if there was any error. It will also modify the
+		 * UserInfo object passed by parameter and set its database id.
+         */
+        function addUser( &$user )
+        {
+        }
+
+        /**
+         * disables a user
+         *
+         * @param userId The identifier of the user we are trying to disable
+         */
+        function disableUser( $userId )
+        {
+        }
+        
+        /**
+         * Removes users from the database
+         *
+         * @param userId The identifier of the user we are trying to remove
+         */
+        function deleteUser( $userId )
+        {
+        }        
+
+        /**
+         * returns the total number of users
+         *
+         * @return total number of users
+         */
+        function getNumUsers( $status = USER_STATUS_ALL )
+        {
+        }
+        
+        /**
+         * returns true if the given username exists
+         *
+         * @param userName
+         * @return true if it exists or false otherwise
+         */
+        function userExists( $userName )
+        {
+        }
+
+        /**
+         * get the blogid of user own
+         */
+		function getUserBlogId( $username )
+		{
+		}
+
+        /**
+         * check if the email account has been registered
+         * @return true if the email account has been registered
+         */
+        function emailExists($email)
+        {
+        }
+    }
+?>


Property changes on: plog/trunk/class/dao/userdata/phpbb2userdataprovider.class.php
___________________________________________________________________
Name: svn:executable
   + *

Added: plog/trunk/class/dao/userdata/ploguserdataprovider.class.php
===================================================================
--- plog/trunk/class/dao/userdata/ploguserdataprovider.class.php	2005-06-06 21:28:28 UTC (rev 2176)
+++ plog/trunk/class/dao/userdata/ploguserdataprovider.class.php	2005-06-06 21:37:24 UTC (rev 2177)
@@ -0,0 +1,559 @@
+<?php
+
+    include_once( PLOG_CLASS_PATH."class/dao/userdata/baseuserdataprovider.class.php" );
+    include_once( PLOG_CLASS_PATH."class/dao/userinfo.class.php" );
+    include_once( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" );
+    include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
+    include_once( PLOG_CLASS_PATH."class/dao/userstatus.class.php" );
+    include_once( PLOG_CLASS_PATH."class/gallery/dao/galleryresources.class.php" );  
+    
+    /**
+     * Model representing the users in our application. Provides the methods such as
+     * authentication and querying for users.
+	 *
+	 * \ingroup User_Data_Providers
+     */
+    class PlogUserDataProvider extends BaseUserDataProvider
+    {
+
+        /**
+         * Initializes the model
+         */
+        function PlogUserDataProvider( $providerConfig )
+        {
+            $this->BaseUserDataProvider( $providerConfig, true );
+
+            $this->usercache = Array();
+
+            $this->perms =  new UserPermissions();
+        }
+
+        /**
+         * Returns true if the user is in the database and the username
+         * and password match
+         *
+         * @param user Username of the user who we'd like to authenticate
+         * @param pass Password of the user
+         * @return true if user and password correct or false otherwise.
+         */
+        function authenticateUser( $user, $pass )
+        {
+            $query = "SELECT * FROM ".$this->getPrefix()."users 
+                      WHERE user = '".Db::qstr($user)."' AND password = '".md5($pass)."'
+                            AND status = '".USER_STATUS_ACTIVE."'";
+
+            $result = $this->Execute( $query );
+
+            if( $result == false )
+                return false;
+
+            if( $result->RecordCount() == 1 )
+                return true;
+            else
+                return false;
+        }
+
+        /**
+         * Returns all the information associated to the user given
+         *
+         * @param user Username of the user from who we'd like to get the information
+         * @param pass Password of the user we'd like to get the information
+         * @return Returns a UserInfo object with the requested information, or false otherwise.
+         */
+        function getUserInfo( $user, $pass )
+        {
+            $userInfo = $this->getUserInfoFromUsername( $user );
+            if ( $userInfo->getPassword() == md5($pass) ) {
+                return $userInfo;
+            } else {
+                return false;
+            }
+        }
+
+        /**
+         * Retrieves the user information but given only a username
+         *
+         * @param username The username of the user
+         * @return Returns a UserInfo object with the requested information, or false otherwise.
+         */
+        function getUserInfoFromUsername( $username )
+        {
+            $userId = $this->_cache->getData( $username, CACHE_USERIDBYNAME );
+
+            if ( !$userId ) {
+                $prefix = $this->getPrefix();
+                $query = "SELECT u.id AS id, u.user AS user, u.password AS password, u.email AS email,
+                                 u.about AS about, u.full_name AS full_name, u.properties AS properties,
+                                 u.resource_picture_id AS resource_picture_id,
+                                 IF(p.permission_id = 1, 1, 0 ) AS site_admin,
+                                 u.status AS status
+                          FROM {$prefix}users u LEFT JOIN {$prefix}users_permissions p ON u.id = p.user_id
+                          WHERE u.user = '".Db::qstr($username)."'";
+                 $userInfo = $this->_getUserInfoFromQuery( $query );
+
+                 if ( $userInfo ) {
+                     $this->_cache->setData( $userInfo->getUsername(), CACHE_USERIDBYNAME, $userInfo->getId() );
+                     $this->_cache->setData( $userInfo->getId(), CACHE_USERINFO, $userInfo );
+                 }
+
+                 return $userInfo;
+            }
+
+            return $this->getUserInfoFromId( $userId );
+        }
+
+        /**
+         * Retrieves the user infromation but given only a userid
+         *
+         * @param userId User ID of the user from whom we'd like to get the information
+         * @return Returns a UserInfo object with the requested information, or false otherwise.
+         */
+        function getUserInfoFromId( $userid, $extendedInfo = false )
+        {
+            if( isset($this->usercache[$userid])) {
+                $userInfo = $this->usercache[$userid];
+            }
+            else {
+                $userInfo = $this->_cache->getData( $userid, CACHE_USERINFO );
+                if ( !$userInfo ) {
+                    $prefix = $this->getPrefix();
+                    $query = "SELECT u.id AS id, u.user AS user, u.password AS password, u.email AS email,
+                                     u.about AS about, u.full_name AS full_name, u.properties AS properties,
+                                     u.resource_picture_id AS resource_picture_id,
+                                     IF(p.permission_id = 1, 1, 0 ) AS site_admin,
+                                     u.status AS status
+                              FROM {$prefix}users u LEFT JOIN {$prefix}users_permissions p ON u.id = p.user_id
+                              WHERE u.id = $userid";
+
+                    $userInfo = $this->_getUserInfoFromQuery( $query, $extendedInfo );
+                    $this->_cache->setData( $userid, CACHE_USERINFO, $userInfo );
+                }
+
+                $this->usercache[$userid] = $userInfo;
+            }
+
+            return $userInfo;
+        }
+
+        /**
+         * More common code used by several functions
+         *
+         * Private function used to fill in all the fields of UserInfo objects given
+         * a row of the database.
+         */
+        function _getUserInfoFromQuery( $sql_query, $extendedInfo = false )
+        {
+            $result = $this->Execute( $sql_query );
+            if( !$result )
+                return false;
+
+            if( $result->RowCount() == 0 )
+                return false;
+
+            $info = $result->FetchRow( $result );
+
+            $userInfo = $this->_fillUserInformation( $info, $extendedInfo );
+
+            return $userInfo;
+        }
+
+        /**
+         * Given a result record from a Execute call, it will fill in the
+         * fields of the object, so that we don't have to repeat the same
+         * code too many times
+         */
+        function _fillUserInformation( $query_result, $extraInfo = false )
+        {
+            $userInfo = new UserInfo( $query_result["user"], $query_result["password"],
+                                      $query_result["email"],
+                                      $query_result["about"],
+                                      $query_result["full_name"],
+                                      $query_result["resource_picture_id"],
+                                      unserialize($query_result["properties"]),
+                                      $query_result["id"]);
+
+            if( $extraInfo ) {
+                // load this data if explicitely required!
+                $userBlogs = $this->getUsersBlogs($userInfo->getId());
+                $userInfo->setBlogs($userBlogs);
+            }
+
+            // set some permissions
+            //$userInfo->setSiteAdmin($this->perms->isSiteAdmin( $userInfo->getId()));
+            $userInfo->setSiteAdmin( $query_result["site_admin"] );
+            $userInfo->setStatus( $query_result["status"] );
+
+            return $userInfo;
+        }
+
+        /**
+         * Returns an array with all the users available in the database
+         *
+		 * @param status
+		 * @param includeExtraInfo
+		 * @param searchTerms
+         * @param page
+         * @param itemsPerPage
+         * @return An array containing all the users.
+         */
+        function getAllUsers( $status = USER_STATUS_ALL, $includeExtraInfo = false, $searchTerms = "", $page = -1, $itemsPerPage = DEFAULT_ITEMS_PER_PAGE )
+        {   
+	    	if( $status != USER_STATUS_ALL )
+	    		$whereStatus = "status = '".Db::qstr($status)."'";
+	    	if( $searchTerms != "" )
+	    	    $whereSearch = $this->buildSearchCondition( $searchTerms );
+	    	    
+	    	$where = $this->buildWhereCondition( Array( $whereStatus, $whereSearch ));
+	        
+            $query = "SELECT * FROM ".$this->getPrefix()."users $where ORDER BY id ASC";
+
+            $result = $this->Execute( $query, $page, $itemsPerPage );
+
+            $users = Array();
+
+            while ($info = $result->FetchRow( $result ))
+                array_push( $users, $this->_fillUserInformation( $info, $includeExtraInfo ));
+
+            return $users;
+        }
+        
+        /**
+         * @see Model::buildSearchCondition
+         */
+        function buildSearchCondition( $searchTerms )
+        {
+            $searchTerms = trim( $searchTerms );
+            $searchCond = "(user LIKE '%".Db::qstr($searchTerms)."%' 
+                           OR full_name LIKE '%".Db::qstr($searchTerms)."%' OR 
+                           email LIKE '%".Db::qstr($searchTerms)."%')";
+            
+            return( $searchCond );
+        }
+
+        /**
+         * Updates the information related to a user
+         *
+         * @param userInfo An UserInfo object containing the <b>already udpated</b> information of the
+         * user we would like to update.
+         * @return Returns true if ok or false otherwise.
+         */
+        function updateUser( $userInfo )
+        {
+            $query = "UPDATE ".$this->getPrefix().
+                     "users SET email = '".$userInfo->getEmail().
+                     "', about = '".Db::qstr($userInfo->getAboutMyself()).
+                     "', password = '".$userInfo->getPassword().
+                     "', full_name = '".Db::qstr($userInfo->getFullName()).
+                     "', resource_picture_id = '".Db::qstr($userInfo->getPictureId()).
+                     "', properties = '".Db::qstr(serialize($userInfo->getProperties())).
+                     "', status = '".Db::qstr($userInfo->getStatus()).
+                     "' WHERE id = ".$userInfo->getId().";";
+
+            // update the users table
+            $result = $this->Execute( $query );
+
+            // and now update the permissions, if there has been any change
+            $perms = new UserPermissions();
+            $perms->updateSiteAdmin( $userInfo->getId(), $userInfo->isSiteAdmin());
+
+            $this->_cache->removeData( $userInfo->getId(), CACHE_USERINFO );
+            $this->_cache->removeData( $userInfo->getUsername(), CACHE_USERIDBYNAME );
+
+            return $result;
+        }
+
+        /**
+         * Adds a user to the database.
+         *
+         * @param user An UserInfo object with the necessary information
+         * @return Returns the identifier assigned to the user, or false if there was any error. It will also modify the
+		 * UserInfo object passed by parameter and set its database id.
+         */
+        function addUser( &$user )
+        {
+            $query = "INSERT INTO ".$this->getPrefix()."users(user,password,email,about,full_name,
+                      resource_picture_id,properties,status)
+                      VALUES ('".Db::qstr($user->getUserName())."','".md5($user->getPassword())."','".
+                      Db::qstr($user->getEmail())."','".Db::qstr($user->getAboutMyself())."','".
+                      Db::qstr($user->getFullName())."', '".
+                      Db::qstr($user->getPictureId())."', '".
+                      Db::qstr(serialize($user->getProperties()))."', '".
+                      Db::qstr($user->getStatus())."');";
+
+            $result = $this->Execute( $query );
+
+            if( !$result )
+                return false;
+
+            $userId = $this->_db->Insert_ID();
+			
+			$user->setId( $userId );
+
+            return $userId;
+        }
+
+        /**
+         * Returns an array with all the users that belong to the given
+         * blog.
+         *
+         * @param blogId The blog identifier.
+         * @param includeOwner Wether to include the owner of the blog or not.
+         * @param status
+         * @param searchTerms
+         * @return An array with the information about the users who belong in
+         * one way or another to that blog.
+         */
+        function getBlogUsers( $blogId, $includeOwner = true, $status = USER_STATUS_ALL, $searchTerms = "" )
+        {
+            $users = Array();
+	        $prefix = $this->getPrefix();
+
+            // get the information about the owner, if requested so
+            if( $includeOwner ) {
+                $query = "SELECT {$prefix}users.* FROM {$prefix}users, {$prefix}blogs 
+                          WHERE {$prefix}users.id = {$prefix}blogs.owner_id AND {$prefix}blogs.id = '".Db::qstr($blogId)."';";
+                $result = $this->Execute( $query );
+
+                if( !$result )
+                    return false;
+
+                $row = $result->FetchRow();
+                array_push( $users, $this->_fillUserInformation( $row ));
+            }
+
+            // now get the other users who have permission for that blog.
+            $query2 = "SELECT {$prefix}users.* FROM {$prefix}users, {$prefix}users_permissions 
+                       WHERE {$prefix}users.id = {$prefix}users_permissions.user_id 
+                       AND {$prefix}users_permissions.blog_id = '".Db::qstr($blogId)."';";
+            $result2 = $this->Execute( $query2 );
+            if( !$result2 ) // if error, return what we have so far...
+                return $users;
+
+            while( $row = $result2->FetchRow()) {
+                array_push( $users, $this->_fillUserInformation($row));
+            }
+
+            return $users;
+        }
+
+        /**
+         * disables a user
+         *
+         * @param userId The identifier of the user we are trying to disable
+         */
+        function disableUser( $userId )
+        {
+            $query = "UPDATE ".$this->getPrefix()."users 
+                      SET status = '".USER_STATUS_DISABLED."'
+                      WHERE id = '".Db::qstr($userId)."'";
+
+            $result = $this->Execute( $query );
+
+            if( !$result )
+                return false;
+
+            if( $this->_db->Affected_Rows() == 0 )
+                return false;
+
+            return true;
+        }
+        
+        /**
+         * Removes users from the database
+         *
+         * @param userId The identifier of the user we are trying to remove
+         */
+        function deleteUser( $userId )
+        {
+            // first, delete all of his/her permissions
+            $perms = new UserPermissions();
+            $perms->revokeUserPermissions( $userId );
+
+            $query = "DELETE FROM ".$this->getPrefix()."users WHERE id = $userId;";
+
+            $result = $this->Execute( $query );
+
+            if( !$result )
+                return false;
+
+            if( $this->_db->Affected_Rows() == 0 )
+                return false;
+
+            $this->_cache->removeData( $userId, CACHE_USERINFO );
+
+            return true;
+        }  
+        
+        /**
+         * removes all users that have 'deleted' status
+         *
+         * @return Returns true if all users were deleted or false otherwise.
+         */        
+        function purgeUsers()
+        {
+            // we first need to delete all resources/album belong to the blog, 
+            // we don't need to care if the ablum has sub album, resource, we
+            // just delete all albums, resources that belong the blog which is
+            // being deleted as a result of deleting the blog's owner
+            
+            // first delete all resources
+            $galleryresources = new GalleryResources();
+            $r_query = "SELECT gr.id AS id, gr.owner_id as owner_id 
+            					  FROM ".$this->getPrefix()."gallery_resources AS gr, ".$this->getPrefix()."blogs AS b, ".$this->getPrefix()."users AS u
+            					  WHERE gr.owner_id=b.id AND b.owner_id=u.id AND u.status = 2";            					 
+            				
+            $r_result = $this->Execute( $r_query );     
+            if( !$r_result )
+                return false;
+                
+            while( $r_row = $r_result->FetchRow()) {
+            	$galleryresources->deleteResource( $r_row["id"], $r_row["owner_id"] );
+            }
+            
+            // now delete album
+            $galleryalbums = new GalleryAlbums();
+            $al_query = "SELECT ga.id AS id, ga.owner_id as owner_id 
+            					  FROM ".$this->getPrefix()."gallery_albums AS ga, ".$this->getPrefix()."blogs AS b, ".$this->getPrefix()."users AS u
+            					  WHERE ga.owner_id=b.id AND b.owner_id=u.id AND u.status = 2";            				   
+            
+            $al_result = $this->Execute( $al_query );     
+            if( !$al_result )
+                return false;
+                
+            while( $al_row = $al_result->FetchRow()) {
+            	$galleryalbums->deleteAlbum( $al_row["id"], $al_row["owner_id"] );
+            }
+                        
+            // check if the deleted user owns any blog, if they does, delete the blog
+            // the deleteBlog function will take care of deleting articles etc...            
+            $blogs = new Blogs();             
+            $userfolder = new GalleryResourceStorage();                        
+            
+            $b_query = "SELECT b.id 
+            					  FROM ".$this->getPrefix()."blogs AS b, ".$this->getPrefix()."users AS u 
+            					  WHERE b.owner_id=u.id AND u.status = 2";            					
+            				
+            $b_result = $this->Execute( $b_query );     
+            if( !$b_result )
+                return false;
+                
+            while( $b_row = $b_result->FetchRow()) {
+            	$blogs->deleteBlog( $b_row["id"] );            	
+            	// since the deleteResource doesn't take care of deleting all the user folder
+            	// and now that we have the blog id here, we can use the deleteDir function
+            	// to delete all uder folders and sub folders :)
+            	$filePath = $userfolder->getUserFolder( $b_row["id"] );  
+            	File::deleteDir( $filePath, true, false );
+            }            
+            
+            // now we need to check if user belong to any other blog and have made any posts
+            // at the moment, we cannot dertermine which resource was posted by which user
+            // so when user is deleted, resource posted by him/her will stay intact in the blog
+            // maybe we should notify the blog owner that his member was deleted?						
+						$articles = new Articles();
+						
+						$a_query = "SELECT a.id AS id, a.user_id AS user_id, a.blog_id AS blog_id
+											  FROM ".$this->getPrefix()."articles AS a, ".$this->getPrefix()."users AS u 
+					 						  WHERE a.user_id=u.id AND u.status = 2";   
+            $a_result = $this->Execute( $a_query );
+            
+            if( !$a_result )
+                return false;
+                
+            while( $a_row = $a_result->FetchRow()) {
+            	$articles->deleteArticle( $a_row["id"], $a_row["user_id"], $a_row["blog_id"], true );          	
+            }            
+                                    
+            // finally remove the user                                 
+            $u_query = "SELECT * FROM ".$this->getPrefix()."users WHERE status = 2";
+
+            $u_result = $this->Execute( $u_query );
+            if( !$u_result )
+                return false;
+            
+            while( $u_row = $u_result->FetchRow()) {
+                $this->deleteUser( $u_row["id"] );
+            }            
+                                   
+            return true;
+    	}
+
+        /**
+         * returns the total number of users
+         *
+         * @return total number of users
+         */
+        function getNumUsers( $status = USER_STATUS_ALL, $searchTerms = "" )
+        {
+            $table = $this->getPrefix()."users";
+			if( $status != USER_STATUS_ALL )
+				$whereStatus = "status = '".Db::qstr($status)."'";
+			if( $searchTerms != "" )
+			    $whereSearch = $this->buildSearchCondition( $searchTerms );
+			    
+			$where = $this->buildWhereCondition( Array( $whereStatus, $whereSearch ), false );
+				
+			return( $this->getNumItems( $table, $where ));
+        }
+        
+        /**
+         * returns true if the given username exists
+         *
+         * @param userName
+         * @return true if it exists or false otherwise
+         */
+        function userExists( $userName )
+        {
+	    	return( $this->getUserInfoFromUsername( $userName ));   
+        }
+
+        /**
+         * get the blogid of user own
+         */
+		function getUserBlogId( $username )
+		{
+            // default blog id
+            $blogId = 1;
+
+			$usersBlogs = Array();
+            $blogs = new Blogs();
+
+            $userinfo = $this->getUserInfoFromUsername($username);
+            // if userinfo is null, this maybe because username is not exists..
+            // return 0 means, should go to summary page
+            if(!$userinfo) return 0;
+            $userid = $userinfo->getId();
+            $userid = $userinfo->getId();
+
+        	// check if the user is the owner of any blog
+            $owner = "SELECT id FROM ".$this->getPrefix()."blogs WHERE owner_id = ".$userid.";";
+            $result = $this->Execute( $owner );
+
+            if(!$result)
+                return $blogId;
+
+            while( $row = $result->FetchRow($result)) {
+            	$blogId = $row["id"];
+            }
+
+            return $blogId;
+		}
+
+        /**
+         * check if the email account has been registered
+         * @return true if the email account has been registered
+         */
+        function emailExists($email){
+            $query = "SELECT email 
+                      FROM ".$this->getPrefix()."users 
+                      WHERE email = '".Db::qstr($email)."'";
+
+            $result = $this->Execute($query);
+
+            if($result && $result->RecordCount() >= 1)
+                return true;
+            else 
+                return false;
+        }
+    }
+?>
\ No newline at end of file


Property changes on: plog/trunk/class/dao/userdata/ploguserdataprovider.class.php
___________________________________________________________________
Name: svn:executable
   + *

Modified: plog/trunk/class/dao/users.class.php
===================================================================
--- plog/trunk/class/dao/users.class.php	2005-06-06 21:28:28 UTC (rev 2176)
+++ plog/trunk/class/dao/users.class.php	2005-06-06 21:37:24 UTC (rev 2177)
@@ -1,11 +1,6 @@
 <?php
 
-    include_once( PLOG_CLASS_PATH."class/dao/model.class.php" );
-    include_once( PLOG_CLASS_PATH."class/dao/userinfo.class.php" );
-    include_once( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" );
-    include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
-    include_once( PLOG_CLASS_PATH."class/dao/userstatus.class.php" );
-    include_once( PLOG_CLASS_PATH."class/gallery/dao/galleryresources.class.php" );  
+	include_once( PLOG_CLASS_PATH."class/object/object.class.php" );
     
     /**
      * Model representing the users in our application. Provides the methods such as
@@ -13,20 +8,52 @@
 	 *
 	 * \ingroup DAO
      */
-    class Users extends Model
+    class Users extends Object
     {
+	    var $_provider;
 
         /**
          * Initializes the model
          */
         function Users()
         {
-            $this->Model();
-
-            $this->usercache = Array();
-
-            $this->perms =  new UserPermissions();
+            $this->Object();
+            
+            $this->_loadUserDataProvider();
         }
+        
+        /** 
+         * @private
+         * loads the user data provider specified in the config file
+         */
+        function _loadUserDataProvider()
+        {
+	        // load the config file
+	        $config = new ConfigFileStorage( Array( "file" => PLOG_CLASS_PATH."config/userdata.properties.php" ));	     
+	        
+	        // see which class has been configured as the user data provider
+	        $providerClass = $config->getValue( "provider" );
+	        
+	        $this->log->debug( "Tring to load provider: $providerClass" );
+	        
+	        if( !$providerClass ) {
+		     	die( "ERROR: No provider class was specified in userdata.properties.php!" );   
+	        }
+	        
+	        // try to load the class
+	        include_once( PLOG_CLASS_PATH."class/dao/userdata/".strtolower( $providerClass ).".class.php" );
+	        
+	        // try to check if the class has been defined at all...
+	        if( !class_exists( $providerClass )) {
+		     	die( "ERROR: Provider class $providerClass has not been defined!" );   
+	        }
+	        	        
+	        // and if so, we can create an instance of it
+	        $this->_provider = new $providerClass( $config );	        
+	        
+	        // all is well...
+	        return( true );
+        }
 
         /**
          * Returns true if the user is in the database and the username
@@ -38,19 +65,7 @@
          */
         function authenticateUser( $user, $pass )
         {
-            $query = "SELECT * FROM ".$this->getPrefix()."users 
-                      WHERE user = '".Db::qstr($user)."' AND password = '".md5($pass)."'
-                            AND status = '".USER_STATUS_ACTIVE."'";
-
-            $result = $this->Execute( $query );
-
-            if( $result == false )
-                return false;
-
-            if( $result->RecordCount() == 1 )
-                return true;
-            else
-                return false;
+	        return( $this->_provider->authenticateUser( $user, $pass ));
         }
 
         /**
@@ -62,12 +77,7 @@
          */
         function getUserInfo( $user, $pass )
         {
-            $userInfo = $this->getUserInfoFromUsername( $user );
-            if ( $userInfo->getPassword() == md5($pass) ) {
-                return $userInfo;
-            } else {
-                return false;
-            }
+			return( $this->_provider->getUserInfo( $user, $pass ));
         }
 
         /**
@@ -78,28 +88,7 @@
          */
         function getUserInfoFromUsername( $username )
         {
-            $userId = $this->_cache->getData( $username, CACHE_USERIDBYNAME );
-
-            if ( !$userId ) {
-                $prefix = $this->getPrefix();
-                $query = "SELECT u.id AS id, u.user AS user, u.password AS password, u.email AS email,
-                                 u.about AS about, u.full_name AS full_name, u.properties AS properties,
-                                 u.resource_picture_id AS resource_picture_id,
-                                 IF(p.permission_id = 1, 1, 0 ) AS site_admin,
-                                 u.status AS status
-                          FROM {$prefix}users u LEFT JOIN {$prefix}users_permissions p ON u.id = p.user_id
-                          WHERE u.user = '".Db::qstr($username)."'";
-                 $userInfo = $this->_getUserInfoFromQuery( $query );
-
-                 if ( $userInfo ) {
-                     $this->_cache->setData( $userInfo->getUsername(), CACHE_USERIDBYNAME, $userInfo->getId() );
-                     $this->_cache->setData( $userInfo->getId(), CACHE_USERINFO, $userInfo );
-                 }
-
-                 return $userInfo;
-            }
-
-            return $this->getUserInfoFromId( $userId );
+	        return( $this->_provider->getUserInfoFromUsername( $username ));
         }
 
         /**
@@ -110,83 +99,10 @@
          */
         function getUserInfoFromId( $userid, $extendedInfo = false )
         {
-            if( isset($this->usercache[$userid])) {
-                $userInfo = $this->usercache[$userid];
-            }
-            else {
-                $userInfo = $this->_cache->getData( $userid, CACHE_USERINFO );
-                if ( !$userInfo ) {
-                    $prefix = $this->getPrefix();
-                    $query = "SELECT u.id AS id, u.user AS user, u.password AS password, u.email AS email,
-                                     u.about AS about, u.full_name AS full_name, u.properties AS properties,
-                                     u.resource_picture_id AS resource_picture_id,
-                                     IF(p.permission_id = 1, 1, 0 ) AS site_admin,
-                                     u.status AS status
-                              FROM {$prefix}users u LEFT JOIN {$prefix}users_permissions p ON u.id = p.user_id
-                              WHERE u.id = $userid";
-
-                    $userInfo = $this->_getUserInfoFromQuery( $query, $extendedInfo );
-                    $this->_cache->setData( $userid, CACHE_USERINFO, $userInfo );
-                }
-
-                $this->usercache[$userid] = $userInfo;
-            }
-
-            return $userInfo;
+	        return( $this->_provider->getUserInfoFromId( $userid, $extendedInfo ));
         }
 
         /**
-         * More common code used by several functions
-         *
-         * Private function used to fill in all the fields of UserInfo objects given
-         * a row of the database.
-         */
-        function _getUserInfoFromQuery( $sql_query, $extendedInfo = false )
-        {
-            $result = $this->Execute( $sql_query );
-            if( !$result )
-                return false;
-
-            if( $result->RowCount() == 0 )
-                return false;
-
-            $info = $result->FetchRow( $result );
-
-            $userInfo = $this->_fillUserInformation( $info, $extendedInfo );
-
-            return $userInfo;
-        }
-
-        /**
-         * Given a result record from a Execute call, it will fill in the
-         * fields of the object, so that we don't have to repeat the same
-         * code too many times
-         */
-        function _fillUserInformation( $query_result, $extraInfo = false )
-        {
-            $userInfo = new UserInfo( $query_result["user"], $query_result["password"],
-                                      $query_result["email"],
-                                      $query_result["about"],
-                                      $query_result["full_name"],
-                                      $query_result["resource_picture_id"],
-                                      unserialize($query_result["properties"]),
-                                      $query_result["id"]);
-
-            if( $extraInfo ) {
-                // load this data if explicitely required!
-                $userBlogs = $this->getUsersBlogs($userInfo->getId());
-                $userInfo->setBlogs($userBlogs);
-            }
-
-            // set some permissions
-            //$userInfo->setSiteAdmin($this->perms->isSiteAdmin( $userInfo->getId()));
-            $userInfo->setSiteAdmin( $query_result["site_admin"] );
-            $userInfo->setStatus( $query_result["status"] );
-
-            return $userInfo;
-        }
-
-        /**
          * Returns an array of BlogInfo objects with the information of all the blogs to which
          * a user belongs
          *
@@ -195,43 +111,7 @@
          */
         function getUsersBlogs( $userid, $status = BLOG_STATUS_ALL )
         {
-            $usersBlogs = Array();
-            $blogs = new Blogs();
-
-            // check if the user is the owner of any blog
-            $owner = "SELECT * FROM ".$this->getPrefix()."blogs WHERE owner_id = ".$userid;
-			
-			if( $status != BLOG_STATUS_ALL ) 
-				$owner .= " AND status = '".Db::qstr( $status )."'";
-            
-			$result = $this->Execute( $owner );
-
-            // return an empty array if the user is assigned to no blog
-            if( !$result )
-                return Array();
-
-            while( $row = $result->FetchRow($result)) {
-                $blogId = $row["id"];
-                $blogInfo = $blogs->getBlogInfo( $blogId );
-                //array_push( $usersBlogs, $blogId );
-                array_push( $usersBlogs, $blogInfo );
-            }
-
-            // and now check to which other blogs he or she belongs
-            $otherBlogs = "SELECT * FROM ".$this->getPrefix()."users_permissions WHERE user_id = ".$userid.";";
-            $result = $this->Execute( $otherBlogs );
-            // now we know to which he or she belongs, so we only have
-            // to load the information about those blogs
-            while( $row = $result->FetchRow($result)) {
-                $blogId = $row["blog_id"];
-                if( $blogId > 0 ) {
-                    $blogInfo = $blogs->getBlogInfo( $blogId );
-                    //array_push( $usersBlogs, $blogId );
-                    array_push( $usersBlogs, $blogInfo );
-                }
-            }
-
-            return $usersBlogs;
+	        return( $this->_provider->getUsersBlogs( $userid, $status ));
         }
 
         /**
@@ -239,43 +119,13 @@
          *
 		 * @param status
 		 * @param includeExtraInfo
-		 * @param searchTerms
          * @param page
          * @param itemsPerPage
          * @return An array containing all the users.
          */
-        function getAllUsers( $status = USER_STATUS_ALL, $includeExtraInfo = false, $searchTerms = "", $page = -1, $itemsPerPage = DEFAULT_ITEMS_PER_PAGE )
-        {   
-	    	if( $status != USER_STATUS_ALL )
-	    		$whereStatus = "status = '".Db::qstr($status)."'";
-	    	if( $searchTerms != "" )
-	    	    $whereSearch = $this->buildSearchCondition( $searchTerms );
-	    	    
-	    	$where = $this->buildWhereCondition( Array( $whereStatus, $whereSearch ));
-	        
-            $query = "SELECT * FROM ".$this->getPrefix()."users $where ORDER BY id ASC";
-
-            $result = $this->Execute( $query, $page, $itemsPerPage );
-
-            $users = Array();
-
-            while ($info = $result->FetchRow( $result ))
-                array_push( $users, $this->_fillUserInformation( $info, $includeExtraInfo ));
-
-            return $users;
-        }
-        
-        /**
-         * @see Model::buildSearchCondition
-         */
-        function buildSearchCondition( $searchTerms )
+        function getAllUsers( $status = USER_STATUS_ALL, $includeExtraInfo = false, $page = -1, $itemsPerPage = DEFAULT_ITEMS_PER_PAGE )
         {
-            $searchTerms = trim( $searchTerms );
-            $searchCond = "(user LIKE '%".Db::qstr($searchTerms)."%' 
-                           OR full_name LIKE '%".Db::qstr($searchTerms)."%' OR 
-                           email LIKE '%".Db::qstr($searchTerms)."%')";
-            
-            return( $searchCond );
+	        return( $this->_provider->getAllUsers( $status, $includeExtraInfo, $page, $itemsPerPage ));	  
         }
 
         /**
@@ -287,27 +137,7 @@
          */
         function updateUser( $userInfo )
         {
-            $query = "UPDATE ".$this->getPrefix().
-                     "users SET email = '".$userInfo->getEmail().
-                     "', about = '".Db::qstr($userInfo->getAboutMyself()).
-                     "', password = '".$userInfo->getPassword().
-                     "', full_name = '".Db::qstr($userInfo->getFullName()).
-                     "', resource_picture_id = '".Db::qstr($userInfo->getPictureId()).
-                     "', properties = '".Db::qstr(serialize($userInfo->getProperties())).
-                     "', status = '".Db::qstr($userInfo->getStatus()).
-                     "' WHERE id = ".$userInfo->getId().";";
-
-            // update the users table
-            $result = $this->Execute( $query );
-
-            // and now update the permissions, if there has been any change
-            $perms = new UserPermissions();
-            $perms->updateSiteAdmin( $userInfo->getId(), $userInfo->isSiteAdmin());
-
-            $this->_cache->removeData( $userInfo->getId(), CACHE_USERINFO );
-            $this->_cache->removeData( $userInfo->getUsername(), CACHE_USERIDBYNAME );
-
-            return $result;
+	        return( $this->_provider->updateUser( $userInfo ));
         }
 
         /**
@@ -319,25 +149,7 @@
          */
         function addUser( &$user )
         {
-            $query = "INSERT INTO ".$this->getPrefix()."users(user,password,email,about,full_name,
-                      resource_picture_id,properties,status)
-                      VALUES ('".Db::qstr($user->getUserName())."','".md5($user->getPassword())."','".
-                      Db::qstr($user->getEmail())."','".Db::qstr($user->getAboutMyself())."','".
-                      Db::qstr($user->getFullName())."', '".
-                      Db::qstr($user->getPictureId())."', '".
-                      Db::qstr(serialize($user->getProperties()))."', '".
-                      Db::qstr($user->getStatus())."');";
-
-            $result = $this->Execute( $query );
-
-            if( !$result )
-                return false;
-
-            $userId = $this->_db->Insert_ID();
-			
-			$user->setId( $userId );
-
-            return $userId;
+	        return( $this->_provider->addUser( $user ));
         }
 
         /**
@@ -346,42 +158,12 @@
          *
          * @param blogId The blog identifier.
          * @param includeOwner Wether to include the owner of the blog or not.
-         * @param status
-         * @param searchTerms
          * @return An array with the information about the users who belong in
          * one way or another to that blog.
          */
-        function getBlogUsers( $blogId, $includeOwner = true, $status = USER_STATUS_ALL, $searchTerms = "" )
+        function getBlogUsers( $blogId, $includeOwner = true, $status = USER_STATUS_ALL )
         {
-            $users = Array();
-	        $prefix = $this->getPrefix();
-
-            // get the information about the owner, if requested so
-            if( $includeOwner ) {
-                $query = "SELECT {$prefix}users.* FROM {$prefix}users, {$prefix}blogs 
-                          WHERE {$prefix}users.id = {$prefix}blogs.owner_id AND {$prefix}blogs.id = '".Db::qstr($blogId)."';";
-                $result = $this->Execute( $query );
-
-                if( !$result )
-                    return false;
-
-                $row = $result->FetchRow();
-                array_push( $users, $this->_fillUserInformation( $row ));
-            }
-
-            // now get the other users who have permission for that blog.
-            $query2 = "SELECT {$prefix}users.* FROM {$prefix}users, {$prefix}users_permissions 
-                       WHERE {$prefix}users.id = {$prefix}users_permissions.user_id 
-                       AND {$prefix}users_permissions.blog_id = '".Db::qstr($blogId)."';";
-            $result2 = $this->Execute( $query2 );
-            if( !$result2 ) // if error, return what we have so far...
-                return $users;
-
-            while( $row = $result2->FetchRow()) {
-                array_push( $users, $this->_fillUserInformation($row));
-            }
-
-            return $users;
+			return( $this->_provider->getBlogUsers( $blogId, $includeOwner, $status ));
         }
 
         /**
@@ -391,19 +173,7 @@
          */
         function disableUser( $userId )
         {
-            $query = "UPDATE ".$this->getPrefix()."users 
-                      SET status = '".USER_STATUS_DISABLED."'
-                      WHERE id = '".Db::qstr($userId)."'";
-
-            $result = $this->Execute( $query );
-
-            if( !$result )
-                return false;
-
-            if( $this->_db->Affected_Rows() == 0 )
-                return false;
-
-            return true;
+	        return( $this->_provider->disableUser( $userId ));
         }
         
         /**
@@ -413,135 +183,17 @@
          */
         function deleteUser( $userId )
         {
-            // first, delete all of his/her permissions
-            $perms = new UserPermissions();
-            $perms->revokeUserPermissions( $userId );
+	        return( $this->_provider->deleteUser( $userId ));
+        }        
 
-            $query = "DELETE FROM ".$this->getPrefix()."users WHERE id = $userId;";
-
-            $result = $this->Execute( $query );
-
-            if( !$result )
-                return false;
-
-            if( $this->_db->Affected_Rows() == 0 )
-                return false;
-
-            $this->_cache->removeData( $userId, CACHE_USERINFO );
-
-            return true;
-        }  
-        
         /**
-         * removes all users that have 'deleted' status
-         *
-         * @return Returns true if all users were deleted or false otherwise.
-         */        
-        function purgeUsers()
-        {
-            // we first need to delete all resources/album belong to the blog, 
-            // we don't need to care if the ablum has sub album, resource, we
-            // just delete all albums, resources that belong the blog which is
-            // being deleted as a result of deleting the blog's owner
-            
-            // first delete all resources
-            $galleryresources = new GalleryResources();
-            $r_query = "SELECT gr.id AS id, gr.owner_id as owner_id 
-            					  FROM ".$this->getPrefix()."gallery_resources AS gr, ".$this->getPrefix()."blogs AS b, ".$this->getPrefix()."users AS u
-            					  WHERE gr.owner_id=b.id AND b.owner_id=u.id AND u.status = 2";            					 
-            				
-            $r_result = $this->Execute( $r_query );     
-            if( !$r_result )
-                return false;
-                
-            while( $r_row = $r_result->FetchRow()) {
-            	$galleryresources->deleteResource( $r_row["id"], $r_row["owner_id"] );
-            }
-            
-            // now delete album
-            $galleryalbums = new GalleryAlbums();
-            $al_query = "SELECT ga.id AS id, ga.owner_id as owner_id 
-            					  FROM ".$this->getPrefix()."gallery_albums AS ga, ".$this->getPrefix()."blogs AS b, ".$this->getPrefix()."users AS u
-            					  WHERE ga.owner_id=b.id AND b.owner_id=u.id AND u.status = 2";            				   
-            
-            $al_result = $this->Execute( $al_query );     
-            if( !$al_result )
-                return false;
-                
-            while( $al_row = $al_result->FetchRow()) {
-            	$galleryalbums->deleteAlbum( $al_row["id"], $al_row["owner_id"] );
-            }
-                        
-            // check if the deleted user owns any blog, if they does, delete the blog
-            // the deleteBlog function will take care of deleting articles etc...            
-            $blogs = new Blogs();             
-            $userfolder = new GalleryResourceStorage();                        
-            
-            $b_query = "SELECT b.id 
-            					  FROM ".$this->getPrefix()."blogs AS b, ".$this->getPrefix()."users AS u 
-            					  WHERE b.owner_id=u.id AND u.status = 2";            					
-            				
-            $b_result = $this->Execute( $b_query );     
-            if( !$b_result )
-                return false;
-                
-            while( $b_row = $b_result->FetchRow()) {
-            	$blogs->deleteBlog( $b_row["id"] );            	
-            	// since the deleteResource doesn't take care of deleting all the user folder
-            	// and now that we have the blog id here, we can use the deleteDir function
-            	// to delete all uder folders and sub folders :)
-            	$filePath = $userfolder->getUserFolder( $b_row["id"] );  
-            	File::deleteDir( $filePath, true, false );
-            }            
-            
-            // now we need to check if user belong to any other blog and have made any posts
-            // at the moment, we cannot dertermine which resource was posted by which user
-            // so when user is deleted, resource posted by him/her will stay intact in the blog
-            // maybe we should notify the blog owner that his member was deleted?						
-						$articles = new Articles();
-						
-						$a_query = "SELECT a.id AS id, a.user_id AS user_id, a.blog_id AS blog_id
-											  FROM ".$this->getPrefix()."articles AS a, ".$this->getPrefix()."users AS u 
-					 						  WHERE a.user_id=u.id AND u.status = 2";   
-            $a_result = $this->Execute( $a_query );
-            
-            if( !$a_result )
-                return false;
-                
-            while( $a_row = $a_result->FetchRow()) {
-            	$articles->deleteArticle( $a_row["id"], $a_row["user_id"], $a_row["blog_id"], true );          	
-            }            
-                                    
-            // finally remove the user                                 
-            $u_query = "SELECT * FROM ".$this->getPrefix()."users WHERE status = 2";
-
-            $u_result = $this->Execute( $u_query );
-            if( !$u_result )
-                return false;
-            
-            while( $u_row = $u_result->FetchRow()) {
-                $this->deleteUser( $u_row["id"] );
-            }            
-                                   
-            return true;
-    	}
-
-        /**
          * returns the total number of users
          *
          * @return total number of users
          */
-        function getNumUsers( $status = USER_STATUS_ALL, $searchTerms = "" )
+        function getNumUsers( $status = USER_STATUS_ALL )
         {
-            $table = $this->getPrefix()."users";
-			if( $status != USER_STATUS_ALL )
-				$whereStatus = "status = '".Db::qstr($status)."'";
-			if( $searchTerms != "" )
-			    $whereSearch = $this->buildSearchCondition( $searchTerms );
-			    
-			$where = $this->buildWhereCondition( Array( $whereStatus, $whereSearch ), false );
-				
-			return( $this->getNumItems( $table, $where ));
+	        return( $this->_provider->getNumUsers( $status ));
         }
         
         /**
@@ -552,7 +204,7 @@
          */
         function userExists( $userName )
         {
-	    	return( $this->getUserInfoFromUsername( $userName ));   
+	    	return( $this->_provider->getUserInfoFromUsername( $userName ));   
         }
 
         /**
@@ -560,48 +212,16 @@
          */
 		function getUserBlogId( $username )
 		{
-            // default blog id
-            $blogId = 1;
-
-			$usersBlogs = Array();
-            $blogs = new Blogs();
-
-            $userinfo = $this->getUserInfoFromUsername($username);
-            // if userinfo is null, this maybe because username is not exists..
-            // return 0 means, should go to summary page
-            if(!$userinfo) return 0;
-            $userid = $userinfo->getId();
-            $userid = $userinfo->getId();
-
-        	// check if the user is the owner of any blog
-            $owner = "SELECT id FROM ".$this->getPrefix()."blogs WHERE owner_id = ".$userid.";";
-            $result = $this->Execute( $owner );
-
-            if(!$result)
-                return $blogId;
-
-            while( $row = $result->FetchRow($result)) {
-            	$blogId = $row["id"];
-            }
-
-            return $blogId;
+			return( $this->_provider->getUserBlogId( $username ));
 		}
 
         /**
          * check if the email account has been registered
          * @return true if the email account has been registered
          */
-        function emailExists($email){
-            $query = "SELECT email 
-                      FROM ".$this->getPrefix()."users 
-                      WHERE email = '".Db::qstr($email)."'";
-
-            $result = $this->Execute($query);
-
-            if($result && $result->RecordCount() >= 1)
-                return true;
-            else 
-                return false;
+        function emailExists($email)
+        {
+	        return( $this->_provider->emailExists( $email ));
         }
     }
-?>
\ No newline at end of file
+?>

Added: plog/trunk/config/userdata.properties.php
===================================================================
--- plog/trunk/config/userdata.properties.php	2005-06-06 21:28:28 UTC (rev 2176)
+++ plog/trunk/config/userdata.properties.php	2005-06-06 21:37:24 UTC (rev 2177)
@@ -0,0 +1,21 @@
+<?php
+
+#
+# Default user data provider, plog's own one
+#
+$config = Array( 
+  "provider" => "PlogUserDataProvider",
+);
+
+#
+# PHPBB2 user data provider -- Not implemented yet
+#
+/*$config = Array( 
+  "provider" => "PhpBB2UserDataProvider",
+  "createBlogIfNotExisting" => true,
+  "database" => "phpbb2",
+  "user" => "xxx",
+  "password" => "yyy",
+  "prefix" => "phpbb_"
+);*/
+?>


Property changes on: plog/trunk/config/userdata.properties.php
___________________________________________________________________
Name: svn:executable
   + *




More information about the pLog-svn mailing list