<?php
require_once '../config.php';

$pdo = new PDO(
    "mysql:host={$database['host']};dbname={$database['name']};charset=utf8",
    $database['user'],
    $database['password'],
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);

$favicon_tag = '<link rel="icon" type="image/svg+xml" href="/favicon.svg">' . "\n";

$stmt = $pdo->query("SELECT css FROM pp_layouts WHERE id = 2");
$layout = $stmt->fetch(PDO::FETCH_ASSOC);

if (strpos($layout['css'], 'favicon') !== false) {
    echo "Favicon already present.\n";
} else {
    $new_css = $favicon_tag . $layout['css'];
    $pdo->prepare("UPDATE pp_layouts SET css = ? WHERE id = 2")->execute([$new_css]);
    echo "Favicon link added.\n";
}

// Clear cache
$deleted = 0;
foreach (glob(__DIR__ . '/../temp/*') as $file) {
    if (is_file($file) && basename($file) !== 'cms_favicon.php') {
        unlink($file);
        $deleted++;
    }
}
echo "Cache cleared: $deleted files.\n";

unlink(__FILE__);
echo "Done.\n";
?>
