'https://www.google.com/recaptcha/api/siteverify',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => ['secret' => $recaptcha_secret, 'response' => $recaptcha_response],
CURLOPT_RETURNTRANSFER => true ]);
$recaptcha = curl_exec($ch);
curl_close($ch);
$recaptcha = json_decode($recaptcha);
if ($recaptcha->success && $recaptcha->action == 'contact' && $recaptcha->score >= 0.5)
{
$recaptcha_valid = true;
}
}
if (!$recaptcha_valid)
{
header('Location: ./formular-error.php');
exit;
}
}
function ValidateEmail($email)
{
$pattern = '/^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i';
return preg_match($pattern, $email);
}
function RecursiveMkdir($path)
{
if (!file_exists($path))
{
RecursiveMkdir(dirname($path));
mkdir($path, 0777);
}
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['formid']) && $_POST['formid'] == 'contact')
{
$mailto = 'office@1print.ro';
$mailfrom = isset($_POST['email']) ? $_POST['email'] : $mailto;
$mailcc = 'aidtprint@gmail.com';
$mailbcc = 'ionescualexandru2003@yahoo.com';
$subject = 'Formular de contact';
$message = '';
$success_url = './succes.php';
$error_url = './contact-error.php';
$eol = "\n";
$error = '';
$internalfields = array ("submit", "reset", "send", "filesize", "formid", "captcha_code", "recaptcha_challenge_field", "recaptcha_response_field", "g-recaptcha-response");
$max_filesize = 51200*1024;
$upload_folder = "upload";
$upload_folder = "http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME'])."/".$upload_folder;
$boundary = md5(uniqid(time()));
$header = 'From: '.$mailfrom.$eol;
$header .= 'Reply-To: '.$mailfrom.$eol;
$header .= 'Cc: '.$mailcc.$eol;
$header .= 'Bcc: '.$mailbcc.$eol;
$header .= 'MIME-Version: 1.0'.$eol;
$header .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'"'.$eol;
$header .= 'X-Mailer: PHP v'.phpversion().$eol;
try
{
if (!ValidateEmail($mailfrom))
{
$error .= "The specified email address (" . $mailfrom . ") is invalid!\n
";
throw new Exception($error);
}
$prefix = rand(111111, 999999);
$i = 0;
while (list ($key, $val) = each ($_FILES))
{
if ($_FILES[$key]['name'] != "" and file_exists($_FILES[$key]['tmp_name']) and $_FILES[$key]['size'] > 0)
{
$upload_DstName[$i] = $prefix . "_" . str_replace(" ", "_", $_FILES[$key]['name']);
$upload_SrcName[$i] = $_FILES[$key]['name'];
$upload_Size[$i] = ($_FILES[$key]['size']);
$upload_Temp[$i] = ($_FILES[$key]['tmp_name']);
$upload_URL[$i] = "$upload_folder/$upload_DstName[$i]";
$upload_FieldName[$i] = $key;
}
if ($upload_Size[$i] >= $max_filesize)
{
$error .= "The size of $key (file: $upload_SrcName[$i]) is bigger than the allowed " . $max_filesize/1024 . " Kbytes!\n";
throw new Exception($error);
}
$i++;
}
$uploadfolder = basename($upload_folder);
for ($i = 0; $i < count($upload_DstName); $i++)
{
$uploadFile = $uploadfolder . "/" . $upload_DstName[$i];
if (!is_dir(dirname($uploadFile)))
{
RecursiveMkdir(dirname($uploadFile));
}
move_uploaded_file($upload_Temp[$i] , $uploadFile);
chmod($uploadFile, 0644);
$name = "$" . $upload_FieldName[$i];
$message = str_replace($name, $upload_URL[$i], $message);
}
$message .= $eol;
foreach ($_POST as $key => $value)
{
if (!in_array(strtolower($key), $internalfields))
{
if (!is_array($value))
{
$message .= ucwords(str_replace("_", " ", $key)) . " : " . $value . $eol;
}
else
{
$message .= ucwords(str_replace("_", " ", $key)) . " : " . implode(",", $value) . $eol;
}
}
}
if (count($upload_SrcName) > 0)
{
$message .= "\nThe following files have been uploaded:\n";
for ($i = 0; $i < count($upload_SrcName); $i++)
{
$message .= $upload_SrcName[$i] . ": " . $upload_URL[$i] . "\n";
}
}
$body = 'This is a multi-part message in MIME format.'.$eol.$eol;
$body .= '--'.$boundary.$eol;
$body .= 'Content-Type: text/plain; charset=ISO-8859-1'.$eol;
$body .= 'Content-Transfer-Encoding: 8bit'.$eol;
$body .= $eol.stripslashes($message).$eol;
$body .= '--'.$boundary.'--'.$eol;
if ($mailto != '')
{
mail($mailto, $subject, $body, $header);
}
header('Location: '.$success_url);
}
catch (Exception $e)
{
$errorcode = file_get_contents($error_url);
$replace = "##error##";
$errorcode = str_replace($replace, $e->getMessage(), $errorcode);
echo $errorcode;
}
exit;
}
?>