-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
contact.php
229 lines (190 loc) · 6.24 KB
/
contact.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
<?php
/**
* @package Joomla.Site
* @subpackage com_contact
*
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
/**
* Controller for single contact view
*
* @since 1.5.19
*/
class ContactControllerContact extends JControllerForm
{
/**
* Method to get a model object, loading it if required.
*
* @param string $name The model name. Optional.
* @param string $prefix The class prefix. Optional.
* @param array $config Configuration array for model. Optional.
*
* @return JModelLegacy The model.
*
* @since 1.6.4
*/
public function getModel($name = '', $prefix = '', $config = array('ignore_request' => true))
{
return parent::getModel($name, $prefix, array('ignore_request' => false));
}
/**
* Method to submit the contact form and send an email.
*
* @return boolean True on success sending the email. False on failure.
*
* @since 1.5.19
*/
public function submit()
{
// Check for request forgeries.
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
$app = JFactory::getApplication();
$model = $this->getModel('contact');
$params = JComponentHelper::getParams('com_contact');
$stub = $this->input->getString('id');
$id = (int) $stub;
// Get the data from POST
$data = $this->input->post->get('jform', array(), 'array');
$contact = $model->getItem($id);
$params->merge($contact->params);
// Check for a valid session cookie
if ($params->get('validate_session', 0))
{
if (JFactory::getSession()->getState() != 'active')
{
JError::raiseWarning(403, JText::_('COM_CONTACT_SESSION_INVALID'));
// Save the data in the session.
$app->setUserState('com_contact.contact.data', $data);
// Redirect back to the contact form.
$this->setRedirect(JRoute::_('index.php?option=com_contact&view=contact&id=' . $stub, false));
return false;
}
}
// Contact plugins
JPluginHelper::importPlugin('contact');
$dispatcher = JEventDispatcher::getInstance();
// Validate the posted data.
$form = $model->getForm();
if (!$form)
{
JError::raiseError(500, $model->getError());
return false;
}
$validate = $model->validate($form, $data);
if ($validate === false)
{
// Get the validation messages.
$errors = $model->getErrors();
// Push up to three validation messages out to the user.
for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++)
{
if ($errors[$i] instanceof Exception)
{
$app->enqueueMessage($errors[$i]->getMessage(), 'warning');
}
else
{
$app->enqueueMessage($errors[$i], 'warning');
}
}
// Save the data in the session.
$app->setUserState('com_contact.contact.data', $data);
// Redirect back to the contact form.
$this->setRedirect(JRoute::_('index.php?option=com_contact&view=contact&id=' . $stub, false));
return false;
}
// Validation succeeded, continue with custom handlers
$results = $dispatcher->trigger('onValidateContact', array(&$contact, &$data));
foreach ($results as $result)
{
if ($result instanceof Exception)
{
return false;
}
}
// Passed Validation: Process the contact plugins to integrate with other applications
$dispatcher->trigger('onSubmitContact', array(&$contact, &$data));
// Send the email
$sent = false;
if (!$params->get('custom_reply'))
{
$sent = $this->_sendEmail($data, $contact, $params->get('show_email_copy'));
}
// Set the success message if it was a success
if (!($sent instanceof Exception))
{
$msg = JText::_('COM_CONTACT_EMAIL_THANKS');
}
else
{
$msg = '';
}
// Flush the data from the session
$app->setUserState('com_contact.contact.data', null);
// Redirect if it is set in the parameters, otherwise redirect back to where we came from
if ($contact->params->get('redirect'))
{
$this->setRedirect($contact->params->get('redirect'), $msg);
}
else
{
$this->setRedirect(JRoute::_('index.php?option=com_contact&view=contact&id=' . $stub, false), $msg);
}
return true;
}
/**
* Method to get a model object, loading it if required.
*
* @param array $data The data to send in the email.
* @param stdClass $contact The user information to send the email to
* @param boolean $copy_email_activated True to send a copy of the email to the user.
*
* @return boolean True on success sending the email, false on failure.
*
* @since 1.6.4
*/
private function _sendEmail($data, $contact, $copy_email_activated)
{
$app = JFactory::getApplication();
if ($contact->email_to == '' && $contact->user_id != 0)
{
$contact_user = JUser::getInstance($contact->user_id);
$contact->email_to = $contact_user->get('email');
}
$mailfrom = $app->get('mailfrom');
$fromname = $app->get('fromname');
$sitename = $app->get('sitename');
$name = $data['contact_name'];
$email = JStringPunycode::emailToPunycode($data['contact_email']);
$subject = $data['contact_subject'];
$body = $data['contact_message'];
// Prepare email body
$prefix = JText::sprintf('COM_CONTACT_ENQUIRY_TEXT', JUri::base());
$body = $prefix . "\n" . $name . ' <' . $email . '>' . "\r\n\r\n" . stripslashes($body);
$mail = JFactory::getMailer();
$mail->addRecipient($contact->email_to);
$mail->addReplyTo($email, $name);
$mail->setSender(array($mailfrom, $fromname));
$mail->setSubject($sitename . ': ' . $subject);
$mail->setBody($body);
$sent = $mail->Send();
// If we are supposed to copy the sender, do so.
// Check whether email copy function activated
if ($copy_email_activated == true && !empty($data['contact_email_copy']))
{
$copytext = JText::sprintf('COM_CONTACT_COPYTEXT_OF', $contact->name, $sitename);
$copytext .= "\r\n\r\n" . $body;
$copysubject = JText::sprintf('COM_CONTACT_COPYSUBJECT_OF', $subject);
$mail = JFactory::getMailer();
$mail->addRecipient($email);
$mail->addReplyTo(array($email, $name));
$mail->setSender(array($mailfrom, $fromname));
$mail->setSubject($copysubject);
$mail->setBody($copytext);
$sent = $mail->Send();
}
return $sent;
}
}