index.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <?php
  2. include 'auto.php';
  3. if(IS_SAE)
  4. header("Location: index_sae.php");
  5. if (file_exists('./install.lock')) {
  6. echo '
  7. <html>
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  10. </head>
  11. <body>
  12. 你已经安装过该系统,如果想重新安装,请先删除站点install目录下的 install.lock 文件,然后再安装。
  13. </body>
  14. </html>';
  15. exit;
  16. }
  17. @set_time_limit(1000);
  18. if (phpversion() <= '5.5.9')
  19. set_magic_quotes_runtime(0);
  20. if ('5.4.0' > phpversion()){
  21. header("Content-type:text/html;charset=utf-8");
  22. exit('您的php版本过低,不能安装本软件,请升级到5.4.0或更高版本再安装,谢谢!');
  23. }
  24. define("TP_SHOP_VERSION", '20180601');
  25. date_default_timezone_set('PRC');
  26. error_reporting(E_ALL & ~E_NOTICE);
  27. header('Content-Type: text/html; charset=UTF-8');
  28. define('SITEDIR', _dir_path(substr(dirname(__FILE__), 0, -8)));
  29. //define('SITEDIR2', substr(SITEDIR,0,-7));
  30. //echo SITEDIR2;
  31. //exit;
  32. //数据库
  33. $sqlFile = 'crmeb.sql';
  34. $configFile = 'config.php';
  35. if (!file_exists(SITEDIR . 'install/' . $sqlFile) || !file_exists(SITEDIR . 'install/' . $configFile)) {
  36. echo '缺少必要的安装文件!';
  37. exit;
  38. }
  39. $Title = "CrmEb安装向导";
  40. $Powered = "Powered by CrmEb";
  41. $steps = array(
  42. '1' => '安装许可协议',
  43. '2' => '运行环境检测',
  44. '3' => '安装参数设置',
  45. '4' => '安装详细过程',
  46. '5' => '安装完成',
  47. );
  48. $step = isset($_GET['step']) ? $_GET['step'] : 1;
  49. //地址
  50. $scriptName = !empty($_SERVER["REQUEST_URI"]) ? $scriptName = $_SERVER["REQUEST_URI"] : $scriptName = $_SERVER["PHP_SELF"];
  51. $rootpath = @preg_replace("/\/(I|i)nstall\/index\.php(.*)$/", "", $scriptName);
  52. $domain = empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
  53. if ((int) $_SERVER['SERVER_PORT'] != 80) {
  54. $domain .= ":" . $_SERVER['SERVER_PORT'];
  55. }
  56. $domain = $domain . $rootpath;
  57. switch ($step) {
  58. case '1':
  59. include_once ("./templates/step1.php");
  60. exit();
  61. case '2':
  62. if (phpversion() < 5) {
  63. die('本系统需要PHP5+MYSQL >=5.5.9环境,当前PHP版本为:' . phpversion());
  64. }
  65. $phpv = @ phpversion();
  66. $os = PHP_OS;
  67. //$os = php_uname();
  68. $tmp = function_exists('gd_info') ? gd_info() : array();
  69. $server = $_SERVER["SERVER_SOFTWARE"];
  70. $host = (empty($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_HOST"] : $_SERVER["SERVER_ADDR"]);
  71. $name = $_SERVER["SERVER_NAME"];
  72. $max_execution_time = ini_get('max_execution_time');
  73. $allow_reference = (ini_get('allow_call_time_pass_reference') ? '<font color=green>[√]On</font>' : '<font color=red>[×]Off</font>');
  74. $allow_url_fopen = (ini_get('allow_url_fopen') ? '<font color=green>[√]On</font>' : '<font color=red>[×]Off</font>');
  75. $safe_mode = (ini_get('safe_mode') ? '<font color=red>[×]On</font>' : '<font color=green>[√]Off</font>');
  76. $err = 0;
  77. if (empty($tmp['GD Version'])) {
  78. $gd = '<font color=red>[×]Off</font>';
  79. $err++;
  80. } else {
  81. $gd = '<font color=green>[√]On</font> ' . $tmp['GD Version'];
  82. }
  83. if (function_exists('mysqli_connect')) {
  84. $mysql = '<span class="correct_span">&radic;</span> 已安装';
  85. } else {
  86. $mysql = '<span class="correct_span error_span">&radic;</span> 请安装mysqli扩展';
  87. $err++;
  88. }
  89. if (ini_get('file_uploads')) {
  90. $uploadSize = '<span class="correct_span">&radic;</span> ' . ini_get('upload_max_filesize');
  91. } else {
  92. $uploadSize = '<span class="correct_span error_span">&radic;</span>禁止上传';
  93. }
  94. if (function_exists('session_start')) {
  95. $session = '<span class="correct_span">&radic;</span> 支持';
  96. } else {
  97. $session = '<span class="correct_span error_span">&radic;</span> 不支持';
  98. $err++;
  99. }
  100. if(function_exists('curl_init')){
  101. $curl = '<font color=green>[√]支持</font> ';
  102. }else{
  103. $curl = '<font color=red>[×]不支持</font>';
  104. $err++;
  105. }
  106. if(function_exists('file_put_contents')){
  107. $file_put_contents = '<font color=green>[√]支持</font> ';
  108. }else{
  109. $file_put_contents = '<font color=red>[×]不支持</font>';
  110. $err++;
  111. }
  112. $folder = array(
  113. 'install',
  114. 'public/uploads',
  115. 'runtime',
  116. 'runtime/cache',
  117. 'runtime/temp',
  118. 'runtime/log',
  119. );
  120. include_once ("./templates/step2.php");
  121. exit();
  122. case '3':
  123. $dbName = strtolower(trim($_POST['dbName']));
  124. $_POST['dbport'] = $_POST['dbport'] ? $_POST['dbport'] : '3306';
  125. if ($_GET['testdbpwd']) {
  126. $dbHost = $_POST['dbHost'];
  127. $conn = @mysqli_connect($dbHost, $_POST['dbUser'], $_POST['dbPwd'],NULL,$_POST['dbport']);
  128. if (mysqli_connect_errno($conn)){
  129. die(json_encode(0));
  130. } else {
  131. $result = mysqli_query($conn,"SELECT @@global.sql_mode");
  132. $result = $result->fetch_array();
  133. $version = mysqli_get_server_info($conn);
  134. if ($version >= 5.7)
  135. {
  136. if(strstr($result[0],'STRICT_TRANS_TABLES') || strstr($result[0],'STRICT_ALL_TABLES') || strstr($result[0],'TRADITIONAL') || strstr($result[0],'ANSI'))
  137. exit(json_encode(-1));
  138. }
  139. $result = mysqli_query($conn,"select count(table_name) as c from information_schema.`TABLES` where table_schema='$dbName'");
  140. $result = $result->fetch_array();
  141. if($result['c'] > 0)
  142. exit(json_encode(-2));
  143. exit(json_encode(1));
  144. }
  145. }
  146. include_once ("./templates/step3.php");
  147. exit();
  148. case '4':
  149. if (intval($_GET['install'])) {
  150. $n = intval($_GET['n']);
  151. if ($i == 999999)
  152. exit;
  153. $arr = array();
  154. $dbHost = trim($_POST['dbhost']);
  155. $_POST['dbport'] = $_POST['dbport'] ? $_POST['dbport'] : '3306';
  156. $dbName = strtolower(trim($_POST['dbname']));
  157. $dbUser = trim($_POST['dbuser']);
  158. $dbPwd = trim($_POST['dbpw']);
  159. $dbPrefix = empty($_POST['dbprefix']) ? 'eb_' : trim($_POST['dbprefix']);
  160. $username = trim($_POST['manager']);
  161. $password = trim($_POST['manager_pwd']);
  162. $email = trim($_POST['manager_email']);
  163. if (!function_exists('mysqli_connect')) {
  164. $arr['msg'] = "请安装 mysqli 扩展!";
  165. echo json_encode($arr);
  166. exit;
  167. }
  168. $conn = @mysqli_connect($dbHost, $dbUser, $dbPwd,NULL,$_POST['dbport']);
  169. if (mysqli_connect_errno($conn)){
  170. $arr['msg'] = "连接数据库失败!".mysqli_connect_error($conn);
  171. echo json_encode($arr);
  172. exit;
  173. }
  174. mysqli_set_charset($conn, "utf8"); //,character_set_client=binary,sql_mode='';
  175. $version = mysqli_get_server_info($conn);
  176. if ($version < 5.5) {
  177. $arr['msg'] = '数据库版本太低! 必须5.5以上';
  178. echo json_encode($arr);
  179. exit;
  180. }
  181. if (!mysqli_select_db($conn,$dbName)) {
  182. //创建数据时同时设置编码
  183. if (!mysqli_query($conn,"CREATE DATABASE IF NOT EXISTS `" . $dbName . "` DEFAULT CHARACTER SET utf8;")) {
  184. $arr['msg'] = '数据库 ' . $dbName . ' 不存在,也没权限创建新的数据库!';
  185. echo json_encode($arr);
  186. exit;
  187. }
  188. if ($n==-1) {
  189. $arr['n'] = 0;
  190. $arr['msg'] = "成功创建数据库:{$dbName}<br>";
  191. echo json_encode($arr);
  192. exit;
  193. }
  194. mysqli_select_db($conn , $dbName);
  195. }
  196. //读取数据文件
  197. $sqldata = file_get_contents(SITEDIR . 'install/' . $sqlFile);
  198. $sqlFormat = sql_split($sqldata, $dbPrefix);
  199. //创建写入sql数据库文件到库中 结束
  200. /**
  201. * 执行SQL语句
  202. */
  203. $counts = count($sqlFormat);
  204. for ($i = $n; $i < $counts; $i++) {
  205. $sql = trim($sqlFormat[$i]);
  206. if (strstr($sql, 'CREATE TABLE')) {
  207. preg_match('/CREATE TABLE IF NOT EXISTS `eb_([^ ]*)`/is', $sql, $matches);
  208. mysqli_query($conn,"DROP TABLE IF EXISTS `$matches[1]");
  209. $sql = str_replace('`eb_','`'.$dbPrefix,$sql);//替换表前缀
  210. $ret = mysqli_query($conn,$sql);
  211. if ($ret) {
  212. $message = '<li><span class="correct_span">&radic;</span>创建数据表['.$dbPrefix.$matches[1] . ']完成!<span style="float: right;">'.date('Y-m-d H:i:s').'</span></li> ';
  213. } else {
  214. $message = '<li><span class="correct_span error_span">&radic;</span>创建数据表['.$dbPrefix.$matches[1] . ']失败!<span style="float: right;">'.date('Y-m-d H:i:s').'</span></li>';
  215. }
  216. $i++;
  217. $arr = array('n' => $i, 'msg' => $message);
  218. echo json_encode($arr);
  219. exit;
  220. } else {
  221. if(trim($sql) == '')
  222. continue;
  223. $sql = str_replace('`eb_','`'.$dbPrefix,$sql);//替换表前缀
  224. $ret = mysqli_query($conn,$sql);
  225. $message = '';
  226. $arr = array('n' => $i, 'msg' => $message);
  227. // echo json_encode($arr); exit;
  228. }
  229. }
  230. // 清空测试数据
  231. if($_POST['demo'] != 'demo')
  232. {
  233. $result = mysqli_query($conn,"show tables");
  234. $tables=mysqli_fetch_all($result,MYSQLI_ASSOC);//参数MYSQL_ASSOC、MYSQLI_NUM、MYSQLI_BOTH规定产生数组类型
  235. $bl_table = array('eb_system_config','eb_system_config_tab','eb_system_menus','eb_system_file','eb_express','eb_system_group','eb_system_group_data');
  236. foreach($bl_table as $k => $v)
  237. {
  238. $bl_table[$k] = str_replace('eb_',$dbPrefix,$v);
  239. }
  240. foreach($tables as $key => $val)
  241. {
  242. if(!in_array($val[0], $bl_table))
  243. {
  244. mysqli_query($conn,"truncate table ".$val[0]);
  245. }
  246. }
  247. delFile('../public/upload'); // 清空测试图片
  248. }
  249. //读取配置文件,并替换真实配置数据1
  250. $strConfig = file_get_contents(SITEDIR . 'install/' . $configFile);
  251. $strConfig = str_replace('#DB_HOST#', $dbHost, $strConfig);
  252. $strConfig = str_replace('#DB_NAME#', $dbName, $strConfig);
  253. $strConfig = str_replace('#DB_USER#', $dbUser, $strConfig);
  254. $strConfig = str_replace('#DB_PWD#', $dbPwd, $strConfig);
  255. $strConfig = str_replace('#DB_PORT#', $_POST['dbport'], $strConfig);
  256. $strConfig = str_replace('#DB_PREFIX#', $dbPrefix, $strConfig);
  257. $strConfig = str_replace('#DB_CHARSET#', 'utf8', $strConfig);
  258. // $strConfig = str_replace('#DB_DEBUG#', false, $strConfig);
  259. @chmod(SITEDIR . '/application/database.php',0777); //数据库配置文件的地址
  260. @file_put_contents(SITEDIR . '/application/database.php', $strConfig); //数据库配置文件的地址
  261. //读取配置文件,并替换换配置
  262. // $strConfig = file_get_contents(SITEDIR . '/application/config.php');
  263. // $strConfig = str_replace('CrmEb_cache_prefix', $uniqid_str, $strConfig);
  264. // @chmod(SITEDIR . '/application/config.php',0777); //配置文件的地址
  265. // @file_put_contents(SITEDIR . '/application/config.php', $strConfig); //配置文件的地址
  266. //更新网站配置信息2
  267. //插入管理员表字段tp_admin表
  268. $time = time();
  269. $ip = get_client_ip();
  270. $ip = empty($ip) ? "0.0.0.0" : $ip;
  271. $password = md5(trim($_POST['manager_pwd']));
  272. mysqli_query($conn,"truncate table {$dbPrefix}system_admin");
  273. $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
  274. (1, '".$username."', '".$password."', 'admin', '1', '".$ip."',$time , $time, 0, 0, 1, 0)";
  275. $res = mysqli_query($conn,$addadminsql);
  276. if($res){
  277. $message = '成功添加管理员<br />成功写入配置文件<br>安装完成.';
  278. $arr = array('n' => 999999, 'msg' => $message);
  279. echo json_encode($arr);exit;
  280. }else{
  281. $message = '添加管理员失败<br />成功写入配置文件<br>安装完成.';
  282. $arr = array('n' => 999999, 'msg' => $message);
  283. echo json_encode($arr);exit;
  284. }
  285. }
  286. include_once ("./templates/step4.php");
  287. exit();
  288. case '5':
  289. $ip = get_client_ip();
  290. $host = $_SERVER['HTTP_HOST'];
  291. $curent_version = file_get_contents(SITEDIR .'/application/version.php');
  292. $time = time();
  293. $mt_rand_str = $create_date.sp_random_string(6);
  294. $str_constant = "<?php".PHP_EOL."define('INSTALL_DATE',".$time.");".PHP_EOL."define('SERIALNUMBER','".$mt_rand_str."');";
  295. @file_put_contents(SITEDIR . '/application/constant.php', $str_constant);
  296. include_once ("./templates/step5.php");
  297. @touch('./install.lock');
  298. exit();
  299. }
  300. function testwrite($d) {
  301. $tfile = "_test.txt";
  302. $fp = @fopen($d . "/" . $tfile, "w");
  303. if (!$fp) {
  304. return false;
  305. }
  306. fclose($fp);
  307. $rs = @unlink($d . "/" . $tfile);
  308. if ($rs) {
  309. return true;
  310. }
  311. return false;
  312. }
  313. function sql_execute($sql, $tablepre) {
  314. $sqls = sql_split($sql, $tablepre);
  315. if (is_array($sqls)) {
  316. foreach ($sqls as $sql) {
  317. if (trim($sql) != '') {
  318. mysqli_query($sql);
  319. }
  320. }
  321. } else {
  322. mysqli_query($sqls);
  323. }
  324. return true;
  325. }
  326. function sql_split($sql, $tablepre) {
  327. if ($tablepre != "tp_")
  328. $sql = str_replace("tp_", $tablepre, $sql);
  329. $sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=utf8", $sql);
  330. $sql = str_replace("\r", "\n", $sql);
  331. $ret = array();
  332. $num = 0;
  333. $queriesarray = explode(";\n", trim($sql));
  334. unset($sql);
  335. foreach ($queriesarray as $query) {
  336. $ret[$num] = '';
  337. $queries = explode("\n", trim($query));
  338. $queries = array_filter($queries);
  339. foreach ($queries as $query) {
  340. $str1 = substr($query, 0, 1);
  341. if ($str1 != '#' && $str1 != '-')
  342. $ret[$num] .= $query;
  343. }
  344. $num++;
  345. }
  346. return $ret;
  347. }
  348. function _dir_path($path) {
  349. $path = str_replace('\\', '/', $path);
  350. if (substr($path, -1) != '/')
  351. $path = $path . '/';
  352. return $path;
  353. }
  354. // 获取客户端IP地址
  355. function get_client_ip() {
  356. static $ip = NULL;
  357. if ($ip !== NULL)
  358. return $ip;
  359. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  360. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  361. $pos = array_search('unknown', $arr);
  362. if (false !== $pos)
  363. unset($arr[$pos]);
  364. $ip = trim($arr[0]);
  365. }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  366. $ip = $_SERVER['HTTP_CLIENT_IP'];
  367. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  368. $ip = $_SERVER['REMOTE_ADDR'];
  369. }
  370. // IP地址合法验证
  371. $ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0';
  372. return $ip;
  373. }
  374. function dir_create($path, $mode = 0777) {
  375. if (is_dir($path))
  376. return TRUE;
  377. $ftp_enable = 0;
  378. $path = dir_path($path);
  379. $temp = explode('/', $path);
  380. $cur_dir = '';
  381. $max = count($temp) - 1;
  382. for ($i = 0; $i < $max; $i++) {
  383. $cur_dir .= $temp[$i] . '/';
  384. if (@is_dir($cur_dir))
  385. continue;
  386. @mkdir($cur_dir, 0777, true);
  387. @chmod($cur_dir, 0777);
  388. }
  389. return is_dir($path);
  390. }
  391. function dir_path($path) {
  392. $path = str_replace('\\', '/', $path);
  393. if (substr($path, -1) != '/')
  394. $path = $path . '/';
  395. return $path;
  396. }
  397. function sp_password($pw, $pre){
  398. $decor = md5($pre);
  399. $mi = md5($pw);
  400. return substr($decor,0,12).$mi.substr($decor,-4,4);
  401. }
  402. function sp_random_string($len = 8) {
  403. $chars = array(
  404. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  405. "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
  406. "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
  407. "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
  408. "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
  409. "3", "4", "5", "6", "7", "8", "9"
  410. );
  411. $charsLen = count($chars) - 1;
  412. shuffle($chars); // 将数组打乱
  413. $output = "";
  414. for ($i = 0; $i < $len; $i++) {
  415. $output .= $chars[mt_rand(0, $charsLen)];
  416. }
  417. return $output;
  418. }
  419. // 递归删除文件夹
  420. function delFile($dir,$file_type='') {
  421. if(is_dir($dir)){
  422. $files = scandir($dir);
  423. //打开目录 //列出目录中的所有文件并去掉 . 和 ..
  424. foreach($files as $filename){
  425. if($filename!='.' && $filename!='..'){
  426. if(!is_dir($dir.'/'.$filename)){
  427. if(empty($file_type)){
  428. unlink($dir.'/'.$filename);
  429. }else{
  430. if(is_array($file_type)){
  431. //正则匹配指定文件
  432. if(preg_match($file_type[0],$filename)){
  433. unlink($dir.'/'.$filename);
  434. }
  435. }else{
  436. //指定包含某些字符串的文件
  437. if(false!=stristr($filename,$file_type)){
  438. unlink($dir.'/'.$filename);
  439. }
  440. }
  441. }
  442. }else{
  443. delFile($dir.'/'.$filename);
  444. rmdir($dir.'/'.$filename);
  445. }
  446. }
  447. }
  448. }else{
  449. if(file_exists($dir)) unlink($dir);
  450. }
  451. }
  452. ?>