I have a form, which uses AJAX to send POST data to the following controller method:
public function action_sendmail() {
$jsonHelper=Loader::helper('json');
$answer = (int) $_POST['security'];
if($answer <= 0)
$answer = 1;
if($answer == 4){
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
$name = strip_tags($name);
$email = strip_tags($email);
$message = strip_tags($message);
$name = str_replace(array("\r", "\n", "%0a", "%0d"), '', stripslashes($name));
$email = str_replace(array("\r", "\n", "%0a", "%0d"), '', stripslashes($email));
$message = str_replace(array("\r", "\n", "%0a", "%0d"), '', stripslashes($message));
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
echo $jsonHelper->encode(t("Wrong email address."));
die;
}
if(preg_match("/^[+]?[0-9]*$/", $_POST['phone'])) {
$phone = strip_tags($_POST['phone']);
}
else {
echo $jsonHelper->encode(t("Wrong phone number format."));
die;
}
$message = t('Phone number').": ".$phone."\n\n".$message;
$mail = Loader::helper('mail');
$mail->setBody($message);
$mail->from($email, $name);
$mail->to("[email protected]");
$mail->to($email);
$mail->setSubject(t('Example subject'));
$sent = true;
$mail->sendMail();
if($sent) {
echo $jsonHelper->encode("sent");
die;
}
else {
echo $jsonHelper->encode(t("Could not send mail. Please try again."));
die;
}
}
else{
echo $jsonHelper->encode(t("Wrong security answer!"));
die;
}
}
The script sanitizes different attributes (name, email and the message) and sends an email to a specified address and the one from the HTML form. My question: Is the form secure?