adminfun.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. //错误登陆记录
  3. function InsertErrorLoginNum($username,$password,$loginauth,$ip,$time){
  4. global $empire,$public_r,$dbtbpre;
  5. //COOKIE
  6. $loginnum=intval(getcvar('loginnum'));
  7. $logintime=$time;
  8. $lastlogintime=intval(getcvar('lastlogintime'));
  9. if($lastlogintime&&($logintime-$lastlogintime>$public_r['logintime']*60))
  10. {
  11. $loginnum=0;
  12. }
  13. $loginnum++;
  14. esetcookie("loginnum",$loginnum,$logintime+3600*24);
  15. esetcookie("lastlogintime",$logintime,$logintime+3600*24);
  16. //数据库
  17. $chtime=$time-$public_r['logintime']*60;
  18. $empire->query("delete from {$dbtbpre}enewsloginfail where lasttime<$chtime");
  19. $r=$empire->fetch1("select ip from {$dbtbpre}enewsloginfail where ip='$ip' limit 1");
  20. if($r['ip'])
  21. {
  22. $empire->query("update {$dbtbpre}enewsloginfail set num=num+1,lasttime='$time' where ip='$ip' limit 1");
  23. }
  24. else
  25. {
  26. $empire->query("insert into {$dbtbpre}enewsloginfail(ip,num,lasttime) values('$ip',1,'$time');");
  27. }
  28. //日志
  29. insert_log($username,$password,0,$ip,$loginauth);
  30. }
  31. //验证登录次数
  32. function CheckLoginNum($ip,$time){
  33. global $empire,$public_r,$dbtbpre;
  34. //COOKIE验证
  35. $loginnum=intval(getcvar('loginnum'));
  36. $lastlogintime=intval(getcvar('lastlogintime'));
  37. if($lastlogintime)
  38. {
  39. if($time-$lastlogintime<$public_r['logintime']*60)
  40. {
  41. if($loginnum>=$public_r['loginnum'])
  42. {
  43. printerror("LoginOutNum",eAdminLoginReturnUrl(0));
  44. }
  45. }
  46. }
  47. //数据库验证
  48. $chtime=$time-$public_r['logintime']*60;
  49. $num=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsloginfail where ip='$ip' and num>=$public_r[loginnum] and lasttime>$chtime limit 1");
  50. if($num)
  51. {
  52. printerror("LoginOutNum",eAdminLoginReturnUrl(0));
  53. }
  54. }
  55. //登陆
  56. function login($username,$password,$key,$post){
  57. global $empire,$public_r,$dbtbpre,$ecms_config;
  58. $username=RepPostVar($username);
  59. $password=RepPostVar($password);
  60. if(!$username||!$password)
  61. {
  62. printerror("EmptyKey",eAdminLoginReturnUrl(0));
  63. }
  64. //验证码
  65. $keyvname='checkkey';
  66. if(!$public_r['adminloginkey'])
  67. {
  68. ecmsCheckShowKey($keyvname,$key,0,0,1);
  69. }
  70. if(strlen($username)>30||strlen($password)>30)
  71. {
  72. printerror("EmptyKey",eAdminLoginReturnUrl(0));
  73. }
  74. $loginip=egetip();
  75. $logintime=time();
  76. CheckLoginNum($loginip,$logintime);
  77. //认证码
  78. if($ecms_config['esafe']['loginauth'])
  79. {
  80. if('dg'.$ecms_config['esafe']['loginauth']!='dg'.$post['loginauth'])
  81. {
  82. InsertErrorLoginNum($username,$password,1,$loginip,$logintime);
  83. printerror("ErrorLoginAuth",eAdminLoginReturnUrl(0));
  84. }
  85. }
  86. $user_r=$empire->fetch1("select userid,password,salt,salt2,lasttime,lastip,addtime,addip,userprikey,lastipport,addipport from {$dbtbpre}enewsuser where username='".$username."' and checked=0 limit 1");
  87. if(!$user_r['userid'])
  88. {
  89. InsertErrorLoginNum($username,$password,0,$loginip,$logintime);
  90. printerror("LoginFail",eAdminLoginReturnUrl(0));
  91. }
  92. $ch_password=DoEmpireCMSAdminPassword($password,$user_r['salt'],$user_r['salt2']);
  93. if('dg'.$user_r['password']!='dg'.$ch_password)
  94. {
  95. InsertErrorLoginNum($username,$password,0,$loginip,$logintime);
  96. printerror("LoginFail",eAdminLoginReturnUrl(0));
  97. }
  98. //安全问答
  99. $user_addr=$empire->fetch1("select userid,equestion,eanswer,openip,certkey from {$dbtbpre}enewsuseradd where userid='$user_r[userid]'");
  100. if(!$user_addr['userid'])
  101. {
  102. InsertErrorLoginNum($username,$password,0,$loginip,$logintime);
  103. printerror("LoginFail",eAdminLoginReturnUrl(0));
  104. }
  105. if($user_addr['equestion'])
  106. {
  107. $equestion=(int)$post['equestion'];
  108. $eanswer=$post['eanswer'];
  109. if($user_addr['equestion']!=$equestion)
  110. {
  111. InsertErrorLoginNum($username,$password,0,$loginip,$logintime);
  112. printerror("LoginFail",eAdminLoginReturnUrl(0));
  113. }
  114. $ckeanswer=ReturnHLoginQuestionStr($user_r['userid'],$username,$user_addr['equestion'],$eanswer);
  115. if('dg'.$ckeanswer!='dg'.$user_addr['eanswer'])
  116. {
  117. InsertErrorLoginNum($username,$password,0,$loginip,$logintime);
  118. printerror("LoginFail",eAdminLoginReturnUrl(0));
  119. }
  120. }
  121. //IP限制
  122. if($user_addr['openip'])
  123. {
  124. eCheckAccessAdminLoginIp($user_addr['openip']);
  125. }
  126. //取得随机密码
  127. $rnd=make_password(20);
  128. $loginipport=egetipport();
  129. $sql=$empire->query("update {$dbtbpre}enewsuser set rnd='$rnd',loginnum=loginnum+1,lastip='$loginip',lasttime='$logintime',pretime='$user_r[lasttime]',preip='".RepPostVar($user_r[lastip])."',lastipport='$loginipport',preipport='".RepPostVar($user_r[lastipport])."' where username='$username' limit 1");
  130. $r=$empire->fetch1("select groupid,userid,styleid,userprikey from {$dbtbpre}enewsuser where username='$username' limit 1");
  131. //样式
  132. if(empty($r[styleid]))
  133. {
  134. $stylepath=$public_r['defadminstyle']?$public_r['defadminstyle']:1;
  135. }
  136. else
  137. {
  138. $styler=$empire->fetch1("select path,styleid from {$dbtbpre}enewsadminstyle where styleid='$r[styleid]'");
  139. if(empty($styler[styleid]))
  140. {
  141. $stylepath=$public_r['defadminstyle']?$public_r['defadminstyle']:1;
  142. }
  143. else
  144. {
  145. $stylepath=$styler['path'];
  146. }
  147. }
  148. //设置备份
  149. $cdbdata=0;
  150. $bnum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsgroup where groupid='$r[groupid]' and dodbdata=1");
  151. if($bnum)
  152. {
  153. $cdbdata=1;
  154. $set5=esetcookie("ecmsdodbdata","empirecms",0,1);
  155. }
  156. else
  157. {
  158. $set5=esetcookie("ecmsdodbdata","",0,1);
  159. }
  160. ecmsEmptyShowKey($keyvname,0,1);//清空验证码
  161. $set4=esetcookie("loginuserid",$r[userid],0,1);
  162. $set1=esetcookie("loginusername",$username,0,1);
  163. $set2=esetcookie("loginrnd",$rnd,0,1);
  164. $set3=esetcookie("loginlevel",$r[groupid],0,1);
  165. $set5=esetcookie("eloginlic","empirecmslic",0,1);
  166. $set6=esetcookie("loginadminstyleid",$stylepath,0,1);
  167. //COOKIE加密验证
  168. DoEDelFileRnd($r[userid]);
  169. DoECookieRnd($r[userid],$username,$rnd,$r['userprikey'],$cdbdata,$r[groupid],intval($stylepath),$logintime);
  170. //最后登陆时间
  171. $set4=esetcookie("logintime",$logintime,0,1);
  172. $set5=esetcookie("truelogintime",$logintime,0,1);
  173. esetcookie('ecertkeyrnds','',0);
  174. //写入日志
  175. insert_log($username,'',1,$loginip,0);
  176. //FireWall
  177. FWSetPassword();
  178. if($set1&&$set2&&$set3)
  179. {
  180. $cache_enews='doclass,doinfo,douserinfo';
  181. $cache_ecmstourl='admin.php'.urlencode(hReturnEcmsHashStrDef(1,'ehref'));
  182. $cache_mess='LoginSuccess';
  183. $cache_url="CreateCache.php?enews=$cache_enews&ecmstourl=$cache_ecmstourl&mess=$cache_mess".hReturnEcmsHashStrDef(0,'ehref');
  184. //操作日志
  185. insert_dolog("");
  186. if($post['adminwindow'])
  187. {
  188. ?>
  189. <script>
  190. AdminWin=window.open("<?=$cache_url?>","EmpireCMS","scrollbars");
  191. AdminWin.moveTo(0,0);
  192. AdminWin.resizeTo(screen.width,screen.height-30);
  193. self.location.href="blank.php";
  194. </script>
  195. <?
  196. exit();
  197. }
  198. else
  199. {
  200. //printerror("LoginSuccess",$cache_url);
  201. echo'<meta http-equiv="refresh" content="0;url='.$cache_url.'">';
  202. db_close();
  203. $empire=null;
  204. exit();
  205. }
  206. }
  207. else
  208. {
  209. printerror("NotCookie",eAdminLoginReturnUrl(0));
  210. }
  211. }
  212. //写入登录日志
  213. function insert_log($username,$password,$status,$loginip,$loginauth){
  214. global $empire,$ecms_config,$dbtbpre;
  215. if($ecms_config['esafe']['theloginlog'])
  216. {
  217. return "";
  218. }
  219. $password=RepPostVar($password);
  220. $loginauth=RepPostVar($loginauth);
  221. $password='';
  222. if($password)
  223. {
  224. $password=preg_replace("/^(.{".round(strlen($password) / 4)."})(.+?)(.{".round(strlen($password) / 6)."})$/s", "\\1***\\3", $password);
  225. }
  226. $password=RepPostVar($password);
  227. $username=RepPostVar($username);
  228. $loginip=RepPostVar($loginip);
  229. $ipport=egetipport();
  230. $status=RepPostVar($status);
  231. $logintime=date("Y-m-d H:i:s");
  232. $sql=$empire->query("insert into {$dbtbpre}enewslog(username,loginip,logintime,status,password,loginauth,ipport) values('$username','$loginip','$logintime','$status','$password','$loginauth','$ipport');");
  233. }
  234. //退出登陆
  235. function loginout($userid,$username,$rnd){
  236. global $empire,$dbtbpre,$ecms_config;
  237. $userid=(int)$userid;
  238. if(!$userid||!$username)
  239. {
  240. printerror("NotLogin","history.go(-1)");
  241. }
  242. $set1=esetcookie("loginuserid","",0,1);
  243. $set2=esetcookie("loginusername","",0,1);
  244. $set3=esetcookie("loginrnd","",0,1);
  245. $set4=esetcookie("loginlevel","",0,1);
  246. //COOKIERND
  247. DelECookieRnd();
  248. DelESessionRnd();
  249. DelECookieAdminLoginFileInfo();
  250. //FireWall
  251. FWEmptyPassword();
  252. //取得随机密码
  253. $rnd=make_password(20);
  254. $sql=$empire->query("update {$dbtbpre}enewsuser set rnd='$rnd' where userid='$userid'");
  255. DoEDelFileRnd($userid);
  256. DoEDelAndAuthRnd($userid);
  257. //操作日志
  258. insert_dolog("");
  259. printerror("ExitSuccess","index.php");
  260. }
  261. //验证登录IP
  262. function eCheckAccessAdminLoginIp($openips){
  263. if(empty($openips))
  264. {
  265. return '';
  266. }
  267. $userip=egetip();
  268. //允许IP
  269. if($openips)
  270. {
  271. $close=1;
  272. foreach(explode("\n",$openips) as $ctrlip)
  273. {
  274. if(preg_match("/^(".preg_quote(($ctrlip=trim($ctrlip)),'/').")/",$userip))
  275. {
  276. $close=0;
  277. break;
  278. }
  279. }
  280. if($close==1)
  281. {
  282. echo"Ip<font color='#cccccc'>(".$userip.")</font> be prohibited.";
  283. exit();
  284. }
  285. }
  286. }
  287. //返回地址
  288. function eAdminLoginReturnUrl($ecms=0){
  289. $eurl=EcmsGetReturnUrl();
  290. return $eurl;
  291. }
  292. ?>