false, 'message' => 'Admin access required']); exit; } header('Content-Type: application/json'); $purgeType = $_POST['type'] ?? $_GET['type'] ?? 'all'; $purgeUrl = $_POST['url'] ?? $_GET['url'] ?? ''; try { switch ($purgeType) { case 'all': // Purge all cache $cacheDir = '../data/images/cache/'; if (is_dir($cacheDir)) { $files = glob($cacheDir . '*'); foreach ($files as $file) { if (is_file($file)) { unlink($file); } } } // Send Varnish purge request $varnishHost = 'varnish'; $varnishPort = 80; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://{$varnishHost}:{$varnishPort}/"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PURGE'); curl_setopt($ch, CURLOPT_HTTPHEADER, ['X-Purge-Regex: .*']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_exec($ch); curl_close($ch); echo json_encode(['success' => true, 'message' => 'All cache purged successfully']); break; case 'image': if (empty($purgeUrl)) { throw new Exception('URL parameter required for image purge'); } // Purge specific image from cache $cacheKey = md5($purgeUrl); $cacheFile = '../data/images/cache/' . $cacheKey; if (file_exists($cacheFile)) { unlink($cacheFile); } // Send Varnish purge request for specific URL $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://varnish:80" . $purgeUrl); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PURGE'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_exec($ch); curl_close($ch); echo json_encode(['success' => true, 'message' => 'Image cache purged successfully']); break; case 'images': // Purge all image cache $cacheDir = '../data/images/cache/'; if (is_dir($cacheDir)) { $files = glob($cacheDir . '*'); foreach ($files as $file) { if (is_file($file)) { unlink($file); } } } // Send Varnish purge request for images $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://varnish:80/"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PURGE'); curl_setopt($ch, CURLOPT_HTTPHEADER, ['X-Purge-Regex: .*\.(jpg|jpeg|png|gif|webp|svg|ico)$']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_exec($ch); curl_close($ch); echo json_encode(['success' => true, 'message' => 'Image cache purged successfully']); break; default: throw new Exception('Invalid purge type'); } } catch (Exception $e) { http_response_code(500); echo json_encode(['success' => false, 'message' => $e->getMessage()]); } ?>