PageRenderTime 62ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/XoopsEngine/htdocs/kernel/user.php

https://github.com/xoops-pi/legacy
PHP | 461 lines | 271 code | 40 blank | 150 comment | 18 complexity | 1d834d0c8d517810c033d86d46c23b34 MD5 | raw file
  1. <?php
  2. /**
  3. * Xoops Engine User Object
  4. *
  5. * You may not change or alter any portion of this comment or credits
  6. * of supporting developers from this source code or any supporting source code
  7. * which is considered copyrighted (c) material of the original comment or credit authors.
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * @copyright The XOOPS Project http://sourceforge.net/projects/xoops/
  13. * @license http://www.fsf.org/copyleft/gpl.html GNU public license
  14. * @package kernel
  15. * @since 2.0
  16. * @author Taiwen Jiang <phppp@users.sourceforge.net>
  17. * @author Kazumi Ono (AKA onokazu>
  18. * @version $Id$
  19. */
  20. if (!defined('XOOPS_ROOT_PATH')) {
  21. exit();
  22. }
  23. class XoopsUser extends XoopsObject
  24. {
  25. /**
  26. * Array of groups that user belongs to
  27. * @var array
  28. * @access private
  29. */
  30. var $_groups = array();
  31. /**
  32. * @var bool is the user admin?
  33. * @access private
  34. */
  35. var $_isAdmin = null;
  36. /**
  37. * @var string user's rank
  38. * @access private
  39. */
  40. var $_rank = null;
  41. /**
  42. * @var bool is the user online?
  43. * @access private
  44. */
  45. var $_isOnline = null;
  46. /**
  47. * constructor
  48. * @param array $id Array of key-value-pairs to be assigned to the user. (for backward compatibility only)
  49. * @param int $id ID of the user to be loaded from the database.
  50. */
  51. function XoopsUser($id = null)
  52. {
  53. $this->initVar('uid', XOBJ_DTYPE_INT, null, false);
  54. $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, false, 60);
  55. $this->initVar('uname', XOBJ_DTYPE_TXTBOX, null, true, 25);
  56. $this->initVar('email', XOBJ_DTYPE_TXTBOX, null, true, 60);
  57. $this->initVar('url', XOBJ_DTYPE_TXTBOX, null, false, 100);
  58. $this->initVar('user_avatar', XOBJ_DTYPE_TXTBOX, null, false, 30);
  59. $this->initVar('user_regdate', XOBJ_DTYPE_INT, null, false);
  60. $this->initVar('user_icq', XOBJ_DTYPE_TXTBOX, null, false, 15);
  61. $this->initVar('user_from', XOBJ_DTYPE_TXTBOX, null, false, 100);
  62. $this->initVar('user_sig', XOBJ_DTYPE_TXTAREA, null, false, null);
  63. $this->initVar('user_viewemail', XOBJ_DTYPE_INT, 0, false);
  64. $this->initVar('actkey', XOBJ_DTYPE_OTHER, null, false);
  65. $this->initVar('user_aim', XOBJ_DTYPE_TXTBOX, null, false, 18);
  66. $this->initVar('user_yim', XOBJ_DTYPE_TXTBOX, null, false, 25);
  67. $this->initVar('user_msnm', XOBJ_DTYPE_TXTBOX, null, false, 100);
  68. $this->initVar('pass', XOBJ_DTYPE_TXTBOX, null, false, 32);
  69. $this->initVar('posts', XOBJ_DTYPE_INT, null, false);
  70. $this->initVar('attachsig', XOBJ_DTYPE_INT, 0, false);
  71. $this->initVar('rank', XOBJ_DTYPE_INT, 0, false);
  72. $this->initVar('level', XOBJ_DTYPE_INT, 0, false);
  73. $this->initVar('theme', XOBJ_DTYPE_OTHER, null, false);
  74. $this->initVar('timezone_offset', XOBJ_DTYPE_OTHER, '0.0', false);
  75. $this->initVar('last_login', XOBJ_DTYPE_INT, 0, false);
  76. $this->initVar('umode', XOBJ_DTYPE_OTHER, null, false);
  77. $this->initVar('uorder', XOBJ_DTYPE_INT, 1, false);
  78. // RMV-NOTIFY
  79. $this->initVar('notify_method', XOBJ_DTYPE_OTHER, 1, false);
  80. $this->initVar('notify_mode', XOBJ_DTYPE_OTHER, 0, false);
  81. $this->initVar('user_occ', XOBJ_DTYPE_TXTBOX, null, false, 100);
  82. $this->initVar('bio', XOBJ_DTYPE_TXTAREA, null, false, null);
  83. $this->initVar('user_intrest', XOBJ_DTYPE_TXTBOX, null, false, 150);
  84. $this->initVar('user_mailok', XOBJ_DTYPE_INT, 1, false);
  85. // for backward compatibility
  86. if (isset($id)) {
  87. if (is_array($id)) {
  88. $this->assignVars($id);
  89. } else {
  90. $member_handler =& xoops_gethandler('member');
  91. $user =& $member_handler->getUser($id);
  92. foreach ($user->vars as $k => $v) {
  93. $this->assignVar($k, $v['value']);
  94. }
  95. }
  96. }
  97. }
  98. /**
  99. * check if the user is a guest user
  100. *
  101. * @return bool returns false
  102. *
  103. */
  104. function isGuest()
  105. {
  106. return false;
  107. }
  108. /**
  109. * Updated by Catzwolf 11 Jan 2004
  110. * find the username for a given ID
  111. *
  112. * @param int $userid ID of the user to find
  113. * @param int $usereal switch for usename or realname
  114. * @return string name of the user. name for "anonymous" if not found.
  115. */
  116. function getUnameFromId( $userid, $usereal = 0 )
  117. {
  118. $userid = intval($userid);
  119. $usereal = intval($usereal);
  120. if ($userid > 0) {
  121. $member_handler =& xoops_gethandler('member');
  122. $user = $member_handler->getUser($userid);
  123. if (is_object($user)) {
  124. $ts = MyTextSanitizer::getInstance();
  125. if ( $usereal ) {
  126. $name = $user->getVar('name');
  127. if($name != '') {
  128. return $ts->htmlSpecialChars($name);
  129. } else {
  130. return $ts->htmlSpecialChars($user->getVar('uname'));
  131. }
  132. } else {
  133. return $ts->htmlSpecialChars($user->getVar('uname'));
  134. }
  135. }
  136. }
  137. return $GLOBALS['xoopsConfig']['anonymous'];
  138. }
  139. /**
  140. * increase the number of posts for the user
  141. *
  142. * @deprecated
  143. */
  144. function incrementPost(){
  145. $userModel = XOOPS::getModel("user");
  146. $profile = $userModel->findRow($this->getVar('uid'))->profile();
  147. $profile->posts ++;
  148. return $profile->save();
  149. $member_handler =& xoops_gethandler('member');
  150. return $member_handler->updateUserByField($this, 'posts', $this->getVar('posts') + 1);
  151. }
  152. /**
  153. * set the groups for the user
  154. *
  155. * @param array $groupsArr Array of groups that user belongs to
  156. */
  157. function setGroups($groupsArr)
  158. {
  159. if (is_array($groupsArr)) {
  160. $this->_groups =& $groupsArr;
  161. }
  162. }
  163. /**
  164. * get the groups that the user belongs to
  165. *
  166. * @return array array of groups
  167. */
  168. function &getGroups()
  169. {
  170. if (empty($this->_groups)) {
  171. $member_handler =& xoops_gethandler('member');
  172. $this->_groups = $member_handler->getGroupsByUser($this->getVar('uid'));
  173. }
  174. return $this->_groups;
  175. }
  176. /**
  177. * alias for {@link getGroups()}
  178. * @see getGroups()
  179. * @return array array of groups
  180. * @deprecated
  181. */
  182. function &groups()
  183. {
  184. $groups = $this->getGroups();
  185. return $groups;
  186. }
  187. /**
  188. * Is the user admin ?
  189. *
  190. * This method will return true if this user has admin rights for the specified module.<br />
  191. * - If you don't specify any module ID, the current module will be checked.<br />
  192. * - If you set the module_id to -1, it will return true if the user has admin rights for at least one module
  193. *
  194. * @param int $module_id check if user is admin of this module
  195. * @return bool is the user admin of that module?
  196. */
  197. function isAdmin( $module_id = null ) {
  198. if ( is_null( $module_id ) ) {
  199. $module_id = isset($GLOBALS['xoopsModule']) ? $GLOBALS['xoopsModule']->getVar( 'mid', 'n' ) : 1;
  200. } elseif ( intval($module_id) < 1 ) {
  201. $module_id = 0;
  202. }
  203. $moduleperm_handler =& xoops_gethandler('groupperm');
  204. return $moduleperm_handler->checkRight('module_admin', $module_id, $this->getGroups());
  205. }
  206. /**
  207. * get the user's rank
  208. * @return array array of rank ID and title
  209. */
  210. function rank()
  211. {
  212. if (!isset($this->_rank)) {
  213. $this->_rank = xoops_getrank($this->getVar('rank'), $this->getVar('posts'));
  214. }
  215. return $this->_rank;
  216. }
  217. /**
  218. * is the user activated?
  219. * @return bool
  220. */
  221. function isActive()
  222. {
  223. if ($this->getVar('level') == 0) {
  224. return false;
  225. }
  226. return true;
  227. }
  228. /**
  229. * is the user currently logged in?
  230. * @return bool
  231. */
  232. function isOnline()
  233. {
  234. if (!isset($this->_isOnline)) {
  235. $onlinehandler =& xoops_gethandler('online');
  236. $this->_isOnline = ($onlinehandler->getCount(new Criteria('online_uid', $this->getVar('uid'))) > 0) ? true : false;
  237. }
  238. return $this->_isOnline;
  239. }
  240. /**#@+
  241. * specialized wrapper for {@link XoopsObject::getVar()}
  242. *
  243. * kept for compatibility reasons.
  244. *
  245. * @see XoopsObject::getVar()
  246. * @deprecated
  247. */
  248. /**
  249. * get the users UID
  250. * @return int
  251. */
  252. function uid()
  253. {
  254. return $this->getVar("uid");
  255. }
  256. /**
  257. * get the users name
  258. * @param string $format format for the output, see {@link XoopsObject::getVar()}
  259. * @return string
  260. */
  261. function name($format="S")
  262. {
  263. return $this->getVar("name", $format);
  264. }
  265. /**
  266. * get the user's uname
  267. * @param string $format format for the output, see {@link XoopsObject::getVar()}
  268. * @return string
  269. */
  270. function uname($format="S")
  271. {
  272. return $this->getVar("uname", $format);
  273. }
  274. /**
  275. * get the user's email
  276. *
  277. * @param string $format format for the output, see {@link XoopsObject::getVar()}
  278. * @return string
  279. */
  280. function email($format="S")
  281. {
  282. return $this->getVar("email", $format);
  283. }
  284. function url($format="S")
  285. {
  286. return $this->getVar("url", $format);
  287. }
  288. function user_avatar($format="S")
  289. {
  290. return $this->getVar("user_avatar");
  291. }
  292. function user_regdate()
  293. {
  294. return $this->getVar("user_regdate");
  295. }
  296. function user_icq($format="S")
  297. {
  298. return $this->getVar("user_icq", $format);
  299. }
  300. function user_from($format="S")
  301. {
  302. return $this->getVar("user_from", $format);
  303. }
  304. function user_sig($format="S")
  305. {
  306. return $this->getVar("user_sig", $format);
  307. }
  308. function user_viewemail()
  309. {
  310. return $this->getVar("user_viewemail");
  311. }
  312. function actkey()
  313. {
  314. return $this->getVar("actkey");
  315. }
  316. function user_aim($format="S")
  317. {
  318. return $this->getVar("user_aim", $format);
  319. }
  320. function user_yim($format="S")
  321. {
  322. return $this->getVar("user_yim", $format);
  323. }
  324. function user_msnm($format="S")
  325. {
  326. return $this->getVar("user_msnm", $format);
  327. }
  328. function pass()
  329. {
  330. return $this->getVar("pass");
  331. }
  332. function posts()
  333. {
  334. return $this->getVar("posts");
  335. }
  336. function attachsig()
  337. {
  338. return $this->getVar("attachsig");
  339. }
  340. function level()
  341. {
  342. return $this->getVar("level");
  343. }
  344. function theme()
  345. {
  346. return $this->getVar("theme");
  347. }
  348. function timezone()
  349. {
  350. return $this->getVar("timezone_offset");
  351. }
  352. function umode()
  353. {
  354. return $this->getVar("umode");
  355. }
  356. function uorder()
  357. {
  358. return $this->getVar("uorder");
  359. }
  360. // RMV-NOTIFY
  361. function notify_method()
  362. {
  363. return $this->getVar("notify_method");
  364. }
  365. function notify_mode()
  366. {
  367. return $this->getVar("notify_mode");
  368. }
  369. function user_occ($format="S")
  370. {
  371. return $this->getVar("user_occ", $format);
  372. }
  373. function bio($format="S")
  374. {
  375. return $this->getVar("bio", $format);
  376. }
  377. function user_intrest($format="S")
  378. {
  379. return $this->getVar("user_intrest", $format);
  380. }
  381. function last_login()
  382. {
  383. return $this->getVar("last_login");
  384. }
  385. /**#@-*/
  386. }
  387. /**
  388. * Class that represents a guest user
  389. * @author Kazumi Ono <onokazu@xoops.org>
  390. * @copyright copyright (c) 2000-2003 XOOPS.org
  391. * @package kernel
  392. */
  393. class XoopsGuestUser extends XoopsUser
  394. {
  395. /**
  396. * check if the user is a guest user
  397. *
  398. * @return bool returns true
  399. *
  400. */
  401. function isGuest()
  402. {
  403. return true;
  404. }
  405. }
  406. /**
  407. * XOOPS user handler class.
  408. * This class is responsible for providing data access mechanisms to the data source
  409. * of XOOPS user class objects.
  410. *
  411. * @author Kazumi Ono <onokazu@xoops.org>
  412. * @author Taiwen Jiang <phppp@users.sourceforge.net>
  413. * @package kernel
  414. */
  415. class XoopsUserHandler extends XoopsPersistableObjectHandler
  416. {
  417. function __construct(&$db)
  418. {
  419. parent::__construct($db, "users", 'XoopsUser', "uid", "uname");
  420. }
  421. }