func.inc.php 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. <?php
  2. /*
  3. [Discuz!] (C)2001-2099 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: forum.func.php 14122 2008-08-20 06:06:33Z cnteacher $
  6. */
  7. if(!defined('IN_COMSENZ')) {
  8. exit('Access Denied');
  9. }
  10. function show_msg($error_no, $error_msg = 'ok', $success = 1, $quit = TRUE) {
  11. if(VIEW_OFF) {
  12. $error_code = $success ? 0 : constant(strtoupper($error_no));
  13. $error_msg = empty($error_msg) ? $error_no : $error_msg;
  14. $error_msg = str_replace('"', '\"', $error_msg);
  15. $str = "<root>\n";
  16. $str .= "\t<error errorCode=\"$error_code\" errorMessage=\"$error_msg\" />\n";
  17. $str .= "</root>";
  18. echo $str;
  19. exit;
  20. } else {
  21. show_header();
  22. global $step;
  23. $title = lang($error_no);
  24. $comment = lang($error_no.'_comment', false);
  25. $errormsg = '';
  26. if($error_msg) {
  27. if(!empty($error_msg)) {
  28. foreach ((array)$error_msg as $k => $v) {
  29. if(is_numeric($k)) {
  30. $comment .= "<li><em class=\"red\">".lang($v)."</em></li>";
  31. }
  32. }
  33. }
  34. }
  35. if($step > 0) {
  36. echo "<div class=\"desc\"><b>$title</b><ul>$comment</ul>";
  37. } else {
  38. echo "</div><div class=\"main\" style=\"margin-top: -123px;\"><b>$title</b><ul style=\"line-height: 200%; margin-left: 30px;\">$comment</ul>";
  39. }
  40. if($quit) {
  41. echo '<br /><span class="red">'.lang('error_quit_msg').'</span><br /><br /><br />';
  42. }
  43. echo '<input type="button" onclick="history.back()" value="'.lang('click_to_back').'" /><br /><br /><br />';
  44. echo '</div>';
  45. $quit && show_footer();
  46. }
  47. }
  48. function check_db($dbhost, $dbuser, $dbpw, $dbname, $tablepre) {
  49. if(!function_exists('mysql_connect') && !function_exists('mysqli_connect')) {
  50. show_msg('undefine_func', 'mysql_connect', 0);
  51. }
  52. $mysqlmode = function_exists('mysql_connect') ? 'mysql' : 'mysqli';
  53. $link = ($mysqlmode == 'mysql') ? @mysql_connect($dbhost, $dbuser, $dbpw) : new mysqli($dbhost, $dbuser, $dbpw);
  54. if(!$link) {
  55. $errno = ($mysqlmode == 'mysql') ? mysql_errno() : mysqli_errno();
  56. $error = ($mysqlmode == 'mysql') ? mysql_error() : mysqli_error();
  57. if($errno == 1045) {
  58. show_msg('database_errno_1045', $error, 0);
  59. } elseif($errno == 2003) {
  60. show_msg('database_errno_2003', $error, 0);
  61. } else {
  62. show_msg('database_connect_error', $error, 0);
  63. }
  64. } else {
  65. if($query = (($mysqlmode == 'mysql') ? @mysql_query("SHOW TABLES FROM $dbname") : $link->query("SHOW TABLES FROM $dbname"))) {
  66. if(!$query) {
  67. return false;
  68. }
  69. while($row = (($mysqlmode == 'mysql') ? mysql_fetch_row($query) : $query->fetch_row())) {
  70. if(preg_match("/^$tablepre/", $row[0])) {
  71. return false;
  72. }
  73. }
  74. }
  75. }
  76. return true;
  77. }
  78. function dirfile_check(&$dirfile_items) {
  79. foreach($dirfile_items as $key => $item) {
  80. $item_path = $item['path'];
  81. if($item['type'] == 'dir') {
  82. if(!dir_writeable(ROOT_PATH.$item_path)) {
  83. if(is_dir(ROOT_PATH.$item_path)) {
  84. $dirfile_items[$key]['status'] = 0;
  85. $dirfile_items[$key]['current'] = '+r';
  86. } else {
  87. $dirfile_items[$key]['status'] = -1;
  88. $dirfile_items[$key]['current'] = 'nodir';
  89. }
  90. } else {
  91. $dirfile_items[$key]['status'] = 1;
  92. $dirfile_items[$key]['current'] = '+r+w';
  93. }
  94. } else {
  95. if(file_exists(ROOT_PATH.$item_path)) {
  96. if(is_writable(ROOT_PATH.$item_path)) {
  97. $dirfile_items[$key]['status'] = 1;
  98. $dirfile_items[$key]['current'] = '+r+w';
  99. } else {
  100. $dirfile_items[$key]['status'] = 0;
  101. $dirfile_items[$key]['current'] = '+r';
  102. }
  103. } else {
  104. if(dir_writeable(dirname(ROOT_PATH.$item_path))) {
  105. $dirfile_items[$key]['status'] = 1;
  106. $dirfile_items[$key]['current'] = '+r+w';
  107. } else {
  108. $dirfile_items[$key]['status'] = -1;
  109. $dirfile_items[$key]['current'] = 'nofile';
  110. }
  111. }
  112. }
  113. }
  114. }
  115. function env_check(&$env_items) {
  116. foreach($env_items as $key => $item) {
  117. if($key == 'php') {
  118. $env_items[$key]['current'] = PHP_VERSION;
  119. } elseif($key == 'attachmentupload') {
  120. $env_items[$key]['current'] = @ini_get('file_uploads') ? ini_get('upload_max_filesize') : 'unknow';
  121. } elseif($key == 'gdversion') {
  122. $tmp = function_exists('gd_info') ? gd_info() : array();
  123. $env_items[$key]['current'] = empty($tmp['GD Version']) ? 'noext' : $tmp['GD Version'];
  124. unset($tmp);
  125. } elseif($key == 'diskspace') {
  126. if(function_exists('disk_free_space')) {
  127. $env_items[$key]['current'] = floor(disk_free_space(ROOT_PATH) / (1024*1024)).'M';
  128. } else {
  129. $env_items[$key]['current'] = 'unknow';
  130. }
  131. } elseif(isset($item['c'])) {
  132. $env_items[$key]['current'] = constant($item['c']);
  133. }
  134. $env_items[$key]['status'] = 1;
  135. if($item['r'] != 'notset' && strcmp($env_items[$key]['current'], $item['r']) < 0) {
  136. $env_items[$key]['status'] = 0;
  137. }
  138. }
  139. }
  140. function function_check(&$func_items) {
  141. foreach($func_items as $item) {
  142. function_exists($item) or show_msg('undefine_func', $item, 0);
  143. }
  144. }
  145. function show_env_result(&$env_items, &$dirfile_items, &$func_items) {
  146. $env_str = $file_str = $dir_str = $func_str = '';
  147. $error_code = 0;
  148. foreach($env_items as $key => $item) {
  149. if($key == 'php' && strcmp($item['current'], $item['r']) < 0) {
  150. show_msg('php_version_too_low', $item['current'], 0);
  151. }
  152. $status = 1;
  153. if($item['r'] != 'notset') {
  154. if(intval($item['current']) && intval($item['r'])) {
  155. if(intval($item['current']) < intval($item['r'])) {
  156. $status = 0;
  157. $error_code = ENV_CHECK_ERROR;
  158. }
  159. } else {
  160. if(strcmp($item['current'], $item['r']) < 0) {
  161. $status = 0;
  162. $error_code = ENV_CHECK_ERROR;
  163. }
  164. }
  165. }
  166. if(VIEW_OFF) {
  167. $env_str .= "\t\t<runCondition name=\"$key\" status=\"$status\" Require=\"$item[r]\" Best=\"$item[b]\" Current=\"$item[current]\"/>\n";
  168. } else {
  169. $env_str .= "<tr>\n";
  170. $env_str .= "<td>".lang($key)."</td>\n";
  171. $env_str .= "<td class=\"padleft\">".lang($item['r'])."</td>\n";
  172. $env_str .= "<td class=\"padleft\">".lang($item['b'])."</td>\n";
  173. $env_str .= ($status ? "<td class=\"w pdleft1\">" : "<td class=\"nw pdleft1\">").$item['current']."</td>\n";
  174. $env_str .= "</tr>\n";
  175. }
  176. }
  177. foreach($dirfile_items as $key => $item) {
  178. $tagname = $item['type'] == 'file' ? 'File' : 'Dir';
  179. $variable = $item['type'].'_str';
  180. if(VIEW_OFF) {
  181. if($item['status'] == 0) {
  182. $error_code = ENV_CHECK_ERROR;
  183. }
  184. $$variable .= "\t\t\t<File name=\"$item[path]\" status=\"$item[status]\" requirePermisson=\"+r+w\" currentPermisson=\"$item[current]\" />\n";
  185. } else {
  186. $$variable .= "<tr>\n";
  187. $$variable .= "<td>$item[path]</td><td class=\"w pdleft1\">".lang('writeable')."</td>\n";
  188. if($item['status'] == 1) {
  189. $$variable .= "<td class=\"w pdleft1\">".lang('writeable')."</td>\n";
  190. } elseif($item['status'] == -1) {
  191. $error_code = ENV_CHECK_ERROR;
  192. $$variable .= "<td class=\"nw pdleft1\">".lang('nodir')."</td>\n";
  193. } else {
  194. $error_code = ENV_CHECK_ERROR;
  195. $$variable .= "<td class=\"nw pdleft1\">".lang('unwriteable')."</td>\n";
  196. }
  197. $$variable .= "</tr>\n";
  198. }
  199. }
  200. if(VIEW_OFF) {
  201. $str = "<root>\n";
  202. $str .= "\t<runConditions>\n";
  203. $str .= $env_str;
  204. $str .= "\t</runConditions>\n";
  205. $str .= "\t<FileDirs>\n";
  206. $str .= "\t\t<Dirs>\n";
  207. $str .= $dir_str;
  208. $str .= "\t\t</Dirs>\n";
  209. $str .= "\t\t<Files>\n";
  210. $str .= $file_str;
  211. $str .= "\t\t</Files>\n";
  212. $str .= "\t</FileDirs>\n";
  213. $str .= "\t<error errorCode=\"$error_code\" errorMessage=\"\" />\n";
  214. $str .= "</root>";
  215. echo $str;
  216. exit;
  217. } else {
  218. show_header();
  219. echo "<h2 class=\"title\">".lang('env_check')."</h2>\n";
  220. echo "<table class=\"tb\" style=\"margin:20px 0 20px 55px;\">\n";
  221. echo "<tr>\n";
  222. echo "\t<th>".lang('project')."</th>\n";
  223. echo "\t<th class=\"padleft\">".lang('ucenter_required')."</th>\n";
  224. echo "\t<th class=\"padleft\">".lang('ucenter_best')."</th>\n";
  225. echo "\t<th class=\"padleft\">".lang('curr_server')."</th>\n";
  226. echo "</tr>\n";
  227. echo $env_str;
  228. echo "</table>\n";
  229. echo "<h2 class=\"title\">".lang('priv_check')."</h2>\n";
  230. echo "<table class=\"tb\" style=\"margin:20px 0 20px 55px;width:90%;\">\n";
  231. echo "\t<tr>\n";
  232. echo "\t<th>".lang('step1_file')."</th>\n";
  233. echo "\t<th class=\"padleft\">".lang('step1_need_status')."</th>\n";
  234. echo "\t<th class=\"padleft\">".lang('step1_status')."</th>\n";
  235. echo "</tr>\n";
  236. echo $file_str;
  237. echo $dir_str;
  238. echo "</table>\n";
  239. foreach($func_items as $item) {
  240. $status = function_exists($item);
  241. $func_str .= "<tr>\n";
  242. $func_str .= "<td>$item()</td>\n";
  243. if($status) {
  244. $func_str .= "<td class=\"w pdleft1\">".lang('supportted')."</td>\n";
  245. $func_str .= "<td class=\"padleft\">".lang('none')."</td>\n";
  246. } else {
  247. $error_code = ENV_CHECK_ERROR;
  248. $func_str .= "<td class=\"nw pdleft1\">".lang('unsupportted')."</td>\n";
  249. $func_str .= "<td><font color=\"red\">".lang('advice_'.$item)."</font></td>\n";
  250. }
  251. }
  252. echo "<h2 class=\"title\">".lang('func_depend')."</h2>\n";
  253. echo "<table class=\"tb\" style=\"margin:20px 0 20px 55px;width:90%;\">\n";
  254. echo "<tr>\n";
  255. echo "\t<th>".lang('func_name')."</th>\n";
  256. echo "\t<th class=\"padleft\">".lang('check_result')."</th>\n";
  257. echo "\t<th class=\"padleft\">".lang('suggestion')."</th>\n";
  258. echo "</tr>\n";
  259. echo $func_str;
  260. echo "</table>\n";
  261. show_next_step(2, $error_code);
  262. show_footer();
  263. }
  264. }
  265. function show_next_step($step, $error_code) {
  266. echo "<form action=\"index.php\" method=\"get\">\n";
  267. echo "<input type=\"hidden\" name=\"step\" value=\"$step\" />";
  268. if(isset($GLOBALS['hidden'])) {
  269. echo $GLOBALS['hidden'];
  270. }
  271. if($error_code == 0) {
  272. $nextstep = "<input type=\"button\" onclick=\"history.back();\" value=\"".lang('old_step')."\"><input type=\"submit\" value=\"".lang('new_step')."\">\n";
  273. } else {
  274. $nextstep = "<input type=\"button\" disabled=\"disabled\" value=\"".lang('not_continue')."\">\n";
  275. }
  276. echo "<div class=\"btnbox marginbot\">".$nextstep."</div>\n";
  277. echo "</form>\n";
  278. }
  279. function show_form(&$form_items, $error_msg) {
  280. global $step;
  281. if(empty($form_items) || !is_array($form_items)) {
  282. return;
  283. }
  284. show_header();
  285. show_setting('start');
  286. show_setting('hidden', 'step', $step);
  287. $is_first = 1;
  288. foreach($form_items as $key => $items) {
  289. global ${'error_'.$key};
  290. if($is_first == 0) {
  291. echo '</table>';
  292. }
  293. if(!${'error_'.$key}) {
  294. show_tips('tips_'.$key);
  295. } else {
  296. show_error('tips_admin_config', ${'error_'.$key});
  297. }
  298. if($is_first == 0) {
  299. echo '<table class="tb2">';
  300. }
  301. foreach($items as $k => $v) {
  302. global $$k;
  303. if(!empty($error_msg)) {
  304. $value = isset($_POST[$key][$k]) ? $_POST[$key][$k] : '';
  305. } else {
  306. if(isset($v['value']) && is_array($v['value'])) {
  307. if($v['value']['type'] == 'constant') {
  308. $value = defined($v['value']['var']) ? constant($v['value']['var']) : '';
  309. } elseif($v['value']['type'] == 'var') {
  310. $value = $GLOBALS[$v['value']['var']];
  311. } elseif($v['value']['type'] == 'string') {
  312. $value = $v['value']['var'];
  313. }
  314. } else {
  315. $value = '';
  316. }
  317. }
  318. if($v['type'] == 'checkbox') {
  319. $value = '1';
  320. }
  321. show_setting($k, $key.'['.$k.']', $value, $v['type'], isset($error_msg[$key][$k]) ? $key.'_'.$k.'_invalid' : '');
  322. }
  323. if($is_first) {
  324. $is_first = 0;
  325. }
  326. }
  327. show_setting('', 'submitname', 'new_step', 'submit');
  328. show_setting('end');
  329. show_footer();
  330. }
  331. function show_license() {
  332. global $self, $uchidden, $step;
  333. $next = $step + 1;
  334. if(VIEW_OFF) {
  335. show_msg('license_contents', lang('license'), 1);
  336. } else {
  337. show_header();
  338. $license = str_replace(' ', '&nbsp; ', lang('license'));
  339. $lang_agreement_yes = lang('agreement_yes');
  340. $lang_agreement_no = lang('agreement_no');
  341. echo <<<EOT
  342. </div>
  343. <div class="main" style="margin-top:-123px;">
  344. <div class="licenseblock">$license</div>
  345. <div class="btnbox marginbot">
  346. <form method="get" action="index.php">
  347. <input type="hidden" name="step" value="$next">
  348. $uchidden
  349. <input type="submit" name="submit" value="{$lang_agreement_yes}" style="padding: 2px">&nbsp;
  350. <input type="button" name="exit" value="{$lang_agreement_no}" style="padding: 2px" onclick="javascript: window.close(); return false;">
  351. </form>
  352. </div>
  353. EOT;
  354. show_footer();
  355. }
  356. }
  357. if(!function_exists('file_put_contents')) {
  358. function file_put_contents($filename, $s) {
  359. $fp = @fopen($filename, 'w');
  360. @fwrite($fp, $s);
  361. @fclose($fp);
  362. return TRUE;
  363. }
  364. }
  365. function createtable($sql) {
  366. $type = strtoupper(preg_replace("/^\s*CREATE TABLE\s+.+\s+\(.+?\).*(ENGINE|TYPE)\s*=\s*([a-z]+?).*$/isU", "\\2", $sql));
  367. $type = in_array($type, array('MYISAM', 'HEAP')) ? $type : 'MYISAM';
  368. return preg_replace("/^\s*(CREATE TABLE\s+.+\s+\(.+?\)).*$/isU", "\\1", $sql).
  369. (mysql_get_server_info() > '4.1' ? " ENGINE=$type DEFAULT CHARSET=".DBCHARSET : " TYPE=$type");
  370. }
  371. function dir_writeable($dir) {
  372. $writeable = 0;
  373. if(!is_dir($dir)) {
  374. @mkdir($dir, 0777);
  375. }
  376. if(is_dir($dir)) {
  377. if($fp = @fopen("$dir/test.txt", 'w')) {
  378. @fclose($fp);
  379. @unlink("$dir/test.txt");
  380. $writeable = 1;
  381. } else {
  382. $writeable = 0;
  383. }
  384. }
  385. return $writeable;
  386. }
  387. function dir_clear($dir) {
  388. global $lang;
  389. showjsmessage($lang['clear_dir'].' '.str_replace(ROOT_PATH, '', $dir));
  390. $directory = dir($dir);
  391. while($entry = $directory->read()) {
  392. $filename = $dir.'/'.$entry;
  393. if(is_file($filename)) {
  394. @unlink($filename);
  395. }
  396. }
  397. $directory->close();
  398. @touch($dir.'/index.htm');
  399. }
  400. function show_header() {
  401. define('SHOW_HEADER', TRUE);
  402. global $step;
  403. $version = SOFT_VERSION;
  404. $release = SOFT_RELEASE;
  405. $install_lang = lang(INSTALL_LANG);
  406. $title = lang('title_install');
  407. $charset = CHARSET;
  408. echo <<<EOT
  409. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  410. <html xmlns="http://www.w3.org/1999/xhtml">
  411. <head>
  412. <meta http-equiv="Content-Type" content="text/html; charset=$charset" />
  413. <title>$title</title>
  414. <link rel="stylesheet" href="style.css" type="text/css" media="all" />
  415. <script type="text/javascript">
  416. function $(id) {
  417. return document.getElementById(id);
  418. }
  419. function showmessage(message) {
  420. $('notice').value += message + "\\r\\n";
  421. }
  422. </script>
  423. <meta content="Comsenz Inc." name="Copyright" />
  424. </head>
  425. <div class="container">
  426. <div class="header">
  427. <h1>$title</h1>
  428. <span>V$version $install_lang $release</span>
  429. EOT;
  430. $step > 0 && show_step($step);
  431. }
  432. function show_footer($quit = true) {
  433. echo <<<EOT
  434. <div class="footer">&copy;2001 - 2017 <a href="http://www.comsenz.com/">Comsenz</a> Inc.</div>
  435. </div>
  436. </div>
  437. </body>
  438. </html>
  439. EOT;
  440. $quit && exit();
  441. }
  442. function loginit($logfile) {
  443. global $lang;
  444. showjsmessage($lang['init_log'].' '.$logfile);
  445. if($fp = @fopen('./forumdata/logs/'.$logfile.'.php', 'w')) {
  446. fwrite($fp, '<'.'?PHP exit(); ?'.">\n");
  447. fclose($fp);
  448. }
  449. }
  450. function showjsmessage($message) {
  451. if(VIEW_OFF) return;
  452. echo '<script type="text/javascript">showmessage(\''.addslashes($message).' \');</script>'."\r\n";
  453. flush();
  454. ob_flush();
  455. }
  456. function random($length) {
  457. $hash = '';
  458. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
  459. $max = strlen($chars) - 1;
  460. PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
  461. for($i = 0; $i < $length; $i++) {
  462. $hash .= $chars[mt_rand(0, $max)];
  463. }
  464. return $hash;
  465. }
  466. function redirect($url) {
  467. echo "<script>".
  468. "function redirect() {window.location.replace('$url');}\n".
  469. "setTimeout('redirect();', 0);\n".
  470. "</script>";
  471. exit();
  472. }
  473. function get_onlineip() {
  474. $onlineip = '';
  475. if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
  476. $onlineip = getenv('HTTP_CLIENT_IP');
  477. } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
  478. $onlineip = getenv('HTTP_X_FORWARDED_FOR');
  479. } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
  480. $onlineip = getenv('REMOTE_ADDR');
  481. } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
  482. $onlineip = $_SERVER['REMOTE_ADDR'];
  483. }
  484. return $onlineip;
  485. }
  486. function config_edit() {
  487. extract($GLOBALS, EXTR_SKIP);
  488. $ucsalt = substr(uniqid(rand()), 0, 6);
  489. $ucfounderpw= md5(md5($ucfounderpw).$ucsalt);
  490. $regdate = time();
  491. $ucauthkey = generate_key();
  492. $ucsiteid = generate_key();
  493. $ucmykey = generate_key();
  494. $config = "<?php \r\ndefine('UC_DBHOST', '$dbhost');\r\n";
  495. $config .= "define('UC_DBUSER', '$dbuser');\r\n";
  496. $config .= "define('UC_DBPW', '$dbpw');\r\n";
  497. $config .= "define('UC_DBNAME', '$dbname');\r\n";
  498. $config .= "define('UC_DBCHARSET', '".DBCHARSET."');\r\n";
  499. $config .= "define('UC_DBTABLEPRE', '$tablepre');\r\n";
  500. $config .= "define('UC_COOKIEPATH', '/');\r\n";
  501. $config .= "define('UC_COOKIEDOMAIN', '');\r\n";
  502. $config .= "define('UC_DBCONNECT', 0);\r\n";
  503. $config .= "define('UC_CHARSET', '".CHARSET."');\r\n";
  504. $config .= "define('UC_FOUNDERPW', '$ucfounderpw');\r\n";
  505. $config .= "define('UC_FOUNDERSALT', '$ucsalt');\r\n";
  506. $config .= "define('UC_KEY', '$ucauthkey');\r\n";
  507. $config .= "define('UC_SITEID', '$ucsiteid');\r\n";
  508. $config .= "define('UC_MYKEY', '$ucmykey');\r\n";
  509. $config .= "define('UC_DEBUG', false);\r\n";
  510. $config .= "define('UC_PPP', 20);\r\n";
  511. $fp = fopen(CONFIG, 'w');
  512. fwrite($fp, $config);
  513. fclose($fp);
  514. }
  515. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  516. $ckey_length = 4;
  517. $key = md5($key ? $key : UC_KEY);
  518. $keya = md5(substr($key, 0, 16));
  519. $keyb = md5(substr($key, 16, 16));
  520. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  521. $cryptkey = $keya.md5($keya.$keyc);
  522. $key_length = strlen($cryptkey);
  523. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  524. $string_length = strlen($string);
  525. $result = '';
  526. $box = range(0, 255);
  527. $rndkey = array();
  528. for($i = 0; $i <= 255; $i++) {
  529. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  530. }
  531. for($j = $i = 0; $i < 256; $i++) {
  532. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  533. $tmp = $box[$i];
  534. $box[$i] = $box[$j];
  535. $box[$j] = $tmp;
  536. }
  537. for($a = $j = $i = 0; $i < $string_length; $i++) {
  538. $a = ($a + 1) % 256;
  539. $j = ($j + $box[$a]) % 256;
  540. $tmp = $box[$a];
  541. $box[$a] = $box[$j];
  542. $box[$j] = $tmp;
  543. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  544. }
  545. if($operation == 'DECODE') {
  546. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  547. return substr($result, 26);
  548. } else {
  549. return '';
  550. }
  551. } else {
  552. return $keyc.str_replace('=', '', base64_encode($result));
  553. }
  554. }
  555. function generate_key() {
  556. $random = random(32);
  557. $info = md5($_SERVER['SERVER_SOFTWARE'].$_SERVER['SERVER_NAME'].$_SERVER['SERVER_ADDR'].$_SERVER['SERVER_PORT'].$_SERVER['HTTP_USER_AGENT'].time());
  558. $return = '';
  559. for($i=0; $i<64; $i++) {
  560. $p = intval($i/2);
  561. $return[$i] = $i % 2 ? $random[$p] : $info[$p];
  562. }
  563. return implode('', $return);
  564. }
  565. function show_install() {
  566. if(VIEW_OFF) return;
  567. ?>
  568. <script type="text/javascript">
  569. function showmessage(message) {
  570. document.getElementById('notice').value += message + "\r\n";
  571. }
  572. function initinput() {
  573. window.location='<?php echo 'index.php?step='.($GLOBALS['step']);?>';
  574. }
  575. </script>
  576. <div class="main">
  577. <div class="btnbox"><textarea name="notice" style="width: 80%;" readonly="readonly" id="notice"></textarea></div>
  578. <div class="btnbox marginbot">
  579. <input type="button" name="submit" value="<?php echo lang('install_in_processed');?>" disabled style="height: 25" id="laststep" onclick="initinput()">
  580. </div>
  581. <?php
  582. }
  583. function runquery($sql) {
  584. global $lang, $tablepre, $db;
  585. if(!isset($sql) || empty($sql)) return;
  586. $sql = str_replace("\r", "\n", str_replace(' '.ORIG_TABLEPRE, ' '.$tablepre, $sql));
  587. $ret = array();
  588. $num = 0;
  589. foreach(explode(";\n", trim($sql)) as $query) {
  590. $ret[$num] = '';
  591. $queries = explode("\n", trim($query));
  592. foreach($queries as $query) {
  593. $ret[$num] .= (isset($query[0]) && $query[0] == '#') || (isset($query[1]) && isset($query[1]) && $query[0].$query[1] == '--') ? '' : $query;
  594. }
  595. $num++;
  596. }
  597. unset($sql);
  598. foreach($ret as $query) {
  599. $query = trim($query);
  600. if($query) {
  601. if(substr($query, 0, 12) == 'CREATE TABLE') {
  602. $name = preg_replace("/CREATE TABLE ([a-z0-9_]+) .*/is", "\\1", $query);
  603. showjsmessage(lang('create_table').' '.$name.' ... '.lang('succeed'));
  604. $db->query(createtable($query));
  605. } else {
  606. $db->query($query);
  607. }
  608. }
  609. }
  610. }
  611. function charcovert($string) {
  612. if(!get_magic_quotes_gpc()) {
  613. $string = str_replace('\'', '\\\'', $string);
  614. } else {
  615. $string = str_replace('\"', '"', $string);
  616. }
  617. return $string;
  618. }
  619. function insertconfig($s, $find, $replace) {
  620. if(preg_match($find, $s)) {
  621. $s = preg_replace($find, $replace, $s);
  622. } else {
  623. $s .= "\r\n".$replace;
  624. }
  625. return $s;
  626. }
  627. function getgpc($k, $t='GP') {
  628. $t = strtoupper($t);
  629. switch($t) {
  630. case 'GP' : isset($_POST[$k]) ? $var = &$_POST : $var = &$_GET; break;
  631. case 'G': $var = &$_GET; break;
  632. case 'P': $var = &$_POST; break;
  633. case 'C': $var = &$_COOKIE; break;
  634. case 'R': $var = &$_REQUEST; break;
  635. }
  636. return isset($var[$k]) ? $var[$k] : '';
  637. }
  638. function var_to_hidden($k, $v) {
  639. return "<input type=\"hidden\" name=\"$k\" value=\"$v\" />\n";
  640. }
  641. function fsocketopen($hostname, $port = 80, &$errno, &$errstr, $timeout = 15) {
  642. $fp = '';
  643. if(function_exists('fsockopen')) {
  644. $fp = @fsockopen($hostname, $port, $errno, $errstr, $timeout);
  645. } elseif(function_exists('pfsockopen')) {
  646. $fp = @pfsockopen($hostname, $port, $errno, $errstr, $timeout);
  647. } elseif(function_exists('stream_socket_client')) {
  648. $fp = @stream_socket_client($hostname.':'.$port, $errno, $errstr, $timeout);
  649. }
  650. return $fp;
  651. }
  652. function dfopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE) {
  653. $return = '';
  654. $matches = parse_url($url);
  655. $scheme = $matches['scheme'];
  656. $host = $matches['host'];
  657. $path = $matches['path'] ? $matches['path'].(isset($matches['query']) && $matches['query'] ? '?'.$matches['query'] : '') : '/';
  658. $port = !empty($matches['port']) ? $matches['port'] : ($matches['scheme'] == 'https' ? 443 : 80);
  659. if($post) {
  660. $out = "POST $path HTTP/1.0\r\n";
  661. $header = "Accept: */*\r\n";
  662. $header .= "Accept-Language: zh-cn\r\n";
  663. $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  664. $header .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  665. $header .= "Host: $host\r\n";
  666. $header .= 'Content-Length: '.strlen($post)."\r\n";
  667. $header .= "Connection: Close\r\n";
  668. $header .= "Cache-Control: no-cache\r\n";
  669. $header .= "Cookie: $cookie\r\n\r\n";
  670. $out .= $header.$post;
  671. } else {
  672. $out = "GET $path HTTP/1.0\r\n";
  673. $header = "Accept: */*\r\n";
  674. $header .= "Accept-Language: zh-cn\r\n";
  675. $header .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  676. $header .= "Host: $host\r\n";
  677. $header .= "Connection: Close\r\n";
  678. $header .= "Cookie: $cookie\r\n\r\n";
  679. $out .= $header;
  680. }
  681. $fpflag = 0;
  682. if(!$fp = @fsocketopen(($scheme == 'https' ? 'ssl' : $scheme).'://'.($scheme == 'https' ? $host : ($ip ? $ip : $host)), $port, $errno, $errstr, $timeout)) {
  683. $context = array(
  684. 'http' => array(
  685. 'method' => $post ? 'POST' : 'GET',
  686. 'header' => $header,
  687. 'content' => $post,
  688. 'timeout' => $timeout,
  689. ),
  690. );
  691. $context = stream_context_create($context);
  692. $fp = @fopen($scheme.'://'.($scheme == 'https' ? $host : ($ip ? $ip : $host)).':'.$port.$path, 'b', false, $context);
  693. $fpflag = 1;
  694. }
  695. if(!$fp) {
  696. return '';
  697. } else {
  698. stream_set_blocking($fp, $block);
  699. stream_set_timeout($fp, $timeout);
  700. @fwrite($fp, $out);
  701. $status = stream_get_meta_data($fp);
  702. if(!$status['timed_out']) {
  703. while (!feof($fp) && !$fpflag) {
  704. if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) {
  705. break;
  706. }
  707. }
  708. $stop = false;
  709. while(!feof($fp) && !$stop) {
  710. $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
  711. $return .= $data;
  712. if($limit) {
  713. $limit -= strlen($data);
  714. $stop = $limit <= 0;
  715. }
  716. }
  717. }
  718. @fclose($fp);
  719. return $return;
  720. }
  721. }
  722. function check_env() {
  723. global $lang, $attachdir;
  724. $errors = array('quit' => false);
  725. $quit = false;
  726. if(!function_exists('mysql_connect')) {
  727. $errors[] = 'mysql_unsupport';
  728. $quit = true;
  729. }
  730. if(PHP_VERSION < '4.3') {
  731. $errors[] = 'php_version_430';
  732. $quit = true;
  733. }
  734. if(!file_exists(DISCUZ_ROOT.'./config.inc.php')) {
  735. $errors[] = 'config_nonexistence';
  736. $quit = true;
  737. } elseif(!is_writeable(DISCUZ_ROOT.'./config.inc.php')) {
  738. $errors[] = 'config_unwriteable';
  739. $quit = true;
  740. }
  741. $checkdirarray = array(
  742. 'attach' => $attachdir,
  743. 'forumdata' => './forumdata',
  744. 'cache' => './forumdata/cache',
  745. 'ftemplates' => './forumdata/templates',
  746. 'threadcache' => './forumdata/threadcaches',
  747. 'log' => './forumdata/logs',
  748. 'uccache' => './uc_client/data/cache'
  749. );
  750. foreach($checkdirarray as $key => $dir) {
  751. if(!dir_writeable(DISCUZ_ROOT.$dir)) {
  752. $langkey = $key.'_unwriteable';
  753. $errors[] = $key.'_unwriteable';
  754. if(!in_array($key, array('ftemplate'))) {
  755. $quit = TRUE;
  756. }
  757. }
  758. }
  759. $errors['quit'] = $quit;
  760. return $errors;
  761. }
  762. function show_error($type, $errors = '', $quit = false) {
  763. global $lang, $step;
  764. $title = lang($type);
  765. $comment = lang($type.'_comment', false);
  766. $errormsg = '';
  767. if($errors) {
  768. if(!empty($errors)) {
  769. foreach ((array)$errors as $k => $v) {
  770. if(is_numeric($k)) {
  771. $comment .= "<li><em class=\"red\">".lang($v)."</em></li>";
  772. }
  773. }
  774. }
  775. }
  776. if($step > 0) {
  777. echo "<div class=\"desc\"><b>$title</b><ul>$comment</ul>";
  778. } else {
  779. echo "</div><div class=\"main\" style=\"margin-top: -123px;\"><b>$title</b><ul style=\"line-height: 200%; margin-left: 30px;\">$comment</ul>";
  780. }
  781. if($quit) {
  782. echo '<br /><span class="red">'.$lang['error_quit_msg'].'</span><br /><br /><br /><br /><br /><br />';
  783. }
  784. echo '</div>';
  785. $quit && show_footer();
  786. }
  787. function show_tips($tip, $title = '', $comment = '', $style = 1) {
  788. global $lang;
  789. $title = empty($title) ? lang($tip) : $title;
  790. $comment = empty($comment) ? lang($tip.'_comment', FALSE) : $comment;
  791. if($style) {
  792. echo "<div class=\"desc\"><b>$title</b>";
  793. } else {
  794. echo "</div><div class=\"main\" style=\"margin-top: -123px;\">$title<div class=\"desc1 marginbot\"><ul>";
  795. }
  796. $comment && print('<br>'.$comment);
  797. echo "</div>";
  798. }
  799. function show_setting($setname, $varname = '', $value = '', $type = 'text|password|checkbox', $error = '') {
  800. if($setname == 'start') {
  801. echo "<form method=\"post\" action=\"index.php\">\n<table class=\"tb2\">\n";
  802. return;
  803. } elseif($setname == 'end') {
  804. echo "\n</table>\n</form>\n";
  805. return;
  806. } elseif($setname == 'hidden') {
  807. echo "<input type=\"hidden\" name=\"$varname\" value=\"$value\">\n";
  808. return;
  809. }
  810. echo "\n".'<tr><th class="tbopt'.($error ? ' red' : '').'">&nbsp;'.(empty($setname) ? '' : lang($setname).':')."</th>\n<td>";
  811. if($type == 'text' || $type == 'password') {
  812. $value = dhtmlspecialchars($value);
  813. echo "<input type=\"$type\" name=\"$varname\" value=\"$value\" size=\"35\" class=\"txt\">";
  814. } elseif($type == 'submit') {
  815. $value = empty($value) ? 'next_step' : $value;
  816. echo "<input type=\"submit\" name=\"$varname\" value=\"".lang($value)."\" class=\"btn\">\n";
  817. } elseif($type == 'checkbox') {
  818. if(!is_array($varname) && !is_array($value)) {
  819. echo'<label><input type="checkbox" name="'.$varname.'" value="'.$value."\" style=\"border: 0\">".lang($setname.'_check_label')."</label>\n";
  820. }
  821. } else {
  822. echo $value;
  823. }
  824. echo "</td>\n<td>&nbsp;";
  825. if($error) {
  826. $comment = '<span class="red">'.(is_string($error) ? lang($error) : lang($setname.'_error')).'</span>';
  827. } else {
  828. $comment = lang($setname.'_comment', false);
  829. }
  830. echo "$comment</td>\n</tr>\n";
  831. return true;
  832. }
  833. function show_step($step) {
  834. global $method;
  835. $laststep = 4;
  836. $title = lang('step_'.$method.'_title');
  837. $comment = lang('step_'.$method.'_desc');
  838. $stepclass = array();
  839. for($i = 1; $i <= $laststep; $i++) {
  840. $stepclass[$i] = $i == $step ? 'current' : ($i < $step ? '' : 'unactivated');
  841. }
  842. $stepclass[$laststep] .= ' last';
  843. echo <<<EOT
  844. <div class="setup step{$step}">
  845. <h2>$title</h2>
  846. <p>$comment</p>
  847. </div>
  848. <div class="stepstat">
  849. <ul>
  850. <li class="$stepclass[1]">1</li>
  851. <li class="$stepclass[2]">2</li>
  852. <li class="$stepclass[3]">3</li>
  853. <li class="$stepclass[4]">4</li>
  854. </ul>
  855. <div class="stepstatbg stepstat1"></div>
  856. </div>
  857. </div>
  858. <div class="main">
  859. EOT;
  860. }
  861. function lang($lang_key, $force = true) {
  862. return isset($GLOBALS['lang'][$lang_key]) ? $GLOBALS['lang'][$lang_key] : ($force ? $lang_key : '');
  863. }
  864. function check_adminuser($username, $password, $email) {
  865. @include ROOT_PATH.'./config.inc.php';
  866. include ROOT_PATH.'./uc_client/client.php';
  867. $error = '';
  868. $uid = uc_user_register($username, $password, $email);
  869. if($uid == -1 || $uid == -2) {
  870. $error = 'admin_username_invalid';
  871. } elseif($uid == -4 || $uid == -5 || $uid == -6) {
  872. $error = 'admin_email_invalid';
  873. } elseif($uid == -3) {
  874. $ucresult = uc_user_login($username, $password);
  875. list($tmp['uid'], $tmp['username'], $tmp['password'], $tmp['email']) = uc_addslashes($ucresult);
  876. $ucresult = $tmp;
  877. if($ucresult['uid'] <= 0) {
  878. $error = 'admin_exist_password_error';
  879. } else {
  880. $uid = $ucresult['uid'];
  881. $email = $ucresult['email'];
  882. $password = $ucresult['password'];
  883. }
  884. }
  885. if(!$error && $uid > 0) {
  886. $password = md5($password);
  887. uc_user_addprotected($username, '');
  888. } else {
  889. $uid = 0;
  890. $error = empty($error) ? 'error_unknow_type' : $error;
  891. }
  892. return array('uid' => $uid, 'username' => $username, 'password' => $password, 'email' => $email, 'error' => $error);
  893. }
  894. function save_uc_config($config, $file) {
  895. $success = false;
  896. list($appauthkey, $appid, $ucdbhost, $ucdbname, $ucdbuser, $ucdbpw, $ucdbcharset, $uctablepre, $uccharset, $ucapi, $ucip) = explode('|', $config);
  897. if($content = file_get_contents($file)) {
  898. $content = trim($content);
  899. $content = substr($content, -2) == '?>' ? substr($content, 0, -2) : $content;
  900. $link = mysql_connect($ucdbhost, $ucdbuser, $ucdbpw, 1);
  901. $uc_connnect = $link && mysql_select_db($ucdbname, $link) ? 'mysql' : '';
  902. $content = insertconfig($content, "/define\('UC_CONNECT',\s*'.*?'\);/i", "define('UC_CONNECT', '$uc_connnect');");
  903. $content = insertconfig($content, "/define\('UC_DBHOST',\s*'.*?'\);/i", "define('UC_DBHOST', '$ucdbhost');");
  904. $content = insertconfig($content, "/define\('UC_DBUSER',\s*'.*?'\);/i", "define('UC_DBUSER', '$ucdbuser');");
  905. $content = insertconfig($content, "/define\('UC_DBPW',\s*'.*?'\);/i", "define('UC_DBPW', '$ucdbpw');");
  906. $content = insertconfig($content, "/define\('UC_DBNAME',\s*'.*?'\);/i", "define('UC_DBNAME', '$ucdbname');");
  907. $content = insertconfig($content, "/define\('UC_DBCHARSET',\s*'.*?'\);/i", "define('UC_DBCHARSET', '$ucdbcharset');");
  908. $content = insertconfig($content, "/define\('UC_DBTABLEPRE',\s*'.*?'\);/i", "define('UC_DBTABLEPRE', '`$ucdbname`.$uctablepre');");
  909. $content = insertconfig($content, "/define\('UC_DBCONNECT',\s*'.*?'\);/i", "define('UC_DBCONNECT', '0');");
  910. $content = insertconfig($content, "/define\('UC_KEY',\s*'.*?'\);/i", "define('UC_KEY', '$appauthkey');");
  911. $content = insertconfig($content, "/define\('UC_API',\s*'.*?'\);/i", "define('UC_API', '$ucapi');");
  912. $content = insertconfig($content, "/define\('UC_CHARSET',\s*'.*?'\);/i", "define('UC_CHARSET', '$uccharset');");
  913. $content = insertconfig($content, "/define\('UC_IP',\s*'.*?'\);/i", "define('UC_IP', '$ucip');");
  914. $content = insertconfig($content, "/define\('UC_APPID',\s*'?.*?'?\);/i", "define('UC_APPID', '$appid');");
  915. $content = insertconfig($content, "/define\('UC_PPP',\s*'?.*?'?\);/i", "define('UC_PPP', '20');");
  916. if(@file_put_contents($file, $content)) {
  917. $success = true;
  918. }
  919. }
  920. return $success;
  921. }
  922. function dhtmlspecialchars($string, $flags = null) {
  923. if(is_array($string)) {
  924. foreach($string as $key => $val) {
  925. $string[$key] = dhtmlspecialchars($val, $flags);
  926. }
  927. } else {
  928. if($flags === null) {
  929. $string = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string);
  930. if(strpos($string, '&amp;#') !== false) {
  931. $string = preg_replace('/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $string);
  932. }
  933. } else {
  934. if(PHP_VERSION < '5.4.0') {
  935. $string = htmlspecialchars($string, $flags);
  936. } else {
  937. if(strtolower(CHARSET) == 'utf-8') {
  938. $charset = 'UTF-8';
  939. } else {
  940. $charset = 'ISO-8859-1';
  941. }
  942. $string = htmlspecialchars($string, $flags, $charset);
  943. }
  944. }
  945. }
  946. return $string;
  947. }