forked from kuafuRace/phprap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstallController.php
More file actions
executable file
·334 lines (225 loc) · 9.52 KB
/
Copy pathInstallController.php
File metadata and controls
executable file
·334 lines (225 loc) · 9.52 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
<?php
namespace app\controllers\home;
use Yii;
use yii\db\Exception;
use yii\web\Response;
use app\models\Account;
use app\models\loginLog\CreateLog;
class InstallController extends PublicController
{
public function beforeAction($action)
{
if($this->isInstalled()){
exit('PHPRAP已安装过,请不要重复安装,如果需要重新安装,请先删除runtime/install/install.lock');
}
return true;
}
/**
* 安装步骤一,环境检测
* @return array|string
*/
public function actionStep1()
{
$request = Yii::$app->request;
if($request->isPost){
Yii::$app->response->format = Response::FORMAT_JSON;
Yii::$app->cache->set('step', 1);
return ['status' => 'success', 'callback' => url('home/install/step2')];
}
$step1 = [
'runtime' => [
'have_chmods' => $this->getChmodsLabel(Yii::getAlias("@runtime")),
'require_chmods' => '可读、可写',
'check_chmod' => is_writable(Yii::getAlias("@runtime")),
],
'runtime/cache' => [
'have_chmods' => $this->getChmodsLabel(Yii::getAlias("@runtime") . '/cache'),
'require_chmods' => '可读、可写',
'check_chmod' => is_writable(Yii::getAlias("@runtime") . '/cache'),
],
'runtime/install' => [
'have_chmods' => $this->getChmodsLabel(Yii::getAlias("@runtime") . '/install'),
'require_chmods' => '可读、可写',
'check_chmod' => is_writable(Yii::getAlias("@runtime") . '/install'),
],
'runtime/logs' => [
'have_chmods' => $this->getChmodsLabel(Yii::getAlias("@runtime") . '/logs'),
'require_chmods' => '可读、可写',
'check_chmod' => is_writable(Yii::getAlias("@runtime") . '/logs'),
],
'configs/db.php' => [
'have_chmods' => $this->getChmodsLabel(Yii::getAlias("@app") . '/configs/db.php'),
'require_chmods' => '可读、可写',
'check_chmod' => is_writable(Yii::getAlias("@app") . '/configs/db.php'),
],
];
return $this->display('/install/step1', ['step1' => $step1]);
}
/**
* 安装步骤二,初始化数据库并将数据库信息写入配置文件
* @return array|string|\yii\web\Response
*/
public function actionStep2()
{
$request = Yii::$app->request;
if(Yii::$app->cache->get('step') != 1){
return $this->redirect(['home/install/step1']);
}
if($request->isPost){
Yii::$app->response->format = Response::FORMAT_JSON;
$step2 = $request->post('Step2');
$db = [
'dsn' => "mysql:host={$step2['host']};port={$step2['port']}}",
'username' => $step2['username'],
'password' => $step2['password'],
'charset' => 'utf8',
];
$connection = new \yii\db\Connection($db);
// 判断数据库连接状态
try {
$connection->open();
} catch(Exception $e) {
return ['status' => 'error', 'message' => '数据库连接失败,请检查数据库配置信息是否正确'];
}
if(!$connection->isActive){
return ['status' => 'error', 'message' => '当前数据库连接处于非激活状态,请检查PDO安装是否正确'];
}
// 创建数据库
$sql = "CREATE DATABASE IF NOT EXISTS {$step2['dbname']} CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';";
if(!$connection->createCommand($sql)->execute()){
return ['status' => 'error', 'message' => "数据库 {$step2['dbname']} 创建失败,没有创建数据库权限,请手动创建数据库"];
}
$db['dsn'] = "mysql:host={$step2['host']};port={$step2['port']};dbname={$step2['dbname']}";
$db['tablePrefix'] = $step2['prefix'];
$db = ['class' => 'yii\db\Connection'] + $db;
$config = "<?php\r\nreturn\n" . var_export($db,true) . "\r\n?>";
// 将数据库配置信息写入配置文件
if(file_put_contents(Yii::getAlias("@app") . '/configs/db.php', $config) === false){
return ['status' => 'error', 'message' => '数据库配置文件写入错误,请检查configs/db.php文件是否有可写权限'];
}
Yii::$app->cache->set('step', 2);
return ['status' => 'success', 'callback' => url('home/install/step3')];
}
return $this->display('/install/step2');
}
/**
* 安装步骤三,创建总管理员
* @return string|\yii\web\Response
*/
public function actionStep3()
{
$request = Yii::$app->request;
if(Yii::$app->cache->get('step') != 2){
return $this->redirect(['home/install/step2']);
}
if($request->isPost){
Yii::$app->response->format = Response::FORMAT_JSON;
$step3 = $request->post('Step3');
try {
// 开启事务
$transaction = Yii::$app->db->beginTransaction();
// 数据库初始化
$sql = $this->getInitSql();
if(Yii::$app->db->createCommand($sql)->execute() === false){
$transaction->rollBack();
return ['status' => 'error', 'message' => '数据库初始化安装失败,请检查 configs/db.php 文件是否完整'];
}
// 插入管理员
$account = new Account();
$account->status = $account::ACTIVE_STATUS;
$account->type = $account::ADMIN_TYPE;
$account->name = $step3['name'];
$account->email = $step3['email'];
$account->ip = Yii::$app->request->userIP;
$account->location = $account->getLocation();
$account->created_at = date('Y-m-d H:i:s');
$account->setPassword($step3['password']);
$account->generateAuthKey();
if(!$account->save()){
$transaction->rollBack();
return ['status' => 'error', 'message' => $account->getErrorMessage(), 'label' => $account->getErrorLabel()];
}
// 记录日志
$loginLog = new CreateLog();
$loginLog->user_id = $account->id;
$loginLog->user_name = $account->name;
$loginLog->user_email = $account->email;
if(!$loginLog->store()){
return false;
}
// 事务提交
$transaction->commit();
// 保存登录状态
$login_keep_time = config('login_keep_time', 'safe');
Yii::$app->user->login($account, 60*60*$login_keep_time);
Yii::$app->cache->set('step', 3);
return ['status' => 'success', 'callback' => url('home/install/step4')];
} catch (Exception $e) {
return ['status' => 'error', 'message' => '数据库初始化安装失败,' . $e->getMessage()];
}
}
return $this->display('/install/step3');
}
/**
* 安装步骤四,显示安装过程
* @return string|\yii\web\Response
*/
public function actionStep4()
{
if(Yii::$app->cache->get('step') != 3){
return $this->redirect(['home/install/step3']);
}
// 创建安装锁文件
if(file_put_contents(Yii::getAlias("@runtime") . '/install/install.lock', json_encode(['installed_at' => date('Y-m-d H:i:s')])) === false){
return ['status' => 'error', 'message' => '数据库锁文件写入错误,请检查 runtime/install 文件夹是否有可写权限'];
}
Yii::$app->cache->set('step', 4);
// 获取所有数据表
$sql = "show tables";
$tables = Yii::$app->db->createCommand($sql)->queryColumn();
return $this->display('/install/step4', ['tables' => $tables]);
}
// 获取权限
private function getChmodsLabel($dirName)
{
$chmod = '';
if (is_readable ($dirName)) {
$chmod = '可读、';
}
if (is_writable ($dirName)) {
$chmod .= '可写、';
}
if (is_executable ($dirName)) {
$chmod .= '可执行、';
}
return trim($chmod, '、');
}
// 获取安装初始化sql语句
private function getInitSql()
{
// 读取初始化数据库脚本文件内容
$lines = file(Yii::getAlias("@runtime") .'/install/db.sql');
$sql = "";
// 循环排除掉不合法的sql语句
foreach($lines as $line){
$line = trim($line);
if($line != ""){
if(!($line{0} == "#" || $line{0}.$line{1} == "--")){
// 将表前缀替换成自定义前缀
$line = str_replace("doc_", Yii::$app->db->tablePrefix, $line);
$sql .= $line;
}
}
}
return $sql;
}
/** 展示模板
* @param $view
* @param array $params
* @return string
*/
public function display($view, $params = [])
{
exit($this->render($view . '.html', $params));
}
}