// +---------------------------------------------------------------------- namespace app\controller; use crmeb\basic\BaseController; use Redis; use think\App; use think\Exception; use think\exception\ValidateException; use think\facade\Config; use think\facade\View; use think\Request; use think\Response; use Throwable; use ZipArchive; use ZipStream\ZipStream; class Install //extends BaseController { /** * Request实例 * @var Request */ protected $request; /** * 应用实例 * @var App */ protected $app; /** * sql文件 * @var App */ public $sqlFile = 'crmeb_merchant.sql'; /** * 配置文件 * @var App */ public $configFile = '.env'; public $env; public $installHost; public $_url; const DEMO_SQL_TABLE = [ 'eb_system_admin', 'eb_system_menu', 'eb_system_role', 'eb_system_group', 'eb_system_group_data', 'eb_express', 'eb_system_config', 'eb_system_config_classify', 'eb_system_config_value', 'eb_template_message', 'system_notice_config', 'eb_city_area', 'eb_diy', 'eb_page_category', 'eb_page_link', 'eb_system_notice_config' ]; public function __construct(App $app) { if (file_exists(__DIR__ . '/../../install/install.lock')) { throw new ValidateException('你已经安装过该系统,如果想重新安装,请先删除install目录下的 install.lock 文件,然后再安装。'); } if (!file_exists(__DIR__ . '/../../install/' . $this->sqlFile)) { throw new ValidateException('缺少数据库文件:"install/'.$this->sqlFile.'"'); } if (!file_exists(__DIR__ . '/../../install/' . $this->configFile)) { throw new ValidateException('缺少配置文件:"install/'.$this->configFile.'"'); } $this->app = $app; $this->request = $this->app->request; $this->env = []; $this->installHost = $this->request->domain(); $originScheme = parse_url($this->installHost)['scheme']; $this->_url = str_replace($originScheme . '://', $originScheme . ':\\\/\\\/', $this->installHost); } /** * TODO 1 开始安装 * @return string * @author Qinii * @day 2020-07-16 */ public function begin() { return View::fetch('/install/step1'); } /** * TODO 2 环境检测 * @return string * @author Qinii * @day 2020-07-16 */ public function environment() { $phpv = @ phpversion(); $os = PHP_OS; $tmp = function_exists('gd_info') ? gd_info() : array(); $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 (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 (extension_loaded('zip')) { $zip = '[√]支持 '; } else { $zip = '[×]不支持'; $err++; } if (extension_loaded(('redis'))) { $redis = '[√]支持 '; } else { $redis = '[×]不支持'; $err++; } if (extension_loaded('swoole')) { $swoole = '[√]支持 '; } else { $swoole = '[×]不支持'; $err++; } if (extension_loaded('swoole_loader')) { $swooleCompiler = '[√]支持 '; } else { $swooleCompiler = ' 请安装swoole_loader扩展'; $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/install', 'public/uploads', 'runtime', '.env', ); //必须开启函数 if (function_exists('file_put_contents')) { $file_put_contents = '[√]开启 '; } else { $file_put_contents = '[×]关闭'; $err++; } if (function_exists('imagettftext')) { $imagettftext = '[√]开启 '; } else { $imagettftext = '[×]关闭'; $err++; } if (function_exists('pcntl_alarm')) { $pcntl_alarm = '[√]开启 '; } else { $pcntl_alarm = '[×]关闭'; $err++; } if (function_exists('proc_open')) { $proc_open = '[√]开启 '; } else { $proc_open = '[×]关闭'; $err++; } if (function_exists('pcntl_signal')) { $pcntl_signal = '[√]开启 '; } else { $pcntl_signal = '[×]关闭'; $err++; } View::assign([ 'max_execution_time' => $max_execution_time, 'allow_reference' => $allow_reference, 'swooleCompiler' => $swooleCompiler, 'phpv' => $phpv, 'allow_url_fopen' => $allow_url_fopen, 'safe_mode' => $safe_mode, 'gd' => $gd, 'mysql' => $mysql, 'uploadSize' => $uploadSize, 'redis' => $redis, 'session' => $session, 'swoole' => $swoole, 'curl' => $curl, 'bcmath' => $bcmath, 'openssl' => $openssl, 'finfo_open' => $finfo_open, 'file_put_contents' => $file_put_contents, 'imagettftext' => $imagettftext, 'folder' => $folder, 'zip' => $zip, 'proc_open' => $proc_open, 'pcntl_alarm' => $pcntl_alarm, 'pcntl_signal' => $pcntl_signal, ]); return View::fetch('/install/step2', ['err' => $err]); } /** * TODO 3 数据库填写表单 * @return string * @author Qinii * @day 2020-07-15 */ public function databases() { return View::fetch('/install/step3'); } /** * TODO 4 安装数据库 * @return string * @author Qinii * @day 2020-07-16 */ public function create() { $data = $this->request->params(['dbhost', 'dbport', 'dbname', 'dbuser', 'dbpw', ['dbprefix','eb_'], 'manager', 'manager_pwd', ['rbhost', '127.0.0.1'], ['rbport', 6379], 'rbpw', ['rbselect', 0], 'demo']); $mysql = $this->checkDatabsaces($data); if ($mysql !== 1) throw new ValidateException('数据库链接失败' . $mysql); return View::fetch('/install/step4', ['data' => $data]); } /** * TODO 5 安装完成 * @return string * @author Qinii * @day 2020-07-16 */ public function end() { $ip = $this->get_client_ip(); $server = $this->request->server(); $host = $server['HTTP_HOST']; $version = get_crmeb_version('未知'); $this->installlog(); @touch(__DIR__ . '/../../install/install.lock'); $this->unzip(); return View::fetch('/install/step5', [ 'host' => $host, 'ip' => $ip, 'version' => $version, 'merchant' => Config::get('admin.merchant_prefix'), 'system' => Config::get('admin.admin_prefix'), ]); } /** * TODO 链接数据库 读取sql * @param $n * @return array * @author Qinii * @day 2020-07-16 */ public function perform($n) { $data = $this->request->param(); $dbName = strtolower(trim($data['dbname'])); $conn = @mysqli_connect($data['dbhost'], $data['dbuser'], $data['dbpw'], NULL, $data['dbport']); if (mysqli_connect_errno($conn)) { throw new ValidateException("连接数据库失败!" . mysqli_connect_error($conn)); } if (!mysqli_select_db($conn, $dbName)) { //创建数据时同时设置编码 if (!mysqli_query($conn, "CREATE DATABASE IF NOT EXISTS `" . $dbName . "` DEFAULT CHARACTER SET utf8;")) { throw new ValidateException('数据库 ' . $dbName . ' 不存在,也没权限创建新的数据库!'); } } mysqli_select_db($conn, $dbName); mysqli_query($conn, 'SET NAMES utf8;'); //读取数据文件 $sqldata = file_get_contents(__DIR__ . '/../../install/' . $this->sqlFile); $sqlFormat = $this->sql_split($sqldata, $data['dbprefix']); $counts = count($sqlFormat); if ($n <= $counts && isset($sqlFormat[$n])) { $sql = trim($sqlFormat[$n]); $message = $this->install($sql, $conn, $data['dbprefix']); $n++; } else { if ($data['demo'] == '') $this->clear($conn, $data['dbprefix']); $this->setConfig($data); $message = $this->setDatabase($conn, $data); $n = -1; } return [ 'n' => $n, 'msg' => $message ]; } /** * TODO 执行数据库文件安装 * @param $sql * @param $conn * @param $dbPrefix * @return string * @author Qinii * @day 2020-07-16 */ public function install($sql, $conn, $dbPrefix) { // 建表 if (strstr($sql, 'CREATE TABLE')) { preg_match('/CREATE TABLE `eb_([^ ]*)`/is', $sql, $matches); mysqli_query($conn, "DROP TABLE IF EXISTS `$matches[1]"); $sql = str_replace('`eb_', '`' . $dbPrefix, $sql);//替换表前缀 $ret = mysqli_query($conn, $sql); if ($ret) { $message = '
  • 创建数据表[' . $dbPrefix . $matches[1] . ']完成!' . date('Y-m-d H:i:s') . '
  • '; } else { $message = '
  • 创建数据表[' . $dbPrefix . $matches[1] . ']失败!' . date('Y-m-d H:i:s') . '
  • '; } } //插入数据 else { $message = ''; if (trim($sql) !== '') { $sql = str_replace('`eb_', '`' . $dbPrefix, $sql);//替换表前缀 $sql = str_replace('https://mer1.crmeb.net', $this->installHost , $sql); $sql = str_replace('https:\\\/\\\/mer1.crmeb.net', $this->_url , $sql); $ret = mysqli_query($conn, $sql); $sql = htmlspecialchars($sql); $msg = substr($sql, 0, 30); if ($ret) { $message = '
  • 执行 [' . $msg . '...]成功!' . date('Y-m-d H:i:s') . '
  • '; } else { $message = '
  • 执行[' . $msg . '..]失败!' . date('Y-m-d H:i:s') . '
  • '; } } } return $message; } /** * TODO 清除测试数据 * @param $conn * @param $dbPrefix * @author Qinii * @day 2020-07-16 */ public function clear($conn, $dbPrefix) { $result = mysqli_query($conn, "show tables"); $tables = mysqli_fetch_all($result);//参数MYSQL_ASSOC、MYSQLI_NUM、MYSQLI_BOTH规定产生数组类型 $bl_table = self::DEMO_SQL_TABLE; foreach ($bl_table as $k => $v) { $bl_table[$k] = str_replace('eb_', $dbPrefix, $v); } foreach ($tables as $key => $val) { if (!in_array($val[0], $bl_table)) { mysqli_query($conn, "truncate table " . $val[0]); } } } /** * TODO 创建.env文件 * @param $data * @author Qinii * @day 2020-07-16 */ public function setConfig($data) { //读取配置文件,并替换真实配置数据1 $strConfig = file_get_contents(__DIR__ . '/../../install/' . $this->configFile); //'dbhost', 'dbport', 'dbname', 'dbuser', 'dbpw', 'dbprefix', 'manager', 'manager_pwd', ['rbhost', '127.0.0.1'], ['rbport', 6379], 'rbpw', ['rbselect', 0] $strConfig = str_replace('#DB_HOST#', $data['dbhost'], $strConfig); $strConfig = str_replace('#DB_NAME#', $data['dbname'], $strConfig); $strConfig = str_replace('#DB_USER#', $data['dbuser'], $strConfig); $strConfig = str_replace('#DB_PWD#', $data['dbpw'], $strConfig); $strConfig = str_replace('#DB_PORT#', $data['dbport'], $strConfig); $strConfig = str_replace('#DB_PREFIX#', $data['dbprefix'], $strConfig); $strConfig = str_replace('#DB_CHARSET#', 'utf8', $strConfig); //redis数据库信息 $strConfig = str_replace('#RB_HOST#', $data['rbhost'], $strConfig); $strConfig = str_replace('#RB_PORT#', $data['rbport'], $strConfig); $strConfig = str_replace('#RB_PWD#', $data['rbpw'], $strConfig); $strConfig = str_replace('#RB_SELECT#', $data['rbselect'], $strConfig); $strConfig = str_replace('#APP_KEY#', md5(time() . random_int(10000000, 99999999)), $strConfig); @chmod(__DIR__ . '/../../.env', 0777); @file_put_contents(__DIR__ . '/../../.env', $strConfig); //数据库配置文件的地址 } /** * TODO 修改后台管理员用户 * @param $conn * @param $data * @return string * @author Qinii * @day 2020-07-16 */ public function setDatabase($conn, $data) { $time = date('Y-m-d H:i:s'); $ip = $this->get_client_ip(); $ip = empty($ip) ? "0.0.0.0" : $ip; $password = password_hash(trim($data['manager_pwd']), PASSWORD_BCRYPT); mysqli_query($conn, "truncate table {$data['dbprefix']}system_admin"); $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 (1, '" . $data['manager'] . "', '" . $password . "', '', '1', '" . $ip . "','$time' , '$time', 0, 0, 1, 0)"; $res = mysqli_query($conn, $addadminsql); if ($res) { $message = '成功添加管理员
    成功写入配置文件
    安装完成.'; } else { $message = '添加管理员失败
    成功写入配置文件
    安装完成.'; } return $message; } /** * TODO 检测数据库链接是否成功以及版本 * @param $data * @return false|int|string * @author Qinii * @day 2020-07-16 */ public function checkDatabsaces($data) { $dbName = strtolower(trim($data['dbname'])); $conn = @mysqli_connect($data['dbhost'], $data['dbuser'], $data['dbpw'], NULL, $data['dbport']); if (mysqli_connect_errno($conn)) return 0; $result = mysqli_query($conn, "SELECT @@global.sql_mode"); $result = $result->fetch_array(); $version = mysqli_get_server_info($conn); if ($version < 5.6) return (json_encode(-4)); if (strstr($result[0], 'STRICT_TRANS_TABLES') || strstr($result[0], 'STRICT_ALL_TABLES') || strstr($result[0], 'TRADITIONAL') || strstr($result[0], 'ANSI')) return ($version < 8.0) ? -1 : -2; $result = mysqli_query($conn, "select count(table_name) as c from information_schema.`TABLES` where table_schema='$dbName'"); $result = $result->fetch_array(); if ($result['c'] > 0) return -3; return 1; } /** * TODO 验证数据库及redis是否正常 * @return false|int|string * @author Qinii * @day 2020-07-15 */ public function databasesCheck() { $data = $this->request->params(['dbhost', 'dbport', 'dbname', 'dbuser', 'dbpw',['dbprefix','eb_'], ['rbhost', '127.0.0.1'], ['rbport', 6379], 'rbpw', ['rbselect', 0]]); // mysql 检测 $mysql = $this->checkDatabsaces($data); if ($mysql !== 1) return $mysql; // redis检测 try { $redis = new Redis(); $redis->connect($data['rbhost'], $data['rbport']); if ($data['rbpw']) $redis->auth($data['rbpw']); if ($data['rbselect']) $redis->select($data['rbselect']); $res = $redis->set('install', 1, 10); return $res ? 1 : -5; } catch (Throwable $e) { return -5; } } /** * TODO 安装记录文件生成 * @author Qinii * @day 2020-07-16 */ public function installlog() { $mt_rand_str = $this->sp_random_string(6); $str_constant = "request->server(); if (isset($server['REMOTE_ADDR'])) $ip = $server['REMOTE_ADDR']; // IP地址合法验证 $ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0'; return $ip; } /** * 生成基础文件 * @Author:Qinii * @Date: 2020/8/31 */ public function unzip() { update_crmeb_compiled(); } /** * TODO swoole_loader 安装向导 * @Author:Qinii * @Date: 2020/9/10 */ public function swooleCompiler() { // Check os type $this->env['os'] = []; if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { $this->env['os']['name'] = "windows"; $this->env['os']['raw_name'] = php_uname(); } else { $this->env['os']['name'] = "unix"; $this->env['os']['raw_name'] = php_uname(); } // Check php $this->env['php'] = []; $this->env['php']['version'] = phpversion(); // Check run mode $sapi_type = php_sapi_name(); if ("cli" == $sapi_type) { $this->env['php']['run_mode'] = "cli"; } else { $this->env['php']['run_mode'] = "web"; } // Check php bit if (PHP_INT_SIZE == 4) { $this->env['php']['bit'] = 32; } else { $this->env['php']['bit'] = 64; } $this->env['php']['sapi'] = $sapi_type; $this->env['php']['ini_loaded_file'] = php_ini_loaded_file(); $this->env['php']['ini_scanned_files'] = php_ini_scanned_files(); $this->env['php']['loaded_extensions'] = get_loaded_extensions(); $this->env['php']['incompatible_extensions'] = ['xdebug', 'ionCube', 'zend_loader']; $this->env['php']['loaded_incompatible_extensions'] = []; $this->env['php']['extension_dir'] = ini_get('extension_dir'); // Check incompatible extensions if (is_array($this->env['php']['loaded_extensions'])) { foreach ($this->env['php']['loaded_extensions'] as $loaded_extension) { foreach ($this->env['php']['incompatible_extensions'] as $incompatible_extension) { if (strpos(strtolower($loaded_extension), strtolower($incompatible_extension)) !== false) { $this->env['php']['loaded_incompatible_extensions'][] = $loaded_extension; } } } } $this->env['php']['loaded_incompatible_extensions'] = array_unique($this->env['php']['loaded_incompatible_extensions']); // Parse System Environment Info $sysInfo = $this->w_getSysInfo(); // Check php thread safety $this->env['php']['raw_thread_safety'] = isset($sysInfo['thread_safety']) ? $sysInfo['thread_safety'] : false; if (isset($sysInfo['thread_safety'])) { $this->env['php']['thread_safety'] = $sysInfo['thread_safety'] ? '线程安全' : '非线程安全'; } else { $this->env['php']['thread_safety'] = '未知'; } // Check swoole loader installation if (isset($sysInfo['swoole_loader']) and isset($sysInfo['swoole_loader_version'])) { $this->env['php']['swoole_loader']['status'] = $sysInfo['swoole_loader'] ? "已安装" : '未安装'; if ($sysInfo['swoole_loader_version'] !== false) { $this->env['php']['swoole_loader']['version'] = "" . $sysInfo['swoole_loader_version'] . ""; } else { $this->env['php']['swoole_loader']['version'] = '未知'; } } else { $this->env['php']['swoole_loader']['status'] = '未安装'; $this->env['php']['swoole_loader']['version'] = '未知'; } $this->html($sysInfo); } /** * 页面输出内容 * @Author:Qinii * @Date: 2020/9/10 * @param $this ->>env * @param $sysInfo */ public function html($sysInfo) { $html = ''; // Header $html_header = ' %s '; $html_header = sprintf($html_header, 'CRMEB Swoole Compiler 安装向导'); $html_body = '
    '; $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 = '
    检查当前环境
    '; // Error infomation $html_error = ""; if (!empty($this->env['php']['loaded_incompatible_extensions'])) { $html_error = '
    错误信息

    %s

    '; $err_msg = "当前PHP包含与swoole_compiler_loader扩展不兼容的扩展" . implode(',', $this->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($this->env['php']['version'], 0, 3); $phpversion = str_replace('.', '', $phpversion); $loaderFileName = ''; if ($this->env['os']['name'] == "windows") { $loaderFileName = 'php_swoole_loader_php' . $phpversion; if ($this->env['php']['thread_safety'] == '非线程安全') { $loaderFileName .= '_nzts_x64.dll'; } else { $loaderFileName .= '_zts_x64.dll'; } } else { if ($this->env['php']['thread_safety'] != '非线程安全') { $loaderFileName = 'swoole_loader' . $phpversion . '_zts.so'; } else { $loaderFileName = 'swoole_loader' . $phpversion . '.so'; } } $html_body_loader .= '

    1 - 安装Swoole Loader

    前往根目录 /install/swoole-loader/' . $loaderFileName . '扩展文件上传到当前PHP的扩展安装目录中:

    ' . $this->env['php']['extension_dir'] . '

    '; $html_body_loader .= '

    2 - 修改php.ini配置(如已修改配置,请忽略此步骤,不必重复添加)

    '; $html_body_loader .= '编辑此PHP配置文件:' . $this->env['php']['ini_loaded_file'] . ',在此文件底部结尾处加入如下配置
    '; if ($this->env['os']['name'] == "windows") { $html_body_loader .= '

    extension=' . $this->env['php']['extension_dir'] . DIRECTORY_SEPARATOR . $loaderFileName . '
    注意:需要名称和刚才上传到当前PHP的扩展安装目录中的文件名一致'; } else { $html_body_loader .= '
    extension=' . $this->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 = ' '; $html = $html_header . $html_body . $html_footer; return Response::create()->content($html)->send(); } public function w_getSysInfo() { $sysEnv = []; // Get content of phpinfo ob_start(); phpinfo(); $sysInfo = ob_get_contents(); ob_end_clean(); // Explode phpinfo content if ($this->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; } }