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

161 lines
6.3 KiB
PHP
Raw Normal View History

<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers", "origin, x-requested-with, content-type");
header("Access-Control-Allow-Methods", "GET, POST");
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
$result=array("success"=>"false","message"=>"", "result" => "");
$lastname = str_replace(' ', '_',$_POST['lastname']);
$inputsArray = $_POST['files_names'];
$inputLabel = $_POST['docs_names'];
$sub_dir = !empty($_POST['sub_dir']) ? $_POST['sub_dir'] : '';
$pdf_page_counts=array();
$img_page_counts=0;
if($inputsArray) {
foreach($inputsArray as $index => $inputsArray_item) {
for($i=0;$i<10;$i++) {
if (!isset($_FILES[$inputsArray_item.'-'.$i])) {
$error = 'Файл не загружен.';
break;
} else {
$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/'.$sub_dir;
if(!is_dir($path)){
mkdir($path, 0755);
}
$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);
// $name = trim(preg_replace('/\s*\([^)]*\)/', '', $name));
$pattern = "[^a-zа-яё0-9.]";
$name = mb_eregi_replace($pattern,'_', $file['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/'.$sub_dir.'/'. $name;
if(strtolower($parts['extension']) != 'pdf') {
$oldfile = 'uploads/'.$sub_dir.'/'.$name;
$newfile = 'uploads/'.$sub_dir.'/'.$name.'_'.date('m-d-Y-H-i-s').'.pdf';
exec("convert ".$oldfile." ".$newfile." ");
$pdfFiles[] = $newfile;
$img_page_counts++;
} else {
$pdfFiles[] = 'uploads/'.$sub_dir.'/'.$name;
$pdf_page_counts[]=get_pdf_count('uploads/'.$sub_dir.'/'.$name);
}
//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 = 'Не удалось загрузить файл.';
}
}
}
}
}
$pages_count=array_sum($pdf_page_counts)+$img_page_counts;
$new = 'uploads/'.$sub_dir.'/'.translit($inputLabel[$index]).'_'.date('m-d-Y-H-i-s').'_'.translit($lastname).'_'.$pages_count.'_CTP.pdf';
$cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=".$new." ".implode(" ", $pdfFiles);
shell_exec($cmd);
}
}
function get_pdf_count($target_pdf){
$cmd = sprintf("identify %s", $target_pdf);
exec($cmd, $output);
$pages = count($output);
return $pages;
}
if($new) {
$result['success']="true";
$result['message']=$new;
$result['sub_dir']=$sub_dir;
}
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);
}
//logi
error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('log_errors','on');
ini_set('error_log', __DIR__ . '/logs/php-errors.log');
ob_start();
foreach (getallheaders() as $name => $value) {
echo "$name: $value\n";
}
$log = date('Y-m-d H:i:s') . PHP_EOL . ob_get_clean() . PHP_EOL;
file_put_contents(__DIR__ . '/log-headers.txt', $log, FILE_APPEND);
// logi end
echo json_encode($result);
?>