Install.php 33 KB

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