One Hat Cyber Team
Your IP :
104.23.243.58
Server IP :
104.21.51.23
Server :
Linux 128-201-239-36.cprapid.com 3.10.0-1160.41.1.el7.x86_64 #1 SMP Tue Aug 31 14:52:47 UTC 2021 x86_64
Server Software :
Apache
PHP Version :
7.4.33
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
home
/
juscatamarca
/
www
/
campusjxj
/
helpers
/
View File Name :
admin_users.php
<?php declare(strict_types=1); require_once ROOT_PATH . '/config/database.php'; /** * @return array<int, array<string, mixed>> */ function get_admin_users(?string $search = null, ?string $role = null, ?string $status = null): array { $conditions = ['1 = 1']; $params = []; if ($search !== null && trim($search) !== '') { $conditions[] = '(u.full_name LIKE :search OR u.email LIKE :search)'; $params[':search'] = '%' . trim($search) . '%'; } if ($role !== null && in_array($role, ['admin', 'student'], true)) { $conditions[] = 'u.role = :role'; $params[':role'] = $role; } if ($status !== null && in_array($status, ['active', 'inactive', 'blocked'], true)) { $conditions[] = 'u.status = :status'; $params[':status'] = $status; } $sql = " SELECT u.id, u.full_name, u.email, u.role, u.geographic_department_id, gd.name AS geographic_department_name, u.status, u.created_at, u.updated_at FROM users u LEFT JOIN geographic_departments gd ON gd.id = u.geographic_department_id WHERE " . implode(' AND ', $conditions) . " ORDER BY u.full_name ASC "; return db_fetch_all($sql, $params); } /** * @return array<string, mixed>|null */ function get_admin_user_by_id(int $id): ?array { $sql = " SELECT u.id, u.full_name, u.email, u.role, u.geographic_department_id, gd.name AS geographic_department_name, u.status, u.created_at, u.updated_at FROM users u LEFT JOIN geographic_departments gd ON gd.id = u.geographic_department_id WHERE u.id = :id LIMIT 1 "; return db_fetch_one($sql, [':id' => $id]); } function admin_user_exists(int $userId): bool { $sql = 'SELECT id FROM users WHERE id = :id LIMIT 1'; return db_fetch_one($sql, [':id' => $userId]) !== null; } function admin_email_exists(string $email, int $excludeId = 0): bool { if ($excludeId > 0) { $sql = ' SELECT id FROM users WHERE email = :email AND id <> :exclude_id LIMIT 1 '; return db_fetch_one($sql, [ ':email' => $email, ':exclude_id' => $excludeId, ]) !== null; } $sql = ' SELECT id FROM users WHERE email = :email LIMIT 1 '; return db_fetch_one($sql, [ ':email' => $email, ]) !== null; } /** * Split a full name string into first_name and last_name. * * @return array{first_name: string, last_name: string} */ function split_full_name(string $fullName): array { $parts = preg_split('/\s+/', trim($fullName), 2); return [ 'first_name' => (string) ($parts[0] ?? ''), 'last_name' => (string) ($parts[1] ?? ''), ]; } /** * @param array<string, mixed> $data */ function create_admin_user(array $data): bool { $passwordHash = password_hash((string) ($data['password'] ?? 'TempPassword123!'), PASSWORD_BCRYPT); $nameParts = split_full_name((string) ($data['full_name'] ?? '')); $sql = " INSERT INTO users ( first_name, last_name, email, password_hash, role, geographic_department_id, status ) VALUES ( :first_name, :last_name, :email, :password_hash, :role, :geographic_department_id, :status ) "; $geoDeptId = !empty($data['geographic_department_id']) ? (int) $data['geographic_department_id'] : null; return db_execute($sql, [ ':first_name' => $nameParts['first_name'], ':last_name' => $nameParts['last_name'], ':email' => (string) $data['email'], ':password_hash' => $passwordHash, ':role' => (string) ($data['role'] ?? 'student'), ':geographic_department_id' => $geoDeptId, ':status' => (string) ($data['status'] ?? 'active'), ]); } /** * @param array<string, mixed> $data */ function update_admin_user(int $id, array $data): bool { $nameParts = split_full_name((string) ($data['full_name'] ?? '')); $updates = [ 'first_name = :first_name', 'last_name = :last_name', 'email = :email', 'role = :role', 'geographic_department_id = :geographic_department_id', 'status = :status', 'updated_at = NOW()', ]; $sql = 'UPDATE users SET ' . implode(', ', $updates) . ' WHERE id = :id'; $geoDeptId = !empty($data['geographic_department_id']) ? (int) $data['geographic_department_id'] : null; $params = [ ':id' => $id, ':first_name' => $nameParts['first_name'], ':last_name' => $nameParts['last_name'], ':email' => (string) $data['email'], ':role' => (string) $data['role'], ':geographic_department_id' => $geoDeptId, ':status' => (string) $data['status'], ]; if (!empty($data['password'])) { $params[':password_hash'] = password_hash((string) $data['password'], PASSWORD_BCRYPT); $sql = 'UPDATE users SET first_name = :first_name, last_name = :last_name, email = :email, role = :role, geographic_department_id = :geographic_department_id, status = :status, password_hash = :password_hash, updated_at = NOW() WHERE id = :id'; } return db_execute($sql, $params); } function delete_admin_user(int $id): bool { $sql = 'DELETE FROM users WHERE id = :id'; return db_execute($sql, [':id' => $id]); } /** * @return array<int, array<string, mixed>> */ function get_geographic_departments_for_select(): array { $sql = " SELECT id, name FROM geographic_departments WHERE status = 'active' ORDER BY name ASC "; return db_fetch_all($sql); } /** * @return array{class: string, label: string} */ function admin_user_role_badge(string $role): array { switch ($role) { case 'admin': return ['class' => 'text-bg-danger', 'label' => 'Administrador']; case 'student': return ['class' => 'text-bg-primary', 'label' => 'Estudiante']; default: return ['class' => 'text-bg-light', 'label' => ucfirst($role)]; } } /** * @return array{class: string, label: string} */ function admin_user_status_badge(string $status): array { switch ($status) { case 'active': return ['class' => 'text-bg-success', 'label' => 'Activo']; case 'inactive': return ['class' => 'text-bg-secondary', 'label' => 'Inactivo']; case 'blocked': return ['class' => 'text-bg-danger', 'label' => 'Bloqueado']; default: return ['class' => 'text-bg-light', 'label' => ucfirst($status)]; } }