Page 1 of 1

Deleting the xhstyles.css file via the backend

Posted: Thu Dec 24, 2020 11:36 am
by frase
It is always necessary to delete the xhstyles.css file manually, just when you work with new plugins or core styles and especially when you restore an older version.

Should there be a way to delete this file via the backend? If so - where?

Re: Deleting the xhstyles.css file via the backend

Posted: Thu Dec 24, 2020 3:07 pm
by Tata
frase wrote:
Thu Dec 24, 2020 11:36 am
Should there be a way to delete this file via the backend? If so - where?
If the necessity to delete the file is a standard, deletion may be hardcoded in functions.php directly after any new plugin is installed/removed, core styles are manipulated, or an older version is restored(?).

Re: Deleting the xhstyles.css file via the backend

Posted: Thu Dec 24, 2020 3:27 pm
by cmb
I agree with Tata. Maybe replace XH_pluginStylesheet with the following:

Code: Select all

function XH_pluginStylesheet()
{
    global $pth;

    $plugins = XH_plugins();

    // create array of pluginname => hash of CSS file contents
    $hashes = ['core' => sha1_file($pth['file']['corestyle'])];
    foreach ($plugins as $plugin) {
        $fn = $pth['folder']['plugins'] . $plugin . '/css/stylesheet.css';
        if (is_file($fn)) {
            $hashes[$plugin] = sha1_file($fn);
        } else {
            $hashes[$plugin] = '';
        }
    }

    $ofn = $pth['folder']['corestyle'] . 'xhstyles.css';
    $expired = !file_exists($ofn);

    // check for newly (un)installed plugins and changes in the individual plugin stylesheets
    if (!$expired) {
        if (($ofp = fopen($ofn, 'r')) !== false
            && fgets($ofp) && fgets($ofp)
            && ($oldPlugins = fgets($ofp))
        ) {
            $oldPlugins = explode(',', trim($oldPlugins, " *\r\n"));
            $oldhashes = [];
            foreach ($oldPlugins as $oldPlugin) {
                list($plugin, $hash) = explode(':', $oldPlugin);
                $oldhashes[$plugin] = $hash;
            }
            $expired = $hashes != $oldhashes;
        } else {
            $expired = true;
        }
        if ($ofp !== false) {
            fclose($ofp);
        }
    }

    // create combined plugin stylesheet
    if ($expired) {
        var_dump("expired");
        $o = array(
            PHP_EOL . '/' . str_pad(' ' . $pth['file']['corestyle'], 76, '*', STR_PAD_LEFT) . ' */'
            . PHP_EOL . PHP_EOL . file_get_contents($pth['file']['corestyle'])
        );
        foreach ($plugins as $plugin) {
            $fn = $pth['folder']['plugins'] . $plugin . '/css/stylesheet.css';
            if (file_exists($fn)) {
                $css = file_get_contents($fn);
                if (substr($css, 0, 3) === "\xEF\xBB\xBF") {
                    $css = substr($css, 3);
                }
                $css = XH_adjustStylesheetURLs($plugin, $css);
                $css = PHP_EOL
                    . '/' . str_pad(' ' . $fn, 76, '*', STR_PAD_LEFT) . ' */'
                    . PHP_EOL . PHP_EOL . $css;
                $o[] = $css;
            }
        }
        $pluginline = '';
        foreach ($hashes as $plugin => $hash) {
            if ($pluginline) {
                $pluginline .= ',';
            }
            $pluginline .= "$plugin:$hash";
        }
        $o = '/*' . PHP_EOL
            . ' * Automatically created by CMSimple_XH. DO NOT MODIFY!' . PHP_EOL
            . ' * ' . $pluginline . PHP_EOL
            . ' */' . PHP_EOL . PHP_EOL
            . implode(PHP_EOL . PHP_EOL, $o);
        if (!XH_writeFile($ofn, $o)) {
            e('cntwriteto', 'stylesheet', $ofn);
        }
    }

    return $ofn;
}
This does no longer check for the file modification time, but rather calculates SHA1 hashes, and compares these. It is likely a bit slower than the old code, but I guess that is acceptable.

Re: Deleting the xhstyles.css file via the backend

Posted: Fri Dec 25, 2020 8:31 am
by frase
cmb wrote:
Thu Dec 24, 2020 3:27 pm
This does no longer check for the file modification time, but rather calculates SHA1 hashes, and compares these. It is likely a bit slower than the old code, but I guess that is acceptable.
It seems to me that this hash method is more effective than just comparing modification time.
Besides, it actually makes the work easier - you don't have to shimmy through folders all the time.
I would be in favor of adopting this in one of the next XH versions.

Re: Deleting the xhstyles.css file via the backend

Posted: Fri Dec 25, 2020 8:37 am
by olape
Well then 1.7.5