| [ Index ] |
|
Code source de LifeType 1.2.4 |
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 define( "JOOMLA_USER_IS_BLOCKED", 1); 7 define( "JOOMLA_USER_IS_ACTIVE", 0); 8 define( "JOOMLA_SITE_ADMIN", "Super Administrator"); 9 10 11 /** 12 * Model representing the users in our application. Provides the methods such as 13 * authentication and querying for users. 14 * 15 * \ingroup User_Data_Providers 16 */ 17 18 class JoomlaUserDataProvider extends BaseUserDataProvider 19 //Based on phpbb2userdataprovider.class.php 20 { 21 var $_db; 22 var $_joomladbprefix; 23 var $_joomlaauxtable; 24 var $_blogtitle_postfix; 25 26 /** 27 * Initializes the model 28 */ 29 function JoomlaUserDataProvider( $providerConfig ) 30 { 31 $this->BaseUserDataProvider( $providerConfig ); 32 33 //*** Temporarily disabled for causing a fatal error ***/ 34 // disable all caching for userdata 35 //CacheManager::disableCache( CACHE_USERINFO ); 36 37 // initialize the database connection based on our parameters 38 $config = $this->getProviderConfiguration(); 39 $user = $config->getValue( "user" ); 40 $pass = $config->getValue( "password" ); 41 $host = $config->getValue( "host" ); 42 $db = $config->getValue( "database" ); 43 $this->_joomladbprefix = $config->getValue( "prefix" ); 44 $this->_blogtitle_postfix = $config->getValue( "blogtitle_postfix" ); 45 //phpbb2_users table is created upon installation, we can use it for joomla too 46 //but better parameterize its name 47 $this->_joomlaauxtable="joomla_users"; 48 49 $this->_dbc =& Db::getNewDb( $host, $user, $pass, $db ); 50 51 //Oddly, there have been problems in reading Joomla! db in utf-8 52 //Setting explicitly connection charset has fixed them 53 $dbcharset = $config->getValue( "dbcharset" ); 54 $query = "SET NAMES '".$dbcharset."';"; 55 $result = $this->_dbc->Execute( $query ); 56 57 58 } 59 60 /** 61 * Returns true if the user is in the database and the username 62 * and password match 63 * 64 * @param user Username of the user who we'd like to authenticate 65 * @param pass Password of the user 66 * @return true if user and password correct or false otherwise. 67 */ 68 function authenticateUser( $user, $pass ) 69 { 70 $query = "SELECT * FROM ".$this->_joomladbprefix."users". 71 " WHERE username = '".$user."'". 72 " AND password = '".md5($pass)."'". 73 " AND block=".JOOMLA_USER_IS_ACTIVE; 74 75 $result = $this->_dbc->Execute( $query ); 76 77 if( !$result ){ 78 return false; 79 } 80 81 $ret = ($result->RecordCount() == 1); 82 $result->Close(); 83 84 if($ret) 85 return true; 86 else{ 87 return false; 88 } 89 } 90 91 /** 92 * Returns all the information associated to the user given 93 * 94 * @param user Username of the user from who we'd like to get the information 95 * @param pass Password of the user we'd like to get the information 96 * @return Returns a UserInfo object with the requested information, or false otherwise. 97 */ 98 function getUserInfo( $user, $pass ) 99 { 100 //Query Joomla db 101 $query = "SELECT * FROM ".$this->_joomladbprefix."users". 102 " WHERE username = '".$user."'". 103 " AND password = '".md5($pass)."';". 104 105 $result = $this->_dbc->Execute( $query ); 106 107 if( !$result ) 108 return false; 109 110 $row = $result->FetchRow(); 111 $result->Close(); 112 113 $userid=$row["id"]; 114 115 $result->Close(); 116 117 return( $this->_mapUserInfoObject( $row )); 118 } 119 120 /** 121 * Retrieves the user information but given only a username 122 * 123 * @param username The username of the user 124 * @return Returns a UserInfo object with the requested information, or false otherwise. 125 */ 126 function getUserInfoFromUsername( $username ) 127 { 128 //Query Joomla table 129 $query = "SELECT * FROM ".$this->_joomladbprefix."users WHERE username = '".Db::qstr( $username )."'"; 130 131 $result = $this->_dbc->Execute( $query ); 132 133 if( !$result ){ 134 return false; 135 } 136 137 if( $result->RowCount() != 1 ){ 138 $result->Close(); 139 return false; 140 } 141 142 $row = $result->FetchRow(); 143 $result->Close(); 144 145 $userid=$row["id"]; 146 147 return( $this->_mapUserInfoObject( $row )); 148 } 149 150 /** 151 * Retrieves the user infromation but given only a userid 152 * 153 * @param userId User ID of the user from whom we'd like to get the information 154 * @return Returns a UserInfo object with the requested information, or false otherwise. 155 */ 156 function getUserInfoFromId( $userid, $extendedInfo = false ) 157 { 158 lt_include( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" ); 159 160 $query = "SELECT * FROM ".$this->_joomladbprefix."users WHERE id = '".Db::qstr( $userid )."'"; 161 162 //print("user__id = $userid"); 163 164 $result = $this->_dbc->Execute( $query ); 165 166 if( !$result ) 167 return false; 168 169 $row = $result->FetchRow(); 170 $result->Close(); 171 172 return( $this->_mapUserInfoObject( $row )); 173 } 174 175 function JoomlaAddBlog( $row ) 176 { 177 // create a new blog 178 lt_include( PLOG_CLASS_PATH."class/dao/bloginfo.class.php" ); 179 lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" ); 180 lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" ); 181 lt_include( PLOG_CLASS_PATH."class/dao/articlecategories.class.php" ); 182 183 $blogs = new Blogs(); 184 $blog = new BlogInfo( $row["user"].$this->_blogtitle_postfix, // name of the new blog 185 $row["id"], // id of the owner 186 "", // no about 187 ""); // no properties either 188 $newBlogId = $blogs->addBlog( $blog ); 189 190 // add a default category and a default post 191 $articleCategories = new ArticleCategories(); 192 $articleCategory = new ArticleCategory( "General", "", $newBlogId, true ); 193 $catId = $articleCategories->addArticleCategory( $articleCategory ); 194 $config =& Config::getConfig(); 195 $locale =& Locales::getLocale( $config->getValue( "default_locale" )); 196 $articleTopic = $locale->tr( "register_default_article_topic" ); 197 $articleText = $locale->tr( "register_default_article_text" ); 198 $article = new Article( $articleTopic, 199 $articleText, 200 Array( $catId ), 201 $row["id"], 202 $newBlogId, 203 POST_STATUS_PUBLISHED, 204 0, 205 Array(), 206 "welcome" ); 207 $t = new Timestamp(); 208 $article->setDateObject( $t ); 209 $articles = new Articles(); 210 $articles->addArticle( $article ); 211 } 212 213 /* Gets rows from Joomla user data table and auxiliary LT table and merges 214 * them into a single userInfo object 215 * 216 * @param row Joomla user data table row, as fetched 217 * @param extraInfo Lifetype auxiliary table extra user data, as fetched 218 * @return userInfo object, as neede by LT 219 */ 220 function _mapUserInfoObject( $row, $extraInfo=false) 221 { 222 lt_include( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" ); 223 224 $plogJoomlaData = $this->getpLogJoomlaUserData( $row["id"] ); 225 226 //Data fetched from Joomla db 227 //$row["id"] = $row["id"]; //no need to map 228 $row["user"] = $row["username"]; 229 $row["password"] = $row["password"]; 230 $row["email"] = $row["email"]; 231 $row["full_name"] = $row["name"]; 232 $row["status"] = ($row["block"] != 1) ? USER_STATUS_ACTIVE : USER_STATUS_DISABLED; 233 234 //Data fetched from auxiliary table 235 //If no data fetched, these fields will be empty 236 if(!$plogJoomlaData){ 237 $row["about"] = ""; 238 $row["resource_picture_id"] = 0; 239 $row["properties"] = serialize(Array()); 240 } else { 241 $row["about"] = $plogJoomlaData["about"]; 242 $row["resource_picture_id"] = $plogJoomlaData["resource_picture_id"]; 243 if( $row["resource_picture_id"] == "" ) $plogJoomlaData["resource_picture_id"] = 0; 244 $row["properties"] = serialize($plogJoomlaData["properties"]); 245 } 246 247 // If this is the first time user data is loaded on LT, Joomla Super administrator 248 // will be an LT site admin as well. 249 // Otherwise, get site admin status from auxiliary table 250 if(!$plogJoomlaData){ 251 $row["site_admin"] = ($row["usertype"]==JOOMLA_SITE_ADMIN); 252 } else { 253 $row["site_admin"] = $plogJoomlaData["blog_site_admin"]; 254 } 255 256 // does this Joomla user have a blog yet? If so, create one if the configuration 257 // of the user data provider says so 258 $providerConfig = $this->getProviderConfiguration(); 259 if( $providerConfig->getValue( "createBlogIfNotExisting" )) { 260 $userInfo = BaseUserDataProvider::mapRow( $row, true ); 261 // check if this user is assigned to any blog 262 $userBlogs = $userInfo->getBlogs(); 263 if( empty($userBlogs )) { 264 // assign the login_perm permission 265 $this->grantLoginPermission( $userInfo ); 266 267 $this->JoomlaAddBlog( $row ); 268 $userInfo->setBlogs( $this->getUsersBlogs( $userInfo->getId())); 269 } 270 } 271 else { 272 $userInfo = BaseUserDataProvider::mapRow( $row ); 273 } 274 275 return( $userInfo ); 276 } 277 278 /** 279 * Returns an array with all the users available in the database 280 * 281 * @param status 282 * @param includeExtraInfo 283 * @param page 284 * @param itemsPerPage 285 * @return An array containing all the users. 286 */ 287 function getAllUsers( $status = USER_STATUS_ALL, $searchTerms = "", $orderBy = "", $page = -1, $itemsPerPage = DEFAULT_ITEMS_PER_PAGE ) 288 { 289 $query = "SELECT * FROM ".$this->_joomladbprefix."users ORDER BY id ASC"; 290 291 $result = $this->_dbc->Execute( $query, $page, $itemsPerPage ); 292 293 $users = Array(); 294 295 while ($info = $result->FetchRow( $result )){ 296 array_push( $users, $this->_mapUserInfoObject( $info )); 297 } 298 $result->Close(); 299 300 return $users; 301 } 302 303 /** 304 * Updates the information related to a user 305 * 306 * @param userInfo An UserInfo object containing the <b>already udpated</b> information of the 307 * user we would like to update. 308 * @return Returns true if ok or false otherwise. 309 */ 310 function updateUser( $userInfo ) 311 { 312 /* These should be accessible only from Joomla! */ 313 /* 314 $query = "UPDATE ".$this->_joomladbprefix."users ". 315 "SET username = '".Db::qstr($userInfo->getUserName()). 316 "', password = '".Db::qstr(md5($userInfo->getPassword())). 317 "', email = '".Db::qstr($userInfo->getEmail()). 318 "', block = '".Db::qstr(($userInfo->getStatus()>USER_STATUS_ACTIVE)? JOOMLA_USER_IS_BLOCKED : JOOMLA_USER_IS_ACTIVE) . 319 "' WHERE id = ".Db::qstr($userInfo->getId()); 320 321 $result = $this->_dbc->Execute( $query ); 322 323 if( !$result ){ 324 $this->log->debug("Error while updating joomla table. Query:\n".$query."\n" ); 325 return false; 326 } 327 328 BaseUserDataProvider::updateUser( $userInfo ); 329 */ 330 331 // update plog's joomla_user table 332 $result = $this->updatepLogJoomlaUserData( $userInfo ); 333 334 return( $result ); 335 } 336 337 /** 338 * Adds a user to the database. 339 * 340 * @param user An UserInfo object with the necessary information 341 * @return Returns the identifier assigned to the user, or false if there was any error. It will also modify the 342 * UserInfo object passed by parameter and set its database id. 343 */ 344 function addUser( &$user ) 345 { 346 /* User registration should be done via Joomla!/Mambo.*/ 347 } 348 349 /** 350 * @private 351 * Updates the plog-specific user data that is used when the joomla integration is enabled, since 352 * plog has some extra information that does not fit anywhere in joomla 353 * 354 * @param user A UserInfo object 355 * @return true if successful or false otherwise 356 */ 357 function updatepLogJoomlaUserData( &$user ) 358 { 359 // is the user already there? 360 if( $this->getpLogJoomlaUserData( $user->getId())) { 361 // we need to run an UPDATE query... 362 $query = "UPDATE ".$this->getPrefix().$this->_joomlaauxtable. 363 " SET about = '".Db::qstr( $user->getAboutMyself()). 364 "', properties = '".Db::qstr( serialize($user->getProperties())). 365 "', resource_picture_id = '".Db::qstr( $user->getPictureId()). 366 "', blog_site_admin = ".(Db::qstr( $user->isSiteAdmin() ? "1" : "0")). 367 " WHERE joomla_id = ".Db::qstr( $user->getId()); 368 } 369 else { 370 // we need to run an INSERT query... 371 $query = "INSERT INTO ".$this->getPrefix().$this->_joomlaauxtable."(joomla_id, about, properties, blog_site_admin, resource_picture_id) ". 372 " VALUES (". 373 Db::qstr($user->getId()).",'". 374 Db::qstr($user->getAboutMyself())."','". 375 Db::qstr(serialize($user->getProperties()))."',". 376 Db::qstr( $user->isSiteAdmin() ? "1" : "0").",'". 377 Db::qstr($user->getPictureId())."')"; 378 } 379 380 $result = $this->Execute( $query ); 381 382 return( true ); 383 } 384 385 /** 386 * @private 387 * Load the plog-specific joomla user data 388 * 389 * @param userId 390 * @return A row with the extra user data or false otherwise 391 */ 392 function getpLogJoomlaUserData( $userId ) 393 { 394 395 $query = "SELECT * FROM ".$this->getPrefix().$this->_joomlaauxtable." WHERE joomla_id = '".Db::qstr($userId)."'"; 396 $result = $this->Execute( $query ); 397 398 if( !$result ) 399 return false; 400 401 if( $result->RowCount() == 0 ){ 402 $result->Close(); 403 return false; 404 } 405 406 $ret = $result->FetchRow(); 407 $result->Close(); 408 409 return $ret; 410 } 411 412 /** 413 * Removes users from the database 414 * 415 * @param userId The identifier of the user we are trying to remove 416 */ 417 function deleteUser( $userId ) 418 { 419 } 420 421 /** 422 * returns the total number of users 423 * 424 * @return total number of users 425 */ 426 function getNumUsers( $status = USER_STATUS_ALL ) 427 { 428 // 429 // :TODO: 430 // add the status check here! 431 // 432 $query = "SELECT COUNT(id) AS total FROM ".$this->_joomladbprefix."users"; 433 434 $result = $this->_dbc->Execute( $query ); 435 436 // return no users if this doesn't work! 437 if( !$result ) 438 return 0; 439 440 $row = $result->FetchRow(); 441 $result->Close(); 442 443 if( $row["total"] == "" ) 444 $row["total"] = 0; 445 446 return( $row["total"] ); 447 } 448 449 /** 450 * check if the email account has been registered 451 * @return true if the email account has been registered 452 */ 453 function emailExists($email) 454 { 455 $query = "SELECT * FROM ".$this->_joomladbprefix."users WHERE email = '".Db::qstr($email)."'"; 456 457 $result = $this->_dbc->Execute( $query ); 458 459 if( !$result ) 460 return false; 461 $ret = ($result->RecordCount() > 0); 462 $result->Close(); 463 return $ret; 464 } 465 } 466 ?>
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 |
|