PageRenderTime 70ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/admin/admincp.php

https://bitbucket.org/johnroyer/phpwind8.7
PHP | 833 lines | 767 code | 36 blank | 30 comment | 235 complexity | 6ea13d3cdee253f85bf8d00b1e80eb17 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * Copyright (c) 2003-09 phpwind.net. All rights reserved.
  5. * Support : http://www.phpwind.net
  6. * This software is the proprietary information of phpwind.com.
  7. *
  8. */
  9. !defined('R_P') && exit('Forbidden');
  10. define('P_W','admincp');
  11. define('UC_CLIENT_ROOT', R_P . '/uc_client/');
  12. (isset($_GET['ajax']) && $_GET['ajax'] == 1) && define('AJAX', 1);
  13. function_exists('date_default_timezone_set') && date_default_timezone_set('Etc/GMT+0');
  14. require_once(R_P.'require/common.php');
  15. require_once(R_P.'require/functions.php');
  16. //S::filter();
  17. //modified@2010/7/7 S&P
  18. S::filter();
  19. //* include_once pwCache::getPath(D_P.'data/bbscache/config.php');
  20. pwCache::getData(D_P.'data/bbscache/config.php');
  21. define('AREA_PATH', R_P . $db_htmdir . '/channel/');
  22. define('PORTAL_PATH', R_P . $db_htmdir . '/portal/');
  23. $db_userurl = ($db_htmifopen && $db_userurlopen) ? 'u/' : 'u.php?uid='; //url
  24. define('USER_URL',$db_userurl);
  25. $timestamp = time();
  26. $db_cvtime != 0 && $timestamp += $db_cvtime*60;
  27. $onlineip = pwGetIp();
  28. $db_cc && pwDefendCc($db_cc);
  29. $ceversion = defined('CE') ? 1 : 0;
  30. #phpwind version
  31. list($wind_version,$wind_repair,$wind_from) = explode(',',WIND_VERSION);
  32. S::gp(array('adminjob','admintype','adminitem','type','hackset','a_type','action','verify','adskin','job','ajax','admin_keyword'));
  33. if (strpos($adminjob,'..') !== false || $admintype && strpos($admintype,'..') !== false) {
  34. exit('Forbidden');
  35. }
  36. if ($ajax) define('AJAX','1');
  37. if ($db_forcecharset && !defined('AJAX')) {
  38. @header("Content-Type:text/html; charset=$db_charset");
  39. }
  40. ObStart();
  41. file_exists('install.php') && adminmsg('installfile_exists');
  42. $admin_file = $pwServer['PHP_SELF'];
  43. $REQUEST_URI = trim($pwServer['PHP_SELF'].'?'.$pwServer['QUERY_STRING'],'#');
  44. if ($adminjob == 'quit') {
  45. Cookie('AdminUser','',0);
  46. ObHeader($admin_file);
  47. }
  48. $imgpath = $db_http != 'N' ? $db_http : $db_picpath;
  49. $attachpath = $db_attachurl != 'N' ? $db_attachurl : "$db_bbsurl/$db_attachname";
  50. $imgdir = R_P.$db_picpath;
  51. $attachdir = R_P.$db_attachname;
  52. $pw_posts = 'pw_posts';
  53. $pw_tmsgs = 'pw_tmsgs';
  54. if (D_P != R_P && $db_http != 'N') {
  55. $R_url = substr($db_http,-1)=='/' ? substr($db_http,0,-1) : $db_http;
  56. $R_url = substr($R_url,0,strrpos($R_url,'/'));
  57. } else {
  58. $R_url = $db_bbsurl;
  59. }
  60. //* include_once pwCache::getPath(D_P."data/style/wind.php");
  61. //* include_once pwCache::getPath(D_P.'data/sql_config.php');
  62. //* include_once pwCache::getPath(D_P.'data/bbscache/forum_cache.php');
  63. pwCache::getData(D_P."data/style/wind.php");
  64. require D_P.'data/sql_config.php';
  65. pwCache::getData(D_P.'data/bbscache/forum_cache.php');
  66. require_once(R_P.'admin/cache.php');
  67. !is_array($manager) && $manager = array();
  68. !is_array($manager_pwd) && $manager_pwd = array();
  69. $newmanager = $newmngpwd = array();
  70. foreach ($manager as $key => $value) {
  71. if (!empty($value) && !is_array($value)) {
  72. $newmanager[$key] = $value;
  73. $newmngpwd[$key] = $manager_pwd[$key];
  74. }
  75. }
  76. $manager = $newmanager;
  77. $manager_pwd = $newmngpwd;
  78. $H_url = $db_wwwurl;
  79. $B_url = $db_bbsurl;
  80. if ($database=='mysqli' && Pwloaddl('mysqli')===false) {
  81. $database = 'mysql';
  82. }
  83. $bbsrecordfile = D_P.'data/bbscache/admin_record.php';
  84. !file_exists($bbsrecordfile) && writeover($bbsrecordfile,"<?php die;?>\n");
  85. /** !file_exists($bbsrecordfile) && pwCache::setData($bbsrecordfile,"<?php die;?>\n"); **/
  86. $F_count = F_L_count($bbsrecordfile,2000);
  87. $L_T = 1200-($timestamp-pwFilemtime($bbsrecordfile));
  88. $L_left = 15-$F_count;
  89. if ($F_count>15 && $L_T>0) {
  90. $db_adminrecord = 0;
  91. Cookie('AdminUser','',0);
  92. adminmsg('login_fail');
  93. }
  94. if (empty($manager)) {
  95. if (file_exists(D_P.'data/sql_config.php')) {
  96. adminmsg('managerinfo_error');
  97. } else {
  98. adminmsg('sql_config');
  99. }
  100. }
  101. $CK = array();$admin_name = '';
  102. if ($_POST['admin_pwd'] && $_POST['admin_name']) {
  103. if ($db_gdcheck & 32) {
  104. GdConfirm($_POST['lg_num']);
  105. }
  106. $admin_name = stripcslashes($_POST['admin_name']);
  107. $safecv = $db_ifsafecv ? questcode($_POST['question'],$_POST['customquest'],$_POST['answer']) : '';
  108. $CK = array($timestamp,$_POST['admin_name'],md5(PwdCode(md5($_POST['admin_pwd'])).$timestamp.getHashSegment()),$safecv);
  109. Cookie('AdminUser',StrCode(implode("\t",$CK)));
  110. } else {
  111. $AdminUser = GetCookie('AdminUser');
  112. if ($AdminUser) {
  113. $CK = explode("\t",StrCode($AdminUser,'DECODE'));
  114. $admin_name = stripcslashes($CK[1]);
  115. }
  116. }
  117. if (!empty($CK)) {
  118. PwNewDB();
  119. $rightset = checkpass($CK);
  120. } else {
  121. $db = null;
  122. $rightset = array();
  123. }
  124. if (empty($rightset)) {
  125. if ($_POST['admin_name'] || $_POST['admin_pwd']) {
  126. writeover($bbsrecordfile,'|'.str_replace('|','&#124;',S::escapeChar($_POST['admin_name'])).'|'.str_replace('|','&#124;',S::escapeChar($_POST['admin_pwd']))."|Logging Failed|$onlineip|$timestamp|\n",'ab');
  127. /** pwCache::setData($bbsrecordfile,'|'.str_replace('|','&#124;',S::escapeChar($_POST['admin_name'])).'|'.str_replace('|','&#124;',S::escapeChar($_POST['admin_pwd']))."|Logging Failed|$onlineip|$timestamp|\n", false, 'ab'); **/
  128. $db_adminrecord = 0;
  129. $REQUEST_URI = $pwServer['PHP_SELF'];
  130. Cookie('AdminUser','',0);
  131. if ($L_left) {
  132. adminmsg('login_error');
  133. } else {
  134. adminmsg('login_fail');
  135. }
  136. }
  137. S::gp(array("ajax"));
  138. if ($ajax == 1) {
  139. define('AJAX',1);
  140. adminmsg('login_invalid');
  141. }
  142. Cookie('AdminUser','',0);
  143. include PrintEot('adminlogin');afooter(true);
  144. } elseif ($_POST['admin_name']) {
  145. $userService = L::loadClass('UserService', 'user'); /* @var $userService PW_UserService */
  146. $uid = $userService->getUserIdByUserName($admin_name);
  147. $slog = $db->get_value("SELECT slog FROM pw_administrators WHERE uid=" . S::sqlEscape($uid,false));
  148. $slog = explode(";",$slog);
  149. !$slog && $slog = array();
  150. if (count($slog) >= 8) unset($slog[0]);
  151. array_push($slog,$timestamp.','.$onlineip);
  152. $slog = implode(";",$slog);
  153. $db->update("UPDATE pw_administrators SET slog=".S::sqlEscape($slog,false)."WHERE uid=" . S::sqlEscape($uid,false));
  154. $REQUEST_URI = trim($REQUEST_URI,'?#');
  155. ObHeader($REQUEST_URI);
  156. }
  157. $bubbleInfo = $rightset['bubble'];
  158. $uidForBubble = $rightset['uid'];
  159. $admin_gid = $rightset['gid'];
  160. if ($db_ifsafecv && strpos($db_safegroup,",$admin_gid,")!==false && !$CK[3]) {
  161. Cookie('AdminUser','',0);
  162. adminmsg('safecv_prompt');
  163. }
  164. //* include_once pwCache::getPath(D_P.'data/bbscache/level.php');
  165. pwCache::getData(D_P.'data/bbscache/level.php');
  166. !defined('If_manager') && define('If_manager',0);
  167. if (!If_manager) {
  168. Iplimit();
  169. $temp_a = array_merge($_POST,$_GET);
  170. foreach ($temp_a as $key => $value) {
  171. if ($key!='module') {
  172. S::checkVar($value);
  173. }
  174. }
  175. unset($temp_a);
  176. $admin_level = $ltitle[$admin_gid];
  177. } else {
  178. $admin_level = getLangInfo('other','admin_level');//'manager';
  179. }
  180. $_postdata = $_POST ? PostLog($_POST) : '';
  181. $new_record = '|'.str_replace('|','&#124;',S::escapeChar($admin_name)).'||'.str_replace('|','&#124;',S::escapeChar($REQUEST_URI))."|$onlineip|$timestamp|$_postdata|\n";
  182. writeover($bbsrecordfile,$new_record,"ab");
  183. //* pwCache::setData($bbsrecordfile,$new_record, false, "ab");
  184. if ($pwServer['REQUEST_METHOD'] == 'POST') {
  185. $referer_a = @parse_url($pwServer['HTTP_REFERER']);
  186. if ($referer_a['host']) {
  187. list($http_host) = explode(':',$pwServer['HTTP_HOST']);
  188. if ($referer_a['host']!=$http_host) {
  189. adminmsg('undefined_action');
  190. }
  191. }
  192. unset($referer_a);
  193. PostCheck($verify);
  194. }
  195. unset($_postdata,$new_record,$bbsrecordfile,$dbhost,$dbuser,$dbpw,$dbname,$pconnect,$newmanager,$newmngpwd);
  196. $thisPWTabs = $_GET['tab'] ? S::escapeChar($_GET['tab']) : S::escapeChar($_COOKIE['thisPWTabs']);
  197. function HtmlConvert(&$array) {
  198. if (is_array($array)) {
  199. foreach ($array as $key => $value) {
  200. if (!is_array($value)) {
  201. $array[$key] = htmlspecialchars($value);
  202. } else {
  203. HtmlConvert($array[$key]);
  204. }
  205. }
  206. } else {
  207. $array = htmlspecialchars($array);
  208. }
  209. }
  210. function checkpass($CK) {
  211. S::slashes($CK);
  212. global $db,$manager,$db_ifsafecv;
  213. if (S::inArray($CK[1],$manager)) {
  214. global $manager_pwd;
  215. $v_key = array_search($CK[1],$manager);
  216. $ifQuery = true; // In order ot get bubble info
  217. if (!SafeCheck($CK,PwdCode($manager_pwd[$v_key]))) {
  218. $userService = L::loadClass('UserService', 'user'); /* @var $userService PW_UserService */
  219. $rt = $userService->getByUserName($CK[1], true, true);
  220. if (!SafeCheck($CK,PwdCode($rt['password'])) || $db_ifsafecv && $rt['safecv']!=$CK['3']) {
  221. return false;
  222. }
  223. if (!admincheck($rt['uid'],$rt['username'],$rt['groupid'],$rt['groups'],'check')) {
  224. return false;
  225. }
  226. $ifQuery = false;
  227. } elseif ($db_ifsafecv) {
  228. $userService = L::loadClass('UserService', 'user'); /* @var $userService PW_UserService */
  229. $rt = $userService->getByUserName($CK[1], true, true);
  230. if ($rt && $rt['safecv']!=$CK['3']) return false;
  231. $ifQuery = false;
  232. }
  233. if ($ifQuery) {
  234. $userService = L::loadClass('UserService', 'user'); /* @var $userService PW_UserService */
  235. $rt = $userService->getByUserName($CK[1], true, true);
  236. }
  237. define('If_manager',1);
  238. $rightset['gid'] = 3;
  239. $rightset['all'] = 1;
  240. $rightset['bubble'] = $rt['bubble'];
  241. require GetLang('purview');
  242. foreach ($purview as $key=>$value) {
  243. $rightset[$key] = 1;
  244. }
  245. foreach ($nav_manager['option'] as $key => $value) {
  246. $rightset[$key] = 1;
  247. }
  248. } else {
  249. $rt = $db->get_one("SELECT m.uid,m.username,m.groupid,m.groups,m.password,m.safecv,m.groupid,u.gptype,p.rvalue as allowadmincp,md.bubble FROM pw_members m LEFT JOIN pw_usergroups u ON u.gid=m.groupid LEFT JOIN pw_permission p ON p.uid='0' AND p.fid='0' AND p.gid=m.groupid AND p.rkey='allowadmincp' LEFT JOIN pw_memberdata md ON md.uid = m.uid WHERE m.username=".S::sqlEscape($CK[1]));
  250. if (!$rt['allowadmincp'] || ($rt['gptype']!='system' && $rt['gptype']!='special') || $db_ifsafecv && $rt['safecv'] != $CK['3']) {
  251. return false;
  252. }
  253. if (!SafeCheck($CK,PwdCode($rt['password'])) || !admincheck($rt['uid'],$CK[1],$rt['groupid'],$rt['groups'],'check')) {
  254. return false;
  255. }
  256. $rightset = $db->get_value('SELECT value FROM pw_adminset WHERE gid='.S::sqlEscape($rt['groupid']));
  257. if ($rightset) {
  258. if (!is_array($rightset = unserialize($rightset))) {
  259. $rightset = array();
  260. }
  261. } else {
  262. $rightset = array();
  263. }
  264. require GetLang('purview');
  265. foreach ($rightset as $key=>$value) {
  266. $rightset[$key] = (isset($purview[$key]) && $rightset[$key]==1) ? 1 : 0;
  267. }
  268. $rightset['gid'] = $rt['groupid'];
  269. $rightset['bubble'] = $rt['bubble'];
  270. }
  271. $rightset['uid'] = $rt['uid'];
  272. return $rightset;
  273. }
  274. function gets($filename,$value='4096') {
  275. $getcontent = '';
  276. if ($handle = @fopen($filename,'rb')) {
  277. flock($handle,LOCK_SH);
  278. $getcontent = fread($handle,$value);//fgets調試
  279. fclose($handle);
  280. }
  281. return $getcontent;
  282. }
  283. function Showmsg($msg,$jumpurl='',$t=2,$langtype='index') {
  284. adminmsg($msg,$jumpurl,$t,$langtype);
  285. }
  286. function adminmsg($msg,$jumpurl='',$t=2,$langtype='admin') {
  287. @extract($GLOBALS,EXTR_SKIP);
  288. if ($langtype == 'admin') {
  289. $msg = getLangInfo('cpmsg',$msg);
  290. } else {
  291. $msg = getLangInfo('msg',$msg);
  292. }
  293. if (defined('AJAX')) {
  294. echo $msg;ajax_footer();
  295. }
  296. if ($jumpurl != '') {
  297. $basename = $jumpurl;
  298. $ifjump = "<meta http-equiv='Refresh' content='$t; url=$jumpurl'>";
  299. } elseif (!$basename) {
  300. $basename = $REQUEST_URI;
  301. }
  302. if ($adminjob!='manager' && $db_adminrecord==1 && $basename!='javascript:history.go(-1);' && !$fromgd) {
  303. $adminmsg = 2;
  304. } else {
  305. $adminmsg = 1;
  306. }
  307. if (strpos($pwServer['HTTP_USER_AGENT'],'MSIE 7.0;') !== false) {
  308. list($basename) = explode('#',$basename);
  309. list($jumpurl) = explode('#',$jumpurl);
  310. }
  311. include PrintEot('message');
  312. $cachetime = $timestamp-3600*24;
  313. if (readover(D_P.'data/bbscache/none.txt')!='' || pwFilemtime(D_P.'data/bbscache/file_lock.txt')<$cachetime || pwFilemtime(D_P.'data/bbscache/info.txt')<$cachetime || pwFilemtime(D_P.'data/bbscache/userpay.txt')<$cachetime) {
  314. echo '<script type="text/javascript">if (parent.notice) {parent.notice.location.href = "'.$admin_file.'?adminjob=notice";}</script>';
  315. }
  316. afooter();
  317. }
  318. function ieconvert($msg) {
  319. if (is_array($msg)) {
  320. foreach ($msg as $key=>$value) {
  321. $msg[$key] = ieconvert($value);
  322. }
  323. } else {
  324. $msg = str_replace(array("\t","\r",' '),array('','','&nbsp; '),$msg);
  325. }
  326. return $msg;
  327. }
  328. function Quot_cv($msg){
  329. return str_replace('"','&quot;',$msg);
  330. }
  331. function deldir($path){
  332. if (file_exists($path)) {
  333. if (is_file($path)) {
  334. P_unlink($path);
  335. } else {
  336. $handle = opendir($path);
  337. while ($file = readdir($handle)) {
  338. if ($file!='' && !in_array($file,array('.','..'))) {
  339. if (is_dir("$path/$file")) {
  340. deldir("$path/$file");
  341. } else {
  342. P_unlink("$path/$file");
  343. }
  344. }
  345. }
  346. closedir($handle);
  347. rmdir($path);
  348. }
  349. }
  350. }
  351. //phpwind
  352. function ifadmin($username){
  353. global $db;
  354. $query=$db->query("SELECT forumadmin FROM pw_forums WHERE forumadmin!=''");
  355. while($forum=$db->fetch_array($query)){
  356. if($forum['forumadmin'] && strpos($forum['forumadmin'],",$username,")!==false){
  357. return true;
  358. }
  359. }
  360. return false;
  361. }
  362. function ifcheck($var,$out) {
  363. $GLOBALS[$out.'_Y'] = $GLOBALS[$out.'_N'] = '';
  364. $GLOBALS[$out.'_'.($var ? 'Y' : 'N')] = 'checked';
  365. }
  366. function F_L_count($filename,$offset){
  367. global $onlineip;
  368. $count=0;
  369. if($fp=@fopen($filename,"rb")){
  370. flock($fp,LOCK_SH);
  371. fseek($fp,-$offset,SEEK_END);
  372. $readb=fread($fp,$offset);
  373. fclose($fp);
  374. $readb=trim($readb);
  375. $readb=explode("\n",$readb);
  376. $count=count($readb);$count_F=0;
  377. for($i=$count-1;$i>0;$i--){
  378. if(strpos($readb[$i],"|Logging Failed|$onlineip|")===false){
  379. break;
  380. }
  381. $count_F++;
  382. }
  383. }
  384. return $count_F;
  385. }
  386. function GetLang($lang,$type='admin',$EXT='php'){
  387. global $tplpath;
  388. !in_array($lang,array('all','cpmsg','left','rightset','purview','search','dbtable')) && $type = 'index';
  389. if ($type <> 'admin') {
  390. if (file_exists(R_P."template/$tplpath/lang_$lang.$EXT")) {
  391. return R_P."template/$tplpath/lang_$lang.$EXT";
  392. } elseif (file_exists(R_P."template/wind/lang_$lang.$EXT")) {
  393. return R_P."template/wind/lang_$lang.$EXT";
  394. } else {
  395. exit("Can not find lang_$lang.$EXT file");
  396. }
  397. }
  398. if (file_exists(R_P."template/admin_$tplpath/cp_lang_$lang.$EXT")) {
  399. return R_P."template/admin_$tplpath/cp_lang_$lang.$EXT";
  400. } elseif (file_exists(R_P."template/admin/cp_lang_$lang.$EXT")) {
  401. return R_P."template/admin/cp_lang_$lang.$EXT";
  402. } else {
  403. exit("Can not find cp_lang_$lang.$EXT file");
  404. }
  405. }
  406. function PrintEot($template,$EXT='htm') {
  407. $tplpath = L::style('tplpath');
  408. !$template && $template = 'N';
  409. //cms
  410. if ($template=='bbscode' || in_array($template,array('wysiwyg_editor_common','c_header','c_footer'))) {
  411. if (file_exists(R_P."template/$tplpath/$template.$EXT")) {
  412. return R_P."template/$tplpath/$template.$EXT";
  413. } elseif (file_exists(R_P."template/wind/$template.$EXT")) {
  414. return R_P."template/wind/$template.$EXT";
  415. } else {
  416. exit("Can not find $template.$EXT file");
  417. }
  418. }
  419. //cms
  420. if (file_exists(R_P."template/admin/$template.$EXT")) {
  421. return R_P."template/admin/$template.$EXT";
  422. } else {
  423. exit("Can not find $template.$EXT file");
  424. }
  425. }
  426. function afooter($unfoot=null){
  427. static $showafooter;
  428. global $db_redundancy,$wind_version,$db,$db_debug,$admin_keyword;
  429. $showafooter = false;
  430. if (empty($unfoot)) {
  431. $showafooter = true;
  432. require PrintEot('adminbottom');
  433. }
  434. $output = ob_get_contents();
  435. $output = str_replace(array('<!--<!--<!---->','<!--<!---->','<!---->-->','<!---->'),'',$output);
  436. if ($admin_keyword) {
  437. $output = preg_replace('/('.preg_quote($admin_keyword, '/').')([^">;]*<)(?!\/script|\/textarea)/si','<font color="red"><u>\\1</u></font>\\2',$output);
  438. }
  439. $output = preg_replace(
  440. "/\<form([^\<\>]*)\saction=['|\"]?([^\s\"'\<\>]+)['|\"]?([^\<\>]*)\>/ies",
  441. "FormCheck('\\1','\\2','\\3')",
  442. rtrim($output,'<!--')
  443. );
  444. echo ObContents($output);
  445. unset($output);
  446. if (defined('SHOWLOG')) Error::writeLog();
  447. exit;
  448. }
  449. function readlog($filename,$offset=1024000) {
  450. $readb = array();
  451. if ($fp = @fopen($filename,"rb")) {
  452. flock($fp,LOCK_SH);
  453. $size = filesize($filename);
  454. $size > $offset ? fseek($fp,-$offset,SEEK_END): $offset = $size;
  455. $readb = fread($fp,$offset);
  456. fclose($fp);
  457. $readb = str_replace("\n","\n<:wind:>",$readb);
  458. $readb = explode("<:wind:>",$readb);
  459. $count = count($readb);
  460. if ($readb[$count-1] == '' || $readb[$count-1] == "\r") {unset($readb[$count-1]);}
  461. if (empty($readb)) {$readb[0] = "";}
  462. }
  463. return $readb;
  464. }
  465. function checkselid($selid) {
  466. if (is_array($selid)) {
  467. $ret = array();
  468. foreach ($selid as $key => $value) {
  469. if (!is_numeric($value)) {
  470. return false;
  471. }
  472. $ret[] = $value;
  473. }
  474. return S::sqlImplode($ret);
  475. } else {
  476. return '';
  477. }
  478. }
  479. function ObHeader($URL) {
  480. echo '<meta http-equiv="expires" content="0">';
  481. echo '<meta http-equiv="Pragma" content="no-cache">';
  482. echo '<meta http-equiv="Cache-Control" content="no-cache">';
  483. echo "<meta http-equiv='refresh' content='0;url=$URL'>";exit;
  484. }
  485. function GetAllowForum($username) {
  486. global $db;
  487. $allowfid = $forumoption = '';
  488. $query = $db->query("SELECT fid,name,forumadmin FROM pw_forums WHERE type!='category' AND (forumadmin LIKE ".S::sqlEscape("%,$username,%")."OR fupadmin LIKE ".S::sqlEscape("%,$username,%").')');
  489. while ($rt = $db->fetch_array($query)) {
  490. $allowfid .= ','.$rt['fid'];
  491. $forumoption .= "<option value=\"$rt[fid]\"> >> $rt[name]</option>";
  492. }
  493. $allowfid = trim($allowfid,',');
  494. return array($allowfid,$forumoption);
  495. }
  496. function GetHiddenForum() {
  497. global $db;
  498. $forumoption = '<option></option>';
  499. $allowfid = '';
  500. $query = $db->query("SELECT fid,name FROM pw_forums WHERE f_type='hidden'");
  501. while ($rt = $db->fetch_array($query)) {
  502. $allowfid .= ','.$rt['fid'];
  503. $forumoption .= "<option value=\"$rt[fid]\"> &nbsp;|- $rt[name]</option>";
  504. }
  505. $allowfid = trim($allowfid,',');
  506. return array($allowfid,$forumoption);
  507. }
  508. function Iplimit() {
  509. global $db_iplimit;
  510. if ($db_iplimit) {
  511. global $onlineip;
  512. $allowip = false;
  513. $ip_a = explode(',',$db_iplimit);
  514. foreach ($ip_a as $value) {
  515. $value = trim($value);
  516. if ($value && strpos(",$onlineip.",",$value.") !== false) {
  517. $allowip = true;
  518. break;
  519. }
  520. }
  521. if (!$allowip) {
  522. Cookie('AdminUser','',0);
  523. adminmsg('ip_ban');
  524. }
  525. }
  526. }
  527. function PostLog($log) {
  528. foreach ($log as $key => $val) {
  529. $key = str_replace(array("\n","\r","|"),array('\n','\r','&#124;'),$key);
  530. if (is_array($val)) {
  531. $data .= "$key=array(".PostLog($val).")";
  532. } else {
  533. $val = str_replace(array("\n","\r","|"),array('\n','\r','&#124;'),$val);
  534. if ($key == 'password' || $key == 'check_pwd') {
  535. $data .= "$key=***, ";
  536. } else {
  537. $data .= "$key=$val, ";
  538. }
  539. }
  540. }
  541. return $data;
  542. }
  543. function GdConfirm($code,$t=1) {
  544. Cookie('cknum','',0);
  545. if (!$code || !SafeCheck(explode("\t",StrCode(GetCookie('cknum'),'DECODE')),strtoupper($code),'cknum',300)) {
  546. global $basename,$admin_file;
  547. $t && Cookie('AdminUser','',0);
  548. $basename = $admin_file;
  549. $GLOBALS['fromgd'] = 1;
  550. adminmsg('check_error');
  551. }
  552. }
  553. function EncodeUrl($url,$r=false) {
  554. global $db_hash,$admin_name,$admin_gid;
  555. $url_a = substr($url,strrpos($url,'?')+1);
  556. substr($url,-1) == '&' && $url = substr($url,0,-1);
  557. parse_str($url_a,$url_a);
  558. $source = '';
  559. foreach ($url_a as $key => $val) {
  560. if ($key != 'verify') $source .= $key.$val;
  561. }
  562. $posthash = substr(md5($source.$admin_name.$admin_gid.$db_hash),0,8);
  563. if ($r) {
  564. return $posthash;
  565. } else {
  566. $url .= "&verify=$posthash";
  567. return $url;
  568. }
  569. }
  570. function FormCheck($pre,$url,$add){
  571. $pre = stripslashes($pre);
  572. $add = stripslashes($add);
  573. return "<form{$pre} action=\"".EncodeUrl($url)."&\"{$add}>";
  574. }
  575. function PostCheck($verify){
  576. global $db_hash,$admin_name,$admin_gid;
  577. $source = '';
  578. foreach ($_GET as $key => $val) {
  579. if (!in_array($key,array('verify','nowtime'))) {
  580. $source .= $key.$val;
  581. }
  582. }
  583. if ($verify != substr(md5($source.$admin_name.$admin_gid.$db_hash),0,8)) {
  584. adminmsg('illegal_request');
  585. }
  586. return true;
  587. }
  588. function PrintHack($template,$EXT="htm") {
  589. return H_P.'template/'.$template.".$EXT";
  590. }
  591. function PrintMode($template,$EXT="htm") {
  592. return M_P.'template/admin/'.$template.".$EXT";
  593. }
  594. function PrintApp($template,$EXT="htm") {
  595. return A_P."template/$template.".$EXT;
  596. }
  597. function maxmin($id) {
  598. global $tlistdb;
  599. $tidmax = $tidmin = 0;
  600. foreach ($tlistdb as $key => $val) {
  601. if ($key == $id) {
  602. $tidmin = $val[1];
  603. break;
  604. }
  605. $tidmax = $val[1];
  606. }
  607. return array($tidmin,$tidmax);
  608. }
  609. function admincheck($uid,$username,$groupid,$groups,$action) {
  610. global $db;
  611. if ($action == 'check') {
  612. $rt = $db->get_one("SELECT username,groupid,groups FROM pw_administrators WHERE uid=".S::sqlEscape($uid));
  613. if ($rt && $rt['username'] == $username && ($rt['groupid'] == $groupid || strpos($rt['groups'], ",$groupid,") !== false)) {
  614. return true;
  615. } else {
  616. return false;
  617. }
  618. } elseif ($action == 'update') {
  619. $rt = $db->get_one("SELECT username,groupid,groups FROM pw_administrators WHERE uid=".S::sqlEscape($uid));
  620. if (empty($rt)) {
  621. $db->update("INSERT INTO pw_administrators SET " . S::sqlSingle(array(
  622. 'uid' => $uid,
  623. 'username' => $username,
  624. 'groupid' => $groupid,
  625. 'groups' => $groups
  626. )));
  627. } elseif ($rt['username'] != $username || $rt['groupid'] != $groupid || $rt['groups'] != $groups) {
  628. $db->update("UPDATE pw_administrators SET " . S::sqlSingle(array(
  629. 'username' => $username,
  630. 'groupid' => $groupid,
  631. 'groups' => $groups
  632. )) . " WHERE uid=".S::sqlEscape($uid));
  633. }
  634. } elseif ($action == 'delete') {
  635. $db->update("DELETE FROM pw_administrators WHERE uid=".S::sqlEscape($uid));
  636. } else {
  637. return false;
  638. }
  639. }
  640. function questcode($question,$customquest,$answer) {
  641. $question = $question=='-1' ? $customquest : $question;
  642. return $question ? substr(md5(md5($question).md5($answer)),8,10) : '';
  643. }
  644. function Pwloaddl($mod,$ckfunc='mysqli_get_client_info') {
  645. static $isallowed = null;
  646. if (extension_loaded($mod)) {
  647. if ($ckfunc && !function_exists($ckfunc)) return false;
  648. return true;
  649. }
  650. return false;
  651. if ($isallowed===null) {
  652. if (!@ini_get('safe_mode') && @ini_get('enable_dl') && @function_exists('dl') && @function_exists('phpinfo')) {
  653. ob_start();
  654. @phpinfo(INFO_GENERAL);
  655. $infomsg = strip_tags(ob_get_contents());
  656. ob_end_clean();
  657. if (preg_match('/thread safety\s*enabled/i',$infomsg) && !preg_match('/server api\s*\(cgi\|cli\)/i',$infomsg)) {
  658. $isallowed = false;
  659. } else {
  660. $isallowed = true;
  661. }
  662. } else {
  663. $isallowed = false;
  664. }
  665. }
  666. if (!$isallowed) return false;
  667. if (strncasecmp(PHP_OS,'win',3) == 0) {
  668. $module = "php_$mod.dll";
  669. } elseif (PHP_OS=='HP-UX') {
  670. $module = "$mod.sl";
  671. } else {
  672. $module ="$mod.so";
  673. }
  674. @dl(S::escapePath($module));
  675. if ($ckfunc && !function_exists($ckfunc)) {
  676. return false;
  677. }
  678. }
  679. function pwConfirm($msg,$inputmsg=null) {
  680. @extract($GLOBALS,EXTR_SKIP);
  681. $adminmsg = 0;
  682. $msg = getLangInfo('cpmsg',$msg);
  683. include PrintEot('message');afooter();
  684. }
  685. function adminRightCheck($key){
  686. global $rightset;
  687. return isset($rightset[$key]) && $rightset[$key] == 1;
  688. }
  689. /**
  690. * 後台公共分頁js
  691. * @param unknown_type $count
  692. * @param unknown_type $page
  693. * @param unknown_type $numofpage
  694. * @param unknown_type $event
  695. */
  696. function pagerforjs($count, $page, $numofpage, $event) {
  697. global $tablecolor;
  698. $total = $numofpage;
  699. if (!empty($max)) {
  700. $max = (int) $max;
  701. $numofpage > $max && $numofpage = $max;
  702. }
  703. if ($numofpage <= 1 || !is_numeric($page)) {
  704. return '';
  705. } else {
  706. $pages = "<div class=\"pages\"><a href=\"javascript:;\" $event page=\"1\">&laquo;</a>";
  707. for ($i = $page - 3; $i <= $page - 1; $i++) {
  708. if ($i < 1) continue;
  709. $pages .= "<a page=\"$i\" href=\"javascript:;\" $event >$i</a>";
  710. }
  711. $pages .= "<b>$page</b>";
  712. if ($page < $numofpage) {
  713. $flag = 0;
  714. for ($i = $page + 1; $i <= $numofpage; $i++) {
  715. $pages .= "<a page=\"$i\" href=\"javascript:;\" $event >$i</a>";
  716. $flag++;
  717. if ($flag == 4) break;
  718. }
  719. }
  720. $pages .= "<a page=\"$numofpage\" href=\"javascript:;\" $event >&raquo;</a><div class=\"fl\">共{$total}頁</div><span class=\"pagesone\"><input id=\"input_page\" type=\"text\" size=\"3\" onkeydown=\"javascript: if(event.keyCode==13){this.nextSibling.onclick();return false;}\"><button $event >Go</button></span></div>";
  721. return $pages;
  722. }
  723. }
  724. function updateadmin() {
  725. global $db;
  726. $f_admin = array();
  727. $query = $db->query("SELECT forumadmin FROM pw_forums");
  728. while ($forum = $db->fetch_array($query)) {
  729. $adminarray = explode(",",$forum['forumadmin']);
  730. foreach ($adminarray as $key => $value) {
  731. $value = trim($value);
  732. if ($value) {
  733. $f_admin[] = $value;
  734. }
  735. }
  736. }
  737. $f_admin = array_unique($f_admin);
  738. $userService = L::loadClass('UserService', 'user'); /* @var $userService PW_UserService */
  739. $query = $db->query("SELECT uid,username,groupid,groups FROM pw_administrators WHERE groupid=5 OR groups LIKE '%,5,%'");
  740. while ($rt = $db->fetch_array($query)) {
  741. if (!in_array($rt['username'],$f_admin)) {
  742. if ($rt['groupid'] == '5') {
  743. $userService->update($rt['uid'], array('groupid'=>-1));
  744. $rt['groupid'] = -1;
  745. } else {
  746. $rt['groups'] = str_replace(',5,',',',$rt['groups']);
  747. $rt['groups'] == ',' && $rt['groups'] = '';
  748. $userService->update($rt['uid'], array('groups'=>$rt['groups']));
  749. }
  750. if ($rt['groupid'] == '-1' && $rt['groups'] == '') {
  751. admincheck($rt['uid'],$rt['username'],$rt['groupid'],$rt['groups'],'delete');
  752. } else {
  753. admincheck($rt['uid'],$rt['username'],$rt['groupid'],$rt['groups'],'update');
  754. }
  755. }
  756. }
  757. if ($f_admin) {
  758. $userService = L::loadClass('UserService', 'user'); /* @var $userService PW_UserService */
  759. $usernames = S::sqlImplode($f_admin);
  760. $pwSQL = array();
  761. $query = $db->query("SELECT m.uid,m.username,m.groupid,m.groups,a.groupid AS gid,a.groups AS gps FROM pw_members m LEFT JOIN pw_administrators a ON m.uid=a.uid WHERE m.username IN($usernames)");
  762. while ($rt = $db->fetch_array($query)) {
  763. if ($rt['groupid'] == '-1') {
  764. $rt['groups'] = str_replace(',5,',',',$rt['groups']);
  765. $rt['groups'] == ',' && $rt['groups'] = '';
  766. //$rt['groups'] = $rt['groups'] ? $rt['groups'].'5,' : ",5,";
  767. $userService->update($rt['uid'], array('groupid'=>5, 'groups'=>$rt['groups']));
  768. $rt['groupid'] = 5;
  769. } elseif ($rt['groupid'] != '5' && strpos($rt['groups'],',5,') === false) {
  770. $rt['groups'] = $rt['groups'] ? $rt['groups'].'5,' : ",5,";
  771. $userService->update($rt['uid'], array('groups'=>$rt['groups']));
  772. }
  773. if ($rt['groupid'] <> $rt['gid'] || $rt['groups'] <> $rt['gps']) {
  774. $pwSQL[] = array($rt['uid'],$rt['username'],$rt['groupid'],$rt['groups']);
  775. }
  776. }
  777. if ($pwSQL) {
  778. $db->update("REPLACE INTO pw_administrators (uid,username,groupid,groups) VALUES ".S::sqlMulti($pwSQL));
  779. }
  780. }
  781. }
  782. function setstatus(&$status, $b, $setv = '1') {
  783. --$b;
  784. for ($i = strlen($setv) - 1; $i >= 0; $i--) {
  785. if ($setv[$i]) {
  786. $status |= 1 << $b;
  787. } else {
  788. $status &= ~(1 << $b);
  789. }
  790. ++$b;
  791. }
  792. //return $status;
  793. }
  794. ?>