admin.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. /**
  3. * 管理员类
  4. *
  5. * @createtime 2018/03/01
  6. * @author 空竹
  7. * @copyright 芝麻开发(http://www.zhimawork.com)
  8. */
  9. class Admin {
  10. public $id = 0; //管理员ID
  11. public $account = ''; //管理员账号
  12. public $gid = 0; //属组ID
  13. public function __construct($id = 0) {
  14. if(!empty($id)) {
  15. $admin = self::getInfoById($id);
  16. if($admin){
  17. $this->id = $admin['id'];
  18. $this->account = $admin['account'];
  19. $this->gid = $admin['group'];
  20. }else{
  21. throw new MyException('管理员不存在', 902);
  22. }
  23. }
  24. }
  25. /**
  26. * 管理员登录
  27. *
  28. * @param $account 账号
  29. * @param $password 密码
  30. * @param $cookie 是否记录cookie
  31. *
  32. */
  33. public function login($account, $password, $cookie = 0){
  34. if(empty($account))throw new MyException('账号不能为空', 101);
  35. if(empty($password))throw new MyException('密码不能为空', 102);
  36. //检查账号
  37. $Table_admin = new Table_admin();
  38. $admininfo = $Table_admin->getInfoByAccount($account);
  39. if(empty($admininfo)) {
  40. throw new MyException('账号或密码错误', 104);//不让用户准确知道是账号错误
  41. }else{
  42. //验证密码
  43. $password = self::buildPassword($password, $admininfo['salt']);
  44. if($password[0] == $admininfo['password']){
  45. //登录成功
  46. $this->id = $admininfo['id'];
  47. $this->account = $admininfo['account'];
  48. $this->gid = $admininfo['group'];
  49. //设置cookie;
  50. if($cookie) $this->buildCookie();
  51. //设置session
  52. self::setSession(1, $this->id);
  53. //记录登陆信息
  54. $this->updateLoginInfo();
  55. //记录管理员日志log表
  56. $log = '成功登录!';
  57. Adminlog::add($log);
  58. return action_msg('登录成功', 1);//登陆成功
  59. }else{
  60. throw new MyException('账号或密码错误', 104);
  61. }
  62. }
  63. }
  64. //设置登陆cookie
  65. private function buildCookie(){
  66. global $cookie_ADMINID, $cookie_ADMINCODE;
  67. $cookie_time = time()+(3600*24*7);//7天
  68. setcookie($cookie_ADMINID, $this->id, $cookie_time, null, null, null, 1);
  69. setcookie($cookie_ADMINCODE, self::getCookieCode($this->id, $this->account, $this->gid), $cookie_time, null, null, null, 1);
  70. }
  71. //消除cookie
  72. static private function rebuildCookie(){
  73. global $cookie_ADMINID, $cookie_ADMINCODE;
  74. setcookie($cookie_ADMINID, '', time()-3600);
  75. setcookie($cookie_ADMINCODE, '', time()-3600);
  76. }
  77. //生成cookie校验码
  78. static private function getCookieCode($id = 0, $account = '', $group = 0){
  79. if(!ParamCheck::is_ID($id))throw new MyException('ID不合法', 101);
  80. if(empty($account))throw new MyException('账号不能为空', 102);
  81. if(!ParamCheck::is_ID($group))throw new MyException('Group不合法', 103);
  82. return md5(md5($account).md5($group).md5($id));//校验码算法
  83. }
  84. /**
  85. * 设置登陆Session
  86. *
  87. * @param $type 1--记录Session 2--清除记录
  88. *
  89. */
  90. static private function setSession($type, $id = 0){
  91. global $session_ADMINID;
  92. session_regenerate_id(1);//防止Session Fixation,重新生成session_id 2019/1/22
  93. if($type == 1){
  94. if(!ParamCheck::is_ID($id))throw new MyException('ID不合法', 101);
  95. $_SESSION[$session_ADMINID] = $id;
  96. }else{
  97. $_SESSION[$session_ADMINID] = 0;
  98. }
  99. }
  100. //更新登陆信息
  101. public function updateLoginInfo(){
  102. $Table_admin = new Table_admin();
  103. return $Table_admin->updateLoginInfo($this->id);
  104. }
  105. /**
  106. * 获得详细信息
  107. *
  108. * @param $id 管理员ID
  109. *
  110. */
  111. static public function getInfoById($id){
  112. if(!ParamCheck::is_ID($id))throw new MyException('ID不合法', 101);
  113. $Table_admin = new Table_admin();
  114. return $Table_admin->getInfoById($id);
  115. }
  116. //退出登录
  117. static public function logout(){
  118. $log = '退出登录!';
  119. Adminlog::add($log);
  120. self::setSession(2);
  121. self::rebuildCookie();
  122. }
  123. //检查是否登录
  124. static public function checkLogin(){
  125. global $session_ADMINID;
  126. global $cookie_ADMINID, $cookie_ADMINCODE;
  127. //是否存在session
  128. if(@$_SESSION[$session_ADMINID]){
  129. return true;
  130. }
  131. //不存在session则检查是否有cookie
  132. $cid = isset($_COOKIE[$cookie_ADMINID]) ? $_COOKIE[$cookie_ADMINID] : null;
  133. if(empty($cid)){
  134. return false;
  135. }
  136. //检查cookie数据是否对应,防止伪造
  137. $vcode = $_COOKIE[$cookie_ADMINCODE];
  138. $Table_admin = new Table_admin();
  139. $admin = $Table_admin->getInfoById($cid);
  140. if(!$admin) {
  141. //cookie数据不正确,清理掉
  142. self::rebuildCookie();
  143. return false;
  144. }
  145. $code = self::getCookieCode($cid, $admin['account'], $admin['group']);
  146. if($vcode != $code){
  147. //cookie数据不正确,清理掉
  148. self::rebuildCookie();
  149. return false;
  150. }
  151. //cookie数据正确,重写Session
  152. self::setSession(1, $cid);
  153. return true;
  154. }
  155. /**
  156. * 管理员列表
  157. *
  158. * @param $group
  159. *
  160. */
  161. static public function getList($group = 0){
  162. if($group){
  163. $filter = array(
  164. 'group' => $group
  165. );
  166. }else{
  167. $filter = array();
  168. }
  169. $Table_admin = new Table_admin();
  170. return $Table_admin->getList($filter);
  171. }
  172. //添加管理员
  173. static public function add($account, $password, $group){
  174. //检查参数
  175. if(empty($account))throw new MyException('账号不能为空', 101);
  176. if(!ParamCheck::is_ID($group))throw new MyException('管理员组ID不合法', 102);
  177. if(ParamCheck::is_weakPwd($password)) throw new MyException('密码太弱', 103);
  178. //获取信息//判断管理帐号是否重复
  179. $Table_admin = new Table_admin();
  180. $admin = $Table_admin->getInfoByAccount($account);
  181. if($admin) throw new MyException('账号已经存在', 104);
  182. //检查管理员组是否存在
  183. $Table_admingroup = new Table_admingroup();
  184. $admingroup = $Table_admingroup->getInfoById($group);
  185. if(!$admingroup) throw new MyException('管理员组不存在', 105);
  186. //生成管理员密码
  187. $password = self::buildPassword($password);
  188. $attr = array(
  189. 'account' => $account,
  190. 'password' => $password[0],
  191. 'salt' => $password[1],
  192. 'group' => $group
  193. );
  194. $rs = $Table_admin->add($attr);
  195. if($rs > 0){
  196. //记录管理员日志log表
  197. $msg = '成功添加管理员('.$account.')';
  198. Adminlog::add($msg);
  199. return action_msg($msg, 1);
  200. }else{
  201. throw new MyException('操作失败', 106);
  202. }
  203. }
  204. /**
  205. * 生成管理员密码
  206. *
  207. * @param $pwd 原始密码
  208. * @param $salt 密码Salt
  209. */
  210. static private function buildPassword($pwd, $salt = ''){
  211. if(empty($pwd))throw new MyException('密码不能为空', 101);
  212. if(empty($salt)) $salt = randcode(10, 4);//生成Salt
  213. $pwd_new = md5(md5($pwd).$salt);//加密算法
  214. return array($pwd_new, $salt);
  215. }
  216. /**
  217. * 删除管理员
  218. *
  219. * @param $adminId 管理员ID
  220. *
  221. * @return
  222. */
  223. static public function del($adminId){
  224. if(!ParamCheck::is_ID($adminId))throw new MyException('管理员ID不合法', 101);
  225. //不能删除当前登录的管理员2018/4/14
  226. if(self::getSession() == $adminId) throw new MyException('不能删除当前登陆的管理员', 103);
  227. $Table_admin = new Table_admin();
  228. $rs = $Table_admin->del($adminId);
  229. if($rs == 1){
  230. $msg = '删除管理员('.$adminId.')成功!';
  231. Adminlog::add($msg);
  232. return action_msg($msg, 1);
  233. }else{
  234. throw new MyException('操作失败', 102);
  235. }
  236. }
  237. /**
  238. * 修改管理员信息
  239. *
  240. * @param $id 管理员ID
  241. * @param $account 账号
  242. * @param $group 群组
  243. *
  244. * @return
  245. */
  246. static public function edit($id, $account, $group){
  247. if(!ParamCheck::is_ID($id))throw new MyException('管理员ID不合法', 101);
  248. if(empty($account))throw new MyException('管理员账号不能为空', 102);
  249. if(!ParamCheck::is_ID($group))throw new MyException('管理员组ID不合法', 103);
  250. //验证ID是否存在
  251. $Table_admin = new Table_admin();
  252. $admin = $Table_admin->getInfoById($id);
  253. if(empty($admin)) throw new MyException('管理员不存在', 104);
  254. //验证账号是否改变,如果改变则需要检查账号的重复性
  255. if($admin['account'] != $account){
  256. $admin2 = $Table_admin->getInfoByAccount($account);
  257. if($admin2) throw new MyException('账号已经存在', 105);
  258. }
  259. $attr = array(
  260. 'account' => $account,
  261. 'group' => $group
  262. );
  263. $rs = $Table_admin->edit($id, $attr);
  264. if($rs >= 0){
  265. $msg = '修改管理员('.$id.')信息成功!';
  266. Adminlog::add($msg);
  267. return action_msg($msg, 1);
  268. }else{
  269. throw new MyException('操作失败', 106);
  270. }
  271. }
  272. /**
  273. * 重置密码
  274. * @param $id 管理员ID
  275. * @param $newpass 新密码
  276. *
  277. */
  278. static public function resetPwd($id, $newpass){
  279. if(!ParamCheck::is_ID($id))throw new MyException('管理员ID不合法', 101);
  280. if(empty($newpass))throw new MyException('新的密码不能为空', 102);
  281. if(ParamCheck::is_weakPwd($newpass)) throw new MyException('新密码太弱', 103);
  282. $pass = self::buildPassword($newpass);
  283. $Table_admin = new Table_admin();
  284. $rs = $Table_admin->updatePwd($id, $pass[0], $pass[1]);
  285. if($rs == 1){
  286. $msg = '管理员('.$id.')密码成功重置为'.$newpass.'。';
  287. Adminlog::add($msg);
  288. return action_msg($msg, 1);
  289. }else{
  290. throw new MyException('操作失败', 104);
  291. }
  292. }
  293. /**
  294. * 修改密码
  295. *
  296. * @param string $oldpwd 旧密码
  297. * @param string $newpwd 新密码
  298. *
  299. */
  300. public function updatePwd($oldpwd, $newpwd){
  301. if(empty($oldpwd))throw new myException('旧密码不能为空', 101);
  302. if(empty($newpwd))throw new myException('新密码不能为空', 102);
  303. if(ParamCheck::is_weakPwd($newpwd)) throw new myException('新密码太弱', 104);
  304. $admin = self::getInfoById($this->id);
  305. //验证密码是否正确
  306. $oldpass = self::buildPassword($oldpwd, $admin['salt']);
  307. if($oldpass[0] != $admin['password']){
  308. throw new myException('旧密码错误', 103);
  309. }
  310. //产生新密码
  311. $newpass = self::buildPassword($newpwd);
  312. //修改密码
  313. $Table_admin = new Table_admin();
  314. $rs = $Table_admin->updatePwd($this->id, $newpass[0], $newpass[1]);
  315. if($rs == 1){
  316. $msg = '修改密码成功';
  317. Adminlog::add($msg);
  318. return action_msg($msg, 1);
  319. }else{
  320. throw new myException('操作失败', 444);
  321. }
  322. }
  323. //获得Session
  324. static public function getSession(){
  325. global $session_ADMINID;
  326. return $_SESSION[$session_ADMINID];
  327. }
  328. //获得管理组ID
  329. public function getGroupID(){
  330. return $this->gid;
  331. }
  332. //获得账号
  333. public function getAccount(){
  334. return $this->account;
  335. }
  336. //检查是否拥有权限
  337. static function checkAuth($powerId, $auth, $isExit = 1){
  338. if(empty($powerId))throw new MyException('权限编号不能为空', 101);
  339. //if(empty($auth))throw new MyException('权限序列不能为空', 102);
  340. //2018/11/27超级权限
  341. if($auth == 'SUPER') return true;
  342. $powers = explode('|', $auth);
  343. if(in_array($powerId, $powers)) {
  344. return true;
  345. }else{
  346. if($isExit) die('无访问权限');
  347. return false;
  348. }
  349. }
  350. }
  351. ?>