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

510 lines
15KB

  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. session_start();
  13. /**
  14. * 检验会员是否有权使用某功能,这个函数是一个回值函数CheckPurview函数只是对他回值的一个处理过程
  15. *
  16. * @access public
  17. * @param string $n 功能名称
  18. * @return mix 如果具有则返回TRUE
  19. */
  20. function TestPurview($n)
  21. {
  22. $rs = FALSE;
  23. $purview = $GLOBALS['cuserLogin']->getPurview();
  24. if (preg_match('/admin_AllowAll/i', $purview)) {
  25. return TRUE;
  26. }
  27. if ($n == '') {
  28. return TRUE;
  29. }
  30. if (!isset($GLOBALS['groupRanks'])) {
  31. $GLOBALS['groupRanks'] = explode(' ', $purview);
  32. }
  33. $ns = explode(',', $n);
  34. foreach ($ns as $n) {
  35. //只要找到一个匹配的权限,即可认为会员有权浏览此页面
  36. if ($n == '') {
  37. continue;
  38. }
  39. if (in_array($n, $GLOBALS['groupRanks'])) {
  40. $rs = TRUE;
  41. break;
  42. }
  43. }
  44. return $rs;
  45. }
  46. /**
  47. * 对权限检测后返回操作对话框
  48. *
  49. * @access public
  50. * @param string $n 功能名称
  51. * @return string
  52. */
  53. function CheckPurview($n)
  54. {
  55. if (!TestPurview($n)) {
  56. ShowMsg("您没有权限进行此操作", "-1");
  57. exit();
  58. }
  59. }
  60. /**
  61. * 是否没权限限制(超级管理员)
  62. *
  63. * @access public
  64. * @param string
  65. * @return bool
  66. */
  67. function TestAdmin()
  68. {
  69. $purview = $GLOBALS['cuserLogin']->getPurview();
  70. if (preg_match('/admin_AllowAll/i', $purview)) {
  71. return TRUE;
  72. } else {
  73. return FALSE;
  74. }
  75. }
  76. $DedeUserCatalogs = array();
  77. /**
  78. * 检测会员是否有权限操作某栏目
  79. *
  80. * @access public
  81. * @param int $cid 栏目id
  82. * @param string $msg 返回消息
  83. * @return string
  84. */
  85. function CheckCatalog($cid, $msg)
  86. {
  87. global $cfg_admin_channel, $admin_catalogs;
  88. if ($cfg_admin_channel == 'all' || TestAdmin()) {
  89. return TRUE;
  90. }
  91. if (!in_array($cid, $admin_catalogs)) {
  92. ShowMsg("$msg", "-1");
  93. exit();
  94. }
  95. return TRUE;
  96. }
  97. /**
  98. * 发布文档临时附件信息缓存、发文档前先清空附件信息
  99. * 发布文档时涉及的附件保存到缓存里,完成后把它与文档关连
  100. *
  101. * @access public
  102. * @param string $fid 文件ID
  103. * @param string $filename 文件名称
  104. * @return void
  105. */
  106. function AddMyAddon($fid, $filename)
  107. {
  108. $cacheFile = DEDEDATA.'/cache/addon-'.session_id().'.inc';
  109. if (!file_exists($cacheFile)) {
  110. $fp = fopen($cacheFile, 'w');
  111. fwrite($fp, '<'.'?php'."\r\n");
  112. fwrite($fp, "\$myaddons = array();\r\n");
  113. fwrite($fp, "\$maNum = 0;\r\n");
  114. fclose($fp);
  115. }
  116. include($cacheFile);
  117. $fp = fopen($cacheFile, 'a');
  118. $arrPos = $maNum;
  119. $maNum++;
  120. fwrite($fp, "\$myaddons[\$maNum] = array('$fid', '$filename');\r\n");
  121. fwrite($fp, "\$maNum = $maNum;\r\n");
  122. fclose($fp);
  123. }
  124. /**
  125. * 清理附件,如果关连的文档id,先把上一批附件传给这个文档id
  126. *
  127. * @access public
  128. * @param string $aid 文档id
  129. * @param string $title 文档标题
  130. * @return empty
  131. */
  132. function ClearMyAddon($aid = 0, $title = '')
  133. {
  134. global $dsql;
  135. $cacheFile = DEDEDATA.'/cache/addon-'.session_id().'.inc';
  136. $_SESSION['bigfile_info'] = array();
  137. $_SESSION['file_info'] = array();
  138. if (!file_exists($cacheFile)) {
  139. return;
  140. }
  141. //把附件与文档关连
  142. if (!empty($aid)) {
  143. include($cacheFile);
  144. foreach ($myaddons as $addons) {
  145. if (!empty($title)) {
  146. $dsql->ExecuteNoneQuery("Update `#@__uploads` set arcid='$aid',title='$title' where aid='{$addons[0]}'");
  147. } else {
  148. $dsql->ExecuteNoneQuery("Update `#@__uploads` set arcid='$aid' where aid='{$addons[0]}' ");
  149. }
  150. }
  151. }
  152. @unlink($cacheFile);
  153. }
  154. /**
  155. * 登录类
  156. *
  157. * @package userLogin
  158. * @subpackage DedeBIZ.Libraries
  159. * @link https://www.dedebiz.com
  160. */
  161. class userLogin
  162. {
  163. var $userName = '';
  164. var $userPwd = '';
  165. var $userID = '';
  166. var $adminDir = '';
  167. var $userType = '';
  168. var $userChannel = '';
  169. var $userPurview = '';
  170. var $userFace = '';
  171. var $keepUserIDTag = 'dede_admin_id';
  172. var $keepUserTypeTag = 'dede_admin_type';
  173. var $keepUserChannelTag = 'dede_admin_channel';
  174. var $keepUserNameTag = 'dede_admin_name';
  175. var $keepUserPurviewTag = 'dede_admin_purview';
  176. var $keepAdminStyleTag = 'dede_admin_style';
  177. var $keepUserFace = 'dede_admin_face';
  178. var $adminStyle = 'DedeBIZ';
  179. //php5构造函数
  180. function __construct($admindir = '')
  181. {
  182. global $admin_path;
  183. if (isset($_SESSION[$this->keepUserIDTag])) {
  184. $this->userID = $_SESSION[$this->keepUserIDTag];
  185. $this->userType = $_SESSION[$this->keepUserTypeTag];
  186. $this->userChannel = $_SESSION[$this->keepUserChannelTag];
  187. $this->userName = $_SESSION[$this->keepUserNameTag];
  188. $this->userPurview = $_SESSION[$this->keepUserPurviewTag];
  189. $this->adminStyle = $_SESSION[$this->keepAdminStyleTag];
  190. $this->userFace = $_SESSION[$this->keepUserFace];
  191. }
  192. if ($admindir != '') {
  193. $this->adminDir = $admindir;
  194. } else {
  195. $this->adminDir = $admin_path;
  196. }
  197. }
  198. function userLogin($admindir = '')
  199. {
  200. $this->__construct($admindir);
  201. }
  202. /**
  203. * 检验会员是否正确
  204. *
  205. * @access public
  206. * @param string $username 账号
  207. * @param string $userpwd 密码
  208. * @return string
  209. */
  210. function checkUser($username, $userpwd)
  211. {
  212. global $dsql;
  213. //只允许账号和密码用0-9,a-z,A-Z,'@','_','.','-'这些字符
  214. $this->userName = preg_replace("/[^0-9a-zA-Z_@!\.-]/", '', $username);
  215. $this->userPwd = preg_replace("/[^0-9a-zA-Z_@!\.-]/", '', $userpwd);
  216. $pwd = substr(md5($this->userPwd), 5, 20);
  217. $dsql->SetQuery("SELECT admin.*,atype.purviews,member.face FROM `#@__admin` admin LEFT JOIN `#@__admintype` atype ON atype.`rank`=admin.usertype LEFT JOIN `#@__member` member ON member.mid = admin.id WHERE admin.userid LIKE '".$this->userName."' LIMIT 0,1");
  218. $dsql->Execute();
  219. $row = $dsql->GetObject();
  220. if (!isset($row->pwd)) {
  221. return -1;
  222. } else if (!empty($row->pwd_new) && !password_verify($this->userPwd, $row->pwd_new)) {
  223. $this->loginError($row->id);
  224. return -2;
  225. } else if (!empty($row->pwd) && $pwd != $row->pwd) {
  226. $this->loginError($row->id);
  227. return -2;
  228. } else {
  229. $upsql = "";
  230. if (empty($row->pwd_new) && function_exists('password_hash')) {
  231. //升级密码
  232. $newpwd = password_hash($this->userPwd, PASSWORD_BCRYPT);
  233. $upsql .= ",pwd='',pwd_new='{$newpwd}'";
  234. }
  235. $loginip = GetIP();
  236. $this->userID = $row->id;
  237. $this->userType = $row->usertype;
  238. $this->userChannel = $row->typeid;
  239. $this->userName = $row->uname;
  240. $this->userPurview = $row->purviews;
  241. $this->userFace = $row->face;
  242. $inquery = "UPDATE `#@__admin` SET loginip='$loginip',logintime='".time()."'{$upsql},loginerr=0 WHERE id='".$row->id."'";
  243. $dsql->ExecuteNoneQuery($inquery);
  244. $sql = "UPDATE `#@__member` SET logintime=".time().", loginip='$loginip' WHERE mid=".$row->id;
  245. $dsql->ExecuteNoneQuery($sql);
  246. return 1;
  247. }
  248. }
  249. /**
  250. * 是否需要验证码
  251. *
  252. * @param mixed $username
  253. * @return bool
  254. */
  255. function isNeedCheckCode($username)
  256. {
  257. $num = $this->getLoginError($username);
  258. return $num >= 3 ? true : false;
  259. }
  260. /**
  261. * 1分钟以内登录错误的次数
  262. *
  263. * @param mixed $username
  264. * @return int 登录错误次数
  265. */
  266. function getLoginError($username)
  267. {
  268. global $dsql;
  269. if (!TableHasField("#@__admin", "loginerr")) {
  270. return 0;
  271. }
  272. $this->userName = preg_replace("/[^0-9a-zA-Z_@!\.-]/", '', $username);
  273. $row = $dsql->GetOne("SELECT loginerr,logintime FROM `#@__admin` WHERE userid LIKE '$this->userName'");
  274. if (is_array($row)) {
  275. //1分钟内如果输错3次则需要验证码
  276. return (time() - (int)$row['logintime']) < 60 ? (int)$row['loginerr'] : 0;
  277. } else {
  278. return -1;
  279. }
  280. }
  281. /**
  282. * 记录登录错误
  283. *
  284. * @return void
  285. */
  286. function loginError($adminid)
  287. {
  288. global $dsql;
  289. $loginip = GetIP();
  290. $inquery = "UPDATE `#@__admin` SET loginip='$loginip',logintime='".time()."',loginerr=loginerr+1 WHERE id='".$adminid."'";
  291. $dsql->ExecuteNoneQuery($inquery);
  292. }
  293. /**
  294. * 保持会员的会话状态
  295. *
  296. * @access public
  297. * @return int 成功返回 1,失败返回 -1
  298. */
  299. function keepUser()
  300. {
  301. if ($this->userID != '' && $this->userType != '') {
  302. global $admincachefile, $adminstyle;
  303. if (empty($adminstyle)) $adminstyle = 'DedeBIZ';
  304. @session_register($this->keepUserIDTag);
  305. $_SESSION[$this->keepUserIDTag] = $this->userID;
  306. @session_register($this->keepUserTypeTag);
  307. $_SESSION[$this->keepUserTypeTag] = $this->userType;
  308. @session_register($this->keepUserChannelTag);
  309. $_SESSION[$this->keepUserChannelTag] = $this->userChannel;
  310. @session_register($this->keepUserNameTag);
  311. $_SESSION[$this->keepUserNameTag] = $this->userName;
  312. @session_register($this->keepUserPurviewTag);
  313. $_SESSION[$this->keepUserPurviewTag] = $this->userPurview;
  314. @session_register($this->keepAdminStyleTag);
  315. $_SESSION[$this->keepAdminStyleTag] = $adminstyle;
  316. @session_register($this->keepUserFace);
  317. $_SESSION[$this->keepUserFace] = $this->userFace;
  318. PutCookie('DedeUserID', $this->userID, 3600 * 24, '/');
  319. PutCookie('DedeLoginTime', time(), 3600 * 24, '/');
  320. $this->ReWriteAdminChannel();
  321. return 1;
  322. } else {
  323. return -1;
  324. }
  325. }
  326. /**
  327. * 重写会员权限栏目
  328. *
  329. * @access public
  330. * @return void
  331. */
  332. function ReWriteAdminChannel()
  333. {
  334. //$this->userChannel
  335. $cacheFile = DEDEDATA.'/cache/admincat_'.$this->userID.'.inc';
  336. //管理员管理的栏目列表
  337. $typeid = trim($this->userChannel);
  338. if (empty($typeid) || $this->getUserType() >= 10) {
  339. $firstConfig = "\$cfg_admin_channel = 'all';\r\n\$admin_catalogs = array();\r\n";
  340. } else {
  341. $firstConfig = "\$cfg_admin_channel = 'array';\r\n";
  342. }
  343. $fp = fopen($cacheFile, 'w');
  344. fwrite($fp, '<'.'?php'."\r\n");
  345. fwrite($fp, $firstConfig);
  346. if (!empty($typeid)) {
  347. $typeids = explode(',', $typeid);
  348. $typeid = '';
  349. foreach ($typeids as $tid) {
  350. $typeid .= ($typeid == '' ? GetSonIdsUL($tid) : ','.GetSonIdsUL($tid));
  351. }
  352. $typeids = explode(',', $typeid);
  353. $typeidsnew = array_unique($typeids);
  354. $typeid = join(',', $typeidsnew);
  355. fwrite($fp, "\$admin_catalogs = array($typeid);\r\n");
  356. }
  357. fwrite($fp, '?'.'>');
  358. fclose($fp);
  359. }
  360. /**
  361. * 结束会员的会话状态
  362. *
  363. * @access public
  364. * @return void
  365. */
  366. function exitUser()
  367. {
  368. ClearMyAddon();
  369. @session_unregister($this->keepUserIDTag);
  370. @session_unregister($this->keepUserTypeTag);
  371. @session_unregister($this->keepUserChannelTag);
  372. @session_unregister($this->keepUserNameTag);
  373. @session_unregister($this->keepUserPurviewTag);
  374. @session_unregister($this->keepUserFace);
  375. DropCookie('dedeAdmindir');
  376. DropCookie('DedeUserID');
  377. DropCookie('DedeLoginTime');
  378. $_SESSION = array();
  379. }
  380. /**
  381. * 获得会员管理栏目的值
  382. *
  383. * @access public
  384. * @return array
  385. */
  386. function getUserChannel()
  387. {
  388. if ($this->userChannel != '') {
  389. return $this->userChannel;
  390. } else {
  391. return '';
  392. }
  393. }
  394. /**
  395. * 获得会员的权限值
  396. *
  397. * @access public
  398. * @return int
  399. */
  400. function getUserType()
  401. {
  402. if ($this->userType != '') {
  403. return $this->userType;
  404. } else {
  405. return -1;
  406. }
  407. }
  408. function getUserFace()
  409. {
  410. if ($this->userFace != '') {
  411. return $this->userFace;
  412. } else {
  413. return '/static/web/img/admin.png';
  414. }
  415. }
  416. /**
  417. * 获取会员权限值
  418. *
  419. * @access public
  420. * @return int
  421. */
  422. function getUserRank()
  423. {
  424. return $this->getUserType();
  425. }
  426. /**
  427. * 获得会员的id
  428. *
  429. * @access public
  430. * @return int
  431. */
  432. function getUserID()
  433. {
  434. if ($this->userID != '') {
  435. return $this->userID;
  436. } else {
  437. return -1;
  438. }
  439. }
  440. /**
  441. * 获得会员的名称
  442. *
  443. * @access public
  444. * @return string
  445. */
  446. function getUserName()
  447. {
  448. if ($this->userName != '') {
  449. return $this->userName;
  450. } else {
  451. return -1;
  452. }
  453. }
  454. /**
  455. * 会员权限表
  456. *
  457. * @access public
  458. * @return string
  459. */
  460. function getPurview()
  461. {
  462. return $this->userPurview;
  463. }
  464. }
  465. /**
  466. * 获得某id的所有下级id
  467. *
  468. * @access public
  469. * @param int $id 栏目id
  470. * @param int $channel 栏目id
  471. * @param int $addthis 是否加入当前这个栏目
  472. * @return string
  473. */
  474. function GetSonIdsUL($id, $channel = 0, $addthis = TRUE)
  475. {
  476. global $cfg_Cs;
  477. $GLOBALS['idArray'] = array();
  478. if (!is_array($cfg_Cs)) {
  479. require_once(DEDEDATA."/cache/inc_catalog_base.inc");
  480. }
  481. GetSonIdsLogicUL($id, $cfg_Cs, $channel, $addthis);
  482. $rquery = join(',', $GLOBALS['idArray']);
  483. return $rquery;
  484. }
  485. /**
  486. * 递归逻辑
  487. *
  488. * @access public
  489. * @param int $id 栏目id
  490. * @param array $sArr 缓存数组
  491. * @param int $channel 栏目id
  492. * @param int $addthis 是否加入当前这个栏目
  493. * @return string
  494. */
  495. function GetSonIdsLogicUL($id, $sArr, $channel = 0, $addthis = FALSE)
  496. {
  497. if ($id != 0 && $addthis) {
  498. $GLOBALS['idArray'][$id] = $id;
  499. }
  500. foreach ($sArr as $k => $v) {
  501. if ($v[0] == $id && ($channel == 0 || $v[1] == $channel)) {
  502. GetSonIdsLogicUL($k, $sArr, $channel, TRUE);
  503. }
  504. }
  505. }
  506. ?>