Install.php 31 KB

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