Files
crm.clientright.ru/formss/server.php

190 lines
8.2 KiB
PHP
Raw Normal View History

<?php
header('Access-Control-Allow-Origin: *');
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8';
$mail->isHTML(true);
try {
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'smtp.fvkorobkov.ru';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'ask@fvkorobkov.ru';
$mail->Password = 'G59UQwYaSl';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->setFrom('ask@fvkorobkov.ru', 'Робот-Клиентправ');
$mail->addAddress('help@clientright.ru', 'Заявка с сайта clientright.ru');
$mail->isHTML(true);
$inputsArray = $_POST['files_names'];
$inputLabel = $_POST['docs_names'];
$files_array = [];
$appends=$_POST['appends'];
$lastname = str_replace(' ', '',$_POST['lastname']);
if($inputsArray) {
foreach($inputsArray as $index => $inputsArray_item) {
for($i=0;$i<10;$i++) {
if (!isset($_FILES[$inputsArray_item.'-'.$i])) {
$error = 'Файл не загружен.';
break;
} else {
// echo 'Фaйл с именем '. $inputsArray_item." существует";
// die;
$file = $_FILES[$inputsArray_item.'-'.$i];
$allow = array();
$deny = array(
'phtml', 'php', 'php3', 'php4', 'php5', 'php6', 'php7', 'phps', 'cgi', 'pl', 'asp',
'aspx', 'shtml', 'shtm', 'htaccess', 'htpasswd', 'ini', 'log', 'sh', 'js', 'html',
'htm', 'css', 'sql', 'spl', 'scgi', 'fcgi', 'exe'
);
$path = __DIR__ . '/uploads/';
$error = $success = '';
if (!empty($file['error']) || empty($file['tmp_name'])) {
$error = 'Не удалось загрузить файл.';
} elseif ($file['tmp_name'] == 'none' || !is_uploaded_file($file['tmp_name'])) {
$error = 'Не удалось загрузить файл.';
} else {
$pattern = "[^a-zа-яё0-9,~!@#%^-_\$\?\(\)\{\}\[\]\.]";
$name = mb_eregi_replace($pattern, '-', $file['name']);
$name = mb_ereg_replace('[-]+', '-', $name);
$parts = pathinfo($name);
if (empty($name) || empty($parts['extension'])) {
$error = 'Недопустимый тип файла';
} elseif (!empty($allow) && !in_array(strtolower($parts['extension']), $allow)) {
$error = 'Недопустимый тип файла';
} elseif (!empty($deny) && in_array(strtolower($parts['extension']), $deny)) {
$error = 'Недопустимый тип файла';
} else {
if (move_uploaded_file($file['tmp_name'], $path . $name)) {
$fullpath = $_SERVER['HTTP_REFERER']. '/uploads/' . $name;
$mail->addAttachment('uploads/'.$name);
if(strtolower($parts['extension']) != 'pdf') {
$oldfile = 'uploads/'.$name;
$newfile = 'uploads/'.$name.'_'.date('m-d-Y-H-i-s').'.pdf';
exec("convert ".$oldfile." ".$newfile." ");
$pdfFiles[] = $newfile;
} else {
$pdfFiles[] = 'uploads/' . $name; // 'uploads/'
}
//exec("convert uploads/".$name." uploads/".$name.'_'.date('m-d-Y-H-i-s').".pdf");
//$success = '<p style="color: green">Файл «' . $name . '» успешно загружен.</p><a href="'.$fullpath.'">Скачать</a>';
} else {
$error = 'Не удалось загрузить файл.';
}
}
}
}
}
$new = 'uploads/'.$inputLabel[$index].'_'.$lastname.'_'.count($pdfFiles).'_стр_'.date('d-m-Y').'.pdf';// 'uploads/'
$cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=".$new." ".implode(" ", $pdfFiles);
shell_exec($cmd);
if(filesize($new) > 5000) {
$mail->addAttachment($new);
$files_array = array_merge($files_array, array($inputsArray_item => new CURLFile(realpath($new))));
$pdfFiles = [];
}
}
}
$new_post = array(
'__vtrftk' => 'sid:ec649134ad232e44c3ad71bbd321cee986f05545,1688385374',
'publicid' => '3ddc71c2d79ef101c09b0d4e9c6bd08b',
'urlencodeenable' => '1',
'name' => 'websiteticket',
);
foreach($appends as $key => $itemjson){
$item=json_decode($itemjson);
$new_post[$item->crm_name] = $item->field_val;
}
$final_post = array_merge($new_post, $files_array);
$curl_connection =
curl_init('https://crm.clientright.ru/modules/Webforms/capture.php');
curl_setopt($curl_connection, CURLOPT_POST, 1);
curl_setopt($curl_connection, CURLOPT_HEADER, 0);
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
// Content-Type: multipart/form-data автоматически добавляется cURL с правильным boundary
// curl_setopt($curl_connection, CURLOPT_HTTPHEADER,array('Content-Type: multipart/form-data'));
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $final_post);
$result = curl_exec($curl_connection);
curl_close($curl_connection);
$mail->Subject = 'Заявкас с сайта clientright.ru';
$mail->Body = 'Пользователь заполнил форму! </br> </br>';
foreach($new_post as $key => $value) {
$mail->Body .= $key.' : '.$value.'<br>';
}
$mail->send();
echo 'Message has been sent'. $error;
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
$files = glob('uploads/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file)) {
unlink($file); // delete file
}
}
function translit($value)
{
$converter = array(
'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
'е' => 'e', 'ё' => 'e', 'ж' => 'zh', 'з' => 'z', 'и' => 'i',
'й' => 'y', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n',
'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't',
'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch',
'ш' => 'sh', 'щ' => 'sch', 'ь' => '', 'ы' => 'y', 'ъ' => '',
'э' => 'e', 'ю' => 'yu', 'я' => 'ya',
'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
'Е' => 'E', 'Ё' => 'E', 'Ж' => 'Zh', 'З' => 'Z', 'И' => 'I',
'Й' => 'Y', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N',
'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T',
'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Ch',
'Ш' => 'Sh', 'Щ' => 'Sch', 'Ь' => '', 'Ы' => 'Y', 'Ъ' => '',
'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya',
);
$value = strtr($value, $converter);
return preg_replace('/\s+/', '', $value);
}