IndexController.php
12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\install\controller;
use app\admin\model\ThemeModel;
use think\Controller;
use think\Db;
class IndexController extends Controller
{
public function _initialize()
{
if (cmf_is_installed()) {
$this->error('网站已经安装', cmf_get_root() . '/');
}
}
// 安装首页
public function index()
{
return $this->fetch(":index");
}
public function step2()
{
// if (file_exists_case('data/conf/config.php')) {
// @unlink('data/conf/config.php');
// }
$data = [];
$data['phpversion'] = @phpversion();
$data['os'] = PHP_OS;
$tmp = function_exists('gd_info') ? gd_info() : [];
// $server = $_SERVER["SERVER_SOFTWARE"];
// $host = $this->request->host();
// $name = $_SERVER["SERVER_NAME"];
// $max_execution_time = ini_get('max_execution_time');
// $allow_reference = (ini_get('allow_call_time_pass_reference') ? '<font color=green>[√]On</font>' : '<font color=red>[×]Off</font>');
// $allow_url_fopen = (ini_get('allow_url_fopen') ? '<font color=green>[√]On</font>' : '<font color=red>[×]Off</font>');
// $safe_mode = (ini_get('safe_mode') ? '<font color=red>[×]On</font>' : '<font color=green>[√]Off</font>');
$err = 0;
if (empty($tmp['GD Version'])) {
$gd = '<font color=red>[×]Off</font>';
$err++;
} else {
$gd = '<font color=green>[√]On</font> ' . $tmp['GD Version'];
}
if (class_exists('pdo')) {
$data['pdo'] = '<i class="fa fa-check correct"></i> 已开启';
} else {
$data['pdo'] = '<i class="fa fa-remove error"></i> 未开启';
$err++;
}
if (extension_loaded('pdo_mysql')) {
$data['pdo_mysql'] = '<i class="fa fa-check correct"></i> 已开启';
} else {
$data['pdo_mysql'] = '<i class="fa fa-remove error"></i> 未开启';
$err++;
}
if (extension_loaded('curl')) {
$data['curl'] = '<i class="fa fa-check correct"></i> 已开启';
} else {
$data['curl'] = '<i class="fa fa-remove error"></i> 未开启';
$err++;
}
if (extension_loaded('gd')) {
$data['gd'] = '<i class="fa fa-check correct"></i> 已开启';
} else {
$data['gd'] = '<i class="fa fa-remove error"></i> 未开启';
if (function_exists('imagettftext')) {
$data['gd'] .= '<br><i class="fa fa-remove error"></i> FreeType Support未开启';
}
$err++;
}
if (extension_loaded('mbstring')) {
$data['mbstring'] = '<i class="fa fa-check correct"></i> 已开启';
} else {
$data['mbstring'] = '<i class="fa fa-remove error"></i> 未开启';
if (function_exists('imagettftext')) {
$data['mbstring'] .= '<br><i class="fa fa-remove error"></i> FreeType Support未开启';
}
$err++;
}
if (extension_loaded('fileinfo')) {
$data['fileinfo'] = '<i class="fa fa-check correct"></i> 已开启';
} else {
$data['fileinfo'] = '<i class="fa fa-remove error"></i> 未开启';
$err++;
}
if (ini_get('file_uploads')) {
$data['upload_size'] = '<i class="fa fa-check correct"></i> ' . ini_get('upload_max_filesize');
} else {
$data['upload_size'] = '<i class="fa fa-remove error"></i> 禁止上传';
}
if (function_exists('session_start')) {
$data['session'] = '<i class="fa fa-check correct"></i> 支持';
} else {
$data['session'] = '<i class="fa fa-remove error"></i> 不支持';
$err++;
}
if (version_compare(phpversion(), '5.6.0', '>=') && version_compare(phpversion(), '7.0.0', '<') && ini_get('always_populate_raw_post_data') != -1) {
$data['always_populate_raw_post_data'] = '<i class="fa fa-remove error"></i> 未关闭';
$data['show_always_populate_raw_post_data_tip'] = true;
$err++;
} else {
$data['always_populate_raw_post_data'] = '<i class="fa fa-check correct"></i> 已关闭';
}
$folders = [
realpath(CMF_ROOT . 'data') . DS,
realpath('./upload') . DS,
];
$newFolders = [];
foreach ($folders as $dir) {
$testDir = $dir;
sp_dir_create($testDir);
if (sp_testwrite($testDir)) {
$newFolders[$dir]['w'] = true;
} else {
$newFolders[$dir]['w'] = false;
$err++;
}
if (is_readable($testDir)) {
$newFolders[$dir]['r'] = true;
} else {
$newFolders[$dir]['r'] = false;
$err++;
}
}
$data['folders'] = $newFolders;
$this->assign($data);
return $this->fetch(":step2");
}
public function step3()
{
return $this->fetch(":step3");
}
public function step4()
{
session(null);
if ($this->request->isPost()) {
//创建数据库
$dbConfig = [];
$dbConfig['type'] = "mysql";
$dbConfig['hostname'] = $this->request->param('dbhost');
$dbConfig['username'] = $this->request->param('dbuser');
$dbConfig['password'] = $this->request->param('dbpw');
$dbConfig['hostport'] = $this->request->param('dbport');
$dbConfig['charset'] = $this->request->param('dbcharset', 'utf8mb4');
$userLogin = $this->request->param('manager');
$userPass = $this->request->param('manager_pwd');
$userEmail = $this->request->param('manager_email');
//检查密码。空 6-32字符。
empty($userPass) && $this->error("密码不可以为空");
strlen($userPass) < 6 && $this->error("密码长度最少6位");
strlen($userPass) > 32 && $this->error("密码长度最多32位");
$db = Db::connect($dbConfig);
$dbName = $this->request->param('dbname');
$sql = "CREATE DATABASE IF NOT EXISTS `{$dbName}` DEFAULT CHARACTER SET " . $dbConfig['charset'];
$db->execute($sql) || $this->error($db->getError());
$dbConfig['database'] = $dbName;
$dbConfig['prefix'] = $this->request->param('dbprefix', '', 'trim');
session('install.db_config', $dbConfig);
$sql = cmf_split_sql(APP_PATH . 'install/data/thinkcmf.sql', $dbConfig['prefix'], $dbConfig['charset']);
session('install.sql', $sql);
$this->assign('sql_count', count($sql));
session('install.error', 0);
$siteName = $this->request->param('sitename');
$seoKeywords = $this->request->param('sitekeywords');
$siteInfo = $this->request->param('siteinfo');
session('install.site_info', [
'site_name' => $siteName,
'site_seo_title' => $siteName,
'site_seo_keywords' => $seoKeywords,
'site_seo_description' => $siteInfo
]);
session('install.admin_info', [
'user_login' => $userLogin,
'user_pass' => $userPass,
'user_email' => $userEmail
]);
return $this->fetch(":step4");
} else {
exit;
}
}
public function install()
{
$dbConfig = session('install.db_config');
$sql = session('install.sql');
if (empty($dbConfig) || empty($sql)) {
$this->error("非法安装!");
}
$sqlIndex = $this->request->param('sql_index', 0, 'intval');
$db = Db::connect($dbConfig);
if ($sqlIndex >= count($sql)) {
$installError = session('install.error');
$this->success("安装完成!", '', ['done' => 1, 'error' => $installError]);
}
$sqlToExec = $sql[$sqlIndex] . ';';
$result = sp_execute_sql($db, $sqlToExec);
if (!empty($result['error'])) {
$installError = session('install.error');
$installError = empty($installError) ? 0 : $installError;
session('install.error', $installError + 1);
$this->error($result['message'], '', [
'sql' => $sqlToExec,
'exception' => $result['exception']
]);
} else {
$this->success($result['message'], '', [
'sql' => $sqlToExec
]);
}
}
public function setDbConfig()
{
$dbConfig = session('install.db_config');
$dbConfig['authcode'] = cmf_random_string(18);
$result = sp_create_db_config($dbConfig);
if ($result) {
$this->success("数据配置文件写入成功!");
} else {
$this->error("数据配置文件写入失败!");
}
}
public function setSite()
{
$dbConfig = session('install.db_config');
if (empty($dbConfig)) {
$this->error("非法安装!");
}
$siteInfo = session('install.site_info');
$admin = session('install.admin_info');
$admin['id'] = 1;
$admin['user_pass'] = cmf_password($admin['user_pass']);
$admin['user_type'] = 1;
$admin['create_time'] = time();
$admin['user_status'] = 1;
$admin['user_nickname'] = $admin['user_login'];
try {
cmf_set_option('site_info', $siteInfo);
Db::name('user')->insert($admin);
} catch (\Exception $e) {
$this->error("网站创建失败!");
}
$this->success("网站创建完成!");
}
public function installTheme()
{
$themeModel = new ThemeModel();
$result = $themeModel->installTheme(config('cmf_default_theme'));
if ($result === false) {
$this->error('模板不存在!');
}
session("install.step", 4);
$this->success("模板安装成功");
}
public function step5()
{
if (session("install.step") == 4) {
@touch(CMF_ROOT . 'data/install.lock');
return $this->fetch(":step5");
} else {
$this->error("非法安装!");
}
}
public function testDbPwd()
{
if ($this->request->isPost()) {
$dbConfig = $this->request->param();
$dbConfig['type'] = "mysql";
$supportInnoDb = false;
try {
Db::connect($dbConfig)->query("SELECT VERSION();");
$engines = Db::connect($dbConfig)->query("SHOW ENGINES;");
foreach ($engines as $engine) {
if ($engine['Engine'] == 'InnoDB' && $engine['Support'] != 'NO') {
$supportInnoDb = true;
break;
}
}
} catch (\Exception $e) {
$this->error('数据库账号或密码不正确!');
}
if($supportInnoDb){
$this->success('验证成功!');
}else{
$this->error('数据库账号密码验证通过,但不支持InnoDb!');
}
} else {
$this->error('非法请求方式!');
}
}
public function testRewrite()
{
$this->success('success');
}
}