#!/usr/bin/php-cgi -dcgi.force_redirect=0
<?php
# encoding: utf-8
# api: cgi
# type: config
# category: admin
# title: Skin editor
# description: Edit current skin parts all at once
# version: 0.6
# state: beta
# depends: php:sqlite
# config: -
#
# Combines header/footer/css/script editing,
# updates current=draft1 directly, backs up into draft9.
#
# Permits any user listed in 'draft1-users' setting.
#


#-- init
header("Content-Type: text/html; inline");
ini_set("display_errors", 1);
if (!is_admin() && !in_array($_SERVER["FOSSIL_USER"], allowed_users())) {
    die("Admin or skin editors only");
}
$h = "htmlentities";
$FIELDS = ['header', 'footer', 'css', 'js'];

#-- database (== fossil repo)
function db($sql="", $params=[]) {
    global $db;
    if (empty($db)) {
        $db = new PDO("sqlite::memory:");
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
        $db->query("ATTACH DATABASE '$_SERVER[FOSSIL_REPOSITORY]' AS 'repo'");
    }
    if ($params) {
        $stmt = $db->prepare($sql);
        $stmt->execute($params);
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
    else {
        return $db->query($sql);
    }
}

# user checks
function allowed_users() {
    $r = db("select * from config where name=?", ['draft1-users']);
    if ($r) {
        return str_getcsv($r[0]["value"]);
    }
    return [];
}
function is_admin() {
    return strpos($_SERVER["FOSSIL_CAPABILITIES"], "s") !== false;
}

# REPLACE list (SQL blob) from current skin parts
function sql_backup($s="") {
    global $db;
    foreach (db("SELECT * FROM config WHERE name IN ('header', 'footer', 'css', 'js', 'details')") as $r) {
        $s .= "REPLACE INTO config (name,mtime,value) VALUES ('$r[name]', $r[mtime], {$db->quote($r['value'])});\n";
    }
    return $s;
}


#-- backup download
if (isset($_GET["backup"])) {
    header("Content-Type: text/sql");
    header("Content-Disposition: attachment; filename=fossil-skin-backup-current.txt");
    die(sql_backup());
}
#-- update
elseif (isset($_POST["save"])) {

    # insert skin:Backup*
    if ($_POST["save_bk"]) {
        db("INSERT INTO config (name,mtime,value) VALUES (?, ?, ?)", ["skin:Backup on ".strftime("%Y-%m-%d %H:%M:%S"), time(), sql_backup()]);
    }
    
    # update individual config skin parts
    foreach ($FIELDS AS $field) {
        $old = db("SELECT value FROM config WHERE name=?", [$field])[0]["value"];
        if ($_POST["save_d9"]) {
            db("REPLACE INTO config (name,mtime,value) VALUES (?, ?, ?)", ["draft9-$field", time(), $old]);
        }
        if ($_POST["save_d1"]) {
            db("REPLACE INTO config (name,mtime,value) VALUES (?, ?, ?)", ["draft1-$field", time(), $_POST[$field]]);
        }
        if (TRUE) {
            db("REPLACE INTO config (name,mtime,value) VALUES (?, ?, ?)", ["$field", time(), $_POST[$field]]);
        }
    }
    print "<div class='fossil-doc' data-title='skin saved'> 🟩 Saved.";

}
#-- display form
else {

    ?>
    <div class='fossil-doc' data-title='skin editor'>
    <style>
     .skin-editor { display: block; width: 1000pt; }
     .skin-editor input[type=submit] { margin: 10pt; }
     .skin-block { display: inline-block; background: #eee; border-radius: 10pt; padding: 5pt; }
     .skin-block b { display: block; }
    </style>
    <form method=POST enctype="multipart/form-data" class=skin-editor>
    <h3>Skin Editor</h3>
    <input type=submit name=save value='save &amp; apply'>
    <br>
    <?php

    foreach ($FIELDS as $field) {
        $value = db("SELECT name,value FROM config WHERE name = ?", [$field])[0]["value"];
        print <<<EOF
        <label class=skin-block>
           <b>{$field}</b>
           <textarea cols=55 rows=20 name={$field}>{$h($value)}</textarea>
        </label>   
EOF;
    }

    ?>
    <br><input type=submit name=save value='save &amp; apply'><br>
    This will update <b>current</b> skin parts (= directly published).<br>
    <input name=save_d1 value=1 type=checkbox checked> Overwrite <b>draft1</b> with current parts too.<br>
    <input name=save_d9 value=1 type=checkbox checked> Save the previous version to <a href="<?=$_SERVER["FOSSIL_URI"]?>/draft9/home">draft9</a><br>
    <input name=save_bk value=1 type=checkbox checked> But also create a skin:Backup entry<br>
    </form>
    <br><b>Other options</b>
    <ul>
    <li> <a href="<?=$_SERVER["PHP_SELF"]?>?backup=current.txt">backup.sql</a> of current skin
    <li> <a href="<?=$_SERVER["FOSSIL_URI"]?>/setup_skin_admin">skin archive/admin</a>
    <li> <a href="<?=$_SERVER["FOSSIL_URI"]?>/setup_skin">setup_skin</a>
    <li> <a href="<?=$_SERVER["FOSSIL_URI"]?>/setup_skinedit?w=2&sk=1">edit draft1</a>
    <li> Publish to remote repo: <code>fossil config push skin</code>
    </ul>
    <?php
}

?>
</div>
