OSDN Git Service

ユーザURLと詳細設定
[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                 'Cakeplus.HtmlEscape',
33                 'Token',
34                 'Crypt',
35                 'DebugKit.Toolbar',
36                 'Benchmark'
37         );
38
39         var $helpers = array(
40                 'Html',
41                 'Form',
42                 'Javascript',
43                 'Text',
44                 'Time',
45                 'Settings',
46                 'Media.Medium',
47                 'Media.Upfile',
48                 'Cache',
49                 'Token'
50         );
51
52         /* Characters抽出条件 */
53         var $conditions = array(
54                 'Character.public_flag' => 'public',
55                 'Character.deleted' => 0
56         );
57         var $fields = array(
58                 'Character.id',
59                 'Character.system_id',
60                 'Character.user_id',
61                 'Character.name',
62                 'Character.main_picture',
63                 'Character.sort_order',
64                 'Character.status',
65                 'Character.public_flag',
66                 'Character.modified',
67         );
68         var $contain = array();
69         var $recursive = -1;
70         var $order = array(
71                 'Character.modified' => 'DESC',
72         );
73         var $paginate = array(
74                 'Character' => array(
75                         'limit' => 20,
76                         'conditions' => array(
77                                 'Character.public_flag' => 'public',
78                                 'Character.deleted' => 0
79                         ),
80                         'recursive' => -1,
81                         'order' => array(
82                                 'Character.modified' => 'DESC',
83                         )
84                 ),
85         );
86
87         var $cacheAction = array();
88
89         // POSTのTokenチェックをしないアクション
90         var $disableTokenActions = array();
91
92         function __construct() {
93                 if (Configure::read('mobileUserAgent')) {
94                         Configure::write('Session.save', 'sessino_m_custom');
95                 }
96
97                 parent::__construct();
98         }
99
100         function beforeFilter()
101         {
102                 $this->Benchmark->report($this->params['controller']. '/'. $this->action . ':' .' beforeFilterStart');
103
104                 parent::beforeFilter();
105
106                 $this->user['User']['name'] = __('Guest', true);
107
108                 // ブラウザキャッシュ破棄
109                 $this->disableCache();
110
111                 if (isset($this->params['prefix'])) {
112                         if ($this->params['prefix'] == Configure::read('Routing.admin')) {
113                                 $this->isAdmin = true;
114                                 $this->layout = 'admin_'.$this->layout;
115                         } elseif ($this->params['prefix'] == 'm') {
116                                 $this->isMobile = true;
117                         }
118                 }
119                 $this->set('admin', $this->isAdmin);
120
121                 // サイト設定
122                 $this->site_configs = CorePlus::set_db_settings();
123                 $this->set('site_configs', $this->site_configs);
124
125                 if ($this->AuthPlus) {
126 //                      // ACL関連
127 //                      $this->AuthPlus->actionPath = 'controllers/';
128 //                      $this->AuthPlus->authorize = 'crud';
129                         $this->AuthPlus->authorize = 'orig';
130                         // 認証済みユーザ情報のセット
131                         $this->user_id = $this->AuthPlus->user('id');
132                         $user = $this->getUser($this->user_id);
133                         if (!empty($user)) {
134                                 $this->user = $user;
135                         }
136
137                         if (in_array($this->AuthPlus->user('group_id'), array(1,2,3))) {
138                                 $this->isAdministrator = true;
139                         }
140
141                         // 対CRSF:Tokenチェック
142                         $this->Token->checkToken();
143
144                         // 認証アクション設定
145                         if (Configure::read('mobileUserAgent')) {
146                                 $this->AuthPlus->loginAction = '/m/users/login';
147                                 $this->AuthPlus->loginRedirect = '/m/users/index';
148                                 $this->AuthPlus->logoutRedirect = '/m/users/index';
149                         } else {
150                                 $this->AuthPlus->loginAction = '/users/login';
151                                 $this->AuthPlus->loginRedirect = '/users/index';
152                                 $this->AuthPlus->logoutRedirect = '/users/index';
153                         }
154                         if ($this->isAdmin) {
155                                 $this->AuthPlus->loginRedirect = '/admin/users/index';
156                         }
157                 }
158                 $this->set('user', $this->user);
159                 $this->set('isAdministrator', $this->isAdministrator);
160
161                 $this->pageTitle = $this->site_configs['Site.siteName']['value'];
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.url',
260                         'User.notes',
261                         'User.modified',
262                 );
263                 if ($isAdmin === true) {
264                         $fields = array_merge($fields, array(
265                                 'User.username',
266                                 'User.useragent',
267                                 'User.host',
268                                 'User.created',
269                         ));
270                 }
271
272                 $contain = array(
273                          'Attachment',
274                 );
275                 if ($isAdmin === true || $isDelete === true) {
276                         $contain = array_merge($contain, array(
277                                 'Character',
278                         ));
279
280                         unset($this->User->hasMany['Character']['conditions']['Character.public_flag']);
281                         unset($this->User->hasMany['Character']['limit']);
282                 }
283
284                 if (empty($contain)) {
285                         $recursive = -1;
286                 } else {
287                         $recursive = Set::countDim($contain);
288                 }
289
290                 $user = $this->User->find('first', array(
291                         'conditions' => $conditions,
292                         'fields' => $fields,
293                         'recursive' => $recursive,
294                         'contain' => $contain,
295                 ));
296
297                 return $user;
298         }
299
300         /* デモモードチェック */
301         function _check_demo()
302         {
303                 if (Configure::read('Mode.Demo')) {
304                         if ($_SESSION['Auth']['User']['username'] == 'guest') {
305                                 $this->Session->setFlash(__('NO AVAILABLE NOW.', true));
306                                 $this->redirect(array('action'=>'index'));
307                         }
308                 }
309         }
310
311         /* システム情報取得 */
312         function _restore_html_system($data) {
313                 if (isset($data['System']['name'])  && !empty($data['System']['name'])) {
314                         $data['System']['name'] = $this->{$this->modelClass}->restore_html($data['System']['name'], false, false, false);
315                 }
316                 if (isset($data['System']['copyright'])  && !empty($data['System']['copyright'])) {
317                         $data['System']['copyright'] = $this->{$this->modelClass}->restore_html($data['System']['copyright'], false, false, false);
318                 }
319                 if (isset($data['System']['url'])  && !empty($data['System']['url'])) {
320                         $data['System']['url'] = $this->{$this->modelClass}->restore_html($data['System']['url'], false, false, false);
321                 }
322                 if (isset($data['System']['detail'])  && !empty($data['System']['detail'])) {
323                         $data['System']['detail'] = $this->{$this->modelClass}->restore_html($data['System']['detail'], false, false, false);
324                 }
325
326                 if (isset($data['Profile']) && !empty($data['Profile'])) {
327                         $data = $this->_restore_html_profile($data);
328                 }
329
330                 return $data;
331         }
332
333         function _get_systems($public_flag = 'public')
334         {
335                 $conditions = array();
336                 if (!empty($public_flag)) {
337                         $conditions['System.public_flag'] = $public_flag;
338                 }
339
340                 if (!isset($this->System)) {
341                         $this->System = CorePlus::set_model('System');
342                 }
343
344                 $systems = $this->System->find('list', array(
345                         'conditions' => $conditions,
346                         'recursive' => -1,
347                 ));
348                 if (!empty($systems)) {
349                         foreach ($systems as $k => $v) {
350                                 $systems[$k] = $this->{$this->modelClass}->restore_html($v);
351                         }
352                 }
353
354                 return $systems;
355         }
356
357         /* キャラクター一覧取得 */
358         /* 条件セット */
359         function _set_conditions_characters4user_id($user_id = null, $conditions = array(), $fields = array(), $contain = array(), $order = array())
360         {
361                 if (!empty($user_id)) {
362                         $conditions = array_merge((array)$conditions, 
363                                 array(
364                                         'Character.user_id' => $user_id,
365                                 )
366                         );
367
368                 }
369                 $conditions = array_merge($this->conditions, (array)$conditions);
370
371                 if ((isset($conditions['isAdmin']))|| (!empty($user_id) && ($user_id == $this->user_id))) {
372                         unset($conditions['Character.public_flag']);
373                         unset($conditions['isAdmin']);
374                 }
375
376                 $fields = array_merge($this->fields, (array)$fields);
377
378
379                 $contain = array_merge($this->contain, (array)$contain);
380                 if (empty($contain)) {
381                         $recursive = -1;
382                 } else {
383                         $recursive = Set::countDim($contain);
384                 }
385
386                 $order = array_merge($this->order, (array)$order);
387
388                 return array($conditions, $fields, $contain, $recursive, $order);
389         }
390
391         function _get_characters_list4user_id($user_id = null, $conditions = array(), $limit = 5, $fields = array(), $contain = array(), $order = array())
392         {
393                 list($conditions, $fields, $contain, $recursive, $order) = $this->_set_conditions_characters4user_id($user_id, $conditions, $fields, $contain, $order);
394
395                 if (!isset($this->Character)) {
396                         $this->Character = CorePlus::set_model('Character');
397                 }
398
399                 return $this->Character->find('all', array(
400                         'conditions' => $conditions,
401                         'limit' => $limit,
402                         'fields' => $fields,
403                         'recursive' => $recursive,
404                         'contain' => $contain,
405                         'order' => $order,
406                 ));
407         }
408
409         function _get_characters_page4user_id($user_id = null, $conditions = array(), $limit = 20, $fields = array(), $contain = array(), $order = array(), $page = 1)
410         {
411                 list($conditions, $fields, $contain, $recursive, $order) = $this->_set_conditions_characters4user_id($user_id, $conditions, $fields, $contain, $order);
412
413                 if (!isset($this->Character)) {
414                         $this->Character = CorePlus::set_model('Character');
415                 }
416
417                 $this->paginate['Character'] = array(
418                         'limit' => $limit,
419                         'conditions' => $conditions,
420                         'fields' => $fields,
421                         'contain' => $contain,
422                         'recursive' => $recursive,
423                         'order' => $order,
424                         'page' => $page,
425                 );
426
427                 return $this->paginate('Character');
428         }
429
430
431         /* キャラクター情報取得 */
432         function _get_character4character_id($character_id, $user_id = null, $public_flag = null, $is_deleted = 0)
433         {
434                 if (!isset($this->Character)) {
435                         $this->Character = CorePlus::set_model('Character');
436                 }
437
438                 $conditions = array(
439                         'Character.id' => $character_id,
440                         'Character.deleted' => $is_deleted,
441                 );
442                 if ($user_id) {
443                         $conditions['Character.user_id'] = $user_id;
444                 }
445
446                 unset($this->Character->hasMany['CharacterPicture']['conditions']['CharacterPicture.public_flag']);
447                 if ($public_flag) {
448                         $conditions['Character.public_flag'] = $public_flag;
449                         $this->Character->hasMany['CharacterPicture']['conditions']['CharacterPicture.public_flag'] = $public_flag;
450                 }
451
452                 $character = $this->Character->find('first', array(
453                         'conditions' => $conditions,
454                         'contain' => array(
455                                 'CharacterPicture' => array(
456                                         'Attachment',
457                                 ),
458                         ),
459                         'recursive' => 2,
460                 ));
461                 if (empty($character)) {
462                         $this->Session->setFlash(__('Invalid Character.', true));
463                         $this->redirect(array('controller' => 'characters', 'action'=>'index'));
464                 }
465                 return $character;
466         }
467
468         function check_character_picture_max($character_pictures_num)
469         {
470                 if (!$max_num = intval($this->site_configs['Character.maxPictures']['value'])) {
471                         return true;
472                 }
473
474                 if ($max_num <= $character_pictures_num) {
475                         return false;
476                 }
477
478                 return true;
479         }
480
481         /* restore_html */
482         function _restore_html_user($data, $nl2br = false) {
483                 $data['name'] = $this->{$this->modelClass}->restore_html($data['name'], false, false, false);
484
485                 if (isset($data['notes']) && !empty($data['notes'])) {
486                         $data['notes'] = $this->{$this->modelClass}->restore_html($data['notes'], false, false, false);
487                         if ($nl2br) {
488                                 $data['notes'] = str_replace('<br />', "\n", $data['notes']);
489                         }
490                 }
491
492                 // アドレス復号化
493                 $data = $this->decrypt_mail($data);
494
495                 return $data;
496         }
497         // アドレス復号化
498         function decrypt_mail($data)
499         {
500                 if (isset($data['pcmail']) && !empty($data['pcmail'])) {
501                         $data['pcmail'] = $this->Crypt->decrypt($data['pcmail']);
502                 }
503                 return $data;
504         }
505
506         /* Profile系 htmlRestore */
507         function _restore_html_profile($data) {
508                 if (isset($data['Profile']['name'])  && !empty($data['Profile']['name'])) {
509                         $data['Profile']['name'] = $this->{$this->modelClass}->restore_html($data['Profile']['name'], false, false, false);
510                 }
511
512                 if (isset($data['Profile'][0]['ProfileSelect'])) {
513                         foreach ($data['Profile'] as $k => $v) {
514                                 $data['Profile'][$k] = $this->__restore_html_profile($v);
515                         }
516                         $data['Profile'] = $this->_restore_html_profiles($data['Profile']);
517
518                 } elseif($data['Profile']['ProfileSelect']) {
519                         $data['Profile'] = $this->__restore_html_profile($data['Profile']);
520                 }
521
522                 return $data;
523         }
524         function __restore_html_profile($data) {
525                 if (isset($data['ProfileSelect']) && !empty($data['ProfileSelect'])) {
526                         $data['ProfileSelect'] = $this->_restore_html_profile_select($data['ProfileSelect']);
527                 }
528                 if (isset($data['ProfileTable']) && !empty($data['ProfileTable'])) {
529                         $data['ProfileTable'] = $this->_restore_html_profile_table($data['ProfileTable']);
530                 }
531                 if (isset($data['ProfileTableStatic']) && !empty($data['ProfileTableStatic'])) {
532                         $data['ProfileTableStatic'] = $this->_restore_html_profile_table_static($data['ProfileTableStatic']);
533                 }
534
535                 return $data;
536         }
537         function _restore_html_profiles($data) {
538                 $sort_order = array();
539                 foreach ($data as $k => $v) {
540                         if (isset($v['value'])  && !empty($v['value'])) {
541                                 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
542                         }
543
544                         if (isset($v['sort_order'])  && !empty($v['sort_order'])) {
545                                 $sort_order[$k] = $v['sort_order'];
546                         } else {
547                                 $sort_order[$k] = 0;
548                         }
549                 }
550
551                 $data = $this->sort4sort_order($data, $sort_order);
552
553                 return $data;
554         }
555         function _restore_html_profile_select($data) {
556                 $sort_order = array();
557                 foreach ($data as $k => $v) {
558                         if (isset($v['value'])  && !empty($v['value'])) {
559                                 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
560                         }
561
562                         if (isset($v['sort_order'])  && !empty($v['sort_order'])) {
563                                 $sort_order[$k] = $v['sort_order'];
564                         } else {
565                                 $sort_order[$k] = 0;
566                         }
567                 }
568
569                 $data = $this->sort4sort_order($data, $sort_order);
570
571                 return $data;
572         }
573         function _restore_html_profile_table($data) {
574                 foreach ($data as $k => $v) {
575                         if (isset($v['value'])  && !empty($v['value'])) {
576                                 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
577                         }
578
579                         if (isset($v['sort_order'])  && !empty($v['sort_order'])) {
580                                 $sort_order[$k] = $v['sort_order'];
581                         } else {
582                                 $sort_order[$k] = 0;
583                         }
584                 }
585
586                 $data = $this->sort4sort_order($data, $sort_order);
587
588                 return $data;
589         }
590         function _restore_html_profile_table_static($data) {
591                 foreach ($data as $k => $v) {
592                         if (isset($v['title'])  && !empty($v['title'])) {
593                                 $data['ProfileTableStatic'][$k]['title'] = $this->{$this->modelClass}->restore_html($v['title'], false, false, false);
594                         }
595
596                         if (isset($v['sort_order'])  && !empty($v['sort_order'])) {
597                                 $sort_order[$k] = $v['sort_order'];
598                         } else {
599                                 $sort_order[$k] = 0;
600                         }
601                 }
602
603                 $data = $this->sort4sort_order($data, $sort_order);
604
605                 return $data;
606         }
607
608         function sort4sort_order($data, $sort_order)
609         {
610                 if (empty($data) || empty($sort_order)) {
611                         return $data;
612                 }
613                 array_multisort($sort_order, SORT_ASC, $data);
614
615                 return $data;
616         }
617
618 }
619
620 // AppControllerを携帯とPCで振り分け
621 if (Configure::read('mobileUserAgent')) {
622         class ModuleController extends MobileAppController
623         {
624         }
625 } else {
626         class ModuleController extends PcAppController
627         {
628         }
629 }
630
631 /**
632  * 携帯用AppController
633  */
634 class MobileAppController extends AppController
635 {
636         function beforeFilter()
637         {
638                 parent::beforeFilter();
639
640                 // 使用停止
641                 if (!Configure::read('Mode.Mobile')) {
642                         exit("Mobile View is inavailable.");
643                 }
644
645                 // PC用URLアクセスはエラー
646                 if (!isset($this->params["prefix"])) {
647                         // BaseURLのみ遷移
648                         if ($this->params["url"]["url"] == "/") {
649                                 $this->redirect("/m/");
650                         }
651                         $this->cakeError("errorPcView");
652                         $this->_stop();
653                 }
654                 // 入力データの文字コード変換
655                 @array_walk_recursive($this->data, "convertEncodeSjis2Utf8");
656         }
657
658         function beforeRender()
659         {
660                 parent::beforeRender();
661
662                 $this->layout = 'mobile_'.$this->layout;
663         }
664
665         function afterFilter()
666         {
667                 parent::afterFilter();
668
669                 // 全角文字の変換
670                 $this->output = mb_convert_kana($this->output, "rak", Configure::read('App.encoding'));
671                 // 出力文字コードの変換
672                 $this->output = mb_convert_encoding($this->output, "SJIS", Configure::read('App.encoding'));
673         }
674
675 }
676
677 /**
678  * PC用AppController
679  */
680 class PcAppController extends AppController 
681 {
682         function beforeFilter()
683         {
684                 parent::beforeFilter();
685
686                 // 携帯用アクションへのアクセスはPC用に変更
687                 if (isset($this->params["prefix"]) && $this->params["prefix"] == "mobile") {
688                         $pc_url = substr_replace($this->params["url"]["url"], "", 0, 2+strlen($this->params["controller"])+1);
689
690                         if (!$pc_url) {
691                                 $pc_url = substr_replace($this->params['action'], "", 0, strlen($this->params["prefix"])+1);
692                         }
693
694                         $this->redirect($pc_url);
695                 }
696         }
697
698         function beforeRender()
699         {
700                 parent::beforeRender();
701
702         }
703
704 }
705
706
707 /*
708  * データ処理用
709  */
710
711 /*
712  * 文字コード変換 SJISWin->UTF-8
713  */
714 function convertEncodeSjis2Utf8(&$str, $key)
715 {
716         $str = mb_convert_encoding($str, 'UTF-8', 'SJIS-Win');
717 }
718