index_sae.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. if (file_exists('./install.lock')) {
  3. echo '
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  7. </head>
  8. <body>
  9. 你已经安装过该系统,如果想重新安装,请先删除站点install目录下的 install.lock 文件,然后再安装。
  10. </body>
  11. </html>';
  12. exit;
  13. }
  14. @set_time_limit(1000);
  15. if (phpversion() <= PHP_EDITION)
  16. set_magic_quotes_runtime(0);
  17. if (PHP_EDITION > phpversion())
  18. exit('您的php版本过低,不能安装本软件,请升级到'.PHP_EDITION.'或更高版本再安装,谢谢!');
  19. date_default_timezone_set('PRC');
  20. error_reporting(E_ALL & ~E_NOTICE);
  21. header('Content-Type: text/html; charset=UTF-8');
  22. define('SITEDIR', _dir_path(substr(dirname(__FILE__), 0, -8)));
  23. define("TUZI_CMS_VERSION", '20150811');
  24. //数据库
  25. $configFile = 'config.php';
  26. if (!file_exists(SITEDIR . 'install/CRMEB.sql' ) || !file_exists(SITEDIR . 'install/' . $configFile)) {
  27. echo '缺少必要的安装文件!';
  28. exit;
  29. }
  30. $Title = "Kirin_Base安装向导";
  31. $Powered = "Powered by CRMEB";
  32. $steps = array(
  33. '1' => '安装许可协议',
  34. '2' => '运行环境检测',
  35. '3' => '安装参数设置',
  36. '4' => '安装详细过程',
  37. '5' => '安装完成',
  38. );
  39. $step = isset($_GET['step']) ? $_GET['step'] : 1;
  40. //地址
  41. $scriptName = !empty($_SERVER["REQUEST_URI"]) ? $scriptName = $_SERVER["REQUEST_URI"] : $scriptName = $_SERVER["PHP_SELF"];
  42. $rootpath = @preg_replace("/\/(I|i)nstall\/index_sae\.php(.*)$/", "", $scriptName);
  43. $domain = empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
  44. if ((int) $_SERVER['SERVER_PORT'] != 80) {
  45. $domain .= ":" . $_SERVER['SERVER_PORT'];
  46. }
  47. $domain = $domain . $rootpath;
  48. // mysql
  49. $conn = @mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
  50. // kvdb
  51. $kv = new SaeKV(); $kv_ini = $kv->init();
  52. // memcache
  53. $mmc=@memcache_init();
  54. // storage
  55. $s = new SaeStorage();
  56. switch ($step) {
  57. case '1':
  58. include_once ("./templates/s1.php");
  59. exit();
  60. case '2':
  61. if (phpversion() < PHP_EDITION) {
  62. die('本系统需要PHP版本 >='.PHP_EDITION.'环境,当前PHP版本为:' . phpversion());
  63. }
  64. $phpv = @ phpversion();
  65. $err = 0;
  66. if($conn){
  67. $saeMysql = '<span class="correct_span">&radic;</span> 已开启';
  68. }else {
  69. $saeMysql = '<span class="correct_span error_span">&radic;</span> 未开启';
  70. $err++;
  71. }
  72. if(is_array($s->getListByPath('data'))){
  73. $storage = '<span class="correct_span">&radic;</span> 已开启';
  74. } else {
  75. $storage = '<span class="correct_span error_span">&radic;</span> 需开启并发创建名称为data的domain';
  76. $err++;
  77. }
  78. if($kv_ini){
  79. $KVDB = '<span class="correct_span">&radic;</span> 已开启';
  80. } else {
  81. $KVDB = '<span class="correct_span error_span">&radic;</span> 未开启';
  82. $err++;
  83. }
  84. if($mmc){
  85. $Memcache = '<span class="correct_span">&radic;</span> 已开启';
  86. } else {
  87. $Memcache = '<span class="correct_span error_span">&radic;</span> 未开启';
  88. $err++;
  89. }
  90. include_once ("./templates/s2_sae.php");
  91. exit();
  92. case '3':
  93. if ($_GET['testdbpwd']) {
  94. $dbHost = $_POST['dbHost'] . ':' . $_POST['dbport'];
  95. $conn = @mysql_connect($dbHost, $_POST['dbUser'], $_POST['dbPwd']);
  96. if ($conn) {
  97. die("1");
  98. } else {
  99. die("");
  100. }
  101. }
  102. include_once ("./templates/s3_sae.php");
  103. exit();
  104. case '4':
  105. if (intval($_GET['install'])) {
  106. $n = intval($_GET['n']);
  107. if ($i == 999999) exit;
  108. $arr = array();
  109. $dbName = SAE_MYSQL_DB;
  110. $dbPrefix = empty($_POST['dbprefix']) ? 'tp_' : trim($_POST['dbprefix']);
  111. $username = trim($_POST['manager']);
  112. $password = trim($_POST['manager_pwd']);
  113. $email = trim($_POST['manager_email']);
  114. if (!$conn) {
  115. $arr['msg'] = "连接数据库失败!";
  116. echo json_encode($arr);
  117. exit;
  118. }
  119. mysql_query("SET NAMES 'utf8'"); //,character_set_client=binary,sql_mode='';
  120. $version = mysql_get_server_info($conn);
  121. if ($version < 4.1) {
  122. $arr['msg'] = '数据库版本太低!';
  123. echo json_encode($arr);
  124. exit;
  125. }
  126. if (!mysql_select_db($dbName, $conn)) {
  127. //创建数据时同时设置编码
  128. if (!mysql_query("CREATE DATABASE IF NOT EXISTS `" . $dbName . "` DEFAULT CHARACTER SET utf8;", $conn)) {
  129. $arr['msg'] = '数据库 ' . $dbName . ' 不存在,也没权限创建新的数据库!';
  130. echo json_encode($arr);
  131. exit;
  132. }
  133. if (empty($n)) {
  134. $arr['n'] = 1;
  135. $arr['msg'] = "成功创建数据库:{$dbName}<br>";
  136. echo json_encode($arr);
  137. exit;
  138. }
  139. mysql_select_db($dbName, $conn);
  140. }
  141. //读取数据文件
  142. $sqldata = file_get_contents(SITEDIR . 'install/' . $sqlFile);
  143. $sqlFormat = sql_split($sqldata, $dbPrefix);
  144. $counts = count($sqlFormat);
  145. for ($i = $n; $i < $counts; $i++) {
  146. $sql = trim($sqlFormat[$i]);
  147. if (strstr($sql, 'CREATE TABLE')) {
  148. preg_match('/CREATE TABLE (IF NOT EXISTS)? `eb_([^ ]*)`/is', $sql, $matches);
  149. mysqli_query($conn,"DROP TABLE IF EXISTS `$matches[2]");
  150. $sql = str_replace('`eb_','`'.$dbPrefix,$sql);//替换表前缀
  151. $ret = mysqli_query($conn,$sql);
  152. if ($ret) {
  153. $message = '<li><span class="correct_span">&radic;</span>创建数据表['.$dbPrefix.$matches[2] . ']完成!<span style="float: right;">'.date('Y-m-d H:i:s').'</span></li> ';
  154. } else {
  155. $message = '<li><span class="correct_span error_span">&radic;</span>创建数据表['.$dbPrefix.$matches[2] . ']失败!<span style="float: right;">'.date('Y-m-d H:i:s').'</span></li>';
  156. }
  157. $i++;
  158. $arr = array('n' => $i, 'msg' => $message);
  159. echo json_encode($arr);
  160. exit;
  161. } else {
  162. if(trim($sql) == '')
  163. continue;
  164. $sql = str_replace('`eb_','`'.$dbPrefix,$sql);//替换表前缀
  165. $ret = mysqli_query($conn,$sql);
  166. $message = '';
  167. $arr = array('n' => $i, 'msg' => $message);
  168. // echo json_encode($arr); exit;
  169. }
  170. }
  171. //插入管理员表字段tp_admin表
  172. $time = time();
  173. $ip = get_client_ip();
  174. $ip = empty($ip) ? "0.0.0.0" : $ip;
  175. $password = md5(trim($_POST['manager_pwd']));
  176. mysql_query("truncate table {$dbPrefix}system_admin");
  177. $addadminsql = "INSERT INTO `{$dbPrefix}system_admin` (`id`, `account`, `pwd`, `real_name`, `roles`, `last_ip`, `last_time`, `add_time`, `login_count`, `level`, `status`, `is_del`) VALUES
  178. (1, '".$username."', '".$password."', 'admin', '1', '".$ip."',$time , $time, 0, 0, 1, 0)";
  179. $res = mysql_query($addadminsql);
  180. if($res){
  181. $message = '成功添加管理员<br />成功写入配置文件<br>安装完成.';
  182. $arr = array('n' => 999999, 'msg' => $message);
  183. echo json_encode($arr);exit;
  184. }else{
  185. $message = '添加管理员失败<br />成功写入配置文件<br>安装完成.';
  186. $arr = array('n' => 999999, 'msg' => $message);
  187. echo json_encode($arr);exit;
  188. }
  189. //插入管理员
  190. //生成随机认证码
  191. $verify = sp_random_string(6);
  192. $time = time();
  193. $create_date=date("Y-m-d h:i:s");
  194. $ip = get_client_ip();
  195. $ip =empty($ip)?"0.0.0.0":$ip;
  196. $addadminsql = "INSERT INTO `{$dbPrefix}system_admin` (`id`, `account`, `pwd`, `real_name`, `roles`, `last_ip`, `last_time`, `add_time`, `login_count`, `level`, `status`, `is_del`) VALUES
  197. (1, '".$username."', '".$password."', 'admin', '1', '".$ip."',$time , $time, 0, 0, 1, 0)";
  198. $res = mysql_query($addadminsql);
  199. if($res){
  200. $message = '成功添加管理员<br />成功写入配置文件<br>安装完成.';
  201. $arr = array('n' => 999999, 'msg' => $message);
  202. echo json_encode($arr);exit;
  203. }else{
  204. $message = '添加管理员失败<br />成功写入配置文件<br>安装完成.';
  205. $arr = array('n' => 999999, 'msg' => $message);
  206. echo json_encode($arr);exit;
  207. }
  208. }
  209. include_once ("./templates/s4.php");
  210. exit();
  211. case '5':
  212. $ip = get_client_ip();
  213. $host=$_SERVER['HTTP_HOST'];
  214. include_once ("./templates/s5.php");
  215. exit();
  216. }
  217. function testwrite($d) {
  218. $tfile = "_test.txt";
  219. $fp = @fopen($d . "/" . $tfile, "w");
  220. if (!$fp) {
  221. return false;
  222. }
  223. fclose($fp);
  224. $rs = @unlink($d . "/" . $tfile);
  225. if ($rs) {
  226. return true;
  227. }
  228. return false;
  229. }
  230. function sql_execute($sql, $tablepre) {
  231. $sqls = sql_split($sql, $tablepre);
  232. if (is_array($sqls)) {
  233. foreach ($sqls as $sql) {
  234. if (trim($sql) != '') {
  235. mysql_query($sql);
  236. }
  237. }
  238. } else {
  239. mysql_query($sqls);
  240. }
  241. return true;
  242. }
  243. function sql_split($sql, $tablepre) {
  244. if ($tablepre != "sp_")
  245. $sql = str_replace("sp_", $tablepre, $sql);
  246. $sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=utf8", $sql);
  247. if ($r_tablepre != $s_tablepre)
  248. $sql = str_replace($s_tablepre, $r_tablepre, $sql);
  249. $sql = str_replace("\r", "\n", $sql);
  250. $ret = array();
  251. $num = 0;
  252. $queriesarray = explode(";\n", trim($sql));
  253. unset($sql);
  254. foreach ($queriesarray as $query) {
  255. $ret[$num] = '';
  256. $queries = explode("\n", trim($query));
  257. $queries = array_filter($queries);
  258. foreach ($queries as $query) {
  259. $str1 = substr($query, 0, 1);
  260. if ($str1 != '#' && $str1 != '-')
  261. $ret[$num] .= $query;
  262. }
  263. $num++;
  264. }
  265. return $ret;
  266. }
  267. function _dir_path($path) {
  268. $path = str_replace('\\', '/', $path);
  269. if (substr($path, -1) != '/')
  270. $path = $path . '/';
  271. return $path;
  272. }
  273. // 获取客户端IP地址
  274. function get_client_ip() {
  275. static $ip = NULL;
  276. if ($ip !== NULL)
  277. return $ip;
  278. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  279. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  280. $pos = array_search('unknown', $arr);
  281. if (false !== $pos)
  282. unset($arr[$pos]);
  283. $ip = trim($arr[0]);
  284. }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  285. $ip = $_SERVER['HTTP_CLIENT_IP'];
  286. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  287. $ip = $_SERVER['REMOTE_ADDR'];
  288. }
  289. // IP地址合法验证
  290. $ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0';
  291. return $ip;
  292. }
  293. function dir_create($path, $mode = 0777) {
  294. if (is_dir($path))
  295. return TRUE;
  296. $ftp_enable = 0;
  297. $path = dir_path($path);
  298. $temp = explode('/', $path);
  299. $cur_dir = '';
  300. $max = count($temp) - 1;
  301. for ($i = 0; $i < $max; $i++) {
  302. $cur_dir .= $temp[$i] . '/';
  303. if (@is_dir($cur_dir))
  304. continue;
  305. @mkdir($cur_dir, 0777, true);
  306. @chmod($cur_dir, 0777);
  307. }
  308. return is_dir($path);
  309. }
  310. function dir_path($path) {
  311. $path = str_replace('\\', '/', $path);
  312. if (substr($path, -1) != '/')
  313. $path = $path . '/';
  314. return $path;
  315. }
  316. function sp_random_string($len = 6) {
  317. $chars = array(
  318. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  319. "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
  320. "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
  321. "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
  322. "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
  323. "3", "4", "5", "6", "7", "8", "9"
  324. );
  325. $charsLen = count($chars) - 1;
  326. shuffle($chars); // 将数组打乱
  327. $output = "";
  328. for ($i = 0; $i < $len; $i++) {
  329. $output .= $chars[mt_rand(0, $charsLen)];
  330. }
  331. return $output;
  332. }
  333. ?>