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

518 lines
14KB

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