-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.lib.php
3420 lines (2743 loc) · 124 KB
/
form.lib.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
define( 'PHPFMG_TO' , '[email protected]' );
define( 'PHPFMG_REDIRECT', '' );
define( 'PHPFMG_ID' , '20190227-06f3' );
define( 'PHPFMG_GDPR' , 'Y' ); // 'Y' to enable General Data Protection Regulation(GDPR), don't save data and log
define( 'PHPFMG_ROOT_DIR' , dirname(__FILE__) );
define( 'PHPFMG_SAVE_FILE' , PHPFMG_ROOT_DIR . '/form-data-log.php' ); // save submitted data to this file
define( 'PHPFMG_EMAILS_LOGFILE' , PHPFMG_ROOT_DIR . '/email-traffics-log.php' ); // log email traffics to this file
if( !defined('PHPFMG_ADMIN_URL') ) define( 'PHPFMG_ADMIN_URL' , 'admin.php' ); // might be defined already by wordpress form loader plugin
define( 'PHPFMG_SAVE_ATTACHMENTS' , '' );
define( 'PHPFMG_SAVE_ATTACHMENTS_DIR' , PHPFMG_ROOT_DIR . '/uploaded/' );
// three options : empty - always mail file as attachment, 0 - always mail file as link, N - mail file as link if filesize larger than N Kilobytes
define( 'PHPFMG_FILE2LINK_SIZE' , '' );
define( 'PHPFMG_UPLOAD_CONTROL' , '' );
define( 'PHPFMG_HARMFUL_EXTS' , ".php, .php2, .php3, .php4, .php5, .php6, .php7, .html, .css, .js, .exe, .com, .bat, .vb, .vbs, scr, .inf, .reg, .lnk, .pif, .ade, .adp, .app, .bas, .chm, .cmd, .cpl, .crt, .csh, .fxp, .hlp, .hta, .ins, .isp, .jse, .ksh, .Lnk, .mda, .mdb, .mde, .mdt, .mdw, .mdz, .msc, .msi, .msp, .mst, .ops, .pcd, .prf, .prg, .pst, .scf, .scr, .sct, .shb, .shs, .url, .vbe, .wsc, .wsf, .wsh" );
define( 'PHPFMG_HARMFUL_EXTS_MSG' , 'File is potential harmful. Upload is not allowed.' );
define( 'PHPFMG_ALLOW_EXTS' , ".jpg, .gif, .png, .bmp" );
define( 'PHPFMG_ALLOW_EXTS_MSG' , "Upload is not allowed. Please check your file type." );
define( 'PHPFMG_SUBJECT' , "Rukanda Pride latest news and Offers Sign Up" );
define( 'PHPFMG_CC' , '[email protected], [email protected]' );
define( 'PHPFMG_BCC', '[email protected]' );
// for auto-response email
define( 'PHPFMG_RETURN_ENABLE' , 'Y' ); // 'Y' to enable auto-response email, use '' or 'N' to turn off
define( 'PHPFMG_YOUR_NAME' , '' ); // name of auto-response mail address
define( 'PHPFMG_RETURN_EMAIL' , "" );
define( 'PHPFMG_RETURN_SUBJECT' , "" ); // auto-response mail subject
define( 'PHPFMG_RETURN_NO_ATTACHMENT' , '' ); // Y - No attachements will be included for auto-response email
define( 'PHPFMG_CHARSET' , 'UTF-8' );
define( 'PHPFMG_MAIL_TYPE' , 'html' ); // send mail in html format or plain text.
define( 'PHPFMG_ACTION' , 'mailandfile' ); // delivery method
define( 'PHPFMG_TEXT_ALIGN' , 'top' ); // field label text alignment: top, right, left
define( 'PHPFMG_NO_FROM_HEADER' , '' ); // don't make up From: header.
define( 'PHPFMG_SENDMAIL_FROM' , '' ); // force sender's email
define( 'PHPFMG_USE_PHPMAILER' , '' ); // Y - use phpmailer as the default
// smtp options
define( 'PHPFMG_USE_SMTP' , '' ); // Y - enable
define( 'PHPFMG_SMTP_HOST' , '' );
define( 'PHPFMG_SMTP_USER' , '' );
define( 'PHPFMG_SMTP_PASSWORD' , '' );
define( 'PHPFMG_SMTP_PLAIN_PASSWORD' , '' ); // use this to overwrite above password
define( 'PHPFMG_SMTP_PORT' , '' ); // default 25, use 465 for gmail
define( 'PHPFMG_SMTP_SECURE' , '' );
define( 'PHPFMG_SMTP_DEBUG_LEVEL' , '' ); // empty or 0 - trun off debug
if( !class_exists('PHPMailer') && file_exists(PHPFMG_ROOT_DIR.'/phpmailer.php') ){
include_once( PHPFMG_ROOT_DIR.'/phpmailer.php' );
};
define( 'RECAP_SITE_KEY' , "" );
define( 'RECAP_SECRET_KEY' , "" );
define( 'RECAP_LANGUAGE' , "en" );
define( 'RECAP_THEME' , "light" );
define( 'HOST_NAME',getEnv( 'SERVER_NAME' ) );
define( 'PHP_SELF', getEnv( 'SCRIPT_NAME' ) );
define( 'PHPFMG_LNCR', phpfmg_linebreak() );
define( 'PHPFMG_ANTI_HOTLINKING' , '' );
define( 'PHPFMG_REFERERS_ALLOW', "" ); // Referers - domains/ips that you will allow forms to reside on.
define( 'PHPFMG_REFERERS_DENIED_MSG', "You are coming from an <b>unauthorized domain.</b>" );
define( 'PHPFMG_ONE_ENTRY' , '' );
define( 'PHPFMG_ONE_ENTRY_METHOD' , '' );
phpfmg_init();
# -----------------------------------------------------------------------------
function phpfmg_thankyou(){
phpfmg_redirect_js();
?>
<!-- [Your confirmation message goes here] -->
<br>
<b>Your have successfully subscribed. Thank you!</b>
<br><br>
<?php
} // end of function phpfmg_thankyou()
function phpfmg_auto_response_message(){
ob_start();
?>
<?php
$msg = ob_get_contents() ;
ob_end_clean();
return trim($msg);
}
function phpfmg_mail_template(){
ob_start();
?>
<?php
$msg = ob_get_contents() ;
ob_end_clean();
return trim($msg);
}
# --- Array of Form Elements ---
$GLOBALS['form_mail'] = array();
$GLOBALS['form_mail']['field_0'] = array( "name" => "field_0", "text" => "Full Name", "type" => "text", "instruction" => "", "required" => "Required" ) ;
$GLOBALS['form_mail']['field_1'] = array( "name" => "field_1", "text" => "email", "type" => "sender's email", "instruction" => "", "required" => "Required" ) ;
$GLOBALS['form_mail']['field_2'] = array( "name" => "field_2", "text" => "Phone Number", "type" => "text", "instruction" => "", "required" => "" ) ;
$GLOBALS['form_mail']['field_3'] = array( "name" => "field_3", "text" => "Select all updates you want to recieve", "type" => "checkbox", "instruction" => "", "required" => "Required" ) ;
/**
* GNU Library or Lesser General Public License version 2.0 (LGPLv2)
*/
function phpfmg_init(){
error_reporting( E_ERROR );
ini_set('magic_quotes_runtime', 0);
ini_set( 'max_execution_time', 0 );
ini_set( 'max_input_time', 36000 );
session_start();
if( !isset($_SESSION['HTTP_REFERER']) )
$_SESSION['HTTP_REFERER'] = $_SERVER['HTTP_REFERER'] ;
phpfmg_check_referers();
if ( get_magic_quotes_gpc() && isset($_POST) ) {
phpfmg_stripslashes( $_POST );
};
}
function phpfmg_stripslashes(&$var){
if(!is_array($var)) {
$var = stripslashes($var);
} else {
array_walk($var,'phpfmg_stripslashes');
};
}
function phpfmg_display_form( $title="", $keywords="", $description="" ){
@header( 'Content-Type: text/html; charset=' . PHPFMG_CHARSET );
$phpfmg_send = phpfmg_sendmail( $GLOBALS['form_mail'] ) ;
$isHideForm = isset($phpfmg_send['isHideForm']) ? $phpfmg_send['isHideForm'] : false;
$sErr = isset($phpfmg_send['error']) ? $phpfmg_send['error'] : '';
# FormMail main()
phpfmg_header( $title, $keywords, $description );
if( !$isHideForm ){
phpfmg_form($sErr);
}else{
phpfmg_thankyou();
};
phpfmg_footer();
return;
}
function phpfmg_linebreak(){
$os = strtolower(PHP_OS);
switch( true ){
case ("\\" == DIRECTORY_SEPARATOR) : // windows
return "\x0d\x0a" ;
case ( strpos($os, 'darwin') !== false ) : // Mac
return "\x0d" ;
default :
return "\x0a" ; // *nix
};
}
function phpfmg_sendmail( &$form_mail ) {
if( !isset($_POST["formmail_submit"]) ) return ;
$isHideForm = false ;
$sErr = checkPass($form_mail);
$err_captcha = phpfmg_check_captcha();
if( $err_captcha != '' ){
$sErr['fields'][] = 'phpfmg_captcha';
$sErr['errors'][] = ERR_CAPTCHA;
};
if( empty($sErr['fields']) && phpfmg_has_entry() ){
$sErr['fields'][] = 'phpfmg_found_entry';
$sErr['errors'][] = 'Found entry already!';
};
if( empty($sErr['fields']) ){
sendFormMail( $form_mail, PHPFMG_SAVE_FILE ) ;
$isHideForm = true;
// move the redirect to phpfmg_thankyou() to get around the redirection within an iframe problem
/*
$redirect = PHPFMG_REDIRECT;
if( strlen(trim($redirect)) ):
header( "Location: $redirect" );
exit;
endif;
*/
};
return array(
'isHideForm' => $isHideForm,
'error' => $sErr ,
);
}
function phpfmg_has_entry(){
if( !file_exists(PHPFMG_SAVE_FILE) ){
return false; // has nothing to check
};
$found = false ;
if( defined('PHPFMG_ONE_ENTRY') && 'Y' == PHPFMG_ONE_ENTRY ){
$query = defined('PHPFMG_ONE_ENTRY_METHOD') && PHPFMG_ONE_ENTRY_METHOD == 'email' && isset($GLOBALS['sender_email']) ? $GLOBALS['sender_email'] : $_SERVER['REMOTE_ADDR'] ;
if( empty($query) )
return false ;
$GLOBALS['OneEntry'] = $query;
$query = '"'. strtolower($query) . '"';
$handle = fopen(PHPFMG_SAVE_FILE,'r');
if ($handle) {
while (!feof($handle)) {
$entry = strtolower(fgets($handle, 4096));
if( strpos($entry,$query) !== false ){
$found = true ;
break;
};
};
fclose($handle);
};
};
return $found ;
}
function sendFormMail( $form_mail, $sFileName = "" )
{
$to = filterEmail(PHPFMG_TO) ;
$cc = filterEmail(PHPFMG_CC) ;
$bcc = filterEmail(PHPFMG_BCC) ;
// simply chop email address to avoid my website being abused
if( false !== strpos( strtolower($_SERVER['HTTP_HOST']),'formmail-maker.com') ){
$cc = substr($cc, 0, 50);
$bcc = substr($bcc,0, 50);
};
$subject = PHPFMG_SUBJECT ;
$from = $to ;
$fromName = "";
$titleOfSender = '';
$firstName = "";
$lastName = "";
$strip = get_magic_quotes_gpc() ;
$content = '' ;
$style = 'font-family:Verdana, Arial, Helvetica, sans-serif; font-size : 13px; color:#474747;padding:6px;border-bottom:1px solid #cccccc;' ;
$tr = array() ; // html table
$csvValues = array();
$cols = array();
$replace = array();
$RecordID = phpfmg_getRecordID();
$isWritable = is_writable( dirname(PHPFMG_SAVE_ATTACHMENTS_DIR) );
foreach( $form_mail as $field ){
$field_type = strtolower($field[ "type" ]);
if( 'sectionbreak' == $field_type ){
continue;
};
$field[ "text" ] = stripslashes( $field[ "text" ] );
//$value = trim( $_POST[ $field[ "name" ] ] );
$value = phpfmg_field_value( $field[ "name" ] );
$value = $strip ? stripslashes($value) : $value ;
if( 'attachment' == $field_type ){
$value = $isWritable ? phpfmg_file2value( $RecordID, $_FILES[ $field[ "name" ] ] ) : $_FILES[ $field[ "name" ] ]['name'];
//$value = $_FILES[ $field[ "name" ] ]['name'];
};
$content .= $field[ "text" ] . " \t : " . $value .PHPFMG_LNCR;
$tr[] = "<tr> <td valign=top style='{$style};width:33%;border-right:1px solid #cccccc;'>" . $field[ "text" ] . " </td> <td valign=top style='{$style};'>" . nl2br($value) . " </td></tr>" ;
$csvValues[] = csvfield( $value );
$cols[] = csvfield( $field[ "text" ] );
$replace["%".$field[ "name" ]."%"] = $value;
switch( $field_type ){
case "sender's email" :
$from = filterEmail($value) ;
break;
case "sender's name" :
$fromName = filterEmail($value) ;
break;
case "titleofsender" :
$titleOfSender = $value ;
break;
case "senderfirstname" :
$firstName = filterEmail($value) ;
break;
case "senderlastname" :
$lastName = filterEmail($value) ;
break;
default :
// nothing
};
}; // for
$isHtml = 'html' == PHPFMG_MAIL_TYPE ;
if( $isHtml ) {
$content = "<table cellspacing=0 cellpadding=0 border=0 >" . PHPFMG_LNCR . join( PHPFMG_LNCR, $tr ) . PHPFMG_LNCR . "</table>" ;
};
if( !empty($firstName) && !empty($lastName) ){
$fromName = $firstName . ' ' . $lastName;
};
$fromHeader = filterEmail( ('' != $fromName ? "\"$fromName\"" : '' ) . " <{$from}>",array(",", ";")) ; // no multiple emails are allowed.
$GLOBALS['ReplyTo'] = $fromHeader;
$_fields = array(
'%NameOfSender%' => $fromName,
'%FirstNameOfSender%' => $firstName,
'%LastNameOfSender%' => $lastName,
'%EmailOfSender%' => $from,
'%TitleOfSender%' => $titleOfSender,
'%DataOfForm%' => $content,
'%IP%' => $_SERVER['REMOTE_ADDR'],
'%Date%' => date("Y-m-d"),
'%Time%' => date("H:i:s"),
'%HTTP_HOST%' => $_SERVER['HTTP_HOST'],
'%FormPageLink%' => phpfmg_request_uri(),
'%HTTP_REFERER%' => $_SESSION['HTTP_REFERER'],
'%AutoID%' => $RecordID,
'%FormAdminURL%' => phpfmg_admin_url()
);
$fields = array_merge( $_fields, $replace );
$esh_mail_template = trim(phpfmg_mail_template());
if( !empty($esh_mail_template) ){
$esh_mail_template = phpfmg_adjust_template($esh_mail_template);
$content = phpfmg_parse_mail_body( $esh_mail_template, $fields );
};
$subject = phpfmg_parse_mail_body( $subject, $fields );
if( $isHtml ) {
$content = phpfmg_getHtmlContent( $content );
};
$oldMask = umask(0);
//$sep = ','; //chr(0x09);
$sep = chr(0x09);
$recordCols = phpfmg_data2record( csvfield('RecordID') . $sep . csvfield('Date') . $sep . csvfield('IP') . $sep . join($sep,$cols) );
$record = phpfmg_data2record( csvfield($RecordID) . $sep . csvfield(date("Y-m-d H:i:s")) . $sep . csvfield($_SERVER['REMOTE_ADDR']) .$sep . join($sep,$csvValues) );
/*
Some hosting companies (like Yahoo and GoDaddy) REQUIRED a registered email address to send out all emails!
The mailer HAS to use the REGISTERED email address as the sender's email address. This is called the sendmail_from.
*/
$sendmail_from = $from;
$sender_email = $from;
$force_sender = defined('PHPFMG_SENDMAIL_FROM') && '' != PHPFMG_SENDMAIL_FROM ;
if( $force_sender ){
ini_set("sendmail_from", PHPFMG_SENDMAIL_FROM);
$sendmail_from = PHPFMG_SENDMAIL_FROM;
};
if( defined('PHPFMG_SMTP') && '' != PHPFMG_SMTP ){
ini_set("SMTP", PHPFMG_SMTP);
};
switch( strtolower(PHPFMG_ACTION) ){
case 'fileonly' :
appendToFile( $sFileName, $record, $recordCols );
break;
case 'mailonly' :
mailAttachments( $to , $subject , $content, $sendmail_from, $fromName, $fromHeader, $cc , $bcc, PHPFMG_CHARSET ) ;
break;
case 'mailandfile' :
default:
mailAttachments( $to , $subject , $content, $sendmail_from, $fromName, $fromHeader, $cc , $bcc, PHPFMG_CHARSET ) ;
appendToFile( $sFileName, $record, $recordCols );
}; // switch
mailAutoResponse( $sender_email, $force_sender ? $sendmail_from : $to, $fields ) ;
umask($oldMask);
session_destroy();
session_regenerate_id(true);
}
function phpfmg_file2value( $recordID, $file ){
$tmp = $file[ "tmp_name" ] ;
$name = phpfmg_rename_harmful(trim($file[ "name" ])) ;
if( !defined('PHPFMG_FILE2LINK_SIZE') ){
return $name;
};
if( is_uploaded_file( $tmp ) ) {
$size = trim(PHPFMG_FILE2LINK_SIZE) ;
switch( $size ){
case '' :
return $name;
default:
$isHtml = 'html' == PHPFMG_MAIL_TYPE;
$filelink = base64_encode($recordID . '-' . $name);
$url = phpfmg_admin_url() . "?mod=filman&func=download&filelink=" . urlencode($filelink) ;
$isLarger = (filesize($tmp)/1024) > $size ;
$link = $isHtml ? "<a href='{$url}'>$name</a>" : $name . " ( {$url} )";
return $isLarger ? $link : $name ; // email download link when size is larger defined size, otherwise send as attachment
};// switch
}; // if
return $name;
}
function phpfmg_dir2unix( $dir ){
return str_replace( array("\\", '//'), '/', $dir );
}
function phpfmg_request_uri(){
$uri = getEnv('REQUEST_URI'); // apache has this
if( false !== $uri && strlen($uri) > 0 ){
return $uri ;
} else {
$uri = ($uri = getEnv('SCRIPT_NAME')) !== false
? $uri
: getEnv('PATH_INFO') ;
$qs = getEnv('QUERY_STRING'); // IIS and Apache has this
return $uri . ( empty($qs) ? '' : '?' . $qs );
};
return "" ;
}
// parse full admin url to view large size uploaded file online
function phpfmg_admin_url(){
$http_host = "http://{$_SERVER['HTTP_HOST']}";
switch( true ){
case (0 === strpos(PHPFMG_ADMIN_URL, 'http://' )) :
$url = PHPFMG_ADMIN_URL;
break;
case ( '/' == substr(PHPFMG_ADMIN_URL,0,1) ) :
$url = $http_host . PHPFMG_ADMIN_URL ;
break;
default:
$uri = phpfmg_request_uri();
$pos = strrpos( $uri, '/' );
$vdir = substr( $uri, 0, $pos );
$url = $http_host . $vdir . '/' . PHPFMG_ADMIN_URL ;
};
return $url;
}
function phpfmg_ispost(){
return 'POST' == strtoupper($_SERVER["REQUEST_METHOD"]) || 'POST' == strtoupper(getEnv('REQUEST_METHOD')) ;
}
function phpfmg_is_mysite(){
return false !== strpos( strtolower($_SERVER['HTTP_HOST']),'formmail-maker.com'); // accessing form at mysite
}
// don't allow hotlink form to my website. To avoid people create phishing form.
function phpfmg_hotlinking_mysite(){
$yes = phpfmg_is_mysite()
&& ( empty($_SERVER['HTTP_REFERER']) || false === strpos( strtolower($_SERVER['HTTP_REFERER']),'formmail-maker.com') ) ; // doesn't have referer of mysite
if( $yes ){
die( "<b>Access Denied.</b>
<br><br>
You are visiting a form hotlinkink from <a href='http://www.formmail-maker.com'>formmail-maker.com</a> which is not allowed.
Please read the <a href='http://www.formmail-maker.com/web-form-mail-faq.php'>FAQ</a>.
" );
};
}
function phpfmg_check_referers(){
phpfmg_hotlinking_mysite(); // anti phishing
$debugs = array();
$debugs[] = "Your IP: " . $_SERVER['REMOTE_ADDR'];
$debugs[] = "Referer link: " . $_SERVER['HTTP_REFERER'];
$debugs[] = "Host of referer: $referer";
$check = defined('PHPFMG_ANTI_HOTLINKING') && 'Y' == PHPFMG_ANTI_HOTLINKING;
if( !$check ) {
$debugs[] = "Referer is empty. No need to check hot linking.";
//echo "<pre>" . join("\n",$debugs) . "</pre>\n";
//appendToFile( PHPFMG_EMAILS_LOGFILE, date("Y-m-d H:i:s") . "\t" . $_SERVER['REMOTE_ADDR'] . " \n" . join("\n",$debugs) ) ;
return true;
};
// maybe post from local file
if( !isset($_SERVER['HTTP_REFERER']) && phpfmg_ispost() ){
appendToFile( PHPFMG_EMAILS_LOGFILE, date("Y-m-d H:i:s") . "\t" . $_SERVER['REMOTE_ADDR'] . " \n phpfmg_ispost " . join("\n",$debugs) ) ;
die( PHPFMG_REFERERS_DENIED_MSG );
};
$url = parse_url($_SERVER['HTTP_REFERER']);
$referer = str_replace( 'www.', '', strtolower($url['host']) );
if( empty($referer) ) {
return true;
};
$hosts = explode(',',PHPFMG_REFERERS_ALLOW);
$http_host = strtolower($_SERVER['HTTP_HOST']);
$referer = $http_host ;
$hosts[] = str_replace('www.', '', $http_host );
$debugs[] = "Hosts Allow: " . PHPFMG_REFERERS_ALLOW;
$allow = false ;
foreach( $hosts as $host ){
$host = strtolower(trim($host));
$debugs[] = "check host: $host " ;
if( false !== strpos($referer, $host) || false !== strpos($referer, 'www.'.$host) ){
$allow = true;
$debugs[] = " -> allow (quick exit)";
break;
}else{
$debugs[] = " -> deny";
};
};
//echo "<pre>" . join("\n",$debugs) . "</pre>\n";
//appendToFile( PHPFMG_EMAILS_LOGFILE, date("Y-m-d H:i:s") . "\t" . $_SERVER['REMOTE_ADDR'] . " \n" . join("\n",$debugs) ) ;
if( !$allow ){
die( PHPFMG_REFERERS_DENIED_MSG );
};
}
function phpfmg_getRecordID(){
if( !isset($GLOBALS['RecordID']) ){
$GLOBALS['RecordID'] = date("Ymd") . '-'. substr( md5(uniqid(rand(), true)), 0,4 );
};
return $GLOBALS['RecordID'];
}
function phpfmg_data2record( $s, $b=true ){
$from = array( "\r", "\n");
$to = array( "\\r", "\\n" );
return $b ? str_replace( $from, $to, $s ) : str_replace( $to, $from, $s ) ;
}
function csvfield( $str ){
$str = str_replace( '"', '""', $str );
return '"' . trim($str) . '"';
}
function mailAttachments( $to = "" , $subject = "" , $message = "" , $from="", $fromName = "" , $fromHeader ="", $cc = "" , $bcc = "", $charset = "UTF-8", $type = 'FormMail' ){
if( ! strlen( trim( $to ) ) ) return "Missing \"To\" Field." ;
$isAutoResponse = $type == 'AutoResponseEmail' ;
// added PHPMailer SMTP support at Mar 12, 2011
$isSMTP = defined('PHPFMG_USE_SMTP') && 'Y' == PHPFMG_USE_SMTP && defined('PHPFMG_SMTP_HOST') && '' != PHPFMG_SMTP_HOST;
// due to security issues, in most case, the smtp will fail on my website. It only works on user's own server
// so just disable the smtp here
if( phpfmg_is_mysite() ){
$isSMTP = false ;
};
$attachments = array();
$noAutoAttachements = $isAutoResponse && defined('PHPFMG_RETURN_NO_ATTACHMENT') && 'Y' == PHPFMG_RETURN_NO_ATTACHMENT ;
$use_phpmailer = defined('PHPFMG_USE_PHPMAILER') && 'Y' == PHPFMG_USE_PHPMAILER ;
$boundary = "====_My_PHP_Form_Generator_" . md5( uniqid( srand( time() ) ) ) . "====";
$content_type = 'html' == PHPFMG_MAIL_TYPE ? "text/html" : "text/plain" ;
// setup mail header infomation
$headers = 'Y' == PHPFMG_NO_FROM_HEADER ? '' : "From: {$fromHeader}" .PHPFMG_LNCR;
$headers .= "Reply-To: {$GLOBALS['ReplyTo']}" .PHPFMG_LNCR;
if ($cc) $headers .= "CC: $cc".PHPFMG_LNCR;
if ($bcc) $headers .= "BCC: $bcc".PHPFMG_LNCR;
//$headers .= "Content-type: {$content_type}; charset={$charset}" .PHPFMG_LNCR ;
$plainHeaders = $headers ; // for no attachments header
$plainHeaders .= 'MIME-Version: 1.0' . PHPFMG_LNCR;
$plainHeaders .= "Content-type: {$content_type}; charset={$charset}" ;
//create mulitipart attachments boundary
$sError = "" ;
$nFound = 0;
if( false && isset($GLOBALS['phpfmg_files_content']) && '' != $GLOBALS['phpfmg_files_content'] ){
// use previous encoded content
$sEncodeBody = $GLOBALS['phpfmg_files_content'] ;
$nFound = $GLOBALS['phpfmg_nFound'] ;
}else{
$file2link_size = trim(PHPFMG_FILE2LINK_SIZE) ;
$isSave = ('' != $file2link_size || defined('PHPFMG_SAVE_ATTACHMENTS') && 'Y' == PHPFMG_SAVE_ATTACHMENTS);
if( $isSave ){
if( defined('PHPFMG_SAVE_ATTACHMENTS_DIR') ){
if( !is_dir(PHPFMG_SAVE_ATTACHMENTS_DIR) ){
$ok = @mkdir( PHPFMG_SAVE_ATTACHMENTS_DIR, 0777 );
if( !$ok ) $isSave = false;
};
};
};
$isWritable = is_writable( dirname(PHPFMG_SAVE_ATTACHMENTS_DIR) );
// parse attachments content
foreach( $_FILES as $aFile ){
$sFileName = $aFile[ "tmp_name" ] ;
$sFileRealName = phpfmg_rename_harmful($aFile[ "name" ]) ;
if( is_uploaded_file( $sFileName ) ):
$isSkip = '' != $file2link_size && ( (filesize($sFileName)/1024) > $file2link_size );
// save uploaded file
if( $isWritable && $isSave ){
$tofile = PHPFMG_SAVE_ATTACHMENTS_DIR . phpfmg_getRecordID() . '-' . basename($sFileRealName);
if( @copy( $sFileName, $tofile) ) {
$sFileName = $tofile; // to fix problem : in some windows php, the uploaded temp file might not be mailed as attachment
chmod($tofile,0777);
};
};
if( $isSkip )
continue; // mail file as link
$attachments[] = array('file' => $sFileName, 'name' => $aFile[ "name" ] );
if( !$use_phpmailer && !$isSMTP && ($fp = @fopen( $sFileName, "rb" )) ) :
$sContent = fread( $fp, filesize( $sFileName ) );
fclose($fp);
$sFName = basename( $sFileRealName ) ;
$sMIME = getMIMEType( $sFName ) ;
$bPlainText = ( $sMIME == "text/plain" ) ;
if( $bPlainText ) :
$encoding = "" ;
else:
$encoding = "Content-Transfer-Encoding: base64".PHPFMG_LNCR;
$sContent = chunk_split( base64_encode( $sContent ) );
endif;
$sEncodeBody .= PHPFMG_LNCR."--$boundary" .PHPFMG_LNCR.
"Content-Type: $sMIME;" . PHPFMG_LNCR.
"\tname=\"$sFName\"" . PHPFMG_LNCR.
$encoding .
"Content-Disposition: attachment;" . PHPFMG_LNCR.
"\tfilename=\"$sFName\"" . PHPFMG_LNCR. PHPFMG_LNCR.
$sContent . PHPFMG_LNCR ;
$nFound ++;
else:
$sError .= "<br>Failed to open file $sFileName.\n" ;
endif; // if( $fp = fopen( $sFileName, "rb" ) ) :
else:
$sError .= "<br>File $sFileName doesn't exist.\n" ;
endif; //if( file_exists( $sFileName ) ):
}; // end foreach
$sEncodeBody .= PHPFMG_LNCR.PHPFMG_LNCR."--$boundary--" ;
$GLOBALS['phpfmg_files_content'] = $sEncodeBody ;
$GLOBALS['phpfmg_nFound'] = $nFound ;
}; // if
$headers .= "MIME-Version: 1.0".PHPFMG_LNCR."Content-type: multipart/mixed;".PHPFMG_LNCR."\tboundary=\"$boundary\"";
$txtMsg = PHPFMG_LNCR."This is a multi-part message in MIME format." .PHPFMG_LNCR .
PHPFMG_LNCR."--$boundary" .PHPFMG_LNCR .
"Content-Type: {$content_type};".PHPFMG_LNCR.
"\tcharset=\"$charset\"" .PHPFMG_LNCR.PHPFMG_LNCR .
$message . PHPFMG_LNCR;
if( $noAutoAttachements ) $sEncodeBody = '' ;
$body = $nFound ? $txtMsg . $sEncodeBody : $message ;
$headers = $nFound ? $headers : $plainHeaders ;
$errmsg = "";
if( $isSMTP || $use_phpmailer ){
if( $noAutoAttachements ) $attachments = false ;
$errmsg = phpfmg_phpmailer( $to, $subject, $body, $from, $fromName, $cc , $bcc , $charset, $attachments );
}else{
if ( !mail( $to, $subject, $body, $headers ) )
$errmsg = "Failed to send mail";
};
$ok = $errmsg == "" ;
$status = $ok ? "\n[Email sent]" : "\n[{$errmsg}]" ;
phpfmg_log_mail( $to, $subject, ($ok ? 'Email sent' : 'Failed to send mail') . "\n" . ($nFound ? $headers . $txtMsg : $headers . $message), '', $type . $status ); // no log for attachments
return $sError ;
}
function phpfmg_phpmailer( $to, $subject, $message, $from, $fromName, $cc = "" , $bcc = "", $charset = "UTF-8",$attachments = false ){
$mail = new PHPMailer();
$mail->Host = PHPFMG_SMTP_HOST; // SMTP server
$mail->Username = PHPFMG_SMTP_USER;
$mail->Password = PHPFMG_SMTP_PLAIN_PASSWORD != '' ? PHPFMG_SMTP_PLAIN_PASSWORD : base64_decode(PHPFMG_SMTP_PASSWORD);
$mail->SMTPAuth = PHPFMG_SMTP_PASSWORD != "";
$mail->SMTPSecure = PHPFMG_SMTP_SECURE;
$mail->Port = PHPFMG_SMTP_PORT == "" ? 25 : PHPFMG_SMTP_PORT;
if( defined('PHPFMG_SMTP_DEBUG_LEVEL') && PHPFMG_SMTP_DEBUG_LEVEL != "" ){
$mail->SMTPDebug = (int)PHPFMG_SMTP_DEBUG_LEVEL ;
};
if( isset($GLOBALS['ReplyTo']) ) $mail->AddReplyTo($GLOBALS['ReplyTo']);
$mail->From = $from;
$mail->FromName = $fromName;
$mail->Subject = $subject;
$mail->Body = $message;
$mail->CharSet = $charset;
if( !phpfmg_is_mysite() && (defined('PHPFMG_USE_SMTP') && 'Y' == PHPFMG_USE_SMTP) ){
$mail->IsSMTP();
};
$mail->IsHTML('html' == PHPFMG_MAIL_TYPE);
$mail->AddAddress($to);
if( ''!= $cc ){
$CCs = explode(',',$cc);
foreach($CCs as $c){
$mail->AddCC( $c );
};
};
if( ''!= $bcc ){
$BCCs = explode(',',$bcc);
foreach($BCCs as $b){
$mail->AddBCC( $b );
};
};
if( is_array($attachments) ){
foreach($attachments as $f){
$mail->AddAttachment( $f['file'], basename($f['name']) );
};
};
return $mail->Send() ? "" : $mail->ErrorInfo;
}
function mailAutoResponse( $to, $from, $fields = false ){
if( !formIsEMail($to) ) return ERR_EMAIL ; // one more check for spam robot
$enable = defined('PHPFMG_RETURN_ENABLE') && PHPFMG_RETURN_ENABLE === 'Y';
$body = trim(phpfmg_auto_response_message());
if( !$enable || empty($body) ){
return false ;
};
$subject = PHPFMG_RETURN_SUBJECT;
$isHtml = 'html' == PHPFMG_MAIL_TYPE ;
$body = phpfmg_adjust_template($body);
$body = phpfmg_parse_mail_body($body,$fields);
$subject = phpfmg_parse_mail_body( $subject, $fields );
if( $isHtml ) {
$body = phpfmg_getHtmlContent( $body );
};
$body = str_replace( "0x0d", '', $body );
$body = str_replace( "0x0a", PHPFMG_LNCR, $body );
if( defined('PHPFMG_RETURN_EMAIL') && formIsEMail(PHPFMG_RETURN_EMAIL) ){
$from = PHPFMG_RETURN_EMAIL;
};
$fromHeader = ( PHPFMG_YOUR_NAME == "" ? "" : "\"".PHPFMG_YOUR_NAME . "\"" ) . " <{$from}>";
return mailAttachments( $to , $subject , $body, filterEmail($from), PHPFMG_YOUR_NAME, $fromHeader, '' , '', PHPFMG_CHARSET, 'AutoResponseEmail' );
}
function phpfmg_log_mail( $to='', $subject='', $body='', $headers = '', $type='' ){
$sep = PHPFMG_LNCR . str_repeat('----',20) . PHPFMG_LNCR ;
appendToFile( PHPFMG_EMAILS_LOGFILE, date("Y-m-d H:i:s") . "\t" . $_SERVER['REMOTE_ADDR'] . "\t{$type}" . $sep . "To: {$to}\r\nSubject: {$subject}\r\n" . $headers . $body . "<br>" . PHPFMG_LNCR . $sep . PHPFMG_LNCR ) ;
}
function phpfmg_getHtmlContent( $body ){
$html = "<html><title>Your Form Mail Content | htttp://phpfmg.sourceforge.net</title><style type='text/css'>body, td{font-family : Verdana, Arial, Helvetica, sans-serif;font-size : 13px;}</style><body>"
. $body ."</body></html>";
return $html ;
}
function phpfmg_adjust_template( $body ){
$isHtml = 'html' == PHPFMG_MAIL_TYPE ;
if( $isHtml ){
$body = preg_match( "/<[^<>]+>/", $body ) ? $body : nl2br($body);
};
return $body;
}
function phpfmg_parse_mail_body( $body, $fields = false ){
if( !is_array($fields) )
return $body ;
$yes = function_exists( 'str_ireplace' );
foreach( $fields as $name => $value ){
$body = $yes ? str_ireplace( $name, $value ,$body )
: str_replace ( $name, $value ,$body );
};
return trim($body);
}
# filter line breaks to avoid emails injecting
function filterEmail($email, $chars = ''){
$email = trim(str_replace( array("\r","\n"), '', $email ));
if( is_array($chars) ) $email = str_replace( $chars, '', $email );
$email = preg_replace( '/(cc\s*\:|bcc\s*\:)/i', '', $email );
return $email;
}
function mailReport( $content = "", $file = '' ){
$content = "
Dear Sir or Madam,
Your online form at " . HOST_NAME . PHP_SELF . " failed to save data to file. Please make sure the web user has permission to write to file \"{$file}\". If you don't know how to fix it, please forward this email to technical support team of your web hosting company or your Administrator.
PHPFMG
- PHP FormMail Generator
";
mail(PHPFMG_TO, "Error@" . HOST_NAME . PHP_SELF, $content );
}
function remove_newline( $str = "" ){
return str_replace( array("\r\n", "\r", "\n"), array('\r\n', '\r', '\n'), $str );
}
function checkPass( $form_mail = array() )
{
$names = array();
$labels = array();
foreach( $form_mail as $field ){
$type = strtolower( $field[ "type" ] );
//$value = trim( $_POST[ $field[ "name" ] ] );
$value = phpfmg_field_value( $field[ "name" ] );
$required = strtolower($field[ "required" ]) ;
$text = stripslashes( $field[ "text" ] );
// simple check the field has something keyed in.
if( !strlen($value) && ( $required == "required" ) && $type != "attachment" ){
$names[] = $field[ "name" ];
$labels[] = $text;
//return ERR_MISSING . $text ;
continue;
};
// verify the special case
if(
( strlen($value) || $type == "attachment" )
&& $required == "required"
):
switch( $type ){
case strtolower("Sender's Name") :
break;
case strtolower("Generic email"):
case strtolower("Sender's email"):
if( ! formIsEMail($value) ) {
$names[] = $field[ "name" ];
$labels[] = $text . ERR_EMAIL;
//return ERR_EMAIL . $text ;
};
// for checking entry limitation
if( $type == "sender's email" ){
$GLOBALS['sender_email'] = $value;
};
break;
case "text" :
break;
case "textarea" :
break;
case "checkbox" :
case "radio" :
break;
case "select" :
break;
case "attachment" :
$upload_file = $_FILES[ $field["name"] ][ "tmp_name" ] ;
if( ! is_uploaded_file($upload_file) ){
$names[] = $field[ "name" ];
$labels[] = $text;
//return ERR_SELECT_UPLOAD . $text;
};
break;
case strtolower("Date(MM-DD-YYYY)"):
break;
case strtolower("Date(MM-YYYY)"):
break;
case strtolower("CreditCard(MM-YYYY)"):
if( $value < date("Y-m") ) {
$names[] = $field[ "name" ];
$labels[] = $text;
//return ERR_CREDIT_CARD_EXPIRED . $text;
};
break;
case strtolower("CreditCard#"):
if( !formIsCreditNumber( $value ) ) {
$names[] = $field[ "name" ];
$labels[] = $text;
//return ERR_CREDIT_CARD_NUMBER . $text ;
};
break;
case strtolower("Time(HH:MM:SS)"):
break;
case strtolower("Time(HH:MM)"):
break;
default :
//return $sErrRequired . $form_mail[ $i ][ "text" ];
}; // switch
endif;