Joywork/ajax/vue_upload_clients.php
2026-05-22 21:21:54 +03:00

230 lines
8.1 KiB
PHP

<?php
require_once($_SERVER['DOCUMENT_ROOT']."/config.php");
$allowedFileTypes = [
"doc", "docx", "xls", "xlsx", "pdf",
"jpeg", "jpg", "gif", "png", "mp3", "zip", "rar"
];
if(isset($_POST['client_id'])){
if (!is_dir($_SERVER['DOCUMENT_ROOT'] . "/upload")) {
mkdir($_SERVER['DOCUMENT_ROOT'] . "/upload");
}
if (!is_dir($_SERVER['DOCUMENT_ROOT'] . "/upload/clients")) {
mkdir($_SERVER['DOCUMENT_ROOT'] . "/upload/clients");
}
$client_id = $_POST['client_id'];
$req_id = 0;
if(isset($_POST['req_id'])){
$req_id = (int)$_POST['req_id'];
}
$object_id = 0;
if(isset($_POST['object_id'])){
$object_id = (int)$_POST['object_id'];
}
$type = $_POST['type_file'];
if ($type) {
$type = strtolower($type);
}
if (!$type || !in_array($type, $allowedFileTypes)) {
$result['res'] = "Неверный тип файла";
echo json_encode($result);
exit;
}
$path = $_SERVER['DOCUMENT_ROOT']."/upload/clients/".$client_id;
$name = md5($_FILES['file']['name'].$_FILES['file']['size']."clients".$client_id.time());
if($req_id > 0){
$path = $_SERVER['DOCUMENT_ROOT']."/upload/reqs/".$req_id;
$name = md5($_FILES['file']['name'].$_FILES['file']['size']."reqs".$req_id.time());
} else if ($object_id > 0){
$path = $_SERVER['DOCUMENT_ROOT']."/upload/objects/".$object_id;
$name = md5($_FILES['file']['name'].$_FILES['file']['size']."objects".$object_id.time());
}
$result['guid'] = $name;
$result['res'] = 'failed';
if(!file_exists($path)){
mkdir($path, 0777);
}
$sql = "INSERT INTO clients_files (guid, client_id, type, name, event_id) VALUES ('{$name}', '{$client_id}', '{$type}', '{$_FILES['file']['name']}', '{$_SESSION['event_id_temp']}')";
if($req_id > 0){
$sql = "INSERT INTO clients_files (guid, req_id, type, name, event_id) VALUES ('{$name}', '{$req_id}', '{$type}', '{$_FILES['file']['name']}', '{$_SESSION['event_id_temp']}')";
} else if($object_id > 0){
$sql = "INSERT INTO clients_files (guid, object_id, type, name, event_id) VALUES ('{$name}', '{$object_id}', '{$type}', '{$_FILES['file']['name']}', '{$_SESSION['event_id_temp']}')";
}
if(!file_exists($path."/".$name.".".$type)){
if (move_uploaded_file($_FILES["file"]["tmp_name"], $path."/".$name.".".$type)) {
mysql_query($sql);
if($req_id > 0) {
//отпарвляем файл в Селектел
$token = SelectelApi::getNewToken();
$contType = "application/octet-stream";
if ($type) {
$type = strtolower($type);
}
if ($type === "doc") {
$contType = "application/msword";
}
if ($type === "docx") {
$contType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
}
if ($type === "xls") {
$contType = "application/vnd.ms-excel";
}
if ($type === "xlsx") {
$contType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
if ($type === "pdf") {
$contType = "application/pdf";
}
if ($type === "jpeg" || $type === "jpg") {
$contType = "image/jpeg";
}
if ($type === "gif") {
$contType = "image/gif";
}
if ($type === "png") {
$contType = "image/png";
}
if ($type === "mp3") {
$contType = "audio/mpeg";
}
if ($type === "zip") {
$contType = "application/zip";
}
if ($type === "rar") {
$contType = "application/vnd.rar";
}
$new_path = "/upload/reqs/". $req_id . "/" . $name . '.' . $type;
$r = SelectelApi::uploadUserFile($token, $path . "/" . $name . "." . $type, $new_path, $contType);
$result['sel'] = $r;
}
if($object_id > 0) {
//отпарвляем файл в Селектел
$token = SelectelApi::getNewToken();
$contType = "application/octet-stream";
if ($type) {
$type = strtolower($type);
}
if ($type === "doc") {
$contType = "application/msword";
}
if ($type === "docx") {
$contType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
}
if ($type === "xls") {
$contType = "application/vnd.ms-excel";
}
if ($type === "xlsx") {
$contType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
if ($type === "pdf") {
$contType = "application/pdf";
}
if ($type === "jpeg" || $type === "jpg") {
$contType = "image/jpeg";
}
if ($type === "gif") {
$contType = "image/gif";
}
if ($type === "png") {
$contType = "image/png";
}
if ($type === "mp3") {
$contType = "audio/mpeg";
}
if ($type === "zip") {
$contType = "application/zip";
}
if ($type === "rar") {
$contType = "application/vnd.rar";
}
$new_path = "/upload/objects/". $object_id . "/" . $name . '.' . $type;
$r = SelectelApi::uploadUserFile($token, $path . "/" . $name . "." . $type, $new_path, $contType);
$result['sel'] = $r;
}
if($client_id > 0) {
//отпарвляем файл в Селектел
$token = SelectelApi::getNewToken();
$contType = "application/octet-stream";
if ($type) {
$type = strtolower($type);
}
if ($type === "doc") {
$contType = "application/msword";
}
if ($type === "docx") {
$contType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
}
if ($type === "xls") {
$contType = "application/vnd.ms-excel";
}
if ($type === "xlsx") {
$contType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
if ($type === "pdf") {
$contType = "application/pdf";
}
if ($type === "jpeg" || $type === "jpg") {
$contType = "image/jpeg";
}
if ($type === "gif") {
$contType = "image/gif";
}
if ($type === "png") {
$contType = "image/png";
}
if ($type === "mp3") {
$contType = "audio/mpeg";
}
if ($type === "zip") {
$contType = "application/zip";
}
if ($type === "rar") {
$contType = "application/vnd.rar";
}
$new_path = "/upload/clients/". $client_id . "/" . $name . '.' . $type;
$r = SelectelApi::uploadUserFile($token, $path . "/" . $name . "." . $type, $new_path, $contType);
$result['sel'] = $r;
}
$result['res'] = "done";
echo json_encode($result);
exit;
}
} else {
mysql_query($sql);
$result['res'] = "done";
echo json_encode($result);
exit;
}
exit();
}