国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

574 lines
19KB

  1. <?php
  2. if (!defined('DEDEINC')) exit('dedebiz');
  3. /**
  4. * 会员登录
  5. *
  6. * @version $id:userlogin.class.php 15:59 2010年7月5日 tianya $
  7. * @package DedeBIZ.Libraries
  8. * @copyright Copyright (c) 2022 DedeBIZ.COM
  9. * @license https://www.dedebiz.com/license
  10. * @link https://www.dedebiz.com
  11. */
  12. //使用缓存助手
  13. helper('cache');
  14. /**
  15. * 检查用户名的合法性
  16. *
  17. * @access public
  18. * @param string $uid 用户UID
  19. * @param string $msgtitle 提示标题
  20. * @param string $ckhas 检查是否存在
  21. * @return string
  22. */
  23. function CheckUserID($uid, $msgtitle = '用户名', $ckhas = TRUE)
  24. {
  25. global $cfg_mb_notallow, $cfg_mb_idmin, $cfg_md_idurl, $cfg_soft_lang, $dsql;
  26. if ($cfg_mb_notallow != '') {
  27. $nas = explode(',', $cfg_mb_notallow);
  28. if (in_array($uid, $nas)) {
  29. return $msgtitle.'为系统禁止的标识';
  30. }
  31. }
  32. if ($cfg_md_idurl == 'Y' && preg_match("/[^a-z0-9]/i", $uid)) {
  33. return $msgtitle.'必须由英文字母或数字组成';
  34. }
  35. if ($cfg_soft_lang == 'utf-8') {
  36. $ck_uid = utf82gb($uid);
  37. } else {
  38. $ck_uid = $uid;
  39. }
  40. for ($i = 0; isset($ck_uid[$i]); $i++) {
  41. if (ord($ck_uid[$i]) > 0x80) {
  42. if (isset($ck_uid[$i + 1]) && ord($ck_uid[$i + 1]) > 0x40) {
  43. $i++;
  44. } else {
  45. return $msgtitle.'建议用英文字母和数字组合';
  46. }
  47. } else {
  48. if (preg_match("/[^0-9a-z@\.-]/i", $ck_uid[$i])) {
  49. return $msgtitle.'不能含有[@]、[.]、[-]以外的特殊符号';
  50. }
  51. }
  52. }
  53. if ($ckhas) {
  54. $row = $dsql->GetOne("SELECT * FROM `#@__member` WHERE userid LIKE '$uid' ");
  55. if (is_array($row)) return $msgtitle."已经存在";
  56. }
  57. return 'ok';
  58. }
  59. /**
  60. * 检查用户是否被禁言
  61. *
  62. * @return void
  63. */
  64. function CheckNotAllow()
  65. {
  66. global $cfg_ml;
  67. if (empty($cfg_ml->M_ID)) return;
  68. if ($cfg_ml->M_Spacesta == -2) {
  69. ShowMsg("您已经被禁言,请与管理员联系", "-1");
  70. exit();
  71. } else if ($cfg_ml->M_Spacesta == -10) {
  72. ShowMsg("系统开启了邮件审核机制,因此您的帐号需要审核后才能发信息", "-1");
  73. exit();
  74. } else if ($cfg_ml->M_Spacesta < 0) {
  75. ShowMsg('系统开启了审核机制,因此您的帐号需要管理员审核后才能发信息', '-1');
  76. exit();
  77. }
  78. }
  79. function FormatUsername($username)
  80. {
  81. $username = str_replace("`", "‘", $username);
  82. $username = str_replace("'", "‘", $username);
  83. $username = str_replace("\"", "“", $username);
  84. $username = str_replace(",", ",", $username);
  85. $username = str_replace("(", "(", $username);
  86. $username = str_replace(")", ")", $username);
  87. return addslashes($username);
  88. }
  89. /**
  90. * 网站会员登录类
  91. *
  92. * @package MemberLogin
  93. * @subpackage DedeBIZ.Libraries
  94. * @link https://www.dedebiz.com
  95. */
  96. class MemberLogin
  97. {
  98. var $M_ID;
  99. var $M_LoginID;
  100. var $M_MbType;
  101. var $M_Money;
  102. var $M_UserMoney;
  103. var $M_Scores;
  104. var $M_UserName;
  105. var $M_Rank;
  106. var $M_Face;
  107. var $M_LoginTime;
  108. var $M_KeepTime;
  109. var $M_Spacesta;
  110. var $fields;
  111. var $isAdmin;
  112. var $M_UpTime;
  113. var $M_ExpTime;
  114. var $M_HasDay;
  115. var $M_JoinTime;
  116. var $M_Honor = '';
  117. var $M_SendMax = 0;
  118. var $memberCache = 'memberlogin';
  119. //php5构造函数
  120. function __construct($kptime = -1, $cache = FALSE)
  121. {
  122. global $dsql;
  123. if ($kptime == -1) {
  124. $this->M_KeepTime = 3600 * 24 * 7;
  125. } else {
  126. $this->M_KeepTime = $kptime;
  127. }
  128. $formcache = FALSE;
  129. $this->M_ID = $this->GetNum(GetCookie("DedeUserID"));
  130. $this->M_LoginTime = GetCookie("DedeLoginTime");
  131. $this->fields = array();
  132. $this->isAdmin = FALSE;
  133. if (empty($this->M_ID)) {
  134. $this->ResetUser();
  135. } else {
  136. $this->M_ID = intval($this->M_ID);
  137. if ($cache) {
  138. $this->fields = GetCache($this->memberCache, $this->M_ID);
  139. if (empty($this->fields)) {
  140. $this->fields = $dsql->GetOne("SELECT * FROM `#@__member` WHERE mid='{$this->M_ID}' ");
  141. } else {
  142. $formcache = TRUE;
  143. }
  144. } else {
  145. $this->fields = $dsql->GetOne("SELECT * FROM `#@__member` WHERE mid='{$this->M_ID}' ");
  146. }
  147. if (is_array($this->fields)) {
  148. //间隔一小时更新一次用户登录时间
  149. if (time() - $this->M_LoginTime > 3600) {
  150. $dsql->ExecuteNoneQuery("update `#@__member` set logintime='".time()."',loginip='".GetIP()."' WHERE mid='".$this->fields['mid']."';");
  151. PutCookie("DedeLoginTime", time(), $this->M_KeepTime);
  152. }
  153. $this->M_LoginID = $this->fields['userid'];
  154. $this->M_MbType = $this->fields['mtype'];
  155. $this->M_Money = $this->fields['money'];
  156. $this->M_UserMoney = $this->fields['user_money'];
  157. $this->M_UserName = FormatUsername($this->fields['uname']);
  158. $this->M_Scores = $this->fields['scores'];
  159. $this->M_Face = $this->fields['face'];
  160. $this->M_Rank = $this->fields['rank'];
  161. $this->M_Spacesta = $this->fields['spacesta'];
  162. $sql = "SELECT titles From `#@__scores` WHERE integral<={$this->fields['scores']} ORDER BY integral DESC";
  163. $scrow = $dsql->GetOne($sql);
  164. $this->fields['honor'] = $scrow['titles'];
  165. $this->M_Honor = $this->fields['honor'];
  166. if ($this->fields['matt'] == 10) $this->isAdmin = TRUE;
  167. $this->M_UpTime = $this->fields['uptime'];
  168. $this->M_ExpTime = $this->fields['exptime'];
  169. $this->M_SendMax = $this->fields['send_max'];
  170. $this->M_JoinTime = MyDate('Y-m-d', $this->fields['jointime']);
  171. if ($this->M_Rank > 10 && $this->M_UpTime > 0) {
  172. $this->M_HasDay = $this->Judgemember();
  173. }
  174. if (!$formcache) {
  175. SetCache($this->memberCache, $this->M_ID, $this->fields, 1800);
  176. }
  177. } else {
  178. $this->ResetUser();
  179. }
  180. }
  181. }
  182. function MemberLogin($kptime = -1)
  183. {
  184. $this->__construct($kptime);
  185. }
  186. /**
  187. * 删除缓存,每次登录时和在修改用户资料的地方会清除
  188. *
  189. * @access public
  190. * @param string
  191. * @return string
  192. */
  193. function DelCache($mid)
  194. {
  195. DelCache($this->memberCache, $mid);
  196. }
  197. /**
  198. * 判断会员是否到期
  199. *
  200. * @return string
  201. */
  202. function Judgemember()
  203. {
  204. global $dsql, $cfg_mb_rank;
  205. $nowtime = time();
  206. $mhasDay = $this->M_ExpTime - ceil(($nowtime - $this->M_UpTime) / 3600 / 24) + 1;
  207. if ($mhasDay <= 0) {
  208. $dsql->ExecuteNoneQuery("UPDATE `#@__member` SET uptime='0',exptime='0',`rank`='$cfg_mb_rank' WHERE mid='".$this->fields['mid']."';");
  209. }
  210. return $mhasDay;
  211. }
  212. /**
  213. * 退出cookie的会话
  214. *
  215. * @return void
  216. */
  217. function ExitCookie()
  218. {
  219. $this->ResetUser();
  220. }
  221. /**
  222. * 验证用户是否已经登录
  223. *
  224. * @return bool
  225. */
  226. function IsLogin()
  227. {
  228. if ($this->M_ID > 0) return TRUE;
  229. else return FALSE;
  230. }
  231. /**
  232. * 检测用户上传空间
  233. *
  234. * @return int
  235. */
  236. function GetUserSpace()
  237. {
  238. global $dsql;
  239. $uid = $this->M_ID;
  240. $row = $dsql->GetOne("SELECT sum(filesize) AS fs FROM `#@__uploads` WHERE mid='$uid'; ");
  241. return $row['fs'];
  242. }
  243. /**
  244. * 检查用户空间信息
  245. *
  246. * @return void
  247. */
  248. function CheckUserSpace()
  249. {
  250. global $cfg_mb_max;
  251. $uid = $this->M_ID;
  252. $hasuse = $this->GetUserSpace();
  253. $maxSize = $cfg_mb_max * 1024 * 1024;
  254. if ($hasuse >= $maxSize) {
  255. ShowMsg('您的空间已满,不允许上传新文件', '-1');
  256. exit();
  257. }
  258. }
  259. /**
  260. * 更新用户信息统计表
  261. *
  262. * @access public
  263. * @param string $field 字段信息
  264. * @param string $uptype 更新类型
  265. * @return string
  266. */
  267. function UpdateUserTj($field, $uptype = 'add')
  268. {
  269. global $dsql;
  270. $mid = $this->M_ID;
  271. $arr = $dsql->GetOne("SELECT * `#@__member_tj` WHERE mid='$mid' ");
  272. if (!is_array($arr)) {
  273. $arr = array('article' => 0, 'album' => 0, 'archives' => 0, 'homecount' => 0, 'pagecount' => 0, 'feedback' => 0, 'friend' => 0, 'stow' => 0);
  274. }
  275. extract($arr);
  276. if (isset($$field)) {
  277. if ($uptype == 'add') {
  278. $$field++;
  279. } else if ($$field > 0) {
  280. $$field--;
  281. }
  282. }
  283. $inquery = "INSERT INTO `#@__member_tj` (`mid`,`article`,`album`,`archives`,`homecount`,`pagecount`,`feedback`,`friend`,`stow`) VALUES ('$mid','$article','$album','$archives','$homecount','$pagecount','$feedback','$friend','$stow'); ";
  284. $dsql->ExecuteNoneQuery("DELETE FROM `#@__member_tj` WHERE mid='$mid' ");
  285. $dsql->ExecuteNoneQuery($inquery);
  286. }
  287. /**
  288. * 重置用户信息
  289. *
  290. * @return void
  291. */
  292. function ResetUser()
  293. {
  294. $this->fields = '';
  295. $this->M_ID = 0;
  296. $this->M_LoginID = '';
  297. $this->M_Rank = 0;
  298. $this->M_Face = "";
  299. $this->M_Money = 0;
  300. $this->M_UserMoney = 0;
  301. $this->M_UserName = "";
  302. $this->M_LoginTime = 0;
  303. $this->M_MbType = '';
  304. $this->M_Scores = 0;
  305. $this->M_Spacesta = -2;
  306. $this->M_UpTime = 0;
  307. $this->M_ExpTime = 0;
  308. $this->M_JoinTime = 0;
  309. $this->M_HasDay = 0;
  310. DropCookie('DedeUserID');
  311. DropCookie('DedeLoginTime');
  312. }
  313. /**
  314. * 获取整数值
  315. *
  316. * @access public
  317. * @param string $fnum 处理的数值
  318. * @return string
  319. */
  320. function GetNum($fnum)
  321. {
  322. $fnum = preg_replace("/[^0-9\.]/", '', $fnum);
  323. return $fnum;
  324. }
  325. /**
  326. * 用户登录
  327. * 把登录密码转为指定长度md5数据
  328. *
  329. * @access public
  330. * @param string $pwd 需要加密的密码
  331. * @return string
  332. */
  333. function GetEncodePwd($pwd)
  334. {
  335. global $cfg_mb_pwdtype;
  336. if (empty($cfg_mb_pwdtype)) $cfg_mb_pwdtype = '32';
  337. switch ($cfg_mb_pwdtype) {
  338. case 'l16':
  339. return substr(md5($pwd), 0, 16);
  340. case 'r16':
  341. return substr(md5($pwd), 16, 16);
  342. case 'm16':
  343. return substr(md5($pwd), 8, 16);
  344. default:
  345. return md5($pwd);
  346. }
  347. }
  348. /**
  349. * 投稿是否被限制
  350. *
  351. * @return bool
  352. */
  353. function IsSendLimited()
  354. {
  355. global $dsql;
  356. $arr = $dsql->GetOne("SELECT COUNT(*) as dd FROM `#@__arctiny` WHERE mid='{$this->M_ID}'");
  357. if ($this->isAdmin === true ) {
  358. return false;
  359. }
  360. if (is_array($arr)) {
  361. if ($arr['dd'] >= $this->M_SendMax) {
  362. return true;
  363. } else {
  364. return false;
  365. }
  366. } else {
  367. return true;
  368. }
  369. }
  370. /**
  371. * 把数据库密码转为特定长度
  372. * 如果数据库密码是明文的,本程序不支持
  373. *
  374. * @access public
  375. * @param string
  376. * @return string
  377. */
  378. function GetShortPwd($dbpwd)
  379. {
  380. global $cfg_mb_pwdtype;
  381. if (empty($cfg_mb_pwdtype)) $cfg_mb_pwdtype = '32';
  382. $dbpwd = trim($dbpwd);
  383. if (strlen($dbpwd) == 16) {
  384. return $dbpwd;
  385. } else {
  386. switch ($cfg_mb_pwdtype) {
  387. case 'l16':
  388. return substr($dbpwd, 0, 16);
  389. case 'r16':
  390. return substr($dbpwd, 16, 16);
  391. case 'm16':
  392. return substr($dbpwd, 8, 16);
  393. default:
  394. return $dbpwd;
  395. }
  396. }
  397. }
  398. /**
  399. * 检查用户是否合法
  400. *
  401. * @access public
  402. * @param string $loginuser 登录用户名
  403. * @param string $loginpwd 用户密码
  404. * @return string
  405. */
  406. function CheckUser(&$loginuser, $loginpwd)
  407. {
  408. global $dsql;
  409. //检测用户名的合法性
  410. $rs = CheckUserID($loginuser, '用户名', FALSE);
  411. //用户名不正确时返回验证错误,原登录名通过引用返回错误提示信息
  412. if ($rs != 'ok') {
  413. $loginuser = $rs;
  414. return '0';
  415. }
  416. //matt=10 是管理员关连的前台帐号,为了安全起见,这个帐号只能从后台登录,不能直接从前台登录
  417. $row = $dsql->GetOne("SELECT mid,matt,pwd,pwd_new,logintime FROM `#@__member` WHERE userid LIKE '$loginuser' ");
  418. if (is_array($row)) {
  419. if (!empty($row['pwd_new']) && !password_verify($loginpwd, $row['pwd_new'])) {
  420. $this->loginError($loginuser);
  421. return -1;
  422. } else if (!empty($row['pwd']) && $this->GetShortPwd($row['pwd']) != $this->GetEncodePwd($loginpwd)) {
  423. $this->loginError($loginuser);
  424. return -1;
  425. } else {
  426. if (empty($row['pwd_new']) && function_exists('password_hash')) {
  427. //升级密码
  428. $newpwd = password_hash($loginpwd, PASSWORD_BCRYPT);
  429. $inquery = "UPDATE `#@__member` SET pwd='',pwd_new='{$newpwd}' WHERE mid='".$row['mid']."'";
  430. $dsql->ExecuteNoneQuery($inquery);
  431. }
  432. //管理员帐号不允许从前台登录
  433. if ($row['matt'] == 10) {
  434. return -2;
  435. } else {
  436. $this->PutLoginInfo($row['mid'], $row['logintime']);
  437. return 1;
  438. }
  439. }
  440. } else {
  441. return 0;
  442. }
  443. }
  444. /**
  445. * 是否需要验证码
  446. *
  447. * @param mixed $loginuser
  448. * @return bool
  449. */
  450. function isNeedCheckCode($loginuser)
  451. {
  452. $num = $this->getLoginError($loginuser);
  453. return $num >= 3 ? true : false;
  454. }
  455. /**
  456. * 1分钟以内登录错误的次数
  457. *
  458. * @param mixed $loginuser
  459. * @return int 登录错误次数
  460. */
  461. function getLoginError($loginuser)
  462. {
  463. global $dsql;
  464. $rs = CheckUserID($loginuser, '用户名', FALSE);
  465. //用户名不正确时返回验证错误,原登录名通过引用返回错误提示信息
  466. if ($rs != 'ok') {
  467. return -1;
  468. }
  469. if (!TableHasField("#@__member", "loginerr")) {
  470. return 0;
  471. }
  472. $row = $dsql->GetOne("SELECT loginerr,logintime FROM `#@__member` WHERE userid LIKE '$loginuser'");
  473. if (is_array($row)) {
  474. //1分钟内如果输错3次则需要验证码
  475. return (time() - (int)$row['logintime']) < 60 ? (int)$row['loginerr'] : 0;
  476. } else {
  477. return -1;
  478. }
  479. }
  480. /**
  481. * 记录登录错误
  482. *
  483. * @return void
  484. */
  485. function loginError($loginuser)
  486. {
  487. global $dsql;
  488. $rs = CheckUserID($loginuser, '用户名', FALSE);
  489. //用户名不正确时返回验证错误,原登录名通过引用返回错误提示信息
  490. if ($rs != 'ok') {
  491. return;
  492. }
  493. $loginip = GetIP();
  494. $inquery = "UPDATE `#@__member` SET loginip='$loginip',logintime='".time()."',loginerr=loginerr+1 WHERE userid='".$loginuser."'";
  495. $dsql->ExecuteNoneQuery($inquery);
  496. }
  497. /**
  498. * 保存用户cookie
  499. *
  500. * @access public
  501. * @param string $uid 用户id
  502. * @param string $logintime 登录限制时间
  503. * @return void
  504. */
  505. function PutLoginInfo($uid, $logintime = 0)
  506. {
  507. global $cfg_login_adds, $dsql;
  508. //登录增加积分(上一次登录时间必须大于两小时)
  509. if (time() - $logintime > 7200 && $cfg_login_adds > 0) {
  510. $dsql->ExecuteNoneQuery("UPDATE `#@__member` SET `scores`=`scores`+{$cfg_login_adds} WHERE mid='$uid' ");
  511. }
  512. $this->M_ID = $uid;
  513. $this->M_LoginTime = time();
  514. $loginip = GetIP();
  515. $inquery = "UPDATE `#@__member` SET loginip='$loginip',logintime='".$this->M_LoginTime."',loginerr=0 WHERE mid='".$uid."'";
  516. $dsql->ExecuteNoneQuery($inquery);
  517. if ($this->M_KeepTime > 0) {
  518. PutCookie('DedeUserID', $uid, $this->M_KeepTime);
  519. PutCookie('DedeLoginTime', $this->M_LoginTime, $this->M_KeepTime);
  520. } else {
  521. PutCookie('DedeUserID', $uid);
  522. PutCookie('DedeLoginTime', $this->M_LoginTime);
  523. }
  524. }
  525. function GetMemberTypeName()
  526. {
  527. global $dsql;
  528. if ($this->M_Rank == 0) {
  529. return '普通会员';
  530. } else {
  531. $row = $dsql->GetOne("SELECT membername FROM `#@__arcrank` WHERE `rank`='".$this->M_Rank."'");
  532. return $row['membername'];
  533. }
  534. }
  535. /**
  536. * 获得会员目前的状态
  537. *
  538. * @access public
  539. * @param object $dsql 数据库连接
  540. * @return string
  541. */
  542. function GetSta($dsql)
  543. {
  544. $sta = '';
  545. if ($this->M_Rank == 0) {
  546. $sta .= "您目前的身份是:普通会员";
  547. } else {
  548. $row = $dsql->GetOne("SELECT membername FROM `#@__arcrank` WHERE `rank`='".$this->M_Rank."'");
  549. $sta .= "您目前的身份是:".$row['membername'];
  550. $rs = $dsql->GetOne("SELECT id FROM `#@__admin` WHERE userid='".$this->M_LoginID."'");
  551. if (!is_array($rs)) {
  552. if ($this->M_Rank > 10 && $this->M_HasDay > 0) $sta .= " 剩余<span class='text-primary'>".$this->M_HasDay."</span>天";
  553. elseif ($this->M_Rank > 10) $sta .= "<span class='text-danger'>会员已到期</span>";
  554. }
  555. }
  556. $sta .= " 积分<span class='text-primary'>{$this->M_Scores}</span>分 金币<span class='text-primary'>{$this->M_Money}</span>个 余额<span class='text-primary'>{$this->M_UserMoney}</span>元";
  557. return $sta;
  558. }
  559. //获取能够发布文档的栏目
  560. public static function GetEnabledChannels() {
  561. global $dsql;
  562. $result = array();
  563. $dsql->SetQuery("SELECT channeltype FROM `#@__arctype` GROUP BY channeltype");
  564. $dsql->Execute();
  565. $candoChannel = '';
  566. while ($row = $dsql->GetObject()) {
  567. $result[] = $row->channeltype;
  568. }
  569. return $result;
  570. }
  571. }//End Class
  572. ?>