index.php 18 KB

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