// // RESPONSE // {"ok":true,"error":null} // {"ok":false,"error":"POST only"|"scripts_dir is required"|"Directory does not exist" // |"Not a Varaverk checkout — common.sh and load_config.sh not found" // |"Failed to write cfg file"} // // DEPENDS ON // include/config.php PLUGIN_CFG // ═══════════════════════════════════════════════════════════════════════════════════════════════ header('Content-Type: application/json'); require_once dirname(__DIR__) . '/include/config.php'; if ($_SERVER['REQUEST_METHOD'] !== 'POST') { echo json_encode(['ok' => false, 'error' => 'POST only']); exit; } $scriptsDir = trim($_POST['scripts_dir'] ?? ''); if (!$scriptsDir) { echo json_encode(['ok' => false, 'error' => 'scripts_dir is required']); exit; } if (!is_dir($scriptsDir)) { echo json_encode(['ok' => false, 'error' => 'Directory does not exist']); exit; } // Pointing SCRIPTS_DIR at an arbitrary directory leaves every conf and script path in the // plugin resolving under something that holds none of them. These two files are sourced by // every script in the repo, so their presence is what makes a directory a checkout. if (!file_exists($scriptsDir . '/common.sh') || !file_exists($scriptsDir . '/load_config.sh')) { echo json_encode([ 'ok' => false, 'error' => 'Not a Varaverk checkout — common.sh and load_config.sh not found', ]); exit; } $cfgFile = PLUGIN_CFG; $cfgDir = dirname($cfgFile); if (!is_dir($cfgDir)) mkdir($cfgDir, 0755, true); // Merge, do not author. varaverk.cfg also carries the Gitea coordinates git_pull_execute.sh // needs — rewriting the file with SCRIPTS_DIR alone would silently break the daily pull. $cfg = @parse_ini_file($cfgFile) ?: []; $cfg['SCRIPTS_DIR'] = $scriptsDir; $content = ''; foreach ($cfg as $k => $v) { $content .= $k . '="' . addslashes((string)$v) . '"' . "\n"; } $tmp = $cfgFile . '.vv.tmp'; $ok = file_put_contents($tmp, $content) !== false && rename($tmp, $cfgFile); if (!$ok) @unlink($tmp); echo json_encode(['ok' => $ok, 'error' => $ok ? null : 'Failed to write cfg file']);