InstallController.php 42 KB

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