NewsNewsFeaturesDownloadsDevelopmentSupportForumDocumentsAbout Us

users.class.php

查看本檔案說明文件.
00001 <?php
00002 
00003     include_once( PLOG_CLASS_PATH."class/dao/model.class.php" );
00004     include_once( PLOG_CLASS_PATH."class/dao/userinfo.class.php" );
00005     include_once( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" );
00006     include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
00007     include_once( PLOG_CLASS_PATH."class/dao/userstatus.class.php" );
00008     
00015     class Users extends Model
00016     {
00017 
00021         function Users()
00022         {
00023             $this->Model();
00024 
00025             $this->usercache = Array();
00026 
00027             $this->perms =  new UserPermissions();
00028         }
00029 
00038         function authenticateUser( $user, $pass )
00039         {
00040             $query = "SELECT * FROM ".$this->getPrefix()."users 
00041                       WHERE user = '".Db::qstr($user)."' AND password = '".md5($pass)."'
00042                             AND status = '".USER_STATUS_ACTIVE."'";
00043 
00044             $result = $this->Execute( $query );
00045 
00046             if( $result == false )
00047                 return false;
00048 
00049             if( $result->RecordCount() == 1 )
00050                 return true;
00051             else
00052                 return false;
00053         }
00054 
00062         function getUserInfo( $user, $pass )
00063         {
00064             $prefix = $this->getPrefix();
00065             $query = "SELECT u.id AS id, u.user AS user, u.password AS password, u.email AS email,
00066                       u.about AS about, u.full_name AS full_name, u.properties AS properties,
00067                       u.resource_picture_id AS resource_picture_id,
00068                       IF(p.permission_id = 1, 1, 0 ) AS site_admin,
00069                       u.status AS status
00070                       FROM {$prefix}users u LEFT JOIN {$prefix}users_permissions p ON u.id = p.user_id
00071                       WHERE u.user = '".Db::qstr($user)."' AND u.password = '".md5($pass)."'
00072                       ORDER BY blog_id";
00073 
00074             $userInfo = $this->_getUserInfoFromQuery( $query );
00075 
00076             return $userInfo;
00077         }
00078 
00085         function getUserInfoFromUsername( $username )
00086         {
00087             $prefix = $this->getPrefix();
00088             $query = "SELECT u.id AS id, u.user AS user, u.password AS password, u.email AS email,
00089                              u.about AS about, u.full_name AS full_name, u.properties AS properties,
00090                              u.resource_picture_id AS resource_picture_id,
00091                              IF(p.permission_id = 1, 1, 0 ) AS site_admin,
00092                              u.status AS status
00093                       FROM {$prefix}users u LEFT JOIN {$prefix}users_permissions p ON u.id = p.user_id
00094                       WHERE u.user = '".Db::qstr($username)."' ORDER BY blog_id";
00095 
00096             $userInfo = $this->_getUserInfoFromQuery( $query );
00097 
00098             return $userInfo;
00099         }
00100 
00107         function getUserInfoFromId( $userid, $extendedInfo = false )
00108         {
00109             if( isset($this->usercache[$userid])) {
00110                 $userInfo = $this->usercache[$userid];
00111             }
00112             else {
00113                 $prefix = $this->getPrefix();
00114                 $query = "SELECT u.id AS id, u.user AS user, u.password AS password, u.email AS email,
00115                                  u.about AS about, u.full_name AS full_name, u.properties AS properties,
00116                                  u.resource_picture_id AS resource_picture_id,
00117                                  IF(p.permission_id = 1, 1, 0 ) AS site_admin,
00118                                  u.status AS status
00119                           FROM {$prefix}users u LEFT JOIN {$prefix}users_permissions p ON u.id = p.user_id
00120                           WHERE u.id = $userid ORDER BY blog_id";
00121 
00122                 $userInfo = $this->_getUserInfoFromQuery( $query, $extendedInfo );
00123 
00124                 $this->usercache[$userid] = $userInfo;
00125             }
00126 
00127             return $userInfo;
00128         }
00129 
00136         function _getUserInfoFromQuery( $sql_query, $extendedInfo = false )
00137         {
00138             $result = $this->Execute( $sql_query );
00139             if( !$result )
00140                 return false;
00141 
00142             if( $result->RowCount() == 0 )
00143                 return false;
00144 
00145             $info = $result->FetchRow( $result );
00146 
00147             $userInfo = $this->_fillUserInformation( $info, $extendedInfo );
00148 
00149             return $userInfo;
00150         }
00151 
00157         function _fillUserInformation( $query_result, $extraInfo = false )
00158         {
00159             $userInfo = new UserInfo( $query_result["user"], $query_result["password"],
00160                                       $query_result["email"],
00161                                       $query_result["about"],
00162                                       $query_result["full_name"],
00163                                       $query_result["resource_picture_id"],
00164                                       unserialize($query_result["properties"]),
00165                                       $query_result["id"]);
00166 
00167             if( $extraInfo ) {
00168                 // load this data if explicitely required!
00169                 $userBlogs = $this->getUsersBlogs($userInfo->getId(), BLOG_STATUS_ACTIVE);
00170                 $userInfo->setBlogs($userBlogs);
00171             }
00172 
00173             // set some permissions
00174             //$userInfo->setSiteAdmin($this->perms->isSiteAdmin( $userInfo->getId()));
00175             $userInfo->setSiteAdmin( $query_result["site_admin"] );
00176             $userInfo->setStatus( $query_result["status"] );
00177 
00178             return $userInfo;
00179         }
00180 
00188         function getUsersBlogs( $userid, $status = BLOG_STATUS_ALL )
00189         {
00190             $usersBlogs = Array();
00191             $blogs = new Blogs();
00192             $ids = Array();
00193 
00194             // check if the user is the owner of any blog
00195             $prefix = $this->getPrefix();
00196             $owner = "SELECT * FROM {$prefix}blogs WHERE owner_id = ".$userid;          
00197             if( $status != BLOG_STATUS_ALL ) 
00198                 $owner .= " AND status = '".Db::qstr( $status )."'";
00199             
00200             $result = $this->Execute( $owner );
00201 
00202             while( $row = $result->FetchRow($result)) {
00203                 $usersBlogs[] = $blogs->_fillBlogInformation( $row );
00204                 $ids[] = $row["id"];
00205             }
00206 
00207             // and now check to which other blogs he or she belongs
00208             $otherBlogs = "SELECT b.* FROM {$prefix}blogs b, {$prefix}users_permissions p 
00209                            WHERE p.user_id = '".Db::qstr($userid)."' AND b.id = p.blog_id";
00210             if( !empty($usersBlogs)) {
00211                 $blogIds = implode( ",", $ids );
00212                 $otherBlogs .= " AND p.blog_id NOT IN (".$blogIds.")";
00213             }
00214             if( $status != BLOG_STATUS_ALL )
00215                 $otherBlogs .= " AND b.status = '".Db::qstr( $status )."'";
00216                 
00217             $result = $this->Execute( $otherBlogs );
00218             // now we know to which he or she belongs, so we only have
00219             // to load the information about those blogs
00220             while( $row = $result->FetchRow($result)) {
00221                 $usersBlogs[] = $blogs->_fillBlogInformation( $row );
00222             }
00223 
00224             return $usersBlogs;
00225         }
00226 
00236         function getAllUsers( $status = USER_STATUS_ALL, $includeExtraInfo = false, $page = -1, $itemsPerPage = DEFAULT_ITEMS_PER_PAGE )
00237         {
00238             if( $status != USER_STATUS_ALL )
00239                 $where = "WHERE status = '".Db::qstr($status)."'";
00240             
00241             $query = "SELECT * FROM ".$this->getPrefix()."users $where ORDER BY id ASC $limits";
00242 
00243             $result = $this->Execute( $query, $page, $itemsPerPage );
00244 
00245             $users = Array();
00246 
00247             while ($info = $result->FetchRow( $result ))
00248                 array_push( $users, $this->_fillUserInformation( $info, $includeExtraInfo ));
00249 
00250             return $users;
00251         }
00252 
00260         function updateUser( $userInfo )
00261         {
00262             $query = "UPDATE ".$this->getPrefix().
00263                      "users SET email = '".$userInfo->getEmail().
00264                      "', about = '".Db::qstr($userInfo->getAboutMyself()).
00265                      "', password = '".$userInfo->getPassword().
00266                      "', full_name = '".Db::qstr($userInfo->getFullName()).
00267                      "', resource_picture_id = '".Db::qstr($userInfo->getPictureId()).
00268                      "', properties = '".Db::qstr(serialize($userInfo->getProperties())).
00269                      "', status = '".Db::qstr($userInfo->getStatus()).
00270                      "' WHERE id = ".$userInfo->getId().";";
00271 
00272             // update the users table
00273             $result = $this->Execute( $query );
00274 
00275             // and now update the permissions, if there has been any change
00276             $perms = new UserPermissions();
00277             $perms->updateSiteAdmin( $userInfo->getId(), $userInfo->isSiteAdmin());
00278 
00279             return $result;
00280         }
00281 
00289         function addUser( &$user )
00290         {
00291             $query = "INSERT INTO ".$this->getPrefix()."users(user,password,email,about,full_name,
00292                       resource_picture_id,properties,status)
00293                       VALUES ('".Db::qstr($user->getUserName())."','".md5($user->getPassword())."','".
00294                       Db::qstr($user->getEmail())."','".Db::qstr($user->getAboutMyself())."','".
00295                       Db::qstr($user->getFullName())."', '".
00296                       Db::qstr($user->getPictureId())."', '".
00297                       Db::qstr(serialize($user->getProperties()))."', '".
00298                       Db::qstr($user->getStatus())."');";
00299 
00300             $result = $this->Execute( $query );
00301 
00302             if( !$result )
00303                 return false;
00304 
00305             $userId = $this->_db->Insert_ID();
00306             
00307             $user->setId( $userId );
00308 
00309             return $userId;
00310         }
00311 
00321         function getBlogUsers( $blogId, $includeOwner = true, $status = USER_STATUS_ALL )
00322         {
00323             $users = Array();
00324             $prefix = $this->getPrefix();
00325 
00326             // get the information about the owner, if requested so
00327             if( $includeOwner ) {
00328                 $query = "SELECT {$prefix}users.* FROM {$prefix}users, {$prefix}blogs 
00329                           WHERE {$prefix}users.id = {$prefix}blogs.owner_id AND {$prefix}blogs.id = '".Db::qstr($blogId)."';";
00330                 $result = $this->Execute( $query );
00331 
00332                 if( !$result )
00333                     return false;
00334 
00335                 $row = $result->FetchRow();
00336                 array_push( $users, $this->_fillUserInformation( $row ));
00337             }
00338 
00339             // now get the other users who have permission for that blog.
00340             $query2 = "SELECT {$prefix}users.* FROM {$prefix}users, {$prefix}users_permissions 
00341                        WHERE {$prefix}users.id = {$prefix}users_permissions.user_id 
00342                        AND {$prefix}users_permissions.blog_id = '".Db::qstr($blogId)."';";
00343             $result2 = $this->Execute( $query2 );
00344             if( !$result2 ) // if error, return what we have so far...
00345                 return $users;
00346 
00347             while( $row = $result2->FetchRow()) {
00348                 array_push( $users, $this->_fillUserInformation($row));
00349             }
00350 
00351             return $users;
00352         }
00353 
00359         function disableUser( $userId )
00360         {
00361             $query = "UPDATE ".$this->getPrefix()."users 
00362                       SET status = '".USER_STATUS_DISABLED."'
00363                       WHERE id = '".Db::qstr($userId)."'";
00364 
00365             $result = $this->Execute( $query );
00366 
00367             if( !$result )
00368                 return false;
00369 
00370             if( $this->_db->Affected_Rows() == 0 )
00371                 return false;
00372 
00373             return true;
00374         }
00375         
00381         function deleteUser( $userId )
00382         {
00383             // first, delete all of his/her permissions
00384             $perms = new UserPermissions();
00385             $perms->revokeUserPermissions( $userId );
00386 
00387             $query = "DELETE FROM ".$this->getPrefix()."users WHERE id = $userId;";
00388 
00389             $result = $this->Execute( $query );
00390 
00391             if( !$result )
00392                 return false;
00393 
00394             if( $this->_db->Affected_Rows() == 0 )
00395                 return false;
00396 
00397             return true;
00398         }        
00399 
00405         function getNumUsers( $status = USER_STATUS_ALL )
00406         {
00407             $prefix = $this->getPrefix();
00408             $table = "{$prefix}users";
00409             if( $status != USER_STATUS_ALL )
00410                 $cond = "status = '".Db::qstr($status)."'";
00411                 
00412             return( $this->getNumItems( $table, $cond ));
00413         }
00414         
00421         function userExists( $userName )
00422         {
00423             return( $this->getUserInfoFromUsername( $userName ));   
00424         }
00425 
00429         function getUserBlogId( $username )
00430         {
00431             // default blog id
00432             $blogId = 1;
00433 
00434             $usersBlogs = Array();
00435             $blogs = new Blogs();
00436 
00437             $userinfo = $this->getUserInfoFromUsername($username);
00438             // if userinfo is null, this maybe because username is not exists..
00439             // return 0 means, should go to summary page
00440             if(!$userinfo) return 0;
00441             $userid = $userinfo->getId();
00442             $userid = $userinfo->getId();
00443 
00444             // check if the user is the owner of any blog
00445             $owner = "SELECT id FROM ".$this->getPrefix()."blogs WHERE owner_id = ".$userid.";";
00446             $result = $this->_db->Execute( $owner );
00447 
00448             if(!$result)
00449                 return $blogId;
00450 
00451             while( $row = $result->FetchRow($result)) {
00452                 $blogId = $row["id"];
00453             }
00454 
00455             return $blogId;
00456         }
00457 
00462         function emailExists($email){
00463             $query = "SELECT email 
00464                       FROM ".$this->getPrefix()."users 
00465                       WHERE email = '".Db::qstr($email)."'";
00466 
00467             $result = $this->_db->Execute($query);
00468 
00469             if($result && $result->RecordCount() >= 1)
00470                 return true;
00471             else 
00472                 return false;
00473         }
00474     }
00475 ?>