Panel/modules/steam_workshop/controllers/WorkshopProfileController.php
copilot-swe-agent[bot] 8eff063a93
feat: add database-driven Steam Workshop system
- Create 3 new DB tables: workshop_game_profiles, workshop_cache, server_workshop_mods
- Add WorkshopRepository (DB access layer for all 3 tables)
- Add WorkshopInstaller (rsync/robocopy/custom_script copy logic, SteamCMD download via agent exec)
- Add WorkshopUpdater (scheduled cache update functions grouped by agent)
- Add WorkshopPreStart (pre-start mod sync helper)
- Add WorkshopProfileController (admin CRUD for profiles)
- Add WorkshopModController (user install/remove/toggle/load_order/sync)
- Add admin views: profiles list + profile_form
- Add user views: user_workshop_index + user_workshop_mods
- Add cron_update.php CLI entry point (--all/--agent-id/--home-id/--profile-id/--workshop-id)
- Add prestart_sync.php CLI helper for XML pre_start hook
- Update workshop_admin.php to route to profile management
- Update main.php to route to new mod management (legacy fallback preserved)
- Update module.php with DB migration SQL and version bump to 2.1
- Update lang/en_US.php with all new strings

Agent-Logs-Url: https://github.com/GameServerPanel/GSP/sessions/dbeebd0e-e7a5-469d-8a8c-e63193d1ebb0

Co-authored-by: iaretechnician <2749183+iaretechnician@users.noreply.github.com>
2026-04-30 18:01:33 +00:00

