index.php 24 KB

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