[ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 /* 4 * Name: vbb3userdataprovider (support read user info from vbb) 5 * Version: 1.0 6 * Author: Pan Ying(http://www.pactofshadow.com/lifetype/~nest) 7 * Contact: panying2000@gmail.com 8 * Release: 2006.10.5 9 * Download Link:http://www.pactofshadow.com/lifetype/2/articleperma/17.html 10 * 11 * Known Issue: 12 * Could not update user info in vbb. 13 * Could not delete user from vbb 14 * Do not support vbb user's Muti-user group , only support main group (todo in future) 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 19 */ 20 21 22 lt_include( PLOG_CLASS_PATH."class/dao/userdata/baseuserdataprovider.class.php" ); 23 lt_include( PLOG_CLASS_PATH."class/database/db.class.php" ); 24 25 /** 26 * Model representing the users in our application. Provides the methods such as 27 * authentication and querying for users. 28 * 29 * \ingroup User_Data_Providers 30 */ 31 class vbb3UserDataProvider extends BaseUserDataProvider 32 { 33 var $_dbc; //database connect 34 var $_vbb3prefix; //vbb database prefix 35 36 37 var $_usepasswordsalt; //vbb3 use password salt 38 var $_allowedusergroups; //which group in vbb will be active . 39 var $_disallowedusergroups; //which group in vbb will be not active , if you have block group , set it 40 41 var $_adminusergroups; //which group in vbb will have admin permission? 42 var $_adminusers; //special user in vbb to have admin permission. 43 44 /** 45 * Initializes the model 46 */ 47 function vbb3UserDataProvider( $providerConfig ) 48 { 49 $this->BaseUserDataProvider( $providerConfig ); 50 51 // initialize the database connection based on our parameters 52 $config = $this->getProviderConfiguration(); 53 $user = $config->getValue( "user" ); 54 $pass = $config->getValue( "password" ); 55 $host = $config->getValue( "host" ); 56 $db = $config->getValue( "database" ); 57 58 $this->_vbb3prefix = $config->getValue( "prefix" ); 59 $this->_usepasswordsalt = $config->getValue( "usesalt" ); 60 $this->_allowedusergroups = $config->getValue( "allowgroup" ); 61 $this->_disallowedusergroups = $config->getValue( "denygroup" ); 62 $this->_adminusergroups = $config->getValue( "admingroup"); 63 $this->_adminusers = $config->getValue( "adminuser"); 64 65 66 $this->_dbc =& Db::getNewDb( $host, $user, $pass, $db ); 67 } 68 69 function vbbAllowed( $row ) 70 { 71 //echo "vbbAllowed called".$row['usergroupid']; 72 if (!in_array($row['usergroupid'], $this->_disallowedusergroups)) 73 if (in_array($row['usergroupid'], $this->_allowedusergroups)) 74 return true; 75 76 // echo "vbbAllowed return false"; 77 78 return false; 79 } 80 81 function vbbAdmin( $row ) 82 { 83 //echo "vbbAdmin called"; 84 if (in_array($row['usergroupid'], $this->_adminusergroups)) 85 return true; 86 87 if (in_array($row['userid'], $this->_adminusers)) 88 return true; 89 90 //echo "vbbAdmin return false"; 91 92 return false; 93 } 94 95 function vbbCheckPassword( $pass , $row ) 96 { 97 //echo "vbbCheckPassword called"; 98 if ($this->_usepasswordsalt) 99 { 100 if(md5(md5($pass) . $row['salt']) == $row['password']) return true; 101 } 102 else 103 { 104 if(md5($pass) == $row['password']) return true; 105 } 106 107 108 return false; 109 } 110 111 /** 112 * Returns true if the user is in the database and the username 113 * and password match 114 * 115 * @param user Username of the user who we'd like to authenticate 116 * @param pass Password of the user 117 * @return true if user and password correct or false otherwise. 118 */ 119 function authenticateUser( $user, $pass ) 120 { 121 $query = "SELECT * FROM ".$this->_vbb3prefix."user WHERE username = '".Db::qstr( $user )."'"; 122 123 $result = $this->_dbc->Execute( $query ); 124 125 126 if( !$result ) 127 return false; 128 129 $ret = ($result->RecordCount() == 1); 130 131 if ($ret) $row = $result->FetchRow(); 132 133 $result->Close(); 134 135 136 if($ret && $this->vbbCheckPassword($pass,$row) && $this->vbbAllowed($row)) 137 return true; 138 else 139 return false; 140 } 141 142 /** 143 * Returns all the information associated to the user given 144 * 145 * @param user Username of the user from who we'd like to get the information 146 * @param pass Password of the user we'd like to get the information 147 * @return Returns a UserInfo object with the requested information, or false otherwise. 148 */ 149 function getUserInfo( $user, $pass ) 150 { 151 $query = "SELECT * FROM ".$this->_vbb3prefix."user WHERE username = '".Db::qstr( $user )."'"; 152 153 154 $result = $this->_dbc->Execute( $query ); 155 156 if( !$result ) 157 return false; 158 159 $row = $result->FetchRow(); 160 $result->Close(); 161 162 if (!$this->vbbCheckPassword($pass,$row)) 163 return false; 164 165 return( $this->_mapUserInfoObject( $row )); 166 } 167 168 /** 169 * Retrieves the user information but given only a username 170 * 171 * @param username The username of the user 172 * @return Returns a UserInfo object with the requested information, or false otherwise. 173 */ 174 function getUserInfoFromUsername( $username ) 175 { 176 $query = "SELECT * FROM ".$this->_vbb3prefix."user WHERE username = '".Db::qstr( $username )."'"; 177 178 $result = $this->_dbc->Execute( $query ); 179 180 if( !$result ) 181 return false; 182 183 if( $result->RowCount() == 0 ){ 184 $result->Close(); 185 return false; 186 } 187 188 $row = $result->FetchRow(); 189 $result->Close(); 190 191 return( $this->_mapUserInfoObject( $row )); 192 } 193 194 /** 195 * Retrieves the user infromation but given only a userid 196 * 197 * @param userId User ID of the user from whom we'd like to get the information 198 * @return Returns a UserInfo object with the requested information, or false otherwise. 199 */ 200 function getUserInfoFromId( $userid, $extendedInfo = false ) 201 { 202 lt_include( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" ); 203 204 205 $query = "SELECT * FROM ".$this->_vbb3prefix."user WHERE userid = '".Db::qstr( $userid )."'"; 206 207 //print("user__id = $userid"); 208 209 $result = $this->_dbc->Execute( $query ); 210 211 if( !$result ) 212 return false; 213 214 $row = $result->FetchRow(); 215 $result->Close(); 216 217 // fetch the user permissions 218 //$perms = new UserPermissions(); 219 //$row["site_admin"] = $perms->isSiteAdmin( $userid ); 220 221 return( $this->_mapUserInfoObject( $row )); 222 } 223 224 function vbb3AddBlog( $row ) 225 { 226 // create a new blog 227 lt_include( PLOG_CLASS_PATH."class/dao/bloginfo.class.php" ); 228 lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" ); 229 lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" ); 230 lt_include( PLOG_CLASS_PATH."class/dao/articlecategories.class.php" ); 231 232 $blogs = new Blogs(); 233 $blog = new BlogInfo( $row["user"], // name of the new blog 234 $row["id"], // id of the owner 235 "", // no about 236 ""); // no properties either 237 $newBlogId = $blogs->addBlog( $blog ); 238 239 // add a default category and a default post 240 $articleCategories = new ArticleCategories(); 241 $articleCategory = new ArticleCategory( "General", "", $newBlogId, true ); 242 $catId = $articleCategories->addArticleCategory( $articleCategory ); 243 $config =& Config::getConfig(); 244 $locale =& Locales::getLocale( $config->getValue( "default_locale" )); 245 $articleTopic = $locale->tr( "register_default_article_topic" ); 246 $articleText = $locale->tr( "register_default_article_text" ); 247 $article = new Article( $articleTopic, 248 $articleText, 249 Array( $catId ), 250 $row["user_id"], 251 $newBlogId, 252 POST_STATUS_PUBLISHED, 253 0, 254 Array(), 255 "welcome" ); 256 $t = new Timestamp(); 257 $article->setDateObject( $t ); 258 $articles = new Articles(); 259 $articles->addArticle( $article ); 260 } 261 262 function _mapUserInfoObject( $row, $extraInfo = false ) 263 { 264 lt_include( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" ); 265 266 $plogPhpBB2Data = $this->getpLogPHPBBUserData( $row["userid"] ); 267 268 $row["user"] = $row["username"]; 269 //$row["password"] = $row["password"]; //todo 270 $row["email"] = $row["email"]; 271 $row["about"] = $plogPhpBB2Data["about"]; 272 $row["full_name"] = $plogPhpBB2Data["full_name"]; 273 $row["resource_picture_id"] = $plogPhpBB2Data["resource_picture_id"]; 274 if( $row["resource_picture_id"] == "" ) 275 $row["resource_picture_id"] = 0; 276 $row["properties"] = serialize(Array()); 277 $row["id"] = $row["userid"]; 278 $row["status"] = $this->vbbAllowed($row) ? USER_STATUS_ACTIVE : USER_STATUS_DISABLED; 279 $row["site_admin"] = $this->vbbAdmin($row)?1:0; 280 281 // does this vbb3 user have a blog yet? If so, create one if the configuration 282 // of the user data provider says so 283 $providerConfig = $this->getProviderConfiguration(); 284 if( $providerConfig->getValue( "createBlogIfNotExisting" )) { 285 $userInfo = BaseUserDataProvider::mapRow( $row, true ); 286 // check if this user is assigned to any blog 287 $userBlogs = $userInfo->getBlogs(); 288 if( empty($userBlogs )) { 289 // assign the login_perm permission 290 $this->grantLoginPermission( $userInfo ); 291 292 $this->vbb3AddBlog( $row ); 293 $userInfo->setBlogs( $this->getUsersBlogs( $userInfo->getId())); 294 } 295 } 296 else { 297 $userInfo = BaseUserDataProvider::mapRow( $row ); 298 } 299 300 return( $userInfo ); 301 } 302 303 /** 304 * Returns an array with all the users available in the database 305 * 306 * @param status 307 * @param includeExtraInfo 308 * @param page 309 * @param itemsPerPage 310 * @return An array containing all the users. 311 */ 312 function getAllUsers( $status = USER_STATUS_ALL, $searchTerms = "", $orderBy = "", $page = -1, $itemsPerPage = DEFAULT_ITEMS_PER_PAGE ) 313 { 314 $where = ""; 315 switch ($status) 316 { 317 case user_status_all: 318 $where = ""; 319 break; 320 case user_status_active: 321 $where = "usergroupid in (".implode(",", $this->_allowedusergroups).")"; 322 break; 323 case user_status_unconfirmed: 324 case user_status_disabled: 325 $where = "not(usergroupid in (".implode(",", $this->_allowedusergroups)."))"; 326 break; 327 } 328 329 if ($searchTerms != "") 330 { 331 if ($where != "") 332 $where = $where." AND ".($this->getSearchConditions($searchTerms)); 333 else 334 $where = $this->getSearchConditions($searchTerms); 335 } 336 337 338 if ($where != "") 339 $where = " where ".$where; 340 341 $query = "SELECT * FROM ".$this->_vbb3prefix."user".$where." ORDER BY userid ASC"; 342 343 $result = $this->_dbc->Execute( $query, $page, $itemsPerPage ); 344 345 $users = Array(); 346 347 while ($info = $result->FetchRow( $result )) 348 array_push( $users, $this->_mapUserInfoObject( $info )); 349 $result->Close(); 350 351 return $users; 352 } 353 354 /** 355 * Updates the information related to a user 356 * 357 * @param userInfo An UserInfo object containing the <b>already udpated</b> information of the 358 * user we would like to update. 359 * @return Returns true if ok or false otherwise. 360 */ 361 function updateUser( $userInfo ) 362 { 363 BaseUserDataProvider::updateUser( $userInfo ); 364 return $this->updatepLogPHPBB2UserData( $userInfo ); //nerver change data in vbb table , just return the updatepLogPHPBB2UserData' return value 365 366 $query = "UPDATE ".$this->_vbb3prefix."user SET 367 username = '".Db::qstr($userInfo->getUserName())."', 368 email = '".Db::qstr($userInfo->getEmail())."', 369 //user_active = '".Db::qstr($userInfo->getPassword())."' 370 WHERE userid = '".Db::qstr($userInfo->getId())."'";//todo 371 372 $result = $this->_dbc->Execute( $query ); 373 374 if( !$result ) 375 return false; 376 377 BaseUserDataProvider::updateUser( $userInfo ); 378 379 // update plog's phpbb2_user table 380 $result = $this->updatepLogPHPBB2UserData( $userInfo ); 381 382 return( $result ); 383 } 384 385 /** 386 * @private 387 * Why the hell couldn't they make the user_id field auto-incrementable??? 388 */ 389 function getLastPhpBBUserId() 390 { 391 $query = "SELECT MAX(userid)+1 AS next_id FROM ".$this->_vbb3prefix."user"; 392 393 $result = $this->_dbc->Execute( $query ); 394 395 $row = $result->FetchRow(); 396 $result->Close(); 397 398 return( $row["next_id"] ); 399 } 400 401 /** 402 * Adds a user to the database. 403 * 404 * @param user An UserInfo object with the necessary information 405 * @return Returns the identifier assigned to the user, or false if there was any error. It will also modify the 406 * UserInfo object passed by parameter and set its database id. 407 */ 408 function addUser( &$user ) 409 { 410 return false; //nerver change data in vbb table , just tell pblog can not do that 411 $password = $user->getPassword(); 412 $id = $this->getLastPhpBBUserId(); 413 414 $query = "INSERT INTO ".$this->_vbb3prefix."user (userid,username,password,useremail) 415 VALUES ($id, '".Db::qstr($user->getUserName())."','".md5($user->getPassword())."','". 416 Db::qstr($user->getEmail())."');"; 417 418 $result = $this->_dbc->Execute( $query ); 419 420 if( !$result ) 421 return false; 422 423 $user->setId( $id ); 424 425 // update plog's phpbb2_user table 426 $this->updatepLogPHPBB2UserData( $user ); 427 428 return( $id ); 429 } 430 431 /** 432 * @private 433 * Updates the plog-specific user data that is used when the vbb3 integration is enabled, since 434 * plog has some extra information that does not fit anywhere in vbb3 435 * 436 * @param user A UserInfo object 437 * @return true if successful or false otherwise 438 */ 439 function updatepLogPHPBB2UserData( &$user ) 440 { 441 // is the user already there? 442 if( $this->getpLogPHPBBUserData( $user->getId())) { 443 // we need to run an UPDATE query... 444 $query = "UPDATE ".$this->getPrefix()."phpbb2_users 445 SET full_name = '".Db::qstr( $user->getFullName())."', 446 about = '".Db::qstr( $user->getAboutMyself())."', 447 properties = '".Db::qstr( serialize($user->getProperties()))."', 448 resource_picture_id = '".Db::qstr( $user->getPictureId())."', 449 status = '".Db::qstr( $user->getStatus())."' 450 WHERE phpbb_id = '".Db::qstr( $user->getId())."'"; 451 } 452 else { 453 // we need to run an INSERT query... 454 $query = "INSERT INTO ".$this->getPrefix()."phpbb2_users 455 (full_name, about, properties, resource_picture_id,phpbb_id,status) 456 VALUES ('".Db::qstr( $user->getFullName())."', '". 457 Db::qstr($user->getAboutMyself())."','". 458 Db::qstr(serialize($user->getProperties()))."','". 459 Db::qstr($user->getPictureId())."','". 460 Db::qstr($user->getId())."','". 461 Db::qstr($user->getStatus())."');"; 462 } 463 464 $result = $this->Execute( $query ); 465 466 return( true ); 467 } 468 469 /** 470 * @private 471 * Load the plog-specific vbb3 user data 472 * 473 * @param userId 474 * @return A row with the extra user data or false otherwise 475 */ 476 function getpLogPHPBBUserData( $userId ) 477 { 478 $query = "SELECT * FROM ".$this->getPrefix()."phpbb2_users WHERE phpbb_id = '".Db::qstr($userId)."'"; 479 480 $result = $this->Execute( $query ); 481 482 if( !$result ) 483 return false; 484 485 if( $result->RowCount() == 0 ){ 486 $result->Close(); 487 return false; 488 } 489 490 $ret = $result->FetchRow(); 491 $result->Close(); 492 493 return $ret; 494 } 495 496 /** 497 * Removes users from the database 498 * 499 * @param userId The identifier of the user we are trying to remove 500 */ 501 function deleteUser( $userId ) 502 { 503 } 504 505 /** 506 * returns the total number of users 507 * 508 * @return total number of users 509 */ 510 function getNumUsers( $status = USER_STATUS_ALL , $searchTerms = "" ) 511 { 512 $where = ""; 513 switch ($status) 514 { 515 case user_status_all: 516 $where = ""; 517 break; 518 case user_status_active: 519 $where = "usergroupid in (".implode(",", $this->_allowedusergroups).")"; 520 break; 521 case user_status_unconfirmed: 522 case user_status_disabled: 523 $where = "not(usergroupid in (".implode(",", $this->_allowedusergroups)."))"; 524 break; 525 } 526 527 if ($searchTerms != "") 528 { 529 if ($where != "") 530 $where = $where." AND ".$this->getSearchConditions($searchTerms); 531 else 532 $where = $this->getSearchConditions($searchTerms); 533 } 534 535 536 if ($where != "") 537 $where = " where ".$where; 538 539 $query = "SELECT COUNT(userid) AS total FROM ".$this->_vbb3prefix."user".$where; 540 541 542 $result = $this->_dbc->Execute( $query ); 543 544 // return no users if this doesn't work! 545 if( !$result ) 546 return 0; 547 548 $row = $result->FetchRow(); 549 $result->Close(); 550 551 if( $row["total"] == "" ) 552 $row["total"] = 0; 553 554 return( $row["total"] ); 555 } 556 557 /** 558 * check if the email account has been registered 559 * @return true if the email account has been registered 560 */ 561 function emailExists($email) 562 { 563 $query = "SELECT * FROM ".$this->_vbb3prefix."user WHERE email = '".Db::qstr($email)."'"; 564 565 $result = $this->_dbc->Execute( $query ); 566 567 if( !$result ) 568 return false; 569 $ret = ($result->RecordCount() > 0); 570 $result->Close(); 571 return $ret; 572 } 573 574 575 /** 576 * @see Model::getSearchConditions 577 */ 578 function getSearchConditions( $searchTerms ) 579 { 580 lt_include( PLOG_CLASS_PATH."class/dao/searchengine.class.php" ); 581 // prepare the query string 582 $searchTerms = SearchEngine::adaptSearchString( $searchTerms ); 583 584 return( "(username LIKE '%".$searchTerms."%')"); 585 } 586 587 /** 588 * Returns an array with all the users that belong to the given 589 * blog. 590 * 591 * @param blogId The blog identifier. 592 * @param includeOwner Wether to include the owner of the blog or not. 593 * @param status 594 * @param searchTerms 595 * @return An array with the information about the users who belong in 596 * one way or another to that blog. 597 */ 598 function getBlogUsers( $blogId, $includeOwner = true, $status = USER_STATUS_ALL, $searchTerms = "" ) 599 { 600 $userids = Array(); 601 $users = Array(); 602 $prefix = $this->getPrefix(); 603 604 605 // get the information about the owner, if requested so 606 if( $includeOwner ) { 607 $query = "SELECT {$prefix}blogs.owner_id as userid FROM {$prefix}blogs 608 WHERE {$prefix}blogs.id = '".Db::qstr($blogId)."';"; 609 $result = $this->Execute( $query ); 610 611 if( !$result ) 612 return $users; 613 614 $row = $result->FetchRow(); 615 $result->Close(); 616 617 array_push($userids,$row['userid']); 618 } 619 620 // now get the other users who have permission for that blog. 621 $query2 = "SELECT {$prefix}users_permissions.user_id as userid FROM {$prefix}users_permissions 622 WHERE {$prefix}users_permissions.blog_id = '".Db::qstr($blogId)."';"; 623 $result2 = $this->Execute( $query2 ); 624 625 if( $result2 ) 626 { 627 while( $row = $result2->FetchRow()) { 628 array_push($userids,$row['userid']); 629 } 630 $result2->Close(); 631 } 632 633 634 if (!is_array($userids)) //return empty value 635 { 636 return $users; 637 } 638 639 640 $where = ""; 641 switch ($status) 642 { 643 case user_status_all: 644 $where = ""; 645 break; 646 case user_status_active: 647 $where = "usergroupid in (".implode(",", $this->_allowedusergroups).")"; 648 break; 649 case user_status_unconfirmed: 650 case user_status_disabled: 651 $where = "not(usergroupid in (".implode(",", $this->_allowedusergroups)."))"; 652 break; 653 } 654 655 if ($searchTerms != "") 656 { 657 if ($where != "") 658 $where = $where." AND ".($this->getSearchConditions($searchTerms)); 659 else 660 $where = $this->getSearchConditions($searchTerms); 661 } 662 663 if ($where != "") 664 $where = $where." AND "; 665 666 $where = $where." (userid in (".implode(",", $userids)."))"; 667 668 669 670 if ($where != "") 671 $where = " where ".$where; 672 673 $query3 = "SELECT * FROM ".$this->_vbb3prefix."user".$where." ORDER BY userid ASC"; 674 675 676 $result3 = $this->_dbc->Execute( $query3); 677 678 679 680 while ($info = $result3->FetchRow( $result3 )) 681 array_push( $users, $this->_mapUserInfoObject( $info )); 682 $result3->Close(); 683 684 return $users; 685 } 686 } 687 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |