| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- <?php
- // +----------------------------------------------------------------------
- // | Description: 安装
- // +----------------------------------------------------------------------
- // | Author: Michael_xu | gengxiaoxu@5kcrm.com
- // +----------------------------------------------------------------------
-
- namespace app\admin\controller;
-
- use think\Controller;
- use think\Request;
- use think\Db;
- use Env;
-
- class Install extends Controller
- {
- // private $count = 100;
- // private $now = 0;
-
- public function _initialize()
- {
- /*防止跨域*/
- header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
- header('Access-Control-Allow-Credentials: true');
- header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
- header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, authKey, sessionId");
- $param = Request::instance()->param();
- $this->param = $param;
-
- // $request = request();
- // $m = strtolower($request->module());
- // $c = strtolower($request->controller());
- // $a = strtolower($request->action());
- //
- // if (!in_array($a, array('upgrade','upgradeprocess','checkversion')) && file_exists(CONF_PATH . "install.lock")) {
- // echo "<meta http-equiv='content-type' content='text/html; charset=UTF-8'> <script>alert('请勿重复安装!');location.href='".$_SERVER["HTTP_HOST"]."';</script>";
- // die();
- // }
- }
-
- private $upgrade_site = "http://message.72crm.com/";
-
- /**
- * [index 安装步骤]
- * @author Michael_xu
- * @param
- */
- public function index()
- {
- if (file_exists(CONF_PATH . "install.lock")) {
- echo "<meta http-equiv='content-type' content='text/html; charset=UTF-8'> <script>alert('请勿重复安装!');location.href='".$_SERVER["HTTP_HOST"]."';</script>";
- die();
- }
- if (!file_exists(getcwd() . "/public/sql/5kcrm.sql")) {
- echo "<meta http-equiv='content-type' content='text/html; charset=UTF-8'> <script>alert('缺少必要的数据库文件!');location.href='".$_SERVER["HTTP_HOST"]."';</script>";
- die();
- }
- return $this->fetch('index');
- }
-
- public function step1()
- {
- session('install_error',null);
- $data = [];
- $data['env'] = self::checkNnv();
- $data['dir'] = self::checkDir();
- $data['version'] = $this->version();
- $this->assign('data',$data);
- return $this->fetch('step1');
- }
-
- //版本
- public function version()
- {
- $res = include(CONF_PATH.'version.php');
- return $res ? : array('VERSION' => '11.0.0','RELEASE' => '20210219');
- }
-
- public function step2(){
- if (session('install_error')){
- echo "<meta http-equiv='content-type' content='text/html; charset=UTF-8'> <script>alert('环境检测未通过,不能进行下一步操作!');location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
- die();
- }
- $data['os'] = PHP_OS;
- $data['php'] = phpversion();
- $data['version'] = $this->version();
- $this->assign('envir_data',$data);
- return $this->fetch();
- }
-
- public function step3(){
- return $this->fetch();
- }
-
- public function step4(){
- if (session('install_error')){
- return resultArray(['error' => '环境检测未通过,不能进行下一步操作!']);
- }
- if (file_exists(CONF_PATH . "install.lock")) {
- return resultArray(['error' => '请勿重复安装!']);
- }
- if (!file_exists(getcwd() . "/public/sql/5kcrm.sql")) {
- return resultArray(['error' => '缺少必要的数据库文件!']);
- }
- $temp = $this->param;
- $param = $temp['form'];
- $db_config['type'] = 'mysql';
- $db_config['hostname'] = $param['databaseUrl'];
- $db_config['hostport'] = $param['databasePort'];
- $db_config['database'] = $param['databaseName'];
- $db_config['username'] = $param['databaseUser'];
- $db_config['password'] = $param['databasePwd'];
- $db_config['prefix'] = $param['databaseTable'];
-
- $username = $param['root'];
- $password = $param['pwd'];
- $wkcode = $param['wkcode'];
- if (empty($db_config['hostname'])) {
- return resultArray(['error' => '请填写数据库主机!']);
- }
- if (empty($db_config['hostport'])) {
- return resultArray(['error' => '请填写数据库端口!']);
- }
- if (preg_match('/[^0-9]/', $db_config['hostport'])) {
- return resultArray(['error' => '数据库端口只能是数字!']);
- }
- if (empty($db_config['database'])) {
- return resultArray(['error' => '请填写数据库名!']);
- }
- if (empty($db_config['username'])) {
- return resultArray(['error' => '请填写数据库用户名!']);
- }
- if (empty($db_config['password'])) {
- return resultArray(['error' => '请填写数据库密码!']);
- }
- if (empty($db_config['prefix'])) {
- return resultArray(['error' => '请填写表前缀!']);
- }
- if (preg_match('/[^a-z0-9_]/i', $db_config['prefix'])) {
- return resultArray(['error' => '表前缀只能包含数字、字母和下划线!']);
- }
- if (empty($username)) {
- return resultArray(['error' => '请填写管理员用户名!']);
- }
- if (empty($wkcode)) {
- return resultArray(['error' => '请填写序列号!']);
- }
- $resCheckData = checkWkCode($wkcode);
- if (!$resCheckData) {
- return resultArray(['error' => '序列号错误!']);
- }
- $resData = object_to_array(json_decode($resCheckData));
- if ($resData['date'] != date('Y-m-d')) {
- return resultArray(['error' => '序列号已失效,请前往悟空官网个人中心获取最新数据!']);
- }
- if (empty($password)) {
- return resultArray(['error' => '请填写管理员密码!']);
- }
- session('install_count','');
- session('install_now','');
- $database = $db_config['database'];
- unset($db_config['database']);
- $connect = Db::connect($db_config);
- // 检测数据库连接
- try{
- $ret = $connect->execute('select version()');
- }catch(\Exception $e){
- return resultArray(['error' => '数据库连接失败,请检查数据库配置!']);
- }
- $check = $connect->execute("SELECT * FROM information_schema.schemata WHERE schema_name='".$database."'");
- if (!$check && !$connect->execute("CREATE DATABASE IF NOT EXISTS `".$database."` default collate utf8_general_ci ")) {
- return resultArray(['error' => '没有找到您填写的数据库名且无法创建!请检查连接账号是否有创建数据库的权限!']);
- }
- $db_config['database'] = $database;
- self::mkDatabase($db_config);
- self::mkLicense($wkcode);
- $C_Patch = substr($_SERVER['SCRIPT_FILENAME'],0,-10);
- $sql = file_get_contents( $C_Patch.'/public/sql/5kcrm.sql');
- $sqlList = parse_sql($sql, 0, ['5kcrm_' => $db_config['prefix']]);
- if ($sqlList) {
- $sqlList = array_filter($sqlList);
- $install_count = count($sqlList);
- session('install_count',$install_count);
- foreach ($sqlList as $k=>$v) {
- $install_now = $k+1;
- session('install_now',$install_now);
- try {
- $temp_sql = $v.';';
- Db::connect($db_config)->query($temp_sql);
- } catch(\Exception $e) {
- // return resultArray(['error' => '请启用InnoDB数据引擎,并检查数据库是否有DROP和CREATE权限']);
- return resultArray(['error' => '数据库sql安装出错,请操作数据库手动导入sql文件']);
- }
- }
- }
- $salt = substr(md5(time()),0,4);
- $password = user_md5(trim($password), $salt, $username);
- //插入信息
- Db::connect($db_config)->query("insert into ".$db_config['prefix']."admin_user (username, password, salt, img, thumb_img, realname, create_time, num, email, mobile, sex, status, structure_id, post, parent_id, type, authkey, authkey_time ) values ( '".$username."', '".$password."', '".$salt."', '', '', '管理员', ".time().", '', '', '".$username."', '', 1, 1, 'CEO', 0, 1, '', 0 )");
- Db::connect($db_config)->query("insert into ".$db_config['prefix']."hrm_user_det (user_id, join_time, type, status, userstatus, create_time, update_time, mobile, sex, age, job_num, idtype, idnum, birth_time, nation, internship, done_time, parroll_id, email, political, location, leave_time ) values ( 1, ".time().", 1, 1, 2, ".time().", ".time().", '".$username."', '', 0, '', 0, '', '', 0, 0, 0, 0, '', '', '', 0 )");
- touch(CONF_PATH . "install.lock");
- return resultArray(['data'=>'安装成功']);
- }
-
- public function step5()
- {
- return $this->fetch();
- }
-
- //ajax 进度条
- public function progress()
- {
- $data['length'] = session('install_count');
- $data['now'] = session('install_now');
- return resultArray(['data'=>$data]);
- }
-
- //添加database.php文件
- private function mkDatabase(array $data)
- {
- $code = <<<INFO
- <?php
- return [
- // 数据库类型
- 'type' => 'mysql',
- // 服务器地址
- 'hostname' => '{$data['hostname']}',
- // 数据库名
- 'database' => '{$data['database']}',
- // 用户名
- 'username' => '{$data['username']}',
- // 密码
- 'password' => '{$data['password']}',
- // 端口
- 'hostport' => '{$data['hostport']}',
- // 连接dsn
- 'dsn' => '',
- // 数据库连接参数
- 'params' => [],
- // 数据库编码默认采用utf8
- 'charset' => 'utf8',
- // 数据库表前缀
- 'prefix' => '{$data['prefix']}',
- // 数据库调试模式
- 'debug' => true,
- // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
- 'deploy' => 0,
- // 数据库读写是否分离 主从式有效
- 'rw_separate' => false,
- // 读写分离后 主服务器数量
- 'master_num' => 1,
- // 指定从服务器序号
- 'slave_no' => '',
- // 自动读取主库数据
- 'read_master' => false,
- // 是否严格检查字段是否存在
- 'fields_strict' => true,
- // 数据集返回类型
- 'resultset_type' => 'array',
- ];
-
- INFO;
- file_put_contents( CONF_PATH.'database.php', $code);
- // 判断写入是否成功
- $config = include CONF_PATH.'database.php';
- if (empty($config['database']) || $config['database'] != $data['database']) {
- return resultArray(['error' => '[config/database.php]数据库配置写入失败!']);
- }
- return true;
- }
-
- //检查目录权限
- public function check_dir_iswritable($dir_path){
- $dir_path=str_replace( '\\','/',$dir_path);
- $is_writale=1;
- if (!is_dir($dir_path)) {
- $is_writale=0;
- return $is_writale;
- } else {
- $file_hd=@fopen($dir_path.'/test.txt','w');
- if (!$file_hd) {
- @fclose($file_hd);
- @unlink($dir_path.'/test.txt');
- $is_writale=0;
- return $is_writale;
- }
- $dir_hd = opendir($dir_path);
- while (false !== ($file=readdir($dir_hd))) {
- if ($file != "." && $file != "..") {
- if (is_file($dir_path.'/'.$file)) {
- //文件不可写,直接返回
- if (!is_writable($dir_path.'/'.$file)) {
- return 0;
- }
- } else {
- $file_hd2=@fopen($dir_path.'/'.$file.'/test.txt','w');
- if (!$file_hd2) {
- @fclose($file_hd2);
- @unlink($dir_path.'/'.$file.'/test.txt');
- $is_writale=0;
- return $is_writale;
- }
- //递归
- $is_writale=check_dir_iswritable($dir_path.'/'.$file);
- }
- }
- }
- }
- return $is_writale;
- }
-
- /**
- * [checkVersion 检查升级]
- * @author Michael_xu
- * @param
- */
- public function checkVersion(){
- $version = Config::load('version');
- $info = sendRequest($this->upgrade_site.'index.php?m=version&a=checkVersion', $version['VERSION']);
- if ($info){
- return resultArray(['data' => $info]);
- } else {
- return resultArray(['error' => '检查新版本出错!']);
- }
- }
-
- /**
- * 环境检测
- * @return array
- */
- private function checkNnv()
- {
- $items = [
- 'os' => ['操作系统', PHP_OS, '类Unix', 'ok'],
- 'php' => ['PHP版本', PHP_VERSION, '7.3 ( <em style="color: #888; font-size: 12px;">>= 5.6</em> )', 'ok','性能更佳'],
- 'gd' => ['gd', '开启', '开启', 'ok'],
- 'openssl' => ['openssl', '开启', '开启', 'ok'],
- 'pdo' => ['pdo', '开启', '开启', 'ok'],
- ];
- session('install_error','');
- if (substr($items['php'][1],0,3) < '5.6') {
- $items['php'][3] = 'error';
- session('install_error', true);
- }
-
- if (!extension_loaded('gd')) {
- $items['gd'][1] = '未开启';
- $items['gd'][3] = 'error';
- session('install_error', true);
- }
- if (!extension_loaded('openssl')) {
- $items['openssl'][1] = '未开启';
- $items['openssl'][3] = 'error';
- session('install_error', true);
- }
- if (!extension_loaded('pdo')) {
- $items['pdo'][1] = '未开启';
- $items['pdo'][3] = 'error';
- session('install_error', true);
- }
- // dump($items);die;
- return $items;
- }
-
- /**
- * 目录权限检查
- * @return array
- */
- private function checkDir()
- {
- $items = [
- ['dir', $this->root_path.'application', 'application', '读写', '读写', 'ok'],
- ['dir', $this->root_path.'extend', 'extend', '读写', '读写', 'ok'],
- ['dir', $this->root_path.'runtime', './temp', '读写', '读写', 'ok'],
- ['dir', $this->root_path.'public', './upload', '读写', '读写', 'ok'],
- ['file', $this->root_path.'config', 'config', '读写', '读写', 'ok'],
- ];
- foreach ($items as &$v) {
- if ($v[0] == 'dir') {// 文件夹
- if (!is_writable($v[1])) {
- if (is_dir($v[1])) {
- $v[4] = '不可写';
- $v[5] = 'no';
- } else {
- $v[4] = '不存在';
- $v[5] = 'no';
- }
- session('install_error', true);
- }
- } else {// 文件
- if (!is_writable($v[1])) {
- $v[4] = '不可写';
- $v[5] = 'no';
- session('install_error', true);
- }
- }
- }
- return $items;
- }
-
- /**
- * 验证序列号
- * @param
- * @return
- */
- public function checkCodeOld($username) {
- $encryption = md5($username);
- $substr = substr($username, strlen($username)-6);
- $subArr = str_split($substr, 1);
- $code = '';
- for ($i = 0; $i <= 5; $i++) {
- $code .= $encryption[$subArr[$i]];
- }
- return $code;
- }
-
- //写入license文件
- private function mkLicense($wkcode)
- {
- file_put_contents( CONF_PATH.'license.dat', $wkcode);
- // 判断写入是否成功
- // $config = include CONF_PATH.'license.dat';
- // if (empty($config)) {
- // return resultArray(['error' => 'license配置写入失败!']);
- // }
- return true;
- }
- }
|