175 lines
5.1 KiB
PHP
175 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Mail;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Carbon;
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class MailController extends Controller
|
|
{
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$domain_list = config('mail.domain_list');
|
|
$block_prefix = config('mail.block_prefix');
|
|
$rand_prefix = Str::random(8);
|
|
$key = Crypt::encryptString(Carbon::now()->format('Y-m-d H:i:s'));
|
|
return view('welcome', [
|
|
'domain_list' => $domain_list,
|
|
'block_prefix' => $block_prefix,
|
|
'rand_prefix' => $rand_prefix,
|
|
'key' => $key,
|
|
]);
|
|
}
|
|
|
|
public function get(Request $request)
|
|
{
|
|
$email = $request->input('email');
|
|
$key = $request->input('key', '');
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
'key' => [
|
|
'required',
|
|
'string',
|
|
],
|
|
'email' => [
|
|
'required',
|
|
'email',
|
|
function ($attribute, $value, $fail) {
|
|
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
|
|
return $fail('The ' . $attribute . ' must be a valid email address.');
|
|
}
|
|
|
|
list($prefix, $domain) = explode('@', $value);
|
|
$length = mb_strlen($prefix);
|
|
|
|
if ($length < 5 || $length > 32) {
|
|
return $fail('The email prefix must be between 5 and 32 characters.');
|
|
}
|
|
|
|
if (in_array($prefix, config('mail.block_prefix'))) {
|
|
return $fail('The email prefix is blocked.');
|
|
}
|
|
},
|
|
],
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return abort(400);
|
|
}
|
|
|
|
$now_time = Carbon::now();
|
|
|
|
try {
|
|
$key = Crypt::decryptString($key);
|
|
} catch (\Throwable $th) {
|
|
return abort(400);
|
|
}
|
|
|
|
$key = Carbon::parse($key);
|
|
if ($key->diffInMinutes($now_time) > 5) {
|
|
$key = $now_time;
|
|
}
|
|
|
|
$to_hash = hash('sha256', $email);
|
|
|
|
$new_email_list = Mail::where([
|
|
['to_hash', '=', $to_hash],
|
|
['created_at', '>=', $key],
|
|
])
|
|
->select([
|
|
'id as key',
|
|
'from',
|
|
'to',
|
|
'title',
|
|
'received_at',
|
|
'created_at',
|
|
])
|
|
->orderBy('received_at', 'asc')
|
|
->get();
|
|
|
|
foreach ($new_email_list as $key => $value) {
|
|
$value->key = Crypt::encryptString($value->key);
|
|
}
|
|
|
|
return response()->json([
|
|
'code' => 1,
|
|
'msg' => '',
|
|
'data' => [
|
|
'list' => $new_email_list,
|
|
'new_key' => Crypt::encryptString($now_time->format('Y-m-d H:i:s')),
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function put(Request $request)
|
|
{
|
|
$key = $request->input('key');
|
|
$from = $request->input('from', '');
|
|
$to = $request->input('to', '');
|
|
$title = $request->input('title', '');
|
|
$body = $request->input('body', '');
|
|
$from_addr = $request->input('from_addr', '');
|
|
$from_protocol = $request->input('from_protocol', '');
|
|
$received_at = $request->input('received_at');
|
|
if ($key != config('app.api_key')) {
|
|
return abort(401);
|
|
}
|
|
|
|
$mail = new Mail;
|
|
|
|
$mail->from = $from;
|
|
$mail->from_hash = hash('sha256', $from);
|
|
$mail->to = $to;
|
|
$mail->to_hash = hash('sha256', $to);
|
|
|
|
$mail->title = $title;
|
|
$mail->body = $body;
|
|
$mail->from_addr = $from_addr;
|
|
$mail->from_protocol = $from_protocol;
|
|
$mail->received_at = $received_at;
|
|
$mail->save();
|
|
|
|
return response()->json([
|
|
'code' => 1,
|
|
'msg' => 'success',
|
|
'data' => new \stdClass,
|
|
]);
|
|
}
|
|
|
|
public function info(Request $request, $key)
|
|
{
|
|
try {
|
|
$id = Crypt::decryptString($key);
|
|
} catch (\Throwable $th) {
|
|
return abort(400);
|
|
}
|
|
|
|
$mail_info = Mail::find($id);
|
|
if (empty($mail_info)) {
|
|
return abort(404);
|
|
}
|
|
|
|
$title = str_replace(["\n", "\r", "\t"], '', strip_tags($mail_info->title));
|
|
$body = preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', "", $mail_info->body);
|
|
|
|
return response()->json([
|
|
'code' => 1,
|
|
'msg' => '',
|
|
'data' => [
|
|
'from' => $mail_info->from,
|
|
'title' => base64_encode($title),
|
|
'body' => base64_encode($body),
|
|
'created_at' => $mail_info->created_at,
|
|
'received_at' => $mail_info->received_at,
|
|
],
|
|
]);
|
|
|
|
}
|
|
}
|