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

473 lines
13KB

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