// +----------------------------------------------------------------------
namespace app\controller;
use app\Request;
class InstallController
{
public function index(Request $request)
{
[$step, $testdbpwd, $install, $n] = $request->getMore([
['step', 1],
['testdbpwd', 0],
['install', 0],
['n', 0]
], true);
$path = app()->getRootPath();
$step = app()->request->param('step') ?? 1;
if (file_exists($path . 'public/install/install.lock')) {
return '你已经安装过该系统,如果想重新安装,请先删除install目录下的 install.lock 文件,然后再安装。';
}
@set_time_limit(1000);
if (phpversion() < '7.1.0' && '7.4.0' > phpversion()) {
return '您的php版本过低,不能安装本软件,php版本7.4,谢谢!';
}
if (phpversion() > '8.0') {
return '您的php版本太高,不能安装本软件,php版本7.4,谢谢!';
}
date_default_timezone_set('PRC');
error_reporting(E_ALL & ~E_NOTICE);
//数据库
$sqlFile = 'crmeb.sql';
$configFile = '.env';
if (!file_exists($path . 'public/install/' . $sqlFile) || !file_exists($path . 'public/install/' . $configFile)) {
return '缺少必要的安装文件!';
}
$Title = "CRMEB安装向导";
$Powered = "Powered by CRMEB";
$steps = array(
'1' => '安装许可协议',
'2' => '运行环境检测',
'3' => '安装参数设置',
'4' => '安装详细过程',
'5' => '安装完成',
);
switch ($step) {
case '1':
return view('/install/view/step1', [
'title' => $Title,
'powered' => $Powered
]);
case '2':
$phpv = @ phpversion();
$os = PHP_OS;
$tmp = function_exists('gd_info') ? gd_info() : array();
$server = $_SERVER["SERVER_SOFTWARE"];
$host = (empty($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_HOST"] : $_SERVER["SERVER_ADDR"]);
$name = $_SERVER["SERVER_NAME"];
$max_execution_time = ini_get('max_execution_time');
$allow_reference = (ini_get('allow_call_time_pass_reference') ? '[√]On' : '[×]Off');
$allow_url_fopen = (ini_get('allow_url_fopen') ? '[√]On' : '[×]Off');
$safe_mode = (ini_get('safe_mode') ? '[×]On' : '[√]Off');
$err = 0;
if (empty($tmp['GD Version'])) {
$gd = '[×]Off';
$err++;
} else {
$gd = '[√]On ' . $tmp['GD Version'];
}
if (extension_loaded('redis')) {
$redis = '√ 已安装';
} else {
$redis = '√ 点击查看帮助';
$err++;
}
if (extension_loaded('swoole_loader')) {
$swoole = '√ 已安装';
} else {
$swoole = '√ 点击查看帮助';
$err++;
}
if (function_exists('mysqli_connect')) {
$mysql = '√ 已安装';
} else {
$mysql = '√ 请安装mysqli扩展';
$err++;
}
if (ini_get('file_uploads')) {
$uploadSize = '√ ' . ini_get('upload_max_filesize');
} else {
$uploadSize = '√禁止上传';
}
if (function_exists('session_start')) {
$session = '√ 支持';
} else {
$session = '√ 不支持';
$err++;
}
if (function_exists('curl_init')) {
$curl = '[√]支持 ';
} else {
$curl = '[×]不支持';
$err++;
}
if (function_exists('bcadd')) {
$bcmath = '[√]支持 ';
} else {
$bcmath = '[×]不支持';
$err++;
}
if (function_exists('openssl_encrypt')) {
$openssl = '[√]支持 ';
} else {
$openssl = '[×]不支持';
$err++;
}
if (function_exists('finfo_open')) {
$finfo_open = '[√]支持 ';
} else {
$finfo_open = '√点击查看帮助';
$err++;
}
$folder = array(
'public/uploads',
'runtime',
);
$dirStr = '';
foreach ($folder as $dir) {
$Testdir = $path . $dir;
if (!is_file($Testdir)) {
if (!is_dir($Testdir)) {
$this->dir_create($Testdir);
}
}
if ($this->testwrite($Testdir)) {
$w = '√可写 ';
} else {
$w = '√不可写 ';
$err++;
}
if (is_readable($Testdir)) {
$r = '√可读';
} else {
$r = '√不可读';
$err++;
}
$dirStr .= "
';
$html_body_nav = '
';
$html_body_nav .= '
CRMEB Swoole Compiler 安装向导
';
$html_body_nav .= '
Version:2.0.2 Date:2019-01-09
';
$html_body_nav .= '
';
// Environment information
$html_body_environment = '
检查当前环境
';
$html_body_environment .= '- 操作系统 : ' . $env['os']['raw_name'] . '
';
$html_body_environment .= '- PHP版本 : ' . $env['php']['version'] . '
';
$html_body_environment .= '- PHP运行环境 : ' . $env['php']['sapi'] . '
';
$html_body_environment .= '- PHP配置文件 : ' . $env['php']['ini_loaded_file'] . '
';
$html_body_environment .= '- PHP扩展安装目录 : ' . $env['php']['extension_dir'] . '
';
$html_body_environment .= '- PHP是否线程安全 : ' . $env['php']['thread_safety'] . '
';
$html_body_environment .= '- 是否安装swoole_loader : ' . $env['php']['swoole_loader']['status'] . '
';
if (isset($sysInfo['swoole_loader']) and $sysInfo['swoole_loader']) {
$html_body_environment .= '- swoole_loader版本 : ' . $env['php']['swoole_loader']['version'] . '
';
}
if ($env['php']['bit'] == 32) {
$html_body_environment .= '- 温馨提示:当前环境使用的PHP为 ' . $env['php']['bit'] . ' 位的PHP,Compiler 目前不支持 Debug 版本或 32 位的PHP,可在 phpinfo() 中查看对应位数,如果误报请忽略此提示
';
}
$html_body_environment .= '
';
// Error infomation
$html_error = "";
if (!empty($env['php']['loaded_incompatible_extensions'])) {
$html_error = '
';
$err_msg = "当前PHP包含与swoole_compiler_loader扩展不兼容的扩展" . implode(',', $env['php']['loaded_incompatible_extensions']) . ",请移除不兼容的扩展。";
$html_error = sprintf($html_error, $err_msg);
}
// Check Loader Status
$html_body_loader = '
';
if (empty($html_error)) {
$html_body_loader .= '
';
$html_body_loader .= '
安装和配置Swoole Loader
';
$phpversion = substr($env['php']['version'], 0, 3);
$phpversion = str_replace('.', '', $phpversion);
$loaderFileName = '';
if ($env['os']['name'] == "windows") {
$loaderFileName = 'php_swoole_loader_php' . $phpversion;
if ($env['php']['thread_safety'] == '非线程安全') {
$loaderFileName .= '_nzts_x64.dll';
} else {
$loaderFileName .= '_zts_x64.dll';
}
} else {
if ($env['php']['thread_safety'] != '非线程安全') {
$loaderFileName = 'swoole_loader' . $phpversion . '_zts.so';
} else {
$loaderFileName = 'swoole_loader' . $phpversion . '.so';
}
}
$html_body_loader .= '
1 - 安装Swoole Loader
前往根目录 /help/swoole-loader/' . $loaderFileName . '扩展文件上传到当前PHP的扩展安装目录中:
' . $env['php']['extension_dir'] . '
';
$html_body_loader .= '
2 - 修改php.ini配置(如已修改配置,请忽略此步骤,不必重复添加)
';
$html_body_loader .= '编辑此PHP配置文件:' . $env['php']['ini_loaded_file'] . ',在此文件底部结尾处加入如下配置
';
if ($env['os']['name'] == "windows") {
$html_body_loader .= '
extension=' . $env['php']['extension_dir'] . DIRECTORY_SEPARATOR . $loaderFileName . '
注意:需要名称和刚才上传到当前PHP的扩展安装目录中的文件名一致';
} else {
$html_body_loader .= '
extension=' . $env['php']['extension_dir'] . DIRECTORY_SEPARATOR . $loaderFileName . '
注意:需要名称和刚才上传到当前PHP的扩展安装目录中的文件名一致';
}
$html_body_loader .= '';
$html_body_loader .= '
3 - 重启服务
重启或重载PHP配置
';
$html_body_loader .= '
';
}
// Body footer
$html_body_footer = '
';
$html_body .= $html_body_nav . '
' . $html_body_environment . $html_error . $html_body_loader . '
' . $html_body_footer;
$html_body .= '
';
// Footer
$html_footer = '
';
// Make full html
$html = $html_header . $html_body . $html_footer;
echo $html;
}
}
function w_getSysInfo($env)
{
$sysEnv = [];
// Get content of phpinfo
ob_start();
phpinfo();
$sysInfo = ob_get_contents();
ob_end_clean();
// Explode phpinfo content
if ($env['php']['run_mode'] == 'cli') {
$sysInfoList = explode('\n', $sysInfo);
} else {
$sysInfoList = explode('', $sysInfo);
}
foreach ($sysInfoList as $sysInfoItem) {
if (preg_match('/thread safety/i', $sysInfoItem)) {
$sysEnv['thread_safety'] = (preg_match('/(enabled|yes)/i', $sysInfoItem) != 0);
}
if (preg_match('/swoole_loader support/i', $sysInfoItem)) {
$sysEnv['swoole_loader'] = (preg_match('/(enabled|yes)/i', $sysInfoItem) != 0);
}
if (preg_match('/swoole_loader version/i', $sysInfoItem)) {
preg_match('/\d+.\d+.\d+/s', $sysInfoItem, $match);
$sysEnv['swoole_loader_version'] = isset($match[0]) ? $match[0] : false;
}
}
return $sysEnv;
}
//读取版本号
function getversion()
{
try {
$curent_version = @file(app()->getRootPath() . '.version');
} catch (\Exception $e) {
$curent_version = [];
}
if ($curent_version && is_array($curent_version)) {
foreach ($curent_version as $val) {
[$k, $v] = explode('=', $val);
$version_arr[$k] = $v;
}
}
return $version_arr;
}
//写入安装信息
function installlog()
{
$mt_rand_str = $this->sp_random_string(6);
$str_constant = "getRootPath() . '.constant', $str_constant);
}
function sp_random_string($len = 8)
{
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
$charsLen = count($chars) - 1;
shuffle($chars); // 将数组打乱
$output = "";
mt_srand();
for ($i = 0; $i < $len; $i++) {
$output .= $chars[mt_rand(0, $charsLen)];
}
return $output;
}
//判断权限
function testwrite($d)
{
if (is_file($d)) {
if (is_writeable($d)) {
return true;
}
return false;
} else {
$tfile = "_test.txt";
$fp = @fopen($d . "/" . $tfile, "w");
if (!$fp) {
return false;
}
fclose($fp);
$rs = @unlink($d . "/" . $tfile);
if ($rs) {
return true;
}
return false;
}
}
// 获取客户端IP地址
function get_client_ip()
{
static $ip = NULL;
if ($ip !== NULL)
return $ip;
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$pos = array_search('unknown', $arr);
if (false !== $pos)
unset($arr[$pos]);
$ip = trim($arr[0]);
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
// IP地址合法验证
$ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0';
return $ip;
}
//创建目录
function dir_create($path, $mode = 0777)
{
if (is_dir($path))
return TRUE;
$ftp_enable = 0;
$path = $this->dir_path($path);
$temp = explode('/', $path);
$cur_dir = '';
$max = count($temp) - 1;
for ($i = 0; $i < $max; $i++) {
$cur_dir .= $temp[$i] . '/';
if (@is_dir($cur_dir))
continue;
@mkdir($cur_dir, 0777, true);
@chmod($cur_dir, 0777);
}
return is_dir($path);
}
function dir_path($path)
{
$path = str_replace('\\', '/', $path);
if (substr($path, -1) != '/')
$path = $path . '/';
return $path;
}
function sql_split($sql, $tablepre)
{
if ($tablepre != "tp_")
$sql = str_replace("tp_", $tablepre, $sql);
$sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=utf8", $sql);
$sql = str_replace("\r", "\n", $sql);
$ret = array();
$num = 0;
$queriesarray = explode(";\n", trim($sql));
unset($sql);
foreach ($queriesarray as $query) {
$ret[$num] = '';
$queries = explode("\n", trim($query));
$queries = array_filter($queries);
foreach ($queries as $query) {
$str1 = substr($query, 0, 1);
if ($str1 != '#' && $str1 != '-')
$ret[$num] .= $query;
}
$num++;
}
return $ret;
}
// 递归删除文件夹
function delFile($dir, $file_type = '')
{
if (is_dir($dir)) {
$files = scandir($dir);
//打开目录 //列出目录中的所有文件并去掉 . 和 ..
foreach ($files as $filename) {
if ($filename != '.' && $filename != '..') {
if (!is_dir($dir . '/' . $filename)) {
if (empty($file_type)) {
unlink($dir . '/' . $filename);
} else {
if (is_array($file_type)) {
//正则匹配指定文件
if (preg_match($file_type[0], $filename)) {
unlink($dir . '/' . $filename);
}
} else {
//指定包含某些字符串的文件
if (false != stristr($filename, $file_type)) {
unlink($dir . '/' . $filename);
}
}
}
} else {
$this->delFile($dir . '/' . $filename);
rmdir($dir . '/' . $filename);
}
}
}
} else {
if (file_exists($dir)) unlink($dir);
}
}
}