/
SendFriendController.php
executable file
·128 lines (98 loc) · 3.69 KB
/
SendFriendController.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
<?php
class SendFriendController extends Page_Controller {
const URLSegment = SENDFRIEND_DIR;
public function getURLSegment() {
return self::URLSegment;
}
static $allowed_actions = array(
'index', 'doSendFriend', 'SendFriendForm'
);
public function checkSentUrl($senturl) {
$pos = strpos($senturl, Director::absoluteBaseURL());
if ($pos === false) {
//print Director::absoluteBaseURL() . ' not in ' . $senturl;
Director::redirect(Director::absoluteBaseURL());
}
else {
return true;
}
}
public function init() {
parent::init();
Requirements::clear();
Requirements::insertHeadTags('<meta http-equiv="Content-language" content="' . i18n::get_locale() . '" />');
Requirements::themedCSS('sendfriend', 'screen');
}
public function index() {
$data = array();
return $this->customise($data)->renderWith(array('SendFriendController_index', 'SendFriendController'));
}
function SendFriendForm() {
$the_url = $this->request->getVar('sendurl');
$fields = new FieldList(
new TextField('YourName', _t('SendFriend.FORM_LABEL_YOURNAME', "Your name")),
new EmailField('YourEmail', _t('SendFriend.FORM_LABEL_YOUREMAIL', "Your e-mail address")),
new TextField('ToName', _t('SendFriend.FORM_LABEL_TONAME', "Name receiver")),
new EmailField('ToEmail', _t('SendFriend.FORM_LABEL_TOMAIL', "E-mail receiver")),
new TextareaField('Remarks', _t('SendFriend.FORM_LABEL_REMARKS', "Remarks")),
new CheckboxField('CopySelf', _t('SendFriend.FORM_LABEL_COPYSELF', "Copy to self")),
new HiddenField('sendurl', 'sendurl', $the_url)
);
$actions = new FieldList(
new FormAction('doSendFriend', _t('SendFriend.FORM_LABEL_SENDBUTTON', "Send"))
);
$validator = new RequiredFields('YourName', 'YourEmail', 'ToName', 'ToEmail');
$Form = new Form($this, 'SendFriendForm', $fields, $actions, $validator);
return $Form;
}
function spamCheck($field) {
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field = filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return true;
}
else
{
Director::redirect(Director::absoluteBaseURL());
return false;
}
}
//send the email
function doSendFriend($data, $form) {
$copyself = false;
$the_url = $data['sendurl'];
$yourname = trim($data['YourName']);
$youremail = trim($data['YourEmail']);
$toname = trim($data['ToName']);
$toemail = trim($data['ToEmail']);
$remarks = trim($data['Remarks']);
if(isset($data['CopySelf'])){
$copyself = $data['CopySelf'];
}
// do a check on the sent url and email addresses
$this->checkSentUrl($the_url);
$this->spamCheck($toemail);
$this->spamCheck($youremail);
$from = $yourname . '<' . $youremail . '>';
$to = $toname . '<' . $toemail . '>';
$subject = _t('SendFriend.SUBJECT','Interesting article on ') . SiteConfig::current_site_config()->getTitle();
$body = '';
$body .= _t('SendFriend.DEAR','Dear ') . $toname . ",\n\n";
$body .= $yourname . _t('SendFriend.TEXT',' would like to let you know about the following page: ')."\n\n";
$body .= $the_url . "\n\n";
if (trim($remarks) != '')
$body .= _t('SendFriend.MESSAGEFROM','Message from ') . $yourname . ":\n " . $remarks . "\n\n";
$email = new Email($from, $to, $subject, $body);
$email->sendPlain();
if ($copyself) {
$body = _t('SendFriend.COPYOFMESSAGE','--- copy of message to ') . $toname . " ---\n\n" . $body;
$email = new Email($from, $from, $subject, $body);
$email->sendPlain();
}
return $this->customise($data)->renderWith(array('SendFriendController_sent', 'SendFriendController'));
}
}