[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/dao/userdata/ -> simplepostnukeuserdataprovider.class.php (source)

   1  <?php
   2  
   3      lt_include( PLOG_CLASS_PATH."class/dao/userdata/baseuserdataprovider.class.php" );
   4      lt_include( PLOG_CLASS_PATH."class/database/db.class.php" );
   5      lt_include( PLOG_CLASS_PATH."class/dao/userinfo.class.php" );
   6      lt_include( PLOG_CLASS_PATH."class/dao/userstatus.class.php" );
   7      
   8      /**
   9      * Model representing the users in our application. Provides the methods such as
  10      * authentication and querying for users.
  11      *
  12      * \ingroup User_Data_Providers
  13      */
  14      class SimplePostNukeUserDataProvider extends BaseUserDataProvider
  15      {
  16          var $_dbc;
  17          var $_postnukedbprefix;
  18          var $_blogtitle_postfix;
  19          /**
  20          * Initializes the model
  21          */
  22          function SimplePostNukeUserDataProvider( $providerConfig )
  23          {
  24              $this->BaseUserDataProvider( $providerConfig );
  25              $this->table = $this->getPrefix()."users";
  26              
  27              // initialize the database connection based on our parameters
  28              $config = $this->getProviderConfiguration();
  29              $user = $config->getValue( "user" );
  30              $pass = $config->getValue( "password" );
  31              $host = $config->getValue( "host" );
  32              $db = $config->getValue( "database" );
  33              $this->_postnukedbprefix = $config->getValue( "prefix" );
  34              $this->_dbc =& Db::getNewDb( $host, $user, $pass, $db );
  35              
  36              $this->_blogtitle_postfix = $config->getValue( "blogtitle_postfix" );                   
  37          }
  38  
  39          /**
  40          * Returns true if the user is in the database and the username
  41          * and password match
  42          *
  43          * First, we check if the user exists as a standard lt user. If not, we check if he
  44          * has an PostNuke account, validate username/password and open a lt account for him.
  45          * This is the only time we interact with the postnuke db. Password changes, user removal and 
  46          * and everything else possible within LifeType does not affect the PostNuke database in any way.
  47          *
  48          * @param username Username of the user who we'd like to authenticate
  49          * @param pass Password of the user
  50          * @return true if user and password correct or false otherwise.
  51          */
  52          function authenticateUser( $username, $pass )
  53          {
  54              // Check if we find the user in the LifeType DB
  55              $user = $this->getUserInfoFromUsername( $username );            
  56              if( $user ) {
  57                  return( $user->getPassword() == md5($pass));
  58              }
  59              
  60              // Check if the user is available in the PostNuke database...
  61              else {
  62                  $query = "SELECT * FROM ".$this->_postnukedbprefix."users WHERE pn_uname = '".Db::qstr( $username )."' AND pn_pass = '".md5( $pass )."'";
  63                  $result = $this->_dbc->Execute( $query );
  64                  
  65                  if( (!$result) || ($result == false) ) {          
  66                      return false;
  67                  }
  68                  // let's add the user to the lt userbase
  69                  elseif ( $result->RecordCount() == 1 ) {
  70                      $result->Close(); 
  71                      $pnUserdata = $this->getUserInfoFromPostNukeUser( $username );
  72  
  73                      $user = new UserInfo( $pnUserdata["pn_uname"], 
  74                                            $pnUserdata["pn_pass"], 
  75                                            $pnUserdata["pn_email"], 
  76                                            "", 
  77                                            $pnUserdata["pn_name"],
  78                                            0,
  79                                            serialize(Array())
  80                                          );
  81                                    
  82                      $user->setStatus( USER_STATUS_ACTIVE );
  83                      
  84                      
  85                      $newUserId = $this->addUser( $user );
  86                      if( !$newUserId ) {
  87                          return false;                  
  88                      }
  89  
  90                      //add Blog
  91                      $this->_PostNukeAddBlog($username, $newUserId);
  92  
  93                      // assign the login_perm permission
  94                      $this->grantLoginPermission( $user );
  95  
  96                      return true;
  97                  }
  98                  else{
  99                          // TODO: shouldn't ever happen?
 100                      $result->Close();
 101                  }
 102                                          
 103                  
 104                  // return false if user authentication failed on both databases
 105                  return false;
 106              }
 107          } // authenticateUser
 108  
 109          /**
 110          *
 111          * @param username Username of the user who we'd like to get all info from the PN DB
 112          * @return Returns an array with all userinformation
 113          */
 114          function getUserInfoFromPostNukeUser( $username )
 115          {
 116              $query = "SELECT * FROM ".$this->_postnukedbprefix."users WHERE pn_uname = '".Db::qstr( $username )."'";
 117                        
 118              $result = $this->_dbc->Execute( $query );
 119              
 120              if( !$result )
 121                  return false;
 122                  
 123              $row = $result->FetchRow();
 124              $result->Close(); 
 125  
 126              return( $row );            
 127          }
 128  
 129          /**
 130          *
 131          * @param username Username for having a meaningful Blogname
 132          * @param userid UserID to link the blog to the new created user
 133          * @return Returns true if blog is created successfully and false otherwise
 134          */
 135          function _PostNukeAddBlog( &$username, &$userid )
 136          {
 137              lt_include( PLOG_CLASS_PATH."class/dao/bloginfo.class.php" );            
 138              lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
 139              lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" );
 140              lt_include( PLOG_CLASS_PATH."class/dao/articlecategories.class.php" );
 141              
 142              $config =& Config::getConfig();
 143              $locale =& Locales::getLocale( $config->getValue( "default_locale" ));
 144              
 145              // create a new blog
 146              $blogs = new Blogs();
 147              $blog = new BlogInfo( $username.$this->_blogtitle_postfix,  // name of the new blog
 148                                       $userid,  // id of the owner
 149                                       "",  // no about
 150                                    Array()); // no properties either
 151              $newBlogId = $blogs->addBlog( $blog );
 152                                
 153              // add a default category and a default post            
 154              $articleCategories = new ArticleCategories();
 155              $articleCategory = new ArticleCategory( $locale->tr( "register_default_category" ), "", $newBlogId, true );
 156              $catId = $articleCategories->addArticleCategory( $articleCategory );
 157  
 158              $articleTopic = $locale->tr( "register_default_article_topic" );
 159              $articleText  = $locale->tr( "register_default_article_text" );
 160              $article = new Article( $articleTopic, 
 161                                      $articleText, 
 162                                      Array( $catId ), 
 163                                      $userid, 
 164                                      $newBlogId, 
 165                                      POST_STATUS_PUBLISHED, 
 166                                      0, 
 167                                      Array(), 
 168                                      "welcome" ); // slug
 169              $t = new Timestamp();
 170              $article->setDateObject( $t );
 171              $article->setInSummary( false );
 172              $articles = new Articles();
 173              $articles->addArticle( $article );
 174          }
 175  
 176          //------------
 177          //  NOTE: Everything below is copy&paste from LifeTypeUserdataprovider.class.php
 178          //------------
 179  
 180          /**
 181          * Retrieves the user information but given only a username
 182          *
 183          * @param username The username of the user
 184          * @return Returns a UserInfo object with the requested information, or false otherwise.
 185          */
 186          function getUserInfoFromUsername( $username )
 187          {
 188              return( $this->get( "user", $username, CACHE_USERIDBYNAME, Array( CACHE_USERINFO => "getId" )));            
 189          }
 190  
 191          
 192          /**
 193          * Retrieves the user infromation but given only a userid
 194          *
 195          * @param userId User ID of the user from whom we'd like to get the information
 196          * @return Returns a UserInfo object with the requested information, or false otherwise.
 197          */
 198          function getUserInfoFromId( $userid )
 199          {
 200              return( $this->get( "id", $userid, CACHE_USERINFO, Array( CACHE_USERIDBYNAME => "getUsername" )));
 201          }
 202  
 203          /**
 204          * Returns an array with all the users available in the database
 205          *
 206          * @param status
 207          * @param includeExtraInfo
 208          * @param searchTerms
 209          * @param page
 210          * @param itemsPerPage
 211          * @return An array containing all the users.
 212          */
 213          function getAllUsers( $status = USER_STATUS_ALL, 
 214                                $searchTerms = "",
 215                                $orderBy = "",
 216                                $page = DEFAULT_PAGING_ENABLED, 
 217                                $itemsPerPage = DEFAULT_ITEMS_PER_PAGE )
 218          {           
 219              $where = "";
 220              
 221              if( $status != USER_STATUS_ALL )
 222                  $where = "status = '".Db::qstr($status)."'";
 223  
 224              if( $searchTerms != "" ) {
 225                  if( $where != "" )
 226                      $where .= " AND ";
 227                  $where = $this->getSearchConditions( $searchTerms );
 228              }
 229              if( $where != "" )
 230                  $where = "WHERE $where";
 231              
 232              $query = "SELECT * FROM ".$this->getPrefix()."users $where ORDER BY id ASC";
 233              $result = $this->Execute( $query, $page, $itemsPerPage );
 234              
 235              $users = Array();            
 236              
 237              if( !$result )
 238                  return $users;
 239  
 240              while ($row = $result->FetchRow()) {
 241                  $user = $this->mapRow( $row );
 242                  $users[] = $user;
 243                  // cache the data for later use
 244                  $this->_cache->setData( $user->getId(), CACHE_USERINFO, $user );
 245                  $this->_cache->setData( $user->getUsername(), CACHE_USERIDBYNAME, $user );
 246              }
 247              $result->Close();
 248  
 249              return $users;
 250          }
 251          
 252          /**
 253          * @see Model::buildSearchCondition
 254          */
 255          function buildSearchCondition( $searchTerms )
 256          {
 257              $searchTerms = trim( $searchTerms );
 258              $searchCond = "(user LIKE '%".Db::qstr($searchTerms)."%' 
 259                             OR full_name LIKE '%".Db::qstr($searchTerms)."%' OR 
 260                             email LIKE '%".Db::qstr($searchTerms)."%')";
 261              
 262              return( $searchCond );
 263          }
 264  
 265          /**
 266          * Updates the information related to a user
 267          *
 268          * @param userInfo An UserInfo object containing the <b>already udpated</b> information of the
 269          * user we would like to update.
 270          * @return Returns true if ok or false otherwise.
 271          */
 272          function updateUser( $user )
 273          {
 274              $result = $this->update( $user );
 275  
 276              if( $result ) {
 277                  // remove the old data
 278                  $this->_cache->removeData( $user->getId(), CACHE_USERINFO );
 279                  $this->_cache->removeData( $user->getUsername(), CACHE_USERIDBYNAME );
 280              }
 281              
 282              BaseUserDataProvider::updateUser( $user );
 283  
 284              return $result;
 285          }
 286  
 287          /**
 288          * Adds a user to the database.
 289          *
 290          * @param user An UserInfo object with the necessary information
 291          * @return Returns the identifier assigned to the user, or false if there was any error. It will also modify the
 292          * UserInfo object passed by parameter and set its database id.
 293          */
 294          function addUser( &$user )
 295          {
 296              $userId = $this->add( $user );
 297  
 298              if( $userId ) {
 299                  // 1. We need to set the password again in this initial UserInfo object, because 
 300                  //    current password is plain password. Through setPassword() we can encrpyt the password
 301                  //    and make the UserInfo object right, then we can cache it. Or user can not login even
 302                  //    we addUser() successfully.
 303                  // 2. Another easy way to solve this is remove the cache code below, don't cache the UserInfo
 304                  //    Object in the first time. Let it cache later.
 305                  $user->setMD5Password( $user->getPassword() );
 306                  $this->_cache->setData( $user->getId(), CACHE_USERINFO, $user );
 307                  $this->_cache->setData( $user->getUsername(), CACHE_USERIDBYNAME, $user );
 308              }
 309              
 310              return( $userId );
 311          }
 312  
 313          /**
 314          * Returns an array with all the users that belong to the given
 315          * blog.
 316          *
 317          * @param blogId The blog identifier.
 318          * @param includeOwner Wether to include the owner of the blog or not.
 319          * @param status
 320          * @param searchTerms
 321          * @return An array with the information about the users who belong in
 322          * one way or another to that blog.
 323          */
 324          function getBlogUsers( $blogId, $includeOwner = true, $status = USER_STATUS_ALL, $searchTerms = "" )
 325          {
 326              $users = Array();
 327              $prefix = $this->getPrefix();
 328  
 329              // get the information about the owner, if requested so
 330              if( $includeOwner ) {
 331                  $query = "SELECT {$prefix}users.* FROM {$prefix}users, {$prefix}blogs 
 332                            WHERE {$prefix}users.id = {$prefix}blogs.owner_id AND {$prefix}blogs.id = '".Db::qstr($blogId)."';";
 333                  $result = $this->Execute( $query );
 334  
 335                  if( !$result )
 336                      return false;
 337  
 338                  $row = $result->FetchRow();
 339                  $result->Close();
 340                  array_push( $users, $this->mapRow( $row ));
 341              }
 342  
 343              // now get the other users who have permission for that blog.
 344              $query2 = "SELECT {$prefix}users.* FROM {$prefix}users, {$prefix}users_permissions 
 345                         WHERE {$prefix}users.id = {$prefix}users_permissions.user_id 
 346                         AND {$prefix}users_permissions.blog_id = '".Db::qstr($blogId)."';";
 347              $result2 = $this->Execute( $query2 );
 348              if( !$result2 ) // if error, return what we have so far...
 349                  return $users;
 350  
 351              while( $row = $result2->FetchRow()) {
 352                  array_push( $users, $this->mapRow($row));
 353              }
 354              $result2->Close();
 355  
 356              return $users;
 357          }
 358          
 359          /**
 360          * Removes users from the database
 361          *
 362          * @param userId The identifier of the user we are trying to remove
 363          */
 364          function deleteUser( $userId )
 365          {
 366              // first, delete all of his/her permissions
 367              if( $this->delete( $userId )) {            
 368                  lt_include( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" );
 369                  $perms = new UserPermissions();
 370                  $perms->revokeUserPermissions( $userId );
 371                  $this->_cache->removeData( $userId, CACHE_USERINFO );                        
 372              }
 373              else
 374                  return( false );
 375          }          
 376  
 377          /**
 378          * returns the total number of users
 379          *
 380          * @return total number of users
 381          */
 382          function getNumUsers( $status = USER_STATUS_ALL, $searchTerms = "" )
 383          {
 384              $table = $this->getPrefix()."users";
 385                  
 386              if( $status != USER_STATUS_ALL )
 387                  $where = "status = '".Db::qstr($status)."'";
 388  
 389              $where = "";
 390              if( $searchTerms != "" ) {
 391                  if( $where != "" )
 392                      $where .= " AND ";
 393                  $where = $this->getSearchConditions( $searchTerms );
 394              }
 395                  
 396              return( $this->getNumItems( $table, $where ));
 397          }
 398  
 399          /**
 400          * check if the email account has been registered
 401          * @return true if the email account has been registered
 402          */
 403          function emailExists($email) 
 404          {
 405              $query = "SELECT email 
 406                        FROM ".$this->getPrefix()."users 
 407                        WHERE email = '".Db::qstr($email)."'";
 408  
 409              $result = $this->Execute($query);
 410  
 411              if(!$result)
 412                  return false;
 413  
 414              $count = $result->RecordCount();
 415              $result->Close();
 416  
 417              return ($count >= 1);
 418          }
 419          
 420          /**
 421           * @see Model::getSearchConditions
 422           */
 423  		function getSearchConditions( $searchTerms )
 424          {
 425              lt_include( PLOG_CLASS_PATH."class/dao/searchengine.class.php" );            
 426              // prepare the query string
 427              $searchTerms = SearchEngine::adaptSearchString( $searchTerms );
 428              
 429              return( "(user LIKE '%".$searchTerms."%' OR full_name LIKE '%".$searchTerms."%')");
 430          }
 431      }
 432  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics