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

535 lines
18KB

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