forked from NetCommons3/NetCommons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetCommonsAppController.php
More file actions
469 lines (415 loc) · 12.4 KB
/
NetCommonsAppController.php
File metadata and controls
469 lines (415 loc) · 12.4 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<?php
/**
* NetCommonsApp Controller
*
* @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('Controller', 'Controller');
App::uses('Utility', 'Inflector');
//App::uses('Current', 'NetCommons.Utility');
App::uses('NetCommonsUrl', 'NetCommons.Utility');
App::uses('PermissionComponent', 'NetCommons.Controller/Component');
App::uses('SiteSettingUtil', 'SiteManager.Utility');
App::uses('CurrentLib', 'NetCommons.Lib');
/**
* NetCommonsApp Controller
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\NetCommons\Controller
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class NetCommonsAppController extends Controller {
/**
* use layout
*
* @var string
*/
public $layout = 'NetCommons.default';
/**
* use theme
*
* @var string
*/
public $theme = 'default';
/**
* use components
*
* @var array
*/
public $components = array(
'NetCommons.AccessCtrl',
'Auth' => array(
'loginAction' => array(
'plugin' => 'auth',
'controller' => 'auth',
'action' => 'login',
),
'loginRedirect' => array(
'plugin' => 'pages',
'controller' => 'pages',
'action' => 'index',
),
'logoutRedirect' => array(
'plugin' => 'pages',
'controller' => 'pages',
'action' => 'index',
),
),
//'DebugKit.Toolbar',
'Flash',
'MobileDetect.MobileDetect',
'NetCommons.Asset',
'NetCommons.Permission' => array(
//アクセスの権限
'allow' => array(
'index' => null,
'view' => null,
),
),
'NetCommons.NetCommons',
'NetCommons.NetCommonsTime',
'RequestHandler',
'Session',
'Workflow.Workflow',
);
/**
* use model
*
* @var array
*/
public $uses = [
'M17n.Language',
];
/**
* use helpers
*
* @var array
*/
public $helpers = array(
'Html' => array(
'className' => 'NetCommons.SingletonViewBlockHtml'
),
'M17n.M17n',
'NetCommons.BackTo',
'NetCommons.Button',
'NetCommons.LinkButton',
'NetCommons.Date',
'NetCommons.MessageFlash',
'NetCommons.NetCommonsForm',
'NetCommons.NetCommonsHtml',
);
/**
* リクエストの許可メソッド
*
* @var array
*/
protected $_allowMethods = [
'get', 'post', 'put', 'delete'
];
/**
* CurrentLibライブラリ
*
* @var CurrentLib
*/
public $CurrentLib = null;
/**
* Constructor.
*
* @param CakeRequest $request Request object for this controller. Can be null for testing,
* but expect that features that use the request parameters will not work.
* @param CakeResponse $response Response object for this controller.
*/
public function __construct($request = null, $response = null) {
parent::__construct($request, $response);
//Cakeでは、$uses,$component,$helpersが直近の継承しているものしか読み込まれないため、
//継承している親クラス全てを読み込む
$parentClass = get_class($this);
while ($parentClass = get_parent_class($parentClass)) {
$this->_mergeVars(['uses', 'components', 'helpers'], $parentClass, false);
}
if (in_array('Html', $this->helpers, true) &&
!isset($this->helpers['Html']['className'])) {
$this->helpers['Html']['className'] = 'NetCommons.SingletonViewBlockHtml';
}
//サイトの設定データセット
if (Configure::read('NetCommons.installed')) {
SiteSettingUtil::initialize();
}
//DebugKitは、debugモードがONのときのみロードするように修正
if (Configure::read('debug') &&
!in_array('DebugKit.Toolbar', $this->components, true) &&
substr(get_class($this), 0, 5) !== 'Mock_') {
$this->components[] = 'DebugKit.Toolbar';
}
}
/**
* 事前準備
*
* @return void
*/
private function __prepare() {
if (Current::read('Block') &&
! $this->Components->loaded('NetCommons.Permission')) {
$this->Components->load('NetCommons.Permission');
}
//現在のテーマを取得
$theme = $this->Asset->getSiteTheme($this);
if ($theme) {
$this->theme = $theme;
}
if ($this->RequestHandler->accepts('json')) {
$this->viewClass = 'Json';
$this->layout = false;
}
if (in_array($this->params['action'], ['emptyRender', 'throwBadRequest', 'emptyFrame'])) {
$this->params['pass'] = array();
}
//$this->params['named']の値がスカラー値以外なら除去する
if (isset($this->request->params['named'])) {
$named = [];
foreach ($this->request->params['named'] as $key => $value) {
if (is_scalar($value)) {
$named[$key] = (string)$value;
}
}
$this->request->params['named'] = $named;
}
if (empty($this->request->params['requested'])) {
//非推奨ブラウザチェック
/** @var \DeprecatedBrowserComponent $DeprecatedBrowser */
$DeprecatedBrowser = $this->Components->load('NetCommons.DeprecatedBrowser');
$DeprecatedBrowser->initialize($this);
if ($DeprecatedBrowser->isDeprecatedBrowser()) {
$DeprecatedBrowser->setFlashNotification();
}
}
}
/**
* beforeFilter
*
* @return void
*/
public function beforeFilter() {
parent::beforeFilter();
if (empty($this->request->params['requested'])) {
$this->request->allowMethod($this->_allowMethods);
}
Security::setHash('sha512');
//言語のセット
$this->_setLanguage();
//if (empty($this->request->params['requested'])) {
// $indent = '';
//} else {
// $indent = ' ';
//}
//$startTime = microtime(true);
$this->__loadCurrentLib();
//$debug = CurrentLib::$current;
//unset($debug['PluginsRoom']);
//ksort($debug);
//CakeLog::debug($indent . __METHOD__ . '(' . __LINE__ . ') ' . var_export($debug, true));
//$debug = NcPermission::$permission;
//ksort($debug);
//CakeLog::debug($indent . __METHOD__ . '(' . __LINE__ . ') ' . var_export($debug, true));
//$endTime = microtime(true);
//CakeLog::debug($indent . __METHOD__ . '(' . __LINE__ . ') Current::initialize()');
//CakeLog::debug($indent . var_export(($endTime - $startTime), true));
if (! $this->AccessCtrl->allowAccess()) {
return;
}
$this->Auth->allow('index', 'view', 'emptyRender', 'download', 'throwBadRequest', 'emptyFrame');
if ($this->Components->loaded('Security')) {
$this->Components->Security->csrfExpires = '+' .
SiteSettingUtil::read('Session.ini.[session.gc_maxlifetime]') .
' second';
}
$this->__prepare();
//モバイルかどうかの判定処理
if (Configure::read('isMobile') === null) {
Configure::write('isMobile', $this->MobileDetect->detect('isMobile'));
}
if ($this->params['plugin'] !== 'frames' &&
Current::read('Frame.id') && ! Current::read('FramePublicLanguage.is_public')) {
return $this->setAction('emptyFrame');
}
}
/**
* Returns the referring URL for this request.
*
* @param string $default Default URL to use if HTTP_REFERER cannot be read from headers
* @param bool $local If true, restrict referring URLs to local server
* @return string Referring URL
* @link https://book.cakephp.org/2.0/en/controllers.html#Controller::referer
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function referer($default = null, $local = true) {
return parent::referer($default, $local);
}
/**
* Called after the controller action is run and rendered.
*
* @return void
* @link http://book.cakephp.org/2.0/ja/controllers.html#request-life-cycle-callbacks
*/
public function afterFilter() {
// CakeErrorControllerだったらCurrentLib::terminate実行しない
if ($this instanceof CakeErrorController) {
return parent::afterFilter();
}
//カレントデータセット
if (Configure::read('NetCommons.installed')) {
//@codeCoverageIgnoreStart
if (empty($this->CurrentLib)) {
$this->CurrentLib = CurrentLib::getInstance();
}
//@codeCoverageIgnoreEnd
$this->CurrentLib->terminate($this);
}
parent::afterFilter();
}
/**
* リクエストもしくはSessionから言語をセットする。
*
* @return void
*/
protected function _setLanguage() {
if (isset($this->request->query['lang']) &&
! array_key_exists('search', $this->request->query)) {
Configure::write('Config.language', $this->request->query['lang']);
$this->Session->write('Config.language', $this->request->query['lang']);
} elseif ($this->Session->check('Config.language')) {
Configure::write('Config.language', $this->Session->read('Config.language'));
}
//多言語の切り替えボックス
$languages = $this->Language->getLanguages();
$this->set('switchLanguages', $languages);
$this->set('hasSwitchLang', count($languages) > 1);
}
/**
* CurrentLibをロードする
*
* @return void
*/
private function __loadCurrentLib() {
if (Configure::read('NetCommons.installed')) {
//カレントデータセット UnitTestでMockに差し替えられるようにメンバ変数としておく
//@codeCoverageIgnoreStart
if (empty($this->CurrentLib)) {
$this->CurrentLib = CurrentLib::getInstance();
}
//@codeCoverageIgnoreEnd
$this->CurrentLib->initialize($this);
}
}
/**
* beforeRender
*
* @return void
*/
public function beforeRender() {
//theme css指定
$this->set('bootstrapMinCss', $this->Asset->isThemeBootstrapMinCss($this));
$controller = Inflector::camelize($this->params['controller']);
$pluginPath = Hash::get(App::path('View', Inflector::camelize($this->params['plugin'])), '0');
$path = $pluginPath . $controller . DS . 'json' . DS . $this->view . '.ctp';
if ($this->viewClass === 'Json' &&
! isset($this->viewVars['_serialize']) && ! file_exists($path)) {
$this->NetCommons->renderJson();
}
}
/**
* The beforeRedirect method is invoked when the controller's redirect method is called but before any
* further action.
*
* If this method returns false the controller will not continue on to redirect the request.
* The $url, $status and $exit variables have same meaning as for the controller's method. You can also
* return a string which will be interpreted as the URL to redirect to or return associative array with
* key 'url' and optionally 'status' and 'exit'.
*
* @param string|array $url A string or array-based URL pointing to another location within the app,
* or an absolute URL
* @param int $status Optional HTTP status code (eg: 404)
* @param bool $exit If true, exit() will be called after the redirect
* @return mixed
* false to stop redirection event,
* string controllers a new redirection URL or
* array with the keys url, status and exit to be used by the redirect method.
* @throws Exception
* @link http://book.cakephp.org/2.0/en/controllers.html#request-life-cycle-callbacks
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function beforeRedirect($url, $status = null, $exit = true) {
if ($url === null && $status >= 400) {
//Auth->allowによるエラーにメッセージが含まれない
$error = $this->response->httpCodes($status);
throw new Exception(__d('net_commons', $error[$status]), $status);
}
return parent::beforeRedirect($url, $status, $exit);
}
/**
* camelizeKeyRecursive
*
* @param array $orig data to camelize
* @return array camelized data
*/
public static function camelizeKeyRecursive($orig) {
$new = [];
$callback = ['Inflector', 'variable'];
foreach ($orig as $key => $value) {
if ($key === 'validationErrors') {
$new[$key] = $value;
} elseif (is_array($value)) {
$new[call_user_func($callback, $key)] = self::camelizeKeyRecursive($value);
} else {
$new[call_user_func($callback, $key)] = $value;
}
}
return $new;
}
/**
* throw bad request
*
* @param string|null $message メッセージ
* @return void
* @throws BadRequestException
*/
public function throwBadRequest($message = null) {
if (! $message) {
$message = __d('net_commons', 'Bad Request');
}
if ($this->request->is('ajax')) {
$this->NetCommons->setFlashNotification(__d('net_commons', 'Bad Request'), array(
'class' => 'danger',
'name' => 'Bad Request',
'message' => $message,
'interval' => NetCommonsComponent::ALERT_VALIDATE_ERROR_INTERVAL,
'error' => $message
), 400);
} else {
throw new BadRequestException($message);
}
}
/**
* Empty render
*
* @return void
*/
public function emptyRender() {
$this->autoRender = false;
}
/**
* Empty render
*
* @return void
*/
public function emptyFrame() {
if (Current::isSettingMode() || $this->layout === 'NetCommons.setting') {
$this->view = 'Frames.Frames/emptyRender';
} else {
$this->autoRender = false;
}
}
}