返回列表 发帖
查看: 1275|回复: 2

Discuz x3.5 核心文件 function/function_member.php 函数注释

4

主题

52

回帖

86

积分

应用开发者

贡献
0 点
金币
27 个
QQ
发表于 2024-10-28 21:34:20 | 查看全部 |阅读模式
  1. <?php

  2. /**
  3. *      [Discuz!] (C)2001-2099 Comsenz Inc.
  4. *      This is NOT a freeware, use is subject to license terms
  5. *
  6. *      $Id: function_member.php 35030 2014-10-23 07:43:23Z laoguozhang $
  7. */

  8. if(!defined('IN_DISCUZ')) {
  9.         exit('Access Denied');
  10. }
  11. /**
  12. * 用户登录函数
  13. *
  14. * @param string $username 用户名、UID、邮箱或安全手机号
  15. * @param string $password 用户密码
  16. * @param int $questionid 安全问题ID(暂未使用)
  17. * @param string $answer 安全问题答案(暂未使用)
  18. * @param string $loginfield 登录方式标识,默认为'username',可选'uid'、'email'、'auto'、'secmobile'
  19. * @param string $ip 用户登录的IP地址,默认为空
  20. * [url=home.php?mod=space&uid=33239]@return[/url] array 返回登录结果,包括状态(status)、用户信息(member)和UC登录结果(ucresult)
  21. */
  22. function userlogin($username, $password, $questionid, $answer, $loginfield = 'username', $ip = '') {
  23.     $return = array();

  24.     // 根据登录字段确定登录类型
  25.     if($loginfield == 'uid' && getglobal('setting/uidlogin')) {
  26.         $isuid = 1;
  27.     } elseif($loginfield == 'email') {
  28.         $isuid = 2;
  29.     } elseif($loginfield == 'auto') {
  30.         $isuid = 3;
  31.     } elseif($loginfield == 'secmobile' && getglobal('setting/secmobilelogin')) {
  32.         $isuid = 4;
  33.     } else {
  34.         $isuid = 0;
  35.     }

  36.     // 加载UCenter通信函数,如未定义则先加载
  37.     if(!function_exists('uc_user_login')) {
  38.         loaducenter();
  39.     }

  40.     // 处理自动登录逻辑
  41.     if($isuid == 3) {
  42.         // 根据用户名尝试登录,支持UID、邮箱和安全手机号
  43.         if(!strcmp(dintval($username), $username) && getglobal('setting/uidlogin')) {
  44.             $return['ucresult'] = uc_user_login($username, $password, 1, 1, $questionid, $answer, $ip, 1);
  45.         } elseif(isemail($username)) {
  46.             $return['ucresult'] = uc_user_login($username, $password, 2, 1, $questionid, $answer, $ip, 1);
  47.         } elseif(preg_match('/^(\d{1,12}|\d{1,3}-\d{1,12})$/', $username) && getglobal('setting/secmobilelogin')) {
  48.             $username = strpos($username, '-') === false ? (getglobal('setting/smsdefaultcc') . '-' . $username) : $username;
  49.             $return['ucresult'] = uc_user_login($username, $password, 4, 1, $questionid, $answer, $ip, 1);
  50.         }
  51.         // 如果登录失败且不是因为账户不存在,则尝试使用用户名密码登录
  52.         if($return['ucresult'][0] <= 0 && $return['ucresult'][0] != -3) {
  53.             $return['ucresult'] = uc_user_login(addslashes($username), $password, 0, 1, $questionid, $answer, $ip);
  54.         }
  55.     } else {
  56.         // 处理非自动登录类型
  57.         if($isuid == 4) {
  58.             $username = strpos($username, '-') === false ? (getglobal('setting/smsdefaultcc') . '-' . $username) : $username;
  59.         }
  60.         $return['ucresult'] = uc_user_login(addslashes($username), $password, $isuid, 1, $questionid, $answer, $ip);
  61.     }

  62.     // 解析UC登录结果
  63.     $tmp = array();
  64.     $duplicate = '';
  65.     list($tmp['uid'], $tmp['username'], $tmp['password'], $tmp['email'], $duplicate) = $return['ucresult'];
  66.     $return['ucresult'] = $tmp;

  67.     // 检查登录结果,登录失败或用户数据重复则返回
  68.     if($duplicate && $return['ucresult']['uid'] > 0 || $return['ucresult']['uid'] <= 0) {
  69.         $return['status'] = 0;
  70.         return $return;
  71.     }

  72.     // 获取用户详细信息
  73.     $member = getuserbyuid($return['ucresult']['uid'], 1);
  74.     if(!$member || empty($member['uid'])) {
  75.         $return['status'] = -1;
  76.         return $return;
  77.     }
  78.     $return['member'] = $member;

  79.     // 登录成功
  80.     $return['status'] = 1;
  81.     if($member['_inarchive']) {
  82.         // 如果用户是归档用户,则将其移回主表
  83.         C::t('common_member_archive')->move_to_master($member['uid']);
  84.     }
  85.     // 更新用户邮箱,解决可能的邮箱变更问题
  86.     if($member['email'] != $return['ucresult']['email']) {
  87.         C::t('common_member')->update($return['ucresult']['uid'], array('email' => $return['ucresult']['email']));
  88.     }

  89.     return $return;
  90. }
  91. /**
  92. * 设置登录状态
  93. *
  94. * 用于在用户成功登录后,设置用户的登录状态,包括但不限于用户ID、用户名、管理员等级、用户组等信息,
  95. * 同时更新会话信息、设置登录相关的cookie,以及更新统计信息。
  96. *
  97. * @param array $member 包含用户登录信息的数组,至少应包含uid、username、adminid、groupid等字段
  98. * @param int $cookietime 登录cookie的有效时间,单位为秒
  99. */
  100. function setloginstatus($member, $cookietime) {
  101.     global $_G;
  102.     $_G['uid'] = intval($member['uid']);
  103.     $_G['username'] = $member['username'];
  104.     $_G['adminid'] = $member['adminid'];
  105.     $_G['groupid'] = $member['groupid'];
  106.     $_G['formhash'] = formhash();
  107.     $_G['session']['invisible'] = getuserprofile('invisible');
  108.     $_G['member'] = $member;
  109.     loadcache('usergroup_'.$_G['groupid']);
  110.     C::app()->session->isnew = true;
  111.     C::app()->session->updatesession();

  112.     // 设置登录认证cookie
  113.     dsetcookie('auth', authcode("{$member['password']}\t{$member['uid']}", 'ENCODE'), $cookietime, 1, true);
  114.     dsetcookie('loginuser');
  115.     dsetcookie('activationauth');
  116.     dsetcookie('pmnum');

  117.     // 更新登录统计信息
  118.     include_once libfile('function/stat');
  119.     updatestat('login', 1);
  120.     if(defined('IN_MOBILE')) {
  121.         updatestat('mobilelogin', 1);
  122.     }
  123.     if($_G['setting']['connect']['allow'] && $_G['member']['conisbind']) {
  124.         updatestat('connectlogin', 1);
  125.     }
  126.     // 更新用户积分
  127.     $rule = updatecreditbyaction('daylogin', $_G['uid']);
  128.     if(!$rule['updatecredit']) {
  129.         checkusergroup($_G['uid']);
  130.     }
  131. }

  132. /**
  133. * 登录检查
  134. *
  135. * 用于检查用户登录名是否可用,如果是,则返回可以登录的标志;如果不可用,根据失败次数返回相应的延迟时间。
  136. *
  137. * @param string $username 用户输入的用户名
  138. * @return int 返回值为0表示可以登录,大于0表示需要等待的时间(秒)
  139. */
  140. function logincheck($username) {
  141.     global $_G;

  142.     $return = 0;
  143.     $username = trim($username);
  144.     loaducenter();
  145.     if(function_exists('uc_user_logincheck')) {
  146.         // 如果存在与UCenter的登录检查函数,则调用UCenter的登录检查
  147.         $return = uc_user_logincheck(addslashes($username), $_G['clientip']);
  148.     } else {
  149.         // 不存在时,进行本地登录检查
  150.         $login = C::t('common_failedlogin')->fetch_ip($_G['clientip']);
  151.         $return = (!$login || (TIMESTAMP - $login['lastupdate'] > 900)) ? 5 : max(0, 5 - $login['count']);

  152.         if(!$login) {
  153.             C::t('common_failedlogin')->insert(array(
  154.                 'ip' => $_G['clientip'],
  155.                 'count' => 0,
  156.                 'lastupdate' => TIMESTAMP
  157.             ), false, true);
  158.         } elseif(TIMESTAMP - $login['lastupdate'] > 900) {
  159.             C::t('common_failedlogin')->insert(array(
  160.                 'ip' => $_G['clientip'],
  161.                 'count' => 0,
  162.                 'lastupdate' => TIMESTAMP
  163.             ), false, true);
  164.             C::t('common_failedlogin')->delete_old(901);
  165.         }
  166.     }
  167.     return $return;
  168. }

  169. /**
  170. * 登录失败处理
  171. *
  172. * 当用户登录失败时,记录登录失败信息,防止恶意登录攻击。
  173. *
  174. * @param string $username 用户输入的用户名
  175. */
  176. function loginfailed($username) {
  177.     global $_G;

  178.     loaducenter();
  179.     if(function_exists('uc_user_logincheck')) {
  180.         // 如果存在与UCenter的登录检查函数,则不进行处理
  181.         return;
  182.     }
  183.     // 记录登录失败信息
  184.     C::t('common_failedlogin')->update_failed($_G['clientip']);
  185. }
  186. /**
  187. * 检查IP尝试次数是否超过限制。
  188. *
  189. * @param $numiptry int 尝试次数限制。
  190. * @param $timeiptry int 时间限制(秒)。超过此时间限制,尝试次数将被重置。
  191. * @return bool 如果尝试次数超过限制,返回true;否则,返回false。
  192. */
  193. function failedipcheck($numiptry, $timeiptry) {
  194.         global $_G;
  195.         if(!$numiptry) {
  196.                 return false;
  197.         }
  198.         // 检查当前IP在指定时间内尝试的次数是否已达到或超过限制
  199.         return $numiptry <= C::t('common_failedip')->get_ip_count($_G['clientip'], TIMESTAMP - $timeiptry);
  200. }

  201. /**
  202. * 记录一个失败的IP尝试。
  203. */
  204. function failedip() {
  205.         global $_G;
  206.         // 插入当前IP到失败尝试表中
  207.         C::t('common_failedip')->insert_ip($_G['clientip']);
  208. }

  209. /**
  210. * 获取邀请码信息。
  211. *
  212. * @return array 包含邀请码相关用户信息的数组,如果不存在有效邀请码则返回空数组。
  213. */
  214. function getinvite() {
  215.         global $_G;

  216.         // 如果注册功能关闭,则直接返回空数组
  217.         if($_G['setting']['regstatus'] == 1) return array();
  218.         $result = array();
  219.         $cookies = empty($_G['cookie']['invite_auth']) ? array() : explode(',', $_G['cookie']['invite_auth']);
  220.         $cookiecount = count($cookies);

  221.         // 处理通过URL传入的邀请码
  222.         $_GET['invitecode'] = trim($_GET['invitecode']);
  223.         if($cookiecount == 2 || $_GET['invitecode']) {
  224.                 $id = intval($cookies[0]);
  225.                 $code = trim($cookies[1]);
  226.                 if($_GET['invitecode']) {
  227.                         // 通过邀请码查询邀请信息
  228.                         $invite = C::t('common_invite')->fetch_by_code($_GET['invitecode']);
  229.                         $code = trim($_GET['invitecode']);
  230.                 } else {
  231.                         // 通过ID查询邀请信息
  232.                         $invite = C::t('common_invite')->fetch($id);
  233.                 }
  234.                 // 验证邀请信息的有效性
  235.                 if(!empty($invite)) {
  236.                         if($invite['code'] == $code && empty($invite['fuid']) && (empty($invite['endtime']) || $_G['timestamp'] < $invite['endtime'])) {
  237.                                 $result['uid'] = $invite['uid'];
  238.                                 $result['id'] = $invite['id'];
  239.                         }
  240.                 }
  241.         } elseif($cookiecount == 3) { // 处理通过cookie传入的邀请码
  242.                 $uid = intval($cookies[0]);
  243.                 $code = trim($cookies[1]);

  244.                 $invite_code = helper_invite::generate_key($uid);
  245.                 if($code === $invite_code) {
  246.                         $member = getuserbyuid($uid);
  247.                         if($member) {
  248.                                 $usergroup = C::t('common_usergroup')->fetch($member['groupid']);
  249.                                 // 如果用户组不允许邀请或邀请需要付费,则返回空数组
  250.                                 if(!$usergroup['allowinvite'] || $usergroup['inviteprice'] > 0) return array();
  251.                         } else {
  252.                                 return array();
  253.                         }
  254.                         $result['uid'] = $uid;
  255.                 }
  256.         }

  257.         // 如果获取到有效的邀请信息,填充邀请者用户名
  258.         if($result['uid']) {
  259.                 $member = getuserbyuid($result['uid']);
  260.                 $result['username'] = $member['username'];
  261.         } else {
  262.                 // 如果没有有效的邀请信息,清除邀请cookie
  263.                 dsetcookie('invite_auth', '');
  264.         }

  265.         return $result;
  266. }
  267. /**
  268. * 替换字符串中的站点变量
  269. *
  270. * @param string $string 需要替换的字符串
  271. * @param array $replaces 用户自定义的替换数组,默认为空数组
  272. * @return string 替换后的字符串
  273. */
  274. function replacesitevar($string, $replaces = array()) {
  275.         global $_G;
  276.         // 定义站点变量
  277.         $sitevars = array(
  278.                 '{sitename}' => $_G['setting']['sitename'],
  279.                 '{bbname}' => $_G['setting']['bbname'],
  280.                 '{time}' => dgmdate(TIMESTAMP, 'Y-n-j H:i'),
  281.                 '{adminemail}' => $_G['setting']['adminemail'],
  282.                 '{username}' => $_G['member']['username'],
  283.                 '{myname}' => $_G['member']['username']
  284.         );
  285.         // 合并用户自定义替换数组和站点变量
  286.         $replaces = array_merge($sitevars, $replaces);
  287.         // 替换字符串并返回
  288.         return str_replace(array_keys($replaces), array_values($replaces), $string);
  289. }

  290. /**
  291. * 清除用户cookie
  292. */
  293. function clearcookies() {
  294.         global $_G;
  295.         // 遍历cookie,除去特定的键值,其余全部清除
  296.         foreach($_G['cookie'] as $k => $v) {
  297.                 if($k != 'widthauto') {
  298.                         dsetcookie($k);
  299.                 }
  300.         }
  301.         // 重置用户登录状态
  302.         $_G['uid'] = $_G['adminid'] = 0;
  303.         $_G['username'] = $_G['member']['password'] = '';
  304. }

  305. /**
  306. * 处理犯罪记录相关操作
  307. *
  308. * @param string $fun 要执行的操作
  309. * @return mixed 操作结果,失败返回false
  310. */
  311. function crime($fun) {
  312.         if(!$fun) {
  313.                 return false;
  314.         }
  315.         include_once libfile('class/member');
  316.         $crimerecord = & crime_action_ctl::instance();
  317.         $arg_list = func_get_args();
  318.         // 根据传入的函数名执行不同的操作
  319.         if($fun == 'recordaction') {
  320.                 list(, $uid, $action, $reason) = $arg_list;
  321.                 return $crimerecord->$fun($uid, $action, $reason);
  322.         } elseif($fun == 'getactionlist') {
  323.                 list(, $uid) = $arg_list;
  324.                 return $crimerecord->$fun($uid);
  325.         } elseif($fun == 'getcount') {
  326.                 list(, $uid, $action) = $arg_list;
  327.                 return $crimerecord->$fun($uid, $action);
  328.         } elseif($fun == 'search') {
  329.                 list(, $action, $username, $operator, $starttime, $endtime, $reason, $start, $limit) = $arg_list;
  330.                 return $crimerecord->$fun($action, $username, $operator, $starttime, $endtime, $reason, $start, $limit);
  331.         } elseif($fun == 'actions') {
  332.                 return crime_action_ctl::$actions;
  333.         }
  334.         return false;
  335. }

  336. /**
  337. * 检查并更新关注的动态
  338. */
  339. function checkfollowfeed() {
  340.         global $_G;

  341.         if($_G['uid']) {
  342.                 $lastcheckfeed = 0;
  343.                 if(!empty($_G['cookie']['lastcheckfeed'])) {
  344.                         $time = explode('|', $_G['cookie']['lastcheckfeed']);
  345.                         if($time[0] == $_G['uid']) {
  346.                                 $lastcheckfeed = $time[1];
  347.                         }
  348.                 }
  349.                 if(!$lastcheckfeed) {
  350.                         $lastcheckfeed = getuserprofile('lastactivity');
  351.                 }
  352.                 // 设置最后一次检查动态的时间
  353.                 dsetcookie('lastcheckfeed', $_G['uid'].'|'.TIMESTAMP, 31536000);
  354.                 // 获取关注的用户
  355.                 $followuser = C::t('home_follow')->fetch_all_following_by_uid($_G['uid']);
  356.                 $uids = array_keys($followuser);
  357.                 if(!empty($uids)) {
  358.                         // 检查是否有新的动态
  359.                         $count = C::t('home_follow_feed')->count_by_uid_dateline($uids, $lastcheckfeed);
  360.                         if($count) {
  361.                                 // 有新动态,添加通知
  362.                                 notification_add($_G['uid'], 'follow', 'member_follow', array('count' => $count, 'from_id'=>$_G['uid'], 'from_idtype' => 'follow'), 1);
  363.                         }
  364.                 }
  365.         }
  366.         // 更新检查动态的cookie
  367.         dsetcookie('checkfollow', 1, 30);
  368. }

  369. /**
  370. * 验证邮箱格式及合法性
  371. *
  372. * @param string $email 需要验证的邮箱地址
  373. */
  374. function checkemail($email) {
  375.         global $_G;

  376.         $email = strtolower(trim($email));
  377.         if(strlen($email) > 255) {
  378.                 // 邮箱地址过长
  379.                 showmessage('profile_email_illegal', '', array(), array('handle' => false));
  380.         }
  381.         if($_G['setting']['regmaildomain']) {
  382.                 // 检查邮箱域名是否合法
  383.                 $maildomainexp = '/('.str_replace("\r\n", '|', preg_quote(trim($_G['setting']['maildomainlist']), '/')).')$/i';
  384.                 if($_G['setting']['regmaildomain'] == 1 && !preg_match($maildomainexp, $email)) {
  385.                         showmessage('profile_email_domain_illegal', '', array(), array('handle' => false));
  386.                 } elseif($_G['setting']['regmaildomain'] == 2 && preg_match($maildomainexp, $email)) {
  387.                         showmessage('profile_email_domain_illegal', '', array(), array('handle' => false));
  388.                 }
  389.         }

  390.         // 调用ucenter接口验证邮箱
  391.         loaducenter();
  392.         $ucresult = uc_user_checkemail($email);

  393.         // 处理ucenter返回的结果
  394.         if($ucresult == -4) {
  395.                 showmessage('profile_email_illegal', '', array(), array('handle' => false));
  396.         } elseif($ucresult == -5) {
  397.                 showmessage('profile_email_domain_illegal', '', array(), array('handle' => false));
  398.         } elseif($ucresult == -6) {
  399.                 showmessage('profile_email_duplicate', '', array(), array('handle' => false));
  400.         }
  401. }

  402. /**
  403. * 生成获取密码的签名链接
  404. *
  405. * @param int $uid 用户ID
  406. * @param string $idstring 用户注册ID字符串
  407. * @return string 签名链接
  408. */
  409. function make_getpws_sign($uid, $idstring) {
  410.         global $_G;
  411.         $link = "member.php?mod=getpasswd&uid={$uid}&id={$idstring}";
  412.         return dsign($link);
  413. }

  414. ?>
复制代码


回复

使用道具 举报

13

主题

3146

回帖

5349

积分

应用开发者

贡献
372 点
金币
164 个
QQ
发表于 2024-10-28 21:56:01 | 查看全部
感谢大佬分享
回复

使用道具 举报

75

主题

524

回帖

640

积分

自成一派

贡献
3 点
金币
0 个
发表于 2024-10-29 08:23:43 | 查看全部
小白路过。请问这是干嘛用的?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 关注公众号
  • 有偿服务微信
  • 有偿服务QQ

手机版|小黑屋|Discuz! 官方交流社区 ( 皖ICP备16010102号 |皖公网安备34010302002376号 )|网站地图|star

GMT+8, 2025-4-30 14:32 , Processed in 0.041790 second(s), 7 queries , Redis On.

Powered by Discuz! W1.0 Licensed

Cpoyright © 2001-2025 Discuz! Team.

关灯 在本版发帖
有偿服务QQ
有偿服务微信
返回顶部
快速回复 返回顶部 返回列表