/
MailController.php
572 lines (502 loc) · 17.2 KB
/
MailController.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
<?php
/* SVN FILE: $Id$ */
/**
* お問い合わせメールフォーム用コントローラー
*
* 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.plugins.mail.controller
* @since baserCMS v 0.1.0
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://basercms.net/license/index.html
*/
/**
* Include files
*/
/**
* お問い合わせメールフォーム用コントローラー
*
* @package baser.plugins.mail.controller
*/
class MailController extends MailAppController {
/**
* クラス名
*
* @var string
* @access public
*/
public $name = 'Mail';
/**
* モデル
*
* @var array
* @access public
*/
public $uses = array('Mail.Message', 'Mail.MailContent', 'Mail.MailField', 'Mail.MailConfig');
/**
* ヘルパー
*
* @var array
* @access public
*/
public $helpers = array(
'BcFreeze', 'BcArray', 'BcTime', 'Mail.Mailform', 'Mail.Maildata', 'Mail.Mailfield', 'Mail.Mail', 'Js'
);
/**
* Array of components a controller will use
*
* @var array
* @access public
*/
// PHP4の場合、メールフォームの部品が別エレメントになった場合、利用するヘルパが別インスタンスとなってしまう様子。
// そのためSecurityコンポーネントが利用できない
// 同じエレメント内で全てのフォーム部品を完結できればよいがその場合デザインの自由度が失われてしまう。
// var $components = array('Email','BcEmail','Security','BcCaptcha');
//
// 2013/03/14 ryuring
// baserCMS2系より必須要件をPHP5以上とした為、SecurityComponent を標準で設定する方針に変更
public $components = array('BcAuth', 'Cookie', 'BcAuthConfigure', 'Email', 'BcEmail', 'BcCaptcha', 'Security');
/**
* CSS
*
* @var array
* @access public
*/
public $css = array('mail/form');
/**
* ページタイトル
*
* @var string
* @access public
*/
public $pageTitle = 'お問い合わせ';
/**
* サブメニューエレメント
*
* @var array
* @access public
*/
public $subMenuElements = array();
/**
* データベースデータ
*
* @var array
* @access public
*/
public $dbDatas = null;
/**
* ぱんくずナビ
*
* @var array
* @access public
*/
public $crumbs = array();
/**
* beforeFilter.
*
* @return void
* @access public
*/
public function beforeFilter() {
/* 認証設定 */
$this->BcAuth->allow(
'index', 'mobile_index', 'smartphone_index', 'confirm', 'mobile_confirm', 'smartphone_confirm', 'submit', 'mobile_submit', 'smartphone_submit', 'captcha', 'smartphone_captcha'
);
parent::beforeFilter();
// バリデーション自動生成用にメールフォームIDを設定
if (!empty($this->contentId)) {
$id = $this->contentId;
} elseif (!empty($this->params['pass'][0]) && is_numeric($this->params['pass'][0])) {
$id = $this->params['pass'][0];
} else {
$id = 1;
}
$this->dbDatas['mailContent'] = $this->MailContent->find('first', array('conditions' => array("MailContent.id" => $id)));
$this->dbDatas['mailConfig'] = $this->MailConfig->find();
$this->Message->mailFields = $this->dbDatas['mailFields'] = $this->MailField->find('all', array('conditions' => array("mail_content_id" => $id), 'order' => 'MailField.sort'));
// ページタイトルをセット
$this->pageTitle = $this->dbDatas['mailContent']['MailContent']['title'];
// レイアウトをセット
$this->layout = $this->dbDatas['mailContent']['MailContent']['layout_template'];
if (empty($this->contentId)) {
// 配列のインデックスが無いためエラーとなるため修正
$this->contentId = isset($this->request->params['pass'][0]) ? $this->request->params['pass'][0] : null;
}
$this->subMenuElements = array('default');
// 2013/03/14 ryuring
// baserCMS2系より必須要件をPHP5以上とした為、SecurityComponent を標準で設定する方針に変更
if (Configure::read('debug') > 0) {
$this->Security->validatePost = false;
$this->Security->csrfCheck = false;
} else {
// PHP4でセキュリティコンポーネントがうまくいかなかったので利用停止
// 詳細はコンポーネント設定のコメントを参照
$this->Security->requireAuth('confirm', 'submit');
$this->Security->disabledFields = array_merge($this->Security->disabledFields, array(
'Message.mode', 'x', 'y'
));
// SSL設定
if ($this->dbDatas['mailContent']['MailContent']['ssl_on']) {
$this->Security->blackHoleCallback = '_sslFail';
$this->Security->requireSecure = am($this->Security->requireSecure, array('index', 'confirm', 'submit'));
}
}
// 複数のメールフォームに対応する為、プレフィックス付のCSVファイルに保存。
// ※ nameフィールドの名称を[message]以外にする
if ($this->dbDatas['mailContent']['MailContent']['name'] != 'message') {
$prefix = $this->dbDatas['mailContent']['MailContent']['name'] . "_";
$this->Message->setTablePrefix($prefix);
$this->Message->mailFields = $this->dbDatas['mailFields'];
}
}
/**
* beforeRender
*
* @return void
* @access public
*/
public function beforeRender() {
parent::beforeRender();
if ($this->dbDatas['mailContent']['MailContent']['widget_area']) {
$this->set('widgetArea', $this->dbDatas['mailContent']['MailContent']['widget_area']);
}
}
/**
* [PUBIC] フォームを表示する
*
* @param mixed mail_content_id
* @return void
* @access public
*/
public function index($id = null) {
if (!$this->MailContent->isPublish($this->dbDatas['mailContent']['MailContent']['status'], $this->dbDatas['mailContent']['MailContent']['publish_begin'], $this->dbDatas['mailContent']['MailContent']['publish_end'])) {
$this->render($this->dbDatas['mailContent']['MailContent']['form_template'] . DS . 'unpublish');
return;
}
if (!$this->dbDatas['mailContent']['MailContent']['status']) {
$this->notFound();
}
// 初期値を取得
if (!isset($this->request->data['Message'])) {
$this->request->data = $this->Message->getDefaultValue($this->request->params['named']);
} else {
$this->request->data['Message'] = $this->Message->sanitizeData($this->request->data['Message']);
}
$this->set('freezed', false);
if ($this->dbDatas['mailFields']) {
$this->set('mailFields', $this->dbDatas['mailFields']);
}
$user = $this->BcAuth->user();
if (!empty($user) && !Configure::read('BcRequest.agent')) {
$this->set('editLink', array('admin' => true, 'plugin' => 'mail', 'controller' => 'mail_contents', 'action' => 'edit', $this->dbDatas['mailContent']['MailContent']['id']));
}
$this->set('mailContent', $this->dbDatas['mailContent']);
$this->render($this->dbDatas['mailContent']['MailContent']['form_template'] . DS . 'index');
}
/**
* [MOBILE] フォームを表示する
*
* @param mixed mail_content_id
* @return void
* @access public
*/
public function mobile_index($id = null) {
$this->setAction('index', $id);
}
/**
* [SMARTPHONE] フォームを表示する
*
* @param mixed mail_content_id
* @return void
* @access public
*/
public function smartphone_index($id = null) {
$this->setAction('index', $id);
}
/**
* [PUBIC] データの確認画面を表示
*
* @param mixed mail_content_id
* @return void
* @access public
*/
public function confirm($id = null) {
if (!$this->MailContent->isPublish($this->dbDatas['mailContent']['MailContent']['status'], $this->dbDatas['mailContent']['MailContent']['publish_begin'], $this->dbDatas['mailContent']['MailContent']['publish_end'])) {
$this->render($this->dbDatas['mailContent']['MailContent']['form_template'] . DS . 'unpublish');
return;
}
if (!$this->dbDatas['mailContent']['MailContent']['status']) {
$this->notFound();
}
if (!$this->request->data) {
$this->redirect(array('action' => 'index', $id));
} else {
// 入力データを整形し、モデルに引き渡す
$this->request->data = $this->Message->create($this->Message->autoConvert($this->request->data));
// 画像認証を行う
if (Configure::read('BcRequest.agent') != 'mobile' && $this->dbDatas['mailContent']['MailContent']['auth_captcha']) {
$captchaResult = $this->BcCaptcha->check($this->request->data['Message']['auth_captcha']);
if (!$captchaResult) {
$this->Message->invalidate('auth_captcha');
}
}
// データの入力チェックを行う
if ($this->Message->validates()) {
$this->set('freezed', true);
} else {
$this->set('freezed', false);
$this->set('error', true);
$this->setMessage('【入力エラーです】<br />入力内容を確認して再度送信してください。', true);
}
$this->request->data['Message'] = $this->Message->sanitizeData($this->request->data['Message']);
}
if ($this->dbDatas['mailFields']) {
$this->set('mailFields', $this->dbDatas['mailFields']);
}
$this->set('mailContent', $this->dbDatas['mailContent']);
$this->render($this->dbDatas['mailContent']['MailContent']['form_template'] . DS . 'confirm');
}
/**
* [MOBILE] フォームを表示する
*
* @param mixed mail_content_id
* @return void
* @access public
*/
public function mobile_confirm($id = null) {
$this->setAction('confirm', $id);
}
/**
* [SMARTPHONE] フォームを表示する
*
* @param mixed mail_content_id
* @return void
* @access public
*/
public function smartphone_confirm($id = null) {
$this->setAction('confirm', $id);
}
/**
* [PUBIC] データ送信
*
* @param mixed mail_content_id
* @return void
* @access public
*/
public function submit($id = null) {
if (!$this->MailContent->isPublish($this->dbDatas['mailContent']['MailContent']['status'], $this->dbDatas['mailContent']['MailContent']['publish_begin'], $this->dbDatas['mailContent']['MailContent']['publish_end'])) {
$this->render($this->dbDatas['mailContent']['MailContent']['form_template'] . DS . 'unpublish');
return;
}
if (!$this->dbDatas['mailContent']['MailContent']['status']) {
$this->notFound();
}
if (!$this->request->data) {
$this->redirect(array('action' => 'index', $id));
} elseif (isset($this->request->data['Message']['mode']) && $this->request->data['Message']['mode'] == 'Back') {
$this->_back($id);
} else {
// 複数のメールフォームに対応する為、プレフィックス付のCSVファイルに保存。
// ※ nameフィールドの名称を[message]以外にする
if ($this->dbDatas['mailContent']['MailContent']['name'] != 'message') {
$prefix = $this->dbDatas['mailContent']['MailContent']['name'] . "_";
} else {
$prefix = "";
}
// 画像認証を行う
if (Configure::read('BcRequest.agent') != 'mobile' && $this->dbDatas['mailContent']['MailContent']['auth_captcha']) {
$captchaResult = $this->BcCaptcha->check($this->request->data['Message']['auth_captcha']);
if (!$captchaResult) {
$this->redirect(array('action' => 'index', $id));
} else {
unset($this->request->data['Message']['auth_captcha']);
}
}
$this->Message->create($this->request->data);
// データの入力チェックを行う
if ($this->Message->validates()) {
// validation OK
if ($this->Message->save(null, false)) {
/* * * Mail.beforeSendEmail ** */
$event = $this->dispatchEvent('beforeSendEmail', array(
'data' => $this->request->data
));
if ($event !== false) {
$this->request->data = $event->result === true ? $event->data['data'] : $event->result;
}
// メール送信
$this->_sendEmail();
/* * * Mail.afterSendEmail ** */
$this->dispatchEvent('afterSendEmail', array(
'data' => $this->request->data
));
} else {
$this->setMessage('【送信エラーです】<br />送信中にエラーが発生しました。しばらくたってから再度送信お願いします。', true);
$this->set('sendError', true);
}
$this->set('mailContent', $this->dbDatas['mailContent']);
$this->render($this->dbDatas['mailContent']['MailContent']['form_template'] . DS . 'submit');
// 入力検証エラー
} else {
$this->set('freezed', false);
$this->set('error', true);
$this->setMessage('【入力エラーです】<br />入力内容を確認して再度送信してください。', true);
$this->request->data['Message'] = $this->Message->sanitizeData($this->request->data['Message']);
$this->action = 'index'; //viewのボタンの表示の切り替えに必要なため変更
if ($this->dbDatas['mailFields']) {
$this->set('mailFields', $this->dbDatas['mailFields']);
}
$this->set('mailContent', $this->dbDatas['mailContent']);
$this->render($this->dbDatas['mailContent']['MailContent']['form_template'] . DS . 'index');
}
}
}
/**
* [private] 確認画面から戻る
*
* @param mixed mail_content_id
* @return void
* @access public
*/
public function _back($id) {
$this->set('freezed', false);
$this->set('error', false);
if ($this->dbDatas['mailFields']) {
$this->set('mailFields', $this->dbDatas['mailFields']);
}
//mailの重複チェックがある場合は、チェック用のデータを復帰
// ↓
// 2013/11/08 - gondoh mailヘッダインジェクション対策時に
// 確認画面にもhiddenタグ出力するよう変更したため削除
$this->action = 'index'; //viewのボタンの表示の切り替えに必要なため変更
$this->set('mailContent', $this->dbDatas['mailContent']);
$this->render($this->dbDatas['mailContent']['MailContent']['form_template'] . DS . 'index');
}
/**
* [MOBILE] 送信完了ページ
*
* @param mixed mail_content_id
* @return void
* @access public
*/
public function mobile_submit($id = null) {
$this->setAction('submit', $id);
}
/**
* [SMARTPHONE] 送信完了ページ
*
* @param mixed mail_content_id
* @return void
* @access public
*/
public function smartphone_submit($id = null) {
$this->setAction('submit', $id);
}
/**
* メール送信する
*
* @return void
* @access protected
*/
protected function _sendEmail() {
$mailConfig = $this->dbDatas['mailConfig']['MailConfig'];
$mailContent = $this->dbDatas['mailContent']['MailContent'];
$userMail = '';
// データを整形
$data = $this->Message->restoreData($this->Message->convertToDb($this->request->data));
$data['message'] = $data['Message'];
unset($data['Message']);
$data['mailFields'] = $this->dbDatas['mailFields'];
$data['mailContents'] = $this->dbDatas['mailContent']['MailContent'];
$data['mailConfig'] = $this->dbDatas['mailConfig']['MailConfig'];
$data['other']['date'] = date('Y/m/d H:i');
$data = $this->Message->convertDatasToMail($data);
// 管理者メールを取得
if ($mailContent['sender_1']) {
$adminMail = $mailContent['sender_1'];
} else {
$adminMail = $this->siteConfigs['email'];
}
if (strpos($adminMail, ',') !== false) {
list($fromAdmin) = explode(',', $adminMail);
} else {
$fromAdmin = $adminMail;
}
foreach ($this->dbDatas['mailFields'] as $mailField) {
$field = $mailField['MailField']['field_name'];
if (!isset($data['message'][$field])) {
continue;
}
$value = $data['message'][$field];
// ユーザーメールを取得
if ($mailField['MailField']['type'] == 'email' && $value) {
$userMail = $value;
}
// 件名にフィールドの値を埋め込む
// 和暦など配列の場合は無視
if (!is_array($value)) {
$mailContent['subject_user'] = str_replace('{$' . $field . '}', $value, $mailContent['subject_user']);
$mailContent['subject_admin'] = str_replace('{$' . $field . '}', $value, $mailContent['subject_admin']);
}
}
// 前バージョンとの互換性の為 type が email じゃない場合にも取得できるようにしておく
if (!$userMail) {
if (!empty($data['message']['email'])) {
$userMail = $data['message']['email'];
} elseif (!empty($data['message']['email_1'])) {
$userMail = $data['message']['email_1'];
}
}
// ユーザーに送信
if (!empty($userMail)) {
$data['other']['mode'] = 'user';
$options = array(
'fromName' => $mailContent['sender_name'],
'reply' => $fromAdmin,
'template' => 'Mail.' . $mailContent['mail_template'],
'reply' => $fromAdmin,
);
$this->sendMail($userMail, $mailContent['subject_user'], $data, $options);
}
// 管理者に送信
if (!empty($adminMail)) {
$data['other']['mode'] = 'admin';
$options = array(
'fromName' => $mailContent['sender_name'],
'reply' => $userMail,
'from' => $fromAdmin,
'template' => 'Mail.' . $mailContent['mail_template'],
'bcc' => $mailContent['sender_2'],
'agentTemplate' => false
);
$this->sendMail($adminMail, $mailContent['subject_admin'], $data, $options);
}
}
/**
* 認証用のキャプチャ画像を表示する
*
* @return void
* @access public
*/
public function captcha() {
$this->BcCaptcha->render();
exit();
}
/**
* [SMARTPHONE] 認証用のキャプチャ画像を表示する
*
* @return void
* @access public
*/
public function smartphone_captcha() {
$this->BcCaptcha->render();
exit();
}
}