OSDN Git Service

基本のビューキャッシュ導入
[trpgtools-onweb/cake-frame.git] / app / controllers / app_controller.php
1 <?php
2 /**
3  * 独自App Contoroller
4  */
5
6 App::import('Vendor', 'pear_ini');  
7 App::import('Vendor', 'Net_UserAgent_Mobile', array('file' => 'Net' . DS . 'UserAgent' . DS . 'Mobile.php'));
8 /**
9  * PC/携帯共通
10  */
11 // 携帯チェック
12 $this->agent = &Net_UserAgent_Mobile::factory();
13 if (!$this->agent->isNonMobile()) {
14         Configure::write('mobileUserAgent', true);
15 }
16
17 class AppController extends Controller
18 {
19         var $user = array('User' => array(
20                 'id' => 0,
21                 'group_id' => 0,
22         ));
23         var $user_id = 0;
24         var $isAdministrator = false;
25         var $isAdmin = false;
26         var $isMobile = false;
27
28         var $site_configs = array();
29
30         var $components = array(
31                 'AuthPlus',
32                 'Acl',
33                 'Cakeplus.HtmlEscape',
34                 'Token',
35                 'Crypt',
36                 'DebugKit.Toolbar',
37                 'Benchmark'
38         );
39
40         var $helpers = array(
41                 'Html',
42                 'Form',
43                 'Javascript',
44                 'Text',
45                 'Time',
46                 'Cache',
47                 'Settings',
48                 'Media.Medium',
49                 'Media.Upfile',
50                 'Token'
51         );
52
53         /* Characters抽出条件 */
54         var $conditions = array(
55                 'Character.public_flag' => 'public',
56                 'Character.deleted' => 0
57         );
58         var $fields = array(
59                 'Character.id',
60                 'Character.system_id',
61                 'Character.user_id',
62                 'Character.name',
63                 'Character.main_picture',
64                 'Character.sort_order',
65                 'Character.status',
66                 'Character.public_flag',
67                 'Character.modified',
68         );
69         var $contain = array();
70         var $recursive = -1;
71         var $order = array(
72                 'Character.modified' => 'DESC',
73         );
74         var $paginate = array(
75                 'Character' => array(
76                         'limit' => 20,
77                         'conditions' => array(
78                                 'Character.public_flag' => 'public',
79                                 'Character.deleted' => 0
80                         ),
81                         'recursive' => -1,
82                         'order' => array(
83                                 'Character.modified' => 'DESC',
84                         )
85                 ),
86         );
87
88         /* ACL */
89         // 追加アクション用 crudMap
90         var $actionMapPlus = array();
91
92         // POSTのTokenチェックをしないアクション
93         var $disableTokenActions = array();
94
95         function __construct() {
96                 if (Configure::read('mobileUserAgent')) {
97                         Configure::write('Session.save', 'sessino_m_custom');
98                 }
99
100                 parent::__construct();
101         }
102
103         function beforeFilter()
104         {
105                 $this->Benchmark->report($this->params['controller']. '/'. $this->action . ':' .' beforeFilterStart');
106
107                 parent::beforeFilter();
108
109                 $this->user['User']['name'] = __('Guest', true);
110
111                 // ブラウザキャッシュ破棄
112                 $this->disableCache();
113
114                 if (isset($this->params['prefix'])) {
115                         if ($this->params['prefix'] == Configure::read('Routing.admin')) {
116                                 $this->isAdmin = true;
117                                 $this->layout = 'admin_'.$this->layout;
118                         } elseif ($this->params['prefix'] == 'm') {
119                                 $this->isMobile = true;
120                         }
121                 }
122                 $this->set('admin', $this->isAdmin);
123
124                 // サイト設定
125                 $this->site_configs = CorePlus::set_db_settings();
126                 $this->set('site_configs', $this->site_configs);
127
128                 if ($this->AuthPlus) {
129                         // ACL関連
130                         $this->AuthPlus->actionPath = 'controllers/';
131                         $this->AuthPlus->authorize = 'crud';
132                         // 認証済みユーザ情報のセット
133                         $this->user_id = $this->AuthPlus->user('id');
134                         $user = $this->getUser($this->user_id);
135                         if (!empty($user)) {
136                                 $this->user = $user;
137                         }
138
139                         if (in_array($this->AuthPlus->user('group_id'), array(1,2,3))) {
140                                 $this->isAdministrator = true;
141                         }
142
143                         // 対CRSF:Tokenチェック
144                         $this->Token->checkToken();
145
146                         // 認証アクション設定
147                         if (Configure::read('mobileUserAgent')) {
148                                 $this->AuthPlus->loginAction = '/m/users/login';
149                                 $this->AuthPlus->loginRedirect = '/m/users/index';
150                                 $this->AuthPlus->logoutRedirect = '/m/users/index';
151                         } else {
152                                 $this->AuthPlus->loginAction = '/users/login';
153                                 $this->AuthPlus->loginRedirect = '/users/index';
154                                 $this->AuthPlus->logoutRedirect = '/users/index';
155                         }
156                         if ($this->isAdmin) {
157                                 $this->AuthPlus->loginRedirect = '/admin/users/index';
158                         }
159                 }
160                 $this->set('user', $this->user);
161                 $this->set('isAdministrator', $this->isAdministrator);
162         }
163
164         function beforeRender()
165         {
166                 $this->Benchmark->report($this->params['controller']. '/'. $this->action . ':' . ' beforeRenderStart');
167                 parent::beforeRender();
168         }
169
170         function afterFilter()
171         {
172                 $this->Benchmark->report($this->params['controller']. '/'. $this->action . ':' . ' afterFilterStart');
173                 parent::afterFilter();
174         }
175
176         /* 共通関数 */
177         /* public_flag設定をview用にセット */
178         function set_public_flag4view()
179         {
180                 $model_public_flags = $this->get_public_flag();
181
182                 $this->set('public_flags', $model_public_flags);
183         }
184         function get_public_flag()
185         {
186                 return $this->{$this->modelClass}->public_flag;
187         }
188         function check_public_flag($data, $key = null)
189         {
190                 return CorePlus::check_public_flag($data, $key);
191         }
192         function check_public_flag2($data, $key = null)
193         {
194                 return CorePlus::check_public_flag2($data, $key);
195         }
196
197         /* status設定をview用にセット */
198         function set_status4view()
199         {
200                 $model_status = $this->get_status();
201
202                 $this->set('status', $model_status);
203         }
204         function get_status()
205         {
206                 if (isset($this->Character)) {
207                         return $this->Character->status;
208                 } elseif (isset($this->CharacterProfileArchive)) {
209                         return $this->CharacterProfileArchive->Character->status;
210                 } else {
211                         return array();
212                 }
213         }
214
215         /* isOwner */
216         function isOwner($data, $user_id)
217         {
218                 return CorePlus::isOwner($data, $user_id);
219         }
220
221         /* ユーザ情報取得 */
222         function getUser($id, $isAdmin=false, $isDelete=false) {
223                 if (!$id) {
224                         return array();
225                 }
226
227                 if (!isset($this->User)) {
228                         $this->User = CorePlus::set_model('User');
229                 }
230
231                 $conditions = array(
232                         'User.id' => $id,
233                 );
234                 if ($isAdmin === true) {
235                         $conditions['User.group_id'] = array(
236                                 Configure::read('Group.admin'),
237                                 Configure::read('Group.subadmin'),
238                                 Configure::read('Group.watcher'),
239                                 Configure::read('Group.member'),
240                                 Configure::read('Group.locked'),
241                                 Configure::read('Group.pre'),
242                         );
243                 } else {
244                         $conditions['User.group_id'] = array(
245                                 Configure::read('Group.admin'),
246                                 Configure::read('Group.subadmin'),
247                                 Configure::read('Group.watcher'),
248                                 Configure::read('Group.member'),
249                                 Configure::read('Group.locked'),
250                         );
251                 }
252
253                 $fields = array(
254                         'User.id',
255                         'User.group_id',
256                         'User.name',
257                         'User.pcmail',
258                         'User.mobile_mail',
259                         'User.modified',
260                 );
261                 if ($isAdmin === true) {
262                         $fields = array_merge($fields, array(
263                                 'User.username',
264                                 'User.useragent',
265                                 'User.host',
266                                 'User.created',
267                         ));
268                 }
269
270                 $contain = array(
271                          'Attachment',
272                 );
273                 if ($isAdmin === true || $isDelete === true) {
274                         $contain = array_merge($contain, array(
275                                 'Character',
276                         ));
277
278                         unset($this->User->hasMany['Character']['conditions']['Character.public_flag']);
279                         unset($this->User->hasMany['Character']['limit']);
280                 }
281
282                 if (empty($contain)) {
283                         $recursive = -1;
284                 } else {
285                         $recursive = Set::countDim($contain);
286                 }
287
288                 $user = $this->User->find('first', array(
289                         'conditions' => $conditions,
290                         'fields' => $fields,
291                         'recursive' => $recursive,
292                         'contain' => $contain,
293                 ));
294
295                 // アドレス復号化
296                 if (isset($user['User']['pcmail']) && !empty($user['User']['pcmail'])) {
297                         $user['User']['pcmail'] = $this->Crypt->decrypt($user['User']['pcmail']);
298                 }
299
300                 return $user;
301         }
302
303         /* デモモードチェック */
304         function _check_demo()
305         {
306                 if (Configure::read('Mode.Demo')) {
307                         if ($_SESSION['Auth']['User']['username'] == 'guest') {
308                                 $this->Session->setFlash(__('NO AVAILABLE NOW.', true));
309                                 $this->redirect(array('action'=>'index'));
310                         }
311                 }
312         }
313
314         /* システム情報取得 */
315         function _restore_html_system($data) {
316                 if (isset($data['System']['name'])  && !empty($data['System']['name'])) {
317                         $data['System']['name'] = $this->{$this->modelClass}->restore_html($data['System']['name'], false, false, false);
318                 }
319                 if (isset($data['System']['copyright'])  && !empty($data['System']['copyright'])) {
320                         $data['System']['copyright'] = $this->{$this->modelClass}->restore_html($data['System']['copyright'], false, false, false);
321                 }
322                 if (isset($data['System']['url'])  && !empty($data['System']['url'])) {
323                         $data['System']['url'] = $this->{$this->modelClass}->restore_html($data['System']['url'], false, false, false);
324                 }
325                 if (isset($data['System']['detail'])  && !empty($data['System']['detail'])) {
326                         $data['System']['detail'] = $this->{$this->modelClass}->restore_html($data['System']['detail'], false, false, false);
327                 }
328
329                 if (isset($data['Profile']) && !empty($data['Profile'])) {
330                         $data = $this->_restore_html_profile($data);
331                 }
332
333                 return $data;
334         }
335
336         function _get_systems($public_flag = 'public')
337         {
338                 $conditions = array();
339                 if (!empty($public_flag)) {
340                         $conditions['System.public_flag'] = $public_flag;
341                 }
342
343                 if (!isset($this->System)) {
344                         $this->System = CorePlus::set_model('System');
345                 }
346
347                 $systems = $this->System->find('list', array(
348                         'conditions' => $conditions,
349                         'recursive' => -1,
350                 ));
351                 if (!empty($systems)) {
352                         foreach ($systems as $k => $v) {
353                                 $systems[$k] = $this->{$this->modelClass}->restore_html($v);
354                         }
355                 }
356
357                 return $systems;
358         }
359
360         /* キャラクター一覧取得 */
361         /* 条件セット */
362         function _set_conditions_characters4user_id($user_id = null, $conditions = array(), $fields = array(), $contain = array(), $order = array())
363         {
364                 if (!empty($user_id)) {
365                         $conditions = array_merge((array)$conditions, 
366                                 array(
367                                         'Character.user_id' => $user_id,
368                                 )
369                         );
370
371                 }
372                 $conditions = array_merge($this->conditions, (array)$conditions);
373
374                 if ((isset($conditions['isAdmin']))|| (!empty($user_id) && ($user_id == $this->user_id))) {
375                         unset($conditions['Character.public_flag']);
376                         unset($conditions['isAdmin']);
377                 }
378
379                 $fields = array_merge($this->fields, (array)$fields);
380
381
382                 $contain = array_merge($this->contain, (array)$contain);
383                 if (empty($contain)) {
384                         $recursive = -1;
385                 } else {
386                         $recursive = Set::countDim($contain);
387                 }
388
389                 $order = array_merge($this->order, (array)$order);
390
391                 return array($conditions, $fields, $contain, $recursive, $order);
392         }
393
394         function _get_characters_list4user_id($user_id = null, $conditions = array(), $limit = 5, $fields = array(), $contain = array(), $order = array())
395         {
396                 list($conditions, $fields, $contain, $recursive, $order) = $this->_set_conditions_characters4user_id($user_id, $conditions, $fields, $contain, $order);
397
398                 if (!isset($this->Character)) {
399                         $this->Character = CorePlus::set_model('Character');
400                 }
401
402                 return $this->Character->find('all', array(
403                         'conditions' => $conditions,
404                         'limit' => $limit,
405                         'fields' => $fields,
406                         'recursive' => $recursive,
407                         'contain' => $contain,
408                         'order' => $order,
409                 ));
410         }
411
412         function _get_characters_page4user_id($user_id = null, $conditions = array(), $limit = 20, $fields = array(), $contain = array(), $order = array(), $page = 1)
413         {
414                 list($conditions, $fields, $contain, $recursive, $order) = $this->_set_conditions_characters4user_id($user_id, $conditions, $fields, $contain, $order);
415
416                 if (!isset($this->Character)) {
417                         $this->Character = CorePlus::set_model('Character');
418                 }
419
420                 $this->paginate['Character'] = array(
421                         'limit' => $limit,
422                         'conditions' => $conditions,
423                         'fields' => $fields,
424                         'contain' => $contain,
425                         'recursive' => $recursive,
426                         'order' => $order,
427                         'page' => $page,
428                 );
429
430                 return $this->paginate('Character');
431         }
432
433
434         /* キャラクター情報取得 */
435         function _get_character4character_id($character_id, $user_id = null, $public_flag = null, $is_deleted = 0)
436         {
437                 if (!isset($this->Character)) {
438                         $this->Character = CorePlus::set_model('Character');
439                 }
440
441                 $conditions = array(
442                         'Character.id' => $character_id,
443                         'Character.deleted' => $is_deleted,
444                 );
445                 if ($user_id) {
446                         $conditions['Character.user_id'] = $user_id;
447                 }
448
449                 unset($this->Character->hasMany['CharacterPicture']['conditions']['CharacterPicture.public_flag']);
450                 if ($public_flag) {
451                         $conditions['Character.public_flag'] = $public_flag;
452                         $this->Character->hasMany['CharacterPicture']['conditions']['CharacterPicture.public_flag'] = $public_flag;
453                 }
454
455                 $character = $this->Character->find('first', array(
456                         'conditions' => $conditions,
457                         'contain' => array(
458                                 'CharacterPicture' => array(
459                                         'Attachment',
460                                 ),
461                         ),
462                         'recursive' => 2,
463                 ));
464                 if (empty($character)) {
465                         $this->Session->setFlash(__('Invalid Character.', true));
466                         $this->redirect(array('controller' => 'characters', 'action'=>'index'));
467                 }
468                 return $character;
469         }
470
471         function check_character_picture_max($character_pictures_num)
472         {
473                 if (!$max_num = intval($this->site_configs['Character.maxPictures']['value'])) {
474                         return true;
475                 }
476
477                 if ($max_num <= $character_pictures_num) {
478                         return false;
479                 }
480
481                 return true;
482         }
483
484         /* Profile系 htmlRestore */
485         function _restore_html_profile($data) {
486                 if (isset($data['Profile']['name'])  && !empty($data['Profile']['name'])) {
487                         $data['Profile']['name'] = $this->{$this->modelClass}->restore_html($data['Profile']['name'], false, false, false);
488                 }
489
490                 if (isset($data['Profile'][0]['ProfileSelect'])) {
491                         foreach ($data['Profile'] as $k => $v) {
492                                 $data['Profile'][$k] = $this->__restore_html_profile($v);
493                         }
494                 } elseif($data['Profile']['ProfileSelect']) {
495                         $data['Profile'] = $this->__restore_html_profile($data['Profile']);
496                 }
497
498                 return $data;
499         }
500         function __restore_html_profile($data) {
501                 if (isset($data['ProfileSelect']) && !empty($data['ProfileSelect'])) {
502                         $data = $this->_restore_html_profile_select($data);
503                 }
504                 if (isset($data['ProfileTable']) && !empty($data['ProfileTable'])) {
505                         $data = $this->_restore_html_profile_table($data);
506                 }
507                 if (isset($data['ProfileTableStatic']) && !empty($data['ProfileTableStatic'])) {
508                         $data = $this->_restore_html_profile_table_static($data);
509                 }
510
511                 return $data;
512         }
513         function _restore_html_profile_select($data) {
514                 foreach ($data['ProfileSelect'] as $k => $v) {
515                         if (isset($data['ProfileSelect'][$k]['value'])  && !empty($data['ProfileSelect'][$k]['value'])) {
516                                 $data['ProfileSelect'][$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
517                         }
518                 }
519
520                 return $data;
521         }
522         function _restore_html_profile_table($data) {
523                 foreach ($data['ProfileTable'] as $k => $v) {
524                         if (isset($data['ProfileTable'][$k]['value'])  && !empty($data['ProfileTable'][$k]['value'])) {
525                                 $data['ProfileTable'][$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
526                         }
527                 }
528
529                 return $data;
530         }
531         function _restore_html_profile_table_static($data) {
532                 foreach ($data['ProfileTableStatic'] as $k => $v) {
533                         if (isset($data['ProfileTableStatic'][$k]['title'])  && !empty($data['ProfileTableStatic'][$k]['title'])) {
534                                 $data['ProfileTableStatic'][$k]['title'] = $this->{$this->modelClass}->restore_html($v['title'], false, false, false);
535                         }
536                 }
537
538                 return $data;
539         }
540
541 }
542
543 // AppControllerを携帯とPCで振り分け
544 if (Configure::read('mobileUserAgent')) {
545         class ModuleController extends MobileAppController
546         {
547         }
548 } else {
549         class ModuleController extends PcAppController
550         {
551         }
552 }
553
554 /**
555  * 携帯用AppController
556  */
557 class MobileAppController extends AppController
558 {
559         function beforeFilter()
560         {
561                 parent::beforeFilter();
562
563                 // 使用停止
564                 if (!Configure::read('Mode.Mobile')) {
565                         exit("Mobile View is inavailable.");
566                 }
567
568                 // PC用URLアクセスはエラー
569                 if (!isset($this->params["prefix"])) {
570                         // BaseURLのみ遷移
571                         if ($this->params["url"]["url"] == "/") {
572                                 $this->redirect("/m/");
573                         }
574                         $this->cakeError("errorPcView");
575                         $this->_stop();
576                 }
577                 // 入力データの文字コード変換
578                 @array_walk_recursive($this->data, "convertEncodeSjis2Utf8");
579         }
580
581         function beforeRender()
582         {
583                 parent::beforeRender();
584
585                 $this->layout = 'mobile_'.$this->layout;
586         }
587
588         function afterFilter()
589         {
590                 parent::afterFilter();
591
592                 // 全角文字の変換
593                 $this->output = mb_convert_kana($this->output, "rak", Configure::read('App.encoding'));
594                 // 出力文字コードの変換
595                 $this->output = mb_convert_encoding($this->output, "SJIS", Configure::read('App.encoding'));
596         }
597
598 }
599
600 /**
601  * PC用AppController
602  */
603 class PcAppController extends AppController 
604 {
605         function beforeFilter()
606         {
607                 parent::beforeFilter();
608
609                 // 携帯用アクションへのアクセスはPC用に変更
610                 if (isset($this->params["prefix"]) && $this->params["prefix"] == "mobile") {
611                         $pc_url = substr_replace($this->params["url"]["url"], "", 0, 2+strlen($this->params["controller"])+1);
612
613                         if (!$pc_url) {
614                                 $pc_url = substr_replace($this->params['action'], "", 0, strlen($this->params["prefix"])+1);
615                         }
616
617                         $this->redirect($pc_url);
618                 }
619         }
620
621         function beforeRender()
622         {
623                 parent::beforeRender();
624
625         }
626
627 }
628
629
630 /*
631  * データ処理用
632  */
633
634 /*
635  * 文字コード変換 SJISWin->UTF-8
636  */
637 function convertEncodeSjis2Utf8(&$str, n$key)
638 {
639         $str = mb_convert_encoding($str, 'UTF-8', 'SJIS-Win');
640 }
641