Install.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller;
  12. use crmeb\basic\BaseController;
  13. use Redis;
  14. use think\App;
  15. use think\Exception;
  16. use think\exception\ValidateException;
  17. use think\facade\Config;
  18. use think\facade\View;
  19. use think\Request;
  20. use think\Response;
  21. use Throwable;
  22. use ZipArchive;
  23. use ZipStream\ZipStream;
  24. class Install //extends BaseController
  25. {
  26. /**
  27. * Request实例
  28. * @var Request
  29. */
  30. protected $request;
  31. /**
  32. * 应用实例
  33. * @var App
  34. */
  35. protected $app;
  36. /**
  37. * sql文件
  38. * @var App
  39. */
  40. public $sqlFile = 'crmeb_merchant.sql';
  41. /**
  42. * 配置文件
  43. * @var App
  44. */
  45. public $configFile = '.env';
  46. public $env;
  47. public $installHost;
  48. public $_url;
  49. const DEMO_SQL_TABLE = [
  50. 'eb_system_admin',
  51. 'eb_system_menu',
  52. 'eb_system_role',
  53. 'eb_system_group',
  54. 'eb_system_group_data',
  55. 'eb_express',
  56. 'eb_system_config',
  57. 'eb_system_config_classify',
  58. 'eb_system_config_value',
  59. 'eb_template_message',
  60. 'system_notice_config',
  61. 'eb_city_area',
  62. 'eb_diy',
  63. 'eb_page_category',
  64. 'eb_page_link',
  65. 'eb_system_notice_config',
  66. 'eb_member_interests',
  67. ];
  68. public function __construct(App $app)
  69. {
  70. if (file_exists(__DIR__ . '/../../install/install.lock')) {
  71. throw new ValidateException('你已经安装过该系统,如果想重新安装,请先删除install目录下的 install.lock 文件,然后再安装。');
  72. }
  73. if (!file_exists(__DIR__ . '/../../install/' . $this->sqlFile)) {
  74. throw new ValidateException('缺少数据库文件:"install/' . $this->sqlFile . '"');
  75. }
  76. if (!file_exists(__DIR__ . '/../../install/' . $this->configFile)) {
  77. throw new ValidateException('缺少配置文件:"install/' . $this->configFile . '"');
  78. }
  79. $this->app = $app;
  80. $this->request = $this->app->request;
  81. $this->env = [];
  82. $this->installHost = $this->request->domain();
  83. $originScheme = parse_url($this->installHost)['scheme'];
  84. $this->_url = str_replace($originScheme . '://', $originScheme . ':\\\/\\\/', $this->installHost);
  85. }
  86. /**
  87. * 1 开始安装
  88. * @return string
  89. * @author Qinii
  90. * @day 2020-07-16
  91. */
  92. public function begin()
  93. {
  94. return View::fetch('/install/step1');
  95. }
  96. /**
  97. * 2 环境检测
  98. * @return string
  99. * @author Qinii
  100. * @day 2020-07-16
  101. */
  102. public function environment()
  103. {
  104. $phpv = @ phpversion();
  105. $os = PHP_OS;
  106. $tmp = function_exists('gd_info') ? gd_info() : array();
  107. $max_execution_time = ini_get('max_execution_time');
  108. $allow_reference = (ini_get('allow_call_time_pass_reference') ? '<font color=green>[√]On</font>' : '<font color=red>[×]Off</font>');
  109. $allow_url_fopen = (ini_get('allow_url_fopen') ? '<font color=green>[√]On</font>' : '<font color=red>[×]Off</font>');
  110. $err = 0;
  111. $passOne = $passTwo = 'yes';
  112. if (!ini_get('safe_mode')) {
  113. $safe_mode = '<img class="yes" src="images/install/yes.png" alt="对"> 启用';
  114. } else {
  115. $safe_mode = '<img class="no" src="images/install/warring.png" alt="错"> 禁用';
  116. $err++;
  117. $passOne = 'no';
  118. }
  119. if (empty($tmp['GD Version'])) {
  120. $gd = '<img class="no" src="images/install/warring.png" alt="错"> 禁用';
  121. $err++;
  122. $passOne = 'no';
  123. } else {
  124. $gd = '<img class="yes" src="images/install/yes.png" alt="对"> ' . $tmp['GD Version'];
  125. }
  126. if (function_exists('mysqli_connect')) {
  127. $mysql = '<img class="yes" src="images/install/yes.png" alt="对"> 已安装';
  128. } else {
  129. $mysql = '<img class="no" src="images/install/warring.png" alt="错"> 请安装mysqli扩展';
  130. $err++;
  131. $passOne = 'no';
  132. }
  133. if (ini_get('file_uploads')) {
  134. $uploadSize = '<img class="yes" src="/install/images/install/yes.png" alt="对"> ' . ini_get('upload_max_filesize');
  135. } else {
  136. $uploadSize = '<img class="no" src="images/install/warring.png" alt="错"> 禁止上传';
  137. $err++;
  138. $passOne = 'no';
  139. }
  140. if (function_exists('session_start')) {
  141. $session = '<img class="yes" src="/install/images/install/yes.png" alt="对"> 启用';
  142. } else {
  143. $session = '<img class="no" src="images/install/warring.png" alt="错"> 禁用';
  144. $err++;
  145. $passOne = 'no';
  146. }
  147. if (extension_loaded('zip')) {
  148. $zip = '<img class="yes" src="/install/images/install/yes.png" alt="对"> 启用';
  149. } else {
  150. $zip = '<img class="no" src="images/install/warring.png" alt="错"> 禁用';
  151. $err++;
  152. $passOne = 'no';
  153. }
  154. if (extension_loaded(('redis'))) {
  155. $redis = '<img class="yes" src="/install/images/install/yes.png" alt="对"> 启用';
  156. } else {
  157. $redis = '<img class="no" src="images/install/warring.png" alt="错"> 禁用';
  158. $err++;
  159. $passOne = 'no';
  160. }
  161. if (extension_loaded('swoole')) {
  162. $swoole = '<img class="yes" src="/install/images/install/yes.png" alt="对"> 启用';
  163. } else {
  164. $swoole = '<img class="no" src="images/install/warring.png" alt="错"> 禁用';
  165. $err++;
  166. $passOne = 'no';
  167. }
  168. if (extension_loaded('swoole_loader')) {
  169. $swooleCompiler = '<img class="yes" src="/install/images/install/yes.png" alt="对"> 启用';
  170. } else {
  171. $swooleCompiler = '<img class="no" src="images/install/warring.png" alt="错"><a href="/install/loader" target="_blank" style="color: #ff0000;"> 请安装swoole_loader扩展</a>';
  172. $err++;
  173. $passOne = 'no';
  174. }
  175. if (function_exists('curl_init')) {
  176. $curl = '<img class="yes" src="/install/images/install/yes.png" alt="对"> 启用';
  177. } else {
  178. $curl = '<img class="no" src="images/install/warring.png" alt="错"> 禁用';
  179. $err++;
  180. $passOne = 'no';
  181. }
  182. if (function_exists('bcadd')) {
  183. $bcmath = '<img class="yes" src="/install/images/install/yes.png" alt="对"> 启用';
  184. } else {
  185. $bcmath = '<img class="no" src="images/install/warring.png" alt="错"> 禁用';
  186. $err++;
  187. $passOne = 'no';
  188. }
  189. if (function_exists('openssl_encrypt')) {
  190. $openssl = '<img class="yes" src="/install/images/install/yes.png" alt="对"> 启用';
  191. } else {
  192. $openssl = '<img class="no" src="images/install/warring.png" alt="错"> 禁用';
  193. $err++;
  194. $passOne = 'no';
  195. }
  196. if (function_exists('finfo_open')) {
  197. $finfo_open = '<img class="yes" src="/install/images/install/yes.png" alt="对"> 启用';
  198. } else {
  199. $finfo_open = '<img class="no" src="images/install/warring.png" alt="错"> 禁用';
  200. $err++;
  201. $passOne = 'no';
  202. }
  203. $folder = array(
  204. 'public/install',
  205. 'public/uploads',
  206. 'runtime',
  207. '.env',
  208. );
  209. //必须开启函数
  210. if (function_exists('file_put_contents')) {
  211. $file_put_contents = '<img class="yes" src="/install/images/install/yes.png" alt="对"> 启用';
  212. } else {
  213. $file_put_contents = '<img class="no" src="images/install/warring.png" alt="错"> 禁用';
  214. $err++;
  215. $passTwo = 'no';
  216. }
  217. if (function_exists('imagettftext')) {
  218. $imagettftext = '<img class="yes" src="/install/images/install/yes.png" alt="对"> 启用';
  219. } else {
  220. $imagettftext = '<img class="no" src="images/install/warring.png" alt="错"> 禁用';
  221. $err++;
  222. $passTwo = 'no';
  223. }
  224. if (function_exists('pcntl_alarm')) {
  225. $pcntl_alarm = '<img class="yes" src="/install/images/install/yes.png" alt="对"> 启用';
  226. } else {
  227. $pcntl_alarm = '<img class="no" src="images/install/warring.png" alt="错"> 禁用';
  228. $err++;
  229. $passTwo = 'no';
  230. }
  231. if (function_exists('proc_open')) {
  232. $proc_open = '<img class="yes" src="/install/images/install/yes.png" alt="对"> 启用';
  233. } else {
  234. $proc_open = '<img class="no" src="images/install/warring.png" alt="错"> 禁用';
  235. $err++;
  236. $passTwo = 'no';
  237. }
  238. if (function_exists('pcntl_signal')) {
  239. $pcntl_signal = '<img class="yes" src="/install/images/install/yes.png" alt="对"> 启用';
  240. } else {
  241. $pcntl_signal = '<img class="no" src="images/install/warring.png" alt="错"> 禁用';
  242. $err++;
  243. $passTwo = 'no';
  244. }
  245. View::assign([
  246. 'max_execution_time' => $max_execution_time,
  247. 'allow_reference' => $allow_reference,
  248. 'swooleCompiler' => $swooleCompiler,
  249. 'phpv' => $phpv,
  250. 'allow_url_fopen' => $allow_url_fopen,
  251. 'safe_mode' => $safe_mode,
  252. 'gd' => $gd,
  253. 'mysql' => $mysql,
  254. 'uploadSize' => $uploadSize,
  255. 'redis' => $redis,
  256. 'session' => $session,
  257. 'swoole' => $swoole,
  258. 'curl' => $curl,
  259. 'bcmath' => $bcmath,
  260. 'openssl' => $openssl,
  261. 'finfo_open' => $finfo_open,
  262. 'file_put_contents' => $file_put_contents,
  263. 'imagettftext' => $imagettftext,
  264. 'folder' => $folder,
  265. 'zip' => $zip,
  266. 'proc_open' => $proc_open,
  267. 'pcntl_alarm' => $pcntl_alarm,
  268. 'pcntl_signal' => $pcntl_signal,
  269. 'passOne' => $passOne,
  270. 'passTwo' => $passTwo,
  271. ]);
  272. return View::fetch('/install/step2', ['err' => $err]);
  273. }
  274. /**
  275. * 3 数据库填写表单
  276. * @return string
  277. * @author Qinii
  278. * @day 2020-07-15
  279. */
  280. public function databases()
  281. {
  282. return View::fetch('/install/step3');
  283. }
  284. /**
  285. * 4 安装数据库
  286. * @return string
  287. * @author Qinii
  288. * @day 2020-07-16
  289. */
  290. public function create()
  291. {
  292. $data = $this->request->params(['dbhost', 'dbport', 'dbname', 'dbuser', 'dbpw', ['dbprefix', 'eb_'], 'manager', 'manager_pwd', ['rbhost', '127.0.0.1'], ['rbport', 6379], 'rbpw', ['rbselect', 0], 'demo']);
  293. $mysql = $this->checkDatabsaces($data);
  294. if ($mysql !== 1) throw new ValidateException('数据库链接失败' . $mysql);
  295. return View::fetch('/install/step4', ['data' => $data]);
  296. }
  297. /**
  298. * 5 安装完成
  299. * @return string
  300. * @author Qinii
  301. * @day 2020-07-16
  302. */
  303. public function end()
  304. {
  305. $ip = $this->get_client_ip();
  306. $server = $this->request->server();
  307. $host = $server['HTTP_HOST'];
  308. $version = get_crmeb_version('未知');
  309. $this->installlog();
  310. @touch(__DIR__ . '/../../install/install.lock');
  311. $this->unzip();
  312. return View::fetch('/install/step5', [
  313. 'host' => $host,
  314. 'ip' => $ip,
  315. 'version' => $version,
  316. 'merchant' => Config::get('admin.merchant_prefix'),
  317. 'system' => Config::get('admin.admin_prefix'),
  318. ]);
  319. }
  320. /**
  321. * 链接数据库 读取sql
  322. * @param $n
  323. * @return array
  324. * @author Qinii
  325. * @day 2020-07-16
  326. */
  327. public function perform($n)
  328. {
  329. $data = $this->request->param();
  330. $dbName = strtolower(trim($data['dbname']));
  331. $conn = @mysqli_connect($data['dbhost'], $data['dbuser'], $data['dbpw'], NULL, $data['dbport']);
  332. if (mysqli_connect_errno($conn)) {
  333. throw new ValidateException("连接数据库失败!" . mysqli_connect_error($conn));
  334. }
  335. if (!mysqli_select_db($conn, $dbName)) {
  336. //创建数据时同时设置编码
  337. if (!mysqli_query($conn, "CREATE DATABASE IF NOT EXISTS `" . $dbName . "` DEFAULT CHARACTER SET utf8;")) {
  338. throw new ValidateException('数据库 ' . $dbName . ' 不存在,也没权限创建新的数据库!');
  339. }
  340. }
  341. mysqli_select_db($conn, $dbName);
  342. mysqli_query($conn, 'SET NAMES utf8;');
  343. //读取数据文件
  344. $sqldata = file_get_contents(__DIR__ . '/../../install/' . $this->sqlFile);
  345. $sqldata = str_replace('https://mer1.crmeb.net', $this->installHost, $sqldata);
  346. $sqldata = str_replace('https:\\\/\\\/mer1.crmeb.net', $this->_url, $sqldata);
  347. $sqldata = str_replace('http://mer1.crmeb.net', $this->installHost, $sqldata);
  348. $sqldata = str_replace('http:\\\/\\\/mer1.crmeb.net', $this->_url, $sqldata);
  349. $sqlFormat = $this->sql_split($sqldata, $data['dbprefix']);
  350. $counts = count($sqlFormat);
  351. if ($n <= $counts && isset($sqlFormat[$n])) {
  352. $sql = trim($sqlFormat[$n]);
  353. $message = $this->install($sql, $conn, $data['dbprefix']);
  354. $n++;
  355. } else {
  356. if ($data['demo'] == '') $this->clear($conn, $data['dbprefix']);
  357. $this->setConfig($data);
  358. $message = $this->setDatabase($conn, $data);
  359. $n = 999999;
  360. }
  361. return [
  362. 'n' => $n,
  363. 'count' => $counts,
  364. 'msg' => $message,
  365. 'time' => date('Y-m-d H:i:s')
  366. ];
  367. }
  368. /**
  369. * 执行数据库文件安装
  370. * @param $sql
  371. * @param $conn
  372. * @param $dbPrefix
  373. * @return string
  374. * @author Qinii
  375. * @day 2020-07-16
  376. */
  377. public function install($sql, $conn, $dbPrefix)
  378. {
  379. // 建表
  380. if (strstr($sql, 'CREATE TABLE')) {
  381. preg_match('/CREATE TABLE `eb_([^ ]*)`/is', $sql, $matches);
  382. mysqli_query($conn, "DROP TABLE IF EXISTS `$matches[1]");
  383. $sql = str_replace('`eb_', '`' . $dbPrefix, $sql);//替换表前缀
  384. $ret = mysqli_query($conn, $sql);
  385. if ($ret) {
  386. $message = '创建数据表[' . $dbPrefix . $matches[1] . ']完成!';
  387. } else {
  388. $message = '创建数据表[' . $dbPrefix . $matches[1] . ']失败!失败原因:' . mysqli_error($conn);
  389. }
  390. } //插入数据
  391. else {
  392. $message = '';
  393. if (trim($sql) !== '') {
  394. $sql = str_replace('`eb_', '`' . $dbPrefix, $sql);//替换表前缀
  395. $ret = mysqli_query($conn, $sql);
  396. $sql = htmlspecialchars($sql);
  397. $msg = substr($sql, 0, 30);
  398. if ($ret) {
  399. $message = '执行 [' . $msg . '...]成功!' . date('Y-m-d H:i:s');
  400. } else {
  401. $message = '执行[' . $msg . '..]失败!' . date('Y-m-d H:i:s');
  402. }
  403. }
  404. }
  405. return $message;
  406. }
  407. /**
  408. * 清除测试数据
  409. * @param $conn
  410. * @param $dbPrefix
  411. * @author Qinii
  412. * @day 2020-07-16
  413. */
  414. public function clear($conn, $dbPrefix)
  415. {
  416. $result = mysqli_query($conn, "show tables");
  417. $tables = mysqli_fetch_all($result);//参数MYSQL_ASSOC、MYSQLI_NUM、MYSQLI_BOTH规定产生数组类型
  418. $bl_table = self::DEMO_SQL_TABLE;
  419. foreach ($bl_table as $k => $v) {
  420. $bl_table[$k] = str_replace('eb_', $dbPrefix, $v);
  421. }
  422. foreach ($tables as $key => $val) {
  423. if (!in_array($val[0], $bl_table)) {
  424. mysqli_query($conn, "truncate table " . $val[0]);
  425. }
  426. }
  427. }
  428. /**
  429. * 创建.env文件
  430. * @param $data
  431. * @author Qinii
  432. * @day 2020-07-16
  433. */
  434. public function setConfig($data)
  435. {
  436. //读取配置文件,并替换真实配置数据1
  437. $strConfig = file_get_contents(__DIR__ . '/../../install/' . $this->configFile);
  438. //'dbhost', 'dbport', 'dbname', 'dbuser', 'dbpw', 'dbprefix', 'manager', 'manager_pwd', ['rbhost', '127.0.0.1'], ['rbport', 6379], 'rbpw', ['rbselect', 0]
  439. $strConfig = str_replace('#DB_HOST#', $data['dbhost'], $strConfig);
  440. $strConfig = str_replace('#DB_NAME#', $data['dbname'], $strConfig);
  441. $strConfig = str_replace('#DB_USER#', $data['dbuser'], $strConfig);
  442. $strConfig = str_replace('#DB_PWD#', $data['dbpw'], $strConfig);
  443. $strConfig = str_replace('#DB_PORT#', $data['dbport'], $strConfig);
  444. $strConfig = str_replace('#DB_PREFIX#', $data['dbprefix'], $strConfig);
  445. $strConfig = str_replace('#DB_CHARSET#', 'utf8', $strConfig);
  446. //redis数据库信息
  447. $strConfig = str_replace('#RB_HOST#', $data['rbhost'], $strConfig);
  448. $strConfig = str_replace('#RB_PORT#', $data['rbport'], $strConfig);
  449. $strConfig = str_replace('#RB_PWD#', $data['rbpw'], $strConfig);
  450. $strConfig = str_replace('#RB_SELECT#', $data['rbselect'], $strConfig);
  451. $strConfig = str_replace('#APP_KEY#', md5(time() . random_int(10000000, 99999999)), $strConfig);
  452. @chmod(__DIR__ . '/../../.env', 0777);
  453. @file_put_contents(__DIR__ . '/../../.env', $strConfig); //数据库配置文件的地址
  454. }
  455. /**
  456. * 修改后台管理员用户
  457. * @param $conn
  458. * @param $data
  459. * @return string
  460. * @author Qinii
  461. * @day 2020-07-16
  462. */
  463. public function setDatabase($conn, $data)
  464. {
  465. $time = date('Y-m-d H:i:s');
  466. $ip = $this->get_client_ip();
  467. $ip = empty($ip) ? "0.0.0.0" : $ip;
  468. $password = password_hash(trim($data['manager_pwd']), PASSWORD_BCRYPT);
  469. mysqli_query($conn, "truncate table {$data['dbprefix']}system_admin");
  470. $addadminsql = "INSERT INTO `{$data['dbprefix']}system_admin` (`admin_id`, `account`, `pwd`, `real_name`, `roles`, `last_ip`, `last_time`, `create_time`, `login_count`, `level`, `status`, `is_del`) VALUES
  471. (1, '" . $data['manager'] . "', '" . $password . "', '', '1', '" . $ip . "','$time' , '$time', 0, 0, 1, 0)";
  472. $res = mysqli_query($conn, $addadminsql);
  473. if ($res) {
  474. $message = '添加管理员成功,成功写入配置文件,安装完成.';
  475. } else {
  476. $message = '添加管理员失败,成功写入配置文件,安装完成.';
  477. }
  478. return $message;
  479. }
  480. /**
  481. * 检测数据库链接是否成功以及版本
  482. * @param $data
  483. * @return false|int|string
  484. * @author Qinii
  485. * @day 2020-07-16
  486. */
  487. public function checkDatabsaces($data)
  488. {
  489. $dbName = strtolower(trim($data['dbname']));
  490. $conn = @mysqli_connect($data['dbhost'], $data['dbuser'], $data['dbpw'], NULL, $data['dbport']);
  491. if (mysqli_connect_errno($conn)) return 0;
  492. $result = mysqli_query($conn, "SELECT @@global.sql_mode");
  493. $result = $result->fetch_array();
  494. $version = mysqli_get_server_info($conn);
  495. if ($version < 5.6) return (json_encode(-4));
  496. if (strstr($result[0], 'STRICT_TRANS_TABLES') || strstr($result[0], 'STRICT_ALL_TABLES') || strstr($result[0], 'TRADITIONAL') || strstr($result[0], 'ANSI'))
  497. return ($version < 8.0) ? -1 : -2;
  498. $result = mysqli_query($conn, "select count(table_name) as c from information_schema.`TABLES` where table_schema='$dbName'");
  499. $result = $result->fetch_array();
  500. if ($result['c'] > 0) return -3;
  501. return 1;
  502. }
  503. /**
  504. * 验证数据库及redis是否正常
  505. * @return false|int|string
  506. * @author Qinii
  507. * @day 2020-07-15
  508. */
  509. public function databasesCheck()
  510. {
  511. $data = $this->request->params(['dbhost', 'dbport', 'dbname', 'dbuser', 'dbpw', ['dbprefix', 'eb_'], ['rbhost', '127.0.0.1'], ['rbport', 6379], 'rbpw', ['rbselect', 0]]);
  512. // mysql 检测
  513. $mysql = $this->checkDatabsaces($data);
  514. if ($mysql !== 1) return $mysql;
  515. // redis检测
  516. try {
  517. $redis = new Redis();
  518. $redis->connect($data['rbhost'], $data['rbport']);
  519. if ($data['rbpw']) $redis->auth($data['rbpw']);
  520. if ($data['rbselect']) $redis->select($data['rbselect']);
  521. $res = $redis->set('install', 1, 10);
  522. return $res ? 1 : -5;
  523. } catch (Throwable $e) {
  524. return -5;
  525. }
  526. }
  527. /**
  528. * 安装记录文件生成
  529. * @author Qinii
  530. * @day 2020-07-16
  531. */
  532. public function installlog()
  533. {
  534. $mt_rand_str = $this->sp_random_string(6);
  535. $str_constant = "<?php" . PHP_EOL . "define('INSTALL_DATE'," . time() . ");" . PHP_EOL . "define('SERIALNUMBER','" . $mt_rand_str . "');";
  536. @file_put_contents(__DIR__ . '/../../.constant', $str_constant);
  537. }
  538. /**
  539. * 随机字符串
  540. * @param int $len
  541. * @return string
  542. * @author Qinii
  543. * @day 2020-07-16
  544. */
  545. public function sp_random_string($len = 8)
  546. {
  547. $chars = array(
  548. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  549. "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
  550. "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
  551. "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
  552. "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
  553. "3", "4", "5", "6", "7", "8", "9"
  554. );
  555. $charsLen = count($chars) - 1;
  556. shuffle($chars); // 将数组打乱
  557. $output = "";
  558. for ($i = 0; $i < $len; $i++) {
  559. $output .= $chars[mt_rand(0, $charsLen)];
  560. }
  561. return $output;
  562. }
  563. /**
  564. * 格式化mysql文件
  565. * @param $sql
  566. * @param $tablepre
  567. * @return array
  568. * @author Qinii
  569. * @day 2020-07-16
  570. */
  571. public function sql_split($sql, $tablepre)
  572. {
  573. if ($tablepre != "tp_")
  574. $sql = str_replace("tp_", $tablepre, $sql);
  575. $sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=utf8", $sql);
  576. $sql = str_replace("\r", "\n", $sql);
  577. $ret = array();
  578. $num = 0;
  579. $queriesarray = explode(";\n", trim($sql, "\xEF\xBB\xBF"));
  580. unset($sql);
  581. foreach ($queriesarray as $query) {
  582. $ret[$num] = '';
  583. $queries = explode("\n", trim($query));
  584. $queries = array_filter($queries);
  585. foreach ($queries as $query) {
  586. $str1 = substr($query, 0, 1);
  587. if ($str1 != '#' && $str1 != '-')
  588. $ret[$num] .= $query;
  589. }
  590. $num++;
  591. }
  592. return $ret;
  593. }
  594. /**
  595. * 获取ip地址
  596. * @return string|null
  597. * @author Qinii
  598. * @day 2020-07-16
  599. */
  600. public function get_client_ip()
  601. {
  602. $server = $this->request->server();
  603. if (isset($server['REMOTE_ADDR']))
  604. $ip = $server['REMOTE_ADDR'];
  605. // IP地址合法验证
  606. $ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0';
  607. return $ip;
  608. }
  609. /**
  610. * 生成基础文件
  611. * @Author:Qinii
  612. * @Date: 2020/8/31
  613. */
  614. public function unzip()
  615. {
  616. update_crmeb_compiled();
  617. }
  618. /**
  619. * swoole_loader 安装向导
  620. * @Author:Qinii
  621. * @Date: 2020/9/10
  622. */
  623. public function swooleCompiler()
  624. {
  625. // Check os type
  626. $this->env['os'] = [];
  627. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  628. $this->env['os']['name'] = "windows";
  629. $this->env['os']['raw_name'] = php_uname();
  630. } else {
  631. $this->env['os']['name'] = "unix";
  632. $this->env['os']['raw_name'] = php_uname();
  633. }
  634. // Check php
  635. $this->env['php'] = [];
  636. $this->env['php']['version'] = phpversion();
  637. // Check run mode
  638. $sapi_type = php_sapi_name();
  639. if ("cli" == $sapi_type) {
  640. $this->env['php']['run_mode'] = "cli";
  641. } else {
  642. $this->env['php']['run_mode'] = "web";
  643. }
  644. // Check php bit
  645. if (PHP_INT_SIZE == 4) {
  646. $this->env['php']['bit'] = 32;
  647. } else {
  648. $this->env['php']['bit'] = 64;
  649. }
  650. $this->env['php']['sapi'] = $sapi_type;
  651. $this->env['php']['ini_loaded_file'] = php_ini_loaded_file();
  652. $this->env['php']['ini_scanned_files'] = php_ini_scanned_files();
  653. $this->env['php']['loaded_extensions'] = get_loaded_extensions();
  654. $this->env['php']['incompatible_extensions'] = ['xdebug', 'ionCube', 'zend_loader'];
  655. $this->env['php']['loaded_incompatible_extensions'] = [];
  656. $this->env['php']['extension_dir'] = ini_get('extension_dir');
  657. // Check incompatible extensions
  658. if (is_array($this->env['php']['loaded_extensions'])) {
  659. foreach ($this->env['php']['loaded_extensions'] as $loaded_extension) {
  660. foreach ($this->env['php']['incompatible_extensions'] as $incompatible_extension) {
  661. if (strpos(strtolower($loaded_extension), strtolower($incompatible_extension)) !== false) {
  662. $this->env['php']['loaded_incompatible_extensions'][] = $loaded_extension;
  663. }
  664. }
  665. }
  666. }
  667. $this->env['php']['loaded_incompatible_extensions'] = array_unique($this->env['php']['loaded_incompatible_extensions']);
  668. // Parse System Environment Info
  669. $sysInfo = $this->w_getSysInfo();
  670. // Check php thread safety
  671. $this->env['php']['raw_thread_safety'] = isset($sysInfo['thread_safety']) ? $sysInfo['thread_safety'] : false;
  672. if (isset($sysInfo['thread_safety'])) {
  673. $this->env['php']['thread_safety'] = $sysInfo['thread_safety'] ? '线程安全' : '非线程安全';
  674. } else {
  675. $this->env['php']['thread_safety'] = '未知';
  676. }
  677. // Check swoole loader installation
  678. if (isset($sysInfo['swoole_loader']) and isset($sysInfo['swoole_loader_version'])) {
  679. $this->env['php']['swoole_loader']['status'] = $sysInfo['swoole_loader'] ? "<span style='color: #007bff;'>已安装</span>"
  680. : '未安装';
  681. if ($sysInfo['swoole_loader_version'] !== false) {
  682. $this->env['php']['swoole_loader']['version'] = "<span style='color: #007bff;'>" . $sysInfo['swoole_loader_version'] . "</span>";
  683. } else {
  684. $this->env['php']['swoole_loader']['version'] = '未知';
  685. }
  686. } else {
  687. $this->env['php']['swoole_loader']['status'] = '未安装';
  688. $this->env['php']['swoole_loader']['version'] = '未知';
  689. }
  690. $this->html($sysInfo);
  691. }
  692. /**
  693. * 页面输出内容
  694. * @Author:Qinii
  695. * @Date: 2020/9/10
  696. * @param $this ->>env
  697. * @param $sysInfo
  698. */
  699. public function html($sysInfo)
  700. {
  701. $html = '';
  702. // Header
  703. $html_header = '<!doctype html>
  704. <html lang="en">
  705. <head>
  706. <!-- Required meta tags -->
  707. <meta charset="utf-8">
  708. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  709. <!-- Bootstrap CSS -->
  710. <link href="https://lib.baomitu.com/twitter-bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet">
  711. <title>%s</title>
  712. <style>
  713. .list_info {display: inline-block; width: 12rem;}
  714. .bold_text {font-weight: bold;}
  715. .code {color:#007bff;font-size: medium;}
  716. </style>
  717. </head>
  718. <body class="bg-light">
  719. ';
  720. $html_header = sprintf($html_header, 'CRMEB Swoole Compiler 安装向导');
  721. $html_body = '<div class="container">';
  722. $html_body_nav = '<div class="py-5 text-center" style="padding-bottom: 1rem!important;">';
  723. $html_body_nav .= '<h2>CRMEB Swoole Compiler 安装向导</h2>';
  724. $html_body_nav .= '<p class="lead"> Version:3.0.8</p>';
  725. $html_body_nav .= '</div><hr>';
  726. // Environment information
  727. $html_body_environment = '
  728. <div class="col-12" style="padding-top: 1rem!important;">
  729. <h5 class="text-center">检查当前环境</h5>
  730. <ul class="list-unstyled text-small">';
  731. $html_body_environment .= '<li><span class="list_info">操作系统 : </span>' . $this->env['os']['raw_name'] . '</li>';
  732. $html_body_environment .= '<li><span class="list_info">PHP版本 : </span>' . $this->env['php']['version'] . '</li>';
  733. $html_body_environment .= '<li><span class="list_info">PHP运行环境 : </span>' . $this->env['php']['sapi'] . '</li>';
  734. $html_body_environment .= '<li><span class="list_info">PHP配置文件 : </span>' . $this->env['php']['ini_loaded_file'] . '</li>';
  735. $html_body_environment .= '<li><span class="list_info">PHP扩展安装目录 : </span>' . $this->env['php']['extension_dir'] . '</li>';
  736. $html_body_environment .= '<li><span class="list_info">PHP是否线程安全 : </span>' . $this->env['php']['thread_safety'] . '</li>';
  737. $html_body_environment .= '<li><span class="list_info">是否安装swoole_loader : </span>' . $this->env['php']['swoole_loader']['status'] . '</li>';
  738. if (isset($sysInfo['swoole_loader']) and $sysInfo['swoole_loader']) {
  739. $html_body_environment .= '<li><span class="list_info">swoole_loader版本 : </span>' . $this->env['php']['swoole_loader']['version'] . '</li>';
  740. }
  741. if ($this->env['php']['bit'] == 32) {
  742. $html_body_environment .= '<li><span style="color:red">温馨提示:当前环境使用的PHP为 ' . $this->env['php']['bit'] . ' 位的PHP,Compiler 目前不支持 Debug 版本或 32 位的PHP,可在 phpinfo() 中查看对应位数,如果误报请忽略此提示</span></li>';
  743. }
  744. $html_body_environment .= ' </ul></div>';
  745. // Error infomation
  746. $html_error = "";
  747. if (!empty($this->env['php']['loaded_incompatible_extensions'])) {
  748. $html_error = '<hr>
  749. <div class="col-12" style="padding-top: 1rem!important;">
  750. <h5 class="text-center" style="color:red">错误信息</h5>
  751. <p class="text-center" style="color:red">%s</p>
  752. </div>
  753. ';
  754. $err_msg = "当前PHP包含与swoole_compiler_loader扩展不兼容的扩展" . implode(',', $this->env['php']['loaded_incompatible_extensions']) . ",请移除不兼容的扩展。";
  755. $html_error = sprintf($html_error, $err_msg);
  756. }
  757. // Check Loader Status
  758. $html_body_loader = '<hr>';
  759. $html_body_loader .= '<div class="col-12" style="padding-top: 1rem!important;">';
  760. $html_body_loader .= '<h5 class="text-center">宝塔面版命令行安装Swoole Loader</h5>';
  761. $html_body_loader .= '<p><span class="bold_text">1 - 命令安装</span></p><p>可直接在项目目录中执行命令:<pre class="code">/bin/bash auto.sh -s </pre></p>';
  762. $html_body_loader .= '<p><span class="bold_text">2 - 如果提醒命令不存在</span></p><p> 可直接在项目目录中执行命令:<pre class="code"> wget -O auto.sh https://mer.crmeb.net/auto.sh && /bin/bash auto.sh -s </pre></p>';
  763. $html_body_loader .= '<p><span class="bold_text">3 - 如果执行完仍然未安装成功,请手动操作</span></p>';
  764. $html_body_loader .= '</div>';
  765. if (empty($html_error)) {
  766. $html_body_loader .= '<div class="col-12" style="padding-top: 1rem!important;">';
  767. $html_body_loader .= '<h5 class="text-center">手动安装和配置Swoole Loader</h5>';
  768. $phpversion = substr($this->env['php']['version'], 0, 3);
  769. $phpversion = str_replace('.', '', $phpversion);
  770. $loaderFileName = '';
  771. if ($this->env['os']['name'] == "windows") {
  772. $loaderFileName = 'php_swoole_loader_php' . $phpversion;
  773. if ($this->env['php']['thread_safety'] == '非线程安全') {
  774. $loaderFileName .= '_nzts_x64.dll';
  775. } else {
  776. $loaderFileName .= '_zts_x64.dll';
  777. }
  778. } else {
  779. if ($this->env['php']['thread_safety'] != '非线程安全') {
  780. $loaderFileName = 'swoole_loader' . $phpversion . '_zts.so';
  781. } else {
  782. $loaderFileName = 'swoole_loader' . $phpversion . '.so';
  783. }
  784. }
  785. $html_body_loader .= '<p><span class="bold_text">1 - 安装Swoole Loader</span></p><p>前往根目录 /install/swoole-loader/' . $loaderFileName . '扩展文件上传到当前PHP的扩展安装目录中:<br/><pre class="code">' . $this->env['php']['extension_dir'] . '</pre></p>';
  786. $html_body_loader .= '<p><span class="bold_text">2 - 修改php.ini配置</span>(如已修改配置,请忽略此步骤,不必重复添加)</p><p>';
  787. $html_body_loader .= '编辑此PHP配置文件:<span class="code">' . $this->env['php']['ini_loaded_file'] . '</span>,在此文件底部结尾处加入如下配置<br/>';
  788. if ($this->env['os']['name'] == "windows") {
  789. $html_body_loader .= '<pre class="code">extension=' . $this->env['php']['extension_dir'] . DIRECTORY_SEPARATOR . $loaderFileName . '</pre>注意:需要名称和刚才上传到当前PHP的扩展安装目录中的文件名一致';
  790. } else {
  791. $html_body_loader .= '<pre class="code">extension=' . $this->env['php']['extension_dir'] . DIRECTORY_SEPARATOR . $loaderFileName . '</pre>注意:需要名称和刚才上传到当前PHP的扩展安装目录中的文件名一致';
  792. }
  793. $html_body_loader .= '</p>';
  794. $html_body_loader .= '<p><span class="bold_text">3 - 重启服务</span></p><p>重启或重载PHP配置</p>';
  795. $html_body_loader .= '</div>';
  796. }
  797. // Body footer
  798. $html_body_footer = '<footer class="my-5 pt-5 text-muted text-center text-small">
  799. </footer>';
  800. $html_body .= $html_body_nav . '<div class="row">' . $html_body_environment . $html_error . $html_body_loader . '</div>' . $html_body_footer;
  801. $html_body .= '</div>';
  802. // Footer
  803. $html_footer = '
  804. <script src="https://lib.baomitu.com/jquery/3.3.1/jquery.min.js"></script>
  805. <script src="https://lib.baomitu.com/axios/0.18.0/axios.min.js"></script>
  806. <script src="https://lib.baomitu.com/twitter-bootstrap/4.1.0/js/bootstrap.min.js"></script>
  807. </body>
  808. </html>';
  809. $html = $html_header . $html_body . $html_footer;
  810. return Response::create()->content($html)->send();
  811. }
  812. public function w_getSysInfo()
  813. {
  814. $sysEnv = [];
  815. // Get content of phpinfo
  816. ob_start();
  817. phpinfo();
  818. $sysInfo = ob_get_contents();
  819. ob_end_clean();
  820. // Explode phpinfo content
  821. if ($this->env['php']['run_mode'] == 'cli') {
  822. $sysInfoList = explode('\n', $sysInfo);
  823. } else {
  824. $sysInfoList = explode('</tr>', $sysInfo);
  825. }
  826. foreach ($sysInfoList as $sysInfoItem) {
  827. if (preg_match('/thread safety/i', $sysInfoItem)) {
  828. $sysEnv['thread_safety'] = (preg_match('/(enabled|yes)/i', $sysInfoItem) != 0);
  829. }
  830. if (preg_match('/swoole_loader support/i', $sysInfoItem)) {
  831. $sysEnv['swoole_loader'] = (preg_match('/(enabled|yes)/i', $sysInfoItem) != 0);
  832. }
  833. if (preg_match('/swoole_loader version/i', $sysInfoItem)) {
  834. preg_match('/\d+.\d+.\d+/s', $sysInfoItem, $match);
  835. $sysEnv['swoole_loader_version'] = isset($match[0]) ? $match[0] : false;
  836. }
  837. }
  838. return $sysEnv;
  839. }
  840. }