index.php 25 KB

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