221 lines
7.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
/*
* OGP / GSP Steam Workshop
* WorkshopProfileController: admin CRUD for Workshop game profiles
* (gsp_workshop_game_profiles table).
*
* Routed via workshop_admin.php:
* ?m=steam_workshop&p=workshop_admin&sw_action=profiles → list
* ?m=steam_workshop&p=workshop_admin&sw_action=profile_form → create/edit
* POST sw_action=profile_save → save
* POST sw_action=profile_delete → delete
*/
require_once __DIR__ . '/../lib/WorkshopRepository.php';
class WorkshopProfileController
{
private WorkshopRepository $repo;
private array $lang;
public function __construct(OGPDatabase $db)
{
$this->repo = new WorkshopRepository($db);
$this->lang = $this->loadLang();
}
// ------------------------------------------------------------------
// Dispatch
// ------------------------------------------------------------------
public function handle(): void
{
echo '<link rel="stylesheet" type="text/css" href="modules/steam_workshop/steam_workshop.css" />';
$action = $_GET['sw_action'] ?? 'profiles';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$postAction = $_POST['sw_action'] ?? '';
switch ($postAction) {
case 'profile_save':
$this->handleSave();
return;
case 'profile_delete':
$this->handleDelete();
return;
}
}
switch ($action) {
case 'profile_form':
$this->handleForm((int)($_GET['profile_id'] ?? 0));
break;
default:
$this->handleList();
break;
}
}
// ------------------------------------------------------------------
// Actions
// ------------------------------------------------------------------
private function handleList(): void
{
$profiles = $this->repo->listProfiles();
$this->render('admin/profiles', [
'lang' => $this->lang,
'profiles' => $profiles,
]);
}
private function handleForm(int $profileId): void
{
$profile = $profileId > 0 ? $this->repo->getProfileById($profileId) : null;
$this->render('admin/profile_form', [
'lang' => $this->lang,
'profile' => $profile,
'profileId' => $profileId,
]);
}
private function handleSave(): void
{
$id = (int)($_POST['profile_id'] ?? 0);
$data = $this->extractProfileData($_POST);
$errors = $this->validateProfileData($data);
if (!empty($errors)) {
foreach ($errors as $err) {
print_failure($err);
}
$profile = $id > 0 ? $this->repo->getProfileById($id) : null;
$this->render('admin/profile_form', [
'lang' => $this->lang,
'profile' => array_merge($profile ?? [], $data, ['id' => $id]),
'profileId' => $id,
]);
return;
}
$data['id'] = $id;
$savedId = $this->repo->saveProfile($data);
if ($savedId > 0) {
print_success($this->lang['profile_saved'] ?? 'Workshop profile saved.');
} else {
print_failure($this->lang['profile_save_error'] ?? 'Failed to save Workshop profile.');
}
$this->handleList();
}
private function handleDelete(): void
{
$id = (int)($_POST['profile_id'] ?? 0);
if ($id <= 0) {
print_failure($this->lang['profile_not_found'] ?? 'Profile not found.');
$this->handleList();
return;
}
if ($this->repo->deleteProfile($id)) {
print_success($this->lang['profile_deleted'] ?? 'Workshop profile deleted.');
} else {
print_failure($this->lang['profile_delete_error'] ?? 'Failed to delete Workshop profile.');
}
$this->handleList();
}
// ------------------------------------------------------------------
// Input helpers
// ------------------------------------------------------------------
/**
* @param array<string,mixed> $post
* @return array<string,mixed>
*/
private function extractProfileData(array $post): array
{
// supported_os can be multiple values (SET type)
$osRaw = $post['supported_os'] ?? [];
if (!is_array($osRaw)) {
$osRaw = [$osRaw];
}
$allowedOs = ['linux', 'windows'];
$osValues = array_values(array_intersect($osRaw, $allowedOs));
$supportedOs = implode(',', $osValues !== [] ? $osValues : ['linux']);
$allowedMethods = ['rsync', 'robocopy', 'custom_script'];
$copyMethod = in_array($post['copy_method'] ?? '', $allowedMethods, true)
? (string)$post['copy_method']
: 'rsync';
return [
'game_key' => trim((string)($post['game_key'] ?? '')),
'game_name' => trim((string)($post['game_name'] ?? '')),
'workshop_app_id' => preg_replace('/[^0-9]/', '', (string)($post['workshop_app_id'] ?? '')) ?? '',
'supported_os' => $supportedOs,
'cache_path_template' => trim((string)($post['cache_path_template'] ?? '')),
'install_path_template' => trim((string)($post['install_path_template'] ?? '')),
'folder_name_template' => trim((string)($post['folder_name_template'] ?? '@{mod_id}')),
'copy_method' => $copyMethod,
'install_script' => trim((string)($post['install_script'] ?? '')),
'config_file_template' => trim((string)($post['config_file_template'] ?? '')),
'launch_param_template' => trim((string)($post['launch_param_template'] ?? '')),
'requires_restart' => !empty($post['requires_restart']) ? 1 : 0,
'enabled' => !empty($post['enabled']) ? 1 : 0,
];
}
/**
* @param array<string,mixed> $data
* @return list<string>
*/
private function validateProfileData(array $data): array
{
$errors = [];
if (($data['game_key'] ?? '') === '') {
$errors[] = $this->lang['error_game_key_required'] ?? 'Game key is required.';
} elseif (!preg_match('/^[a-z0-9_\-.]+$/i', (string)$data['game_key'])) {
$errors[] = $this->lang['error_game_key_invalid'] ?? 'Game key may only contain letters, digits, underscores, dots, and hyphens.';
}
if (($data['game_name'] ?? '') === '') {
$errors[] = $this->lang['error_game_name_required'] ?? 'Game name is required.';
}
if (($data['workshop_app_id'] ?? '') === '') {
$errors[] = $this->lang['error_app_id_required'] ?? 'Workshop App ID is required.';
}
if (($data['cache_path_template'] ?? '') === '') {
$errors[] = $this->lang['error_cache_path_required'] ?? 'Cache path template is required.';
}
if (($data['install_path_template'] ?? '') === '') {
$errors[] = $this->lang['error_install_path_required'] ?? 'Install path template is required.';
}
return $errors;
}
// ------------------------------------------------------------------
// Rendering
// ------------------------------------------------------------------
private function render(string $view, array $data = []): void
{
extract($data);
require __DIR__ . '/../views/' . $view . '.php';
}
private function loadLang(): array
{
$file = __DIR__ . '/../lang/en_US.php';
if (is_file($file)) {
$strings = require $file;
if (is_array($strings)) {
return $strings;
}
}
return [];
}
}