[ 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/ -> phpbb2userdataprovider.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      
   6      /**

   7       * Model representing the users in our application. Provides the methods such as

   8       * authentication and querying for users.

   9       *

  10       * \ingroup User_Data_Providers

  11       */
  12      class PhpBB2UserDataProvider extends BaseUserDataProvider
  13      {
  14          var $_db;
  15          var $_prefix;
  16  
  17          /**

  18           * Initializes the model

  19           */
  20          function PhpBB2UserDataProvider( $providerConfig )
  21          {
  22              $this->BaseUserDataProvider( $providerConfig );
  23  
  24              // initialize the database connection based on our parameters

  25              $config = $this->getProviderConfiguration();
  26              $user = $config->getValue( "user" );
  27              $pass = $config->getValue( "password" );
  28              $host = $config->getValue( "host" );
  29              $db = $config->getValue( "database" );
  30              $this->_phpbbprefix = $config->getValue( "prefix" );
  31              
  32              $this->_dbc =& Db::getNewDb( $host, $user, $pass, $db );                     
  33          }
  34  
  35          /**

  36           * Returns true if the user is in the database and the username

  37           * and password match

  38           *

  39           * @param user Username of the user who we'd like to authenticate

  40           * @param pass Password of the user

  41           * @return true if user and password correct or false otherwise.

  42           */
  43          function authenticateUser( $user, $pass )
  44          {    
  45              $query = "SELECT * FROM ".$this->_phpbbprefix."users WHERE username = '".Db::qstr( $user )."'
  46                        AND user_password = '".md5( $pass )."' AND user_active > 0";
  47                        
  48              $result = $this->_dbc->Execute( $query );
  49              
  50              if( !$result )
  51                  return false;
  52                  
  53              $ret = ($result->RecordCount() == 1);
  54              $result->Close();
  55  
  56              if($ret)
  57                  return true;
  58              else
  59                  return false;        
  60          }
  61  
  62          /**

  63           * Returns all the information associated to the user given

  64           *

  65           * @param user Username of the user from who we'd like to get the information

  66           * @param pass Password of the user we'd like to get the information

  67           * @return Returns a UserInfo object with the requested information, or false otherwise.

  68           */
  69          function getUserInfo( $user, $pass )
  70          {
  71              $query = "SELECT * FROM ".$this->_phpbbprefix."users WHERE username = '".Db::qstr( $user )."'
  72                        AND user_password = '".md5( $pass )."'";
  73                        
  74              $result = $this->_dbc->Execute( $query );
  75              
  76              if( !$result )
  77                  return false;
  78                  
  79              $row = $result->FetchRow();
  80              $result->Close();
  81  
  82              return( $this->_mapUserInfoObject( $row ));            
  83          }
  84  
  85          /**

  86           * Retrieves the user information but given only a username

  87           *

  88           * @param username The username of the user

  89           * @return Returns a UserInfo object with the requested information, or false otherwise.

  90           */
  91          function getUserInfoFromUsername( $username )
  92          {
  93              $query = "SELECT * FROM ".$this->_phpbbprefix."users WHERE username = '".Db::qstr( $username )."'";                      
  94                        
  95              $result = $this->_dbc->Execute( $query );
  96              
  97              if( !$result )
  98                  return false;
  99                                  
 100              if( $result->RowCount() == 0 ){
 101                  $result->Close();
 102                  return false;
 103              }
 104                  
 105              $row = $result->FetchRow();
 106              $result->Close();
 107              
 108              return( $this->_mapUserInfoObject( $row ));            
 109          }
 110  
 111          /**

 112           * Retrieves the user infromation but given only a userid

 113           *

 114           * @param userId User ID of the user from whom we'd like to get the information

 115           * @return Returns a UserInfo object with the requested information, or false otherwise.

 116           */
 117          function getUserInfoFromId( $userid, $extendedInfo = false )
 118          {
 119              lt_include( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" );
 120              
 121              $query = "SELECT * FROM ".$this->_phpbbprefix."users WHERE user_id = '".Db::qstr( $userid )."'";
 122  
 123              $result = $this->_dbc->Execute( $query );
 124              
 125              if( !$result )
 126                  return false;
 127                  
 128              $row = $result->FetchRow();
 129              $result->Close();
 130              
 131              return( $this->_mapUserInfoObject( $row ));
 132          }
 133          
 134          function phpBB2AddBlog( $row )
 135          {
 136              // create a new blog

 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              $blogs = new Blogs();
 143              $blog = new BlogInfo( $row["user"],  // name of the new blog
 144                                       $row["id"],  // id of the owner
 145                                       "",  // no about
 146                                    ""); // no properties either

 147              $newBlogId = $blogs->addBlog( $blog );
 148                                
 149              // add a default category and a default post            

 150              $articleCategories = new ArticleCategories();
 151              $articleCategory = new ArticleCategory( "General", "", $newBlogId, true );
 152              $catId = $articleCategories->addArticleCategory( $articleCategory );
 153              $config =& Config::getConfig();
 154              $locale =& Locales::getLocale( $config->getValue( "default_locale" ));
 155              $articleTopic = $locale->tr( "register_default_article_topic" );
 156              $articleText  = $locale->tr( "register_default_article_text" );
 157              $article = new Article( $articleTopic, 
 158                                      $articleText, 
 159                                      Array( $catId ), 
 160                                      $row["user_id"], 
 161                                      $newBlogId, 
 162                                      POST_STATUS_PUBLISHED, 
 163                                      0, 
 164                                      Array(), 
 165                                      "welcome" );
 166              $t = new Timestamp();
 167              $article->setDateObject( $t );
 168              $article->setInSummary( false );
 169              $articles = new Articles();
 170              $articles->addArticle( $article );               
 171          }
 172          
 173          function _mapUserInfoObject( $row, $extraInfo = false )
 174          {
 175              lt_include( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" );
 176              
 177              $plogPhpBB2Data = $this->getpLogPHPBBUserData( $row["user_id"] );
 178  
 179              $row["user"] = $row["username"];
 180              $row["password"] = $row["user_password"];
 181              $row["email"] = $row["user_email"];
 182              $row["about"] = $plogPhpBB2Data["about"];
 183              $row["full_name"] = $plogPhpBB2Data["full_name"];
 184              $row["resource_picture_id"] = $plogPhpBB2Data["resource_picture_id"];
 185              if( $row["resource_picture_id"] == "" ) $row["resource_picture_id"] = 0;
 186              $row["properties"] = serialize(Array());
 187              $row["id"] = $row["user_id"];   
 188              $row["status"] = ($row["user_active"] > 0) ? USER_STATUS_ACTIVE : USER_STATUS_DISABLED;
 189              $row["site_admin"] = $row["user_level"];         
 190                          
 191                 // does this phpbb user have a blog yet? If so, create one if the configuration

 192              // of the user data provider says so

 193              $providerConfig = $this->getProviderConfiguration();
 194              if( $providerConfig->getValue( "createBlogIfNotExisting" )) {
 195          
 196          
 197                  $userInfo = BaseUserDataProvider::mapRow( $row, true );
 198                  // check if this user is assigned to any blog

 199                  $userBlogs = $userInfo->getBlogs();
 200                  if( empty($userBlogs )) {
 201                      // assign the login_perm permission

 202                      $this->grantLoginPermission( $userInfo );
 203                                  
 204                      $this->phpBB2AddBlog( $row );
 205                      $userInfo->setBlogs( $this->getUsersBlogs( $userInfo->getId()));
 206                   }                
 207              }
 208              else {
 209                  $userInfo = BaseUserDataProvider::mapRow( $row );
 210              }
 211              
 212              return( $userInfo );
 213          }
 214  
 215          /**

 216           * Returns an array with all the users available in the database

 217           *

 218           * @param status

 219           * @param includeExtraInfo

 220           * @param page

 221           * @param itemsPerPage

 222           * @return An array containing all the users.

 223           */
 224          function getAllUsers( $status = USER_STATUS_ALL, $searchTerms = "", $orderBy = "", $page = -1, $itemsPerPage = DEFAULT_ITEMS_PER_PAGE )
 225          {            
 226              $query = "SELECT * FROM ".$this->_phpbbprefix."users WHERE user_id >= 0 ORDER BY user_id ASC";
 227  
 228              $result = $this->_dbc->Execute( $query, $page, $itemsPerPage );            
 229  
 230              $users = Array();
 231  
 232              while ($info = $result->FetchRow( $result ))
 233                  array_push( $users, $this->_mapUserInfoObject( $info ));
 234              $result->Close();
 235  
 236              return $users;                        
 237          }
 238  
 239          /**

 240           * Updates the information related to a user

 241           *

 242           * @param userInfo An UserInfo object containing the <b>already udpated</b> information of the

 243           * user we would like to update.

 244           * @return Returns true if ok or false otherwise.

 245           */
 246          function updateUser( $userInfo )
 247          {
 248              $query = "UPDATE ".$this->_phpbbprefix."users SET
 249                        username = '".Db::qstr($userInfo->getUserName())."',
 250                        user_email = '".Db::qstr($userInfo->getEmail())."',
 251                        user_active = '".Db::qstr($userInfo->getPassword())."'
 252                        WHERE user_id = '".Db::qstr($userInfo->getId())."'";
 253                                    
 254              $result = $this->_dbc->Execute( $query );            
 255              
 256              if( !$result )
 257                  return false;
 258              
 259              BaseUserDataProvider::updateUser( $userInfo );
 260              
 261              // update plog's phpbb2_user table

 262              $result = $this->updatepLogPHPBB2UserData( $userInfo );
 263  
 264              return( $result );
 265          }
 266          
 267          /**

 268           * @private

 269           * Why the hell couldn't they make the user_id field auto-incrementable???

 270           */
 271          function getLastPhpBBUserId()
 272          {
 273             $query = "SELECT MAX(user_id)+1 AS next_id FROM ".$this->_phpbbprefix."users"; 
 274             
 275             $result = $this->_dbc->Execute( $query );
 276             
 277             $row = $result->FetchRow();
 278             $result->Close();
 279             
 280             return( $row["next_id"] );
 281          }
 282  
 283          /**

 284           * Adds a user to the database.

 285           *

 286           * @param user An UserInfo object with the necessary information

 287           * @return Returns the identifier assigned to the user, or false if there was any error. It will also modify the

 288           * UserInfo object passed by parameter and set its database id.

 289           */
 290          function addUser( &$user )
 291          {            
 292              // update the phpbb table

 293              $password = $user->getPassword();
 294              $id = $this->getLastPhpBBUserId();
 295                  
 296              $query = "INSERT INTO ".$this->_phpbbprefix."users (user_id,username,user_password,user_email,user_active)
 297                        VALUES ($id, '".Db::qstr($user->getUserName())."','".md5($user->getPassword())."','".
 298                        Db::qstr($user->getEmail())."','1');";                      
 299                        
 300              $result = $this->_dbc->Execute( $query );            
 301  
 302              if( !$result )
 303                  return false;
 304              
 305              $user->setId( $id );
 306              
 307              // update plog's phpbb2_user table

 308              $this->updatepLogPHPBB2UserData( $user );
 309  
 310              return( $id );
 311          }
 312          
 313          /**

 314           * @private

 315           * Updates the plog-specific user data that is used when the phpbb2 integration is enabled, since

 316           * plog has some extra information that does not fit anywhere in phpbb2

 317           *

 318           * @param user A UserInfo object

 319           * @return true if successful or false otherwise

 320           */
 321          function updatepLogPHPBB2UserData( &$user )
 322          {
 323              // is the user already there?

 324              if( $this->getpLogPHPBBUserData( $user->getId())) {
 325                  // we need to run an UPDATE query...

 326                  $query = "UPDATE ".$this->getPrefix()."phpbb2_users
 327                            SET full_name = '".Db::qstr( $user->getFullName())."', 
 328                                about = '".Db::qstr( $user->getAboutMyself())."',
 329                                properties = '".Db::qstr( serialize($user->getProperties()))."',
 330                                resource_picture_id = '".Db::qstr( $user->getPictureId())."',
 331                                status = '".Db::qstr( $user->getStatus())."'
 332                            WHERE phpbb_id = '".Db::qstr( $user->getId())."'";    
 333              }
 334              else {
 335                  // we need to run an INSERT query...

 336                  $query = "INSERT INTO ".$this->getPrefix()."phpbb2_users
 337                            (full_name, about, properties, resource_picture_id,phpbb_id,status)
 338                            VALUES ('".Db::qstr( $user->getFullName())."', '".
 339                            Db::qstr($user->getAboutMyself())."','".
 340                            Db::qstr(serialize($user->getProperties()))."','".
 341                            Db::qstr($user->getPictureId())."','".
 342                            Db::qstr($user->getId())."','".
 343                            Db::qstr($user->getStatus())."');";
 344              }
 345              
 346              $result = $this->Execute( $query );
 347              
 348              return( true );
 349          }
 350          
 351          /**

 352           * @private

 353           * Load the plog-specific phpbb2 user data

 354           *

 355           * @param userId

 356           * @return A row with the extra user data or false otherwise

 357           */
 358          function getpLogPHPBBUserData( $userId )
 359          {
 360              $query = "SELECT * FROM ".$this->getPrefix()."phpbb2_users WHERE phpbb_id = '".Db::qstr($userId)."'";
 361              
 362              $result = $this->Execute( $query );
 363      
 364              if( !$result ) 
 365                  return false;
 366                  
 367              if( $result->RowCount() == 0 ) {
 368                  $result->Close();
 369                  return false;
 370              }
 371  
 372              $ret = $result->FetchRow();
 373              $result->Close();
 374  
 375              return $ret;
 376          }
 377          
 378          /**

 379           * Removes users from the database

 380           *

 381           * @param userId The identifier of the user we are trying to remove

 382           */
 383          function deleteUser( $userId )
 384          {
 385          }        
 386  
 387          /**

 388           * returns the total number of users

 389           *

 390           * @return total number of users

 391           */
 392          function getNumUsers( $status = USER_STATUS_ALL )
 393          {
 394              //

 395              // :TODO:

 396              // add the status check here!

 397              //

 398              $query = "SELECT COUNT(id) AS total FROM ".$this->_phpbbprefix."users";
 399              
 400              $result = $this->_dbc->Execute( $query );
 401              
 402              // return no users if this doesn't work!

 403              if( !$result )
 404                  return 0;
 405              
 406              $row = $result->FetchRow();
 407              $result->Close();
 408              
 409              if( $row["total"] == "" )
 410                  $row["total"] = 0;
 411                  
 412              return( $row["total"] );
 413          }
 414  
 415          /**

 416           * check if the email account has been registered

 417           * @return true if the email account has been registered

 418           */
 419          function emailExists($email)        
 420          {
 421              $query = "SELECT * FROM ".$this->_phpbbprefix."users WHERE user_email = '".Db::qstr($email)."'";
 422              
 423              $result = $this->_dbc->Execute( $query );
 424              
 425              if( !$result )
 426                  return false;
 427              $ret = ($result->RecordCount() > 0);
 428              $result->Close();
 429              return $ret;
 430          }
 431      }
 432  ?>


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