index.php 25 KB

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