久久久久av_欧美日韩一区二区在线_国产精品三区四区_日韩中字在线

返回列表 發(fā)帖
查看: 1467|回復: 2

Discuz x3.5 核心文件 function/function_member.php 函數(shù)注釋

4

主題

52

回帖

86

積分

應用開發(fā)者

貢獻
0 點
金幣
27 個
QQ
樓主
發(fā)表于 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. * 用戶登錄函數(shù)
  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 返回登錄結果,包括狀態(tài)(status)、用戶信息(member)和UC登錄結果(ucresult)
  21. */
  22. function userlogin($username, $password, $questionid, $answer, $loginfield = 'username', $ip = '') {
  23.     $return = array();

  24.     // 根據(jù)登錄字段確定登錄類型
  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通信函數(shù),如未定義則先加載
  37.     if(!function_exists('uc_user_login')) {
  38.         loaducenter();
  39.     }

  40.     // 處理自動登錄邏輯
  41.     if($isuid == 3) {
  42.         // 根據(jù)用戶名嘗試登錄,支持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.     // 檢查登錄結果,登錄失敗或用戶數(shù)據(jù)重復則返回
  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. * 設置登錄狀態(tài)
  93. *
  94. * 用于在用戶成功登錄后,設置用戶的登錄狀態(tài),包括但不限于用戶ID、用戶名、管理員等級、用戶組等信息,
  95. * 同時更新會話信息、設置登錄相關的cookie,以及更新統(tǒng)計信息。
  96. *
  97. * @param array $member 包含用戶登錄信息的數(shù)組,至少應包含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.     // 更新登錄統(tǒng)計信息
  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. * 用于檢查用戶登錄名是否可用,如果是,則返回可以登錄的標志;如果不可用,根據(jù)失敗次數(shù)返回相應的延遲時間。
  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的登錄檢查函數(shù),則調用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的登錄檢查函數(shù),則不進行處理
  181.         return;
  182.     }
  183.     // 記錄登錄失敗信息
  184.     C::t('common_failedlogin')->update_failed($_G['clientip']);
  185. }
  186. /**
  187. * 檢查IP嘗試次數(shù)是否超過限制。
  188. *
  189. * @param $numiptry int 嘗試次數(shù)限制。
  190. * @param $timeiptry int 時間限制(秒)。超過此時間限制,嘗試次數(shù)將被重置。
  191. * @return bool 如果嘗試次數(shù)超過限制,返回true;否則,返回false。
  192. */
  193. function failedipcheck($numiptry, $timeiptry) {
  194.         global $_G;
  195.         if(!$numiptry) {
  196.                 return false;
  197.         }
  198.         // 檢查當前IP在指定時間內(nèi)嘗試的次數(shù)是否已達到或超過限制
  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 包含邀請碼相關用戶信息的數(shù)組,如果不存在有效邀請碼則返回空數(shù)組。
  213. */
  214. function getinvite() {
  215.         global $_G;

  216.         // 如果注冊功能關閉,則直接返回空數(shù)組
  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.                                 // 如果用戶組不允許邀請或邀請需要付費,則返回空數(shù)組
  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 用戶自定義的替換數(shù)組,默認為空數(shù)組
  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.         // 合并用戶自定義替換數(shù)組和站點變量
  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.         // 重置用戶登錄狀態(tài)
  302.         $_G['uid'] = $_G['adminid'] = 0;
  303.         $_G['username'] = $_G['member']['password'] = '';
  304. }

  305. /**
  306. * 處理犯罪記錄相關操作
  307. *
  308. * @param string $fun 要執(zhí)行的操作
  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.         // 根據(jù)傳入的函數(shù)名執(zhí)行不同的操作
  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. * 檢查并更新關注的動態(tài)
  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.                 // 設置最后一次檢查動態(tài)的時間
  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.                         // 檢查是否有新的動態(tài)
  359.                         $count = C::t('home_follow_feed')->count_by_uid_dateline($uids, $lastcheckfeed);
  360.                         if($count) {
  361.                                 // 有新動態(tài),添加通知
  362.                                 notification_add($_G['uid'], 'follow', 'member_follow', array('count' => $count, 'from_id'=>$_G['uid'], 'from_idtype' => 'follow'), 1);
  363.                         }
  364.                 }
  365.         }
  366.         // 更新檢查動態(tài)的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

主題

3093

回帖

5249

積分

應用開發(fā)者

貢獻
365 點
金幣
158 個
QQ
沙發(fā)
發(fā)表于 2024-10-28 21:56:01 | 只看Ta
感謝大佬分享
回復

使用道具 舉報

74

主題

513

回帖

628

積分

自成一派

貢獻
3 點
金幣
0 個
板凳
發(fā)表于 2024-10-29 08:23:43 | 只看Ta
小白路過。請問這是干嘛用的?
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

  • 關注公眾號
  • 有償服務微信
  • 有償服務QQ

手機版|小黑屋|Discuz! 官方交流社區(qū) ( 皖ICP備16010102號 |皖公網(wǎng)安備34010302002376號 )|網(wǎng)站地圖|star

GMT+8, 2025-7-1 16:54 , Processed in 0.055152 second(s), 10 queries , Redis On.

Powered by Discuz! W1.0 Licensed

Cpoyright © 2001-2025 Discuz! Team.

關燈 在本版發(fā)帖
有償服務QQ
有償服務微信
返回頂部
快速回復 返回頂部 返回列表