| [ Index ] |
|
Code source de XOOPS 2.0.17.1 |
1 <?php 2 // $Id: user.php 506 2006-05-26 23:10:37Z skalpa $ 3 // ------------------------------------------------------------------------ // 4 // XOOPS - PHP Content Management System // 5 // Copyright (c) 2000 XOOPS.org // 6 // <http://www.xoops.org/> // 7 // ------------------------------------------------------------------------ // 8 // This program is free software; you can redistribute it and/or modify // 9 // it under the terms of the GNU General Public License as published by // 10 // the Free Software Foundation; either version 2 of the License, or // 11 // (at your option) any later version. // 12 // // 13 // You may not change or alter any portion of this comment or credits // 14 // of supporting developers from this source code or any supporting // 15 // source code which is considered copyrighted (c) material of the // 16 // original comment or credit authors. // 17 // // 18 // This program is distributed in the hope that it will be useful, // 19 // but WITHOUT ANY WARRANTY; without even the implied warranty of // 20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 21 // GNU General Public License for more details. // 22 // // 23 // You should have received a copy of the GNU General Public License // 24 // along with this program; if not, write to the Free Software // 25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // 26 // ------------------------------------------------------------------------ // 27 // Author: Kazumi Ono (AKA onokazu) // 28 // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ // 29 // Project: The XOOPS Project // 30 // ------------------------------------------------------------------------- // 31 if (!defined('XOOPS_ROOT_PATH')) { 32 exit(); 33 } 34 /** 35 * Class for users 36 * @author Kazumi Ono <onokazu@xoops.org> 37 * @copyright copyright (c) 2000-2003 XOOPS.org 38 * @package kernel 39 */ 40 class XoopsUser extends XoopsObject 41 { 42 43 /** 44 * Array of groups that user belongs to 45 * @var array 46 * @access private 47 */ 48 var $_groups = array(); 49 /** 50 * @var bool is the user admin? 51 * @access private 52 */ 53 var $_isAdmin = null; 54 /** 55 * @var string user's rank 56 * @access private 57 */ 58 var $_rank = null; 59 /** 60 * @var bool is the user online? 61 * @access private 62 */ 63 var $_isOnline = null; 64 65 /** 66 * constructor 67 * @param array $id Array of key-value-pairs to be assigned to the user. (for backward compatibility only) 68 * @param int $id ID of the user to be loaded from the database. 69 */ 70 function XoopsUser($id = null) 71 { 72 $this->initVar('uid', XOBJ_DTYPE_INT, null, false); 73 $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, false, 60); 74 $this->initVar('uname', XOBJ_DTYPE_TXTBOX, null, true, 25); 75 $this->initVar('email', XOBJ_DTYPE_TXTBOX, null, true, 60); 76 $this->initVar('url', XOBJ_DTYPE_TXTBOX, null, false, 100); 77 $this->initVar('user_avatar', XOBJ_DTYPE_TXTBOX, null, false, 30); 78 $this->initVar('user_regdate', XOBJ_DTYPE_INT, null, false); 79 $this->initVar('user_icq', XOBJ_DTYPE_TXTBOX, null, false, 15); 80 $this->initVar('user_from', XOBJ_DTYPE_TXTBOX, null, false, 100); 81 $this->initVar('user_sig', XOBJ_DTYPE_TXTAREA, null, false, null); 82 $this->initVar('user_viewemail', XOBJ_DTYPE_INT, 0, false); 83 $this->initVar('actkey', XOBJ_DTYPE_OTHER, null, false); 84 $this->initVar('user_aim', XOBJ_DTYPE_TXTBOX, null, false, 18); 85 $this->initVar('user_yim', XOBJ_DTYPE_TXTBOX, null, false, 25); 86 $this->initVar('user_msnm', XOBJ_DTYPE_TXTBOX, null, false, 100); 87 $this->initVar('pass', XOBJ_DTYPE_TXTBOX, null, false, 32); 88 $this->initVar('posts', XOBJ_DTYPE_INT, null, false); 89 $this->initVar('attachsig', XOBJ_DTYPE_INT, 0, false); 90 $this->initVar('rank', XOBJ_DTYPE_INT, 0, false); 91 $this->initVar('level', XOBJ_DTYPE_INT, 0, false); 92 $this->initVar('theme', XOBJ_DTYPE_OTHER, null, false); 93 $this->initVar('timezone_offset', XOBJ_DTYPE_OTHER, null, false); 94 $this->initVar('last_login', XOBJ_DTYPE_INT, 0, false); 95 $this->initVar('umode', XOBJ_DTYPE_OTHER, null, false); 96 $this->initVar('uorder', XOBJ_DTYPE_INT, 1, false); 97 // RMV-NOTIFY 98 $this->initVar('notify_method', XOBJ_DTYPE_OTHER, 1, false); 99 $this->initVar('notify_mode', XOBJ_DTYPE_OTHER, 0, false); 100 $this->initVar('user_occ', XOBJ_DTYPE_TXTBOX, null, false, 100); 101 $this->initVar('bio', XOBJ_DTYPE_TXTAREA, null, false, null); 102 $this->initVar('user_intrest', XOBJ_DTYPE_TXTBOX, null, false, 150); 103 $this->initVar('user_mailok', XOBJ_DTYPE_INT, 1, false); 104 105 // for backward compatibility 106 if (isset($id)) { 107 if (is_array($id)) { 108 $this->assignVars($id); 109 } else { 110 $member_handler =& xoops_gethandler('member'); 111 $user =& $member_handler->getUser($id); 112 foreach ($user->vars as $k => $v) { 113 $this->assignVar($k, $v['value']); 114 } 115 } 116 } 117 } 118 119 /** 120 * check if the user is a guest user 121 * 122 * @return bool returns false 123 * 124 */ 125 function isGuest() 126 { 127 return false; 128 } 129 130 131 /** 132 * Updated by Catzwolf 11 Jan 2004 133 * find the username for a given ID 134 * 135 * @param int $userid ID of the user to find 136 * @param int $usereal switch for usename or realname 137 * @return string name of the user. name for "anonymous" if not found. 138 */ 139 function getUnameFromId( $userid, $usereal = 0 ) 140 { 141 $userid = intval($userid); 142 $usereal = intval($usereal); 143 if ($userid > 0) { 144 $member_handler =& xoops_gethandler('member'); 145 $user =& $member_handler->getUser($userid); 146 if (is_object($user)) { 147 $ts =& MyTextSanitizer::getInstance(); 148 if ( $usereal ) { 149 return $ts->htmlSpecialChars($user->getVar('name')); 150 } else { 151 return $ts->htmlSpecialChars($user->getVar('uname')); 152 } 153 } 154 } 155 return $GLOBALS['xoopsConfig']['anonymous']; 156 } 157 /** 158 * increase the number of posts for the user 159 * 160 * @deprecated 161 */ 162 function incrementPost(){ 163 $member_handler =& xoops_gethandler('member'); 164 return $member_handler->updateUserByField($this, 'posts', $this->getVar('posts') + 1); 165 } 166 /** 167 * set the groups for the user 168 * 169 * @param array $groupsArr Array of groups that user belongs to 170 */ 171 function setGroups($groupsArr) 172 { 173 if (is_array($groupsArr)) { 174 $this->_groups =& $groupsArr; 175 } 176 } 177 /** 178 * get the groups that the user belongs to 179 * 180 * @return array array of groups 181 */ 182 function &getGroups() 183 { 184 if (empty($this->_groups)) { 185 $member_handler =& xoops_gethandler('member'); 186 $this->_groups =& $member_handler->getGroupsByUser($this->getVar('uid')); 187 } 188 return $this->_groups; 189 } 190 /** 191 * alias for {@link getGroups()} 192 * @see getGroups() 193 * @return array array of groups 194 * @deprecated 195 */ 196 function &groups() 197 { 198 $groups =& $this->getGroups(); 199 return $groups; 200 } 201 /** 202 * Is the user admin ? 203 * 204 * This method will return true if this user has admin rights for the specified module.<br /> 205 * - If you don't specify any module ID, the current module will be checked.<br /> 206 * - If you set the module_id to -1, it will return true if the user has admin rights for at least one module 207 * 208 * @param int $module_id check if user is admin of this module 209 * @return bool is the user admin of that module? 210 */ 211 function isAdmin( $module_id = null ) { 212 if ( is_null( $module_id ) ) { 213 $module_id = isset($GLOBALS['xoopsModule']) ? $GLOBALS['xoopsModule']->getVar( 'mid', 'n' ) : 1; 214 } elseif ( intval($module_id) < 1 ) { 215 $module_id = 0; 216 } 217 $moduleperm_handler =& xoops_gethandler('groupperm'); 218 return $moduleperm_handler->checkRight('module_admin', $module_id, $this->getGroups()); 219 } 220 /** 221 * get the user's rank 222 * @return array array of rank ID and title 223 */ 224 function rank() 225 { 226 if (!isset($this->_rank)) { 227 $this->_rank = xoops_getrank($this->getVar('rank'), $this->getVar('posts')); 228 } 229 return $this->_rank; 230 } 231 /** 232 * is the user activated? 233 * @return bool 234 */ 235 function isActive() 236 { 237 if ($this->getVar('level') == 0) { 238 return false; 239 } 240 return true; 241 } 242 /** 243 * is the user currently logged in? 244 * @return bool 245 */ 246 function isOnline() 247 { 248 if (!isset($this->_isOnline)) { 249 $onlinehandler =& xoops_gethandler('online'); 250 $this->_isOnline = ($onlinehandler->getCount(new Criteria('online_uid', $this->getVar('uid'))) > 0) ? true : false; 251 } 252 return $this->_isOnline; 253 } 254 /**#@+ 255 * specialized wrapper for {@link XoopsObject::getVar()} 256 * 257 * kept for compatibility reasons. 258 * 259 * @see XoopsObject::getVar() 260 * @deprecated 261 */ 262 /** 263 * get the users UID 264 * @return int 265 */ 266 function uid() 267 { 268 return $this->getVar("uid"); 269 } 270 271 /** 272 * get the users name 273 * @param string $format format for the output, see {@link XoopsObject::getVar()} 274 * @return string 275 */ 276 function name($format="S") 277 { 278 return $this->getVar("name", $format); 279 } 280 281 /** 282 * get the user's uname 283 * @param string $format format for the output, see {@link XoopsObject::getVar()} 284 * @return string 285 */ 286 function uname($format="S") 287 { 288 return $this->getVar("uname", $format); 289 } 290 291 /** 292 * get the user's email 293 * 294 * @param string $format format for the output, see {@link XoopsObject::getVar()} 295 * @return string 296 */ 297 function email($format="S") 298 { 299 return $this->getVar("email", $format); 300 } 301 302 function url($format="S") 303 { 304 return $this->getVar("url", $format); 305 } 306 307 function user_avatar($format="S") 308 { 309 return $this->getVar("user_avatar"); 310 } 311 312 function user_regdate() 313 { 314 return $this->getVar("user_regdate"); 315 } 316 317 function user_icq($format="S") 318 { 319 return $this->getVar("user_icq", $format); 320 } 321 322 function user_from($format="S") 323 { 324 return $this->getVar("user_from", $format); 325 } 326 function user_sig($format="S") 327 { 328 return $this->getVar("user_sig", $format); 329 } 330 331 function user_viewemail() 332 { 333 return $this->getVar("user_viewemail"); 334 } 335 336 function actkey() 337 { 338 return $this->getVar("actkey"); 339 } 340 341 function user_aim($format="S") 342 { 343 return $this->getVar("user_aim", $format); 344 } 345 346 function user_yim($format="S") 347 { 348 return $this->getVar("user_yim", $format); 349 } 350 351 function user_msnm($format="S") 352 { 353 return $this->getVar("user_msnm", $format); 354 } 355 356 function pass() 357 { 358 return $this->getVar("pass"); 359 } 360 361 function posts() 362 { 363 return $this->getVar("posts"); 364 } 365 366 function attachsig() 367 { 368 return $this->getVar("attachsig"); 369 } 370 371 function level() 372 { 373 return $this->getVar("level"); 374 } 375 376 function theme() 377 { 378 return $this->getVar("theme"); 379 } 380 381 function timezone() 382 { 383 return $this->getVar("timezone_offset"); 384 } 385 386 function umode() 387 { 388 return $this->getVar("umode"); 389 } 390 391 function uorder() 392 { 393 return $this->getVar("uorder"); 394 } 395 396 // RMV-NOTIFY 397 function notify_method() 398 { 399 return $this->getVar("notify_method"); 400 } 401 402 function notify_mode() 403 { 404 return $this->getVar("notify_mode"); 405 } 406 407 function user_occ($format="S") 408 { 409 return $this->getVar("user_occ", $format); 410 } 411 412 function bio($format="S") 413 { 414 return $this->getVar("bio", $format); 415 } 416 417 function user_intrest($format="S") 418 { 419 return $this->getVar("user_intrest", $format); 420 } 421 422 function last_login() 423 { 424 return $this->getVar("last_login"); 425 } 426 /**#@-*/ 427 428 } 429 430 /** 431 * Class that represents a guest user 432 * @author Kazumi Ono <onokazu@xoops.org> 433 * @copyright copyright (c) 2000-2003 XOOPS.org 434 * @package kernel 435 */ 436 class XoopsGuestUser extends XoopsUser 437 { 438 /** 439 * check if the user is a guest user 440 * 441 * @return bool returns true 442 * 443 */ 444 function isGuest() 445 { 446 return true; 447 } 448 } 449 450 451 /** 452 * XOOPS user handler class. 453 * This class is responsible for providing data access mechanisms to the data source 454 * of XOOPS user class objects. 455 * 456 * @author Kazumi Ono <onokazu@xoops.org> 457 * @copyright copyright (c) 2000-2003 XOOPS.org 458 * @package kernel 459 */ 460 class XoopsUserHandler extends XoopsObjectHandler 461 { 462 463 /** 464 * create a new user 465 * 466 * @param bool $isNew flag the new objects as "new"? 467 * @return object XoopsUser 468 */ 469 function &create($isNew = true) 470 { 471 $user = new XoopsUser(); 472 if ($isNew) { 473 $user->setNew(); 474 } 475 return $user; 476 } 477 478 /** 479 * retrieve a user 480 * 481 * @param int $id UID of the user 482 * @return mixed reference to the {@link XoopsUser} object, FALSE if failed 483 */ 484 function &get($id) 485 { 486 $user = false; 487 if (intval($id) > 0) { 488 $sql = 'SELECT * FROM '.$this->db->prefix('users').' WHERE uid='.$id; 489 if (!$result = $this->db->query($sql)) { 490 return $user; 491 } 492 $numrows = $this->db->getRowsNum($result); 493 if ($numrows == 1) { 494 $user = new XoopsUser(); 495 $user->assignVars($this->db->fetchArray($result)); 496 } 497 } 498 return $user; 499 } 500 501 /** 502 * insert a new user in the database 503 * 504 * @param object $user reference to the {@link XoopsUser} object 505 * @param bool $force 506 * @return bool FALSE if failed, TRUE if already present and unchanged or successful 507 */ 508 function insert(&$user, $force = false) 509 { 510 if (strtolower(get_class($user)) != 'xoopsuser') { 511 return false; 512 } 513 if (!$user->isDirty()) { 514 return true; 515 } 516 if (!$user->cleanVars()) { 517 return false; 518 } 519 foreach ($user->cleanVars as $k => $v) { 520 ${$k} = $v; 521 } 522 // RMV-NOTIFY 523 // Added two fields, notify_method, notify_mode 524 if ($user->isNew()) { 525 $uid = $this->db->genId($this->db->prefix('users').'_uid_seq'); 526 $sql = sprintf("INSERT INTO %s (uid, uname, name, email, url, user_avatar, user_regdate, user_icq, user_from, user_sig, user_viewemail, actkey, user_aim, user_yim, user_msnm, pass, posts, attachsig, rank, level, theme, timezone_offset, last_login, umode, uorder, notify_method, notify_mode, user_occ, bio, user_intrest, user_mailok) VALUES (%u, %s, %s, %s, %s, %s, %u, %s, %s, %s, %u, %s, %s, %s, %s, %s, %u, %u, %u, %u, %s, %.2f, %u, %s, %u, %u, %u, %s, %s, %s, %u)", $this->db->prefix('users'), $uid, $this->db->quoteString($uname), $this->db->quoteString($name), $this->db->quoteString($email), $this->db->quoteString($url), $this->db->quoteString($user_avatar), time(), $this->db->quoteString($user_icq), $this->db->quoteString($user_from), $this->db->quoteString($user_sig), $user_viewemail, $this->db->quoteString($actkey), $this->db->quoteString($user_aim), $this->db->quoteString($user_yim), $this->db->quoteString($user_msnm), $this->db->quoteString($pass), $posts, $attachsig, $rank, $level, $this->db->quoteString($theme), $timezone_offset, 0, $this->db->quoteString($umode), $uorder, $notify_method, $notify_mode, $this->db->quoteString($user_occ), $this->db->quoteString($bio), $this->db->quoteString($user_intrest), $user_mailok); 527 } else { 528 $sql = sprintf("UPDATE %s SET uname = %s, name = %s, email = %s, url = %s, user_avatar = %s, user_icq = %s, user_from = %s, user_sig = %s, user_viewemail = %u, user_aim = %s, user_yim = %s, user_msnm = %s, posts = %d, pass = %s, attachsig = %u, rank = %u, level= %u, theme = %s, timezone_offset = %.2f, umode = %s, last_login = %u, uorder = %u, notify_method = %u, notify_mode = %u, user_occ = %s, bio = %s, user_intrest = %s, user_mailok = %u WHERE uid = %u", $this->db->prefix('users'), $this->db->quoteString($uname), $this->db->quoteString($name), $this->db->quoteString($email), $this->db->quoteString($url), $this->db->quoteString($user_avatar), $this->db->quoteString($user_icq), $this->db->quoteString($user_from), $this->db->quoteString($user_sig), $user_viewemail, $this->db->quoteString($user_aim), $this->db->quoteString($user_yim), $this->db->quoteString($user_msnm), $posts, $this->db->quoteString($pass), $attachsig, $rank, $level, $this->db->quoteString($theme), $timezone_offset, $this->db->quoteString($umode), $last_login, $uorder, $notify_method, $notify_mode, $this->db->quoteString($user_occ), $this->db->quoteString($bio), $this->db->quoteString($user_intrest), $user_mailok, $uid); 529 } 530 if (false != $force) { 531 $result = $this->db->queryF($sql); 532 } else { 533 $result = $this->db->query($sql); 534 } 535 if (!$result) { 536 return false; 537 } 538 if (empty($uid)) { 539 $uid = $this->db->getInsertId(); 540 } 541 $user->assignVar('uid', $uid); 542 return true; 543 } 544 545 /** 546 * delete a user from the database 547 * 548 * @param object $user reference to the user to delete 549 * @param bool $force 550 * @return bool FALSE if failed. 551 */ 552 function delete(&$user, $force = false) 553 { 554 if (strtolower(get_class($user)) != 'xoopsuser') { 555 return false; 556 } 557 $sql = sprintf("DELETE FROM %s WHERE uid = %u", $this->db->prefix("users"), $user->getVar('uid')); 558 if (false != $force) { 559 $result = $this->db->queryF($sql); 560 } else { 561 $result = $this->db->query($sql); 562 } 563 if (!$result) { 564 return false; 565 } 566 return true; 567 } 568 569 /** 570 * retrieve users from the database 571 * 572 * @param object $criteria {@link CriteriaElement} conditions to be met 573 * @param bool $id_as_key use the UID as key for the array? 574 * @return array array of {@link XoopsUser} objects 575 */ 576 function getObjects($criteria = null, $id_as_key = false) 577 { 578 $ret = array(); 579 $limit = $start = 0; 580 $sql = 'SELECT * FROM '.$this->db->prefix('users'); 581 if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { 582 $sql .= ' '.$criteria->renderWhere(); 583 if ($criteria->getSort() != '') { 584 $sql .= ' ORDER BY '.$criteria->getSort().' '.$criteria->getOrder(); 585 } 586 $limit = $criteria->getLimit(); 587 $start = $criteria->getStart(); 588 } 589 $result = $this->db->query($sql, $limit, $start); 590 if (!$result) { 591 return $ret; 592 } 593 while ($myrow = $this->db->fetchArray($result)) { 594 $user = new XoopsUser(); 595 $user->assignVars($myrow); 596 if (!$id_as_key) { 597 $ret[] =& $user; 598 } else { 599 $ret[$myrow['uid']] =& $user; 600 } 601 unset($user); 602 } 603 return $ret; 604 } 605 606 /** 607 * count users matching a condition 608 * 609 * @param object $criteria {@link CriteriaElement} to match 610 * @return int count of users 611 */ 612 function getCount($criteria = null) 613 { 614 $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('users'); 615 if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { 616 $sql .= ' '.$criteria->renderWhere(); 617 } 618 $result = $this->db->query($sql); 619 if (!$result) { 620 return 0; 621 } 622 list($count) = $this->db->fetchRow($result); 623 return $count; 624 } 625 626 /** 627 * delete users matching a set of conditions 628 * 629 * @param object $criteria {@link CriteriaElement} 630 * @return bool FALSE if deletion failed 631 */ 632 function deleteAll($criteria = null) 633 { 634 $sql = 'DELETE FROM '.$this->db->prefix('users'); 635 if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { 636 $sql .= ' '.$criteria->renderWhere(); 637 } 638 if (!$result = $this->db->query($sql)) { 639 return false; 640 } 641 return true; 642 } 643 644 /** 645 * Change a value for users with a certain criteria 646 * 647 * @param string $fieldname Name of the field 648 * @param string $fieldvalue Value to write 649 * @param object $criteria {@link CriteriaElement} 650 * 651 * @return bool 652 **/ 653 function updateAll($fieldname, $fieldvalue, $criteria = null) 654 { 655 $set_clause = is_numeric($fieldvalue) ? $fieldname.' = '.$fieldvalue : $fieldname.' = '.$this->db->quoteString($fieldvalue); 656 $sql = 'UPDATE '.$this->db->prefix('users').' SET '.$set_clause; 657 if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { 658 $sql .= ' '.$criteria->renderWhere(); 659 } 660 if (!$result = $this->db->query($sql)) { 661 return false; 662 } 663 return true; 664 } 665 } 666 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Nov 25 11:44:32 2007 | par Balluche grâce à PHPXref 0.7 |
|