/
TransmitMail.php
executable file
·2100 lines (1822 loc) · 71.7 KB
/
TransmitMail.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
/**
* TransmitMail クラス
*
* @package TransmitMail
* @license MIT License
* @copyright TAGAWA Takao, dounokouno@gmail.com
* @link https://github.com/dounokouno/TransmitMail
*/
class TransmitMail
{
// システム情報
const SYSTEM_NAME = 'TransmitMail';
const VERSION = '2.6.2';
// 表示モード
public $mode = null;
// グローバルエラー
public $global_errors = array();
// アクセス拒否フラグ
public $deny_flag = false;
// セッションによる多重送信防止フラグ
public $session_flag = false;
// ページ名
public $page_name = '';
// リクエスト値
public $get = array();
public $post = array();
public $server = array();
public $files = array();
// テンプレート
public $tpl = null;
// メール
public $mail = null;
public $smtp = null;
// 入力情報として除外する項目
public $exclusion_item = '[
"x",
"y",
"page_name",
"required",
"hankaku",
"hankaku_eisu",
"hankaku_eiji",
"num",
"num_hyphen",
"hiragana",
"zenkaku_katakana",
"zenkaku",
"zenkaku_all",
"email",
"match",
"len",
"url",
"num_range",
"file",
"file_remove",
"file_required",
"csrf_token"
]';
// 文字参照に変換する文字(主にテンプレートエンジンで利用している文字)
public $character_references = array(
'start_delim' => array(
'character' => '{',
'reference' => '{'
),
'end_delim' => array(
'character' => '}',
'reference' => '}'
),
'var_symbol' => array(
'character' => '$',
'reference' => '$'
)
);
// 設定の初期値
private $default_config = array(
// 基本的な設定
'return_path' => '',
'email' => '',
'cc_email' => '',
'bcc_email' => '',
'from_email' => '',
'subject' => '',
'auto_reply' => true,
'auto_reply_email_input_name' => 'メールアドレス',
'auto_reply_cc_email' => '',
'auto_reply_bcc_email' => '',
'auto_reply_from_email' => '',
'auto_reply_subject' => '',
'auto_reply_name' => '',
// ファイルアップロード
'file' => false,
'file_allow_extension' => '',
'file_max_size' => 512000,
'file_retention_period' => 1800,
// CSV
'csv_output' => false,
'csv_file' => 'data.csv',
'csv_encode' => 'SJIS-win',
// アクセス拒否ホスト
'deny_host' => '',
// セッションによる多重送信防止
'session' => true,
// CSRF 対策
'csrf' => true,
'csrf_token_length' => 32,
// チェックモード
// 0 => 無効
// 1 => 簡易モード
// 2 => 詳細モード
'checkmode' => 0,
// エラーログ
'display_error' => false,
'error_log_file_name' => 'error.log',
'qdmail_error_log_file_name' => 'qdmail_error.log',
// SMTP
'smtp' => false,
'smtp_host' => '',
'smtp_port' => '',
'smtp_protocol' => '',
'smtp_user' => '',
'smtp_password' => '',
// 言語
'language' => 'ja',
'charset' => 'UTF-8',
'reg_option' => 'u',
// タイムゾーン
'timezone' => 'Asia/Tokyo',
// メールフォームプログラム
'mailform_program' => 'index.php',
// テンプレート
'tpl_input' => 'input.html',
'tpl_confirm' => 'confirm.html',
'tpl_finish' => 'finish.html',
'tpl_error' => 'error.html',
'mail_body' => 'config/mail_body.txt',
'mail_auto_reply_body' => 'config/mail_auto_reply_body.txt',
'page_title' => array(
'input' => '',
'confirm' => '入力内容の確認',
'finish' => 'お問い合わせいただきありがとうございます',
'error' => 'エラー'
),
// エラーメッセージ
'error_required' => 'は入力必須です。',
'error_hankaku' => 'は半角文字で入力してください。',
'error_hankaku_eisu' => 'は半角英数字で入力してください。',
'error_hankaku_eiji' => 'は半角英字で入力してください。',
'error_num' => 'は数字で入力してください。',
'error_num_hyphen' => 'は数字とハイフンで入力してください。',
'error_hiragana' => 'はひらがなで入力してください。',
'error_zenkaku_katakana' => 'は全角カタカナで入力してください。',
'error_zenkaku' => 'は全角文字を含めて入力してください。',
'error_zenkaku_all' => 'は全て全角文字で入力してください。',
'error_email' => 'はメールアドレスの書式で入力してください。',
'error_match' => 'が一致しません。',
'error_len' => 'は{文字数}で入力してください。',
'error_url' => 'はURLの書式で入力してください。',
'error_num_range' => 'は{範囲}の数字で入力してください。',
'error_file_extension' => 'は許可されていない拡張子です。',
'error_file_empty' => 'は空のファイルです。',
'error_file_max_size' => 'は指定サイズ({ファイルサイズ})を超えています。',
'error_file_upload' => 'のアップロードに失敗しました。',
'error_file_remove' => 'を削除できませんでした。',
'error_file_not_exist' => 'は見つかりませんでした。',
'error_file_over_the_period' => 'は一時保存期間を超えました。',
'error_file_required' => 'は入力必須です。',
'error_deny' => 'お使いのホストからのアクセスは管理者によって拒否されています。',
'error_csrf' => 'CSRFトークンが無効です。',
'error_failure_send_mail' => 'メールの送信に失敗しました。',
'error_failure_send_mail_auto_reply' => '自動返信メールの送信に失敗しました。',
// その他
'attr_checked' => 'checked',
'attr_selected' => 'selected',
'index_dir' => './',
'log_dir' => 'log/',
'tmp_dir' => 'tmp/',
'file_name_prefix' => 'file_'
);
// 読み込まれた設定情報
private $loaded_config = array();
// 設定情報
public $config = array();
// 設定ファイル
private $config_file = array();
/**
* 各種初期設定
*/
public function init()
{
// 設定の初期値を $config に代入
$this->config = $this->default_config;
// 設定ファイルを読み込む
$args = func_get_args();
foreach ($args as $arg) {
$this->config_file[] = $arg;
if (is_file($arg)) {
$this->loadConfig($arg);
}
}
// エラー表示の設定
if ($this->config['display_error']) {
ini_set('display_errors', 'On');
} else {
ini_set('display_errors', 'Off');
}
if (defined('E_DEPRECATED')) {
error_reporting(E_ALL ^ E_DEPRECATED);
} else {
error_reporting(E_ALL);
}
// エラーログの出力先
ini_set('error_log', $this->config['log_dir'] . $this->config['error_log_file_name']);
// 言語設定など
mb_language($this->config['language']);
mb_internal_encoding($this->config['charset']);
// タイムゾーン
if (function_exists('date_default_timezone_set')) {
date_default_timezone_set($this->config['timezone']);
}
// tinyTemplate を読み込む
require_once dirname(__FILE__) . '/tinyTemplate.php';
$this->tpl = new tinyTemplate();
// リクエストを取得
$this->getRequest();
// 表示モードの判別
if (isset($this->get['file'])) {
$this->mode = 'file';
} elseif ($this->config['checkmode'] && isset($this->get['checkmode'])) {
$this->mode = 'check';
} else {
$this->startSession();
}
}
/**
* 実行
*/
public function run()
{
// 表示モードによる表示の切り替え
if ($this->mode === 'file') {
$this->displayTmpFile($this->get['file']);
} elseif ($this->mode === 'check') {
$this->displayCheckmode();
}
// アクセス拒否ホストを判別
$this->checkDenyHost();
if (method_exists($this, 'afterCheckDenyHost')) {
$this->afterCheckDenyHost();
}
// 入力内容をチェック
$this->checkInput();
if (method_exists($this, 'afterCheckInput')) {
$this->afterCheckInput();
}
// 表示画面を判別、$page_name にセット
$this->setPageName();
if (method_exists($this, 'afterSetPageName')) {
$this->afterSetPageName();
}
// 入力内容をテンプレートプロパティにセット
$this->setTemplateProperty();
if (method_exists($this, 'afterSetTemplateProperty')) {
$this->afterSetTemplateProperty();
}
// $page にあわせて、メール送信、HTMLを出力
$this->setTemplateAndSendMail();
if (method_exists($this, 'afterSetTemplateAndSendMail')) {
$this->afterSetTemplateAndSendMail();
}
}
/**
* 設定ファイルを読み込む
*
* @param string $config_file
*/
public function loadConfig($config_file)
{
// 拡張子を取得
preg_match('/\.([a-z0-9]{2,4})\z/i', $config_file, $matches);
$suffix = strtolower($matches[1]);
// 拡張子を判別
switch ($suffix) {
case 'php':
// php の場合
require_once $config_file;
break;
case 'json':
// json の場合
$json = json_decode(file_get_contents($config_file));
$config = $json->config;
break;
case 'yaml':
case 'yml':
// ymlの場合
require_once dirname(__FILE__) . '/Spyc.php';
$yaml = Spyc::YAMLLoad($config_file);
$config = $yaml['config'];
break;
}
// $config を置き換え
foreach ($config as $key => $value) {
$this->loaded_config[$key] = $value;
$this->config[$key] = $value;
}
}
/**
* リクエストを取得
*/
public function getRequest()
{
$this->get = $_GET;
$this->post = $_POST;
$this->server = $_SERVER;
$this->get = $this->deleteNullbyte($this->get);
$this->post = $this->deleteNullbyte($this->post);
$this->server = $this->deleteNullbyte($this->server);
if (empty($this->server['REMOTE_HOST'])) {
$this->server['REMOTE_HOST'] = gethostbyaddr($this->server['REMOTE_ADDR']);
}
}
/**
* セッションの開始
*/
public function startSession()
{
if ($this->config['session']) {
if (!isset($_SESSION)) {
session_start();
}
session_regenerate_id(true);
}
}
/**
* アクセス拒否ホストを判別
*/
public function checkDenyHost()
{
if (!empty($this->config['deny_host'])) {
$pattern = '/\A' . $this->config['deny_host'] . '\z/';
if (preg_match($pattern, $this->server['REMOTE_ADDR']) ||
preg_match($pattern, $this->server['REMOTE_HOST'])) {
$this->deny_flag = true;
}
}
}
/**
* 入力内容をチェック
*/
public function checkInput()
{
// デフォルトの checked 、 selected をテンプレートにセット
$this->tpl->set('checked.default', $this->config['attr_checked']);
$this->tpl->set('selected.default', $this->config['attr_selected']);
if (count($this->post) > 0) {
$this->tpl->set('checked.default', '');
$this->tpl->set('selected.default', '');
}
// ラジオボタン、チェックボックス、セレクトメニューの選択状態
foreach ($this->post as $key1 => $value1) {
if (is_array($value1)) {
foreach ($value1 as $value2) {
if (!is_array($value2)) {
$this->tpl->set("checked.$key1.$value2", $this->config['attr_checked']);
$this->tpl->set("selected.$key1.$value2", $this->config['attr_selected']);
}
}
} else {
$this->tpl->set("checked.$key1.$value1", $this->config['attr_checked']);
$this->tpl->set("selected.$key1.$value1", $this->config['attr_selected']);
}
}
// 入力必須チェック
if (isset($this->post['required'])) {
foreach ($this->post['required'] as $value) {
$this->tpl->set("required.$value", false);
if (!isset($this->post[$value]) || (isset($this->post[$value]) && (is_null($this->post[$value]) || ($this->post[$value] === '')))) {
$this->tpl->set("required.$value", $this->h($value . $this->config['error_required']));
$this->global_errors[] = $this->h($value . $this->config['error_required']);
}
}
}
// 半角文字チェック
if (isset($this->post['hankaku'])) {
foreach ($this->post['hankaku'] as $value) {
$this->tpl->set("hankaku.$value", false);
if (!empty($this->post[$value])) {
$this->post[$value] = mb_convert_kana($this->post[$value], 'a');
if (!$this->isHankaku($this->post[$value])) {
$this->tpl->set("hankaku.$value", $this->h($value . $this->config['error_hankaku']));
$this->global_errors[] = $this->h($value . $this->config['error_hankaku']);
}
}
}
}
// 半角英数字チェック
if (isset($this->post['hankaku_eisu'])) {
foreach ($this->post['hankaku_eisu'] as $value) {
$this->tpl->set("hankaku_eisu.$value", false);
if (!empty($this->post[$value])) {
$this->post[$value] = mb_convert_kana($this->post[$value], 'a');
if (!$this->isHankakuEisu($this->post[$value])) {
$this->tpl->set(
"hankaku_eisu.$value",
$this->h($value . $this->config['error_hankaku_eisu'])
);
$this->global_errors[] = $this->h($value . $this->config['error_hankaku_eisu']);
}
}
}
}
// 半角英字チェック
if (isset($this->post['hankaku_eiji'])) {
foreach ($this->post['hankaku_eiji'] as $value) {
$this->tpl->set("hankaku_eiji.$value", false);
if (!empty($this->post[$value])) {
$this->post[$value] = mb_convert_kana($this->post[$value], 'r');
if (!$this->isHankakuEiji($this->post[$value])) {
$this->tpl->set(
"hankaku_eiji.$value",
$this->h($value . $this->config['error_hankaku_eiji'])
);
$this->global_errors[] = $this->h($value . $this->config['error_hankaku_eiji']);
}
}
}
}
// 数字チェック
if (isset($this->post['num'])) {
foreach ($this->post['num'] as $value) {
$this->tpl->set("num.$value", false);
if (!empty($this->post[$value])) {
$this->post[$value] = mb_convert_kana($this->post[$value], 'n');
if (!$this->isNum($this->post[$value])) {
$this->tpl->set("num.$value", $this->h($value . $this->config['error_num']));
$this->global_errors[] = $this->h($value . $this->config['error_num']);
}
}
}
}
// 数字とハイフンチェック
if (isset($this->post['num_hyphen'])) {
foreach ($this->post['num_hyphen'] as $value) {
$this->tpl->set("num_hyphen.$value", false);
if (!empty($this->post[$value])) {
$this->post[$value] = mb_convert_kana($this->post[$value], 'a');
if (!$this->isNumHyphen($this->post[$value])) {
$this->tpl->set("num_hyphen.$value", $this->h($value . $this->config['error_num_hyphen']));
$this->global_errors[] = $this->h($value . $this->config['error_num_hyphen']);
}
}
}
}
// ひらがなチェック
if (isset($this->post['hiragana'])) {
foreach ($this->post['hiragana'] as $value) {
$this->tpl->set("hiragana.$value", false);
if (!empty($this->post[$value])) {
$this->post[$value] = mb_convert_kana($this->post[$value], 'cH');
$this->post[$value] = $this->deleteBlank($this->post[$value]);
if (!$this->isHiragana($this->post[$value])) {
$this->tpl->set("hiragana.$value", $this->h($value . $this->config['error_hiragana']));
$this->global_errors[] = $this->h($value . $this->config['error_hiragana']);
}
}
}
}
// 全角カタカナチェック
if (isset($this->post['zenkaku_katakana'])) {
foreach ($this->post['zenkaku_katakana'] as $value) {
$this->tpl->set("zenkaku_katakana.$value", false);
if (!empty($this->post[$value])) {
$this->post[$value] = mb_convert_kana($this->post[$value], 'CK');
$this->post[$value] = $this->deleteBlank($this->post[$value]);
if (!$this->isZenkakuKatakana($this->post[$value])) {
$this->tpl->set(
"zenkaku_katakana.$value",
$this->h($value . $this->config['error_zenkaku_katakana'])
);
$this->global_errors[] = ($value . $this->config['error_zenkaku_katakana']);
}
}
}
}
// 全角文字チェック
if (isset($this->post['zenkaku'])) {
foreach ($this->post['zenkaku'] as $value) {
$this->tpl->set("zenkaku.$value", false);
if (!empty($this->post[$value])) {
if (!$this->isZenkaku($this->post[$value])) {
$this->tpl->set("zenkaku.$value", $this->h($value . $this->config['error_zenkaku']));
$this->global_errors[] = $this->h($value . $this->config['error_zenkaku']);
}
}
}
}
// 全て全角文字チェック
if (isset($this->post['zenkaku_all'])) {
foreach ($this->post['zenkaku_all'] as $value) {
$this->tpl->set("zenkaku_all.$value", false);
if (!empty($this->post[$value])) {
if (!$this->isZenkakuAll($this->post[$value])) {
$this->tpl->set(
"zenkaku_all.$value",
$this->h($value . $this->config['error_zenkaku_all'])
);
$this->global_errors[] = $this->h($value . $this->config['error_zenkaku_all']);
}
}
}
}
// メールアドレスチェック
if (isset($this->post['email'])) {
foreach ($this->post['email'] as $value) {
$this->tpl->set("email.$value", false);
if (!empty($this->post[$value])) {
$this->post[$value] = mb_convert_kana($this->post[$value], 'a');
$this->post[$value] = $this->deleteCrlf($this->post[$value]);
if (!$this->isEmail($this->post[$value])) {
$this->tpl->set("email.$value", $this->h($value . $this->config['error_email']));
$this->global_errors[] = $this->h($value . $this->config['error_email']);
}
}
}
}
// 自動返信メールの宛先( $this->post[$this->config['auto_reply_email_input_name']] )のメールアドレスチェック
if (!empty($this->post[$this->config['auto_reply_email_input_name']])) {
$this->post[$this->config['auto_reply_email_input_name']] = mb_convert_kana($this->post[$this->config['auto_reply_email_input_name']], 'a');
$this->post[$this->config['auto_reply_email_input_name']] = $this->deleteCrlf($this->post[$this->config['auto_reply_email_input_name']]);
if (!$this->isEmail($this->post[$this->config['auto_reply_email_input_name']])) {
$this->tpl->set("email.$this->config['auto_reply_email_input_name']", $this->h($this->config['auto_reply_email_input_name'] . $this->config['error_email']));
if (!in_array($this->h($this->config['auto_reply_email_input_name'] . $this->config['error_email']), $this->global_errors, true)) {
$this->global_errors[] = $this->h($this->config['auto_reply_email_input_name'] . $this->config['error_email']);
}
}
}
// 一致チェック
if (isset($this->post['match'])) {
foreach ($this->post['match'] as $value) {
$array = preg_split('/\s|,/', $value);
$this->tpl->set("match.$array[0]", false);
if ((!empty($this->post[$array[0]]) || !empty($this->post[$array[1]]))
&& $this->post[$array[0]] != $this->post[$array[1]]
) {
$this->tpl->set("match.$array[0]", $this->h($array[0] . $this->config['error_match']));
$this->global_errors[] = $this->h($array[0] . $this->config['error_match']);
}
}
}
// 文字数チェック
if (isset($this->post['len'])) {
foreach ($this->post['len'] as $value) {
$array = preg_split('/\s|,/', $value);
$delim = explode('-', $array[1]);
$delim = array_map('intval', $delim);
$this->tpl->set("len.$array[0]", false);
if (!empty($this->post[$array[0]]) && !$this->isAllowLen($this->post[$array[0]], $delim)) {
if (empty($delim[0])) {
$error_len = str_replace('{文字数}', "$delim[1]文字以下", $this->config['error_len']);
} elseif (empty($delim[1])) {
$error_len = str_replace('{文字数}', "$delim[0]文字以上", $this->config['error_len']);
} else {
if ($delim[0] === $delim[1]) {
$error_len = str_replace('{文字数}', "$delim[0]文字", $this->config['error_len']);
} else {
$error_len = str_replace('{文字数}', "$delim[0]〜$delim[1]文字", $this->config['error_len']);
}
}
$this->tpl->set("len.$array[0]", $this->h($array[0] . $error_len));
$this->global_errors[] = $this->h($array[0] . $error_len);
}
}
}
// URL チェック
if (isset($this->post['url'])) {
foreach ($this->post['url'] as $value) {
$this->tpl->set("url.$value", false);
if (!empty($this->post[$value])) {
$this->post[$value] = mb_convert_kana($this->post[$value], 'a');
$this->post[$value] = $this->deleteCrlf($this->post[$value]);
if (!$this->isUrl($this->post[$value])) {
$this->tpl->set("url.$value", $this->h($value . $this->config['error_url']));
$this->global_errors[] = $this->h($value . $this->config['error_url']);
}
}
}
}
// 整数範囲チェック
if (isset($this->post['num_range'])) {
foreach ($this->post['num_range'] as $value) {
$array = preg_split('/\s|,/', $value);
$delim = explode('-', $array[1]);
$delim = array_map('intval', $delim);
$this->tpl->set("num_range.$array[0]", false);
if ($this->post[$array[0]] !== '') {
// 数字チェック
$this->post[$array[0]] = mb_convert_kana($this->post[$array[0]], 'n');
if (!$this->isNum($this->post[$array[0]])) {
$this->tpl->set("num_range.$array[0]", $this->h($array[0] . $this->config['error_num']));
$this->global_errors[] = $this->h($array[0] . $this->config['error_num']);
} else {
if (!$this->isAllowNumRange($this->post[$array[0]], $delim)) {
if ($delim[0] === $delim[1]) {
$error_num_range = str_replace(
'{範囲}',
"ちょうど{$delim[0]}",
$this->config['error_num_range']
);
} else {
if ($delim[1] === 0) {
$error_num_range = str_replace(
'{範囲}',
"{$delim[0]}以上",
$this->config['error_num_range']
);
} else {
$error_num_range = str_replace(
'{範囲}',
"{$delim[0]}以上、{$delim[1]}以下",
$this->config['error_num_range']
);
}
}
$this->tpl->set("num_range.$array[0]", $this->h($array[0] . $error_num_range));
$this->global_errors[] = $this->h($array[0] . $error_num_range);
}
}
}
}
}
// ファイル添付を利用する場合
if ($this->config['file']) {
// ファイルの削除
if (isset($this->post['file_remove'])) {
foreach ($this->post['file_remove'] as $value) {
$tmp_name = $this->post['file'][$value]['tmp_name'];
$file_path = $this->config['tmp_dir'] . basename($tmp_name);
if (is_file($file_path) && unlink($file_path)) {
$this->post['file'][$value]['tmp_name'] = '';
$this->post['file'][$value]['name'] = '';
} else {
$this->global_errors[] = $this->h($tmp_name . $this->config['error_file_remove']);
}
}
}
// 既にファイルがアップロードされている場合
if (isset($this->post['file'])) {
foreach ($this->post['file'] as $key => $value) {
if (isset($value['tmp_name'])) {
// single の場合
if (is_file($this->config['tmp_dir'] . basename($value['tmp_name']))) {
$this->tpl->set("$key.tmp_name", $this->h($value['tmp_name']));
$this->tpl->set("$key.name", $this->h($value['name']));
$this->files[$key] = array(
'tmp_name' => $this->h($value['tmp_name']),
'name' => $this->h($value['name'])
);
}
}
}
}
// ファイルの入力必須チェック
if (isset($this->post['file_required'])) {
foreach ($this->post['file_required'] as $value) {
$this->tpl->set("file_required.$value", false);
if (empty($_FILES[$value]['tmp_name']) && empty($this->post['file'][$value]['tmp_name'])) {
$this->tpl->set(
"file_required.$value",
$this->h($value . $this->config['error_file_required'])
);
$this->global_errors[] = $this->h($value . $this->config['error_file_required']);
}
}
}
// ファイルのアップロード
if (isset($_FILES)) {
foreach ($_FILES as $key => $value) {
$file_error = array();
$this->tpl->set("file.$key", false);
if (!is_array($value['tmp_name'])) {
// single の場合
if (!empty($value['tmp_name'])) {
// 拡張子のチェック
if (!empty($this->config['file_allow_extension']) &&
!$this->isAllowFileExtension($value['name']))
{
$file_error[] = $this->h($key . $this->config['error_file_extension']);
$this->global_errors[] = $this->h($key . $this->config['error_file_extension']);
}
// 空ファイルのチェック
if ($value['size'] === 0) {
$file_error[] = $this->h($key . $this->config['error_file_empty']);
$this->global_errors[] = $this->h($key . $this->config['error_file_empty']);
}
// ファイルサイズのチェック
if ($value['size'] > $this->config['file_max_size']) {
$file_error[] = $this->h($key . str_replace(
'{ファイルサイズ}',
$this->getFormatedBytes($this->config['file_max_size']),
$this->config['error_file_max_size']));
$this->global_errors[] = $this->h($key . str_replace(
'{ファイルサイズ}',
$this->getFormatedBytes($this->config['file_max_size']),
$this->config['error_file_max_size']));
}
// エラーを判別
if (count($file_error) > 0) {
// エラーがある場合、エラーメッセージをセット
$this->tpl->set("file.$key", $file_error);
} else {
// エラーが無い場合、ファイルを$config['tmp_dir']に移動
$tmp_name = $this->config['file_name_prefix'] . uniqid(rand()) .
'_' . $value['name'];
$file_path = $this->config['tmp_dir'] . $tmp_name;
if (move_uploaded_file($value['tmp_name'], $file_path)) {
$this->tpl->set("$key.tmp_name", $this->h($tmp_name));
$this->tpl->set("$key.name", $this->h($value['name']));
$this->files[$key] = array(
'tmp_name' => $this->h($tmp_name),
'name' => $this->h($value['name'])
);
} else {
// アップロードに失敗した場合
$file_error[] = $this->h($key . $this->config['error_file_upload']);
$this->global_errors[] = $this->h($key . $this->config['error_file_upload']);
$this->tpl->set("file.$key", $file_error);
}
}
}
}
}
}
}
// CSRF トークンチェック
if ($this->config['session'] && $this->config['csrf'] && (count($this->post) > 0)) {
if (!isset($this->post['csrf_token']) ||
!isset($_SESSION['csrf_token']) ||
($this->post['csrf_token'] !== $_SESSION['csrf_token'])) {
$this->tpl->set('csrf', $this->h($this->config['error_csrf']));
$this->global_errors[] = $this->h($this->config['error_csrf']);
}
}
}
/**
* 表示画面を判別、 $page_name にセット
*/
public function setPageName()
{
// セッションの判別
if ($this->config['session']) {
if (isset($_SESSION['transmit_mail_input']) && $_SESSION['transmit_mail_input']) {
$this->session_flag = true;
}
} else {
$this->session_flag = true;
}
// $page_name の判別
if ($this->deny_flag) {
// アクセス拒否
$this->page_name = 'deny';
} elseif (!$this->session_flag) {
// セッションが無い場合 入力画面
$this->page_name = '';
} elseif (count($this->post) > 0) {
if ($this->global_errors) {
// エラーがある場合 入力エラー画面
$this->page_name = '';
} elseif (isset($this->post['page_name']) &&
($this->post['page_name'] === 'input') &&
!$this->global_errors)
{
// 再入力画面
$this->page_name = '';
} elseif (isset($this->post['page_name']) &&
($this->post['page_name'] === 'finish') &&
!$this->global_errors)
{
// 完了画面
$this->page_name = 'finish';
} elseif (!$this->global_errors) {
// エラーが無い場合 確認画面
$this->page_name = 'confirm';
}
}
}
/**
* 入力内容をテンプレートプロパティにセット
*/
public function setTemplateProperty()
{
$params = array();
$hiddens = array();
if (empty($this->page_name)) {
// 入力画面 or 入力エラー画面
foreach ($this->post as $key => $value) {
$this->tpl->set($key, $this->h($value));
}
} elseif ($this->page_name === 'confirm' || $this->page_name === 'finish') {
// 確認画面 or 完了画面
foreach ($this->post as $key => $value1) {
if ($this->isExcludedExclusionItem($key)) {
if (is_array($value1)) {
$this->tpl->set("$key.array", array_map(array($this, 'h'), $value1));
$value2 = implode(', ', $value1);
} else {
$value2 = $value1;
}
$hidden = $this->getInputHidden($key, $value1);
$this->tpl->set("$key.key", $this->h($key));
$this->tpl->set("$key.value", $this->h($value2));
$this->tpl->set("$key.value.nl2br", nl2br($this->h($value2)));
$this->tpl->set("$key.hidden", $hidden);
$params[] = array(
'key' => $this->h($key),
'value' => $this->h($value2),
'value.nl2br' => nl2br($this->h($value2)),
'hidden' => $hidden
);
$hiddens[] = $hidden;
}
}
}
// $_FILES
if ($this->config['file']) {
$array = array();
foreach ($this->files as $key => $value) {
if (isset($value['tmp_name'])) {
// single の場合
$hidden_tmp_name = $this->getInputHidden('file[' . $key . '][tmp_name]', $value['tmp_name']);
$hidden_name = $this->getInputHidden('file[' . $key . '][name]', $value['name']);
$this->tpl->set("$key.key", $this->h($key));
$this->tpl->set("$key.tmp_name", $this->h($value['tmp_name']));
$this->tpl->set("$key.name", $this->h($value['name']));
$this->tpl->set("$key.hidden_tmp_name", $hidden_tmp_name);
$this->tpl->set("$key.hidden_name", $hidden_name);
$array[] = array(
'key' => $this->h($key),
'tmp_name' => $this->h($value['tmp_name']),
'name' => $this->h($value['name']),
'hidden_tmp_name' => $hidden_tmp_name,
'hidden_name' => $hidden_name
);
$hiddens[] = $hidden_tmp_name;
$hiddens[] = $hidden_name;
}
}
$this->tpl->set('files', $array);
}
// CSRF トークン
if ($this->config['session'] && $this->config['csrf']) {
if (!isset($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = $this->generateToken();
}
$this->tpl->set('csrf_token', $_SESSION['csrf_token']);
$hiddens[] = $this->getInputHidden('csrf_token', $_SESSION['csrf_token']);
}
$this->tpl->set('params', $params);
$this->tpl->set('hiddens', implode('', $hiddens));
$this->tpl->set('_GET', $this->h($this->get));
$this->tpl->set('_SERVER', $this->h($this->server));
}
/**
* $page_name にあわせてHTMLを出力、メール送信
*/
public function setTemplateAndSendMail()
{
// $page_name を判別
if ($this->page_name === 'deny') {