346 lines
11 KiB
PHP
346 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Login;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Mail\verificationMail;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use thiagoalessio\TesseractOCR\TesseractOCR;
|
|
use Intervention\Image\ImageManagerStatic as Image;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Laravolt\Indonesia\Models\City;
|
|
use Laravolt\Indonesia\Models\District;
|
|
use Laravolt\Indonesia\Models\Province;
|
|
use Laravolt\Indonesia\Models\Village;
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
class LoginController extends Controller
|
|
{
|
|
// /**
|
|
// * Create a new AuthController instance.
|
|
// *
|
|
// * @return void
|
|
// */
|
|
// public function __construct()
|
|
// {
|
|
// $this->middleware('auth:api', ['except' => ['login', 'authenticate', 'register', 'hai']]);
|
|
// }
|
|
|
|
public function login()
|
|
{
|
|
$provinces = Province::all();
|
|
$cities = City::all();
|
|
$districts = District::all();
|
|
$villages = Village::all();
|
|
return view('index');
|
|
}
|
|
|
|
/**
|
|
* Get a JWT via given credentials.
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function authenticate(Request $request)
|
|
{
|
|
// dd($request);
|
|
$credentials = $request->validate([
|
|
'email' => ['required', 'email'],
|
|
'password' => ['required', 'min:8'],
|
|
]);
|
|
|
|
if (Auth::attempt($credentials)) {
|
|
if (Auth::user()->status == 'Finished') {
|
|
$request->session()->regenerate();
|
|
if (ucwords(Auth::user()->role) == 'Admin') {
|
|
// return redirect()->route('admin.index');
|
|
return redirect()->intended('admin');
|
|
} else {
|
|
return redirect()->intended('user');
|
|
}
|
|
} else {
|
|
Session::flash('message', 'Akun tidak ditemukan atau sedang dalam pengajuan');
|
|
return redirect()->back();
|
|
}
|
|
}
|
|
|
|
return redirect()
|
|
->back()
|
|
->withErrors([
|
|
'email' => 'Email dengan' . $credentials['email'] . ' tidak tersedia.',
|
|
])
|
|
->onlyInput('email');
|
|
}
|
|
|
|
/**
|
|
* Log the user out (Invalidate the token).
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function logout(Request $request)
|
|
{
|
|
Auth::logout();
|
|
|
|
$request->session()->invalidate();
|
|
|
|
$request->session()->regenerateToken();
|
|
|
|
return redirect()->route('login');
|
|
}
|
|
|
|
public function register(Request $request)
|
|
{
|
|
// input
|
|
$nama_depan = $request->get('nama-depan');
|
|
$nama_belakang = $request->get('nama-belakang');
|
|
$tanggal_lahir = $request->get('tanggal-lahir');
|
|
$new_password = $request->get('new-password');
|
|
$nik = $request->get('nik');
|
|
$email = $request->get('email');
|
|
$nohp = $request->get('nohp');
|
|
$alamat = $request->get('alamat');
|
|
$foto_ktp = '';
|
|
$foto_wajah = '';
|
|
$persentase_kemiripan = '0%';
|
|
$gender = $request->get('gender');
|
|
$kode_kelurahan = $request->get('kode-kelurahan');
|
|
|
|
$ktpBase64 = $request->request->get('ktp');
|
|
$wajahBase64 = $request->request->get('wajah');
|
|
|
|
$validatedData['email_verified_at'] = now();
|
|
|
|
if ($ktpBase64 && $wajahBase64) {
|
|
// Konversi string Base64 ke file gambar
|
|
$ktpFile = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $ktpBase64));
|
|
$wajahFile = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $wajahBase64));
|
|
|
|
$foto_ktp = 'Foto-KTP-' . $nama_depan . '-' . $nama_belakang . '.jpg';
|
|
$foto_wajah = 'Foto-Wajah-' . $nama_depan . '-' . $nama_belakang . '.jpg';
|
|
|
|
file_put_contents(public_path('storage/foto-ktp/' . $foto_ktp), $ktpFile);
|
|
file_put_contents(public_path('storage/foto-wajah/' . $foto_wajah), $wajahFile);
|
|
}
|
|
|
|
//OCR
|
|
try {
|
|
$fotoKTP = public_path('storage/foto-ktp/' . $foto_ktp);
|
|
|
|
$image = Image::make($fotoKTP);
|
|
|
|
$image->greyscale(); // Convert to grayscale
|
|
$image->contrast(10); // Increase contrast, adjust the value as needed
|
|
|
|
$preprocessedfotoKTP = public_path('storage/preprocessed/preprocessed_image.jpg');
|
|
$image->save($preprocessedfotoKTP);
|
|
|
|
$result = (new TesseractOCR($preprocessedfotoKTP))->run();
|
|
|
|
// (5) Normalize
|
|
|
|
$lines = explode("\n", $result);
|
|
$namaOCR = '';
|
|
$nikOCR = '';
|
|
$nikInputan = $nik;
|
|
$namaInputan = $nama_depan.' '.$nama_belakang;
|
|
|
|
foreach ($lines as $line) {
|
|
// Mencari NIK
|
|
if (strpos($line, $nikInputan) !== false) {
|
|
$nikOCR = preg_replace('/[^0-9]/', '', $line);
|
|
}
|
|
|
|
// Mencari nama
|
|
if (strpos($line, $namaInputan) !== false) {
|
|
$namaOCR = trim(substr($line, strpos($line, ':') + 1));
|
|
}
|
|
}
|
|
|
|
//Selesai
|
|
|
|
$persentase_kemiripan = (similar_text($nikInputan, $nikOCR, $percent) + similar_text($namaOCR, $namaOCR, $percent))/2;
|
|
// $status = 'Progress';
|
|
|
|
// if (similar_text($nikInputan, $nikOCR, $percent) >= 70 && similar_text($namaOCR, $namaOCR, $percent) >= 70) {
|
|
// $status = 'Progress';
|
|
// } else {
|
|
// $status = 'Progress';
|
|
// }
|
|
|
|
} catch (\Exception $e) {
|
|
// $status = 'Progress';
|
|
}
|
|
//OCR
|
|
|
|
//Deteksi wajah belum
|
|
|
|
$password = Hash::make($new_password);
|
|
|
|
$result = User::create([
|
|
'id' => Uuid::uuid4(),
|
|
'nama_depan' => $nama_depan,
|
|
'nama_belakang' => $nama_belakang,
|
|
'tanggal_lahir' => $tanggal_lahir,
|
|
'email' => $email,
|
|
'email_verified_at' => now(),
|
|
'password' => $password,
|
|
'nohp' => $nohp,
|
|
'nik' => $nik,
|
|
'alamat' => $alamat,
|
|
'foto_ktp' => $foto_ktp,
|
|
'foto_wajah' => $foto_wajah,
|
|
'foto_profil' => null,
|
|
'persentase_kemiripan' => $persentase_kemiripan,
|
|
'gender' => $gender,
|
|
'kode_kelurahan' => $kode_kelurahan,
|
|
'remember_token' => Str::random(10),
|
|
]);
|
|
|
|
if ($result) {
|
|
return response()->json([
|
|
'status' => true,
|
|
'message' => 'Akun anda sudah terdaftar dan butuh verifikasi hingga maksimal 1 hari kerja',
|
|
]);
|
|
} else {
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => 'Akun anda gagal terdaftar. Coba lagi! ' + $result,
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function statusAkun($email)
|
|
{
|
|
$result = User::where('email', $email)->get();
|
|
if ($result->isNotEmpty()) {
|
|
return response()->json([
|
|
'status' => true,
|
|
'message' => $result,
|
|
]);
|
|
} else {
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => $result,
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function cariProvinsi()
|
|
{
|
|
$data = Province::where('name', 'LIKE', '%' . strtoupper(request('q')) . '%')->paginate(10);
|
|
|
|
return response()->json($data);
|
|
}
|
|
|
|
public function cariKota($code)
|
|
{
|
|
$data = City::where('province_code', $code)
|
|
->where('name', 'LIKE', '%' . strtoupper(request('q')) . '%')
|
|
->paginate(10);
|
|
|
|
return response()->json($data);
|
|
}
|
|
|
|
public function cariKecamatan($code)
|
|
{
|
|
$data = District::where('city_code', $code)
|
|
->where('name', 'LIKE', '%' . strtoupper(request('q')) . '%')
|
|
->paginate(10);
|
|
|
|
return response()->json($data);
|
|
}
|
|
|
|
public function cariKelurahan($code)
|
|
{
|
|
$data = Village::where('district_code', $code)
|
|
->where('name', 'LIKE', '%' . strtoupper(request('q')) . '%')
|
|
->paginate(10);
|
|
|
|
return response()->json($data);
|
|
}
|
|
|
|
public function kirimKodeVerifikasi(Request $request){
|
|
$email = $request->get('email');
|
|
$code = $request->get('code');
|
|
|
|
$verificationEmail = [
|
|
'code' => $code,
|
|
'email' => $email
|
|
];
|
|
try{
|
|
Mail::to($email)->send(new verificationMail($verificationEmail));
|
|
return response()->json([
|
|
'message' => 'Kode verifikasi berhasil dikirim ke email. Silahkan cek di email anda.',
|
|
'status' => true,
|
|
]);
|
|
|
|
}catch(\Exception $e){
|
|
return response()->json([
|
|
'message' => 'Kode verifikasi gagal dikirim ke email. '.$e,
|
|
'status' => false,
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function getOcr(){
|
|
//OCR
|
|
// dd(phpinfo());
|
|
try {
|
|
$fotoKTP = public_path('storage/foto-ktp/ktp.jpg');
|
|
|
|
$image = Image::make($fotoKTP);
|
|
|
|
$image->greyscale(); // Convert to grayscale
|
|
$image->contrast(10); // Increase contrast, adjust the value as needed
|
|
|
|
$preprocessedfotoKTP = public_path('storage/preprocessed/preprocessed_image.jpg');
|
|
$image->save($preprocessedfotoKTP);
|
|
|
|
$result = (new TesseractOCR($preprocessedfotoKTP))->run();
|
|
|
|
// (5) Normalize
|
|
|
|
$lines = explode("\n", $result);
|
|
$nikOCR = '';
|
|
$namaOCR = '';
|
|
$nikInputan = '3471140209790001';
|
|
$namaInputan = 'RIYANTO. SE';
|
|
|
|
foreach ($lines as $line) {
|
|
// Mencari NIK
|
|
if (strpos($line, $nikInputan) !== false) {
|
|
$nikOCR = preg_replace('/[^0-9]/', '', $line);
|
|
}
|
|
|
|
// Mencari nama
|
|
if (strpos($line, $namaInputan) !== false) {
|
|
$namaOCR = trim(substr($line, strpos($line, ':') + 1));
|
|
}
|
|
}
|
|
|
|
//Selesai
|
|
|
|
$persentase_kemiripan = (similar_text($nikInputan, $nikOCR, $percent) + similar_text($namaOCR, $namaOCR, $percent))/2;
|
|
// $status = 'Progress';
|
|
|
|
dd([$persentase_kemiripan, $lines]);
|
|
|
|
// if (similar_text($nikInputan, $nikOCR, $percent) >= 70 && similar_text($namaOCR, $namaOCR, $percent) >= 70) {
|
|
// $status = 'Progress';
|
|
// } else {
|
|
// $status = 'Progress';
|
|
// }
|
|
|
|
} catch (\Exception $e) {
|
|
// $status = 'Progress';
|
|
dd($e);
|
|
}
|
|
//OCR
|
|
}
|
|
}
|