国内流行的内容管理系统(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.

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