PageRenderTime 66ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/php/idc_manager/cidc-read-only/public/install/func.inc.php

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