Files
myProfile/api/profile.php
T

122 lines
4.2 KiB
PHP

<?php
require_once 'config.php';
require_once __DIR__ . '/error_config.php';
start_secure_session();
set_json_headers();
$method = $_SERVER['REQUEST_METHOD'];
// =====================================================
// 헬퍼: 기존 퍼센트 기반 스킬을 카테고리 기반으로 마이그레이션
// =====================================================
function migrate_skills($profile) {
if (!isset($profile['skills']) || !is_array($profile['skills'])) {
$profile['skills'] = [];
return $profile;
}
// 이미 카테고리 형식인지 확인 (첫 항목에 'items' 있으면 새 형식)
$first = $profile['skills'][0] ?? null;
if ($first && isset($first['category']) && isset($first['items'])) {
return $profile; // 이미 새 형식
}
// 기존 형식 (name + level) → 카테고리 형식으로 변환
if ($first && isset($first['name']) && isset($first['level'])) {
$migrated = [
[
'category' => '기술',
'items' => array_map(fn($s) => $s['name'] ?? '', $profile['skills'])
]
];
$profile['skills'] = $migrated;
}
return $profile;
}
// =====================================================
// GET: 프로필 조회 (인증 불필요)
// =====================================================
if ($method === 'GET') {
$profile = read_json_safe(PROFILE_FILE);
if ($profile === null) {
json_response(['error' => '프로필을 읽을 수 없습니다'], 500);
}
$profile = migrate_skills($profile);
json_response($profile);
}
// 이하 수정은 인증 필요
require_auth();
require_csrf();
// =====================================================
// PUT: 프로필 전체 업데이트
// =====================================================
if ($method === 'PUT') {
$input = get_json_input();
if (!is_array($input)) {
json_response(['error' => '잘못된 데이터 형식'], 400);
}
$current = read_json_safe(PROFILE_FILE) ?? [];
$updated = array_merge($current, [
'name' => trim($input['name'] ?? $current['name'] ?? ''),
'title' => trim($input['title'] ?? $current['title'] ?? ''),
'tagline' => trim($input['tagline'] ?? $current['tagline'] ?? ''),
'avatar' => clean_public_url($input['avatar'] ?? $current['avatar'] ?? ''),
'bio' => trim($input['bio'] ?? $current['bio'] ?? ''),
'location' => trim($input['location'] ?? $current['location'] ?? ''),
'email' => trim($input['email'] ?? $current['email'] ?? ''),
]);
// 카테고리화된 스킬 처리
if (isset($input['skills']) && is_array($input['skills'])) {
$cleanSkills = [];
foreach ($input['skills'] as $cat) {
if (!isset($cat['category']) || !isset($cat['items'])) continue;
$catName = trim($cat['category']);
if ($catName === '') continue;
$items = is_array($cat['items'])
? array_values(array_filter(array_map('trim', $cat['items']), fn($v) => $v !== ''))
: [];
if (count($items) > 0) {
$cleanSkills[] = [
'category' => $catName,
'items' => $items
];
}
}
$updated['skills'] = $cleanSkills;
}
if (isset($input['timeline']) && is_array($input['timeline'])) {
$updated['timeline'] = array_values(array_filter($input['timeline'], function($t) {
return !empty($t['title']);
}));
}
if (isset($input['social']) && is_array($input['social'])) {
$social = array_merge($current['social'] ?? [], $input['social']);
foreach (['github', 'linkedin', 'blog'] as $key) {
if (isset($social[$key])) {
$social[$key] = clean_public_url($social[$key]);
}
}
$updated['social'] = $social;
}
if (write_json_safe(PROFILE_FILE, $updated)) {
json_response(['success' => true, 'profile' => $updated]);
} else {
json_response(['error' => '저장에 실패했습니다'], 500);
}
}
json_response(['error' => 'Method not allowed'], 405);