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

/install/func.inc.php

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