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

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