forked from baserproject/basercms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baser_app_controller.php
1472 lines (1302 loc) · 38.6 KB
/
baser_app_controller.php
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* SVN FILE: $Id$ */
/**
* Controller 拡張クラス
*
* PHP versions 5
*
* baserCMS : Based Website Development Project <http://basercms.net>
* Copyright 2008 - 2013, baserCMS Users Community <http://sites.google.com/site/baserusers/>
*
* @copyright Copyright 2008 - 2013, baserCMS Users Community
* @link http://basercms.net baserCMS Project
* @package baser.controllers
* @since baserCMS v 0.1.0
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://basercms.net/license/index.html
*/
/**
* Include files
*/
App::import('View', 'AppView');
App::import('Component','BcAuthConfigure');
//App::import('Component', 'Emoji');
/**
* Controller 拡張クラス
*
* @package baser.controllers
*/
class BaserAppController extends Controller {
/**
* view
*
* @var string
*/
var $view = 'App';
/**
* ページタイトル
*
* @var string
* @access public
*/
var $pageTitle = '';
/**
* ヘルパー
*
* @var mixed
* @access public
*/
// TODO 見直し
var $helpers = array(
'Session', 'BcPluginHook', BC_HTML_HELPER, BC_HTML_HELPER, 'Form', BC_FORM_HELPER,
'Javascript', BC_BASER_HELPER, BC_XML_HELPER, BC_ARRAY_HELPER, BC_BASER_ADMIN_HELPER
);
/**
* レイアウト
*
* @var string
* @access public
*/
var $layout = 'default';
/**
* モデル
*
* @var mixed
* @access protected
* TODO メニュー管理を除外後、GlobalMenuを除外する
*/
var $uses = array('GlobalMenu', 'Favorite');
/**
* コンポーネント
*
* @var array
* @access public
*/
var $components = array('BcPluginHook', 'RequestHandler', 'Security', 'Session', 'BcManager');
/**
* サブディレクトリ
*
* @var string
* @access public
*/
var $subDir = null;
/**
* サブメニューエレメント
*
* @var string
* @access public
*/
var $subMenuElements = '';
/**
* パンくずナビ
*
* @var array
* @access public
*/
var $crumbs = array();
/**
* 検索ボックス
*
* @var string
* @access public
*/
var $search = '';
/**
* ヘルプ
*
* @var string
* @access public
*/
var $help = '';
/**
* ページ説明文
*
* @var string
* @access public
*/
var $siteDescription = '';
/**
* コンテンツタイトル
*
* @var string
* @access public
*/
var $contentsTitle = '';
/**
* サイトコンフィグデータ
*
* @var array
* @access public
*/
var $siteConfigs = array();
/**
* プレビューフラグ
*
* @var boolean
* @access public
*/
var $preview = false;
/**
* 管理画面テーマ
*
* @var string
*/
var $adminTheme = null;
/**
* コンストラクタ
*
* @return void
* @access private
*/
function __construct() {
parent::__construct();
// テンプレートの拡張子
$this->ext = Configure::read('BcApp.templateExt');
if(BC_INSTALLED) {
// サイト基本設定の読み込み
$SiteConfig = ClassRegistry::init('SiteConfig','Model');
$this->siteConfigs = $SiteConfig->findExpanded();
if(empty($this->siteConfigs['version'])) {
$this->siteConfigs['version'] = $this->getBaserVersion();
$SiteConfig->saveKeyValue($this->siteConfigs);
}
} elseif($this->name != 'Installations') {
$this->redirect('/');
}
// TODO beforeFilterでも定義しているので整理する
if($this->name == 'CakeError') {
$this->uses = null;
$params = Router::parse('/'.Configure::read('BcRequest.pureUrl'));
$this->setTheme($params, true);
// モバイルのエラー用
if(Configure::read('BcRequest.agent')) {
$this->layoutPath = Configure::read('BcRequest.agentPrefix');
$agent = Configure::read('BcRequest.agent');
if($agent == 'mobile') {
$this->helpers[] = BC_MOBILE_HELPER;
} elseif($agent == 'smartphone') {
$this->helpers[] = BC_SMARTPHONE_HELPER;
}
}
}
if(Configure::read('BcRequest.agent') == 'mobile') {
if(!Configure::read('BcApp.mobile')) {
$this->notFound();
}
}
if(Configure::read('BcRequest.agent') == 'smartphone') {
if(!Configure::read('BcApp.smartphone')) {
$this->notFound();
}
}
/* 携帯用絵文字のモデルとコンポーネントを設定 */
// TODO 携帯をコンポーネントなどで判別し、携帯からのアクセスのみ実行させるようにする
// ※ コンストラクト時点で、$this->params['prefix']を利用できない為。
// TODO 2008/10/08 egashira
// beforeFilterに移動してみた。実際に携帯を使うサイトで使えるかどうか確認する
//$this->uses[] = 'EmojiData';
//$this->components[] = 'Emoji';
}
/**
* beforeFilter
*
* @return void
* @access public
*/
function beforeFilter() {
parent::beforeFilter();
if(!BC_INSTALLED || Configure::read('BcRequest.isUpdater')) {
return;
}
// テーマを設定
$this->setTheme($this->params);
if($this->params['controller'] != 'installations') {
// ===============================================================================
// テーマ内プラグインのテンプレートをテーマに梱包できるようにプラグインパスにテーマのパスを追加
// 実際には、プラグインの場合も下記パスがテンプレートの検索対象となっている為不要だが、
// ビューが存在しない場合に、プラグインテンプレートの正規のパスがエラーメッセージに
// 表示されてしまうので明示的に指定している。
// (例)
// [変更後] app/webroot/themed/demo/blog/news/index.php
// [正 規] app/plugins/blog/views/themed/demo/blog/news/index.php
// 但し、CakePHPの仕様としてはテーマ内にプラグインのテンプレートを梱包できる仕様となっていないので
// 将来的には、blog / mail / feed をプラグインではなくコアへのパッケージングを検討する必要あり。
// ※ AppView::_pathsも関連している
// ===============================================================================
$pluginThemePath = WWW_ROOT.'themed' . DS . $this->theme . DS;
$pluginPaths = Configure::read('pluginPaths');
if(!in_array($pluginThemePath, $pluginPaths)) {
Configure::write('pluginPaths', am(array($pluginThemePath), $pluginPaths));
}
}
// メンテナンス
if(!empty($this->siteConfigs['maintenance']) &&
($this->params['controller'] != 'maintenance' && @$this->params['url']['url'] != 'maintenance') &&
(!isset($this->params['prefix']) || $this->params['prefix'] != 'admin') &&
(Configure::read('debug') < 1 && empty($_SESSION['Auth']['User']))){
if(!empty($this->params['return']) && !empty($this->params['requested'])){
return;
}else{
$this->redirect('/' . Configure::read('BcRequest.agentAlias') . '/maintenance');
}
}
/* 認証設定 */
if ($this->name != 'Installations' && $this->name != 'Updaters' && isset($this->BcAuthConfigure)) {
$configs = Configure::read('BcAuthPrefix');
if (!empty($this->params['prefix']) && isset($configs[$this->params['prefix']])) {
$config = $configs[$this->params['prefix']];
if (count($configs) >= 2) {
if(getDbDriver() != 'bc_csv') {
$config['auth_prefix'] = $this->params['prefix'];
} else {
trigger_error('csv を利用している場合、プレフィックス認証は利用できません。config/baser.php の設定を見なおしてください。', E_USER_ERROR);
}
}
} elseif (isset($configs['front'])) {
$config = $configs['front'];
if(count($configs) >= 2) {
if(getDbDriver() != 'bc_csv') {
$config['auth_prefix'] = 'front';
} else {
trigger_error('csv を利用している場合、プレフィックス認証は利用できません。config/baser.php の設定を見なおしてください。', E_USER_ERROR);
}
}
} else {
$config = array();
}
$this->BcAuthConfigure->setting($config);
// ユーザーの存在チェック
$user = $this->BcAuth->user();
if($user) {
$userModel = key($user);
if($userModel) {
if (!empty($this->{$userModel}) && !$this->{$userModel}->find('count', array(
'conditions' => array($userModel . '.id' => $user[$userModel]['id'], $userModel . '.name' => $user[$userModel]['name']),
'recursive' => -1))) {
$this->Session->delete($this->BcAuth->sessionKey);
}
}
}
}
// 送信データの文字コードを内部エンコーディングに変換
$this->__convertEncodingHttpInput();
// $this->params['url'] の調整
// 環境によって?キーにamp;が付加されてしまうため
if(isset($this->params['url']) && is_array($this->params['url'])) {
foreach ($this->params['url'] as $key => $val ) {
if ( strpos( $key, 'amp;' ) === 0 ) {
$this->params['url'][substr( $key, 4 )] = $val;
unset( $this->params['url'][$key] );
}
}
}
/* レイアウトとビュー用サブディレクトリの設定 */
if(isset($this->params['prefix'])) {
$this->layoutPath = str_replace('_', '/', $this->params['prefix']);
$this->subDir = str_replace('_', '/', $this->params['prefix']);
$agent = Configure::read('BcRequest.agent');
if($agent == 'mobile') {
$this->helpers[] = BC_MOBILE_HELPER;
} elseif($agent == 'smartphone') {
$this->helpers[] = BC_SMARTPHONE_HELPER;
}
}
// Ajax
if(isset($this->RequestHandler) && $this->RequestHandler->isAjax() || !empty($this->params['url']['ajax'])) {
// キャッシュ対策
header("Cache-Control: no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
}
// 権限チェック
if(isset($this->BcAuth) && isset($this->params['prefix']) && !Configure::read('BcRequest.agent') && isset($this->params['action']) && empty($this->params['requested'])) {
if(!$this->BcAuth->allowedActions || !in_array($this->params['action'], $this->BcAuth->allowedActions)) {
$user = $this->BcAuth->user();
$Permission = ClassRegistry::init('Permission');
if($user) {
if(!$Permission->check($this->params['url']['url'], $user[key($user)]['user_group_id'])) {
$this->setMessage('指定されたページへのアクセスは許可されていません。', true);
$this->redirect($this->BcAuth->loginAction);
}
}
}
}
// SSLリダイレクト設定
if(Configure::read('BcApp.adminSsl') && !empty($this->params['admin'])) {
$adminSslMethods = array_filter(get_class_methods(get_class($this)), array($this, '_adminSslMethods'));
if($adminSslMethods) {
$this->Security->blackHoleCallback = '_sslFail';
$this->Security->requireSecure = $adminSslMethods;
}
} else {
$this->Security->enabled = false;
}
// 管理画面は送信データチェックを行わない(全て対応させるのは大変なので暫定処置)
if($this->Security->enabled && !empty($this->params['admin'])) {
$this->Security->validatePost = false;
}
}
/**
* テーマをセットする
*
* @param array $params
* @return void
* @access public
*/
function setTheme($params, $isError = false) {
// エラーの場合は強制的にフロントのテーマを設定する
if($isError) {
$this->theme = $this->siteConfigs['theme'];
return;
}
if(BC_INSTALLED && $params['controller'] != 'installations') {
if(empty($this->siteConfigs['admin_theme']) && Configure::read('BcApp.adminTheme')) {
$this->siteConfigs['admin_theme'] = Configure::read('BcApp.adminTheme');
}
$this->theme = $this->siteConfigs['theme'];
if(!empty($this->siteConfigs['admin_theme'])) {
$this->adminTheme = $this->siteConfigs['admin_theme'];
}
}
}
/**
* 管理画面用のメソッドを取得(コールバックメソッド)
*
* @param string $var
* @return boolean
* @access public
*/
function _adminSslMethods($var) {
return preg_match('/^admin_/', $var);
}
/**
* beforeRender
*
* @return void
* @access public
*/
function beforeRender() {
parent::beforeRender();
// テーマのヘルパーをセット
$this->setThemeHelpers();
// テンプレートの拡張子
// RSSの場合、RequestHandlerのstartupで強制的に拡張子を.ctpに切り替えられてしまう為、
// beforeRenderでも再設定する仕様にした
$this->ext = Configure::read('BcApp.templateExt');
// モバイルでは、mobileHelper::afterLayout をフックしてSJISへの変換が必要だが、
// エラーが発生した場合には、afterLayoutでは、エラー用のビューを持ったviewクラスを取得できない。
// 原因は、エラーが発生する前のcontrollerがviewを登録してしまっている為。
// エラー時のview登録にフックする場所はここしかないのでここでviewの登録を削除する
if($this->name == 'CakeError') {
ClassRegistry::removeObject('view');
}
$this->__updateFirstAccess();
$favoriteBoxOpened = false;
if(!empty($this->BcAuth) && !empty($this->params['url']['url']) && $this->params['url']['url'] != 'update') {
$user = $this->BcAuth->user();
if($user) {
if($this->Session->check('Baser.favorite_box_opened')) {
$favoriteBoxOpened = $this->Session->read('Baser.favorite_box_opened');
} else {
$favoriteBoxOpened = true;
}
}
}
$this->__loadDataToView();
$this->set('favoriteBoxOpened', $favoriteBoxOpened);
$this->set('isSSL', $this->RequestHandler->isSSL());
$this->set('safeModeOn', ini_get('safe_mode'));
$this->set('baserVersion',$this->getBaserVersion());
$this->set('siteConfig',$this->siteConfigs);
if(isset($this->siteConfigs['widget_area'])){
$this->set('widgetArea',$this->siteConfigs['widget_area']);
}
}
/**
* 初回アクセスメッセージ用のフラグを更新する
*/
function __updateFirstAccess() {
// 初回アクセスメッセージ表示設定
if(!empty($this->params['admin']) && !empty($this->siteConfigs['first_access'])) {
$data = array('SiteConfig' => array('first_access' => false));
$SiteConfig = ClassRegistry::init('SiteConfig','Model');
$SiteConfig->saveKeyValue($data);
}
}
/**
* SSLエラー処理
*
* SSL通信が必要なURLの際にSSLでない場合、
* SSLのURLにリダイレクトさせる
*
* @param string $err
* @return void
* @access protected
*/
function _sslFail($err) {
if ($err === 'secure') {
// 共用SSLの場合、設置URLがサブディレクトリになる場合があるので、$this->here は利用せずURLを生成する
$url = $this->params['url']['url'];
if(Configure::read('App.baseUrl')) {
$url = 'index.php/'.$url;
}
$url = Configure::read('BcEnv.sslUrl').$url;
$this->redirect($url);
exit();
}
}
/**
* NOT FOUNDページを出力する
*
* @return void
* @access public
*/
function notFound() {
return $this->cakeError('error404', array(array($this->here)));
}
/**
* 配列の文字コードを変換する
*
* @param array 変換前データ
* @param string 変換後の文字コード
* @return array 変換後データ
* @access protected
*/
function _autoConvertEncodingByArray($data, $outenc) {
foreach($data as $key=>$value) {
if(is_array($value)) {
$data[$key] = $this->_autoConvertEncodingByArray($value, $outenc);
} else {
if(isset($this->params['prefix']) && $this->params['prefix'] == 'mobile') {
$inenc = 'SJIS';
}else {
$inenc = mb_detect_encoding($value);
}
if ($inenc != $outenc) {
// 半角カナは一旦全角に変換する
$value = mb_convert_kana($value, "KV",$inenc);
$value = mb_convert_encoding($value, $outenc, $inenc);
$data[$key] = $value;
}
}
}
return $data;
}
/**
* View用のデータを読み込む。
* beforeRenderで呼び出される
*
* @return void
* @access private
*/
function __loadDataToView() {
$this->set('subMenuElements',$this->subMenuElements); // サブメニューエレメント
$this->set('crumbs',$this->crumbs); // パンくずなび
$this->set('search', $this->search);
$this->set('help', $this->help);
$this->set('preview', $this->preview);
/* ログインユーザー */
if (BC_INSTALLED && isset($_SESSION['Auth']['User']) && $this->name != 'Installations' && !Configure::read('BcRequest.isUpdater') && !Configure::read('BcRequest.isMaintenance') && $this->name != 'CakeError') {
$this->set('user',$_SESSION['Auth']['User']);
if(!empty($this->params['admin'])) {
$this->set('favorites', $this->Favorite->find('all', array('conditions' => array('Favorite.user_id' => $_SESSION['Auth']['User']['id']), 'order' => 'Favorite.sort', 'recursive' => -1)));
}
}
if(!empty($this->params['prefix'])) {
$currentPrefix = $this->params['prefix'];
} else {
$currentPrefix = 'front';
}
$this->set('currentPrefix', $currentPrefix);
$this->set('authPrefix', $this->Session->read('Auth.User.authPrefix'));
/* 携帯用絵文字データの読込 */
// TODO 実装するかどうか検討する
/*if(isset($this->params['prefix']) && $this->params['prefix'] == 'mobile' && !empty($this->EmojiData)) {
$emojiData = $this->EmojiData->find('all');
$this->set('emoji',$this->Emoji->EmojiData($emojiData));
}*/
}
/**
* baserCMSのバージョンを取得する
*
* @return string Baserバージョン
* @access public
*/
function getBaserVersion($plugin = '') {
return getVersion($plugin);
}
/**
* テーマのバージョン番号を取得する
*
* @param string $theme
* @return string
* @access public
*/
function getThemeVersion($theme) {
$path = WWW_ROOT.'themed'.DS.$theme.DS.'VERSION.txt';
if(!file_exists($path)) {
return false;
}
App::import('File');
$versionFile = new File($path);
$versionData = $versionFile->read();
$aryVersionData = split("\n",$versionData);
if(!empty($aryVersionData[0])) {
return $aryVersionData[0];
}else {
return false;
}
}
/**
* DBのバージョンを取得する
*
* @return string
* @access public
*/
function getSiteVersion($plugin = '') {
if(!$plugin) {
if(isset($this->siteConfigs['version'])) {
return preg_replace("/baserCMS ([0-9\.]+?[\sa-z]*)/is","$1",$this->siteConfigs['version']);
} else {
return '';
}
} else {
$Plugin = ClassRegistry::init('Plugin');
return $Plugin->field('version', array('name'=>$plugin));
}
}
/**
* CakePHPのバージョンを取得する
*
* @return string Baserバージョン
*/
function getCakeVersion() {
App::import('File');
$versionFile = new File(CAKE_CORE_INCLUDE_PATH.DS.CAKE.'VERSION.txt');
$versionData = $versionFile->read();
$lines = split("\n",$versionData);
$version = null;
foreach($lines as $line) {
if(preg_match('/^([0-9\.]+)$/', $line, $matches)) {
$version = $matches[1];
break;
}
}
if($version) {
return $version;
}else {
return false;
}
}
/**
* http経由で送信されたデータを変換する
* とりあえず、UTF-8で固定
*
* @return void
* @access private
*/
function __convertEncodingHttpInput() {
// TODO Cakeマニュアルに合わせた方がよいかも
if(isset($this->params['form'])) {
$this->params['form'] = $this->_autoConvertEncodingByArray($this->params['form'],'UTF-8');
}
if(isset($this->params['data'])) {
$this->params['data'] = $this->_autoConvertEncodingByArray($this->params['data'],'UTF-8');
}
}
/**
* メールを送信する
*
* @param string $to 送信先アドレス
* @param string $title タイトル
* @param mixed $body 本文
* @options array
* @return boolean 送信結果
* @access public
*/
function sendMail($to, $title = '', $body = '', $options = array()) {
$formalName = $email = '';
if(!empty($this->siteConfigs)) {
$formalName = $this->siteConfigs['formal_name'];
$email = $this->siteConfigs['email'];
if(strpos($email, ',') !== false) {
$email = explode(',', $email);
$email = $email[0];
}
}
if(!$formalName) {
$formalName = Configure::read('BcApp.title');
}
$options = array_merge(array(
'fromName' => $formalName,
'reply' => $email,
'cc' => '',
'bcc' => '',
'template' => 'default',
'from' => $email,
'agentTemplate' => true
), $options);
extract($options);
if(!isset($this->BcEmail)) {
return false;
}
if(strpos($to, ',') !== false) {
$_to = explode(',', $to);
$to = $_to[0];
if(count($_to) > 1) {
unset($_to[0]);
if($bcc) {
$bcc .= ',';
}
$bcc .= implode(',', $_to);
}
}
// メール基本設定
$this->_setMail();
if(!empty($options['filePaths'])) {
if(!is_array($options['filePaths'])) {
$this->BcEmail->filePaths = array($options['filePaths']);
} else {
$this->BcEmail->filePaths = $options['filePaths'];
}
}
if(!empty($options['attachments'])) {
if(!is_array($options['attachments'])) {
$this->BcEmail->attachments = array($options['attachments']);
} else {
$this->BcEmail->attachments = $options['attachments'];
}
}
// テンプレート
if($agentTemplate && Configure::read('BcRequest.agent')) {
$this->BcEmail->layoutPath = Configure::read('BcRequest.agentPrefix');
$this->BcEmail->subDir = Configure::read('BcRequest.agentPrefix');
} else {
$this->BcEmail->layoutPath = '';
$this->BcEmail->subDir = '';
}
$this->BcEmail->template = $template;
// データ
if(is_array($body)) {
$this->set($body);
}else {
$this->set('body', $body);
}
// 送信先アドレス
$this->BcEmail->to = $to;
// 件名
$this->BcEmail->subject = $title;
// 送信元・返信先
if($from) {
if(strpos($from, ',') !== false) {
$_from = explode(',', $from);
$from = $_from[0];
}
$this->BcEmail->from = $from;
$this->BcEmail->additionalParams = '-f'.$from;
$this->BcEmail->return = $from;
$this->BcEmail->replyTo = $reply;
} else {
$this->BcEmail->from = $to;
$this->BcEmail->additionalParams = '-f'.$to;
$this->BcEmail->return = $to;
$this->BcEmail->replyTo = $to;
}
// 送信元名
if($from && $fromName) {
$this->BcEmail->from = $fromName.' <'.$from.'>';
}
// CC
if($cc) {
if(strpos($cc, ',') !== false) {
$cc = explode(',', $cc);
}else{
$cc = array($cc);
}
$this->BcEmail->cc = $cc;
}
// BCC
if($bcc) {
if(strpos($bcc, ',') !== false) {
$bcc = explode(',', $bcc);
}else{
$bcc = array($bcc);
}
$this->BcEmail->bcc = $bcc;
}
return $this->BcEmail->send();
}
/**
* メールコンポーネントの初期設定
*
* @return boolean 設定結果
* @access protected
*/
function _setMail() {
if(!isset($this->BcEmail)) {
return false;
}
if(!empty($this->siteConfigs['mail_encode'])) {
$encode = $this->siteConfigs['mail_encode'];
} else {
$encode = 'ISO-2022-JP';
}
$this->BcEmail->reset();
$this->BcEmail->charset = $encode;
$this->BcEmail->sendAs = 'text'; // text or html or both
$this->BcEmail->lineLength=105; // TODO ちゃんとした数字にならない大きめの数字で設定する必要がある。
if(!empty($this->siteConfigs['smtp_host'])) {
$this->BcEmail->delivery = 'smtp'; // mail or smtp or debug
$this->BcEmail->smtpOptions = array(
'host' => $this->siteConfigs['smtp_host'],
'port' => ($this->siteConfigs['smtp_port'])? $this->siteConfigs['smtp_port'] : 25,
'timeout' => 30,
'username' => ($this->siteConfigs['smtp_user'])? $this->siteConfigs['smtp_user'] : null,
'password' => ($this->siteConfigs['smtp_password'])? $this->siteConfigs['smtp_password'] : nul
);
} else {
$this->BcEmail->delivery = "mail";
}
return true;
}
/**
* 画面の情報をセットする
*
* @param array $filterModels
* @param string $options
* @return void
* @access public
*/
function setViewConditions($filterModels = array(), $options = array()) {
$_options = array('type' => 'post', 'session' => true);
$options = am($_options, $options);
extract($options);
if($type == 'post' && $session == true) {
$this->_saveViewConditions($filterModels, $options);
} elseif ($type == 'get') {
$options['session'] = false;
}
$this->_loadViewConditions($filterModels, $options);
}
/**
* 画面の情報をセッションに保存する
*
* @param string $options
* @return void
* @access protected
*/
function _saveViewConditions($filterModels = array(), $options = array()) {
$_options = array('action' => '', 'group' => '');
$options = am($_options, $options);
extract($options);
if(!is_array($filterModels)){
$filterModels = array($filterModels);
}
if(!$action) {
$action = $this->action;
}
$contentsName = $this->name.Inflector::classify($action);
if($group) {
$contentsName .= ".".$group;
}
foreach($filterModels as $model) {
if(isset($this->data[$model])) {
$this->Session->write("{$contentsName}.filter.{$model}",$this->data[$model]);
}
}
if(!empty($this->params['named'])) {
$named = am($this->Session->read("{$contentsName}.named"), $this->params['named']);
$this->Session->write("{$contentsName}.named", $named);
}
}
/**
* 画面の情報をセッションから読み込む
*
* @param array $filterModels
* @param array|string $options
* @return void
* @access protected
*/
function _loadViewConditions($filterModels = array(), $options = array()) {
$_options = array('default'=>array(), 'action' => '', 'group' => '', 'type' => 'post' , 'session' => true);
$options = am($_options, $options);
$named = array();
$filter = array();
extract($options);
if(!is_array($filterModels)){
$model = $filterModels;
$filterModels = array($filterModels);
} else {
$model = $filterModels[0];
}
if(!$action) {
$action = $this->action;
}
$contentsName = $this->name.Inflector::classify($action);
if($group) {
$contentsName .= ".".$group;
}
if($type == 'post' && $session) {
foreach($filterModels as $model) {
if($this->Session->check("{$contentsName}.filter.{$model}")) {
$filter = $this->Session->read("{$contentsName}.filter.{$model}");
} elseif(!empty($default[$model])) {
$filter = $default[$model];
} else {
$filter = array();
}
$this->data[$model] = $filter;
}
$named = array();
if(!empty($default['named'])) {
$named = $default['named'];
}
if($this->Session->check("{$contentsName}.named")) {
$named = am($named, $this->Session->read("{$contentsName}.named"));
}
} elseif($type == 'get') {
if(!empty($this->params['url'])) {
$url = $this->params['url'];
unset($url['url']);
unset($url['ext']);
unset($url['x']);
unset($url['y']);
}
if(!empty($url)) {
$filter = $url;
} elseif(!empty($default[$model])) {
$filter = $default[$model];
}
$this->data[$model] = $filter;
if(!empty($default['named'])) {
$named = $default['named'];
}
$named['?'] = $filter;
}
$this->passedArgs += $named;
}
/**
* Select Text 用の条件を生成する
*
* @param string $fieldName
* @param mixed $values
* @param array $options