211 lines
8.4 KiB
PHP
211 lines
8.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers;
|
||
|
|
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
|
||
|
|
use App\RemoteContact;
|
||
|
|
use App\RemoteTransfer;
|
||
|
|
use App\Permission;
|
||
|
|
use App\Notifications\DocumentNotification;
|
||
|
|
use Notification;
|
||
|
|
use Validator;
|
||
|
|
use File;
|
||
|
|
use Hash;
|
||
|
|
use GuzzleHttp\Client;
|
||
|
|
|
||
|
|
class ApiController extends Controller
|
||
|
|
{
|
||
|
|
static $timeout = 20;
|
||
|
|
private function isAllowed($login, $pass, $checkIP=false)
|
||
|
|
{
|
||
|
|
$remote_contact = RemoteContact::where('login', $login)->where('direction', 0)->where('password', $pass)->first();
|
||
|
|
if(!$remote_contact)
|
||
|
|
return false;
|
||
|
|
|
||
|
|
// if($checkIP)
|
||
|
|
// if(!isset(static::$ip[$login]) || !(static::$ip[$login] == request()->getClientIp()))
|
||
|
|
// return false;
|
||
|
|
|
||
|
|
return $remote_contact;
|
||
|
|
}
|
||
|
|
|
||
|
|
private function rules()
|
||
|
|
{
|
||
|
|
return Validator::make(request()->all(), [
|
||
|
|
'registration_number' => 'required',
|
||
|
|
'registration_date' => 'required | date | date_format:Y-m-d',
|
||
|
|
'file' => 'required|mimes:doc,docx,xls,xlsx,pdf,txt,jpeg,jpg,png|max:20480',
|
||
|
|
'description' => 'nullable | max:255',
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
private function checkStatusRules()
|
||
|
|
{
|
||
|
|
return Validator::make(request()->all(), [
|
||
|
|
'receipt' => 'required',
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public function receiveDocument(Request $request)
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
if(!$request->hasHeader('login') || !$request->hasHeader('password'))
|
||
|
|
return response()->json(['success' => false, 'error' => 'login and password required'], 401);
|
||
|
|
|
||
|
|
$remote_contact = $this->isAllowed($request->header()['login'][0], $request->header()['password'][0]);
|
||
|
|
if( $remote_contact == false)
|
||
|
|
return response()->json(['success' => false, 'error' => 'Unauthorized'], 401);
|
||
|
|
|
||
|
|
$validate = $this->rules();
|
||
|
|
if($validate->fails())
|
||
|
|
return response()->json(['success' => false, 'error'=>$validate->errors()], 400);
|
||
|
|
|
||
|
|
$transfer = new RemoteTransfer;
|
||
|
|
$transfer->receipt = \sha1(rand(99999, 9999999999));
|
||
|
|
$transfer->login = $request->header()['login'][0];
|
||
|
|
$transfer->direction = 0;
|
||
|
|
$transfer->status = 3;
|
||
|
|
$transfer->send_at = now();
|
||
|
|
// $transfer->description = $request->input('description');
|
||
|
|
$transfer->content = json_encode(['registration_number' => $request->input('registration_number') ?? '', 'registration_date'=>$request->input('registration_date') ?? '', 'topic'=>$request->input('topic') ?? '']);
|
||
|
|
$transfer->contact_id = $remote_contact->contact_id;
|
||
|
|
|
||
|
|
if ($request->hasFile('file'))
|
||
|
|
{
|
||
|
|
if(!empty($request->file()) && is_array($request->file()))
|
||
|
|
{
|
||
|
|
$place_of_the_documents='documents/'.date('Y').'/'.'remote';
|
||
|
|
$files = $request->file();
|
||
|
|
foreach($files as $key=>$file)
|
||
|
|
{
|
||
|
|
if(!File::isDirectory(public_path().'/'.$place_of_the_documents))
|
||
|
|
File::makeDirectory(public_path().'/'.$place_of_the_documents, 0777, true, true);
|
||
|
|
|
||
|
|
$document_name = 'remote' . '_' . md5($file->getClientOriginalName() . microtime()) .'.'. $file->getClientOriginalExtension();;
|
||
|
|
$file->move(public_path($place_of_the_documents), $document_name);
|
||
|
|
|
||
|
|
$transfer->place_of_the_documents = $place_of_the_documents;
|
||
|
|
$transfer->document_name = $document_name;
|
||
|
|
|
||
|
|
if(!in_array($file->getClientOriginalExtension(), ['pdf', 'zip', 'tar', 'rar', 'exe', 'gz']))
|
||
|
|
{
|
||
|
|
shell_exec('export HOME=/tmp && /usr/bin/libreoffice --headless --convert-to pdf ' . public_path($place_of_the_documents) . '/' . $document_name . ' --outdir ' . public_path($place_of_the_documents));
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
if($transfer->save())
|
||
|
|
{
|
||
|
|
$link = route('transfers.show', base64_encode($transfer->id));
|
||
|
|
|
||
|
|
$permitted_users = Permission::getPermittedUsersList(13);
|
||
|
|
foreach($permitted_users as $puser)
|
||
|
|
{
|
||
|
|
Notification::send($puser, new DocumentNotification($transfer->id, "api", "received", $link));
|
||
|
|
}
|
||
|
|
return response()->json(['success' => true, 'data' => ['receipt' => $transfer->receipt]], 200);
|
||
|
|
}
|
||
|
|
|
||
|
|
} catch (\Throwable $th) {
|
||
|
|
\Log::info(['error' => $th->getMessage()]);
|
||
|
|
return response()->json(['success' => false, 'error' => 'unknown error'], 500);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function checkStatus(Request $request)
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
if(!$request->hasHeader('login') || !$request->hasHeader('password'))
|
||
|
|
return response()->json(['success' => false, 'error' => 'login and password required'], 401);
|
||
|
|
|
||
|
|
$remote_contact = $this->isAllowed($request->header()['login'][0], $request->header()['password'][0]);
|
||
|
|
if( $remote_contact == false)
|
||
|
|
return response()->json(['success' => false, 'error' => 'Unauthorized'], 401);
|
||
|
|
|
||
|
|
$validate = $this->checkStatusRules();
|
||
|
|
if($validate->fails())
|
||
|
|
return response()->json(['success' => false, 'error'=>$validate->errors()], 400);
|
||
|
|
|
||
|
|
$transfer = RemoteTransfer::where('receipt', $request->input('receipt'))->first();
|
||
|
|
if(!$transfer)
|
||
|
|
return response()->json(['success' => false, 'error'=>'not found'], 404);
|
||
|
|
|
||
|
|
|
||
|
|
return response()->json(['success' => true, 'data' => ['status' => $transfer->status]], 200);
|
||
|
|
|
||
|
|
} catch (\Throwable $th) {
|
||
|
|
\Log::info(['error' => $th->getMessage()]);
|
||
|
|
return response()->json(['success' => false, 'error' => 'unknown error'], 500);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function sendDocuments($id=null)
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$remotetransfers = ($id == null) ? RemoteTransfer::where('direction', 1)->whereIn('status', [1, 4])->get() : RemoteTransfer::where('id', $id)->get();
|
||
|
|
if($remotetransfers)
|
||
|
|
{
|
||
|
|
foreach($remotetransfers as $remotetransfer)
|
||
|
|
{
|
||
|
|
$this->sendDocument($remotetransfer);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} catch (\Throwable $th) {
|
||
|
|
\Log::info(['error' => $th->getMessage()]);
|
||
|
|
return response()->json(['success' => false, 'data' => 'unknown error'], 500);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getStatus(Request $request)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
private function sendDocument(RemoteTransfer $remotetransfer)
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$remotecontact = RemoteContact::where('archive', 0)->where('direction', 1)->where('contact_id', $remotetransfer->contact_id)->whereHas('remotecontactapis')->with('remotecontactapis')->first();
|
||
|
|
$api = $remotecontact->remotecontactapis->first();
|
||
|
|
|
||
|
|
$file_path = public_path().'/'.$remotetransfer->place_of_the_documents.'/'.$remotetransfer->document_name;
|
||
|
|
$client = new Client(['base_uri' => $api->connection_string, 'verify'=>false, 'timeout'=>static::$timeout, 'headers'=>['login'=>$remotecontact->login, 'password'=>$remotecontact->password]]);
|
||
|
|
|
||
|
|
$data[] = [
|
||
|
|
'name' => 'file',
|
||
|
|
'contents' => fopen($file_path, 'rb'),
|
||
|
|
'filename' => $remotetransfer->document_name,
|
||
|
|
];
|
||
|
|
|
||
|
|
$contents = json_decode($remotetransfer->content);
|
||
|
|
foreach($contents as $key=>$value)
|
||
|
|
{
|
||
|
|
$data[] =['name' => $key, 'contents'=>$value];
|
||
|
|
}
|
||
|
|
|
||
|
|
$response = $client->request($api->connection_method, '',[
|
||
|
|
'multipart' => $data,
|
||
|
|
]);
|
||
|
|
|
||
|
|
if($response->getStatusCode() == 200)
|
||
|
|
{
|
||
|
|
$result = json_decode($response->getBody()->getContents());
|
||
|
|
if($result->success == "true")
|
||
|
|
{
|
||
|
|
$remotetransfer->receipt = $result->data->receipt;
|
||
|
|
$remotetransfer->status = 2;
|
||
|
|
$remotetransfer->save();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
\Log::info(['success'=>false, 'msg'=>'connection refused', 'info'=>$e->getMessage()]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|