index.php 27 KB

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