« Back to History
enroll_face.php
|
20260721_154032.php
Initial Bulk Import
Copy Code
<?php // /erp/api/attendance/enroll_face.php // Enroll face samples for an employee (expects JSON POST with employee_id and images array) // Auth + DB bootstrap adapted to your ERP (require_login + auth_user + $pdo) header('Content-Type: application/json; charset=utf-8'); try { // bootstrap auth + db (match payment.php / other working pages) require_once __DIR__ . '/../../modules/auth/auth.php'; require_login(); $u = auth_user(); $company_id = (int)($u['company_id'] ?? 0); $user_id = (int)($u['id'] ?? 0); // DB (use existing $GLOBALS['pdo'] if present, else require core/db.php) $pdo = $GLOBALS['pdo'] ?? null; if (!$pdo) { require_once __DIR__ . '/../../core/db.php'; $pdo = $GLOBALS['pdo'] ?? null; } if (!$pdo) throw new Exception('Database connection not available'); // Read JSON input $raw = json_decode(file_get_contents('php://input'), true); if (!$raw) $raw = $_POST; // fallback $employee_id = isset($raw['employee_id']) ? (int)$raw['employee_id'] : 0; $device_id = trim($raw['device_id'] ?? ($_SERVER['HTTP_USER_AGENT'] ?? 'web')); $images = $raw['images'] ?? null; if ($employee_id <= 0) { http_response_code(400); echo json_encode(['error'=>'employee_id required']); exit; } if (empty($images) || !is_array($images)) { http_response_code(400); echo json_encode(['error'=>'images (array) required']); exit; } // Save images to uploads folder $saved_files = []; $base_dir = __DIR__ . '/../../uploads/attendance_photos/' . $company_id . '/enroll/' . $employee_id; if (!is_dir($base_dir)) { if (!@mkdir($base_dir, 0755, true)) { // Try to log and return error error_log("enroll_face: mkdir failed for $base_dir"); http_response_code(500); echo json_encode(['error'=>'unable_to_create_directory']); exit; } } foreach ($images as $img) { // expect dataURL or raw base64 $img = trim($img); $data = preg_replace('#^data:image/[^;]+;base64,#', '', $img); $bin = base64_decode($data); if ($bin === false) continue; $fname = uniqid('enr_') . '.jpg'; $fpath = $base_dir . '/' . $fname; $ok = @file_put_contents($fpath, $bin); if ($ok === false) { error_log("enroll_face: file_put_contents failed for $fpath"); continue; } // Save web-accessible relative path $saved_files[] = 'uploads/attendance_photos/' . $company_id . '/enroll/' . $employee_id . '/' . $fname; } if (empty($saved_files)) { http_response_code(500); echo json_encode(['error'=>'no_files_saved']); exit; } // Create a template_id (could be vendor id later) $template_id = bin2hex(random_bytes(12)); $template_type = 'local'; $meta = ['files'=>$saved_files, 'device'=>$device_id, 'enrolled_by'=>$user_id]; // Upsert into fc_face_templates (company_id + employee_id unique) $sql = "INSERT INTO fc_face_templates (company_id, employee_id, template_id, template_type, enroll_device, meta, active, enrolled_at) VALUES (:cid, :eid, :tid, :ttype, :device, :meta, 1, NOW()) ON DUPLICATE KEY UPDATE template_id = VALUES(template_id), template_type = VALUES(template_type), enroll_device = VALUES(enroll_device), meta = VALUES(meta), active = 1, enrolled_at = NOW()"; $st = $pdo->prepare($sql); $st->execute([ ':cid'=>$company_id, ':eid'=>$employee_id, ':tid'=>$template_id, ':ttype'=>$template_type, ':device'=>$device_id, ':meta'=>json_encode($meta) ]); echo json_encode(['ok'=>1, 'template_id'=>$template_id, 'files'=>$saved_files]); } catch (Throwable $e) { // Log server error error_log("enroll_face.php error: " . $e->getMessage()); http_response_code(500); echo json_encode(['error'=>'internal', 'msg'=>$e->getMessage()]); }