first(); $smtp = Setting::where('key', 'smtp')->first(); $settingValue = $setting ? json_decode((string) $setting->value, true) : []; $smtpValue = $smtp ? json_decode((string) $smtp->value, true) : []; // Never expose raw smtp password if (isset($smtpValue['password']) && !empty($smtpValue['password'])) { $smtpValue['password'] = '********'; } return response()->json([ 'error' => false, 'data' => [ 'setting' => $settingValue, 'smtp' => $smtpValue, ], ]); } /** * PUT /administrator/settings * Accepts: { setting: {...}, smtp: {...} } */ public function update(Request $request): JsonResponse { $validator = Validator::make($request->all(), [ 'setting' => 'sometimes|array', 'setting.api_inet' => 'sometimes|string|nullable', 'smtp' => 'sometimes|array', 'smtp.host' => 'sometimes|string', 'smtp.port' => 'sometimes|integer', 'smtp.user' => 'sometimes|email', 'smtp.encryption' => 'sometimes|in:ssl,tls,starttls,', 'smtp.from_name' => 'sometimes|string|nullable', 'smtp.from_address' => 'sometimes|email|nullable', ]); if ($validator->fails()) { return response()->json(['error' => true, 'message' => $validator->errors()->first()], 422); } if ($request->has('setting')) { $current = Setting::where('key', 'setting')->first(); $existing = $current ? json_decode((string) $current->value, true) : []; $merged = array_merge($existing, $request->input('setting')); Setting::updateOrCreate( ['key' => 'setting'], ['value' => json_encode($merged, JSON_UNESCAPED_UNICODE)] ); } if ($request->has('smtp')) { $smtpData = $request->input('smtp'); $current = Setting::where('key', 'smtp')->first(); $existing = $current ? json_decode((string) $current->value, true) : []; // Keep existing password if placeholder sent (not changed) if (isset($smtpData['password']) && $smtpData['password'] === '********') { $smtpData['password'] = $existing['password'] ?? ''; } elseif (isset($smtpData['password']) && !empty($smtpData['password'])) { $smtpData['password'] = SignRSA::EncryptFunc($smtpData['password']); } $merged = array_merge($existing, $smtpData); Setting::updateOrCreate( ['key' => 'smtp'], ['value' => json_encode($merged, JSON_UNESCAPED_UNICODE)] ); // Flush mail config cache so new SMTP settings take effect immediately Cache::forget(CacheKey::build('settings', 'mail')); } return response()->json([ 'error' => false, 'message' => 'Cập nhật cài đặt thành công.', ]); } /** * POST /administrator/settings/test-telegram * Sends a test message using the currently saved Telegram config. */ public function testTelegram(): JsonResponse { $response = (new Telegram())->SendMessage('🔔 Đây là tin nhắn test từ Portal Admin — cấu hình Telegram đang hoạt động!'); if (!$response || !$response->successful()) { return response()->json([ 'error' => true, 'message' => $response ? 'Telegram trả về lỗi: ' . $response->body() : 'Chưa cấu hình đủ Bot Token / Bot ID.', ], 422); } return response()->json(['error' => false, 'message' => 'Đã gửi tin nhắn test thành công.']); } public function uploadMedia(Request $request): JsonResponse { $request->validate([ 'file' => 'required|file|mimes:jpg,jpeg,png,webp,svg|max:5120', ]); $path = $request->file('file')->store('system-config', 'public'); return response()->json([ 'error' => false, 'url' => Storage::disk('public')->url($path), 'path' => $path, ]); } }