index.php 26 KB

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