0, 'time' => time()])); 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'); $email = strtolower($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); } $where = []; $to_hash = hash('sha256', $email); $where[] = ['to_hash', '=', $to_hash]; try { $key = Crypt::decryptString($key); $key = json_decode($key, true); if (!empty($key['id'])) { $where[] = ['id', '>', $key['id']]; } elseif (time() - $key['time'] < 300) { $where[] = ['received_at', '>', Carbon::parse($key['time'])]; } else { $where[] = ['received_at', '>', Carbon::now() - 60]; } } catch (\Throwable $th) { return abort(400); } $new_email_list = Mail::where($where) ->select([ 'id as key', 'from', 'to', 'title', 'received_at', 'created_at', ]) ->orderBy('received_at', 'asc') ->get(); $key = ['id' => $key['id'], 'time' => time()]; if ($new_email_list->count() > 0) { $key['id'] = $new_email_list->last()->key; } foreach ($new_email_list as $value) { $value->key = Crypt::encryptString($value->key); } return response()->json([ 'code' => 1, 'msg' => '', 'data' => [ 'list' => $new_email_list, 'new_key' => Crypt::encryptString(json_encode($key)), ], ]); } 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); } $to = strtolower($to); $from = strtolower($from); $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 = Carbon::parse($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); } if (empty(intval($id))) { 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>/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, ], ]); } }