index.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. <?php
  2. //文件签名
  3. $fileValue = '';
  4. //最低php版本要求
  5. define('PHP_EDITION', '7.1.0');
  6. //服务环境检测
  7. if (function_exists('saeAutoLoader') || isset($_SERVER['HTTP_BAE_ENV_APPID'])) {
  8. showHtml('对不起,当前环境不支持本系统,请使用独立服务或云主机!');
  9. }
  10. define('APP_DIR', _dir_path(substr(dirname(__FILE__), 0, -15)));//项目目录
  11. define('SITE_DIR', _dir_path(substr(dirname(__FILE__), 0, -8)));//入口文件目录
  12. if (file_exists('../install.lock')) {
  13. showHtml('你已经安装过该系统,如果想重新安装,请先删除public目录下的 install.lock 文件,然后再安装。');
  14. }
  15. @set_time_limit(1000);
  16. if ('7.1.0' > phpversion()) {
  17. exit('您的php版本过低,不能安装本软件,兼容php版本7.1~7.4,谢谢!');
  18. }
  19. if (phpversion() >= '8.0.0') {
  20. exit('您的php版本太高,不能安装本软件,兼容php版本7.1~7.4,谢谢!');
  21. }
  22. date_default_timezone_set('PRC');
  23. error_reporting(E_ALL & ~E_NOTICE);
  24. header('Content-Type: text/html; charset=UTF-8');
  25. //mysql数据库配置容器中获取
  26. $MYSQL_HOST_IP = getenv('MYSQL_HOST_IP')?:'127.0.0.1';
  27. $MYSQL_PORT = getenv('MYSQL_PORT')?:'3306';
  28. $MYSQL_USER = getenv('MYSQL_USER')?:'root';
  29. $MYSQL_PASSWORD = getenv('MYSQL_PASSWORD')?:'123456';
  30. $MYSQL_DATABASE = getenv('MYSQL_DATABASE')?:'crmeb';
  31. //redis配置容器中获取
  32. $REDIS_HOST_IP = getenv('REDIS_HOST_IP')?:'127.0.0.1';
  33. $REDIS_PORT = getenv('REDIS_PORT')?:'6379';
  34. $REDIS_DATABASE = getenv('REDIS_DATABASE')?:0;
  35. $REDIS_PASSWORD = getenv('REDIS_PASSWORD')?:'';
  36. //数据库;
  37. $sqlFile = 'crmeb.sql';
  38. $configFile = '.env';
  39. if (!file_exists(SITE_DIR . 'install/' . $sqlFile) || !file_exists(SITE_DIR . 'install/' . $configFile)) {
  40. echo '缺少必要的安装文件!';
  41. exit;
  42. }
  43. $Title = "CRMEB安装向导";
  44. $Powered = "Powered by CRMEB";
  45. $steps = array(
  46. '1' => '安装许可协议',
  47. '2' => '运行环境检测',
  48. '3' => '安装参数设置',
  49. '4' => '安装详细过程',
  50. '5' => '安装完成',
  51. );
  52. $step = $_GET['step'] ?? 1;
  53. //地址
  54. $scriptName = !empty($_SERVER["REQUEST_URI"]) ? $scriptName = $_SERVER["REQUEST_URI"] : $scriptName = $_SERVER["PHP_SELF"];
  55. $rootPath = @preg_replace("/\/(I|i)nstall\/index\.php(.*)$/", "", $scriptName);
  56. $domain = empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
  57. if ((int)$_SERVER['SERVER_PORT'] != 80) {
  58. $domain .= ":" . $_SERVER['SERVER_PORT'];
  59. }
  60. $domain = $domain . $rootPath;
  61. switch ($step) {
  62. case '1':
  63. include_once("./templates/step1.php");
  64. exit();
  65. case '2':
  66. if (phpversion() < '7.1.0' || phpversion() >= '8.0.0') {
  67. die('本系统需要PHP为 7.1~7.4 版本,当前PHP版本为:' . phpversion());
  68. }
  69. $passOne = $passTwo = 'yes';
  70. $os = PHP_OS;
  71. $server = $_SERVER["SERVER_SOFTWARE"];
  72. $phpv = phpversion();
  73. if (ini_get('file_uploads')) {
  74. $uploadSize = '<img class="yes" src="images/install/yes.png" alt="对">' . ini_get('upload_max_filesize');
  75. } else {
  76. $passOne = 'no';
  77. $uploadSize = '<img class="no" src="images/install/warring.png" alt="错">禁止上传';
  78. }
  79. if (function_exists('session_start')) {
  80. $session = '<img class="yes" src="images/install/yes.png" alt="对">启用';
  81. } else {
  82. $passOne = 'no';
  83. $session = '<img class="no" src="images/install/warring.png" alt="错">关闭';
  84. }
  85. if (!ini_get('safe_mode')) {
  86. $safe_mode = '<img class="yes" src="images/install/yes.png" alt="对">启用';
  87. } else {
  88. $passOne = 'no';
  89. $safe_mode = '<img class="no" src="images/install/warring.png" alt="错">关闭';
  90. }
  91. $tmp = function_exists('gd_info') ? gd_info() : array();
  92. if (!empty($tmp['GD Version'])) {
  93. $gd = '<img class="yes" src="images/install/yes.png" alt="对">' . $tmp['GD Version'];
  94. } else {
  95. $passOne = 'no';
  96. $gd = '<img class="no" src="images/install/warring.png" alt="错">未安装';
  97. }
  98. if (function_exists('mysqli_connect')) {
  99. $mysql = '<img class="yes" src="images/install/yes.png" alt="对">已安装';
  100. } else {
  101. $passOne = 'no';
  102. $mysql = '<img class="no" src="images/install/warring.png" alt="错">请安装mysqli扩展';
  103. }
  104. if (function_exists('curl_init')) {
  105. $curl = '<img class="yes" src="images/install/yes.png" alt="对">启用';
  106. } else {
  107. $passOne = 'no';
  108. $curl = '<img class="no" src="images/install/warring.png" alt="错">关闭';
  109. }
  110. if (function_exists('bcadd')) {
  111. $bcmath = '<img class="yes" src="images/install/yes.png" alt="对">启用';
  112. } else {
  113. $passOne = 'no';
  114. $bcmath = '<img class="no" src="images/install/warring.png" alt="错">关闭';
  115. }
  116. if (function_exists('openssl_encrypt')) {
  117. $openssl = '<img class="yes" src="images/install/yes.png" alt="对">启用';
  118. } else {
  119. $passOne = 'no';
  120. $openssl = '<img class="no" src="images/install/warring.png" alt="错">关闭';
  121. }
  122. $folder = array(
  123. 'backup',
  124. 'public',
  125. 'runtime',
  126. );
  127. foreach ($folder as $dir) {
  128. if (!is_file(APP_DIR . $dir)) {
  129. if (!is_dir(APP_DIR . $dir)) {
  130. dir_create(APP_DIR . $dir);
  131. }
  132. }
  133. if (!testwrite(APP_DIR . $dir) || !is_readable(APP_DIR . $dir)) {
  134. $passTwo = 'no';
  135. }
  136. }
  137. $file = array(
  138. '.env',
  139. '.version',
  140. '.constant',
  141. );
  142. foreach ($file as $filename) {
  143. if (!is_writeable(APP_DIR . $filename) || !is_readable(APP_DIR . $filename)) {
  144. $passTwo = 'no';
  145. }
  146. }
  147. include_once("./templates/step2.php");
  148. exit();
  149. case '3':
  150. $dbName = strtolower(trim($_POST['dbName']));
  151. $_POST['dbport'] = $_POST['dbport'] ?: '3306';
  152. if ($_GET['mysqldbpwd']) {
  153. $dbHost = $_POST['dbHost'];
  154. $conn = mysqli_init();
  155. mysqli_options($conn, MYSQLI_OPT_CONNECT_TIMEOUT, 2);
  156. @mysqli_real_connect($conn, $dbHost, $_POST['dbUser'], $_POST['dbPwd'], NULL, $_POST['dbport']);
  157. if ($error = mysqli_connect_errno($conn)) {
  158. if ($error == 2002) {
  159. die(json_encode(2002));//地址或端口错误
  160. } else if ($error == 1045) {
  161. die(json_encode(1045));//用户名或密码错误
  162. } else {
  163. die(json_encode(-1));//链接失败
  164. }
  165. } else {
  166. if (mysqli_get_server_info($conn) < 5.1) {
  167. die(json_encode(-5));//版本过低
  168. }
  169. $result = mysqli_query($conn, "SELECT @@global.sql_mode");
  170. $result = $result->fetch_array();
  171. $version = mysqli_get_server_info($conn);
  172. if ($version >= 5.7) {
  173. if (strstr($result[0], 'STRICT_TRANS_TABLES') || strstr($result[0], 'STRICT_ALL_TABLES') || strstr($result[0], 'TRADITIONAL') || strstr($result[0], 'ANSI'))
  174. exit(json_encode(-2));//数据库配置需要修改
  175. }
  176. $result = mysqli_query($conn, "select count(table_name) as c from information_schema.`TABLES` where table_schema='$dbName'");
  177. $result = $result->fetch_array();
  178. if ($result['c'] > 0) {
  179. mysqli_close($conn);
  180. exit(json_encode(-3));//数据库存在
  181. } else {
  182. if (!mysqli_select_db($conn, $dbName)) {
  183. //创建数据时同时设置编码
  184. if (!mysqli_query($conn, "CREATE DATABASE IF NOT EXISTS `" . $dbName . "` DEFAULT CHARACTER SET utf8;")) {
  185. exit(json_encode(-4));//无权限创建数据库
  186. } else {
  187. mysqli_query($conn, "DROP DATABASE `" . $dbName . "` ;");
  188. mysqli_close($conn);
  189. exit(json_encode(1));//数据库配置成功
  190. }
  191. } else {
  192. mysqli_close($conn);
  193. exit(json_encode(1));//数据库配置成功
  194. }
  195. }
  196. }
  197. }
  198. if ($_GET['redisdbpwd']) {
  199. //redis数据库信息
  200. $rbhost = $_POST['rbhost'] ?? '127.0.0.1';
  201. $rbport = $_POST['rbport'] ?? 6379;
  202. $rbpw = $_POST['rbpw'] ?? '';
  203. $rbselect = $_POST['rbselect'] ?? 0;
  204. try {
  205. if (!class_exists('redis')) {
  206. exit(json_encode(-1));
  207. }
  208. $redis = new Redis();
  209. if (!$redis) {
  210. exit(json_encode(-1));
  211. }
  212. $redis->connect($rbhost, $rbport);
  213. if ($rbpw) {
  214. $redis->auth($rbpw);
  215. }
  216. if ($rbselect) {
  217. $redis->select($rbselect);
  218. }
  219. $res = $redis->set('install', 1, 10);
  220. if ($res) {
  221. exit(json_encode(1));
  222. } else {
  223. exit(json_encode(-3));
  224. }
  225. } catch (Throwable $e) {
  226. exit(json_encode(-3));
  227. }
  228. }
  229. include_once("./templates/step3.php");
  230. exit();
  231. case '4':
  232. if (intval($_GET['install'])) {
  233. $n = intval($_GET['n']);
  234. if ($n == 999999)
  235. exit;
  236. $arr = array();
  237. $dbHost = trim($_POST['dbhost']);
  238. $_POST['dbport'] = $_POST['dbport'] ?: '3306';
  239. $dbName = strtolower(trim($_POST['dbname']));
  240. $dbUser = trim($_POST['dbuser']);
  241. $dbPwd = trim($_POST['dbpw']);
  242. $dbPrefix = empty($_POST['dbprefix']) ? 'eb_' : trim($_POST['dbprefix']);
  243. $username = trim($_POST['manager']);
  244. $password = trim($_POST['manager_pwd']) ?: 'crmeb.com';
  245. if (!function_exists('mysqli_connect')) {
  246. $arr['msg'] = "请安装 mysqli 扩展!";
  247. exit(json_encode($arr));
  248. }
  249. $conn = @mysqli_connect($dbHost, $dbUser, $dbPwd, NULL, $_POST['dbport']);
  250. if (mysqli_connect_errno($conn)) {
  251. $arr['msg'] = "连接数据库失败!" . mysqli_connect_error($conn);
  252. exit(json_encode($arr));
  253. }
  254. mysqli_set_charset($conn, "utf8"); //,character_set_client=binary,sql_mode='';
  255. $version = mysqli_get_server_info($conn);
  256. if ($version < 5.1) {
  257. $arr['msg'] = '数据库版本太低! 必须5.1以上';
  258. exit(json_encode($arr));
  259. }
  260. if (!mysqli_select_db($conn, $dbName)) {
  261. //创建数据时同时设置编码
  262. if (!mysqli_query($conn, "CREATE DATABASE IF NOT EXISTS `" . $dbName . "` DEFAULT CHARACTER SET utf8;")) {
  263. $arr['msg'] = '数据库 ' . $dbName . ' 不存在,也没权限创建新的数据库!';
  264. exit(json_encode($arr));
  265. }
  266. if ($n == -1) {
  267. $arr['n'] = 0;
  268. $arr['msg'] = "成功创建数据库:{$dbName}";
  269. exit(json_encode($arr));
  270. }
  271. mysqli_select_db($conn, $dbName);
  272. }
  273. //读取数据文件
  274. $sqldata = file_get_contents(SITE_DIR . 'install/' . $sqlFile);
  275. $sqlFormat = sql_split($sqldata, $dbPrefix);
  276. //创建写入sql数据库文件到库中 结束
  277. /**
  278. * 执行SQL语句
  279. */
  280. $counts = count($sqlFormat);
  281. if (isset($_SERVER['REQUEST_SCHEME'])) {
  282. $request_scheme = $_SERVER['REQUEST_SCHEME'];
  283. } else {
  284. if ($_SERVER['HTTPS'] == 'on') {
  285. $request_scheme = 'https';
  286. } else {
  287. $request_scheme = 'http';
  288. }
  289. }
  290. for ($i = $n; $i < $counts; $i++) {
  291. $sql = trim($sqlFormat[$i]);
  292. if (strstr($sql, 'CREATE TABLE')) {
  293. preg_match('/CREATE TABLE (IF NOT EXISTS)? `eb_([^ ]*)`/is', $sql, $matches);
  294. mysqli_query($conn, "DROP TABLE IF EXISTS `$matches[2]`");
  295. $sql = str_replace('`eb_', '`' . $dbPrefix, $sql);//替换表前缀
  296. $ret = mysqli_query($conn, $sql);
  297. if ($ret) {
  298. $message = '创建数据表[' . $dbPrefix . $matches[2] . ']完成!';
  299. } else {
  300. $err = mysqli_error($conn);
  301. $message = '创建数据表[' . $dbPrefix . $matches[2] . ']失败!失败原因:' . $err;
  302. }
  303. $i++;
  304. $arr = array('n' => $i, 'count' => $counts, 'msg' => $message, 'time' => date('Y-m-d H:i:s'));
  305. exit(json_encode($arr));
  306. } else {
  307. if (trim($sql) == '')
  308. continue;
  309. $sql = str_replace('`eb_', '`' . $dbPrefix, $sql);//替换表前缀
  310. $sql = str_replace('http://demo.crmeb.com', $request_scheme . '://' . $_SERVER['SERVER_NAME'], $sql);//替换图片域名
  311. $sql = str_replace('http:\\\\/\\\\/demo.crmeb.com', $request_scheme . ':\\\\/\\\\/' . $_SERVER['SERVER_NAME'], $sql);//替换图片域名
  312. $ret = mysqli_query($conn, $sql);
  313. $message = '';
  314. $arr = array('n' => $i, 'count' => $counts, 'msg' => $message, 'time' => date('Y-m-d H:i:s'));
  315. }
  316. }
  317. // 清空测试数据
  318. if (!$_POST['demo']) {
  319. $result = mysqli_query($conn, "show tables");
  320. $tables = mysqli_fetch_all($result);//参数MYSQL_ASSOC、MYSQLI_NUM、MYSQLI_BOTH规定产生数组类型
  321. $bl_table = array('eb_system_admin'
  322. , 'eb_system_role'
  323. , 'eb_cache'
  324. , 'eb_agent_level'
  325. , 'eb_page_link'
  326. , 'eb_page_categroy'
  327. , 'eb_system_config'
  328. , 'eb_system_config_tab'
  329. , 'eb_system_menus'
  330. , 'eb_system_notification'
  331. , 'eb_express'
  332. , 'eb_system_group'
  333. , 'eb_system_group_data'
  334. , 'eb_lang_code'
  335. , 'eb_lang_country'
  336. , 'eb_lang_type'
  337. , 'eb_template_message'
  338. , 'eb_shipping_templates'
  339. , "eb_shipping_templates_region"
  340. , 'eb_system_city'
  341. , 'eb_diy'
  342. , 'eb_member_ship'
  343. , 'eb_system_timer'
  344. , 'eb_member_right'
  345. , 'eb_agreement'
  346. , 'eb_store_service_speechcraft'
  347. , 'eb_system_user_level'
  348. , 'eb_out_interface'
  349. , 'eb_cache');
  350. foreach ($bl_table as $k => $v) {
  351. $bl_table[$k] = str_replace('eb_', $dbPrefix, $v);
  352. }
  353. foreach ($tables as $key => $val) {
  354. if (!in_array($val[0], $bl_table)) {
  355. mysqli_query($conn, "truncate table " . $val[0]);
  356. }
  357. }
  358. }
  359. $unique = uniqid();
  360. //读取配置文件,并替换真实配置数据1
  361. $strConfig = file_get_contents(SITE_DIR . 'install/' . $configFile);
  362. $strConfig = str_replace('#DB_HOST#', $dbHost, $strConfig);
  363. $strConfig = str_replace('#DB_NAME#', $dbName, $strConfig);
  364. $strConfig = str_replace('#DB_USER#', $dbUser, $strConfig);
  365. $strConfig = str_replace('#DB_PWD#', $dbPwd, $strConfig);
  366. $strConfig = str_replace('#DB_PORT#', $_POST['dbport'], $strConfig);
  367. $strConfig = str_replace('#DB_PREFIX#', $dbPrefix, $strConfig);
  368. $strConfig = str_replace('#DB_CHARSET#', 'utf8', $strConfig);
  369. //缓存配置
  370. $cachetype = $_POST['cache_type'] == 0 ? 'file' : 'redis';
  371. $strConfig = str_replace('#CACHE_TYPE#', $cachetype, $strConfig);
  372. $strConfig = str_replace('#CACHE_PREFIX#', 'cache_' . $unique . ':', $strConfig);
  373. $strConfig = str_replace('#CACHE_TAG_PREFIX#', 'cache_tag_' . $unique . ':', $strConfig);
  374. //redis数据库信息
  375. $rbhost = $_POST['rbhost'] ?? '127.0.0.1';
  376. $rbport = $_POST['rbport'] ?? '6379';
  377. $rbpw = $_POST['rbpw'] ?? '';
  378. $rbselect = $_POST['rbselect'] ?? 0;
  379. $strConfig = str_replace('#RB_HOST#', $rbhost, $strConfig);
  380. $strConfig = str_replace('#RB_PORT#', $rbport, $strConfig);
  381. $strConfig = str_replace('#RB_PWD#', $rbpw, $strConfig);
  382. $strConfig = str_replace('#RB_SELECT#', $rbselect, $strConfig);
  383. //需改队列名称
  384. $strConfig = str_replace('#QUEUE_NAME#', $unique, $strConfig);
  385. @chmod(APP_DIR . '/.env', 0777); //数据库配置文件的地址
  386. @file_put_contents(APP_DIR . '/.env', $strConfig); //数据库配置文件的地址
  387. //插入管理员表字段tp_admin表
  388. $time = time();
  389. $ip = get_client_ip();
  390. $ip = empty($ip) ? "0.0.0.0" : $ip;
  391. $password = password_hash($_POST['manager_pwd'], PASSWORD_BCRYPT);
  392. mysqli_query($conn, "truncate table {$dbPrefix}system_admin");
  393. $addadminsql = "INSERT INTO `{$dbPrefix}system_admin` (`id`, `account`, `head_pic`, `pwd`, `real_name`, `roles`, `last_ip`, `last_time`, `add_time`, `login_count`, `level`, `status`, `is_del`) VALUES
  394. (1, '" . $username . "', '/statics/system_images/admin_head_pic.png', '" . $password . "', 'admin', '1', '" . $ip . "',$time , $time, 0, 0, 1, 0)";
  395. $res = mysqli_query($conn, $addadminsql);
  396. $res2 = true;
  397. if (isset($_SERVER['SERVER_NAME'])) {
  398. $site_url = '\'"' . $request_scheme . '://' . $_SERVER['SERVER_NAME'] . '"\'';
  399. $res2 = mysqli_query($conn, 'UPDATE `' . $dbPrefix . 'system_config` SET `value`=' . $site_url . ' WHERE `menu_name`="site_url"');
  400. }
  401. $arr = array('n' => 999999, 'count' => $counts, 'msg' => '安装完成', 'time' => date('Y-m-d H:i:s'));
  402. exit(json_encode($arr));
  403. }
  404. include_once("./templates/step4.php");
  405. exit();
  406. case '5':
  407. $ip = get_client_ip();
  408. $host = $_SERVER['HTTP_HOST'];
  409. $curent_version = getversion();
  410. $version = trim($curent_version['version']);
  411. $platform = trim($curent_version['platform']);
  412. installlog();
  413. include_once("./templates/step5.php");
  414. @touch('../install.lock');
  415. generateSignature();
  416. exit();
  417. }
  418. //读取版本号
  419. function getversion()
  420. {
  421. $version_arr = [];
  422. $curent_version = @file(APP_DIR . '.version');
  423. foreach ($curent_version as $val) {
  424. list($k, $v) = explode('=', $val);
  425. $version_arr[$k] = $v;
  426. }
  427. return $version_arr;
  428. }
  429. //写入安装信息
  430. function installlog()
  431. {
  432. $mt_rand_str = sp_random_string(6);
  433. $str_constant = "<?php" . PHP_EOL . "define('INSTALL_DATE'," . time() . ");" . PHP_EOL . "define('SERIALNUMBER','" . $mt_rand_str . "');";
  434. @file_put_contents(APP_DIR . '.constant', $str_constant);
  435. }
  436. //判断权限
  437. function testwrite($d)
  438. {
  439. if (is_file($d)) {
  440. if (is_writeable($d)) {
  441. return true;
  442. }
  443. return false;
  444. } else {
  445. $tfile = "_test.txt";
  446. $fp = @fopen($d . "/" . $tfile, "w");
  447. if (!$fp) {
  448. return false;
  449. }
  450. fclose($fp);
  451. $rs = @unlink($d . "/" . $tfile);
  452. if ($rs) {
  453. return true;
  454. }
  455. return false;
  456. }
  457. }
  458. function sql_split($sql, $tablepre)
  459. {
  460. if ($tablepre != "tp_")
  461. $sql = str_replace("tp_", $tablepre, $sql);
  462. $sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=utf8", $sql);
  463. $sql = str_replace("\r", "\n", $sql);
  464. $ret = array();
  465. $num = 0;
  466. $queriesarray = explode(";\n", trim($sql));
  467. unset($sql);
  468. foreach ($queriesarray as $query) {
  469. $ret[$num] = '';
  470. $queries = explode("\n", trim($query));
  471. $queries = array_filter($queries);
  472. foreach ($queries as $query) {
  473. $str1 = substr($query, 0, 1);
  474. if ($str1 != '#' && $str1 != '-')
  475. $ret[$num] .= $query;
  476. }
  477. $num++;
  478. }
  479. return $ret;
  480. }
  481. function _dir_path($path)
  482. {
  483. $path = str_replace('\\', '/', $path);
  484. if (substr($path, -1) != '/')
  485. $path = $path . '/';
  486. return $path;
  487. }
  488. // 获取客户端IP地址
  489. function get_client_ip()
  490. {
  491. static $ip = NULL;
  492. if ($ip !== NULL)
  493. return $ip;
  494. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  495. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  496. $pos = array_search('unknown', $arr);
  497. if (false !== $pos)
  498. unset($arr[$pos]);
  499. $ip = trim($arr[0]);
  500. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  501. $ip = $_SERVER['HTTP_CLIENT_IP'];
  502. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  503. $ip = $_SERVER['REMOTE_ADDR'];
  504. }
  505. // IP地址合法验证
  506. $ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0';
  507. return $ip;
  508. }
  509. function dir_create($path, $mode = 0777)
  510. {
  511. if (is_dir($path))
  512. return TRUE;
  513. $ftp_enable = 0;
  514. $path = dir_path($path);
  515. $temp = explode('/', $path);
  516. $cur_dir = '';
  517. $max = count($temp) - 1;
  518. for ($i = 0; $i < $max; $i++) {
  519. $cur_dir .= $temp[$i] . '/';
  520. if (@is_dir($cur_dir))
  521. continue;
  522. @mkdir($cur_dir, 0777, true);
  523. @chmod($cur_dir, 0777);
  524. }
  525. return is_dir($path);
  526. }
  527. function dir_path($path)
  528. {
  529. $path = str_replace('\\', '/', $path);
  530. if (substr($path, -1) != '/')
  531. $path = $path . '/';
  532. return $path;
  533. }
  534. function sp_password($pw, $pre)
  535. {
  536. $decor = md5($pre);
  537. $mi = md5($pw);
  538. return substr($decor, 0, 12) . $mi . substr($decor, -4, 4);
  539. }
  540. function sp_random_string($len = 8)
  541. {
  542. $chars = array(
  543. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  544. "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
  545. "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
  546. "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
  547. "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
  548. "3", "4", "5", "6", "7", "8", "9"
  549. );
  550. $charsLen = count($chars) - 1;
  551. shuffle($chars); // 将数组打乱
  552. $output = "";
  553. for ($i = 0; $i < $len; $i++) {
  554. $output .= $chars[mt_rand(0, $charsLen)];
  555. }
  556. return $output;
  557. }
  558. // 递归删除文件夹
  559. function delFile($dir, $file_type = '')
  560. {
  561. if (is_dir($dir)) {
  562. $files = scandir($dir);
  563. //打开目录 //列出目录中的所有文件并去掉 . 和 ..
  564. foreach ($files as $filename) {
  565. if ($filename != '.' && $filename != '..') {
  566. if (!is_dir($dir . '/' . $filename)) {
  567. if (empty($file_type)) {
  568. unlink($dir . '/' . $filename);
  569. } else {
  570. if (is_array($file_type)) {
  571. //正则匹配指定文件
  572. if (preg_match($file_type[0], $filename)) {
  573. unlink($dir . '/' . $filename);
  574. }
  575. } else {
  576. //指定包含某些字符串的文件
  577. if (false != stristr($filename, $file_type)) {
  578. unlink($dir . '/' . $filename);
  579. }
  580. }
  581. }
  582. } else {
  583. delFile($dir . '/' . $filename);
  584. rmdir($dir . '/' . $filename);
  585. }
  586. }
  587. }
  588. } else {
  589. if (file_exists($dir)) unlink($dir);
  590. }
  591. }
  592. //错误提示方法
  593. function showHtml($str)
  594. {
  595. echo '
  596. <html>
  597. <head>
  598. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  599. </head>
  600. <body>
  601. ' . $str . '
  602. </body>
  603. </html>';
  604. exit;
  605. }
  606. /**
  607. * 计算签名
  608. * @param string $path
  609. * @throws Exception
  610. */
  611. function getFileSignature(string $path)
  612. {
  613. global $fileValue;
  614. if (!is_dir($path)) {
  615. $fileValue .= @md5_file($path);
  616. } else {
  617. if (!$dh = opendir($path)) throw new Exception($path . " File open failed!");
  618. while (($file = readdir($dh)) != false) {
  619. if ($file == "." || $file == "..") {
  620. continue;
  621. } else {
  622. getFileSignature($path . DIRECTORY_SEPARATOR . $file);
  623. }
  624. }
  625. closedir($dh);
  626. }
  627. }
  628. /**
  629. * 写入签名
  630. * @return void
  631. * @throws Exception
  632. */
  633. function generateSignature()
  634. {
  635. $file = APP_DIR . '.version';
  636. if (!$data = @file($file)) {
  637. throw new Exception('.version读取失败');
  638. }
  639. $list = [];
  640. if (!empty($data)) {
  641. foreach ($data as $datum) {
  642. list($name, $value) = explode('=', $datum);
  643. $list[$name] = rtrim($value);
  644. }
  645. }
  646. if (!isset($list['project_signature'])) {
  647. $list['project_signature'] = '';
  648. }
  649. global $fileValue;
  650. getFileSignature(APP_DIR . DIRECTORY_SEPARATOR . 'app');
  651. getFileSignature(APP_DIR . DIRECTORY_SEPARATOR . 'crmeb');
  652. $list['project_signature'] = md5($fileValue);
  653. $str = "";
  654. foreach ($list as $key => $item) {
  655. $str .= "{$key}={$item}\n";
  656. }
  657. file_put_contents($file, $str);
  658. }
  659. ?>