index.php 25 KB

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