-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInstallController.php
More file actions
340 lines (298 loc) · 8.65 KB
/
InstallController.php
File metadata and controls
340 lines (298 loc) · 8.65 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
<?php
/**
* Install Controller
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('InstallAppController', 'Install.Controller');
App::uses('InstallUtil', 'Install.Utility');
App::uses('InstallValidatorUtil', 'Install.Utility');
App::uses('NetCommonsCache', 'NetCommons.Utility');
/**
* Install Controller
*
* @package NetCommons\Install\Controlelr
*/
class InstallController extends InstallAppController {
/**
* Helpers
*/
public $helpers = array(
'M17n.M17n',
);
/**
* beforeFilter
*
* @return void
* @throws NotFoundException
*/
public function beforeFilter() {
if (Configure::read('NetCommons.installed')) {
throw new NotFoundException();
}
$this->Auth->allow();
$this->layout = 'Install.default';
//テストのために必要
if (empty($this->InstallUtil) ||
substr(get_class($this->InstallUtil), 0, strlen('Mock_')) !== 'Mock_') {
$this->InstallUtil = new InstallUtil();
}
if (isset($this->request->query['language'])) {
Configure::write('Config.language', $this->request->query['language']);
$this->Session->write('Config.language', $this->request->query['language']);
} else {
Configure::write('Config.language', 'ja');
$this->Session->write('Config.language', 'ja');
}
$this->Components->unload('NetCommons.Permission');
}
/**
* ステップ 1
* application.ymlの初期値セット
*
* @return void
*/
public function index() {
$this->set('pageTitle', __d('install', 'Term'));
//// Initialize master database connection
//$configs = $this->InstallUtil->chooseDBByEnvironment();
//if (! $this->InstallUtil->saveDBConf($configs)) {
// $message = __d(
// 'install',
// 'Failed to write %s. Please check permission.',
// array(APP . 'Config' . DS . 'database.php')
// );
// $this->Session->setFlash($message);
// return;
//}
if (! $this->InstallUtil->installApplicationYaml($this->request->query)) {
$message = __d(
'install',
'Failed to write %s. Please check permission.',
array(APP . 'Config' . DS . 'application.yml')
);
$this->Session->setFlash($message);
return;
}
if ($this->request->is('post')) {
$this->redirect(array(
'action' => 'init_permission',
'?' => ['language' => Configure::read('Config.language')]
));
}
}
/**
* ステップ 2
* パーミッションのチェック
*
* @return void
*/
public function init_permission() {
$this->set('pageTitle', __d('install', 'Permissions'));
$validator = new InstallValidatorUtil();
$ret = true;
$versions = $validator->versions();
$cliVersions = $validator->cliVersions();
$permissions = $validator->permissions();
$messages = array_merge($versions, $cliVersions, $permissions);
foreach ($messages as $message) {
if ($message['error']) {
$ret = false;
break;
}
}
// Show current page on failure
$this->set('cliVersions', $cliVersions);
$this->set('versions', $versions);
$this->set('permissions', $permissions);
$this->set('canInstall', $ret);
if (! $ret) {
foreach ($messages as $output) {
CakeLog::error($output['message']);
}
return;
}
if ($this->request->is('post')) {
$this->redirect(array(
'action' => 'init_db',
'?' => ['language' => Configure::read('Config.language')]
));
return;
}
}
/**
* ステップ 3
* データベース設定
*
* @return void
*/
public function init_db() {
$this->set('pageTitle', __d('install', 'Database Settings'));
// Destroy session in order to handle ping request
$this->Session->destroy();
$this->set('masterDB', $this->InstallUtil->chooseDBByEnvironment());
$this->set('errors', array());
$this->set('validationErrors', array());
if ($this->request->is('post')) {
set_time_limit(1800);
if ($this->request->data['prefix'] &&
substr($this->request->data['prefix'], -1, 1) !== '_') {
$this->request->data['prefix'] .= '_';
}
if ($this->InstallUtil->validatesDBConf($this->request->data)) {
$this->InstallUtil->saveDBConf($this->request->data);
} else {
$this->set('validationErrors', $this->InstallUtil->validationErrors);
$this->response->statusCode(400);
$this->set('errors', [
__d('net_commons', 'Failed on validation errors. Please check the input data.')
]);
CakeLog::info('[ValidationErrors] ' . $this->request->here());
if (Configure::read('debug')) {
CakeLog::info(var_export($this->InstallUtil->validationErrors, true));
}
return;
}
if (! $this->InstallUtil->createDB($this->request->data)) {
$this->response->statusCode(400);
CakeLog::info('Failed to create database.');
$this->set('errors', [__d('install', 'Failed to create database.')]);
return;
}
// Install migrations
$plugins = array_unique(array_merge(
App::objects('plugins'),
array_map('basename', glob(ROOT . DS . 'app' . DS . 'Plugin' . DS . '*', GLOB_ONLYDIR))
));
if (!$this->InstallUtil->installMigrations('master', $plugins)) {
$this->response->statusCode(400);
CakeLog::error('Failed to install migrations');
$this->set('errors', [__d('install', 'Failed to install migrations.')]);
return;
}
//Webrootへのコピー処理
if (! $this->InstallUtil->installWebrootCopy()) {
$this->response->statusCode(400);
CakeLog::error('Failed to install webroot copies css,js,img');
$this->set('errors', [__d('install', 'Failed to install webroot copies css,js,img')]);
}
$this->redirect(array(
'action' => 'init_admin_user',
'?' => ['language' => Configure::read('Config.language')]
));
}
}
/**
* ステップ 4
* 管理者アカウントの登録、
*
* @return void
*/
public function init_admin_user() {
$this->set('pageTitle', __d('install', 'Create an Administrator'));
if ($this->request->is('post')) {
if (! $this->InstallUtil->saveAdminUser($this->request->data)) {
$this->Session->setFlash(
__d('install', 'The user could not be saved. Please try again.')
);
return;
}
$this->redirect(array(
'action' => 'init_site_setting',
'?' => ['language' => Configure::read('Config.language')]
));
}
}
/**
* ステップ 5
* サイト設定
*
* @return void
*/
public function init_site_setting() {
App::uses('M17nHelper', 'M17n.View/Helper');
$this->set('pageTitle', __d('install', 'Site Setting'));
$this->set('errors', array());
$this->Language = ClassRegistry::init('M17n.Language');
$languages = $this->Language->find('list', array(
'fields' => array('code', 'is_active'),
'recursive' => -1,
));
$activeLangs = array();
$defaultLangs = array_intersect_key(M17nHelper::$languages, $languages);
foreach ($defaultLangs as $code => $value) {
if ($languages[$code]) {
$activeLangs[] = $code;
}
$defaultLangs[$code] = __d('m17n', $value);
}
$this->set('activeLangs', $activeLangs);
$this->set('defaultLangs', $defaultLangs);
if ($this->request->is('post')) {
if (! $this->InstallUtil->saveSiteSetting($this->request->data)) {
$this->set('validationErrors', $this->InstallUtil->validationErrors);
$this->set('errors', [
__d('net_commons', 'Failed on validation errors. Please check the input data.')
]);
} else {
$this->redirect(array(
'action' => 'save_init_data',
'?' => ['language' => Configure::read('Config.language')]
));
}
return;
}
}
/**
* ステップ 6
* 初期データの登録
*
* @return void
*/
public function save_init_data() {
if (! $this->InstallUtil->saveInitData()) {
$this->set('errors', [
__d('net_commons', 'Internal Error.')
]);
$this->redirect(array(
'action' => 'init_site_setting',
'?' => ['language' => Configure::read('Config.language')]
));
} else {
$this->redirect(array(
'action' => 'finish',
'?' => ['language' => Configure::read('Config.language')]
));
}
}
/**
* ステップ 7
* インストール終了
*
* @return void
*/
public function finish() {
$this->set('pageTitle', __d('install', 'Installed'));
if (file_exists(APP . 'VERSION')) {
$ncCache = new NetCommonsCache('version', false, 'netcommons_core');
$ncCache->write(trim(file_get_contents(APP . 'VERSION')));
}
Configure::write('NetCommons.installed', true);
/* Configure::write('NetCommons.installed', false); */
$this->InstallUtil->saveAppConf();
}
/**
* Keep connection alive
*
* @author Jun Nishikawa <topaz2@m0n0m0n0.com>
* @return void
*/
public function ping() {
$this->set('result', array('message' => 'OK'));
$this->set('_serialize', array('result'));
}
}